Line data Source code
1 : use std::{
2 : borrow::Cow,
3 : cmp::Ordering,
4 : collections::{BTreeMap, HashMap, HashSet},
5 : path::PathBuf,
6 : str::FromStr,
7 : sync::Arc,
8 : time::{Duration, Instant},
9 : };
10 :
11 : use crate::{
12 : background_node_operations::{
13 : Drain, Fill, Operation, OperationError, OperationHandler, MAX_RECONCILES_PER_OPERATION,
14 : },
15 : compute_hook::NotifyError,
16 : id_lock_map::{trace_exclusive_lock, trace_shared_lock, IdLockMap, WrappedWriteGuard},
17 : persistence::{AbortShardSplitStatus, TenantFilter},
18 : reconciler::{ReconcileError, ReconcileUnits},
19 : scheduler::{MaySchedule, ScheduleContext, ScheduleMode},
20 : tenant_shard::{
21 : MigrateAttachment, ReconcileNeeded, ReconcilerStatus, ScheduleOptimization,
22 : ScheduleOptimizationAction,
23 : },
24 : };
25 : use anyhow::Context;
26 : use control_plane::storage_controller::{
27 : AttachHookRequest, AttachHookResponse, InspectRequest, InspectResponse,
28 : };
29 : use diesel::result::DatabaseErrorKind;
30 : use futures::{stream::FuturesUnordered, StreamExt};
31 : use itertools::Itertools;
32 : use pageserver_api::{
33 : controller_api::{
34 : NodeAvailability, NodeRegisterRequest, NodeSchedulingPolicy, PlacementPolicy,
35 : ShardSchedulingPolicy, TenantCreateResponse, TenantCreateResponseShard,
36 : TenantDescribeResponse, TenantDescribeResponseShard, TenantLocateResponse,
37 : TenantPolicyRequest, TenantShardMigrateRequest, TenantShardMigrateResponse,
38 : UtilizationScore,
39 : },
40 : models::{SecondaryProgress, TenantConfigRequest, TopTenantShardsRequest},
41 : };
42 : use reqwest::StatusCode;
43 : use tracing::{instrument, Instrument};
44 :
45 : use crate::pageserver_client::PageserverClient;
46 : use pageserver_api::{
47 : models::{
48 : self, LocationConfig, LocationConfigListResponse, LocationConfigMode,
49 : PageserverUtilization, ShardParameters, TenantConfig, TenantCreateRequest,
50 : TenantLocationConfigRequest, TenantLocationConfigResponse, TenantShardLocation,
51 : TenantShardSplitRequest, TenantShardSplitResponse, TenantTimeTravelRequest,
52 : TimelineCreateRequest, TimelineInfo,
53 : },
54 : shard::{ShardCount, ShardIdentity, ShardNumber, ShardStripeSize, TenantShardId},
55 : upcall_api::{
56 : ReAttachRequest, ReAttachResponse, ReAttachResponseTenant, ValidateRequest,
57 : ValidateResponse, ValidateResponseTenant,
58 : },
59 : };
60 : use pageserver_client::mgmt_api;
61 : use tokio::sync::mpsc::error::TrySendError;
62 : use tokio_util::sync::CancellationToken;
63 : use utils::{
64 : completion::Barrier,
65 : failpoint_support,
66 : generation::Generation,
67 : http::error::ApiError,
68 : id::{NodeId, TenantId, TimelineId},
69 : sync::gate::Gate,
70 : };
71 :
72 : use crate::{
73 : compute_hook::ComputeHook,
74 : heartbeater::{Heartbeater, PageserverState},
75 : node::{AvailabilityTransition, Node},
76 : persistence::{split_state::SplitState, DatabaseError, Persistence, TenantShardPersistence},
77 : reconciler::attached_location_conf,
78 : scheduler::Scheduler,
79 : tenant_shard::{
80 : IntentState, ObservedState, ObservedStateLocation, ReconcileResult, ReconcileWaitError,
81 : ReconcilerWaiter, TenantShard,
82 : },
83 : };
84 :
85 : // For operations that should be quick, like attaching a new tenant
86 : const SHORT_RECONCILE_TIMEOUT: Duration = Duration::from_secs(5);
87 :
88 : // For operations that might be slow, like migrating a tenant with
89 : // some data in it.
90 : pub const RECONCILE_TIMEOUT: Duration = Duration::from_secs(30);
91 :
92 : // If we receive a call using Secondary mode initially, it will omit generation. We will initialize
93 : // tenant shards into this generation, and as long as it remains in this generation, we will accept
94 : // input generation from future requests as authoritative.
95 : const INITIAL_GENERATION: Generation = Generation::new(0);
96 :
97 : /// How long [`Service::startup_reconcile`] is allowed to take before it should give
98 : /// up on unresponsive pageservers and proceed.
99 : pub(crate) const STARTUP_RECONCILE_TIMEOUT: Duration = Duration::from_secs(30);
100 :
101 : /// How long a node may be unresponsive to heartbeats before we declare it offline.
102 : /// This must be long enough to cover node restarts as well as normal operations: in future
103 : /// it should be separated into distinct timeouts for startup vs. normal operation
104 : /// (`<https://github.com/neondatabase/neon/issues/7552>`)
105 : pub const MAX_UNAVAILABLE_INTERVAL_DEFAULT: Duration = Duration::from_secs(300);
106 :
107 0 : #[derive(Clone, strum_macros::Display)]
108 : enum TenantOperations {
109 : Create,
110 : LocationConfig,
111 : ConfigSet,
112 : TimeTravelRemoteStorage,
113 : Delete,
114 : UpdatePolicy,
115 : ShardSplit,
116 : SecondaryDownload,
117 : TimelineCreate,
118 : TimelineDelete,
119 : }
120 :
121 0 : #[derive(Clone, strum_macros::Display)]
122 : enum NodeOperations {
123 : Register,
124 : Configure,
125 : }
126 :
127 : pub const RECONCILER_CONCURRENCY_DEFAULT: usize = 128;
128 :
129 : // Depth of the channel used to enqueue shards for reconciliation when they can't do it immediately.
130 : // This channel is finite-size to avoid using excessive memory if we get into a state where reconciles are finishing more slowly
131 : // than they're being pushed onto the queue.
132 : const MAX_DELAYED_RECONCILES: usize = 10000;
133 :
134 : // Top level state available to all HTTP handlers
135 : struct ServiceState {
136 : tenants: BTreeMap<TenantShardId, TenantShard>,
137 :
138 : nodes: Arc<HashMap<NodeId, Node>>,
139 :
140 : scheduler: Scheduler,
141 :
142 : /// Ongoing background operation on the cluster if any is running.
143 : /// Note that only one such operation may run at any given time,
144 : /// hence the type choice.
145 : ongoing_operation: Option<OperationHandler>,
146 :
147 : /// Queue of tenants who are waiting for concurrency limits to permit them to reconcile
148 : delayed_reconcile_rx: tokio::sync::mpsc::Receiver<TenantShardId>,
149 : }
150 :
151 : /// Transform an error from a pageserver into an error to return to callers of a storage
152 : /// controller API.
153 0 : fn passthrough_api_error(node: &Node, e: mgmt_api::Error) -> ApiError {
154 0 : match e {
155 0 : mgmt_api::Error::ReceiveErrorBody(str) => {
156 0 : // Presume errors receiving body are connectivity/availability issues
157 0 : ApiError::ResourceUnavailable(
158 0 : format!("{node} error receiving error body: {str}").into(),
159 0 : )
160 : }
161 0 : mgmt_api::Error::ReceiveBody(str) => {
162 0 : // Presume errors receiving body are connectivity/availability issues
163 0 : ApiError::ResourceUnavailable(format!("{node} error receiving body: {str}").into())
164 : }
165 0 : mgmt_api::Error::ApiError(StatusCode::NOT_FOUND, msg) => {
166 0 : ApiError::NotFound(anyhow::anyhow!(format!("{node}: {msg}")).into())
167 : }
168 0 : mgmt_api::Error::ApiError(StatusCode::SERVICE_UNAVAILABLE, msg) => {
169 0 : ApiError::ResourceUnavailable(format!("{node}: {msg}").into())
170 : }
171 0 : mgmt_api::Error::ApiError(status @ StatusCode::UNAUTHORIZED, msg)
172 0 : | mgmt_api::Error::ApiError(status @ StatusCode::FORBIDDEN, msg) => {
173 : // Auth errors talking to a pageserver are not auth errors for the caller: they are
174 : // internal server errors, showing that something is wrong with the pageserver or
175 : // storage controller's auth configuration.
176 0 : ApiError::InternalServerError(anyhow::anyhow!("{node} {status}: {msg}"))
177 : }
178 0 : mgmt_api::Error::ApiError(status, msg) => {
179 0 : // Presume general case of pageserver API errors is that we tried to do something
180 0 : // that can't be done right now.
181 0 : ApiError::Conflict(format!("{node} {status}: {status} {msg}"))
182 : }
183 0 : mgmt_api::Error::Cancelled => ApiError::ShuttingDown,
184 : }
185 0 : }
186 :
187 : impl ServiceState {
188 0 : fn new(
189 0 : nodes: HashMap<NodeId, Node>,
190 0 : tenants: BTreeMap<TenantShardId, TenantShard>,
191 0 : scheduler: Scheduler,
192 0 : delayed_reconcile_rx: tokio::sync::mpsc::Receiver<TenantShardId>,
193 0 : ) -> Self {
194 0 : Self {
195 0 : tenants,
196 0 : nodes: Arc::new(nodes),
197 0 : scheduler,
198 0 : ongoing_operation: None,
199 0 : delayed_reconcile_rx,
200 0 : }
201 0 : }
202 :
203 0 : fn parts_mut(
204 0 : &mut self,
205 0 : ) -> (
206 0 : &mut Arc<HashMap<NodeId, Node>>,
207 0 : &mut BTreeMap<TenantShardId, TenantShard>,
208 0 : &mut Scheduler,
209 0 : ) {
210 0 : (&mut self.nodes, &mut self.tenants, &mut self.scheduler)
211 0 : }
212 : }
213 :
214 : #[derive(Clone)]
215 : pub struct Config {
216 : // All pageservers managed by one instance of this service must have
217 : // the same public key. This JWT token will be used to authenticate
218 : // this service to the pageservers it manages.
219 : pub jwt_token: Option<String>,
220 :
221 : // This JWT token will be used to authenticate this service to the control plane.
222 : pub control_plane_jwt_token: Option<String>,
223 :
224 : /// Where the compute hook should send notifications of pageserver attachment locations
225 : /// (this URL points to the control plane in prod). If this is None, the compute hook will
226 : /// assume it is running in a test environment and try to update neon_local.
227 : pub compute_hook_url: Option<String>,
228 :
229 : /// Grace period within which a pageserver does not respond to heartbeats, but is still
230 : /// considered active. Once the grace period elapses, the next heartbeat failure will
231 : /// mark the pagseserver offline.
232 : pub max_unavailable_interval: Duration,
233 :
234 : /// How many Reconcilers may be spawned concurrently
235 : pub reconciler_concurrency: usize,
236 :
237 : /// How large must a shard grow in bytes before we split it?
238 : /// None disables auto-splitting.
239 : pub split_threshold: Option<u64>,
240 :
241 : // TODO: make this cfg(feature = "testing")
242 : pub neon_local_repo_dir: Option<PathBuf>,
243 : }
244 :
245 : impl From<DatabaseError> for ApiError {
246 0 : fn from(err: DatabaseError) -> ApiError {
247 0 : match err {
248 0 : DatabaseError::Query(e) => ApiError::InternalServerError(e.into()),
249 : // FIXME: ApiError doesn't have an Unavailable variant, but ShuttingDown maps to 503.
250 : DatabaseError::Connection(_) | DatabaseError::ConnectionPool(_) => {
251 0 : ApiError::ShuttingDown
252 : }
253 0 : DatabaseError::Logical(reason) => {
254 0 : ApiError::InternalServerError(anyhow::anyhow!(reason))
255 : }
256 : }
257 0 : }
258 : }
259 :
260 : pub struct Service {
261 : inner: Arc<std::sync::RwLock<ServiceState>>,
262 : config: Config,
263 : persistence: Arc<Persistence>,
264 : compute_hook: Arc<ComputeHook>,
265 : result_tx: tokio::sync::mpsc::UnboundedSender<ReconcileResult>,
266 :
267 : heartbeater: Heartbeater,
268 :
269 : // Channel for background cleanup from failed operations that require cleanup, such as shard split
270 : abort_tx: tokio::sync::mpsc::UnboundedSender<TenantShardSplitAbort>,
271 :
272 : // Locking on a tenant granularity (covers all shards in the tenant):
273 : // - Take exclusively for rare operations that mutate the tenant's persistent state (e.g. create/delete/split)
274 : // - Take in shared mode for operations that need the set of shards to stay the same to complete reliably (e.g. timeline CRUD)
275 : tenant_op_locks: IdLockMap<TenantId, TenantOperations>,
276 :
277 : // Locking for node-mutating operations: take exclusively for operations that modify the node's persistent state, or
278 : // that transition it to/from Active.
279 : node_op_locks: IdLockMap<NodeId, NodeOperations>,
280 :
281 : // Limit how many Reconcilers we will spawn concurrently
282 : reconciler_concurrency: Arc<tokio::sync::Semaphore>,
283 :
284 : /// Queue of tenants who are waiting for concurrency limits to permit them to reconcile
285 : /// Send into this queue to promptly attempt to reconcile this shard next time units are available.
286 : ///
287 : /// Note that this state logically lives inside ServiceInner, but carrying Sender here makes the code simpler
288 : /// by avoiding needing a &mut ref to something inside the ServiceInner. This could be optimized to
289 : /// use a VecDeque instead of a channel to reduce synchronization overhead, at the cost of some code complexity.
290 : delayed_reconcile_tx: tokio::sync::mpsc::Sender<TenantShardId>,
291 :
292 : // Process shutdown will fire this token
293 : cancel: CancellationToken,
294 :
295 : // Background tasks will hold this gate
296 : gate: Gate,
297 :
298 : /// This waits for initial reconciliation with pageservers to complete. Until this barrier
299 : /// passes, it isn't safe to do any actions that mutate tenants.
300 : pub(crate) startup_complete: Barrier,
301 : }
302 :
303 : impl From<ReconcileWaitError> for ApiError {
304 0 : fn from(value: ReconcileWaitError) -> Self {
305 0 : match value {
306 0 : ReconcileWaitError::Shutdown => ApiError::ShuttingDown,
307 0 : e @ ReconcileWaitError::Timeout(_) => ApiError::Timeout(format!("{e}").into()),
308 0 : e @ ReconcileWaitError::Failed(..) => ApiError::InternalServerError(anyhow::anyhow!(e)),
309 : }
310 0 : }
311 : }
312 :
313 : impl From<OperationError> for ApiError {
314 0 : fn from(value: OperationError) -> Self {
315 0 : match value {
316 0 : OperationError::NodeStateChanged(err) | OperationError::FinalizeError(err) => {
317 0 : ApiError::InternalServerError(anyhow::anyhow!(err))
318 : }
319 0 : OperationError::Cancelled => ApiError::Conflict("Operation was cancelled".into()),
320 : }
321 0 : }
322 : }
323 :
324 : #[allow(clippy::large_enum_variant)]
325 : enum TenantCreateOrUpdate {
326 : Create(TenantCreateRequest),
327 : Update(Vec<ShardUpdate>),
328 : }
329 :
330 : struct ShardSplitParams {
331 : old_shard_count: ShardCount,
332 : new_shard_count: ShardCount,
333 : new_stripe_size: Option<ShardStripeSize>,
334 : targets: Vec<ShardSplitTarget>,
335 : policy: PlacementPolicy,
336 : config: TenantConfig,
337 : shard_ident: ShardIdentity,
338 : }
339 :
340 : // When preparing for a shard split, we may either choose to proceed with the split,
341 : // or find that the work is already done and return NoOp.
342 : enum ShardSplitAction {
343 : Split(ShardSplitParams),
344 : NoOp(TenantShardSplitResponse),
345 : }
346 :
347 : // A parent shard which will be split
348 : struct ShardSplitTarget {
349 : parent_id: TenantShardId,
350 : node: Node,
351 : child_ids: Vec<TenantShardId>,
352 : }
353 :
354 : /// When we tenant shard split operation fails, we may not be able to clean up immediately, because nodes
355 : /// might not be available. We therefore use a queue of abort operations processed in the background.
356 : struct TenantShardSplitAbort {
357 : tenant_id: TenantId,
358 : /// The target values from the request that failed
359 : new_shard_count: ShardCount,
360 : new_stripe_size: Option<ShardStripeSize>,
361 : /// Until this abort op is complete, no other operations may be done on the tenant
362 : _tenant_lock: WrappedWriteGuard<TenantOperations>,
363 : }
364 :
365 0 : #[derive(thiserror::Error, Debug)]
366 : enum TenantShardSplitAbortError {
367 : #[error(transparent)]
368 : Database(#[from] DatabaseError),
369 : #[error(transparent)]
370 : Remote(#[from] mgmt_api::Error),
371 : #[error("Unavailable")]
372 : Unavailable,
373 : }
374 :
375 : struct ShardUpdate {
376 : tenant_shard_id: TenantShardId,
377 : placement_policy: PlacementPolicy,
378 : tenant_config: TenantConfig,
379 :
380 : /// If this is None, generation is not updated.
381 : generation: Option<Generation>,
382 : }
383 :
384 : impl Service {
385 0 : pub fn get_config(&self) -> &Config {
386 0 : &self.config
387 0 : }
388 :
389 : /// Called once on startup, this function attempts to contact all pageservers to build an up-to-date
390 : /// view of the world, and determine which pageservers are responsive.
391 0 : #[instrument(skip_all)]
392 : async fn startup_reconcile(
393 : self: &Arc<Service>,
394 : bg_compute_notify_result_tx: tokio::sync::mpsc::Sender<
395 : Result<(), (TenantShardId, NotifyError)>,
396 : >,
397 : ) {
398 : // For all tenant shards, a vector of observed states on nodes (where None means
399 : // indeterminate, same as in [`ObservedStateLocation`])
400 : let mut observed: HashMap<TenantShardId, Vec<(NodeId, Option<LocationConfig>)>> =
401 : HashMap::new();
402 :
403 : // Startup reconciliation does I/O to other services: whether they
404 : // are responsive or not, we should aim to finish within our deadline, because:
405 : // - If we don't, a k8s readiness hook watching /ready will kill us.
406 : // - While we're waiting for startup reconciliation, we are not fully
407 : // available for end user operations like creating/deleting tenants and timelines.
408 : //
409 : // We set multiple deadlines to break up the time available between the phases of work: this is
410 : // arbitrary, but avoids a situation where the first phase could burn our entire timeout period.
411 : let start_at = Instant::now();
412 : let node_scan_deadline = start_at
413 : .checked_add(STARTUP_RECONCILE_TIMEOUT / 2)
414 : .expect("Reconcile timeout is a modest constant");
415 :
416 : // Accumulate a list of any tenant locations that ought to be detached
417 : let mut cleanup = Vec::new();
418 :
419 : let node_listings = self.scan_node_locations(node_scan_deadline).await;
420 : // Send initial heartbeat requests to nodes that replied to the location listing above.
421 : let nodes_online = self.initial_heartbeat_round(node_listings.keys()).await;
422 :
423 : for (node_id, list_response) in node_listings {
424 : let tenant_shards = list_response.tenant_shards;
425 : tracing::info!(
426 : "Received {} shard statuses from pageserver {}, setting it to Active",
427 : tenant_shards.len(),
428 : node_id
429 : );
430 :
431 : for (tenant_shard_id, conf_opt) in tenant_shards {
432 : let shard_observations = observed.entry(tenant_shard_id).or_default();
433 : shard_observations.push((node_id, conf_opt));
434 : }
435 : }
436 :
437 : // List of tenants for which we will attempt to notify compute of their location at startup
438 : let mut compute_notifications = Vec::new();
439 :
440 : // Populate intent and observed states for all tenants, based on reported state on pageservers
441 : tracing::info!("Populating tenant shards' states from initial pageserver scan...");
442 : let shard_count = {
443 : let mut locked = self.inner.write().unwrap();
444 : let (nodes, tenants, scheduler) = locked.parts_mut();
445 :
446 : // Mark nodes online if they responded to us: nodes are offline by default after a restart.
447 : let mut new_nodes = (**nodes).clone();
448 : for (node_id, node) in new_nodes.iter_mut() {
449 : if let Some(utilization) = nodes_online.get(node_id) {
450 : node.set_availability(NodeAvailability::Active(UtilizationScore(
451 : utilization.utilization_score,
452 : )));
453 : scheduler.node_upsert(node);
454 : }
455 : }
456 : *nodes = Arc::new(new_nodes);
457 :
458 : for (tenant_shard_id, shard_observations) in observed {
459 : for (node_id, observed_loc) in shard_observations {
460 : let Some(tenant_shard) = tenants.get_mut(&tenant_shard_id) else {
461 : cleanup.push((tenant_shard_id, node_id));
462 : continue;
463 : };
464 : tenant_shard
465 : .observed
466 : .locations
467 : .insert(node_id, ObservedStateLocation { conf: observed_loc });
468 : }
469 : }
470 :
471 : // Populate each tenant's intent state
472 : let mut schedule_context = ScheduleContext::default();
473 : for (tenant_shard_id, tenant_shard) in tenants.iter_mut() {
474 : if tenant_shard_id.shard_number == ShardNumber(0) {
475 : // Reset scheduling context each time we advance to the next Tenant
476 : schedule_context = ScheduleContext::default();
477 : }
478 :
479 : tenant_shard.intent_from_observed(scheduler);
480 : if let Err(e) = tenant_shard.schedule(scheduler, &mut schedule_context) {
481 : // Non-fatal error: we are unable to properly schedule the tenant, perhaps because
482 : // not enough pageservers are available. The tenant may well still be available
483 : // to clients.
484 : tracing::error!("Failed to schedule tenant {tenant_shard_id} at startup: {e}");
485 : } else {
486 : // If we're both intending and observed to be attached at a particular node, we will
487 : // emit a compute notification for this. In the case where our observed state does not
488 : // yet match our intent, we will eventually reconcile, and that will emit a compute notification.
489 : if let Some(attached_at) = tenant_shard.stably_attached() {
490 : compute_notifications.push((
491 : *tenant_shard_id,
492 : attached_at,
493 : tenant_shard.shard.stripe_size,
494 : ));
495 : }
496 : }
497 : }
498 :
499 : tenants.len()
500 : };
501 :
502 : // TODO: if any tenant's intent now differs from its loaded generation_pageserver, we should clear that
503 : // generation_pageserver in the database.
504 :
505 : // Emit compute hook notifications for all tenants which are already stably attached. Other tenants
506 : // will emit compute hook notifications when they reconcile.
507 : //
508 : // Ordering: our calls to notify_background synchronously establish a relative order for these notifications vs. any later
509 : // calls into the ComputeHook for the same tenant: we can leave these to run to completion in the background and any later
510 : // calls will be correctly ordered wrt these.
511 : //
512 : // Concurrency: we call notify_background for all tenants, which will create O(N) tokio tasks, but almost all of them
513 : // will just wait on the ComputeHook::API_CONCURRENCY semaphore immediately, so very cheap until they get that semaphore
514 : // unit and start doing I/O.
515 : tracing::info!(
516 : "Sending {} compute notifications",
517 : compute_notifications.len()
518 : );
519 : self.compute_hook.notify_background(
520 : compute_notifications,
521 : bg_compute_notify_result_tx.clone(),
522 : &self.cancel,
523 : );
524 :
525 : // Finally, now that the service is up and running, launch reconcile operations for any tenants
526 : // which require it: under normal circumstances this should only include tenants that were in some
527 : // transient state before we restarted, or any tenants whose compute hooks failed above.
528 : tracing::info!("Checking for shards in need of reconciliation...");
529 : let reconcile_tasks = self.reconcile_all();
530 : // We will not wait for these reconciliation tasks to run here: we're now done with startup and
531 : // normal operations may proceed.
532 :
533 : // Clean up any tenants that were found on pageservers but are not known to us. Do this in the
534 : // background because it does not need to complete in order to proceed with other work.
535 : if !cleanup.is_empty() {
536 : tracing::info!("Cleaning up {} locations in the background", cleanup.len());
537 : tokio::task::spawn({
538 : let cleanup_self = self.clone();
539 0 : async move { cleanup_self.cleanup_locations(cleanup).await }
540 : });
541 : }
542 :
543 : tracing::info!("Startup complete, spawned {reconcile_tasks} reconciliation tasks ({shard_count} shards total)");
544 : }
545 :
546 0 : async fn initial_heartbeat_round<'a>(
547 0 : &self,
548 0 : node_ids: impl Iterator<Item = &'a NodeId>,
549 0 : ) -> HashMap<NodeId, PageserverUtilization> {
550 0 : assert!(!self.startup_complete.is_ready());
551 :
552 0 : let all_nodes = {
553 0 : let locked = self.inner.read().unwrap();
554 0 : locked.nodes.clone()
555 0 : };
556 0 :
557 0 : let mut nodes_to_heartbeat = HashMap::new();
558 0 : for node_id in node_ids {
559 0 : match all_nodes.get(node_id) {
560 0 : Some(node) => {
561 0 : nodes_to_heartbeat.insert(*node_id, node.clone());
562 0 : }
563 : None => {
564 0 : tracing::warn!("Node {node_id} was removed during start-up");
565 : }
566 : }
567 : }
568 :
569 0 : tracing::info!("Sending initial heartbeats...");
570 0 : let res = self
571 0 : .heartbeater
572 0 : .heartbeat(Arc::new(nodes_to_heartbeat))
573 0 : .await;
574 :
575 0 : let mut online_nodes = HashMap::new();
576 0 : if let Ok(deltas) = res {
577 0 : for (node_id, status) in deltas.0 {
578 0 : match status {
579 0 : PageserverState::Available { utilization, .. } => {
580 0 : online_nodes.insert(node_id, utilization);
581 0 : }
582 0 : PageserverState::Offline => {}
583 : }
584 : }
585 0 : }
586 :
587 0 : online_nodes
588 0 : }
589 :
590 : /// Used during [`Self::startup_reconcile`]: issue GETs to all nodes concurrently, with a deadline.
591 : ///
592 : /// The result includes only nodes which responded within the deadline
593 0 : async fn scan_node_locations(
594 0 : &self,
595 0 : deadline: Instant,
596 0 : ) -> HashMap<NodeId, LocationConfigListResponse> {
597 0 : let nodes = {
598 0 : let locked = self.inner.read().unwrap();
599 0 : locked.nodes.clone()
600 0 : };
601 0 :
602 0 : let mut node_results = HashMap::new();
603 0 :
604 0 : let mut node_list_futs = FuturesUnordered::new();
605 0 :
606 0 : tracing::info!("Scanning shards on {} nodes...", nodes.len());
607 0 : for node in nodes.values() {
608 0 : node_list_futs.push({
609 0 : async move {
610 0 : tracing::info!("Scanning shards on node {node}...");
611 0 : let timeout = Duration::from_secs(1);
612 0 : let response = node
613 0 : .with_client_retries(
614 0 : |client| async move { client.list_location_config().await },
615 0 : &self.config.jwt_token,
616 0 : 1,
617 0 : 5,
618 0 : timeout,
619 0 : &self.cancel,
620 0 : )
621 0 : .await;
622 0 : (node.get_id(), response)
623 0 : }
624 0 : });
625 0 : }
626 :
627 : loop {
628 0 : let (node_id, result) = tokio::select! {
629 : next = node_list_futs.next() => {
630 : match next {
631 : Some(result) => result,
632 : None =>{
633 : // We got results for all our nodes
634 : break;
635 : }
636 :
637 : }
638 : },
639 : _ = tokio::time::sleep(deadline.duration_since(Instant::now())) => {
640 : // Give up waiting for anyone who hasn't responded: we will yield the results that we have
641 : tracing::info!("Reached deadline while waiting for nodes to respond to location listing requests");
642 : break;
643 : }
644 : };
645 :
646 0 : let Some(list_response) = result else {
647 0 : tracing::info!("Shutdown during startup_reconcile");
648 0 : break;
649 : };
650 :
651 0 : match list_response {
652 0 : Err(e) => {
653 0 : tracing::warn!("Could not scan node {} ({e})", node_id);
654 : }
655 0 : Ok(listing) => {
656 0 : node_results.insert(node_id, listing);
657 0 : }
658 : }
659 : }
660 :
661 0 : node_results
662 0 : }
663 :
664 : /// Used during [`Self::startup_reconcile`]: detach a list of unknown-to-us tenants from pageservers.
665 : ///
666 : /// This is safe to run in the background, because if we don't have this TenantShardId in our map of
667 : /// tenants, then it is probably something incompletely deleted before: we will not fight with any
668 : /// other task trying to attach it.
669 0 : #[instrument(skip_all)]
670 : async fn cleanup_locations(&self, cleanup: Vec<(TenantShardId, NodeId)>) {
671 : let nodes = self.inner.read().unwrap().nodes.clone();
672 :
673 : for (tenant_shard_id, node_id) in cleanup {
674 : // A node reported a tenant_shard_id which is unknown to us: detach it.
675 : let Some(node) = nodes.get(&node_id) else {
676 : // This is legitimate; we run in the background and [`Self::startup_reconcile`] might have identified
677 : // a location to clean up on a node that has since been removed.
678 : tracing::info!(
679 : "Not cleaning up location {node_id}/{tenant_shard_id}: node not found"
680 : );
681 : continue;
682 : };
683 :
684 : if self.cancel.is_cancelled() {
685 : break;
686 : }
687 :
688 : let client = PageserverClient::new(
689 : node.get_id(),
690 : node.base_url(),
691 : self.config.jwt_token.as_deref(),
692 : );
693 : match client
694 : .location_config(
695 : tenant_shard_id,
696 : LocationConfig {
697 : mode: LocationConfigMode::Detached,
698 : generation: None,
699 : secondary_conf: None,
700 : shard_number: tenant_shard_id.shard_number.0,
701 : shard_count: tenant_shard_id.shard_count.literal(),
702 : shard_stripe_size: 0,
703 : tenant_conf: models::TenantConfig::default(),
704 : },
705 : None,
706 : false,
707 : )
708 : .await
709 : {
710 : Ok(()) => {
711 : tracing::info!(
712 : "Detached unknown shard {tenant_shard_id} on pageserver {node_id}"
713 : );
714 : }
715 : Err(e) => {
716 : // Non-fatal error: leaving a tenant shard behind that we are not managing shouldn't
717 : // break anything.
718 : tracing::error!(
719 : "Failed to detach unknkown shard {tenant_shard_id} on pageserver {node_id}: {e}"
720 : );
721 : }
722 : }
723 : }
724 : }
725 :
726 : /// Long running background task that periodically wakes up and looks for shards that need
727 : /// reconciliation. Reconciliation is fallible, so any reconciliation tasks that fail during
728 : /// e.g. a tenant create/attach/migrate must eventually be retried: this task is responsible
729 : /// for those retries.
730 0 : #[instrument(skip_all)]
731 : async fn background_reconcile(self: &Arc<Self>) {
732 : self.startup_complete.clone().wait().await;
733 :
734 : const BACKGROUND_RECONCILE_PERIOD: Duration = Duration::from_secs(20);
735 :
736 : let mut interval = tokio::time::interval(BACKGROUND_RECONCILE_PERIOD);
737 : while !self.cancel.is_cancelled() {
738 : tokio::select! {
739 : _ = interval.tick() => {
740 : let reconciles_spawned = self.reconcile_all();
741 : if reconciles_spawned == 0 {
742 : // Run optimizer only when we didn't find any other work to do
743 : let optimizations = self.optimize_all().await;
744 : if optimizations == 0 {
745 : // Run new splits only when no optimizations are pending
746 : self.autosplit_tenants().await;
747 : }
748 : }
749 : }
750 : _ = self.cancel.cancelled() => return
751 : }
752 : }
753 : }
754 0 : #[instrument(skip_all)]
755 : async fn spawn_heartbeat_driver(&self) {
756 : self.startup_complete.clone().wait().await;
757 :
758 : const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
759 :
760 : let mut interval = tokio::time::interval(HEARTBEAT_INTERVAL);
761 : while !self.cancel.is_cancelled() {
762 : tokio::select! {
763 : _ = interval.tick() => { }
764 : _ = self.cancel.cancelled() => return
765 : };
766 :
767 : let nodes = {
768 : let locked = self.inner.read().unwrap();
769 : locked.nodes.clone()
770 : };
771 :
772 : let res = self.heartbeater.heartbeat(nodes).await;
773 : if let Ok(deltas) = res {
774 : for (node_id, state) in deltas.0 {
775 : let (new_node, new_availability) = match state {
776 : PageserverState::Available {
777 : utilization, new, ..
778 : } => (
779 : new,
780 : NodeAvailability::Active(UtilizationScore(
781 : utilization.utilization_score,
782 : )),
783 : ),
784 : PageserverState::Offline => (false, NodeAvailability::Offline),
785 : };
786 :
787 : if new_node {
788 : // When the heartbeats detect a newly added node, we don't wish
789 : // to attempt to reconcile the shards assigned to it. The node
790 : // is likely handling it's re-attach response, so reconciling now
791 : // would be counterproductive.
792 : //
793 : // Instead, update the in-memory state with the details learned about the
794 : // node.
795 : let mut locked = self.inner.write().unwrap();
796 : let (nodes, _tenants, scheduler) = locked.parts_mut();
797 :
798 : let mut new_nodes = (**nodes).clone();
799 :
800 : if let Some(node) = new_nodes.get_mut(&node_id) {
801 : node.set_availability(new_availability);
802 : scheduler.node_upsert(node);
803 : }
804 :
805 : locked.nodes = Arc::new(new_nodes);
806 : } else {
807 : // This is the code path for geniune availability transitions (i.e node
808 : // goes unavailable and/or comes back online).
809 : let res = self
810 : .node_configure(node_id, Some(new_availability), None)
811 : .await;
812 :
813 : match res {
814 : Ok(()) => {}
815 : Err(ApiError::NotFound(_)) => {
816 : // This should be rare, but legitimate since the heartbeats are done
817 : // on a snapshot of the nodes.
818 : tracing::info!(
819 : "Node {} was not found after heartbeat round",
820 : node_id
821 : );
822 : }
823 : Err(err) => {
824 : tracing::error!(
825 : "Failed to update node {} after heartbeat round: {}",
826 : node_id,
827 : err
828 : );
829 : }
830 : }
831 : }
832 : }
833 : }
834 : }
835 : }
836 :
837 : /// Apply the contents of a [`ReconcileResult`] to our in-memory state: if the reconciliation
838 : /// was successful and intent hasn't changed since the Reconciler was spawned, this will update
839 : /// the observed state of the tenant such that subsequent calls to [`TenantShard::get_reconcile_needed`]
840 : /// will indicate that reconciliation is not needed.
841 0 : #[instrument(skip_all, fields(
842 : tenant_id=%result.tenant_shard_id.tenant_id, shard_id=%result.tenant_shard_id.shard_slug(),
843 : sequence=%result.sequence
844 0 : ))]
845 : fn process_result(&self, result: ReconcileResult) {
846 : let mut locked = self.inner.write().unwrap();
847 : let Some(tenant) = locked.tenants.get_mut(&result.tenant_shard_id) else {
848 : // A reconciliation result might race with removing a tenant: drop results for
849 : // tenants that aren't in our map.
850 : return;
851 : };
852 :
853 : // Usually generation should only be updated via this path, so the max() isn't
854 : // needed, but it is used to handle out-of-band updates via. e.g. test hook.
855 : tenant.generation = std::cmp::max(tenant.generation, result.generation);
856 :
857 : // If the reconciler signals that it failed to notify compute, set this state on
858 : // the shard so that a future [`TenantShard::maybe_reconcile`] will try again.
859 : tenant.pending_compute_notification = result.pending_compute_notification;
860 :
861 : // Let the TenantShard know it is idle.
862 : tenant.reconcile_complete(result.sequence);
863 :
864 : match result.result {
865 : Ok(()) => {
866 : for (node_id, loc) in &result.observed.locations {
867 : if let Some(conf) = &loc.conf {
868 : tracing::info!("Updating observed location {}: {:?}", node_id, conf);
869 : } else {
870 : tracing::info!("Setting observed location {} to None", node_id,)
871 : }
872 : }
873 : tenant.observed = result.observed;
874 : tenant.waiter.advance(result.sequence);
875 : }
876 : Err(e) => {
877 : match e {
878 : ReconcileError::Cancel => {
879 : tracing::info!("Reconciler was cancelled");
880 : }
881 : ReconcileError::Remote(mgmt_api::Error::Cancelled) => {
882 : // This might be due to the reconciler getting cancelled, or it might
883 : // be due to the `Node` being marked offline.
884 : tracing::info!("Reconciler cancelled during pageserver API call");
885 : }
886 : _ => {
887 : tracing::warn!("Reconcile error: {}", e);
888 : }
889 : }
890 :
891 : // Ordering: populate last_error before advancing error_seq,
892 : // so that waiters will see the correct error after waiting.
893 : tenant.set_last_error(result.sequence, e);
894 :
895 : for (node_id, o) in result.observed.locations {
896 : tenant.observed.locations.insert(node_id, o);
897 : }
898 : }
899 : }
900 :
901 : // Maybe some other work can proceed now that this job finished.
902 : if self.reconciler_concurrency.available_permits() > 0 {
903 : while let Ok(tenant_shard_id) = locked.delayed_reconcile_rx.try_recv() {
904 : let (nodes, tenants, _scheduler) = locked.parts_mut();
905 : if let Some(shard) = tenants.get_mut(&tenant_shard_id) {
906 : shard.delayed_reconcile = false;
907 : self.maybe_reconcile_shard(shard, nodes);
908 : }
909 :
910 : if self.reconciler_concurrency.available_permits() == 0 {
911 : break;
912 : }
913 : }
914 : }
915 : }
916 :
917 0 : async fn process_results(
918 0 : &self,
919 0 : mut result_rx: tokio::sync::mpsc::UnboundedReceiver<ReconcileResult>,
920 0 : mut bg_compute_hook_result_rx: tokio::sync::mpsc::Receiver<
921 0 : Result<(), (TenantShardId, NotifyError)>,
922 0 : >,
923 0 : ) {
924 0 : loop {
925 0 : // Wait for the next result, or for cancellation
926 0 : tokio::select! {
927 : r = result_rx.recv() => {
928 : match r {
929 : Some(result) => {self.process_result(result);},
930 : None => {break;}
931 : }
932 : }
933 0 : _ = async{
934 0 : match bg_compute_hook_result_rx.recv().await {
935 0 : Some(result) => {
936 0 : if let Err((tenant_shard_id, notify_error)) = result {
937 0 : tracing::warn!("Marking shard {tenant_shard_id} for notification retry, due to error {notify_error}");
938 0 : let mut locked = self.inner.write().unwrap();
939 0 : if let Some(shard) = locked.tenants.get_mut(&tenant_shard_id) {
940 0 : shard.pending_compute_notification = true;
941 0 : }
942 :
943 0 : }
944 : },
945 : None => {
946 : // This channel is dead, but we don't want to terminate the outer loop{}: just wait for shutdown
947 0 : self.cancel.cancelled().await;
948 : }
949 : }
950 0 : } => {},
951 : _ = self.cancel.cancelled() => {
952 : break;
953 : }
954 0 : };
955 0 : }
956 :
957 : // We should only fall through on shutdown
958 0 : assert!(self.cancel.is_cancelled());
959 0 : }
960 :
961 0 : async fn process_aborts(
962 0 : &self,
963 0 : mut abort_rx: tokio::sync::mpsc::UnboundedReceiver<TenantShardSplitAbort>,
964 0 : ) {
965 : loop {
966 : // Wait for the next result, or for cancellation
967 0 : let op = tokio::select! {
968 : r = abort_rx.recv() => {
969 : match r {
970 : Some(op) => {op},
971 : None => {break;}
972 : }
973 : }
974 : _ = self.cancel.cancelled() => {
975 : break;
976 : }
977 : };
978 :
979 : // Retry until shutdown: we must keep this request object alive until it is properly
980 : // processed, as it holds a lock guard that prevents other operations trying to do things
981 : // to the tenant while it is in a weird part-split state.
982 0 : while !self.cancel.is_cancelled() {
983 0 : match self.abort_tenant_shard_split(&op).await {
984 0 : Ok(_) => break,
985 0 : Err(e) => {
986 0 : tracing::warn!(
987 0 : "Failed to abort shard split on {}, will retry: {e}",
988 : op.tenant_id
989 : );
990 :
991 : // If a node is unavailable, we hope that it has been properly marked Offline
992 : // when we retry, so that the abort op will succeed. If the abort op is failing
993 : // for some other reason, we will keep retrying forever, or until a human notices
994 : // and does something about it (either fixing a pageserver or restarting the controller).
995 0 : tokio::time::timeout(Duration::from_secs(5), self.cancel.cancelled())
996 0 : .await
997 0 : .ok();
998 : }
999 : }
1000 : }
1001 : }
1002 0 : }
1003 :
1004 0 : pub async fn spawn(config: Config, persistence: Arc<Persistence>) -> anyhow::Result<Arc<Self>> {
1005 0 : let (result_tx, result_rx) = tokio::sync::mpsc::unbounded_channel();
1006 0 : let (abort_tx, abort_rx) = tokio::sync::mpsc::unbounded_channel();
1007 0 :
1008 0 : tracing::info!("Loading nodes from database...");
1009 0 : let nodes = persistence
1010 0 : .list_nodes()
1011 0 : .await?
1012 0 : .into_iter()
1013 0 : .map(Node::from_persistent)
1014 0 : .collect::<Vec<_>>();
1015 0 : let nodes: HashMap<NodeId, Node> = nodes.into_iter().map(|n| (n.get_id(), n)).collect();
1016 0 : tracing::info!("Loaded {} nodes from database.", nodes.len());
1017 :
1018 0 : tracing::info!("Loading shards from database...");
1019 0 : let mut tenant_shard_persistence = persistence.list_tenant_shards().await?;
1020 0 : tracing::info!(
1021 0 : "Loaded {} shards from database.",
1022 0 : tenant_shard_persistence.len()
1023 : );
1024 :
1025 : // If any shard splits were in progress, reset the database state to abort them
1026 0 : let mut tenant_shard_count_min_max: HashMap<TenantId, (ShardCount, ShardCount)> =
1027 0 : HashMap::new();
1028 0 : for tsp in &mut tenant_shard_persistence {
1029 0 : let shard = tsp.get_shard_identity()?;
1030 0 : let tenant_shard_id = tsp.get_tenant_shard_id()?;
1031 0 : let entry = tenant_shard_count_min_max
1032 0 : .entry(tenant_shard_id.tenant_id)
1033 0 : .or_insert_with(|| (shard.count, shard.count));
1034 0 : entry.0 = std::cmp::min(entry.0, shard.count);
1035 0 : entry.1 = std::cmp::max(entry.1, shard.count);
1036 : }
1037 :
1038 0 : for (tenant_id, (count_min, count_max)) in tenant_shard_count_min_max {
1039 0 : if count_min != count_max {
1040 : // Aborting the split in the database and dropping the child shards is sufficient: the reconciliation in
1041 : // [`Self::startup_reconcile`] will implicitly drop the child shards on remote pageservers, or they'll
1042 : // be dropped later in [`Self::node_activate_reconcile`] if it isn't available right now.
1043 0 : tracing::info!("Aborting shard split {tenant_id} {count_min:?} -> {count_max:?}");
1044 0 : let abort_status = persistence.abort_shard_split(tenant_id, count_max).await?;
1045 :
1046 : // We may never see the Complete status here: if the split was complete, we wouldn't have
1047 : // identified this tenant has having mismatching min/max counts.
1048 0 : assert!(matches!(abort_status, AbortShardSplitStatus::Aborted));
1049 :
1050 : // Clear the splitting status in-memory, to reflect that we just aborted in the database
1051 0 : tenant_shard_persistence.iter_mut().for_each(|tsp| {
1052 0 : // Set idle split state on those shards that we will retain.
1053 0 : let tsp_tenant_id = TenantId::from_str(tsp.tenant_id.as_str()).unwrap();
1054 0 : if tsp_tenant_id == tenant_id
1055 0 : && tsp.get_shard_identity().unwrap().count == count_min
1056 0 : {
1057 0 : tsp.splitting = SplitState::Idle;
1058 0 : } else if tsp_tenant_id == tenant_id {
1059 : // Leave the splitting state on the child shards: this will be used next to
1060 : // drop them.
1061 0 : tracing::info!(
1062 0 : "Shard {tsp_tenant_id} will be dropped after shard split abort",
1063 : );
1064 0 : }
1065 0 : });
1066 0 :
1067 0 : // Drop shards for this tenant which we didn't just mark idle (i.e. child shards of the aborted split)
1068 0 : tenant_shard_persistence.retain(|tsp| {
1069 0 : TenantId::from_str(tsp.tenant_id.as_str()).unwrap() != tenant_id
1070 0 : || tsp.splitting == SplitState::Idle
1071 0 : });
1072 0 : }
1073 : }
1074 :
1075 0 : let mut tenants = BTreeMap::new();
1076 0 :
1077 0 : let mut scheduler = Scheduler::new(nodes.values());
1078 0 :
1079 0 : #[cfg(feature = "testing")]
1080 0 : {
1081 0 : // Hack: insert scheduler state for all nodes referenced by shards, as compatibility
1082 0 : // tests only store the shards, not the nodes. The nodes will be loaded shortly
1083 0 : // after when pageservers start up and register.
1084 0 : let mut node_ids = HashSet::new();
1085 0 : for tsp in &tenant_shard_persistence {
1086 0 : if let Some(node_id) = tsp.generation_pageserver {
1087 0 : node_ids.insert(node_id);
1088 0 : }
1089 : }
1090 0 : for node_id in node_ids {
1091 0 : tracing::info!("Creating node {} in scheduler for tests", node_id);
1092 0 : let node = Node::new(
1093 0 : NodeId(node_id as u64),
1094 0 : "".to_string(),
1095 0 : 123,
1096 0 : "".to_string(),
1097 0 : 123,
1098 0 : );
1099 0 :
1100 0 : scheduler.node_upsert(&node);
1101 : }
1102 : }
1103 0 : for tsp in tenant_shard_persistence {
1104 0 : let tenant_shard_id = tsp.get_tenant_shard_id()?;
1105 :
1106 : // We will populate intent properly later in [`Self::startup_reconcile`], initially populate
1107 : // it with what we can infer: the node for which a generation was most recently issued.
1108 0 : let mut intent = IntentState::new();
1109 0 : if let Some(generation_pageserver) = tsp.generation_pageserver {
1110 0 : intent.set_attached(&mut scheduler, Some(NodeId(generation_pageserver as u64)));
1111 0 : }
1112 0 : let new_tenant = TenantShard::from_persistent(tsp, intent)?;
1113 :
1114 0 : tenants.insert(tenant_shard_id, new_tenant);
1115 : }
1116 :
1117 0 : let (startup_completion, startup_complete) = utils::completion::channel();
1118 0 :
1119 0 : // This channel is continuously consumed by process_results, so doesn't need to be very large.
1120 0 : let (bg_compute_notify_result_tx, bg_compute_notify_result_rx) =
1121 0 : tokio::sync::mpsc::channel(512);
1122 0 :
1123 0 : let (delayed_reconcile_tx, delayed_reconcile_rx) =
1124 0 : tokio::sync::mpsc::channel(MAX_DELAYED_RECONCILES);
1125 0 :
1126 0 : let cancel = CancellationToken::new();
1127 0 : let heartbeater = Heartbeater::new(
1128 0 : config.jwt_token.clone(),
1129 0 : config.max_unavailable_interval,
1130 0 : cancel.clone(),
1131 0 : );
1132 0 : let this = Arc::new(Self {
1133 0 : inner: Arc::new(std::sync::RwLock::new(ServiceState::new(
1134 0 : nodes,
1135 0 : tenants,
1136 0 : scheduler,
1137 0 : delayed_reconcile_rx,
1138 0 : ))),
1139 0 : config: config.clone(),
1140 0 : persistence,
1141 0 : compute_hook: Arc::new(ComputeHook::new(config.clone())),
1142 0 : result_tx,
1143 0 : heartbeater,
1144 0 : reconciler_concurrency: Arc::new(tokio::sync::Semaphore::new(
1145 0 : config.reconciler_concurrency,
1146 0 : )),
1147 0 : delayed_reconcile_tx,
1148 0 : abort_tx,
1149 0 : startup_complete: startup_complete.clone(),
1150 0 : cancel,
1151 0 : gate: Gate::default(),
1152 0 : tenant_op_locks: Default::default(),
1153 0 : node_op_locks: Default::default(),
1154 0 : });
1155 0 :
1156 0 : let result_task_this = this.clone();
1157 0 : tokio::task::spawn(async move {
1158 : // Block shutdown until we're done (we must respect self.cancel)
1159 0 : if let Ok(_gate) = result_task_this.gate.enter() {
1160 0 : result_task_this
1161 0 : .process_results(result_rx, bg_compute_notify_result_rx)
1162 0 : .await
1163 0 : }
1164 0 : });
1165 0 :
1166 0 : tokio::task::spawn({
1167 0 : let this = this.clone();
1168 0 : async move {
1169 : // Block shutdown until we're done (we must respect self.cancel)
1170 0 : if let Ok(_gate) = this.gate.enter() {
1171 0 : this.process_aborts(abort_rx).await
1172 0 : }
1173 0 : }
1174 0 : });
1175 0 :
1176 0 : tokio::task::spawn({
1177 0 : let this = this.clone();
1178 0 : async move {
1179 0 : if let Ok(_gate) = this.gate.enter() {
1180 0 : loop {
1181 0 : tokio::select! {
1182 : _ = this.cancel.cancelled() => {
1183 : break;
1184 : },
1185 : _ = tokio::time::sleep(Duration::from_secs(60)) => {}
1186 0 : };
1187 0 : this.tenant_op_locks.housekeeping();
1188 0 : }
1189 0 : }
1190 0 : }
1191 0 : });
1192 0 :
1193 0 : tokio::task::spawn({
1194 0 : let this = this.clone();
1195 0 : // We will block the [`Service::startup_complete`] barrier until [`Self::startup_reconcile`]
1196 0 : // is done.
1197 0 : let startup_completion = startup_completion.clone();
1198 0 : async move {
1199 : // Block shutdown until we're done (we must respect self.cancel)
1200 0 : let Ok(_gate) = this.gate.enter() else {
1201 0 : return;
1202 : };
1203 :
1204 0 : this.startup_reconcile(bg_compute_notify_result_tx).await;
1205 0 : drop(startup_completion);
1206 0 : }
1207 0 : });
1208 0 :
1209 0 : tokio::task::spawn({
1210 0 : let this = this.clone();
1211 0 : let startup_complete = startup_complete.clone();
1212 0 : async move {
1213 0 : startup_complete.wait().await;
1214 0 : this.background_reconcile().await;
1215 0 : }
1216 0 : });
1217 0 :
1218 0 : tokio::task::spawn({
1219 0 : let this = this.clone();
1220 0 : let startup_complete = startup_complete.clone();
1221 0 : async move {
1222 0 : startup_complete.wait().await;
1223 0 : this.spawn_heartbeat_driver().await;
1224 0 : }
1225 0 : });
1226 0 :
1227 0 : Ok(this)
1228 0 : }
1229 :
1230 0 : pub(crate) async fn attach_hook(
1231 0 : &self,
1232 0 : attach_req: AttachHookRequest,
1233 0 : ) -> anyhow::Result<AttachHookResponse> {
1234 0 : // This is a test hook. To enable using it on tenants that were created directly with
1235 0 : // the pageserver API (not via this service), we will auto-create any missing tenant
1236 0 : // shards with default state.
1237 0 : let insert = {
1238 0 : let locked = self.inner.write().unwrap();
1239 0 : !locked.tenants.contains_key(&attach_req.tenant_shard_id)
1240 0 : };
1241 0 :
1242 0 : if insert {
1243 0 : let tsp = TenantShardPersistence {
1244 0 : tenant_id: attach_req.tenant_shard_id.tenant_id.to_string(),
1245 0 : shard_number: attach_req.tenant_shard_id.shard_number.0 as i32,
1246 0 : shard_count: attach_req.tenant_shard_id.shard_count.literal() as i32,
1247 0 : shard_stripe_size: 0,
1248 0 : generation: attach_req.generation_override.or(Some(0)),
1249 0 : generation_pageserver: None,
1250 0 : placement_policy: serde_json::to_string(&PlacementPolicy::Attached(0)).unwrap(),
1251 0 : config: serde_json::to_string(&TenantConfig::default()).unwrap(),
1252 0 : splitting: SplitState::default(),
1253 0 : scheduling_policy: serde_json::to_string(&ShardSchedulingPolicy::default())
1254 0 : .unwrap(),
1255 0 : };
1256 0 :
1257 0 : match self.persistence.insert_tenant_shards(vec![tsp]).await {
1258 0 : Err(e) => match e {
1259 : DatabaseError::Query(diesel::result::Error::DatabaseError(
1260 : DatabaseErrorKind::UniqueViolation,
1261 : _,
1262 : )) => {
1263 0 : tracing::info!(
1264 0 : "Raced with another request to insert tenant {}",
1265 : attach_req.tenant_shard_id
1266 : )
1267 : }
1268 0 : _ => return Err(e.into()),
1269 : },
1270 : Ok(()) => {
1271 0 : tracing::info!("Inserted shard {} in database", attach_req.tenant_shard_id);
1272 :
1273 0 : let mut locked = self.inner.write().unwrap();
1274 0 : locked.tenants.insert(
1275 0 : attach_req.tenant_shard_id,
1276 0 : TenantShard::new(
1277 0 : attach_req.tenant_shard_id,
1278 0 : ShardIdentity::unsharded(),
1279 0 : PlacementPolicy::Attached(0),
1280 0 : ),
1281 0 : );
1282 0 : tracing::info!("Inserted shard {} in memory", attach_req.tenant_shard_id);
1283 : }
1284 : }
1285 0 : }
1286 :
1287 0 : let new_generation = if let Some(req_node_id) = attach_req.node_id {
1288 0 : let maybe_tenant_conf = {
1289 0 : let locked = self.inner.write().unwrap();
1290 0 : locked
1291 0 : .tenants
1292 0 : .get(&attach_req.tenant_shard_id)
1293 0 : .map(|t| t.config.clone())
1294 0 : };
1295 0 :
1296 0 : match maybe_tenant_conf {
1297 0 : Some(conf) => {
1298 0 : let new_generation = self
1299 0 : .persistence
1300 0 : .increment_generation(attach_req.tenant_shard_id, req_node_id)
1301 0 : .await?;
1302 :
1303 : // Persist the placement policy update. This is required
1304 : // when we reattaching a detached tenant.
1305 0 : self.persistence
1306 0 : .update_tenant_shard(
1307 0 : TenantFilter::Shard(attach_req.tenant_shard_id),
1308 0 : Some(PlacementPolicy::Attached(0)),
1309 0 : Some(conf),
1310 0 : None,
1311 0 : None,
1312 0 : )
1313 0 : .await?;
1314 0 : Some(new_generation)
1315 : }
1316 : None => {
1317 0 : anyhow::bail!("Attach hook handling raced with tenant removal")
1318 : }
1319 : }
1320 : } else {
1321 0 : self.persistence.detach(attach_req.tenant_shard_id).await?;
1322 0 : None
1323 : };
1324 :
1325 0 : let mut locked = self.inner.write().unwrap();
1326 0 : let (_nodes, tenants, scheduler) = locked.parts_mut();
1327 0 :
1328 0 : let tenant_shard = tenants
1329 0 : .get_mut(&attach_req.tenant_shard_id)
1330 0 : .expect("Checked for existence above");
1331 :
1332 0 : if let Some(new_generation) = new_generation {
1333 0 : tenant_shard.generation = Some(new_generation);
1334 0 : tenant_shard.policy = PlacementPolicy::Attached(0);
1335 0 : } else {
1336 : // This is a detach notification. We must update placement policy to avoid re-attaching
1337 : // during background scheduling/reconciliation, or during storage controller restart.
1338 0 : assert!(attach_req.node_id.is_none());
1339 0 : tenant_shard.policy = PlacementPolicy::Detached;
1340 : }
1341 :
1342 0 : if let Some(attaching_pageserver) = attach_req.node_id.as_ref() {
1343 0 : tracing::info!(
1344 : tenant_id = %attach_req.tenant_shard_id,
1345 : ps_id = %attaching_pageserver,
1346 : generation = ?tenant_shard.generation,
1347 0 : "issuing",
1348 : );
1349 0 : } else if let Some(ps_id) = tenant_shard.intent.get_attached() {
1350 0 : tracing::info!(
1351 : tenant_id = %attach_req.tenant_shard_id,
1352 : %ps_id,
1353 : generation = ?tenant_shard.generation,
1354 0 : "dropping",
1355 : );
1356 : } else {
1357 0 : tracing::info!(
1358 : tenant_id = %attach_req.tenant_shard_id,
1359 0 : "no-op: tenant already has no pageserver");
1360 : }
1361 0 : tenant_shard
1362 0 : .intent
1363 0 : .set_attached(scheduler, attach_req.node_id);
1364 0 :
1365 0 : tracing::info!(
1366 0 : "attach_hook: tenant {} set generation {:?}, pageserver {}",
1367 0 : attach_req.tenant_shard_id,
1368 0 : tenant_shard.generation,
1369 0 : // TODO: this is an odd number of 0xf's
1370 0 : attach_req.node_id.unwrap_or(utils::id::NodeId(0xfffffff))
1371 : );
1372 :
1373 : // Trick the reconciler into not doing anything for this tenant: this helps
1374 : // tests that manually configure a tenant on the pagesrever, and then call this
1375 : // attach hook: they don't want background reconciliation to modify what they
1376 : // did to the pageserver.
1377 : #[cfg(feature = "testing")]
1378 : {
1379 0 : if let Some(node_id) = attach_req.node_id {
1380 0 : tenant_shard.observed.locations = HashMap::from([(
1381 0 : node_id,
1382 0 : ObservedStateLocation {
1383 0 : conf: Some(attached_location_conf(
1384 0 : tenant_shard.generation.unwrap(),
1385 0 : &tenant_shard.shard,
1386 0 : &tenant_shard.config,
1387 0 : false,
1388 0 : )),
1389 0 : },
1390 0 : )]);
1391 0 : } else {
1392 0 : tenant_shard.observed.locations.clear();
1393 0 : }
1394 : }
1395 :
1396 0 : Ok(AttachHookResponse {
1397 0 : gen: attach_req
1398 0 : .node_id
1399 0 : .map(|_| tenant_shard.generation.expect("Test hook, not used on tenants that are mid-onboarding with a NULL generation").into().unwrap()),
1400 0 : })
1401 0 : }
1402 :
1403 0 : pub(crate) fn inspect(&self, inspect_req: InspectRequest) -> InspectResponse {
1404 0 : let locked = self.inner.read().unwrap();
1405 0 :
1406 0 : let tenant_shard = locked.tenants.get(&inspect_req.tenant_shard_id);
1407 0 :
1408 0 : InspectResponse {
1409 0 : attachment: tenant_shard.and_then(|s| {
1410 0 : s.intent
1411 0 : .get_attached()
1412 0 : .map(|ps| (s.generation.expect("Test hook, not used on tenants that are mid-onboarding with a NULL generation").into().unwrap(), ps))
1413 0 : }),
1414 0 : }
1415 0 : }
1416 :
1417 : // When the availability state of a node transitions to active, we must do a full reconciliation
1418 : // of LocationConfigs on that node. This is because while a node was offline:
1419 : // - we might have proceeded through startup_reconcile without checking for extraneous LocationConfigs on this node
1420 : // - aborting a tenant shard split might have left rogue child shards behind on this node.
1421 : //
1422 : // This function must complete _before_ setting a `Node` to Active: once it is set to Active, other
1423 : // Reconcilers might communicate with the node, and these must not overlap with the work we do in
1424 : // this function.
1425 : //
1426 : // The reconciliation logic in here is very similar to what [`Self::startup_reconcile`] does, but
1427 : // for written for a single node rather than as a batch job for all nodes.
1428 0 : #[tracing::instrument(skip_all, fields(node_id=%node.get_id()))]
1429 : async fn node_activate_reconcile(
1430 : &self,
1431 : mut node: Node,
1432 : _lock: &WrappedWriteGuard<NodeOperations>,
1433 : ) -> Result<(), ApiError> {
1434 : // This Node is a mutable local copy: we will set it active so that we can use its
1435 : // API client to reconcile with the node. The Node in [`Self::nodes`] will get updated
1436 : // later.
1437 : node.set_availability(NodeAvailability::Active(UtilizationScore::worst()));
1438 :
1439 : let configs = match node
1440 : .with_client_retries(
1441 0 : |client| async move { client.list_location_config().await },
1442 : &self.config.jwt_token,
1443 : 1,
1444 : 5,
1445 : SHORT_RECONCILE_TIMEOUT,
1446 : &self.cancel,
1447 : )
1448 : .await
1449 : {
1450 : None => {
1451 : // We're shutting down (the Node's cancellation token can't have fired, because
1452 : // we're the only scope that has a reference to it, and we didn't fire it).
1453 : return Err(ApiError::ShuttingDown);
1454 : }
1455 : Some(Err(e)) => {
1456 : // This node didn't succeed listing its locations: it may not proceed to active state
1457 : // as it is apparently unavailable.
1458 : return Err(ApiError::PreconditionFailed(
1459 : format!("Failed to query node location configs, cannot activate ({e})").into(),
1460 : ));
1461 : }
1462 : Some(Ok(configs)) => configs,
1463 : };
1464 : tracing::info!("Loaded {} LocationConfigs", configs.tenant_shards.len());
1465 :
1466 : let mut cleanup = Vec::new();
1467 : {
1468 : let mut locked = self.inner.write().unwrap();
1469 :
1470 : for (tenant_shard_id, observed_loc) in configs.tenant_shards {
1471 : let Some(tenant_shard) = locked.tenants.get_mut(&tenant_shard_id) else {
1472 : cleanup.push(tenant_shard_id);
1473 : continue;
1474 : };
1475 : tenant_shard
1476 : .observed
1477 : .locations
1478 : .insert(node.get_id(), ObservedStateLocation { conf: observed_loc });
1479 : }
1480 : }
1481 :
1482 : for tenant_shard_id in cleanup {
1483 : tracing::info!("Detaching {tenant_shard_id}");
1484 : match node
1485 : .with_client_retries(
1486 0 : |client| async move {
1487 0 : let config = LocationConfig {
1488 0 : mode: LocationConfigMode::Detached,
1489 0 : generation: None,
1490 0 : secondary_conf: None,
1491 0 : shard_number: tenant_shard_id.shard_number.0,
1492 0 : shard_count: tenant_shard_id.shard_count.literal(),
1493 0 : shard_stripe_size: 0,
1494 0 : tenant_conf: models::TenantConfig::default(),
1495 0 : };
1496 0 : client
1497 0 : .location_config(tenant_shard_id, config, None, false)
1498 0 : .await
1499 0 : },
1500 : &self.config.jwt_token,
1501 : 1,
1502 : 5,
1503 : SHORT_RECONCILE_TIMEOUT,
1504 : &self.cancel,
1505 : )
1506 : .await
1507 : {
1508 : None => {
1509 : // We're shutting down (the Node's cancellation token can't have fired, because
1510 : // we're the only scope that has a reference to it, and we didn't fire it).
1511 : return Err(ApiError::ShuttingDown);
1512 : }
1513 : Some(Err(e)) => {
1514 : // Do not let the node proceed to Active state if it is not responsive to requests
1515 : // to detach. This could happen if e.g. a shutdown bug in the pageserver is preventing
1516 : // detach completing: we should not let this node back into the set of nodes considered
1517 : // okay for scheduling.
1518 : return Err(ApiError::Conflict(format!(
1519 : "Node {node} failed to detach {tenant_shard_id}: {e}"
1520 : )));
1521 : }
1522 : Some(Ok(_)) => {}
1523 : };
1524 : }
1525 :
1526 : Ok(())
1527 : }
1528 :
1529 0 : pub(crate) async fn re_attach(
1530 0 : &self,
1531 0 : reattach_req: ReAttachRequest,
1532 0 : ) -> Result<ReAttachResponse, ApiError> {
1533 0 : if let Some(register_req) = reattach_req.register {
1534 0 : self.node_register(register_req).await?;
1535 0 : }
1536 :
1537 : // Ordering: we must persist generation number updates before making them visible in the in-memory state
1538 0 : let incremented_generations = self.persistence.re_attach(reattach_req.node_id).await?;
1539 :
1540 0 : tracing::info!(
1541 : node_id=%reattach_req.node_id,
1542 0 : "Incremented {} tenant shards' generations",
1543 0 : incremented_generations.len()
1544 : );
1545 :
1546 : // Apply the updated generation to our in-memory state, and
1547 : // gather discover secondary locations.
1548 0 : let mut locked = self.inner.write().unwrap();
1549 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
1550 0 :
1551 0 : let mut response = ReAttachResponse {
1552 0 : tenants: Vec::new(),
1553 0 : };
1554 :
1555 : // TODO: cancel/restart any running reconciliation for this tenant, it might be trying
1556 : // to call location_conf API with an old generation. Wait for cancellation to complete
1557 : // before responding to this request. Requires well implemented CancellationToken logic
1558 : // all the way to where we call location_conf. Even then, there can still be a location_conf
1559 : // request in flight over the network: TODO handle that by making location_conf API refuse
1560 : // to go backward in generations.
1561 :
1562 : // Scan through all shards, applying updates for ones where we updated generation
1563 : // and identifying shards that intend to have a secondary location on this node.
1564 0 : for (tenant_shard_id, shard) in tenants {
1565 0 : if let Some(new_gen) = incremented_generations.get(tenant_shard_id) {
1566 0 : let new_gen = *new_gen;
1567 0 : response.tenants.push(ReAttachResponseTenant {
1568 0 : id: *tenant_shard_id,
1569 0 : gen: Some(new_gen.into().unwrap()),
1570 0 : // A tenant is only put into multi or stale modes in the middle of a [`Reconciler::live_migrate`]
1571 0 : // execution. If a pageserver is restarted during that process, then the reconcile pass will
1572 0 : // fail, and start from scratch, so it doesn't make sense for us to try and preserve
1573 0 : // the stale/multi states at this point.
1574 0 : mode: LocationConfigMode::AttachedSingle,
1575 0 : });
1576 0 :
1577 0 : shard.generation = std::cmp::max(shard.generation, Some(new_gen));
1578 0 : if let Some(observed) = shard.observed.locations.get_mut(&reattach_req.node_id) {
1579 : // Why can we update `observed` even though we're not sure our response will be received
1580 : // by the pageserver? Because the pageserver will not proceed with startup until
1581 : // it has processed response: if it loses it, we'll see another request and increment
1582 : // generation again, avoiding any uncertainty about dirtiness of tenant's state.
1583 0 : if let Some(conf) = observed.conf.as_mut() {
1584 0 : conf.generation = new_gen.into();
1585 0 : }
1586 0 : } else {
1587 0 : // This node has no observed state for the shard: perhaps it was offline
1588 0 : // when the pageserver restarted. Insert a None, so that the Reconciler
1589 0 : // will be prompted to learn the location's state before it makes changes.
1590 0 : shard
1591 0 : .observed
1592 0 : .locations
1593 0 : .insert(reattach_req.node_id, ObservedStateLocation { conf: None });
1594 0 : }
1595 0 : } else if shard.intent.get_secondary().contains(&reattach_req.node_id) {
1596 0 : // Ordering: pageserver will not accept /location_config requests until it has
1597 0 : // finished processing the response from re-attach. So we can update our in-memory state
1598 0 : // now, and be confident that we are not stamping on the result of some later location config.
1599 0 : // TODO: however, we are not strictly ordered wrt ReconcileResults queue,
1600 0 : // so we might update observed state here, and then get over-written by some racing
1601 0 : // ReconcileResult. The impact is low however, since we have set state on pageserver something
1602 0 : // that matches intent, so worst case if we race then we end up doing a spurious reconcile.
1603 0 :
1604 0 : response.tenants.push(ReAttachResponseTenant {
1605 0 : id: *tenant_shard_id,
1606 0 : gen: None,
1607 0 : mode: LocationConfigMode::Secondary,
1608 0 : });
1609 0 :
1610 0 : // We must not update observed, because we have no guarantee that our
1611 0 : // response will be received by the pageserver. This could leave it
1612 0 : // falsely dirty, but the resulting reconcile should be idempotent.
1613 0 : }
1614 : }
1615 :
1616 : // We consider a node Active once we have composed a re-attach response, but we
1617 : // do not call [`Self::node_activate_reconcile`]: the handling of the re-attach response
1618 : // implicitly synchronizes the LocationConfigs on the node.
1619 : //
1620 : // Setting a node active unblocks any Reconcilers that might write to the location config API,
1621 : // but those requests will not be accepted by the node until it has finished processing
1622 : // the re-attach response.
1623 : //
1624 : // Additionally, reset the nodes scheduling policy to match the conditional update done
1625 : // in [`Persistence::re_attach`].
1626 0 : if let Some(node) = nodes.get(&reattach_req.node_id) {
1627 0 : let reset_scheduling = matches!(
1628 0 : node.get_scheduling(),
1629 : NodeSchedulingPolicy::PauseForRestart
1630 : | NodeSchedulingPolicy::Draining
1631 : | NodeSchedulingPolicy::Filling
1632 : );
1633 :
1634 0 : if !node.is_available() || reset_scheduling {
1635 0 : let mut new_nodes = (**nodes).clone();
1636 0 : if let Some(node) = new_nodes.get_mut(&reattach_req.node_id) {
1637 0 : if !node.is_available() {
1638 0 : node.set_availability(NodeAvailability::Active(UtilizationScore::worst()));
1639 0 : }
1640 :
1641 0 : if reset_scheduling {
1642 0 : node.set_scheduling(NodeSchedulingPolicy::Active);
1643 0 : }
1644 :
1645 0 : scheduler.node_upsert(node);
1646 0 : let new_nodes = Arc::new(new_nodes);
1647 0 : *nodes = new_nodes;
1648 0 : }
1649 0 : }
1650 0 : }
1651 :
1652 0 : Ok(response)
1653 0 : }
1654 :
1655 0 : pub(crate) fn validate(&self, validate_req: ValidateRequest) -> ValidateResponse {
1656 0 : let locked = self.inner.read().unwrap();
1657 0 :
1658 0 : let mut response = ValidateResponse {
1659 0 : tenants: Vec::new(),
1660 0 : };
1661 :
1662 0 : for req_tenant in validate_req.tenants {
1663 0 : if let Some(tenant_shard) = locked.tenants.get(&req_tenant.id) {
1664 0 : let valid = tenant_shard.generation == Some(Generation::new(req_tenant.gen));
1665 0 : tracing::info!(
1666 0 : "handle_validate: {}(gen {}): valid={valid} (latest {:?})",
1667 : req_tenant.id,
1668 : req_tenant.gen,
1669 : tenant_shard.generation
1670 : );
1671 0 : response.tenants.push(ValidateResponseTenant {
1672 0 : id: req_tenant.id,
1673 0 : valid,
1674 0 : });
1675 0 : } else {
1676 0 : // After tenant deletion, we may approve any validation. This avoids
1677 0 : // spurious warnings on the pageserver if it has pending LSN updates
1678 0 : // at the point a deletion happens.
1679 0 : response.tenants.push(ValidateResponseTenant {
1680 0 : id: req_tenant.id,
1681 0 : valid: true,
1682 0 : });
1683 0 : }
1684 : }
1685 0 : response
1686 0 : }
1687 :
1688 0 : pub(crate) async fn tenant_create(
1689 0 : &self,
1690 0 : create_req: TenantCreateRequest,
1691 0 : ) -> Result<TenantCreateResponse, ApiError> {
1692 0 : let tenant_id = create_req.new_tenant_id.tenant_id;
1693 :
1694 : // Exclude any concurrent attempts to create/access the same tenant ID
1695 0 : let _tenant_lock = trace_exclusive_lock(
1696 0 : &self.tenant_op_locks,
1697 0 : create_req.new_tenant_id.tenant_id,
1698 0 : TenantOperations::Create,
1699 0 : )
1700 0 : .await;
1701 0 : let (response, waiters) = self.do_tenant_create(create_req).await?;
1702 :
1703 0 : if let Err(e) = self.await_waiters(waiters, RECONCILE_TIMEOUT).await {
1704 : // Avoid deadlock: reconcile may fail while notifying compute, if the cloud control plane refuses to
1705 : // accept compute notifications while it is in the process of creating. Reconciliation will
1706 : // be retried in the background.
1707 0 : tracing::warn!(%tenant_id, "Reconcile not done yet while creating tenant ({e})");
1708 0 : }
1709 0 : Ok(response)
1710 0 : }
1711 :
1712 0 : pub(crate) async fn do_tenant_create(
1713 0 : &self,
1714 0 : create_req: TenantCreateRequest,
1715 0 : ) -> Result<(TenantCreateResponse, Vec<ReconcilerWaiter>), ApiError> {
1716 0 : let placement_policy = create_req
1717 0 : .placement_policy
1718 0 : .clone()
1719 0 : // As a default, zero secondaries is convenient for tests that don't choose a policy.
1720 0 : .unwrap_or(PlacementPolicy::Attached(0));
1721 :
1722 : // This service expects to handle sharding itself: it is an error to try and directly create
1723 : // a particular shard here.
1724 0 : let tenant_id = if !create_req.new_tenant_id.is_unsharded() {
1725 0 : return Err(ApiError::BadRequest(anyhow::anyhow!(
1726 0 : "Attempted to create a specific shard, this API is for creating the whole tenant"
1727 0 : )));
1728 : } else {
1729 0 : create_req.new_tenant_id.tenant_id
1730 0 : };
1731 0 :
1732 0 : tracing::info!(
1733 0 : "Creating tenant {}, shard_count={:?}",
1734 : create_req.new_tenant_id,
1735 : create_req.shard_parameters.count,
1736 : );
1737 :
1738 0 : let create_ids = (0..create_req.shard_parameters.count.count())
1739 0 : .map(|i| TenantShardId {
1740 0 : tenant_id,
1741 0 : shard_number: ShardNumber(i),
1742 0 : shard_count: create_req.shard_parameters.count,
1743 0 : })
1744 0 : .collect::<Vec<_>>();
1745 :
1746 : // If the caller specifies a None generation, it means "start from default". This is different
1747 : // to [`Self::tenant_location_config`], where a None generation is used to represent
1748 : // an incompletely-onboarded tenant.
1749 0 : let initial_generation = if matches!(placement_policy, PlacementPolicy::Secondary) {
1750 0 : tracing::info!(
1751 0 : "tenant_create: secondary mode, generation is_some={}",
1752 0 : create_req.generation.is_some()
1753 : );
1754 0 : create_req.generation.map(Generation::new)
1755 : } else {
1756 0 : tracing::info!(
1757 0 : "tenant_create: not secondary mode, generation is_some={}",
1758 0 : create_req.generation.is_some()
1759 : );
1760 0 : Some(
1761 0 : create_req
1762 0 : .generation
1763 0 : .map(Generation::new)
1764 0 : .unwrap_or(INITIAL_GENERATION),
1765 0 : )
1766 : };
1767 :
1768 : // Ordering: we persist tenant shards before creating them on the pageserver. This enables a caller
1769 : // to clean up after themselves by issuing a tenant deletion if something goes wrong and we restart
1770 : // during the creation, rather than risking leaving orphan objects in S3.
1771 0 : let persist_tenant_shards = create_ids
1772 0 : .iter()
1773 0 : .map(|tenant_shard_id| TenantShardPersistence {
1774 0 : tenant_id: tenant_shard_id.tenant_id.to_string(),
1775 0 : shard_number: tenant_shard_id.shard_number.0 as i32,
1776 0 : shard_count: tenant_shard_id.shard_count.literal() as i32,
1777 0 : shard_stripe_size: create_req.shard_parameters.stripe_size.0 as i32,
1778 0 : generation: initial_generation.map(|g| g.into().unwrap() as i32),
1779 0 : // The pageserver is not known until scheduling happens: we will set this column when
1780 0 : // incrementing the generation the first time we attach to a pageserver.
1781 0 : generation_pageserver: None,
1782 0 : placement_policy: serde_json::to_string(&placement_policy).unwrap(),
1783 0 : config: serde_json::to_string(&create_req.config).unwrap(),
1784 0 : splitting: SplitState::default(),
1785 0 : scheduling_policy: serde_json::to_string(&ShardSchedulingPolicy::default())
1786 0 : .unwrap(),
1787 0 : })
1788 0 : .collect();
1789 0 :
1790 0 : match self
1791 0 : .persistence
1792 0 : .insert_tenant_shards(persist_tenant_shards)
1793 0 : .await
1794 : {
1795 0 : Ok(_) => {}
1796 : Err(DatabaseError::Query(diesel::result::Error::DatabaseError(
1797 : DatabaseErrorKind::UniqueViolation,
1798 : _,
1799 : ))) => {
1800 : // Unique key violation: this is probably a retry. Because the shard count is part of the unique key,
1801 : // if we see a unique key violation it means that the creation request's shard count matches the previous
1802 : // creation's shard count.
1803 0 : tracing::info!("Tenant shards already present in database, proceeding with idempotent creation...");
1804 : }
1805 : // Any other database error is unexpected and a bug.
1806 0 : Err(e) => return Err(ApiError::InternalServerError(anyhow::anyhow!(e))),
1807 : };
1808 :
1809 0 : let mut schedule_context = ScheduleContext::default();
1810 :
1811 0 : let (waiters, response_shards) = {
1812 0 : let mut locked = self.inner.write().unwrap();
1813 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
1814 0 :
1815 0 : let mut response_shards = Vec::new();
1816 0 : let mut schcedule_error = None;
1817 :
1818 0 : for tenant_shard_id in create_ids {
1819 0 : tracing::info!("Creating shard {tenant_shard_id}...");
1820 :
1821 : use std::collections::btree_map::Entry;
1822 0 : match tenants.entry(tenant_shard_id) {
1823 0 : Entry::Occupied(mut entry) => {
1824 0 : tracing::info!(
1825 0 : "Tenant shard {tenant_shard_id} already exists while creating"
1826 : );
1827 :
1828 : // TODO: schedule() should take an anti-affinity expression that pushes
1829 : // attached and secondary locations (independently) away frorm those
1830 : // pageservers also holding a shard for this tenant.
1831 :
1832 0 : entry
1833 0 : .get_mut()
1834 0 : .schedule(scheduler, &mut schedule_context)
1835 0 : .map_err(|e| {
1836 0 : ApiError::Conflict(format!(
1837 0 : "Failed to schedule shard {tenant_shard_id}: {e}"
1838 0 : ))
1839 0 : })?;
1840 :
1841 0 : if let Some(node_id) = entry.get().intent.get_attached() {
1842 0 : let generation = entry
1843 0 : .get()
1844 0 : .generation
1845 0 : .expect("Generation is set when in attached mode");
1846 0 : response_shards.push(TenantCreateResponseShard {
1847 0 : shard_id: tenant_shard_id,
1848 0 : node_id: *node_id,
1849 0 : generation: generation.into().unwrap(),
1850 0 : });
1851 0 : }
1852 :
1853 0 : continue;
1854 : }
1855 0 : Entry::Vacant(entry) => {
1856 0 : let state = entry.insert(TenantShard::new(
1857 0 : tenant_shard_id,
1858 0 : ShardIdentity::from_params(
1859 0 : tenant_shard_id.shard_number,
1860 0 : &create_req.shard_parameters,
1861 0 : ),
1862 0 : placement_policy.clone(),
1863 0 : ));
1864 0 :
1865 0 : state.generation = initial_generation;
1866 0 : state.config = create_req.config.clone();
1867 0 : if let Err(e) = state.schedule(scheduler, &mut schedule_context) {
1868 0 : schcedule_error = Some(e);
1869 0 : }
1870 :
1871 : // Only include shards in result if we are attaching: the purpose
1872 : // of the response is to tell the caller where the shards are attached.
1873 0 : if let Some(node_id) = state.intent.get_attached() {
1874 0 : let generation = state
1875 0 : .generation
1876 0 : .expect("Generation is set when in attached mode");
1877 0 : response_shards.push(TenantCreateResponseShard {
1878 0 : shard_id: tenant_shard_id,
1879 0 : node_id: *node_id,
1880 0 : generation: generation.into().unwrap(),
1881 0 : });
1882 0 : }
1883 : }
1884 : };
1885 : }
1886 :
1887 : // If we failed to schedule shards, then they are still created in the controller,
1888 : // but we return an error to the requester to avoid a silent failure when someone
1889 : // tries to e.g. create a tenant whose placement policy requires more nodes than
1890 : // are present in the system. We do this here rather than in the above loop, to
1891 : // avoid situations where we only create a subset of shards in the tenant.
1892 0 : if let Some(e) = schcedule_error {
1893 0 : return Err(ApiError::Conflict(format!(
1894 0 : "Failed to schedule shard(s): {e}"
1895 0 : )));
1896 0 : }
1897 0 :
1898 0 : let waiters = tenants
1899 0 : .range_mut(TenantShardId::tenant_range(tenant_id))
1900 0 : .filter_map(|(_shard_id, shard)| self.maybe_reconcile_shard(shard, nodes))
1901 0 : .collect::<Vec<_>>();
1902 0 : (waiters, response_shards)
1903 0 : };
1904 0 :
1905 0 : Ok((
1906 0 : TenantCreateResponse {
1907 0 : shards: response_shards,
1908 0 : },
1909 0 : waiters,
1910 0 : ))
1911 0 : }
1912 :
1913 : /// Helper for functions that reconcile a number of shards, and would like to do a timeout-bounded
1914 : /// wait for reconciliation to complete before responding.
1915 0 : async fn await_waiters(
1916 0 : &self,
1917 0 : waiters: Vec<ReconcilerWaiter>,
1918 0 : timeout: Duration,
1919 0 : ) -> Result<(), ReconcileWaitError> {
1920 0 : let deadline = Instant::now().checked_add(timeout).unwrap();
1921 0 : for waiter in waiters {
1922 0 : let timeout = deadline.duration_since(Instant::now());
1923 0 : waiter.wait_timeout(timeout).await?;
1924 : }
1925 :
1926 0 : Ok(())
1927 0 : }
1928 :
1929 : /// Same as [`Service::await_waiters`], but returns the waiters which are still
1930 : /// in progress
1931 0 : async fn await_waiters_remainder(
1932 0 : &self,
1933 0 : waiters: Vec<ReconcilerWaiter>,
1934 0 : timeout: Duration,
1935 0 : ) -> Vec<ReconcilerWaiter> {
1936 0 : let deadline = Instant::now().checked_add(timeout).unwrap();
1937 0 : for waiter in waiters.iter() {
1938 0 : let timeout = deadline.duration_since(Instant::now());
1939 0 : let _ = waiter.wait_timeout(timeout).await;
1940 : }
1941 :
1942 0 : waiters
1943 0 : .into_iter()
1944 0 : .filter(|waiter| matches!(waiter.get_status(), ReconcilerStatus::InProgress))
1945 0 : .collect::<Vec<_>>()
1946 0 : }
1947 :
1948 : /// Part of [`Self::tenant_location_config`]: dissect an incoming location config request,
1949 : /// and transform it into either a tenant creation of a series of shard updates.
1950 : ///
1951 : /// If the incoming request makes no changes, a [`TenantCreateOrUpdate::Update`] result will
1952 : /// still be returned.
1953 0 : fn tenant_location_config_prepare(
1954 0 : &self,
1955 0 : tenant_id: TenantId,
1956 0 : req: TenantLocationConfigRequest,
1957 0 : ) -> TenantCreateOrUpdate {
1958 0 : let mut updates = Vec::new();
1959 0 : let mut locked = self.inner.write().unwrap();
1960 0 : let (nodes, tenants, _scheduler) = locked.parts_mut();
1961 0 : let tenant_shard_id = TenantShardId::unsharded(tenant_id);
1962 :
1963 : // Use location config mode as an indicator of policy.
1964 0 : let placement_policy = match req.config.mode {
1965 0 : LocationConfigMode::Detached => PlacementPolicy::Detached,
1966 0 : LocationConfigMode::Secondary => PlacementPolicy::Secondary,
1967 : LocationConfigMode::AttachedMulti
1968 : | LocationConfigMode::AttachedSingle
1969 : | LocationConfigMode::AttachedStale => {
1970 0 : if nodes.len() > 1 {
1971 0 : PlacementPolicy::Attached(1)
1972 : } else {
1973 : // Convenience for dev/test: if we just have one pageserver, import
1974 : // tenants into non-HA mode so that scheduling will succeed.
1975 0 : PlacementPolicy::Attached(0)
1976 : }
1977 : }
1978 : };
1979 :
1980 0 : let mut create = true;
1981 0 : for (shard_id, shard) in tenants.range_mut(TenantShardId::tenant_range(tenant_id)) {
1982 : // Saw an existing shard: this is not a creation
1983 0 : create = false;
1984 :
1985 : // Shards may have initially been created by a Secondary request, where we
1986 : // would have left generation as None.
1987 : //
1988 : // We only update generation the first time we see an attached-mode request,
1989 : // and if there is no existing generation set. The caller is responsible for
1990 : // ensuring that no non-storage-controller pageserver ever uses a higher
1991 : // generation than they passed in here.
1992 : use LocationConfigMode::*;
1993 0 : let set_generation = match req.config.mode {
1994 0 : AttachedMulti | AttachedSingle | AttachedStale if shard.generation.is_none() => {
1995 0 : req.config.generation.map(Generation::new)
1996 : }
1997 0 : _ => None,
1998 : };
1999 :
2000 0 : updates.push(ShardUpdate {
2001 0 : tenant_shard_id: *shard_id,
2002 0 : placement_policy: placement_policy.clone(),
2003 0 : tenant_config: req.config.tenant_conf.clone(),
2004 0 : generation: set_generation,
2005 0 : });
2006 : }
2007 :
2008 0 : if create {
2009 : use LocationConfigMode::*;
2010 0 : let generation = match req.config.mode {
2011 0 : AttachedMulti | AttachedSingle | AttachedStale => req.config.generation,
2012 : // If a caller provided a generation in a non-attached request, ignore it
2013 : // and leave our generation as None: this enables a subsequent update to set
2014 : // the generation when setting an attached mode for the first time.
2015 0 : _ => None,
2016 : };
2017 :
2018 0 : TenantCreateOrUpdate::Create(
2019 0 : // Synthesize a creation request
2020 0 : TenantCreateRequest {
2021 0 : new_tenant_id: tenant_shard_id,
2022 0 : generation,
2023 0 : shard_parameters: ShardParameters {
2024 0 : count: tenant_shard_id.shard_count,
2025 0 : // We only import un-sharded or single-sharded tenants, so stripe
2026 0 : // size can be made up arbitrarily here.
2027 0 : stripe_size: ShardParameters::DEFAULT_STRIPE_SIZE,
2028 0 : },
2029 0 : placement_policy: Some(placement_policy),
2030 0 : config: req.config.tenant_conf,
2031 0 : },
2032 0 : )
2033 : } else {
2034 0 : assert!(!updates.is_empty());
2035 0 : TenantCreateOrUpdate::Update(updates)
2036 : }
2037 0 : }
2038 :
2039 : /// This API is used by the cloud control plane to migrate unsharded tenants that it created
2040 : /// directly with pageservers into this service.
2041 : ///
2042 : /// Cloud control plane MUST NOT continue issuing GENERATION NUMBERS for this tenant once it
2043 : /// has attempted to call this API. Failure to oblige to this rule may lead to S3 corruption.
2044 : /// Think of the first attempt to call this API as a transfer of absolute authority over the
2045 : /// tenant's source of generation numbers.
2046 : ///
2047 : /// The mode in this request coarse-grained control of tenants:
2048 : /// - Call with mode Attached* to upsert the tenant.
2049 : /// - Call with mode Secondary to either onboard a tenant without attaching it, or
2050 : /// to set an existing tenant to PolicyMode::Secondary
2051 : /// - Call with mode Detached to switch to PolicyMode::Detached
2052 0 : pub(crate) async fn tenant_location_config(
2053 0 : &self,
2054 0 : tenant_shard_id: TenantShardId,
2055 0 : req: TenantLocationConfigRequest,
2056 0 : ) -> Result<TenantLocationConfigResponse, ApiError> {
2057 : // We require an exclusive lock, because we are updating both persistent and in-memory state
2058 0 : let _tenant_lock = trace_exclusive_lock(
2059 0 : &self.tenant_op_locks,
2060 0 : tenant_shard_id.tenant_id,
2061 0 : TenantOperations::LocationConfig,
2062 0 : )
2063 0 : .await;
2064 :
2065 0 : if !tenant_shard_id.is_unsharded() {
2066 0 : return Err(ApiError::BadRequest(anyhow::anyhow!(
2067 0 : "This API is for importing single-sharded or unsharded tenants"
2068 0 : )));
2069 0 : }
2070 0 :
2071 0 : // First check if this is a creation or an update
2072 0 : let create_or_update = self.tenant_location_config_prepare(tenant_shard_id.tenant_id, req);
2073 0 :
2074 0 : let mut result = TenantLocationConfigResponse {
2075 0 : shards: Vec::new(),
2076 0 : stripe_size: None,
2077 0 : };
2078 0 : let waiters = match create_or_update {
2079 0 : TenantCreateOrUpdate::Create(create_req) => {
2080 0 : let (create_resp, waiters) = self.do_tenant_create(create_req).await?;
2081 0 : result.shards = create_resp
2082 0 : .shards
2083 0 : .into_iter()
2084 0 : .map(|s| TenantShardLocation {
2085 0 : node_id: s.node_id,
2086 0 : shard_id: s.shard_id,
2087 0 : })
2088 0 : .collect();
2089 0 : waiters
2090 : }
2091 0 : TenantCreateOrUpdate::Update(updates) => {
2092 0 : // Persist updates
2093 0 : // Ordering: write to the database before applying changes in-memory, so that
2094 0 : // we will not appear time-travel backwards on a restart.
2095 0 : let mut schedule_context = ScheduleContext::default();
2096 : for ShardUpdate {
2097 0 : tenant_shard_id,
2098 0 : placement_policy,
2099 0 : tenant_config,
2100 0 : generation,
2101 0 : } in &updates
2102 : {
2103 0 : self.persistence
2104 0 : .update_tenant_shard(
2105 0 : TenantFilter::Shard(*tenant_shard_id),
2106 0 : Some(placement_policy.clone()),
2107 0 : Some(tenant_config.clone()),
2108 0 : *generation,
2109 0 : None,
2110 0 : )
2111 0 : .await?;
2112 : }
2113 :
2114 : // Apply updates in-memory
2115 0 : let mut waiters = Vec::new();
2116 0 : {
2117 0 : let mut locked = self.inner.write().unwrap();
2118 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
2119 :
2120 : for ShardUpdate {
2121 0 : tenant_shard_id,
2122 0 : placement_policy,
2123 0 : tenant_config,
2124 0 : generation: update_generation,
2125 0 : } in updates
2126 : {
2127 0 : let Some(shard) = tenants.get_mut(&tenant_shard_id) else {
2128 0 : tracing::warn!("Shard {tenant_shard_id} removed while updating");
2129 0 : continue;
2130 : };
2131 :
2132 : // Update stripe size
2133 0 : if result.stripe_size.is_none() && shard.shard.count.count() > 1 {
2134 0 : result.stripe_size = Some(shard.shard.stripe_size);
2135 0 : }
2136 :
2137 0 : shard.policy = placement_policy;
2138 0 : shard.config = tenant_config;
2139 0 : if let Some(generation) = update_generation {
2140 0 : shard.generation = Some(generation);
2141 0 : }
2142 :
2143 0 : shard.schedule(scheduler, &mut schedule_context)?;
2144 :
2145 0 : let maybe_waiter = self.maybe_reconcile_shard(shard, nodes);
2146 0 : if let Some(waiter) = maybe_waiter {
2147 0 : waiters.push(waiter);
2148 0 : }
2149 :
2150 0 : if let Some(node_id) = shard.intent.get_attached() {
2151 0 : result.shards.push(TenantShardLocation {
2152 0 : shard_id: tenant_shard_id,
2153 0 : node_id: *node_id,
2154 0 : })
2155 0 : }
2156 : }
2157 : }
2158 0 : waiters
2159 : }
2160 : };
2161 :
2162 0 : if let Err(e) = self.await_waiters(waiters, SHORT_RECONCILE_TIMEOUT).await {
2163 : // Do not treat a reconcile error as fatal: we have already applied any requested
2164 : // Intent changes, and the reconcile can fail for external reasons like unavailable
2165 : // compute notification API. In these cases, it is important that we do not
2166 : // cause the cloud control plane to retry forever on this API.
2167 0 : tracing::warn!(
2168 0 : "Failed to reconcile after /location_config: {e}, returning success anyway"
2169 : );
2170 0 : }
2171 :
2172 : // Logging the full result is useful because it lets us cross-check what the cloud control
2173 : // plane's tenant_shards table should contain.
2174 0 : tracing::info!("Complete, returning {result:?}");
2175 :
2176 0 : Ok(result)
2177 0 : }
2178 :
2179 0 : pub(crate) async fn tenant_config_set(&self, req: TenantConfigRequest) -> Result<(), ApiError> {
2180 : // We require an exclusive lock, because we are updating persistent and in-memory state
2181 0 : let _tenant_lock = trace_exclusive_lock(
2182 0 : &self.tenant_op_locks,
2183 0 : req.tenant_id,
2184 0 : TenantOperations::ConfigSet,
2185 0 : )
2186 0 : .await;
2187 :
2188 0 : let tenant_id = req.tenant_id;
2189 0 : let config = req.config;
2190 0 :
2191 0 : self.persistence
2192 0 : .update_tenant_shard(
2193 0 : TenantFilter::Tenant(req.tenant_id),
2194 0 : None,
2195 0 : Some(config.clone()),
2196 0 : None,
2197 0 : None,
2198 0 : )
2199 0 : .await?;
2200 :
2201 0 : let waiters = {
2202 0 : let mut waiters = Vec::new();
2203 0 : let mut locked = self.inner.write().unwrap();
2204 0 : let (nodes, tenants, _scheduler) = locked.parts_mut();
2205 0 : for (_shard_id, shard) in tenants.range_mut(TenantShardId::tenant_range(tenant_id)) {
2206 0 : shard.config = config.clone();
2207 0 : if let Some(waiter) = self.maybe_reconcile_shard(shard, nodes) {
2208 0 : waiters.push(waiter);
2209 0 : }
2210 : }
2211 0 : waiters
2212 : };
2213 :
2214 0 : if let Err(e) = self.await_waiters(waiters, SHORT_RECONCILE_TIMEOUT).await {
2215 : // Treat this as success because we have stored the configuration. If e.g.
2216 : // a node was unavailable at this time, it should not stop us accepting a
2217 : // configuration change.
2218 0 : tracing::warn!(%tenant_id, "Accepted configuration update but reconciliation failed: {e}");
2219 0 : }
2220 :
2221 0 : Ok(())
2222 0 : }
2223 :
2224 0 : pub(crate) fn tenant_config_get(
2225 0 : &self,
2226 0 : tenant_id: TenantId,
2227 0 : ) -> Result<HashMap<&str, serde_json::Value>, ApiError> {
2228 0 : let config = {
2229 0 : let locked = self.inner.read().unwrap();
2230 0 :
2231 0 : match locked
2232 0 : .tenants
2233 0 : .range(TenantShardId::tenant_range(tenant_id))
2234 0 : .next()
2235 : {
2236 0 : Some((_tenant_shard_id, shard)) => shard.config.clone(),
2237 : None => {
2238 0 : return Err(ApiError::NotFound(
2239 0 : anyhow::anyhow!("Tenant not found").into(),
2240 0 : ))
2241 : }
2242 : }
2243 : };
2244 :
2245 : // Unlike the pageserver, we do not have a set of global defaults: the config is
2246 : // entirely per-tenant. Therefore the distinction between `tenant_specific_overrides`
2247 : // and `effective_config` in the response is meaningless, but we retain that syntax
2248 : // in order to remain compatible with the pageserver API.
2249 :
2250 0 : let response = HashMap::from([
2251 : (
2252 : "tenant_specific_overrides",
2253 0 : serde_json::to_value(&config)
2254 0 : .context("serializing tenant specific overrides")
2255 0 : .map_err(ApiError::InternalServerError)?,
2256 : ),
2257 : (
2258 0 : "effective_config",
2259 0 : serde_json::to_value(&config)
2260 0 : .context("serializing effective config")
2261 0 : .map_err(ApiError::InternalServerError)?,
2262 : ),
2263 : ]);
2264 :
2265 0 : Ok(response)
2266 0 : }
2267 :
2268 0 : pub(crate) async fn tenant_time_travel_remote_storage(
2269 0 : &self,
2270 0 : time_travel_req: &TenantTimeTravelRequest,
2271 0 : tenant_id: TenantId,
2272 0 : timestamp: Cow<'_, str>,
2273 0 : done_if_after: Cow<'_, str>,
2274 0 : ) -> Result<(), ApiError> {
2275 0 : let _tenant_lock = trace_exclusive_lock(
2276 0 : &self.tenant_op_locks,
2277 0 : tenant_id,
2278 0 : TenantOperations::TimeTravelRemoteStorage,
2279 0 : )
2280 0 : .await;
2281 :
2282 0 : let node = {
2283 0 : let locked = self.inner.read().unwrap();
2284 : // Just a sanity check to prevent misuse: the API expects that the tenant is fully
2285 : // detached everywhere, and nothing writes to S3 storage. Here, we verify that,
2286 : // but only at the start of the process, so it's really just to prevent operator
2287 : // mistakes.
2288 0 : for (shard_id, shard) in locked.tenants.range(TenantShardId::tenant_range(tenant_id)) {
2289 0 : if shard.intent.get_attached().is_some() || !shard.intent.get_secondary().is_empty()
2290 : {
2291 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
2292 0 : "We want tenant to be attached in shard with tenant_shard_id={shard_id}"
2293 0 : )));
2294 0 : }
2295 0 : let maybe_attached = shard
2296 0 : .observed
2297 0 : .locations
2298 0 : .iter()
2299 0 : .filter_map(|(node_id, observed_location)| {
2300 0 : observed_location
2301 0 : .conf
2302 0 : .as_ref()
2303 0 : .map(|loc| (node_id, observed_location, loc.mode))
2304 0 : })
2305 0 : .find(|(_, _, mode)| *mode != LocationConfigMode::Detached);
2306 0 : if let Some((node_id, _observed_location, mode)) = maybe_attached {
2307 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!("We observed attached={mode:?} tenant in node_id={node_id} shard with tenant_shard_id={shard_id}")));
2308 0 : }
2309 : }
2310 0 : let scheduler = &locked.scheduler;
2311 : // Right now we only perform the operation on a single node without parallelization
2312 : // TODO fan out the operation to multiple nodes for better performance
2313 0 : let node_id = scheduler.schedule_shard(&[], &ScheduleContext::default())?;
2314 0 : let node = locked
2315 0 : .nodes
2316 0 : .get(&node_id)
2317 0 : .expect("Pageservers may not be deleted while lock is active");
2318 0 : node.clone()
2319 0 : };
2320 0 :
2321 0 : // The shard count is encoded in the remote storage's URL, so we need to handle all historically used shard counts
2322 0 : let mut counts = time_travel_req
2323 0 : .shard_counts
2324 0 : .iter()
2325 0 : .copied()
2326 0 : .collect::<HashSet<_>>()
2327 0 : .into_iter()
2328 0 : .collect::<Vec<_>>();
2329 0 : counts.sort_unstable();
2330 :
2331 0 : for count in counts {
2332 0 : let shard_ids = (0..count.count())
2333 0 : .map(|i| TenantShardId {
2334 0 : tenant_id,
2335 0 : shard_number: ShardNumber(i),
2336 0 : shard_count: count,
2337 0 : })
2338 0 : .collect::<Vec<_>>();
2339 0 : for tenant_shard_id in shard_ids {
2340 0 : let client = PageserverClient::new(
2341 0 : node.get_id(),
2342 0 : node.base_url(),
2343 0 : self.config.jwt_token.as_deref(),
2344 0 : );
2345 0 :
2346 0 : tracing::info!("Doing time travel recovery for shard {tenant_shard_id}",);
2347 :
2348 0 : client
2349 0 : .tenant_time_travel_remote_storage(
2350 0 : tenant_shard_id,
2351 0 : ×tamp,
2352 0 : &done_if_after,
2353 0 : )
2354 0 : .await
2355 0 : .map_err(|e| {
2356 0 : ApiError::InternalServerError(anyhow::anyhow!(
2357 0 : "Error doing time travel recovery for shard {tenant_shard_id} on node {}: {e}",
2358 0 : node
2359 0 : ))
2360 0 : })?;
2361 : }
2362 : }
2363 0 : Ok(())
2364 0 : }
2365 :
2366 0 : pub(crate) async fn tenant_secondary_download(
2367 0 : &self,
2368 0 : tenant_id: TenantId,
2369 0 : wait: Option<Duration>,
2370 0 : ) -> Result<(StatusCode, SecondaryProgress), ApiError> {
2371 0 : let _tenant_lock = trace_shared_lock(
2372 0 : &self.tenant_op_locks,
2373 0 : tenant_id,
2374 0 : TenantOperations::SecondaryDownload,
2375 0 : )
2376 0 : .await;
2377 :
2378 : // Acquire lock and yield the collection of shard-node tuples which we will send requests onward to
2379 0 : let targets = {
2380 0 : let locked = self.inner.read().unwrap();
2381 0 : let mut targets = Vec::new();
2382 :
2383 0 : for (tenant_shard_id, shard) in
2384 0 : locked.tenants.range(TenantShardId::tenant_range(tenant_id))
2385 : {
2386 0 : for node_id in shard.intent.get_secondary() {
2387 0 : let node = locked
2388 0 : .nodes
2389 0 : .get(node_id)
2390 0 : .expect("Pageservers may not be deleted while referenced");
2391 0 :
2392 0 : targets.push((*tenant_shard_id, node.clone()));
2393 0 : }
2394 : }
2395 0 : targets
2396 0 : };
2397 0 :
2398 0 : // Issue concurrent requests to all shards' locations
2399 0 : let mut futs = FuturesUnordered::new();
2400 0 : for (tenant_shard_id, node) in targets {
2401 0 : let client = PageserverClient::new(
2402 0 : node.get_id(),
2403 0 : node.base_url(),
2404 0 : self.config.jwt_token.as_deref(),
2405 0 : );
2406 0 : futs.push(async move {
2407 0 : let result = client
2408 0 : .tenant_secondary_download(tenant_shard_id, wait)
2409 0 : .await;
2410 0 : (result, node, tenant_shard_id)
2411 0 : })
2412 : }
2413 :
2414 : // Handle any errors returned by pageservers. This includes cases like this request racing with
2415 : // a scheduling operation, such that the tenant shard we're calling doesn't exist on that pageserver any more, as
2416 : // well as more general cases like 503s, 500s, or timeouts.
2417 0 : let mut aggregate_progress = SecondaryProgress::default();
2418 0 : let mut aggregate_status: Option<StatusCode> = None;
2419 0 : let mut error: Option<mgmt_api::Error> = None;
2420 0 : while let Some((result, node, tenant_shard_id)) = futs.next().await {
2421 0 : match result {
2422 0 : Err(e) => {
2423 0 : // Secondary downloads are always advisory: if something fails, we nevertheless report success, so that whoever
2424 0 : // is calling us will proceed with whatever migration they're doing, albeit with a slightly less warm cache
2425 0 : // than they had hoped for.
2426 0 : tracing::warn!("Secondary download error from pageserver {node}: {e}",);
2427 0 : error = Some(e)
2428 : }
2429 0 : Ok((status_code, progress)) => {
2430 0 : tracing::info!(%tenant_shard_id, "Shard status={status_code} progress: {progress:?}");
2431 0 : aggregate_progress.layers_downloaded += progress.layers_downloaded;
2432 0 : aggregate_progress.layers_total += progress.layers_total;
2433 0 : aggregate_progress.bytes_downloaded += progress.bytes_downloaded;
2434 0 : aggregate_progress.bytes_total += progress.bytes_total;
2435 0 : aggregate_progress.heatmap_mtime =
2436 0 : std::cmp::max(aggregate_progress.heatmap_mtime, progress.heatmap_mtime);
2437 0 : aggregate_status = match aggregate_status {
2438 0 : None => Some(status_code),
2439 0 : Some(StatusCode::OK) => Some(status_code),
2440 0 : Some(cur) => {
2441 0 : // Other status codes (e.g. 202) -- do not overwrite.
2442 0 : Some(cur)
2443 : }
2444 : };
2445 : }
2446 : }
2447 : }
2448 :
2449 : // If any of the shards return 202, indicate our result as 202.
2450 0 : match aggregate_status {
2451 : None => {
2452 0 : match error {
2453 0 : Some(e) => {
2454 0 : // No successes, and an error: surface it
2455 0 : Err(ApiError::Conflict(format!("Error from pageserver: {e}")))
2456 : }
2457 : None => {
2458 : // No shards found
2459 0 : Err(ApiError::NotFound(
2460 0 : anyhow::anyhow!("Tenant {} not found", tenant_id).into(),
2461 0 : ))
2462 : }
2463 : }
2464 : }
2465 0 : Some(aggregate_status) => Ok((aggregate_status, aggregate_progress)),
2466 : }
2467 0 : }
2468 :
2469 0 : pub(crate) async fn tenant_delete(&self, tenant_id: TenantId) -> Result<StatusCode, ApiError> {
2470 0 : let _tenant_lock =
2471 0 : trace_exclusive_lock(&self.tenant_op_locks, tenant_id, TenantOperations::Delete).await;
2472 :
2473 : // Detach all shards
2474 0 : let (detach_waiters, shard_ids, node) = {
2475 0 : let mut shard_ids = Vec::new();
2476 0 : let mut detach_waiters = Vec::new();
2477 0 : let mut locked = self.inner.write().unwrap();
2478 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
2479 0 : for (tenant_shard_id, shard) in
2480 0 : tenants.range_mut(TenantShardId::tenant_range(tenant_id))
2481 : {
2482 0 : shard_ids.push(*tenant_shard_id);
2483 0 :
2484 0 : // Update the tenant's intent to remove all attachments
2485 0 : shard.policy = PlacementPolicy::Detached;
2486 0 : shard
2487 0 : .schedule(scheduler, &mut ScheduleContext::default())
2488 0 : .expect("De-scheduling is infallible");
2489 0 : debug_assert!(shard.intent.get_attached().is_none());
2490 0 : debug_assert!(shard.intent.get_secondary().is_empty());
2491 :
2492 0 : if let Some(waiter) = self.maybe_reconcile_shard(shard, nodes) {
2493 0 : detach_waiters.push(waiter);
2494 0 : }
2495 : }
2496 :
2497 : // Pick an arbitrary node to use for remote deletions (does not have to be where the tenant
2498 : // was attached, just has to be able to see the S3 content)
2499 0 : let node_id = scheduler.schedule_shard(&[], &ScheduleContext::default())?;
2500 0 : let node = nodes
2501 0 : .get(&node_id)
2502 0 : .expect("Pageservers may not be deleted while lock is active");
2503 0 : (detach_waiters, shard_ids, node.clone())
2504 0 : };
2505 0 :
2506 0 : // This reconcile wait can fail in a few ways:
2507 0 : // A there is a very long queue for the reconciler semaphore
2508 0 : // B some pageserver is failing to handle a detach promptly
2509 0 : // C some pageserver goes offline right at the moment we send it a request.
2510 0 : //
2511 0 : // A and C are transient: the semaphore will eventually become available, and once a node is marked offline
2512 0 : // the next attempt to reconcile will silently skip detaches for an offline node and succeed. If B happens,
2513 0 : // it's a bug, and needs resolving at the pageserver level (we shouldn't just leave attachments behind while
2514 0 : // deleting the underlying data).
2515 0 : self.await_waiters(detach_waiters, RECONCILE_TIMEOUT)
2516 0 : .await?;
2517 :
2518 0 : let locations = shard_ids
2519 0 : .into_iter()
2520 0 : .map(|s| (s, node.clone()))
2521 0 : .collect::<Vec<_>>();
2522 0 : let results = self.tenant_for_shards_api(
2523 0 : locations,
2524 0 : |tenant_shard_id, client| async move { client.tenant_delete(tenant_shard_id).await },
2525 0 : 1,
2526 0 : 3,
2527 0 : RECONCILE_TIMEOUT,
2528 0 : &self.cancel,
2529 0 : )
2530 0 : .await;
2531 0 : for result in results {
2532 0 : match result {
2533 : Ok(StatusCode::ACCEPTED) => {
2534 : // This should never happen: we waited for detaches to finish above
2535 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
2536 0 : "Unexpectedly still attached on {}",
2537 0 : node
2538 0 : )));
2539 : }
2540 0 : Ok(_) => {}
2541 : Err(mgmt_api::Error::Cancelled) => {
2542 0 : return Err(ApiError::ShuttingDown);
2543 : }
2544 0 : Err(e) => {
2545 0 : // This is unexpected: remote deletion should be infallible, unless the object store
2546 0 : // at large is unavailable.
2547 0 : tracing::error!("Error deleting via node {}: {e}", node);
2548 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(e)));
2549 : }
2550 : }
2551 : }
2552 :
2553 : // Fall through: deletion of the tenant on pageservers is complete, we may proceed to drop
2554 : // our in-memory state and database state.
2555 :
2556 : // Ordering: we delete persistent state first: if we then
2557 : // crash, we will drop the in-memory state.
2558 :
2559 : // Drop persistent state.
2560 0 : self.persistence.delete_tenant(tenant_id).await?;
2561 :
2562 : // Drop in-memory state
2563 : {
2564 0 : let mut locked = self.inner.write().unwrap();
2565 0 : let (_nodes, tenants, scheduler) = locked.parts_mut();
2566 :
2567 : // Dereference Scheduler from shards before dropping them
2568 0 : for (_tenant_shard_id, shard) in
2569 0 : tenants.range_mut(TenantShardId::tenant_range(tenant_id))
2570 0 : {
2571 0 : shard.intent.clear(scheduler);
2572 0 : }
2573 :
2574 0 : tenants.retain(|tenant_shard_id, _shard| tenant_shard_id.tenant_id != tenant_id);
2575 0 : tracing::info!(
2576 0 : "Deleted tenant {tenant_id}, now have {} tenants",
2577 0 : locked.tenants.len()
2578 : );
2579 : };
2580 :
2581 : // Success is represented as 404, to imitate the existing pageserver deletion API
2582 0 : Ok(StatusCode::NOT_FOUND)
2583 0 : }
2584 :
2585 : /// Naming: this configures the storage controller's policies for a tenant, whereas [`Self::tenant_config_set`] is "set the TenantConfig"
2586 : /// for a tenant. The TenantConfig is passed through to pageservers, whereas this function modifies
2587 : /// the tenant's policies (configuration) within the storage controller
2588 0 : pub(crate) async fn tenant_update_policy(
2589 0 : &self,
2590 0 : tenant_id: TenantId,
2591 0 : req: TenantPolicyRequest,
2592 0 : ) -> Result<(), ApiError> {
2593 : // We require an exclusive lock, because we are updating persistent and in-memory state
2594 0 : let _tenant_lock = trace_exclusive_lock(
2595 0 : &self.tenant_op_locks,
2596 0 : tenant_id,
2597 0 : TenantOperations::UpdatePolicy,
2598 0 : )
2599 0 : .await;
2600 :
2601 0 : failpoint_support::sleep_millis_async!("tenant-update-policy-exclusive-lock");
2602 :
2603 : let TenantPolicyRequest {
2604 0 : placement,
2605 0 : scheduling,
2606 0 : } = req;
2607 0 :
2608 0 : self.persistence
2609 0 : .update_tenant_shard(
2610 0 : TenantFilter::Tenant(tenant_id),
2611 0 : placement.clone(),
2612 0 : None,
2613 0 : None,
2614 0 : scheduling,
2615 0 : )
2616 0 : .await?;
2617 :
2618 0 : let mut schedule_context = ScheduleContext::default();
2619 0 : let mut locked = self.inner.write().unwrap();
2620 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
2621 0 : for (shard_id, shard) in tenants.range_mut(TenantShardId::tenant_range(tenant_id)) {
2622 0 : if let Some(placement) = &placement {
2623 0 : shard.policy = placement.clone();
2624 0 :
2625 0 : tracing::info!(tenant_id=%shard_id.tenant_id, shard_id=%shard_id.shard_slug(),
2626 0 : "Updated placement policy to {placement:?}");
2627 0 : }
2628 :
2629 0 : if let Some(scheduling) = &scheduling {
2630 0 : shard.set_scheduling_policy(*scheduling);
2631 0 :
2632 0 : tracing::info!(tenant_id=%shard_id.tenant_id, shard_id=%shard_id.shard_slug(),
2633 0 : "Updated scheduling policy to {scheduling:?}");
2634 0 : }
2635 :
2636 : // In case scheduling is being switched back on, try it now.
2637 0 : shard.schedule(scheduler, &mut schedule_context).ok();
2638 0 : self.maybe_reconcile_shard(shard, nodes);
2639 : }
2640 :
2641 0 : Ok(())
2642 0 : }
2643 :
2644 0 : pub(crate) async fn tenant_timeline_create(
2645 0 : &self,
2646 0 : tenant_id: TenantId,
2647 0 : mut create_req: TimelineCreateRequest,
2648 0 : ) -> Result<TimelineInfo, ApiError> {
2649 0 : tracing::info!(
2650 0 : "Creating timeline {}/{}",
2651 : tenant_id,
2652 : create_req.new_timeline_id,
2653 : );
2654 :
2655 0 : let _tenant_lock = trace_shared_lock(
2656 0 : &self.tenant_op_locks,
2657 0 : tenant_id,
2658 0 : TenantOperations::TimelineCreate,
2659 0 : )
2660 0 : .await;
2661 :
2662 0 : self.ensure_attached_wait(tenant_id).await?;
2663 :
2664 0 : let mut targets = {
2665 0 : let locked = self.inner.read().unwrap();
2666 0 : let mut targets = Vec::new();
2667 :
2668 0 : for (tenant_shard_id, shard) in
2669 0 : locked.tenants.range(TenantShardId::tenant_range(tenant_id))
2670 0 : {
2671 0 : let node_id = shard.intent.get_attached().ok_or_else(|| {
2672 0 : ApiError::InternalServerError(anyhow::anyhow!("Shard not scheduled"))
2673 0 : })?;
2674 0 : let node = locked
2675 0 : .nodes
2676 0 : .get(&node_id)
2677 0 : .expect("Pageservers may not be deleted while referenced");
2678 0 :
2679 0 : targets.push((*tenant_shard_id, node.clone()));
2680 : }
2681 0 : targets
2682 0 : };
2683 0 :
2684 0 : if targets.is_empty() {
2685 0 : return Err(ApiError::NotFound(
2686 0 : anyhow::anyhow!("Tenant not found").into(),
2687 0 : ));
2688 0 : };
2689 0 : let shard_zero = targets.remove(0);
2690 :
2691 0 : async fn create_one(
2692 0 : tenant_shard_id: TenantShardId,
2693 0 : node: Node,
2694 0 : jwt: Option<String>,
2695 0 : create_req: TimelineCreateRequest,
2696 0 : ) -> Result<TimelineInfo, ApiError> {
2697 0 : tracing::info!(
2698 0 : "Creating timeline on shard {}/{}, attached to node {node}",
2699 : tenant_shard_id,
2700 : create_req.new_timeline_id,
2701 : );
2702 0 : let client = PageserverClient::new(node.get_id(), node.base_url(), jwt.as_deref());
2703 0 :
2704 0 : client
2705 0 : .timeline_create(tenant_shard_id, &create_req)
2706 0 : .await
2707 0 : .map_err(|e| passthrough_api_error(&node, e))
2708 0 : }
2709 :
2710 : // Because the caller might not provide an explicit LSN, we must do the creation first on a single shard, and then
2711 : // use whatever LSN that shard picked when creating on subsequent shards. We arbitrarily use shard zero as the shard
2712 : // that will get the first creation request, and propagate the LSN to all the >0 shards.
2713 0 : let timeline_info = create_one(
2714 0 : shard_zero.0,
2715 0 : shard_zero.1,
2716 0 : self.config.jwt_token.clone(),
2717 0 : create_req.clone(),
2718 0 : )
2719 0 : .await?;
2720 :
2721 : // Propagate the LSN that shard zero picked, if caller didn't provide one
2722 0 : if create_req.ancestor_timeline_id.is_some() && create_req.ancestor_start_lsn.is_none() {
2723 0 : create_req.ancestor_start_lsn = timeline_info.ancestor_lsn;
2724 0 : }
2725 :
2726 : // Create timeline on remaining shards with number >0
2727 0 : if !targets.is_empty() {
2728 : // If we had multiple shards, issue requests for the remainder now.
2729 0 : let jwt = self.config.jwt_token.clone();
2730 0 : self.tenant_for_shards(targets, |tenant_shard_id: TenantShardId, node: Node| {
2731 0 : let create_req = create_req.clone();
2732 0 : Box::pin(create_one(tenant_shard_id, node, jwt.clone(), create_req))
2733 0 : })
2734 0 : .await?;
2735 0 : }
2736 :
2737 0 : Ok(timeline_info)
2738 0 : }
2739 :
2740 : /// Helper for concurrently calling a pageserver API on a number of shards, such as timeline creation.
2741 : ///
2742 : /// On success, the returned vector contains exactly the same number of elements as the input `locations`.
2743 0 : async fn tenant_for_shards<F, R>(
2744 0 : &self,
2745 0 : locations: Vec<(TenantShardId, Node)>,
2746 0 : mut req_fn: F,
2747 0 : ) -> Result<Vec<R>, ApiError>
2748 0 : where
2749 0 : F: FnMut(
2750 0 : TenantShardId,
2751 0 : Node,
2752 0 : )
2753 0 : -> std::pin::Pin<Box<dyn futures::Future<Output = Result<R, ApiError>> + Send>>,
2754 0 : {
2755 0 : let mut futs = FuturesUnordered::new();
2756 0 : let mut results = Vec::with_capacity(locations.len());
2757 :
2758 0 : for (tenant_shard_id, node) in locations {
2759 0 : futs.push(req_fn(tenant_shard_id, node));
2760 0 : }
2761 :
2762 0 : while let Some(r) = futs.next().await {
2763 0 : results.push(r?);
2764 : }
2765 :
2766 0 : Ok(results)
2767 0 : }
2768 :
2769 : /// Concurrently invoke a pageserver API call on many shards at once
2770 0 : pub(crate) async fn tenant_for_shards_api<T, O, F>(
2771 0 : &self,
2772 0 : locations: Vec<(TenantShardId, Node)>,
2773 0 : op: O,
2774 0 : warn_threshold: u32,
2775 0 : max_retries: u32,
2776 0 : timeout: Duration,
2777 0 : cancel: &CancellationToken,
2778 0 : ) -> Vec<mgmt_api::Result<T>>
2779 0 : where
2780 0 : O: Fn(TenantShardId, PageserverClient) -> F + Copy,
2781 0 : F: std::future::Future<Output = mgmt_api::Result<T>>,
2782 0 : {
2783 0 : let mut futs = FuturesUnordered::new();
2784 0 : let mut results = Vec::with_capacity(locations.len());
2785 :
2786 0 : for (tenant_shard_id, node) in locations {
2787 0 : futs.push(async move {
2788 0 : node.with_client_retries(
2789 0 : |client| op(tenant_shard_id, client),
2790 0 : &self.config.jwt_token,
2791 0 : warn_threshold,
2792 0 : max_retries,
2793 0 : timeout,
2794 0 : cancel,
2795 0 : )
2796 0 : .await
2797 0 : });
2798 0 : }
2799 :
2800 0 : while let Some(r) = futs.next().await {
2801 0 : let r = r.unwrap_or(Err(mgmt_api::Error::Cancelled));
2802 0 : results.push(r);
2803 0 : }
2804 :
2805 0 : results
2806 0 : }
2807 :
2808 0 : pub(crate) async fn tenant_timeline_delete(
2809 0 : &self,
2810 0 : tenant_id: TenantId,
2811 0 : timeline_id: TimelineId,
2812 0 : ) -> Result<StatusCode, ApiError> {
2813 0 : tracing::info!("Deleting timeline {}/{}", tenant_id, timeline_id,);
2814 0 : let _tenant_lock = trace_shared_lock(
2815 0 : &self.tenant_op_locks,
2816 0 : tenant_id,
2817 0 : TenantOperations::TimelineDelete,
2818 0 : )
2819 0 : .await;
2820 :
2821 0 : self.ensure_attached_wait(tenant_id).await?;
2822 :
2823 0 : let mut targets = {
2824 0 : let locked = self.inner.read().unwrap();
2825 0 : let mut targets = Vec::new();
2826 :
2827 0 : for (tenant_shard_id, shard) in
2828 0 : locked.tenants.range(TenantShardId::tenant_range(tenant_id))
2829 0 : {
2830 0 : let node_id = shard.intent.get_attached().ok_or_else(|| {
2831 0 : ApiError::InternalServerError(anyhow::anyhow!("Shard not scheduled"))
2832 0 : })?;
2833 0 : let node = locked
2834 0 : .nodes
2835 0 : .get(&node_id)
2836 0 : .expect("Pageservers may not be deleted while referenced");
2837 0 :
2838 0 : targets.push((*tenant_shard_id, node.clone()));
2839 : }
2840 0 : targets
2841 0 : };
2842 0 :
2843 0 : if targets.is_empty() {
2844 0 : return Err(ApiError::NotFound(
2845 0 : anyhow::anyhow!("Tenant not found").into(),
2846 0 : ));
2847 0 : }
2848 0 : let shard_zero = targets.remove(0);
2849 :
2850 0 : async fn delete_one(
2851 0 : tenant_shard_id: TenantShardId,
2852 0 : timeline_id: TimelineId,
2853 0 : node: Node,
2854 0 : jwt: Option<String>,
2855 0 : ) -> Result<StatusCode, ApiError> {
2856 0 : tracing::info!(
2857 0 : "Deleting timeline on shard {tenant_shard_id}/{timeline_id}, attached to node {node}",
2858 : );
2859 :
2860 0 : let client = PageserverClient::new(node.get_id(), node.base_url(), jwt.as_deref());
2861 0 : client
2862 0 : .timeline_delete(tenant_shard_id, timeline_id)
2863 0 : .await
2864 0 : .map_err(|e| {
2865 0 : ApiError::InternalServerError(anyhow::anyhow!(
2866 0 : "Error deleting timeline {timeline_id} on {tenant_shard_id} on node {node}: {e}",
2867 0 : ))
2868 0 : })
2869 0 : }
2870 :
2871 0 : let statuses = self
2872 0 : .tenant_for_shards(targets, |tenant_shard_id: TenantShardId, node: Node| {
2873 0 : Box::pin(delete_one(
2874 0 : tenant_shard_id,
2875 0 : timeline_id,
2876 0 : node,
2877 0 : self.config.jwt_token.clone(),
2878 0 : ))
2879 0 : })
2880 0 : .await?;
2881 :
2882 : // If any shards >0 haven't finished deletion yet, don't start deletion on shard zero
2883 0 : if statuses.iter().any(|s| s != &StatusCode::NOT_FOUND) {
2884 0 : return Ok(StatusCode::ACCEPTED);
2885 0 : }
2886 :
2887 : // Delete shard zero last: this is not strictly necessary, but since a caller's GET on a timeline will be routed
2888 : // to shard zero, it gives a more obvious behavior that a GET returns 404 once the deletion is done.
2889 0 : let shard_zero_status = delete_one(
2890 0 : shard_zero.0,
2891 0 : timeline_id,
2892 0 : shard_zero.1,
2893 0 : self.config.jwt_token.clone(),
2894 0 : )
2895 0 : .await?;
2896 :
2897 0 : Ok(shard_zero_status)
2898 0 : }
2899 :
2900 : /// When you need to send an HTTP request to the pageserver that holds shard0 of a tenant, this
2901 : /// function looks up and returns node. If the tenant isn't found, returns Err(ApiError::NotFound)
2902 0 : pub(crate) fn tenant_shard0_node(
2903 0 : &self,
2904 0 : tenant_id: TenantId,
2905 0 : ) -> Result<(Node, TenantShardId), ApiError> {
2906 0 : let locked = self.inner.read().unwrap();
2907 0 : let Some((tenant_shard_id, shard)) = locked
2908 0 : .tenants
2909 0 : .range(TenantShardId::tenant_range(tenant_id))
2910 0 : .next()
2911 : else {
2912 0 : return Err(ApiError::NotFound(
2913 0 : anyhow::anyhow!("Tenant {tenant_id} not found").into(),
2914 0 : ));
2915 : };
2916 :
2917 : // TODO: should use the ID last published to compute_hook, rather than the intent: the intent might
2918 : // point to somewhere we haven't attached yet.
2919 0 : let Some(node_id) = shard.intent.get_attached() else {
2920 0 : tracing::warn!(
2921 0 : tenant_id=%tenant_shard_id.tenant_id, shard_id=%tenant_shard_id.shard_slug(),
2922 0 : "Shard not scheduled (policy {:?}), cannot generate pass-through URL",
2923 : shard.policy
2924 : );
2925 0 : return Err(ApiError::Conflict(
2926 0 : "Cannot call timeline API on non-attached tenant".to_string(),
2927 0 : ));
2928 : };
2929 :
2930 0 : let Some(node) = locked.nodes.get(node_id) else {
2931 : // This should never happen
2932 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
2933 0 : "Shard refers to nonexistent node"
2934 0 : )));
2935 : };
2936 :
2937 0 : Ok((node.clone(), *tenant_shard_id))
2938 0 : }
2939 :
2940 0 : pub(crate) fn tenant_locate(
2941 0 : &self,
2942 0 : tenant_id: TenantId,
2943 0 : ) -> Result<TenantLocateResponse, ApiError> {
2944 0 : let locked = self.inner.read().unwrap();
2945 0 : tracing::info!("Locating shards for tenant {tenant_id}");
2946 :
2947 0 : let mut result = Vec::new();
2948 0 : let mut shard_params: Option<ShardParameters> = None;
2949 :
2950 0 : for (tenant_shard_id, shard) in locked.tenants.range(TenantShardId::tenant_range(tenant_id))
2951 : {
2952 0 : let node_id =
2953 0 : shard
2954 0 : .intent
2955 0 : .get_attached()
2956 0 : .ok_or(ApiError::BadRequest(anyhow::anyhow!(
2957 0 : "Cannot locate a tenant that is not attached"
2958 0 : )))?;
2959 :
2960 0 : let node = locked
2961 0 : .nodes
2962 0 : .get(&node_id)
2963 0 : .expect("Pageservers may not be deleted while referenced");
2964 0 :
2965 0 : result.push(node.shard_location(*tenant_shard_id));
2966 0 :
2967 0 : match &shard_params {
2968 0 : None => {
2969 0 : shard_params = Some(ShardParameters {
2970 0 : stripe_size: shard.shard.stripe_size,
2971 0 : count: shard.shard.count,
2972 0 : });
2973 0 : }
2974 0 : Some(params) => {
2975 0 : if params.stripe_size != shard.shard.stripe_size {
2976 : // This should never happen. We enforce at runtime because it's simpler than
2977 : // adding an extra per-tenant data structure to store the things that should be the same
2978 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
2979 0 : "Inconsistent shard stripe size parameters!"
2980 0 : )));
2981 0 : }
2982 : }
2983 : }
2984 : }
2985 :
2986 0 : if result.is_empty() {
2987 0 : return Err(ApiError::NotFound(
2988 0 : anyhow::anyhow!("No shards for this tenant ID found").into(),
2989 0 : ));
2990 0 : }
2991 0 : let shard_params = shard_params.expect("result is non-empty, therefore this is set");
2992 0 : tracing::info!(
2993 0 : "Located tenant {} with params {:?} on shards {}",
2994 0 : tenant_id,
2995 0 : shard_params,
2996 0 : result
2997 0 : .iter()
2998 0 : .map(|s| format!("{:?}", s))
2999 0 : .collect::<Vec<_>>()
3000 0 : .join(",")
3001 : );
3002 :
3003 0 : Ok(TenantLocateResponse {
3004 0 : shards: result,
3005 0 : shard_params,
3006 0 : })
3007 0 : }
3008 :
3009 : /// Returns None if the input iterator of shards does not include a shard with number=0
3010 0 : fn tenant_describe_impl<'a>(
3011 0 : &self,
3012 0 : shards: impl Iterator<Item = &'a TenantShard>,
3013 0 : ) -> Option<TenantDescribeResponse> {
3014 0 : let mut shard_zero = None;
3015 0 : let mut describe_shards = Vec::new();
3016 :
3017 0 : for shard in shards {
3018 0 : if shard.tenant_shard_id.is_shard_zero() {
3019 0 : shard_zero = Some(shard);
3020 0 : }
3021 :
3022 0 : describe_shards.push(TenantDescribeResponseShard {
3023 0 : tenant_shard_id: shard.tenant_shard_id,
3024 0 : node_attached: *shard.intent.get_attached(),
3025 0 : node_secondary: shard.intent.get_secondary().to_vec(),
3026 0 : last_error: shard
3027 0 : .last_error
3028 0 : .lock()
3029 0 : .unwrap()
3030 0 : .as_ref()
3031 0 : .map(|e| format!("{e}"))
3032 0 : .unwrap_or("".to_string())
3033 0 : .clone(),
3034 0 : is_reconciling: shard.reconciler.is_some(),
3035 0 : is_pending_compute_notification: shard.pending_compute_notification,
3036 0 : is_splitting: matches!(shard.splitting, SplitState::Splitting),
3037 0 : scheduling_policy: *shard.get_scheduling_policy(),
3038 : })
3039 : }
3040 :
3041 0 : let shard_zero = shard_zero?;
3042 :
3043 0 : Some(TenantDescribeResponse {
3044 0 : tenant_id: shard_zero.tenant_shard_id.tenant_id,
3045 0 : shards: describe_shards,
3046 0 : stripe_size: shard_zero.shard.stripe_size,
3047 0 : policy: shard_zero.policy.clone(),
3048 0 : config: shard_zero.config.clone(),
3049 0 : })
3050 0 : }
3051 :
3052 0 : pub(crate) fn tenant_describe(
3053 0 : &self,
3054 0 : tenant_id: TenantId,
3055 0 : ) -> Result<TenantDescribeResponse, ApiError> {
3056 0 : let locked = self.inner.read().unwrap();
3057 0 :
3058 0 : self.tenant_describe_impl(
3059 0 : locked
3060 0 : .tenants
3061 0 : .range(TenantShardId::tenant_range(tenant_id))
3062 0 : .map(|(_k, v)| v),
3063 0 : )
3064 0 : .ok_or_else(|| ApiError::NotFound(anyhow::anyhow!("Tenant {tenant_id} not found").into()))
3065 0 : }
3066 :
3067 0 : pub(crate) fn tenant_list(&self) -> Vec<TenantDescribeResponse> {
3068 0 : let locked = self.inner.read().unwrap();
3069 0 :
3070 0 : let mut result = Vec::new();
3071 0 : for (_tenant_id, tenant_shards) in
3072 0 : &locked.tenants.iter().group_by(|(id, _shard)| id.tenant_id)
3073 0 : {
3074 0 : result.push(
3075 0 : self.tenant_describe_impl(tenant_shards.map(|(_k, v)| v))
3076 0 : .expect("Groups are always non-empty"),
3077 0 : );
3078 0 : }
3079 :
3080 0 : result
3081 0 : }
3082 :
3083 0 : #[instrument(skip_all, fields(tenant_id=%op.tenant_id))]
3084 : async fn abort_tenant_shard_split(
3085 : &self,
3086 : op: &TenantShardSplitAbort,
3087 : ) -> Result<(), TenantShardSplitAbortError> {
3088 : // Cleaning up a split:
3089 : // - Parent shards are not destroyed during a split, just detached.
3090 : // - Failed pageserver split API calls can leave the remote node with just the parent attached,
3091 : // just the children attached, or both.
3092 : //
3093 : // Therefore our work to do is to:
3094 : // 1. Clean up storage controller's internal state to just refer to parents, no children
3095 : // 2. Call out to pageservers to ensure that children are detached
3096 : // 3. Call out to pageservers to ensure that parents are attached.
3097 : //
3098 : // Crash safety:
3099 : // - If the storage controller stops running during this cleanup *after* clearing the splitting state
3100 : // from our database, then [`Self::startup_reconcile`] will regard child attachments as garbage
3101 : // and detach them.
3102 : // - TODO: If the storage controller stops running during this cleanup *before* clearing the splitting state
3103 : // from our database, then we will re-enter this cleanup routine on startup.
3104 :
3105 : let TenantShardSplitAbort {
3106 : tenant_id,
3107 : new_shard_count,
3108 : new_stripe_size,
3109 : ..
3110 : } = op;
3111 :
3112 : // First abort persistent state, if any exists.
3113 : match self
3114 : .persistence
3115 : .abort_shard_split(*tenant_id, *new_shard_count)
3116 : .await?
3117 : {
3118 : AbortShardSplitStatus::Aborted => {
3119 : // Proceed to roll back any child shards created on pageservers
3120 : }
3121 : AbortShardSplitStatus::Complete => {
3122 : // The split completed (we might hit that path if e.g. our database transaction
3123 : // to write the completion landed in the database, but we dropped connection
3124 : // before seeing the result).
3125 : //
3126 : // We must update in-memory state to reflect the successful split.
3127 : self.tenant_shard_split_commit_inmem(
3128 : *tenant_id,
3129 : *new_shard_count,
3130 : *new_stripe_size,
3131 : );
3132 : return Ok(());
3133 : }
3134 : }
3135 :
3136 : // Clean up in-memory state, and accumulate the list of child locations that need detaching
3137 : let detach_locations: Vec<(Node, TenantShardId)> = {
3138 : let mut detach_locations = Vec::new();
3139 : let mut locked = self.inner.write().unwrap();
3140 : let (nodes, tenants, scheduler) = locked.parts_mut();
3141 :
3142 : for (tenant_shard_id, shard) in
3143 : tenants.range_mut(TenantShardId::tenant_range(op.tenant_id))
3144 : {
3145 : if shard.shard.count == op.new_shard_count {
3146 : // Surprising: the phase of [`Self::do_tenant_shard_split`] which inserts child shards in-memory
3147 : // is infallible, so if we got an error we shouldn't have got that far.
3148 : tracing::warn!(
3149 : "During split abort, child shard {tenant_shard_id} found in-memory"
3150 : );
3151 : continue;
3152 : }
3153 :
3154 : // Add the children of this shard to this list of things to detach
3155 : if let Some(node_id) = shard.intent.get_attached() {
3156 : for child_id in tenant_shard_id.split(*new_shard_count) {
3157 : detach_locations.push((
3158 : nodes
3159 : .get(node_id)
3160 : .expect("Intent references nonexistent node")
3161 : .clone(),
3162 : child_id,
3163 : ));
3164 : }
3165 : } else {
3166 : tracing::warn!(
3167 : "During split abort, shard {tenant_shard_id} has no attached location"
3168 : );
3169 : }
3170 :
3171 : tracing::info!("Restoring parent shard {tenant_shard_id}");
3172 : shard.splitting = SplitState::Idle;
3173 : if let Err(e) = shard.schedule(scheduler, &mut ScheduleContext::default()) {
3174 : // If this shard can't be scheduled now (perhaps due to offline nodes or
3175 : // capacity issues), that must not prevent us rolling back a split. In this
3176 : // case it should be eventually scheduled in the background.
3177 : tracing::warn!("Failed to schedule {tenant_shard_id} during shard abort: {e}")
3178 : }
3179 :
3180 : self.maybe_reconcile_shard(shard, nodes);
3181 : }
3182 :
3183 : // We don't expect any new_shard_count shards to exist here, but drop them just in case
3184 0 : tenants.retain(|_id, s| s.shard.count != *new_shard_count);
3185 :
3186 : detach_locations
3187 : };
3188 :
3189 : for (node, child_id) in detach_locations {
3190 : if !node.is_available() {
3191 : // An unavailable node cannot be cleaned up now: to avoid blocking forever, we will permit this, and
3192 : // rely on the reconciliation that happens when a node transitions to Active to clean up. Since we have
3193 : // removed child shards from our in-memory state and database, the reconciliation will implicitly remove
3194 : // them from the node.
3195 : tracing::warn!("Node {node} unavailable, can't clean up during split abort. It will be cleaned up when it is reactivated.");
3196 : continue;
3197 : }
3198 :
3199 : // Detach the remote child. If the pageserver split API call is still in progress, this call will get
3200 : // a 503 and retry, up to our limit.
3201 : tracing::info!("Detaching {child_id} on {node}...");
3202 : match node
3203 : .with_client_retries(
3204 0 : |client| async move {
3205 0 : let config = LocationConfig {
3206 0 : mode: LocationConfigMode::Detached,
3207 0 : generation: None,
3208 0 : secondary_conf: None,
3209 0 : shard_number: child_id.shard_number.0,
3210 0 : shard_count: child_id.shard_count.literal(),
3211 0 : // Stripe size and tenant config don't matter when detaching
3212 0 : shard_stripe_size: 0,
3213 0 : tenant_conf: TenantConfig::default(),
3214 0 : };
3215 0 :
3216 0 : client.location_config(child_id, config, None, false).await
3217 0 : },
3218 : &self.config.jwt_token,
3219 : 1,
3220 : 10,
3221 : Duration::from_secs(5),
3222 : &self.cancel,
3223 : )
3224 : .await
3225 : {
3226 : Some(Ok(_)) => {}
3227 : Some(Err(e)) => {
3228 : // We failed to communicate with the remote node. This is problematic: we may be
3229 : // leaving it with a rogue child shard.
3230 : tracing::warn!(
3231 : "Failed to detach child {child_id} from node {node} during abort"
3232 : );
3233 : return Err(e.into());
3234 : }
3235 : None => {
3236 : // Cancellation: we were shutdown or the node went offline. Shutdown is fine, we'll
3237 : // clean up on restart. The node going offline requires a retry.
3238 : return Err(TenantShardSplitAbortError::Unavailable);
3239 : }
3240 : };
3241 : }
3242 :
3243 : tracing::info!("Successfully aborted split");
3244 : Ok(())
3245 : }
3246 :
3247 : /// Infallible final stage of [`Self::tenant_shard_split`]: update the contents
3248 : /// of the tenant map to reflect the child shards that exist after the split.
3249 0 : fn tenant_shard_split_commit_inmem(
3250 0 : &self,
3251 0 : tenant_id: TenantId,
3252 0 : new_shard_count: ShardCount,
3253 0 : new_stripe_size: Option<ShardStripeSize>,
3254 0 : ) -> (
3255 0 : TenantShardSplitResponse,
3256 0 : Vec<(TenantShardId, NodeId, ShardStripeSize)>,
3257 0 : Vec<ReconcilerWaiter>,
3258 0 : ) {
3259 0 : let mut response = TenantShardSplitResponse {
3260 0 : new_shards: Vec::new(),
3261 0 : };
3262 0 : let mut child_locations = Vec::new();
3263 0 : let mut waiters = Vec::new();
3264 0 :
3265 0 : {
3266 0 : let mut locked = self.inner.write().unwrap();
3267 0 :
3268 0 : let parent_ids = locked
3269 0 : .tenants
3270 0 : .range(TenantShardId::tenant_range(tenant_id))
3271 0 : .map(|(shard_id, _)| *shard_id)
3272 0 : .collect::<Vec<_>>();
3273 0 :
3274 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
3275 0 : for parent_id in parent_ids {
3276 0 : let child_ids = parent_id.split(new_shard_count);
3277 :
3278 0 : let (pageserver, generation, policy, parent_ident, config) = {
3279 0 : let mut old_state = tenants
3280 0 : .remove(&parent_id)
3281 0 : .expect("It was present, we just split it");
3282 0 :
3283 0 : // A non-splitting state is impossible, because [`Self::tenant_shard_split`] holds
3284 0 : // a TenantId lock and passes it through to [`TenantShardSplitAbort`] in case of cleanup:
3285 0 : // nothing else can clear this.
3286 0 : assert!(matches!(old_state.splitting, SplitState::Splitting));
3287 :
3288 0 : let old_attached = old_state.intent.get_attached().unwrap();
3289 0 : old_state.intent.clear(scheduler);
3290 0 : let generation = old_state.generation.expect("Shard must have been attached");
3291 0 : (
3292 0 : old_attached,
3293 0 : generation,
3294 0 : old_state.policy,
3295 0 : old_state.shard,
3296 0 : old_state.config,
3297 0 : )
3298 0 : };
3299 0 :
3300 0 : let mut schedule_context = ScheduleContext::default();
3301 0 : for child in child_ids {
3302 0 : let mut child_shard = parent_ident;
3303 0 : child_shard.number = child.shard_number;
3304 0 : child_shard.count = child.shard_count;
3305 0 : if let Some(stripe_size) = new_stripe_size {
3306 0 : child_shard.stripe_size = stripe_size;
3307 0 : }
3308 :
3309 0 : let mut child_observed: HashMap<NodeId, ObservedStateLocation> = HashMap::new();
3310 0 : child_observed.insert(
3311 0 : pageserver,
3312 0 : ObservedStateLocation {
3313 0 : conf: Some(attached_location_conf(
3314 0 : generation,
3315 0 : &child_shard,
3316 0 : &config,
3317 0 : matches!(policy, PlacementPolicy::Attached(n) if n > 0),
3318 : )),
3319 : },
3320 : );
3321 :
3322 0 : let mut child_state = TenantShard::new(child, child_shard, policy.clone());
3323 0 : child_state.intent = IntentState::single(scheduler, Some(pageserver));
3324 0 : child_state.observed = ObservedState {
3325 0 : locations: child_observed,
3326 0 : };
3327 0 : child_state.generation = Some(generation);
3328 0 : child_state.config = config.clone();
3329 0 :
3330 0 : // The child's TenantShard::splitting is intentionally left at the default value of Idle,
3331 0 : // as at this point in the split process we have succeeded and this part is infallible:
3332 0 : // we will never need to do any special recovery from this state.
3333 0 :
3334 0 : child_locations.push((child, pageserver, child_shard.stripe_size));
3335 :
3336 0 : if let Err(e) = child_state.schedule(scheduler, &mut schedule_context) {
3337 : // This is not fatal, because we've implicitly already got an attached
3338 : // location for the child shard. Failure here just means we couldn't
3339 : // find a secondary (e.g. because cluster is overloaded).
3340 0 : tracing::warn!("Failed to schedule child shard {child}: {e}");
3341 0 : }
3342 : // In the background, attach secondary locations for the new shards
3343 0 : if let Some(waiter) = self.maybe_reconcile_shard(&mut child_state, nodes) {
3344 0 : waiters.push(waiter);
3345 0 : }
3346 :
3347 0 : tenants.insert(child, child_state);
3348 0 : response.new_shards.push(child);
3349 : }
3350 : }
3351 0 : (response, child_locations, waiters)
3352 0 : }
3353 0 : }
3354 :
3355 0 : async fn tenant_shard_split_start_secondaries(
3356 0 : &self,
3357 0 : tenant_id: TenantId,
3358 0 : waiters: Vec<ReconcilerWaiter>,
3359 0 : ) {
3360 : // Wait for initial reconcile of child shards, this creates the secondary locations
3361 0 : if let Err(e) = self.await_waiters(waiters, RECONCILE_TIMEOUT).await {
3362 : // This is not a failure to split: it's some issue reconciling the new child shards, perhaps
3363 : // their secondaries couldn't be attached.
3364 0 : tracing::warn!("Failed to reconcile after split: {e}");
3365 0 : return;
3366 0 : }
3367 :
3368 : // Take the state lock to discover the attached & secondary intents for all shards
3369 0 : let (attached, secondary) = {
3370 0 : let locked = self.inner.read().unwrap();
3371 0 : let mut attached = Vec::new();
3372 0 : let mut secondary = Vec::new();
3373 :
3374 0 : for (tenant_shard_id, shard) in
3375 0 : locked.tenants.range(TenantShardId::tenant_range(tenant_id))
3376 : {
3377 0 : let Some(node_id) = shard.intent.get_attached() else {
3378 : // Unexpected. Race with a PlacementPolicy change?
3379 0 : tracing::warn!(
3380 0 : "No attached node on {tenant_shard_id} immediately after shard split!"
3381 : );
3382 0 : continue;
3383 : };
3384 :
3385 0 : let Some(secondary_node_id) = shard.intent.get_secondary().first() else {
3386 : // No secondary location. Nothing for us to do.
3387 0 : continue;
3388 : };
3389 :
3390 0 : let attached_node = locked
3391 0 : .nodes
3392 0 : .get(node_id)
3393 0 : .expect("Pageservers may not be deleted while referenced");
3394 0 :
3395 0 : let secondary_node = locked
3396 0 : .nodes
3397 0 : .get(secondary_node_id)
3398 0 : .expect("Pageservers may not be deleted while referenced");
3399 0 :
3400 0 : attached.push((*tenant_shard_id, attached_node.clone()));
3401 0 : secondary.push((*tenant_shard_id, secondary_node.clone()));
3402 : }
3403 0 : (attached, secondary)
3404 0 : };
3405 0 :
3406 0 : if secondary.is_empty() {
3407 : // No secondary locations; nothing for us to do
3408 0 : return;
3409 0 : }
3410 :
3411 0 : for result in self
3412 0 : .tenant_for_shards_api(
3413 0 : attached,
3414 0 : |tenant_shard_id, client| async move {
3415 0 : client.tenant_heatmap_upload(tenant_shard_id).await
3416 0 : },
3417 0 : 1,
3418 0 : 1,
3419 0 : SHORT_RECONCILE_TIMEOUT,
3420 0 : &self.cancel,
3421 0 : )
3422 0 : .await
3423 : {
3424 0 : if let Err(e) = result {
3425 0 : tracing::warn!("Error calling heatmap upload after shard split: {e}");
3426 0 : return;
3427 0 : }
3428 : }
3429 :
3430 0 : for result in self
3431 0 : .tenant_for_shards_api(
3432 0 : secondary,
3433 0 : |tenant_shard_id, client| async move {
3434 0 : client
3435 0 : .tenant_secondary_download(tenant_shard_id, Some(Duration::ZERO))
3436 0 : .await
3437 0 : },
3438 0 : 1,
3439 0 : 1,
3440 0 : SHORT_RECONCILE_TIMEOUT,
3441 0 : &self.cancel,
3442 0 : )
3443 0 : .await
3444 : {
3445 0 : if let Err(e) = result {
3446 0 : tracing::warn!("Error calling secondary download after shard split: {e}");
3447 0 : return;
3448 0 : }
3449 : }
3450 0 : }
3451 :
3452 0 : pub(crate) async fn tenant_shard_split(
3453 0 : &self,
3454 0 : tenant_id: TenantId,
3455 0 : split_req: TenantShardSplitRequest,
3456 0 : ) -> Result<TenantShardSplitResponse, ApiError> {
3457 : // TODO: return 503 if we get stuck waiting for this lock
3458 : // (issue https://github.com/neondatabase/neon/issues/7108)
3459 0 : let _tenant_lock = trace_exclusive_lock(
3460 0 : &self.tenant_op_locks,
3461 0 : tenant_id,
3462 0 : TenantOperations::ShardSplit,
3463 0 : )
3464 0 : .await;
3465 :
3466 0 : let new_shard_count = ShardCount::new(split_req.new_shard_count);
3467 0 : let new_stripe_size = split_req.new_stripe_size;
3468 :
3469 : // Validate the request and construct parameters. This phase is fallible, but does not require
3470 : // rollback on errors, as it does no I/O and mutates no state.
3471 0 : let shard_split_params = match self.prepare_tenant_shard_split(tenant_id, split_req)? {
3472 0 : ShardSplitAction::NoOp(resp) => return Ok(resp),
3473 0 : ShardSplitAction::Split(params) => params,
3474 : };
3475 :
3476 : // Execute this split: this phase mutates state and does remote I/O on pageservers. If it fails,
3477 : // we must roll back.
3478 0 : let r = self
3479 0 : .do_tenant_shard_split(tenant_id, shard_split_params)
3480 0 : .await;
3481 :
3482 0 : let (response, waiters) = match r {
3483 0 : Ok(r) => r,
3484 0 : Err(e) => {
3485 0 : // Split might be part-done, we must do work to abort it.
3486 0 : tracing::warn!("Enqueuing background abort of split on {tenant_id}");
3487 0 : self.abort_tx
3488 0 : .send(TenantShardSplitAbort {
3489 0 : tenant_id,
3490 0 : new_shard_count,
3491 0 : new_stripe_size,
3492 0 : _tenant_lock,
3493 0 : })
3494 0 : // Ignore error sending: that just means we're shutting down: aborts are ephemeral so it's fine to drop it.
3495 0 : .ok();
3496 0 : return Err(e);
3497 : }
3498 : };
3499 :
3500 : // The split is now complete. As an optimization, we will trigger all the child shards to upload
3501 : // a heatmap immediately, and all their secondary locations to start downloading: this avoids waiting
3502 : // for the background heatmap/download interval before secondaries get warm enough to migrate shards
3503 : // in [`Self::optimize_all`]
3504 0 : self.tenant_shard_split_start_secondaries(tenant_id, waiters)
3505 0 : .await;
3506 0 : Ok(response)
3507 0 : }
3508 :
3509 0 : fn prepare_tenant_shard_split(
3510 0 : &self,
3511 0 : tenant_id: TenantId,
3512 0 : split_req: TenantShardSplitRequest,
3513 0 : ) -> Result<ShardSplitAction, ApiError> {
3514 0 : fail::fail_point!("shard-split-validation", |_| Err(ApiError::BadRequest(
3515 0 : anyhow::anyhow!("failpoint")
3516 0 : )));
3517 :
3518 0 : let mut policy = None;
3519 0 : let mut config = None;
3520 0 : let mut shard_ident = None;
3521 : // Validate input, and calculate which shards we will create
3522 0 : let (old_shard_count, targets) =
3523 : {
3524 0 : let locked = self.inner.read().unwrap();
3525 0 :
3526 0 : let pageservers = locked.nodes.clone();
3527 0 :
3528 0 : let mut targets = Vec::new();
3529 0 :
3530 0 : // In case this is a retry, count how many already-split shards we found
3531 0 : let mut children_found = Vec::new();
3532 0 : let mut old_shard_count = None;
3533 :
3534 0 : for (tenant_shard_id, shard) in
3535 0 : locked.tenants.range(TenantShardId::tenant_range(tenant_id))
3536 : {
3537 0 : match shard.shard.count.count().cmp(&split_req.new_shard_count) {
3538 : Ordering::Equal => {
3539 : // Already split this
3540 0 : children_found.push(*tenant_shard_id);
3541 0 : continue;
3542 : }
3543 : Ordering::Greater => {
3544 0 : return Err(ApiError::BadRequest(anyhow::anyhow!(
3545 0 : "Requested count {} but already have shards at count {}",
3546 0 : split_req.new_shard_count,
3547 0 : shard.shard.count.count()
3548 0 : )));
3549 : }
3550 0 : Ordering::Less => {
3551 0 : // Fall through: this shard has lower count than requested,
3552 0 : // is a candidate for splitting.
3553 0 : }
3554 0 : }
3555 0 :
3556 0 : match old_shard_count {
3557 0 : None => old_shard_count = Some(shard.shard.count),
3558 0 : Some(old_shard_count) => {
3559 0 : if old_shard_count != shard.shard.count {
3560 : // We may hit this case if a caller asked for two splits to
3561 : // different sizes, before the first one is complete.
3562 : // e.g. 1->2, 2->4, where the 4 call comes while we have a mixture
3563 : // of shard_count=1 and shard_count=2 shards in the map.
3564 0 : return Err(ApiError::Conflict(
3565 0 : "Cannot split, currently mid-split".to_string(),
3566 0 : ));
3567 0 : }
3568 : }
3569 : }
3570 0 : if policy.is_none() {
3571 0 : policy = Some(shard.policy.clone());
3572 0 : }
3573 0 : if shard_ident.is_none() {
3574 0 : shard_ident = Some(shard.shard);
3575 0 : }
3576 0 : if config.is_none() {
3577 0 : config = Some(shard.config.clone());
3578 0 : }
3579 :
3580 0 : if tenant_shard_id.shard_count.count() == split_req.new_shard_count {
3581 0 : tracing::info!(
3582 0 : "Tenant shard {} already has shard count {}",
3583 : tenant_shard_id,
3584 : split_req.new_shard_count
3585 : );
3586 0 : continue;
3587 0 : }
3588 :
3589 0 : let node_id = shard.intent.get_attached().ok_or(ApiError::BadRequest(
3590 0 : anyhow::anyhow!("Cannot split a tenant that is not attached"),
3591 0 : ))?;
3592 :
3593 0 : let node = pageservers
3594 0 : .get(&node_id)
3595 0 : .expect("Pageservers may not be deleted while referenced");
3596 0 :
3597 0 : targets.push(ShardSplitTarget {
3598 0 : parent_id: *tenant_shard_id,
3599 0 : node: node.clone(),
3600 0 : child_ids: tenant_shard_id
3601 0 : .split(ShardCount::new(split_req.new_shard_count)),
3602 0 : });
3603 : }
3604 :
3605 0 : if targets.is_empty() {
3606 0 : if children_found.len() == split_req.new_shard_count as usize {
3607 0 : return Ok(ShardSplitAction::NoOp(TenantShardSplitResponse {
3608 0 : new_shards: children_found,
3609 0 : }));
3610 : } else {
3611 : // No shards found to split, and no existing children found: the
3612 : // tenant doesn't exist at all.
3613 0 : return Err(ApiError::NotFound(
3614 0 : anyhow::anyhow!("Tenant {} not found", tenant_id).into(),
3615 0 : ));
3616 : }
3617 0 : }
3618 0 :
3619 0 : (old_shard_count, targets)
3620 0 : };
3621 0 :
3622 0 : // unwrap safety: we would have returned above if we didn't find at least one shard to split
3623 0 : let old_shard_count = old_shard_count.unwrap();
3624 0 : let shard_ident = if let Some(new_stripe_size) = split_req.new_stripe_size {
3625 : // This ShardIdentity will be used as the template for all children, so this implicitly
3626 : // applies the new stripe size to the children.
3627 0 : let mut shard_ident = shard_ident.unwrap();
3628 0 : if shard_ident.count.count() > 1 && shard_ident.stripe_size != new_stripe_size {
3629 0 : return Err(ApiError::BadRequest(anyhow::anyhow!("Attempted to change stripe size ({:?}->{new_stripe_size:?}) on a tenant with multiple shards", shard_ident.stripe_size)));
3630 0 : }
3631 0 :
3632 0 : shard_ident.stripe_size = new_stripe_size;
3633 0 : tracing::info!("applied stripe size {}", shard_ident.stripe_size.0);
3634 0 : shard_ident
3635 : } else {
3636 0 : shard_ident.unwrap()
3637 : };
3638 0 : let policy = policy.unwrap();
3639 0 : let config = config.unwrap();
3640 0 :
3641 0 : Ok(ShardSplitAction::Split(ShardSplitParams {
3642 0 : old_shard_count,
3643 0 : new_shard_count: ShardCount::new(split_req.new_shard_count),
3644 0 : new_stripe_size: split_req.new_stripe_size,
3645 0 : targets,
3646 0 : policy,
3647 0 : config,
3648 0 : shard_ident,
3649 0 : }))
3650 0 : }
3651 :
3652 0 : async fn do_tenant_shard_split(
3653 0 : &self,
3654 0 : tenant_id: TenantId,
3655 0 : params: ShardSplitParams,
3656 0 : ) -> Result<(TenantShardSplitResponse, Vec<ReconcilerWaiter>), ApiError> {
3657 0 : // FIXME: we have dropped self.inner lock, and not yet written anything to the database: another
3658 0 : // request could occur here, deleting or mutating the tenant. begin_shard_split checks that the
3659 0 : // parent shards exist as expected, but it would be neater to do the above pre-checks within the
3660 0 : // same database transaction rather than pre-check in-memory and then maybe-fail the database write.
3661 0 : // (https://github.com/neondatabase/neon/issues/6676)
3662 0 :
3663 0 : let ShardSplitParams {
3664 0 : old_shard_count,
3665 0 : new_shard_count,
3666 0 : new_stripe_size,
3667 0 : mut targets,
3668 0 : policy,
3669 0 : config,
3670 0 : shard_ident,
3671 0 : } = params;
3672 :
3673 : // Drop any secondary locations: pageservers do not support splitting these, and in any case the
3674 : // end-state for a split tenant will usually be to have secondary locations on different nodes.
3675 : // The reconciliation calls in this block also implicitly cancel+barrier wrt any ongoing reconciliation
3676 : // at the time of split.
3677 0 : let waiters = {
3678 0 : let mut locked = self.inner.write().unwrap();
3679 0 : let mut waiters = Vec::new();
3680 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
3681 0 : for target in &mut targets {
3682 0 : let Some(shard) = tenants.get_mut(&target.parent_id) else {
3683 : // Paranoia check: this shouldn't happen: we have the oplock for this tenant ID.
3684 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
3685 0 : "Shard {} not found",
3686 0 : target.parent_id
3687 0 : )));
3688 : };
3689 :
3690 0 : if shard.intent.get_attached() != &Some(target.node.get_id()) {
3691 : // Paranoia check: this shouldn't happen: we have the oplock for this tenant ID.
3692 0 : return Err(ApiError::Conflict(format!(
3693 0 : "Shard {} unexpectedly rescheduled during split",
3694 0 : target.parent_id
3695 0 : )));
3696 0 : }
3697 0 :
3698 0 : // Irrespective of PlacementPolicy, clear secondary locations from intent
3699 0 : shard.intent.clear_secondary(scheduler);
3700 :
3701 : // Run Reconciler to execute detach fo secondary locations.
3702 0 : if let Some(waiter) = self.maybe_reconcile_shard(shard, nodes) {
3703 0 : waiters.push(waiter);
3704 0 : }
3705 : }
3706 0 : waiters
3707 0 : };
3708 0 : self.await_waiters(waiters, RECONCILE_TIMEOUT).await?;
3709 :
3710 : // Before creating any new child shards in memory or on the pageservers, persist them: this
3711 : // enables us to ensure that we will always be able to clean up if something goes wrong. This also
3712 : // acts as the protection against two concurrent attempts to split: one of them will get a database
3713 : // error trying to insert the child shards.
3714 0 : let mut child_tsps = Vec::new();
3715 0 : for target in &targets {
3716 0 : let mut this_child_tsps = Vec::new();
3717 0 : for child in &target.child_ids {
3718 0 : let mut child_shard = shard_ident;
3719 0 : child_shard.number = child.shard_number;
3720 0 : child_shard.count = child.shard_count;
3721 0 :
3722 0 : tracing::info!(
3723 0 : "Create child shard persistence with stripe size {}",
3724 : shard_ident.stripe_size.0
3725 : );
3726 :
3727 0 : this_child_tsps.push(TenantShardPersistence {
3728 0 : tenant_id: child.tenant_id.to_string(),
3729 0 : shard_number: child.shard_number.0 as i32,
3730 0 : shard_count: child.shard_count.literal() as i32,
3731 0 : shard_stripe_size: shard_ident.stripe_size.0 as i32,
3732 0 : // Note: this generation is a placeholder, [`Persistence::begin_shard_split`] will
3733 0 : // populate the correct generation as part of its transaction, to protect us
3734 0 : // against racing with changes in the state of the parent.
3735 0 : generation: None,
3736 0 : generation_pageserver: Some(target.node.get_id().0 as i64),
3737 0 : placement_policy: serde_json::to_string(&policy).unwrap(),
3738 0 : config: serde_json::to_string(&config).unwrap(),
3739 0 : splitting: SplitState::Splitting,
3740 0 :
3741 0 : // Scheduling policies do not carry through to children
3742 0 : scheduling_policy: serde_json::to_string(&ShardSchedulingPolicy::default())
3743 0 : .unwrap(),
3744 0 : });
3745 : }
3746 :
3747 0 : child_tsps.push((target.parent_id, this_child_tsps));
3748 : }
3749 :
3750 0 : if let Err(e) = self
3751 0 : .persistence
3752 0 : .begin_shard_split(old_shard_count, tenant_id, child_tsps)
3753 0 : .await
3754 : {
3755 0 : match e {
3756 : DatabaseError::Query(diesel::result::Error::DatabaseError(
3757 : DatabaseErrorKind::UniqueViolation,
3758 : _,
3759 : )) => {
3760 : // Inserting a child shard violated a unique constraint: we raced with another call to
3761 : // this function
3762 0 : tracing::warn!("Conflicting attempt to split {tenant_id}: {e}");
3763 0 : return Err(ApiError::Conflict("Tenant is already splitting".into()));
3764 : }
3765 0 : _ => return Err(ApiError::InternalServerError(e.into())),
3766 : }
3767 0 : }
3768 0 : fail::fail_point!("shard-split-post-begin", |_| Err(
3769 0 : ApiError::InternalServerError(anyhow::anyhow!("failpoint"))
3770 0 : ));
3771 :
3772 : // Now that I have persisted the splitting state, apply it in-memory. This is infallible, so
3773 : // callers may assume that if splitting is set in memory, then it was persisted, and if splitting
3774 : // is not set in memory, then it was not persisted.
3775 : {
3776 0 : let mut locked = self.inner.write().unwrap();
3777 0 : for target in &targets {
3778 0 : if let Some(parent_shard) = locked.tenants.get_mut(&target.parent_id) {
3779 0 : parent_shard.splitting = SplitState::Splitting;
3780 0 : // Put the observed state to None, to reflect that it is indeterminate once we start the
3781 0 : // split operation.
3782 0 : parent_shard
3783 0 : .observed
3784 0 : .locations
3785 0 : .insert(target.node.get_id(), ObservedStateLocation { conf: None });
3786 0 : }
3787 : }
3788 : }
3789 :
3790 : // TODO: issue split calls concurrently (this only matters once we're splitting
3791 : // N>1 shards into M shards -- initially we're usually splitting 1 shard into N).
3792 :
3793 0 : for target in &targets {
3794 : let ShardSplitTarget {
3795 0 : parent_id,
3796 0 : node,
3797 0 : child_ids,
3798 0 : } = target;
3799 0 : let client = PageserverClient::new(
3800 0 : node.get_id(),
3801 0 : node.base_url(),
3802 0 : self.config.jwt_token.as_deref(),
3803 0 : );
3804 0 : let response = client
3805 0 : .tenant_shard_split(
3806 0 : *parent_id,
3807 0 : TenantShardSplitRequest {
3808 0 : new_shard_count: new_shard_count.literal(),
3809 0 : new_stripe_size,
3810 0 : },
3811 0 : )
3812 0 : .await
3813 0 : .map_err(|e| ApiError::Conflict(format!("Failed to split {}: {}", parent_id, e)))?;
3814 :
3815 0 : fail::fail_point!("shard-split-post-remote", |_| Err(ApiError::Conflict(
3816 0 : "failpoint".to_string()
3817 0 : )));
3818 :
3819 0 : tracing::info!(
3820 0 : "Split {} into {}",
3821 0 : parent_id,
3822 0 : response
3823 0 : .new_shards
3824 0 : .iter()
3825 0 : .map(|s| format!("{:?}", s))
3826 0 : .collect::<Vec<_>>()
3827 0 : .join(",")
3828 : );
3829 :
3830 0 : if &response.new_shards != child_ids {
3831 : // This should never happen: the pageserver should agree with us on how shard splits work.
3832 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
3833 0 : "Splitting shard {} resulted in unexpected IDs: {:?} (expected {:?})",
3834 0 : parent_id,
3835 0 : response.new_shards,
3836 0 : child_ids
3837 0 : )));
3838 0 : }
3839 : }
3840 :
3841 : // TODO: if the pageserver restarted concurrently with our split API call,
3842 : // the actual generation of the child shard might differ from the generation
3843 : // we expect it to have. In order for our in-database generation to end up
3844 : // correct, we should carry the child generation back in the response and apply it here
3845 : // in complete_shard_split (and apply the correct generation in memory)
3846 : // (or, we can carry generation in the request and reject the request if
3847 : // it doesn't match, but that requires more retry logic on this side)
3848 :
3849 0 : self.persistence
3850 0 : .complete_shard_split(tenant_id, old_shard_count)
3851 0 : .await?;
3852 :
3853 0 : fail::fail_point!("shard-split-post-complete", |_| Err(
3854 0 : ApiError::InternalServerError(anyhow::anyhow!("failpoint"))
3855 0 : ));
3856 :
3857 : // Replace all the shards we just split with their children: this phase is infallible.
3858 0 : let (response, child_locations, waiters) =
3859 0 : self.tenant_shard_split_commit_inmem(tenant_id, new_shard_count, new_stripe_size);
3860 0 :
3861 0 : // Send compute notifications for all the new shards
3862 0 : let mut failed_notifications = Vec::new();
3863 0 : for (child_id, child_ps, stripe_size) in child_locations {
3864 0 : if let Err(e) = self
3865 0 : .compute_hook
3866 0 : .notify(child_id, child_ps, stripe_size, &self.cancel)
3867 0 : .await
3868 : {
3869 0 : tracing::warn!("Failed to update compute of {}->{} during split, proceeding anyway to complete split ({e})",
3870 : child_id, child_ps);
3871 0 : failed_notifications.push(child_id);
3872 0 : }
3873 : }
3874 :
3875 : // If we failed any compute notifications, make a note to retry later.
3876 0 : if !failed_notifications.is_empty() {
3877 0 : let mut locked = self.inner.write().unwrap();
3878 0 : for failed in failed_notifications {
3879 0 : if let Some(shard) = locked.tenants.get_mut(&failed) {
3880 0 : shard.pending_compute_notification = true;
3881 0 : }
3882 : }
3883 0 : }
3884 :
3885 0 : Ok((response, waiters))
3886 0 : }
3887 :
3888 0 : pub(crate) async fn tenant_shard_migrate(
3889 0 : &self,
3890 0 : tenant_shard_id: TenantShardId,
3891 0 : migrate_req: TenantShardMigrateRequest,
3892 0 : ) -> Result<TenantShardMigrateResponse, ApiError> {
3893 0 : let waiter = {
3894 0 : let mut locked = self.inner.write().unwrap();
3895 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
3896 :
3897 0 : let Some(node) = nodes.get(&migrate_req.node_id) else {
3898 0 : return Err(ApiError::BadRequest(anyhow::anyhow!(
3899 0 : "Node {} not found",
3900 0 : migrate_req.node_id
3901 0 : )));
3902 : };
3903 :
3904 0 : if !node.is_available() {
3905 : // Warn but proceed: the caller may intend to manually adjust the placement of
3906 : // a shard even if the node is down, e.g. if intervening during an incident.
3907 0 : tracing::warn!("Migrating to unavailable node {node}");
3908 0 : }
3909 :
3910 0 : let Some(shard) = tenants.get_mut(&tenant_shard_id) else {
3911 0 : return Err(ApiError::NotFound(
3912 0 : anyhow::anyhow!("Tenant shard not found").into(),
3913 0 : ));
3914 : };
3915 :
3916 0 : if shard.intent.get_attached() == &Some(migrate_req.node_id) {
3917 : // No-op case: we will still proceed to wait for reconciliation in case it is
3918 : // incomplete from an earlier update to the intent.
3919 0 : tracing::info!("Migrating: intent is unchanged {:?}", shard.intent);
3920 : } else {
3921 0 : let old_attached = *shard.intent.get_attached();
3922 0 :
3923 0 : match shard.policy {
3924 0 : PlacementPolicy::Attached(n) => {
3925 0 : // If our new attached node was a secondary, it no longer should be.
3926 0 : shard.intent.remove_secondary(scheduler, migrate_req.node_id);
3927 :
3928 : // If we were already attached to something, demote that to a secondary
3929 0 : if let Some(old_attached) = old_attached {
3930 0 : if n > 0 {
3931 : // Remove other secondaries to make room for the location we'll demote
3932 0 : while shard.intent.get_secondary().len() >= n {
3933 0 : shard.intent.pop_secondary(scheduler);
3934 0 : }
3935 :
3936 0 : shard.intent.push_secondary(scheduler, old_attached);
3937 0 : }
3938 0 : }
3939 :
3940 0 : shard.intent.set_attached(scheduler, Some(migrate_req.node_id));
3941 : }
3942 0 : PlacementPolicy::Secondary => {
3943 0 : shard.intent.clear(scheduler);
3944 0 : shard.intent.push_secondary(scheduler, migrate_req.node_id);
3945 0 : }
3946 : PlacementPolicy::Detached => {
3947 0 : return Err(ApiError::BadRequest(anyhow::anyhow!(
3948 0 : "Cannot migrate a tenant that is PlacementPolicy::Detached: configure it to an attached policy first"
3949 0 : )))
3950 : }
3951 : }
3952 :
3953 0 : tracing::info!("Migrating: new intent {:?}", shard.intent);
3954 0 : shard.sequence = shard.sequence.next();
3955 : }
3956 :
3957 0 : self.maybe_reconcile_shard(shard, nodes)
3958 : };
3959 :
3960 0 : if let Some(waiter) = waiter {
3961 0 : waiter.wait_timeout(RECONCILE_TIMEOUT).await?;
3962 : } else {
3963 0 : tracing::info!("Migration is a no-op");
3964 : }
3965 :
3966 0 : Ok(TenantShardMigrateResponse {})
3967 0 : }
3968 :
3969 : /// This is for debug/support only: we simply drop all state for a tenant, without
3970 : /// detaching or deleting it on pageservers.
3971 0 : pub(crate) async fn tenant_drop(&self, tenant_id: TenantId) -> Result<(), ApiError> {
3972 0 : self.persistence.delete_tenant(tenant_id).await?;
3973 :
3974 0 : let mut locked = self.inner.write().unwrap();
3975 0 : let (_nodes, tenants, scheduler) = locked.parts_mut();
3976 0 : let mut shards = Vec::new();
3977 0 : for (tenant_shard_id, _) in tenants.range(TenantShardId::tenant_range(tenant_id)) {
3978 0 : shards.push(*tenant_shard_id);
3979 0 : }
3980 :
3981 0 : for shard_id in shards {
3982 0 : if let Some(mut shard) = tenants.remove(&shard_id) {
3983 0 : shard.intent.clear(scheduler);
3984 0 : }
3985 : }
3986 :
3987 0 : Ok(())
3988 0 : }
3989 :
3990 : /// This is for debug/support only: assuming tenant data is already present in S3, we "create" a
3991 : /// tenant with a very high generation number so that it will see the existing data.
3992 0 : pub(crate) async fn tenant_import(
3993 0 : &self,
3994 0 : tenant_id: TenantId,
3995 0 : ) -> Result<TenantCreateResponse, ApiError> {
3996 0 : // Pick an arbitrary available pageserver to use for scanning the tenant in remote storage
3997 0 : let maybe_node = {
3998 0 : self.inner
3999 0 : .read()
4000 0 : .unwrap()
4001 0 : .nodes
4002 0 : .values()
4003 0 : .find(|n| n.is_available())
4004 0 : .cloned()
4005 : };
4006 0 : let Some(node) = maybe_node else {
4007 0 : return Err(ApiError::BadRequest(anyhow::anyhow!("No nodes available")));
4008 : };
4009 :
4010 0 : let client = PageserverClient::new(
4011 0 : node.get_id(),
4012 0 : node.base_url(),
4013 0 : self.config.jwt_token.as_deref(),
4014 0 : );
4015 :
4016 0 : let scan_result = client
4017 0 : .tenant_scan_remote_storage(tenant_id)
4018 0 : .await
4019 0 : .map_err(|e| passthrough_api_error(&node, e))?;
4020 :
4021 : // A post-split tenant may contain a mixture of shard counts in remote storage: pick the highest count.
4022 0 : let Some(shard_count) = scan_result
4023 0 : .shards
4024 0 : .iter()
4025 0 : .map(|s| s.tenant_shard_id.shard_count)
4026 0 : .max()
4027 : else {
4028 0 : return Err(ApiError::NotFound(
4029 0 : anyhow::anyhow!("No shards found").into(),
4030 0 : ));
4031 : };
4032 :
4033 : // Ideally we would set each newly imported shard's generation independently, but for correctness it is sufficient
4034 : // to
4035 0 : let generation = scan_result
4036 0 : .shards
4037 0 : .iter()
4038 0 : .map(|s| s.generation)
4039 0 : .max()
4040 0 : .expect("We already validated >0 shards");
4041 0 :
4042 0 : // FIXME: we have no way to recover the shard stripe size from contents of remote storage: this will
4043 0 : // only work if they were using the default stripe size.
4044 0 : let stripe_size = ShardParameters::DEFAULT_STRIPE_SIZE;
4045 :
4046 0 : let (response, waiters) = self
4047 0 : .do_tenant_create(TenantCreateRequest {
4048 0 : new_tenant_id: TenantShardId::unsharded(tenant_id),
4049 0 : generation,
4050 0 :
4051 0 : shard_parameters: ShardParameters {
4052 0 : count: shard_count,
4053 0 : stripe_size,
4054 0 : },
4055 0 : placement_policy: Some(PlacementPolicy::Attached(0)), // No secondaries, for convenient debug/hacking
4056 0 :
4057 0 : // There is no way to know what the tenant's config was: revert to defaults
4058 0 : config: TenantConfig::default(),
4059 0 : })
4060 0 : .await?;
4061 :
4062 0 : if let Err(e) = self.await_waiters(waiters, SHORT_RECONCILE_TIMEOUT).await {
4063 : // Since this is a debug/support operation, all kinds of weird issues are possible (e.g. this
4064 : // tenant doesn't exist in the control plane), so don't fail the request if it can't fully
4065 : // reconcile, as reconciliation includes notifying compute.
4066 0 : tracing::warn!(%tenant_id, "Reconcile not done yet while importing tenant ({e})");
4067 0 : }
4068 :
4069 0 : Ok(response)
4070 0 : }
4071 :
4072 : /// For debug/support: a full JSON dump of TenantShards. Returns a response so that
4073 : /// we don't have to make TenantShard clonable in the return path.
4074 0 : pub(crate) fn tenants_dump(&self) -> Result<hyper::Response<hyper::Body>, ApiError> {
4075 0 : let serialized = {
4076 0 : let locked = self.inner.read().unwrap();
4077 0 : let result = locked.tenants.values().collect::<Vec<_>>();
4078 0 : serde_json::to_string(&result).map_err(|e| ApiError::InternalServerError(e.into()))?
4079 : };
4080 :
4081 0 : hyper::Response::builder()
4082 0 : .status(hyper::StatusCode::OK)
4083 0 : .header(hyper::header::CONTENT_TYPE, "application/json")
4084 0 : .body(hyper::Body::from(serialized))
4085 0 : .map_err(|e| ApiError::InternalServerError(e.into()))
4086 0 : }
4087 :
4088 : /// Check the consistency of in-memory state vs. persistent state, and check that the
4089 : /// scheduler's statistics are up to date.
4090 : ///
4091 : /// These consistency checks expect an **idle** system. If changes are going on while
4092 : /// we run, then we can falsely indicate a consistency issue. This is sufficient for end-of-test
4093 : /// checks, but not suitable for running continuously in the background in the field.
4094 0 : pub(crate) async fn consistency_check(&self) -> Result<(), ApiError> {
4095 0 : let (mut expect_nodes, mut expect_shards) = {
4096 0 : let locked = self.inner.read().unwrap();
4097 0 :
4098 0 : locked
4099 0 : .scheduler
4100 0 : .consistency_check(locked.nodes.values(), locked.tenants.values())
4101 0 : .context("Scheduler checks")
4102 0 : .map_err(ApiError::InternalServerError)?;
4103 :
4104 0 : let expect_nodes = locked
4105 0 : .nodes
4106 0 : .values()
4107 0 : .map(|n| n.to_persistent())
4108 0 : .collect::<Vec<_>>();
4109 0 :
4110 0 : let expect_shards = locked
4111 0 : .tenants
4112 0 : .values()
4113 0 : .map(|t| t.to_persistent())
4114 0 : .collect::<Vec<_>>();
4115 :
4116 : // This method can only validate the state of an idle system: if a reconcile is in
4117 : // progress, fail out early to avoid giving false errors on state that won't match
4118 : // between database and memory under a ReconcileResult is processed.
4119 0 : for t in locked.tenants.values() {
4120 0 : if t.reconciler.is_some() {
4121 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
4122 0 : "Shard {} reconciliation in progress",
4123 0 : t.tenant_shard_id
4124 0 : )));
4125 0 : }
4126 : }
4127 :
4128 0 : (expect_nodes, expect_shards)
4129 : };
4130 :
4131 0 : let mut nodes = self.persistence.list_nodes().await?;
4132 0 : expect_nodes.sort_by_key(|n| n.node_id);
4133 0 : nodes.sort_by_key(|n| n.node_id);
4134 0 :
4135 0 : if nodes != expect_nodes {
4136 0 : tracing::error!("Consistency check failed on nodes.");
4137 0 : tracing::error!(
4138 0 : "Nodes in memory: {}",
4139 0 : serde_json::to_string(&expect_nodes)
4140 0 : .map_err(|e| ApiError::InternalServerError(e.into()))?
4141 : );
4142 0 : tracing::error!(
4143 0 : "Nodes in database: {}",
4144 0 : serde_json::to_string(&nodes)
4145 0 : .map_err(|e| ApiError::InternalServerError(e.into()))?
4146 : );
4147 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
4148 0 : "Node consistency failure"
4149 0 : )));
4150 0 : }
4151 :
4152 0 : let mut shards = self.persistence.list_tenant_shards().await?;
4153 0 : shards.sort_by_key(|tsp| (tsp.tenant_id.clone(), tsp.shard_number, tsp.shard_count));
4154 0 : expect_shards.sort_by_key(|tsp| (tsp.tenant_id.clone(), tsp.shard_number, tsp.shard_count));
4155 0 :
4156 0 : if shards != expect_shards {
4157 0 : tracing::error!("Consistency check failed on shards.");
4158 0 : tracing::error!(
4159 0 : "Shards in memory: {}",
4160 0 : serde_json::to_string(&expect_shards)
4161 0 : .map_err(|e| ApiError::InternalServerError(e.into()))?
4162 : );
4163 0 : tracing::error!(
4164 0 : "Shards in database: {}",
4165 0 : serde_json::to_string(&shards)
4166 0 : .map_err(|e| ApiError::InternalServerError(e.into()))?
4167 : );
4168 0 : return Err(ApiError::InternalServerError(anyhow::anyhow!(
4169 0 : "Shard consistency failure"
4170 0 : )));
4171 0 : }
4172 0 :
4173 0 : Ok(())
4174 0 : }
4175 :
4176 : /// For debug/support: a JSON dump of the [`Scheduler`]. Returns a response so that
4177 : /// we don't have to make TenantShard clonable in the return path.
4178 0 : pub(crate) fn scheduler_dump(&self) -> Result<hyper::Response<hyper::Body>, ApiError> {
4179 0 : let serialized = {
4180 0 : let locked = self.inner.read().unwrap();
4181 0 : serde_json::to_string(&locked.scheduler)
4182 0 : .map_err(|e| ApiError::InternalServerError(e.into()))?
4183 : };
4184 :
4185 0 : hyper::Response::builder()
4186 0 : .status(hyper::StatusCode::OK)
4187 0 : .header(hyper::header::CONTENT_TYPE, "application/json")
4188 0 : .body(hyper::Body::from(serialized))
4189 0 : .map_err(|e| ApiError::InternalServerError(e.into()))
4190 0 : }
4191 :
4192 : /// This is for debug/support only: we simply drop all state for a tenant, without
4193 : /// detaching or deleting it on pageservers. We do not try and re-schedule any
4194 : /// tenants that were on this node.
4195 : ///
4196 : /// TODO: proper node deletion API that unhooks things more gracefully
4197 0 : pub(crate) async fn node_drop(&self, node_id: NodeId) -> Result<(), ApiError> {
4198 0 : self.persistence.delete_node(node_id).await?;
4199 :
4200 0 : let mut locked = self.inner.write().unwrap();
4201 :
4202 0 : for shard in locked.tenants.values_mut() {
4203 0 : shard.deref_node(node_id);
4204 0 : }
4205 :
4206 0 : let mut nodes = (*locked.nodes).clone();
4207 0 : nodes.remove(&node_id);
4208 0 : locked.nodes = Arc::new(nodes);
4209 0 :
4210 0 : locked.scheduler.node_remove(node_id);
4211 0 :
4212 0 : Ok(())
4213 0 : }
4214 :
4215 0 : pub(crate) async fn node_list(&self) -> Result<Vec<Node>, ApiError> {
4216 0 : let nodes = {
4217 0 : self.inner
4218 0 : .read()
4219 0 : .unwrap()
4220 0 : .nodes
4221 0 : .values()
4222 0 : .cloned()
4223 0 : .collect::<Vec<_>>()
4224 0 : };
4225 0 :
4226 0 : Ok(nodes)
4227 0 : }
4228 :
4229 0 : pub(crate) async fn get_node(&self, node_id: NodeId) -> Result<Node, ApiError> {
4230 0 : self.inner
4231 0 : .read()
4232 0 : .unwrap()
4233 0 : .nodes
4234 0 : .get(&node_id)
4235 0 : .cloned()
4236 0 : .ok_or(ApiError::NotFound(
4237 0 : format!("Node {node_id} not registered").into(),
4238 0 : ))
4239 0 : }
4240 :
4241 0 : pub(crate) async fn node_register(
4242 0 : &self,
4243 0 : register_req: NodeRegisterRequest,
4244 0 : ) -> Result<(), ApiError> {
4245 0 : let _node_lock = trace_exclusive_lock(
4246 0 : &self.node_op_locks,
4247 0 : register_req.node_id,
4248 0 : NodeOperations::Register,
4249 0 : )
4250 0 : .await;
4251 :
4252 : {
4253 0 : let locked = self.inner.read().unwrap();
4254 0 : if let Some(node) = locked.nodes.get(®ister_req.node_id) {
4255 : // Note that we do not do a total equality of the struct, because we don't require
4256 : // the availability/scheduling states to agree for a POST to be idempotent.
4257 0 : if node.registration_match(®ister_req) {
4258 0 : tracing::info!(
4259 0 : "Node {} re-registered with matching address",
4260 : register_req.node_id
4261 : );
4262 0 : return Ok(());
4263 : } else {
4264 : // TODO: decide if we want to allow modifying node addresses without removing and re-adding
4265 : // the node. Safest/simplest thing is to refuse it, and usually we deploy with
4266 : // a fixed address through the lifetime of a node.
4267 0 : tracing::warn!(
4268 0 : "Node {} tried to register with different address",
4269 : register_req.node_id
4270 : );
4271 0 : return Err(ApiError::Conflict(
4272 0 : "Node is already registered with different address".to_string(),
4273 0 : ));
4274 : }
4275 0 : }
4276 0 : }
4277 0 :
4278 0 : // We do not require that a node is actually online when registered (it will start life
4279 0 : // with it's availability set to Offline), but we _do_ require that its DNS record exists. We're
4280 0 : // therefore not immune to asymmetric L3 connectivity issues, but we are protected against nodes
4281 0 : // that register themselves with a broken DNS config. We check only the HTTP hostname, because
4282 0 : // the postgres hostname might only be resolvable to clients (e.g. if we're on a different VPC than clients).
4283 0 : if tokio::net::lookup_host(format!(
4284 0 : "{}:{}",
4285 0 : register_req.listen_http_addr, register_req.listen_http_port
4286 0 : ))
4287 0 : .await
4288 0 : .is_err()
4289 : {
4290 : // If we have a transient DNS issue, it's up to the caller to retry their registration. Because
4291 : // we can't robustly distinguish between an intermittent issue and a totally bogus DNS situation,
4292 : // we return a soft 503 error, to encourage callers to retry past transient issues.
4293 0 : return Err(ApiError::ResourceUnavailable(
4294 0 : format!(
4295 0 : "Node {} tried to register with unknown DNS name '{}'",
4296 0 : register_req.node_id, register_req.listen_http_addr
4297 0 : )
4298 0 : .into(),
4299 0 : ));
4300 0 : }
4301 0 :
4302 0 : // Ordering: we must persist the new node _before_ adding it to in-memory state.
4303 0 : // This ensures that before we use it for anything or expose it via any external
4304 0 : // API, it is guaranteed to be available after a restart.
4305 0 : let new_node = Node::new(
4306 0 : register_req.node_id,
4307 0 : register_req.listen_http_addr,
4308 0 : register_req.listen_http_port,
4309 0 : register_req.listen_pg_addr,
4310 0 : register_req.listen_pg_port,
4311 0 : );
4312 0 :
4313 0 : // TODO: idempotency if the node already exists in the database
4314 0 : self.persistence.insert_node(&new_node).await?;
4315 :
4316 0 : let mut locked = self.inner.write().unwrap();
4317 0 : let mut new_nodes = (*locked.nodes).clone();
4318 0 :
4319 0 : locked.scheduler.node_upsert(&new_node);
4320 0 : new_nodes.insert(register_req.node_id, new_node);
4321 0 :
4322 0 : locked.nodes = Arc::new(new_nodes);
4323 0 :
4324 0 : tracing::info!(
4325 0 : "Registered pageserver {}, now have {} pageservers",
4326 0 : register_req.node_id,
4327 0 : locked.nodes.len()
4328 : );
4329 0 : Ok(())
4330 0 : }
4331 :
4332 0 : pub(crate) async fn node_configure(
4333 0 : &self,
4334 0 : node_id: NodeId,
4335 0 : availability: Option<NodeAvailability>,
4336 0 : scheduling: Option<NodeSchedulingPolicy>,
4337 0 : ) -> Result<(), ApiError> {
4338 0 : let _node_lock =
4339 0 : trace_exclusive_lock(&self.node_op_locks, node_id, NodeOperations::Configure).await;
4340 :
4341 0 : if let Some(scheduling) = scheduling {
4342 : // Scheduling is a persistent part of Node: we must write updates to the database before
4343 : // applying them in memory
4344 0 : self.persistence.update_node(node_id, scheduling).await?;
4345 0 : }
4346 :
4347 : // If we're activating a node, then before setting it active we must reconcile any shard locations
4348 : // on that node, in case it is out of sync, e.g. due to being unavailable during controller startup,
4349 : // by calling [`Self::node_activate_reconcile`]
4350 : //
4351 : // The transition we calculate here remains valid later in the function because we hold the op lock on the node:
4352 : // nothing else can mutate its availability while we run.
4353 0 : let availability_transition = if let Some(input_availability) = availability {
4354 0 : let (activate_node, availability_transition) = {
4355 0 : let locked = self.inner.read().unwrap();
4356 0 : let Some(node) = locked.nodes.get(&node_id) else {
4357 0 : return Err(ApiError::NotFound(
4358 0 : anyhow::anyhow!("Node {} not registered", node_id).into(),
4359 0 : ));
4360 : };
4361 :
4362 0 : (
4363 0 : node.clone(),
4364 0 : node.get_availability_transition(input_availability),
4365 0 : )
4366 : };
4367 :
4368 0 : if matches!(availability_transition, AvailabilityTransition::ToActive) {
4369 0 : self.node_activate_reconcile(activate_node, &_node_lock)
4370 0 : .await?;
4371 0 : }
4372 0 : availability_transition
4373 : } else {
4374 0 : AvailabilityTransition::Unchanged
4375 : };
4376 :
4377 : // Apply changes from the request to our in-memory state for the Node
4378 0 : let mut locked = self.inner.write().unwrap();
4379 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
4380 0 :
4381 0 : let mut new_nodes = (**nodes).clone();
4382 :
4383 0 : let Some(node) = new_nodes.get_mut(&node_id) else {
4384 0 : return Err(ApiError::NotFound(
4385 0 : anyhow::anyhow!("Node not registered").into(),
4386 0 : ));
4387 : };
4388 :
4389 0 : if let Some(availability) = &availability {
4390 0 : node.set_availability(*availability);
4391 0 : }
4392 :
4393 0 : if let Some(scheduling) = scheduling {
4394 0 : node.set_scheduling(scheduling);
4395 0 : }
4396 :
4397 : // Update the scheduler, in case the elegibility of the node for new shards has changed
4398 0 : scheduler.node_upsert(node);
4399 0 :
4400 0 : let new_nodes = Arc::new(new_nodes);
4401 0 :
4402 0 : // Modify scheduling state for any Tenants that are affected by a change in the node's availability state.
4403 0 : match availability_transition {
4404 : AvailabilityTransition::ToOffline => {
4405 0 : tracing::info!("Node {} transition to offline", node_id);
4406 0 : let mut tenants_affected: usize = 0;
4407 :
4408 0 : for (tenant_shard_id, tenant_shard) in tenants {
4409 0 : if let Some(observed_loc) = tenant_shard.observed.locations.get_mut(&node_id) {
4410 0 : // When a node goes offline, we set its observed configuration to None, indicating unknown: we will
4411 0 : // not assume our knowledge of the node's configuration is accurate until it comes back online
4412 0 : observed_loc.conf = None;
4413 0 : }
4414 :
4415 0 : if new_nodes.len() == 1 {
4416 : // Special case for single-node cluster: there is no point trying to reschedule
4417 : // any tenant shards: avoid doing so, in order to avoid spewing warnings about
4418 : // failures to schedule them.
4419 0 : continue;
4420 0 : }
4421 0 :
4422 0 : if !new_nodes
4423 0 : .values()
4424 0 : .any(|n| matches!(n.may_schedule(), MaySchedule::Yes(_)))
4425 : {
4426 : // Special case for when all nodes are unavailable and/or unschedulable: there is no point
4427 : // trying to reschedule since there's nowhere else to go. Without this
4428 : // branch we incorrectly detach tenants in response to node unavailability.
4429 0 : continue;
4430 0 : }
4431 0 :
4432 0 : if tenant_shard.intent.demote_attached(scheduler, node_id) {
4433 0 : tenant_shard.sequence = tenant_shard.sequence.next();
4434 0 :
4435 0 : // TODO: populate a ScheduleContext including all shards in the same tenant_id (only matters
4436 0 : // for tenants without secondary locations: if they have a secondary location, then this
4437 0 : // schedule() call is just promoting an existing secondary)
4438 0 : let mut schedule_context = ScheduleContext::default();
4439 0 :
4440 0 : match tenant_shard.schedule(scheduler, &mut schedule_context) {
4441 0 : Err(e) => {
4442 0 : // It is possible that some tenants will become unschedulable when too many pageservers
4443 0 : // go offline: in this case there isn't much we can do other than make the issue observable.
4444 0 : // TODO: give TenantShard a scheduling error attribute to be queried later.
4445 0 : tracing::warn!(%tenant_shard_id, "Scheduling error when marking pageserver {} offline: {e}", node_id);
4446 : }
4447 : Ok(()) => {
4448 0 : if self
4449 0 : .maybe_reconcile_shard(tenant_shard, &new_nodes)
4450 0 : .is_some()
4451 0 : {
4452 0 : tenants_affected += 1;
4453 0 : };
4454 : }
4455 : }
4456 0 : }
4457 : }
4458 0 : tracing::info!(
4459 0 : "Launched {} reconciler tasks for tenants affected by node {} going offline",
4460 : tenants_affected,
4461 : node_id
4462 : )
4463 : }
4464 : AvailabilityTransition::ToActive => {
4465 0 : tracing::info!("Node {} transition to active", node_id);
4466 : // When a node comes back online, we must reconcile any tenant that has a None observed
4467 : // location on the node.
4468 0 : for tenant_shard in locked.tenants.values_mut() {
4469 : // If a reconciliation is already in progress, rely on the previous scheduling
4470 : // decision and skip triggering a new reconciliation.
4471 0 : if tenant_shard.reconciler.is_some() {
4472 0 : continue;
4473 0 : }
4474 :
4475 0 : if let Some(observed_loc) = tenant_shard.observed.locations.get_mut(&node_id) {
4476 0 : if observed_loc.conf.is_none() {
4477 0 : self.maybe_reconcile_shard(tenant_shard, &new_nodes);
4478 0 : }
4479 0 : }
4480 : }
4481 :
4482 : // TODO: in the background, we should balance work back onto this pageserver
4483 : }
4484 : AvailabilityTransition::Unchanged => {
4485 0 : tracing::debug!("Node {} no availability change during config", node_id);
4486 : }
4487 : }
4488 :
4489 0 : locked.nodes = new_nodes;
4490 0 :
4491 0 : Ok(())
4492 0 : }
4493 :
4494 0 : pub(crate) async fn start_node_drain(
4495 0 : self: &Arc<Self>,
4496 0 : node_id: NodeId,
4497 0 : ) -> Result<(), ApiError> {
4498 0 : let (ongoing_op, node_available, node_policy, schedulable_nodes_count) = {
4499 0 : let locked = self.inner.read().unwrap();
4500 0 : let nodes = &locked.nodes;
4501 0 : let node = nodes.get(&node_id).ok_or(ApiError::NotFound(
4502 0 : anyhow::anyhow!("Node {} not registered", node_id).into(),
4503 0 : ))?;
4504 0 : let schedulable_nodes_count = nodes
4505 0 : .iter()
4506 0 : .filter(|(_, n)| matches!(n.may_schedule(), MaySchedule::Yes(_)))
4507 0 : .count();
4508 0 :
4509 0 : (
4510 0 : locked
4511 0 : .ongoing_operation
4512 0 : .as_ref()
4513 0 : .map(|ongoing| ongoing.operation),
4514 0 : node.is_available(),
4515 0 : node.get_scheduling(),
4516 0 : schedulable_nodes_count,
4517 0 : )
4518 : };
4519 :
4520 0 : if let Some(ongoing) = ongoing_op {
4521 0 : return Err(ApiError::PreconditionFailed(
4522 0 : format!("Background operation already ongoing for node: {}", ongoing).into(),
4523 0 : ));
4524 0 : }
4525 0 :
4526 0 : if !node_available {
4527 0 : return Err(ApiError::ResourceUnavailable(
4528 0 : format!("Node {node_id} is currently unavailable").into(),
4529 0 : ));
4530 0 : }
4531 0 :
4532 0 : if schedulable_nodes_count == 0 {
4533 0 : return Err(ApiError::PreconditionFailed(
4534 0 : "No other schedulable nodes to drain to".into(),
4535 0 : ));
4536 0 : }
4537 0 :
4538 0 : match node_policy {
4539 : NodeSchedulingPolicy::Active | NodeSchedulingPolicy::Pause => {
4540 0 : self.node_configure(node_id, None, Some(NodeSchedulingPolicy::Draining))
4541 0 : .await?;
4542 :
4543 0 : let cancel = CancellationToken::new();
4544 0 :
4545 0 : self.inner.write().unwrap().ongoing_operation = Some(OperationHandler {
4546 0 : operation: Operation::Drain(Drain { node_id }),
4547 0 : cancel: cancel.clone(),
4548 0 : });
4549 0 :
4550 0 : tokio::task::spawn({
4551 0 : let service = self.clone();
4552 0 : let cancel = cancel.clone();
4553 0 : async move {
4554 : scopeguard::defer! {
4555 : let prev = service.inner.write().unwrap().ongoing_operation.take();
4556 :
4557 0 : if let Some(Operation::Drain(removed_drain)) = prev.map(|h| h.operation) {
4558 : assert_eq!(removed_drain.node_id, node_id, "We always take the same operation");
4559 : } else {
4560 : panic!("We always remove the same operation")
4561 : }
4562 : }
4563 :
4564 0 : tracing::info!(%node_id, "Drain background operation starting");
4565 0 : let res = service.drain_node(node_id, cancel).await;
4566 0 : match res {
4567 : Ok(()) => {
4568 0 : tracing::info!(%node_id, "Drain background operation completed successfully");
4569 : }
4570 : Err(OperationError::Cancelled) => {
4571 0 : tracing::info!(%node_id, "Drain background operation was cancelled");
4572 : }
4573 0 : Err(err) => {
4574 0 : tracing::error!(%node_id, "Drain background operation encountered: {err}")
4575 : }
4576 : }
4577 0 : }
4578 0 : });
4579 : }
4580 : NodeSchedulingPolicy::Draining => {
4581 0 : return Err(ApiError::Conflict(format!(
4582 0 : "Node {node_id} has drain in progress"
4583 0 : )));
4584 : }
4585 0 : policy => {
4586 0 : return Err(ApiError::PreconditionFailed(
4587 0 : format!("Node {node_id} cannot be drained due to {policy:?} policy").into(),
4588 0 : ));
4589 : }
4590 : }
4591 :
4592 0 : Ok(())
4593 0 : }
4594 :
4595 0 : pub(crate) async fn start_node_fill(self: &Arc<Self>, node_id: NodeId) -> Result<(), ApiError> {
4596 0 : let (ongoing_op, node_available, node_policy, total_nodes_count) = {
4597 0 : let locked = self.inner.read().unwrap();
4598 0 : let nodes = &locked.nodes;
4599 0 : let node = nodes.get(&node_id).ok_or(ApiError::NotFound(
4600 0 : anyhow::anyhow!("Node {} not registered", node_id).into(),
4601 0 : ))?;
4602 :
4603 0 : (
4604 0 : locked
4605 0 : .ongoing_operation
4606 0 : .as_ref()
4607 0 : .map(|ongoing| ongoing.operation),
4608 0 : node.is_available(),
4609 0 : node.get_scheduling(),
4610 0 : nodes.len(),
4611 0 : )
4612 : };
4613 :
4614 0 : if let Some(ongoing) = ongoing_op {
4615 0 : return Err(ApiError::PreconditionFailed(
4616 0 : format!("Background operation already ongoing for node: {}", ongoing).into(),
4617 0 : ));
4618 0 : }
4619 0 :
4620 0 : if !node_available {
4621 0 : return Err(ApiError::ResourceUnavailable(
4622 0 : format!("Node {node_id} is currently unavailable").into(),
4623 0 : ));
4624 0 : }
4625 0 :
4626 0 : if total_nodes_count <= 1 {
4627 0 : return Err(ApiError::PreconditionFailed(
4628 0 : "No other nodes to fill from".into(),
4629 0 : ));
4630 0 : }
4631 0 :
4632 0 : match node_policy {
4633 : NodeSchedulingPolicy::Active => {
4634 0 : self.node_configure(node_id, None, Some(NodeSchedulingPolicy::Filling))
4635 0 : .await?;
4636 :
4637 0 : let cancel = CancellationToken::new();
4638 0 :
4639 0 : self.inner.write().unwrap().ongoing_operation = Some(OperationHandler {
4640 0 : operation: Operation::Fill(Fill { node_id }),
4641 0 : cancel: cancel.clone(),
4642 0 : });
4643 0 :
4644 0 : tokio::task::spawn({
4645 0 : let service = self.clone();
4646 0 : let cancel = cancel.clone();
4647 0 : async move {
4648 : scopeguard::defer! {
4649 : let prev = service.inner.write().unwrap().ongoing_operation.take();
4650 :
4651 0 : if let Some(Operation::Fill(removed_fill)) = prev.map(|h| h.operation) {
4652 : assert_eq!(removed_fill.node_id, node_id, "We always take the same operation");
4653 : } else {
4654 : panic!("We always remove the same operation")
4655 : }
4656 : }
4657 :
4658 0 : tracing::info!(%node_id, "Fill background operation starting");
4659 0 : let res = service.fill_node(node_id, cancel).await;
4660 0 : match res {
4661 : Ok(()) => {
4662 0 : tracing::info!(%node_id, "Fill background operation completed successfully");
4663 : }
4664 : Err(OperationError::Cancelled) => {
4665 0 : tracing::info!(%node_id, "Fill background operation was cancelled");
4666 : }
4667 0 : Err(err) => {
4668 0 : tracing::error!(%node_id, "Fill background operation encountered: {err}")
4669 : }
4670 : }
4671 0 : }
4672 0 : });
4673 : }
4674 : NodeSchedulingPolicy::Filling => {
4675 0 : return Err(ApiError::Conflict(format!(
4676 0 : "Node {node_id} has fill in progress"
4677 0 : )));
4678 : }
4679 0 : policy => {
4680 0 : return Err(ApiError::PreconditionFailed(
4681 0 : format!("Node {node_id} cannot be filled due to {policy:?} policy").into(),
4682 0 : ));
4683 : }
4684 : }
4685 :
4686 0 : Ok(())
4687 0 : }
4688 :
4689 : /// Helper for methods that will try and call pageserver APIs for
4690 : /// a tenant, such as timeline CRUD: they cannot proceed unless the tenant
4691 : /// is attached somewhere.
4692 0 : fn ensure_attached_schedule(
4693 0 : &self,
4694 0 : mut locked: std::sync::RwLockWriteGuard<'_, ServiceState>,
4695 0 : tenant_id: TenantId,
4696 0 : ) -> Result<Vec<ReconcilerWaiter>, anyhow::Error> {
4697 0 : let mut waiters = Vec::new();
4698 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
4699 0 :
4700 0 : let mut schedule_context = ScheduleContext::default();
4701 0 : for (tenant_shard_id, shard) in tenants.range_mut(TenantShardId::tenant_range(tenant_id)) {
4702 0 : shard.schedule(scheduler, &mut schedule_context)?;
4703 :
4704 : // The shard's policies may not result in an attached location being scheduled: this
4705 : // is an error because our caller needs it attached somewhere.
4706 0 : if shard.intent.get_attached().is_none() {
4707 0 : return Err(anyhow::anyhow!(
4708 0 : "Tenant {tenant_id} not scheduled to be attached"
4709 0 : ));
4710 0 : };
4711 0 :
4712 0 : if shard.stably_attached().is_some() {
4713 : // We do not require the shard to be totally up to date on reconciliation: we just require
4714 : // that it has been attached on the intended node. Other dirty state such as unattached secondary
4715 : // locations, or compute hook notifications can be ignored.
4716 0 : continue;
4717 0 : }
4718 :
4719 0 : if let Some(waiter) = self.maybe_reconcile_shard(shard, nodes) {
4720 0 : tracing::info!("Waiting for shard {tenant_shard_id} to reconcile, in order to ensure it is attached");
4721 0 : waiters.push(waiter);
4722 0 : }
4723 : }
4724 0 : Ok(waiters)
4725 0 : }
4726 :
4727 0 : async fn ensure_attached_wait(&self, tenant_id: TenantId) -> Result<(), ApiError> {
4728 0 : let ensure_waiters = {
4729 0 : let locked = self.inner.write().unwrap();
4730 :
4731 : // Check if the tenant is splitting: in this case, even if it is attached,
4732 : // we must act as if it is not: this blocks e.g. timeline creation/deletion
4733 : // operations during the split.
4734 0 : for (_shard_id, shard) in locked.tenants.range(TenantShardId::tenant_range(tenant_id)) {
4735 0 : if !matches!(shard.splitting, SplitState::Idle) {
4736 0 : return Err(ApiError::ResourceUnavailable(
4737 0 : "Tenant shards are currently splitting".into(),
4738 0 : ));
4739 0 : }
4740 : }
4741 :
4742 0 : self.ensure_attached_schedule(locked, tenant_id)
4743 0 : .map_err(ApiError::InternalServerError)?
4744 : };
4745 :
4746 0 : let deadline = Instant::now().checked_add(Duration::from_secs(5)).unwrap();
4747 0 : for waiter in ensure_waiters {
4748 0 : let timeout = deadline.duration_since(Instant::now());
4749 0 : waiter.wait_timeout(timeout).await?;
4750 : }
4751 :
4752 0 : Ok(())
4753 0 : }
4754 :
4755 : /// Wrap [`TenantShard`] reconciliation methods with acquisition of [`Gate`] and [`ReconcileUnits`],
4756 0 : fn maybe_reconcile_shard(
4757 0 : &self,
4758 0 : shard: &mut TenantShard,
4759 0 : nodes: &Arc<HashMap<NodeId, Node>>,
4760 0 : ) -> Option<ReconcilerWaiter> {
4761 0 : let reconcile_needed = shard.get_reconcile_needed(nodes);
4762 0 :
4763 0 : match reconcile_needed {
4764 0 : ReconcileNeeded::No => return None,
4765 0 : ReconcileNeeded::WaitExisting(waiter) => return Some(waiter),
4766 0 : ReconcileNeeded::Yes => {
4767 0 : // Fall through to try and acquire units for spawning reconciler
4768 0 : }
4769 : };
4770 :
4771 0 : let units = match self.reconciler_concurrency.clone().try_acquire_owned() {
4772 0 : Ok(u) => ReconcileUnits::new(u),
4773 : Err(_) => {
4774 0 : tracing::info!(tenant_id=%shard.tenant_shard_id.tenant_id, shard_id=%shard.tenant_shard_id.shard_slug(),
4775 0 : "Concurrency limited: enqueued for reconcile later");
4776 0 : if !shard.delayed_reconcile {
4777 0 : match self.delayed_reconcile_tx.try_send(shard.tenant_shard_id) {
4778 0 : Err(TrySendError::Closed(_)) => {
4779 0 : // Weird mid-shutdown case?
4780 0 : }
4781 : Err(TrySendError::Full(_)) => {
4782 : // It is safe to skip sending our ID in the channel: we will eventually get retried by the background reconcile task.
4783 0 : tracing::warn!(
4784 0 : "Many shards are waiting to reconcile: delayed_reconcile queue is full"
4785 : );
4786 : }
4787 0 : Ok(()) => {
4788 0 : shard.delayed_reconcile = true;
4789 0 : }
4790 : }
4791 0 : }
4792 :
4793 : // We won't spawn a reconciler, but we will construct a waiter that waits for the shard's sequence
4794 : // number to advance. When this function is eventually called again and succeeds in getting units,
4795 : // it will spawn a reconciler that makes this waiter complete.
4796 0 : return Some(shard.future_reconcile_waiter());
4797 : }
4798 : };
4799 :
4800 0 : let Ok(gate_guard) = self.gate.enter() else {
4801 : // Gate closed: we're shutting down, drop out.
4802 0 : return None;
4803 : };
4804 :
4805 0 : shard.spawn_reconciler(
4806 0 : &self.result_tx,
4807 0 : nodes,
4808 0 : &self.compute_hook,
4809 0 : &self.config,
4810 0 : &self.persistence,
4811 0 : units,
4812 0 : gate_guard,
4813 0 : &self.cancel,
4814 0 : )
4815 0 : }
4816 :
4817 : /// Check all tenants for pending reconciliation work, and reconcile those in need.
4818 : /// Additionally, reschedule tenants that require it.
4819 : ///
4820 : /// Returns how many reconciliation tasks were started, or `1` if no reconciles were
4821 : /// spawned but some _would_ have been spawned if `reconciler_concurrency` units where
4822 : /// available. A return value of 0 indicates that everything is fully reconciled already.
4823 0 : fn reconcile_all(&self) -> usize {
4824 0 : let mut locked = self.inner.write().unwrap();
4825 0 : let (nodes, tenants, _scheduler) = locked.parts_mut();
4826 0 : let pageservers = nodes.clone();
4827 0 :
4828 0 : let mut schedule_context = ScheduleContext::default();
4829 0 :
4830 0 : let mut reconciles_spawned = 0;
4831 0 : for (tenant_shard_id, shard) in tenants.iter_mut() {
4832 0 : if tenant_shard_id.is_shard_zero() {
4833 0 : schedule_context = ScheduleContext::default();
4834 0 : }
4835 :
4836 : // Skip checking if this shard is already enqueued for reconciliation
4837 0 : if shard.delayed_reconcile && self.reconciler_concurrency.available_permits() == 0 {
4838 : // If there is something delayed, then return a nonzero count so that
4839 : // callers like reconcile_all_now do not incorrectly get the impression
4840 : // that the system is in a quiescent state.
4841 0 : reconciles_spawned = std::cmp::max(1, reconciles_spawned);
4842 0 : continue;
4843 0 : }
4844 0 :
4845 0 : // Eventual consistency: if an earlier reconcile job failed, and the shard is still
4846 0 : // dirty, spawn another rone
4847 0 : if self.maybe_reconcile_shard(shard, &pageservers).is_some() {
4848 0 : reconciles_spawned += 1;
4849 0 : }
4850 :
4851 0 : schedule_context.avoid(&shard.intent.all_pageservers());
4852 : }
4853 :
4854 0 : reconciles_spawned
4855 0 : }
4856 :
4857 : /// `optimize` in this context means identifying shards which have valid scheduled locations, but
4858 : /// could be scheduled somewhere better:
4859 : /// - Cutting over to a secondary if the node with the secondary is more lightly loaded
4860 : /// * e.g. after a node fails then recovers, to move some work back to it
4861 : /// - Cutting over to a secondary if it improves the spread of shard attachments within a tenant
4862 : /// * e.g. after a shard split, the initial attached locations will all be on the node where
4863 : /// we did the split, but are probably better placed elsewhere.
4864 : /// - Creating new secondary locations if it improves the spreading of a sharded tenant
4865 : /// * e.g. after a shard split, some locations will be on the same node (where the split
4866 : /// happened), and will probably be better placed elsewhere.
4867 : ///
4868 : /// To put it more briefly: whereas the scheduler respects soft constraints in a ScheduleContext at
4869 : /// the time of scheduling, this function looks for cases where a better-scoring location is available
4870 : /// according to those same soft constraints.
4871 0 : async fn optimize_all(&self) -> usize {
4872 0 : // Limit on how many shards' optmizations each call to this function will execute. Combined
4873 0 : // with the frequency of background calls, this acts as an implicit rate limit that runs a small
4874 0 : // trickle of optimizations in the background, rather than executing a large number in parallel
4875 0 : // when a change occurs.
4876 0 : const MAX_OPTIMIZATIONS_EXEC_PER_PASS: usize = 2;
4877 0 :
4878 0 : // Synchronous prepare: scan shards for possible scheduling optimizations
4879 0 : let candidate_work = self.optimize_all_plan();
4880 0 : let candidate_work_len = candidate_work.len();
4881 :
4882 : // Asynchronous validate: I/O to pageservers to make sure shards are in a good state to apply validation
4883 0 : let validated_work = self.optimize_all_validate(candidate_work).await;
4884 :
4885 0 : let was_work_filtered = validated_work.len() != candidate_work_len;
4886 0 :
4887 0 : // Synchronous apply: update the shards' intent states according to validated optimisations
4888 0 : let mut reconciles_spawned = 0;
4889 0 : let mut optimizations_applied = 0;
4890 0 : let mut locked = self.inner.write().unwrap();
4891 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
4892 0 : for (tenant_shard_id, optimization) in validated_work {
4893 0 : let Some(shard) = tenants.get_mut(&tenant_shard_id) else {
4894 : // Shard was dropped between planning and execution;
4895 0 : continue;
4896 : };
4897 0 : if shard.apply_optimization(scheduler, optimization) {
4898 0 : optimizations_applied += 1;
4899 0 : if self.maybe_reconcile_shard(shard, nodes).is_some() {
4900 0 : reconciles_spawned += 1;
4901 0 : }
4902 0 : }
4903 :
4904 0 : if optimizations_applied >= MAX_OPTIMIZATIONS_EXEC_PER_PASS {
4905 0 : break;
4906 0 : }
4907 : }
4908 :
4909 0 : if was_work_filtered {
4910 0 : // If we filtered any work out during validation, ensure we return a nonzero value to indicate
4911 0 : // to callers that the system is not in a truly quiet state, it's going to do some work as soon
4912 0 : // as these validations start passing.
4913 0 : reconciles_spawned = std::cmp::max(reconciles_spawned, 1);
4914 0 : }
4915 :
4916 0 : reconciles_spawned
4917 0 : }
4918 :
4919 0 : fn optimize_all_plan(&self) -> Vec<(TenantShardId, ScheduleOptimization)> {
4920 0 : let mut schedule_context = ScheduleContext::default();
4921 0 :
4922 0 : let mut tenant_shards: Vec<&TenantShard> = Vec::new();
4923 0 :
4924 0 : // How many candidate optimizations we will generate, before evaluating them for readniess: setting
4925 0 : // this higher than the execution limit gives us a chance to execute some work even if the first
4926 0 : // few optimizations we find are not ready.
4927 0 : const MAX_OPTIMIZATIONS_PLAN_PER_PASS: usize = 8;
4928 0 :
4929 0 : let mut work = Vec::new();
4930 0 :
4931 0 : let mut locked = self.inner.write().unwrap();
4932 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
4933 0 : for (tenant_shard_id, shard) in tenants.iter() {
4934 0 : if tenant_shard_id.is_shard_zero() {
4935 0 : // Reset accumulators on the first shard in a tenant
4936 0 : schedule_context = ScheduleContext::default();
4937 0 : schedule_context.mode = ScheduleMode::Speculative;
4938 0 : tenant_shards.clear();
4939 0 : }
4940 :
4941 0 : if work.len() >= MAX_OPTIMIZATIONS_PLAN_PER_PASS {
4942 0 : break;
4943 0 : }
4944 0 :
4945 0 : match shard.get_scheduling_policy() {
4946 0 : ShardSchedulingPolicy::Active => {
4947 0 : // Ok to do optimization
4948 0 : }
4949 : ShardSchedulingPolicy::Essential
4950 : | ShardSchedulingPolicy::Pause
4951 : | ShardSchedulingPolicy::Stop => {
4952 : // Policy prevents optimizing this shard.
4953 0 : continue;
4954 : }
4955 : }
4956 :
4957 : // Accumulate the schedule context for all the shards in a tenant: we must have
4958 : // the total view of all shards before we can try to optimize any of them.
4959 0 : schedule_context.avoid(&shard.intent.all_pageservers());
4960 0 : if let Some(attached) = shard.intent.get_attached() {
4961 0 : schedule_context.push_attached(*attached);
4962 0 : }
4963 0 : tenant_shards.push(shard);
4964 0 :
4965 0 : // Once we have seen the last shard in the tenant, proceed to search across all shards
4966 0 : // in the tenant for optimizations
4967 0 : if shard.shard.number.0 == shard.shard.count.count() - 1 {
4968 0 : if tenant_shards.iter().any(|s| s.reconciler.is_some()) {
4969 : // Do not start any optimizations while another change to the tenant is ongoing: this
4970 : // is not necessary for correctness, but simplifies operations and implicitly throttles
4971 : // optimization changes to happen in a "trickle" over time.
4972 0 : continue;
4973 0 : }
4974 0 :
4975 0 : if tenant_shards.iter().any(|s| {
4976 0 : !matches!(s.splitting, SplitState::Idle)
4977 0 : || matches!(s.policy, PlacementPolicy::Detached)
4978 0 : }) {
4979 : // Never attempt to optimize a tenant that is currently being split, or
4980 : // a tenant that is meant to be detached
4981 0 : continue;
4982 0 : }
4983 :
4984 : // TODO: optimization calculations are relatively expensive: create some fast-path for
4985 : // the common idle case (avoiding the search on tenants that we have recently checked)
4986 :
4987 0 : for shard in &tenant_shards {
4988 0 : if let Some(optimization) =
4989 : // If idle, maybe ptimize attachments: if a shard has a secondary location that is preferable to
4990 : // its primary location based on soft constraints, cut it over.
4991 0 : shard.optimize_attachment(nodes, &schedule_context)
4992 : {
4993 0 : work.push((shard.tenant_shard_id, optimization));
4994 0 : break;
4995 0 : } else if let Some(optimization) =
4996 : // If idle, maybe optimize secondary locations: if a shard has a secondary location that would be
4997 : // better placed on another node, based on ScheduleContext, then adjust it. This
4998 : // covers cases like after a shard split, where we might have too many shards
4999 : // in the same tenant with secondary locations on the node where they originally split.
5000 0 : shard.optimize_secondary(scheduler, &schedule_context)
5001 : {
5002 0 : work.push((shard.tenant_shard_id, optimization));
5003 0 : break;
5004 0 : }
5005 :
5006 : // TODO: extend this mechanism to prefer attaching on nodes with fewer attached
5007 : // tenants (i.e. extend schedule state to distinguish attached from secondary counts),
5008 : // for the total number of attachments on a node (not just within a tenant.)
5009 : }
5010 0 : }
5011 : }
5012 :
5013 0 : work
5014 0 : }
5015 :
5016 0 : async fn optimize_all_validate(
5017 0 : &self,
5018 0 : candidate_work: Vec<(TenantShardId, ScheduleOptimization)>,
5019 0 : ) -> Vec<(TenantShardId, ScheduleOptimization)> {
5020 0 : // Take a clone of the node map to use outside the lock in async validation phase
5021 0 : let validation_nodes = { self.inner.read().unwrap().nodes.clone() };
5022 0 :
5023 0 : let mut want_secondary_status = Vec::new();
5024 0 :
5025 0 : // Validate our plans: this is an async phase where we may do I/O to pageservers to
5026 0 : // check that the state of locations is acceptable to run the optimization, such as
5027 0 : // checking that a secondary location is sufficiently warmed-up to cleanly cut over
5028 0 : // in a live migration.
5029 0 : let mut validated_work = Vec::new();
5030 0 : for (tenant_shard_id, optimization) in candidate_work {
5031 0 : match optimization.action {
5032 : ScheduleOptimizationAction::MigrateAttachment(MigrateAttachment {
5033 : old_attached_node_id: _,
5034 0 : new_attached_node_id,
5035 0 : }) => {
5036 0 : match validation_nodes.get(&new_attached_node_id) {
5037 0 : None => {
5038 0 : // Node was dropped between planning and validation
5039 0 : }
5040 0 : Some(node) => {
5041 0 : if !node.is_available() {
5042 0 : tracing::info!("Skipping optimization migration of {tenant_shard_id} to {new_attached_node_id} because node unavailable");
5043 0 : } else {
5044 0 : // Accumulate optimizations that require fetching secondary status, so that we can execute these
5045 0 : // remote API requests concurrently.
5046 0 : want_secondary_status.push((
5047 0 : tenant_shard_id,
5048 0 : node.clone(),
5049 0 : optimization,
5050 0 : ));
5051 0 : }
5052 : }
5053 : }
5054 : }
5055 : ScheduleOptimizationAction::ReplaceSecondary(_) => {
5056 : // No extra checks needed to replace a secondary: this does not interrupt client access
5057 0 : validated_work.push((tenant_shard_id, optimization))
5058 : }
5059 : };
5060 : }
5061 :
5062 : // Call into pageserver API to find out if the destination secondary location is warm enough for a reasonably smooth migration: we
5063 : // do this so that we avoid spawning a Reconciler that would have to wait minutes/hours for a destination to warm up: that reconciler
5064 : // would hold a precious reconcile semaphore unit the whole time it was waiting for the destination to warm up.
5065 0 : let results = self
5066 0 : .tenant_for_shards_api(
5067 0 : want_secondary_status
5068 0 : .iter()
5069 0 : .map(|i| (i.0, i.1.clone()))
5070 0 : .collect(),
5071 0 : |tenant_shard_id, client| async move {
5072 0 : client.tenant_secondary_status(tenant_shard_id).await
5073 0 : },
5074 0 : 1,
5075 0 : 1,
5076 0 : SHORT_RECONCILE_TIMEOUT,
5077 0 : &self.cancel,
5078 0 : )
5079 0 : .await;
5080 :
5081 0 : for ((tenant_shard_id, node, optimization), secondary_status) in
5082 0 : want_secondary_status.into_iter().zip(results.into_iter())
5083 : {
5084 0 : match secondary_status {
5085 0 : Err(e) => {
5086 0 : tracing::info!("Skipping migration of {tenant_shard_id} to {node}, error querying secondary: {e}");
5087 : }
5088 0 : Ok(progress) => {
5089 0 : // We require secondary locations to have less than 10GiB of downloads pending before we will use
5090 0 : // them in an optimization
5091 0 : const DOWNLOAD_FRESHNESS_THRESHOLD: u64 = 10 * 1024 * 1024 * 1024;
5092 0 :
5093 0 : if progress.heatmap_mtime.is_none()
5094 0 : || progress.bytes_total < DOWNLOAD_FRESHNESS_THRESHOLD
5095 0 : && progress.bytes_downloaded != progress.bytes_total
5096 0 : || progress.bytes_total - progress.bytes_downloaded
5097 0 : > DOWNLOAD_FRESHNESS_THRESHOLD
5098 : {
5099 0 : tracing::info!("Skipping migration of {tenant_shard_id} to {node} because secondary isn't ready: {progress:?}");
5100 : } else {
5101 : // Location looks ready: proceed
5102 0 : tracing::info!(
5103 0 : "{tenant_shard_id} secondary on {node} is warm enough for migration: {progress:?}"
5104 : );
5105 0 : validated_work.push((tenant_shard_id, optimization))
5106 : }
5107 : }
5108 : }
5109 : }
5110 :
5111 0 : validated_work
5112 0 : }
5113 :
5114 : /// Look for shards which are oversized and in need of splitting
5115 0 : async fn autosplit_tenants(self: &Arc<Self>) {
5116 0 : let Some(split_threshold) = self.config.split_threshold else {
5117 : // Auto-splitting is disabled
5118 0 : return;
5119 : };
5120 :
5121 0 : let nodes = self.inner.read().unwrap().nodes.clone();
5122 0 :
5123 0 : const SPLIT_TO_MAX: ShardCount = ShardCount::new(8);
5124 0 :
5125 0 : let mut top_n = Vec::new();
5126 0 :
5127 0 : // Call into each node to look for big tenants
5128 0 : let top_n_request = TopTenantShardsRequest {
5129 0 : // We currently split based on logical size, for simplicity: logical size is a signal of
5130 0 : // the user's intent to run a large database, whereas physical/resident size can be symptoms
5131 0 : // of compaction issues. Eventually we should switch to using resident size to bound the
5132 0 : // disk space impact of one shard.
5133 0 : order_by: models::TenantSorting::MaxLogicalSize,
5134 0 : limit: 10,
5135 0 : where_shards_lt: Some(SPLIT_TO_MAX),
5136 0 : where_gt: Some(split_threshold),
5137 0 : };
5138 0 : for node in nodes.values() {
5139 0 : let request_ref = &top_n_request;
5140 0 : match node
5141 0 : .with_client_retries(
5142 0 : |client| async move {
5143 0 : let request = request_ref.clone();
5144 0 : client.top_tenant_shards(request.clone()).await
5145 0 : },
5146 0 : &self.config.jwt_token,
5147 0 : 3,
5148 0 : 3,
5149 0 : Duration::from_secs(5),
5150 0 : &self.cancel,
5151 0 : )
5152 0 : .await
5153 : {
5154 0 : Some(Ok(node_top_n)) => {
5155 0 : top_n.extend(node_top_n.shards.into_iter());
5156 0 : }
5157 : Some(Err(mgmt_api::Error::Cancelled)) => {
5158 0 : continue;
5159 : }
5160 0 : Some(Err(e)) => {
5161 0 : tracing::warn!("Failed to fetch top N tenants from {node}: {e}");
5162 0 : continue;
5163 : }
5164 : None => {
5165 : // Node is shutting down
5166 0 : continue;
5167 : }
5168 : };
5169 : }
5170 :
5171 : // Pick the biggest tenant to split first
5172 0 : top_n.sort_by_key(|i| i.resident_size);
5173 0 : let Some(split_candidate) = top_n.into_iter().next() else {
5174 0 : tracing::debug!("No split-elegible shards found");
5175 0 : return;
5176 : };
5177 :
5178 : // We spawn a task to run this, so it's exactly like some external API client requesting it. We don't
5179 : // want to block the background reconcile loop on this.
5180 0 : tracing::info!("Auto-splitting tenant for size threshold {split_threshold}: current size {split_candidate:?}");
5181 :
5182 0 : let this = self.clone();
5183 0 : tokio::spawn(
5184 0 : async move {
5185 0 : match this
5186 0 : .tenant_shard_split(
5187 0 : split_candidate.id.tenant_id,
5188 0 : TenantShardSplitRequest {
5189 0 : // Always split to the max number of shards: this avoids stepping through
5190 0 : // intervening shard counts and encountering the overrhead of a split+cleanup
5191 0 : // each time as a tenant grows, and is not too expensive because our max shard
5192 0 : // count is relatively low anyway.
5193 0 : // This policy will be adjusted in future once we support higher shard count.
5194 0 : new_shard_count: SPLIT_TO_MAX.literal(),
5195 0 : new_stripe_size: Some(ShardParameters::DEFAULT_STRIPE_SIZE),
5196 0 : },
5197 0 : )
5198 0 : .await
5199 : {
5200 : Ok(_) => {
5201 0 : tracing::info!("Successful auto-split");
5202 : }
5203 0 : Err(e) => {
5204 0 : tracing::error!("Auto-split failed: {e}");
5205 : }
5206 : }
5207 0 : }
5208 0 : .instrument(tracing::info_span!("auto_split", tenant_id=%split_candidate.id.tenant_id)),
5209 : );
5210 0 : }
5211 :
5212 : /// Useful for tests: run whatever work a background [`Self::reconcile_all`] would have done, but
5213 : /// also wait for any generated Reconcilers to complete. Calling this until it returns zero should
5214 : /// put the system into a quiescent state where future background reconciliations won't do anything.
5215 0 : pub(crate) async fn reconcile_all_now(&self) -> Result<usize, ReconcileWaitError> {
5216 0 : let reconciles_spawned = self.reconcile_all();
5217 0 : let reconciles_spawned = if reconciles_spawned == 0 {
5218 : // Only optimize when we are otherwise idle
5219 0 : self.optimize_all().await
5220 : } else {
5221 0 : reconciles_spawned
5222 : };
5223 :
5224 0 : let waiters = {
5225 0 : let mut waiters = Vec::new();
5226 0 : let locked = self.inner.read().unwrap();
5227 0 : for (_tenant_shard_id, shard) in locked.tenants.iter() {
5228 0 : if let Some(waiter) = shard.get_waiter() {
5229 0 : waiters.push(waiter);
5230 0 : }
5231 : }
5232 0 : waiters
5233 0 : };
5234 0 :
5235 0 : let waiter_count = waiters.len();
5236 0 : match self.await_waiters(waiters, RECONCILE_TIMEOUT).await {
5237 0 : Ok(()) => {}
5238 0 : Err(ReconcileWaitError::Failed(_, reconcile_error))
5239 0 : if matches!(*reconcile_error, ReconcileError::Cancel) =>
5240 0 : {
5241 0 : // Ignore reconciler cancel errors: this reconciler might have shut down
5242 0 : // because some other change superceded it. We will return a nonzero number,
5243 0 : // so the caller knows they might have to call again to quiesce the system.
5244 0 : }
5245 0 : Err(e) => {
5246 0 : return Err(e);
5247 : }
5248 : };
5249 :
5250 0 : tracing::info!(
5251 0 : "{} reconciles in reconcile_all, {} waiters",
5252 : reconciles_spawned,
5253 : waiter_count
5254 : );
5255 :
5256 0 : Ok(std::cmp::max(waiter_count, reconciles_spawned))
5257 0 : }
5258 :
5259 0 : pub async fn shutdown(&self) {
5260 0 : // Note that this already stops processing any results from reconciles: so
5261 0 : // we do not expect that our [`TenantShard`] objects will reach a neat
5262 0 : // final state.
5263 0 : self.cancel.cancel();
5264 0 :
5265 0 : // The cancellation tokens in [`crate::reconciler::Reconciler`] are children
5266 0 : // of our cancellation token, so we do not need to explicitly cancel each of
5267 0 : // them.
5268 0 :
5269 0 : // Background tasks and reconcilers hold gate guards: this waits for them all
5270 0 : // to complete.
5271 0 : self.gate.close().await;
5272 0 : }
5273 :
5274 : /// Drain a node by moving the shards attached to it as primaries.
5275 : /// This is a long running operation and it should run as a separate Tokio task.
5276 0 : pub(crate) async fn drain_node(
5277 0 : &self,
5278 0 : node_id: NodeId,
5279 0 : cancel: CancellationToken,
5280 0 : ) -> Result<(), OperationError> {
5281 0 : let mut last_inspected_shard: Option<TenantShardId> = None;
5282 0 : let mut inspected_all_shards = false;
5283 0 : let mut waiters = Vec::new();
5284 0 : let mut schedule_context = ScheduleContext::default();
5285 :
5286 0 : while !inspected_all_shards {
5287 0 : if cancel.is_cancelled() {
5288 0 : return Err(OperationError::Cancelled);
5289 0 : }
5290 0 :
5291 0 : {
5292 0 : let mut locked = self.inner.write().unwrap();
5293 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
5294 :
5295 0 : let node = nodes.get(&node_id).ok_or(OperationError::NodeStateChanged(
5296 0 : format!("node {node_id} was removed").into(),
5297 0 : ))?;
5298 :
5299 0 : let current_policy = node.get_scheduling();
5300 0 : if !matches!(current_policy, NodeSchedulingPolicy::Draining) {
5301 : // TODO(vlad): maybe cancel pending reconciles before erroring out. need to think
5302 : // about it
5303 0 : return Err(OperationError::NodeStateChanged(
5304 0 : format!("node {node_id} changed state to {current_policy:?}").into(),
5305 0 : ));
5306 0 : }
5307 0 :
5308 0 : let mut cursor = tenants.iter_mut().skip_while({
5309 0 : let skip_past = last_inspected_shard;
5310 0 : move |(tid, _)| match skip_past {
5311 0 : Some(last) => **tid != last,
5312 0 : None => false,
5313 0 : }
5314 0 : });
5315 :
5316 0 : while waiters.len() < MAX_RECONCILES_PER_OPERATION {
5317 0 : let (tid, tenant_shard) = match cursor.next() {
5318 0 : Some(some) => some,
5319 : None => {
5320 0 : inspected_all_shards = true;
5321 0 : break;
5322 : }
5323 : };
5324 :
5325 0 : if tenant_shard.intent.demote_attached(scheduler, node_id) {
5326 0 : match tenant_shard.schedule(scheduler, &mut schedule_context) {
5327 0 : Err(e) => {
5328 0 : tracing::warn!(
5329 0 : tenant_id=%tid.tenant_id, shard_id=%tid.shard_slug(),
5330 0 : "Scheduling error when draining pageserver {} : {e}", node_id
5331 : );
5332 : }
5333 : Ok(()) => {
5334 0 : let scheduled_to = tenant_shard.intent.get_attached();
5335 0 : tracing::info!(
5336 0 : tenant_id=%tid.tenant_id, shard_id=%tid.shard_slug(),
5337 0 : "Rescheduled shard while draining node {}: {} -> {:?}",
5338 : node_id,
5339 : node_id,
5340 : scheduled_to
5341 : );
5342 :
5343 0 : let waiter = self.maybe_reconcile_shard(tenant_shard, nodes);
5344 0 : if let Some(some) = waiter {
5345 0 : waiters.push(some);
5346 0 : }
5347 : }
5348 : }
5349 0 : }
5350 :
5351 0 : last_inspected_shard = Some(*tid);
5352 : }
5353 : }
5354 :
5355 0 : waiters = self
5356 0 : .await_waiters_remainder(waiters, SHORT_RECONCILE_TIMEOUT)
5357 0 : .await;
5358 : }
5359 :
5360 0 : while !waiters.is_empty() {
5361 0 : tracing::info!("Awaiting {} pending drain reconciliations", waiters.len());
5362 :
5363 0 : waiters = self
5364 0 : .await_waiters_remainder(waiters, SHORT_RECONCILE_TIMEOUT)
5365 0 : .await;
5366 : }
5367 :
5368 : // At this point we have done the best we could to drain shards from this node.
5369 : // Set the node scheduling policy to `[NodeSchedulingPolicy::PauseForRestart]`
5370 : // to complete the drain.
5371 0 : if let Err(err) = self
5372 0 : .node_configure(node_id, None, Some(NodeSchedulingPolicy::PauseForRestart))
5373 0 : .await
5374 : {
5375 : // This is not fatal. Anything that is polling the node scheduling policy to detect
5376 : // the end of the drain operations will hang, but all such places should enforce an
5377 : // overall timeout. The scheduling policy will be updated upon node re-attach and/or
5378 : // by the counterpart fill operation.
5379 0 : return Err(OperationError::FinalizeError(
5380 0 : format!(
5381 0 : "Failed to finalise drain of {node_id} by setting scheduling policy to PauseForRestart: {err}"
5382 0 : )
5383 0 : .into(),
5384 0 : ));
5385 0 : }
5386 0 :
5387 0 : Ok(())
5388 0 : }
5389 :
5390 : /// Create a node fill plan (pick secondaries to promote) that meets the following requirements:
5391 : /// 1. The node should be filled until it reaches the expected cluster average of
5392 : /// attached shards. If there are not enough secondaries on the node, the plan stops early.
5393 : /// 2. Select tenant shards to promote such that the number of attached shards is balanced
5394 : /// throughout the cluster. We achieve this by picking tenant shards from each node,
5395 : /// starting from the ones with the largest number of attached shards, until the node
5396 : /// reaches the expected cluster average.
5397 0 : fn fill_node_plan(&self, node_id: NodeId) -> Vec<TenantShardId> {
5398 0 : let mut locked = self.inner.write().unwrap();
5399 0 : let fill_requirement = locked.scheduler.compute_fill_requirement(node_id);
5400 0 :
5401 0 : let mut tids_by_node = locked
5402 0 : .tenants
5403 0 : .iter_mut()
5404 0 : .filter_map(|(tid, tenant_shard)| {
5405 0 : if tenant_shard.intent.get_secondary().contains(&node_id) {
5406 0 : if let Some(primary) = tenant_shard.intent.get_attached() {
5407 0 : return Some((*primary, *tid));
5408 0 : }
5409 0 : }
5410 :
5411 0 : None
5412 0 : })
5413 0 : .into_group_map();
5414 0 :
5415 0 : let expected_attached = locked.scheduler.expected_attached_shard_count();
5416 0 : let nodes_by_load = locked.scheduler.nodes_by_attached_shard_count();
5417 0 :
5418 0 : let mut plan = Vec::new();
5419 0 : for (node_id, attached) in nodes_by_load {
5420 0 : if plan.len() >= fill_requirement
5421 0 : || tids_by_node.is_empty()
5422 0 : || attached <= expected_attached
5423 : {
5424 0 : break;
5425 0 : }
5426 0 :
5427 0 : let can_take = attached - expected_attached;
5428 0 : let mut remove_node = false;
5429 0 : for _ in 0..can_take {
5430 0 : match tids_by_node.get_mut(&node_id) {
5431 0 : Some(tids) => match tids.pop() {
5432 0 : Some(tid) => {
5433 0 : plan.push(tid);
5434 0 : }
5435 : None => {
5436 0 : remove_node = true;
5437 0 : break;
5438 : }
5439 : },
5440 : None => {
5441 0 : break;
5442 : }
5443 : }
5444 : }
5445 :
5446 0 : if remove_node {
5447 0 : tids_by_node.remove(&node_id);
5448 0 : }
5449 : }
5450 :
5451 0 : plan
5452 0 : }
5453 :
5454 : /// Fill a node by promoting its secondaries until the cluster is balanced
5455 : /// with regards to attached shard counts. Note that this operation only
5456 : /// makes sense as a counterpart to the drain implemented in [`Service::drain_node`].
5457 : /// This is a long running operation and it should run as a separate Tokio task.
5458 0 : pub(crate) async fn fill_node(
5459 0 : &self,
5460 0 : node_id: NodeId,
5461 0 : cancel: CancellationToken,
5462 0 : ) -> Result<(), OperationError> {
5463 0 : // TODO(vlad): Currently this operates on the assumption that all
5464 0 : // secondaries are warm. This is not always true (e.g. we just migrated the
5465 0 : // tenant). Take that into consideration by checking the secondary status.
5466 0 : let mut tids_to_promote = self.fill_node_plan(node_id);
5467 0 :
5468 0 : let mut waiters = Vec::new();
5469 0 : let mut schedule_context = ScheduleContext::default();
5470 :
5471 : // Execute the plan we've composed above. Before aplying each move from the plan,
5472 : // we validate to ensure that it has not gone stale in the meantime.
5473 0 : while !tids_to_promote.is_empty() {
5474 0 : if cancel.is_cancelled() {
5475 0 : return Err(OperationError::Cancelled);
5476 0 : }
5477 0 :
5478 0 : {
5479 0 : let mut locked = self.inner.write().unwrap();
5480 0 : let (nodes, tenants, scheduler) = locked.parts_mut();
5481 :
5482 0 : let node = nodes.get(&node_id).ok_or(OperationError::NodeStateChanged(
5483 0 : format!("node {node_id} was removed").into(),
5484 0 : ))?;
5485 :
5486 0 : let current_policy = node.get_scheduling();
5487 0 : if !matches!(current_policy, NodeSchedulingPolicy::Filling) {
5488 : // TODO(vlad): maybe cancel pending reconciles before erroring out. need to think
5489 : // about it
5490 0 : return Err(OperationError::NodeStateChanged(
5491 0 : format!("node {node_id} changed state to {current_policy:?}").into(),
5492 0 : ));
5493 0 : }
5494 :
5495 0 : while waiters.len() < MAX_RECONCILES_PER_OPERATION {
5496 0 : if let Some(tid) = tids_to_promote.pop() {
5497 0 : if let Some(tenant_shard) = tenants.get_mut(&tid) {
5498 : // If the node being filled is not a secondary anymore,
5499 : // skip the promotion.
5500 0 : if !tenant_shard.intent.get_secondary().contains(&node_id) {
5501 0 : continue;
5502 0 : }
5503 0 :
5504 0 : let previously_attached_to = *tenant_shard.intent.get_attached();
5505 0 :
5506 0 : tenant_shard.intent.promote_attached(scheduler, node_id);
5507 0 : match tenant_shard.schedule(scheduler, &mut schedule_context) {
5508 0 : Err(e) => {
5509 0 : tracing::warn!(
5510 0 : tenant_id=%tid.tenant_id, shard_id=%tid.shard_slug(),
5511 0 : "Scheduling error when filling pageserver {} : {e}", node_id
5512 : );
5513 : }
5514 : Ok(()) => {
5515 0 : tracing::info!(
5516 0 : tenant_id=%tid.tenant_id, shard_id=%tid.shard_slug(),
5517 0 : "Rescheduled shard while filling node {}: {:?} -> {}",
5518 : node_id,
5519 : previously_attached_to,
5520 : node_id
5521 : );
5522 :
5523 0 : if let Some(waiter) =
5524 0 : self.maybe_reconcile_shard(tenant_shard, nodes)
5525 0 : {
5526 0 : waiters.push(waiter);
5527 0 : }
5528 : }
5529 : }
5530 0 : }
5531 : } else {
5532 0 : break;
5533 : }
5534 : }
5535 : }
5536 :
5537 0 : waiters = self
5538 0 : .await_waiters_remainder(waiters, SHORT_RECONCILE_TIMEOUT)
5539 0 : .await;
5540 : }
5541 :
5542 0 : while !waiters.is_empty() {
5543 0 : tracing::info!("Awaiting {} pending fill reconciliations", waiters.len());
5544 :
5545 0 : waiters = self
5546 0 : .await_waiters_remainder(waiters, SHORT_RECONCILE_TIMEOUT)
5547 0 : .await;
5548 : }
5549 :
5550 0 : if let Err(err) = self
5551 0 : .node_configure(node_id, None, Some(NodeSchedulingPolicy::Active))
5552 0 : .await
5553 : {
5554 : // This isn't a huge issue since the filling process starts upon request. However, it
5555 : // will prevent the next drain from starting. The only case in which this can fail
5556 : // is database unavailability. Such a case will require manual intervention.
5557 0 : return Err(OperationError::FinalizeError(
5558 0 : format!("Failed to finalise fill of {node_id} by setting scheduling policy to Active: {err}")
5559 0 : .into(),
5560 0 : ));
5561 0 : }
5562 0 :
5563 0 : Ok(())
5564 0 : }
5565 : }
|