Line data Source code
1 : use std::{
2 : collections::{HashMap, HashSet},
3 : sync::Arc,
4 : time::Duration,
5 : };
6 :
7 : use crate::{
8 : metrics::{self, ReconcileCompleteLabelGroup, ReconcileOutcome},
9 : persistence::TenantShardPersistence,
10 : reconciler::{ReconcileUnits, ReconcilerConfig},
11 : scheduler::{AffinityScore, MaySchedule, RefCountUpdate, ScheduleContext},
12 : service::ReconcileResultRequest,
13 : };
14 : use pageserver_api::controller_api::{
15 : NodeSchedulingPolicy, PlacementPolicy, ShardSchedulingPolicy,
16 : };
17 : use pageserver_api::{
18 : models::{LocationConfig, LocationConfigMode, TenantConfig},
19 : shard::{ShardIdentity, TenantShardId},
20 : };
21 : use serde::{Deserialize, Serialize};
22 : use tokio::task::JoinHandle;
23 : use tokio_util::sync::CancellationToken;
24 : use tracing::{instrument, Instrument};
25 : use utils::{
26 : generation::Generation,
27 : id::NodeId,
28 : seqwait::{SeqWait, SeqWaitError},
29 : sync::gate::GateGuard,
30 : };
31 :
32 : use crate::{
33 : compute_hook::ComputeHook,
34 : node::Node,
35 : persistence::{split_state::SplitState, Persistence},
36 : reconciler::{
37 : attached_location_conf, secondary_location_conf, ReconcileError, Reconciler, TargetState,
38 : },
39 : scheduler::{ScheduleError, Scheduler},
40 : service, Sequence,
41 : };
42 :
43 : /// Serialization helper
44 0 : fn read_last_error<S, T>(v: &std::sync::Mutex<Option<T>>, serializer: S) -> Result<S::Ok, S::Error>
45 0 : where
46 0 : S: serde::ser::Serializer,
47 0 : T: std::fmt::Display,
48 0 : {
49 0 : serializer.collect_str(
50 0 : &v.lock()
51 0 : .unwrap()
52 0 : .as_ref()
53 0 : .map(|e| format!("{e}"))
54 0 : .unwrap_or("".to_string()),
55 0 : )
56 0 : }
57 :
58 : /// In-memory state for a particular tenant shard.
59 : ///
60 : /// This struct implement Serialize for debugging purposes, but is _not_ persisted
61 : /// itself: see [`crate::persistence`] for the subset of tenant shard state that is persisted.
62 0 : #[derive(Serialize)]
63 : pub(crate) struct TenantShard {
64 : pub(crate) tenant_shard_id: TenantShardId,
65 :
66 : pub(crate) shard: ShardIdentity,
67 :
68 : // Runtime only: sequence used to coordinate when updating this object while
69 : // with background reconcilers may be running. A reconciler runs to a particular
70 : // sequence.
71 : pub(crate) sequence: Sequence,
72 :
73 : // Latest generation number: next time we attach, increment this
74 : // and use the incremented number when attaching.
75 : //
76 : // None represents an incompletely onboarded tenant via the [`Service::location_config`]
77 : // API, where this tenant may only run in PlacementPolicy::Secondary.
78 : pub(crate) generation: Option<Generation>,
79 :
80 : // High level description of how the tenant should be set up. Provided
81 : // externally.
82 : pub(crate) policy: PlacementPolicy,
83 :
84 : // Low level description of exactly which pageservers should fulfil
85 : // which role. Generated by `Self::schedule`.
86 : pub(crate) intent: IntentState,
87 :
88 : // Low level description of how the tenant is configured on pageservers:
89 : // if this does not match `Self::intent` then the tenant needs reconciliation
90 : // with `Self::reconcile`.
91 : pub(crate) observed: ObservedState,
92 :
93 : // Tenant configuration, passed through opaquely to the pageserver. Identical
94 : // for all shards in a tenant.
95 : pub(crate) config: TenantConfig,
96 :
97 : /// If a reconcile task is currently in flight, it may be joined here (it is
98 : /// only safe to join if either the result has been received or the reconciler's
99 : /// cancellation token has been fired)
100 : #[serde(skip)]
101 : pub(crate) reconciler: Option<ReconcilerHandle>,
102 :
103 : /// If a tenant is being split, then all shards with that TenantId will have a
104 : /// SplitState set, this acts as a guard against other operations such as background
105 : /// reconciliation, and timeline creation.
106 : pub(crate) splitting: SplitState,
107 :
108 : /// If a tenant was enqueued for later reconcile due to hitting concurrency limit, this flag
109 : /// is set. This flag is cleared when the tenant is popped off the delay queue.
110 : pub(crate) delayed_reconcile: bool,
111 :
112 : /// Optionally wait for reconciliation to complete up to a particular
113 : /// sequence number.
114 : #[serde(skip)]
115 : pub(crate) waiter: std::sync::Arc<SeqWait<Sequence, Sequence>>,
116 :
117 : /// Indicates sequence number for which we have encountered an error reconciling. If
118 : /// this advances ahead of [`Self::waiter`] then a reconciliation error has occurred,
119 : /// and callers should stop waiting for `waiter` and propagate the error.
120 : #[serde(skip)]
121 : pub(crate) error_waiter: std::sync::Arc<SeqWait<Sequence, Sequence>>,
122 :
123 : /// The most recent error from a reconcile on this tenant. This is a nested Arc
124 : /// because:
125 : /// - ReconcileWaiters need to Arc-clone the overall object to read it later
126 : /// - ReconcileWaitError needs to use an `Arc<ReconcileError>` because we can construct
127 : /// many waiters for one shard, and the underlying error types are not Clone.
128 : ///
129 : /// TODO: generalize to an array of recent events
130 : /// TOOD: use a ArcSwap instead of mutex for faster reads?
131 : #[serde(serialize_with = "read_last_error")]
132 : pub(crate) last_error: std::sync::Arc<std::sync::Mutex<Option<Arc<ReconcileError>>>>,
133 :
134 : /// If we have a pending compute notification that for some reason we weren't able to send,
135 : /// set this to true. If this is set, calls to [`Self::get_reconcile_needed`] will return Yes
136 : /// and trigger a Reconciler run. This is the mechanism by which compute notifications are included in the scope
137 : /// of state that we publish externally in an eventually consistent way.
138 : pub(crate) pending_compute_notification: bool,
139 :
140 : // Support/debug tool: if something is going wrong or flapping with scheduling, this may
141 : // be set to a non-active state to avoid making changes while the issue is fixed.
142 : scheduling_policy: ShardSchedulingPolicy,
143 : }
144 :
145 : #[derive(Default, Clone, Debug, Serialize)]
146 : pub(crate) struct IntentState {
147 : attached: Option<NodeId>,
148 : secondary: Vec<NodeId>,
149 : }
150 :
151 : impl IntentState {
152 13 : pub(crate) fn new() -> Self {
153 13 : Self {
154 13 : attached: None,
155 13 : secondary: vec![],
156 13 : }
157 13 : }
158 0 : pub(crate) fn single(scheduler: &mut Scheduler, node_id: Option<NodeId>) -> Self {
159 0 : if let Some(node_id) = node_id {
160 0 : scheduler.update_node_ref_counts(node_id, RefCountUpdate::Attach);
161 0 : }
162 0 : Self {
163 0 : attached: node_id,
164 0 : secondary: vec![],
165 0 : }
166 0 : }
167 :
168 24 : pub(crate) fn set_attached(&mut self, scheduler: &mut Scheduler, new_attached: Option<NodeId>) {
169 24 : if self.attached != new_attached {
170 24 : if let Some(old_attached) = self.attached.take() {
171 0 : scheduler.update_node_ref_counts(old_attached, RefCountUpdate::Detach);
172 24 : }
173 24 : if let Some(new_attached) = &new_attached {
174 24 : scheduler.update_node_ref_counts(*new_attached, RefCountUpdate::Attach);
175 24 : }
176 24 : self.attached = new_attached;
177 0 : }
178 24 : }
179 :
180 : /// Like set_attached, but the node is from [`Self::secondary`]. This swaps the node from
181 : /// secondary to attached while maintaining the scheduler's reference counts.
182 5 : pub(crate) fn promote_attached(
183 5 : &mut self,
184 5 : scheduler: &mut Scheduler,
185 5 : promote_secondary: NodeId,
186 5 : ) {
187 5 : // If we call this with a node that isn't in secondary, it would cause incorrect
188 5 : // scheduler reference counting, since we assume the node is already referenced as a secondary.
189 5 : debug_assert!(self.secondary.contains(&promote_secondary));
190 :
191 10 : self.secondary.retain(|n| n != &promote_secondary);
192 5 :
193 5 : let demoted = self.attached;
194 5 : self.attached = Some(promote_secondary);
195 5 :
196 5 : scheduler.update_node_ref_counts(promote_secondary, RefCountUpdate::PromoteSecondary);
197 5 : if let Some(demoted) = demoted {
198 0 : scheduler.update_node_ref_counts(demoted, RefCountUpdate::DemoteAttached);
199 5 : }
200 5 : }
201 :
202 17 : pub(crate) fn push_secondary(&mut self, scheduler: &mut Scheduler, new_secondary: NodeId) {
203 17 : debug_assert!(!self.secondary.contains(&new_secondary));
204 17 : scheduler.update_node_ref_counts(new_secondary, RefCountUpdate::AddSecondary);
205 17 : self.secondary.push(new_secondary);
206 17 : }
207 :
208 : /// It is legal to call this with a node that is not currently a secondary: that is a no-op
209 5 : pub(crate) fn remove_secondary(&mut self, scheduler: &mut Scheduler, node_id: NodeId) {
210 5 : let index = self.secondary.iter().position(|n| *n == node_id);
211 5 : if let Some(index) = index {
212 5 : scheduler.update_node_ref_counts(node_id, RefCountUpdate::RemoveSecondary);
213 5 : self.secondary.remove(index);
214 5 : }
215 5 : }
216 :
217 23 : pub(crate) fn clear_secondary(&mut self, scheduler: &mut Scheduler) {
218 23 : for secondary in self.secondary.drain(..) {
219 12 : scheduler.update_node_ref_counts(secondary, RefCountUpdate::RemoveSecondary);
220 12 : }
221 23 : }
222 :
223 : /// Remove the last secondary node from the list of secondaries
224 0 : pub(crate) fn pop_secondary(&mut self, scheduler: &mut Scheduler) {
225 0 : if let Some(node_id) = self.secondary.pop() {
226 0 : scheduler.update_node_ref_counts(node_id, RefCountUpdate::RemoveSecondary);
227 0 : }
228 0 : }
229 :
230 23 : pub(crate) fn clear(&mut self, scheduler: &mut Scheduler) {
231 23 : if let Some(old_attached) = self.attached.take() {
232 23 : scheduler.update_node_ref_counts(old_attached, RefCountUpdate::Detach);
233 23 : }
234 :
235 23 : self.clear_secondary(scheduler);
236 23 : }
237 :
238 70 : pub(crate) fn all_pageservers(&self) -> Vec<NodeId> {
239 70 : let mut result = Vec::new();
240 70 : if let Some(p) = self.attached {
241 68 : result.push(p)
242 2 : }
243 :
244 70 : result.extend(self.secondary.iter().copied());
245 70 :
246 70 : result
247 70 : }
248 :
249 59 : pub(crate) fn get_attached(&self) -> &Option<NodeId> {
250 59 : &self.attached
251 59 : }
252 :
253 16 : pub(crate) fn get_secondary(&self) -> &Vec<NodeId> {
254 16 : &self.secondary
255 16 : }
256 :
257 : /// If the node is in use as the attached location, demote it into
258 : /// the list of secondary locations. This is used when a node goes offline,
259 : /// and we want to use a different node for attachment, but not permanently
260 : /// forget the location on the offline node.
261 : ///
262 : /// Returns true if a change was made
263 5 : pub(crate) fn demote_attached(&mut self, scheduler: &mut Scheduler, node_id: NodeId) -> bool {
264 5 : if self.attached == Some(node_id) {
265 5 : self.attached = None;
266 5 : self.secondary.push(node_id);
267 5 : scheduler.update_node_ref_counts(node_id, RefCountUpdate::DemoteAttached);
268 5 : true
269 : } else {
270 0 : false
271 : }
272 5 : }
273 : }
274 :
275 : impl Drop for IntentState {
276 24 : fn drop(&mut self) {
277 24 : // Must clear before dropping, to avoid leaving stale refcounts in the Scheduler.
278 24 : // We do not check this while panicking, to avoid polluting unit test failures or
279 24 : // other assertions with this assertion's output. It's still wrong to leak these,
280 24 : // but if we already have a panic then we don't need to independently flag this case.
281 24 : if !(std::thread::panicking()) {
282 24 : debug_assert!(self.attached.is_none() && self.secondary.is_empty());
283 0 : }
284 23 : }
285 : }
286 :
287 0 : #[derive(Default, Clone, Serialize, Deserialize, Debug)]
288 : pub(crate) struct ObservedState {
289 : pub(crate) locations: HashMap<NodeId, ObservedStateLocation>,
290 : }
291 :
292 : /// Our latest knowledge of how this tenant is configured in the outside world.
293 : ///
294 : /// Meaning:
295 : /// * No instance of this type exists for a node: we are certain that we have nothing configured on that
296 : /// node for this shard.
297 : /// * Instance exists with conf==None: we *might* have some state on that node, but we don't know
298 : /// what it is (e.g. we failed partway through configuring it)
299 : /// * Instance exists with conf==Some: this tells us what we last successfully configured on this node,
300 : /// and that configuration will still be present unless something external interfered.
301 0 : #[derive(Clone, Serialize, Deserialize, Debug)]
302 : pub(crate) struct ObservedStateLocation {
303 : /// If None, it means we do not know the status of this shard's location on this node, but
304 : /// we know that we might have some state on this node.
305 : pub(crate) conf: Option<LocationConfig>,
306 : }
307 : pub(crate) struct ReconcilerWaiter {
308 : // For observability purposes, remember the ID of the shard we're
309 : // waiting for.
310 : pub(crate) tenant_shard_id: TenantShardId,
311 :
312 : seq_wait: std::sync::Arc<SeqWait<Sequence, Sequence>>,
313 : error_seq_wait: std::sync::Arc<SeqWait<Sequence, Sequence>>,
314 : error: std::sync::Arc<std::sync::Mutex<Option<Arc<ReconcileError>>>>,
315 : seq: Sequence,
316 : }
317 :
318 : pub(crate) enum ReconcilerStatus {
319 : Done,
320 : Failed,
321 : InProgress,
322 : }
323 :
324 0 : #[derive(thiserror::Error, Debug)]
325 : pub(crate) enum ReconcileWaitError {
326 : #[error("Timeout waiting for shard {0}")]
327 : Timeout(TenantShardId),
328 : #[error("shutting down")]
329 : Shutdown,
330 : #[error("Reconcile error on shard {0}: {1}")]
331 : Failed(TenantShardId, Arc<ReconcileError>),
332 : }
333 :
334 : #[derive(Eq, PartialEq, Debug)]
335 : pub(crate) struct ReplaceSecondary {
336 : old_node_id: NodeId,
337 : new_node_id: NodeId,
338 : }
339 :
340 : #[derive(Eq, PartialEq, Debug)]
341 : pub(crate) struct MigrateAttachment {
342 : pub(crate) old_attached_node_id: NodeId,
343 : pub(crate) new_attached_node_id: NodeId,
344 : }
345 :
346 : #[derive(Eq, PartialEq, Debug)]
347 : pub(crate) enum ScheduleOptimizationAction {
348 : // Replace one of our secondary locations with a different node
349 : ReplaceSecondary(ReplaceSecondary),
350 : // Migrate attachment to an existing secondary location
351 : MigrateAttachment(MigrateAttachment),
352 : }
353 :
354 : #[derive(Eq, PartialEq, Debug)]
355 : pub(crate) struct ScheduleOptimization {
356 : // What was the reconcile sequence when we generated this optimization? The optimization
357 : // should only be applied if the shard's sequence is still at this value, in case other changes
358 : // happened between planning the optimization and applying it.
359 : sequence: Sequence,
360 :
361 : pub(crate) action: ScheduleOptimizationAction,
362 : }
363 :
364 : impl ReconcilerWaiter {
365 0 : pub(crate) async fn wait_timeout(&self, timeout: Duration) -> Result<(), ReconcileWaitError> {
366 : tokio::select! {
367 : result = self.seq_wait.wait_for_timeout(self.seq, timeout)=> {
368 0 : result.map_err(|e| match e {
369 0 : SeqWaitError::Timeout => ReconcileWaitError::Timeout(self.tenant_shard_id),
370 0 : SeqWaitError::Shutdown => ReconcileWaitError::Shutdown
371 0 : })?;
372 : },
373 : result = self.error_seq_wait.wait_for(self.seq) => {
374 0 : result.map_err(|e| match e {
375 0 : SeqWaitError::Shutdown => ReconcileWaitError::Shutdown,
376 0 : SeqWaitError::Timeout => unreachable!()
377 0 : })?;
378 :
379 : return Err(ReconcileWaitError::Failed(self.tenant_shard_id,
380 : self.error.lock().unwrap().clone().expect("If error_seq_wait was advanced error was set").clone()))
381 : }
382 : }
383 :
384 0 : Ok(())
385 0 : }
386 :
387 0 : pub(crate) fn get_status(&self) -> ReconcilerStatus {
388 0 : if self.seq_wait.would_wait_for(self.seq).is_ok() {
389 0 : ReconcilerStatus::Done
390 0 : } else if self.error_seq_wait.would_wait_for(self.seq).is_ok() {
391 0 : ReconcilerStatus::Failed
392 : } else {
393 0 : ReconcilerStatus::InProgress
394 : }
395 0 : }
396 : }
397 :
398 : /// Having spawned a reconciler task, the tenant shard's state will carry enough
399 : /// information to optionally cancel & await it later.
400 : pub(crate) struct ReconcilerHandle {
401 : sequence: Sequence,
402 : handle: JoinHandle<()>,
403 : cancel: CancellationToken,
404 : }
405 :
406 : pub(crate) enum ReconcileNeeded {
407 : /// shard either doesn't need reconciliation, or is forbidden from spawning a reconciler
408 : /// in its current state (e.g. shard split in progress, or ShardSchedulingPolicy forbids it)
409 : No,
410 : /// shard has a reconciler running, and its intent hasn't changed since that one was
411 : /// spawned: wait for the existing reconciler rather than spawning a new one.
412 : WaitExisting(ReconcilerWaiter),
413 : /// shard needs reconciliation: call into [`TenantShard::spawn_reconciler`]
414 : Yes,
415 : }
416 :
417 : /// When a reconcile task completes, it sends this result object
418 : /// to be applied to the primary TenantShard.
419 : pub(crate) struct ReconcileResult {
420 : pub(crate) sequence: Sequence,
421 : /// On errors, `observed` should be treated as an incompleted description
422 : /// of state (i.e. any nodes present in the result should override nodes
423 : /// present in the parent tenant state, but any unmentioned nodes should
424 : /// not be removed from parent tenant state)
425 : pub(crate) result: Result<(), ReconcileError>,
426 :
427 : pub(crate) tenant_shard_id: TenantShardId,
428 : pub(crate) generation: Option<Generation>,
429 : pub(crate) observed: ObservedState,
430 :
431 : /// Set [`TenantShard::pending_compute_notification`] from this flag
432 : pub(crate) pending_compute_notification: bool,
433 : }
434 :
435 : impl ObservedState {
436 0 : pub(crate) fn new() -> Self {
437 0 : Self {
438 0 : locations: HashMap::new(),
439 0 : }
440 0 : }
441 : }
442 :
443 : impl TenantShard {
444 11 : pub(crate) fn new(
445 11 : tenant_shard_id: TenantShardId,
446 11 : shard: ShardIdentity,
447 11 : policy: PlacementPolicy,
448 11 : ) -> Self {
449 11 : Self {
450 11 : tenant_shard_id,
451 11 : policy,
452 11 : intent: IntentState::default(),
453 11 : generation: Some(Generation::new(0)),
454 11 : shard,
455 11 : observed: ObservedState::default(),
456 11 : config: TenantConfig::default(),
457 11 : reconciler: None,
458 11 : splitting: SplitState::Idle,
459 11 : sequence: Sequence(1),
460 11 : delayed_reconcile: false,
461 11 : waiter: Arc::new(SeqWait::new(Sequence(0))),
462 11 : error_waiter: Arc::new(SeqWait::new(Sequence(0))),
463 11 : last_error: Arc::default(),
464 11 : pending_compute_notification: false,
465 11 : scheduling_policy: ShardSchedulingPolicy::default(),
466 11 : }
467 11 : }
468 :
469 : /// For use on startup when learning state from pageservers: generate my [`IntentState`] from my
470 : /// [`ObservedState`], even if it violates my [`PlacementPolicy`]. Call [`Self::schedule`] next,
471 : /// to get an intent state that complies with placement policy. The overall goal is to do scheduling
472 : /// in a way that makes use of any configured locations that already exist in the outside world.
473 1 : pub(crate) fn intent_from_observed(&mut self, scheduler: &mut Scheduler) {
474 1 : // Choose an attached location by filtering observed locations, and then sorting to get the highest
475 1 : // generation
476 1 : let mut attached_locs = self
477 1 : .observed
478 1 : .locations
479 1 : .iter()
480 2 : .filter_map(|(node_id, l)| {
481 2 : if let Some(conf) = &l.conf {
482 2 : if conf.mode == LocationConfigMode::AttachedMulti
483 1 : || conf.mode == LocationConfigMode::AttachedSingle
484 1 : || conf.mode == LocationConfigMode::AttachedStale
485 : {
486 2 : Some((node_id, conf.generation))
487 : } else {
488 0 : None
489 : }
490 : } else {
491 0 : None
492 : }
493 2 : })
494 1 : .collect::<Vec<_>>();
495 1 :
496 2 : attached_locs.sort_by_key(|i| i.1);
497 1 : if let Some((node_id, _gen)) = attached_locs.into_iter().last() {
498 1 : self.intent.set_attached(scheduler, Some(*node_id));
499 1 : }
500 :
501 : // All remaining observed locations generate secondary intents. This includes None
502 : // observations, as these may well have some local content on disk that is usable (this
503 : // is an edge case that might occur if we restarted during a migration or other change)
504 : //
505 : // We may leave intent.attached empty if we didn't find any attached locations: [`Self::schedule`]
506 : // will take care of promoting one of these secondaries to be attached.
507 2 : self.observed.locations.keys().for_each(|node_id| {
508 2 : if Some(*node_id) != self.intent.attached {
509 1 : self.intent.push_secondary(scheduler, *node_id);
510 1 : }
511 2 : });
512 1 : }
513 :
514 : /// Part of [`Self::schedule`] that is used to choose exactly one node to act as the
515 : /// attached pageserver for a shard.
516 : ///
517 : /// Returns whether we modified it, and the NodeId selected.
518 7 : fn schedule_attached(
519 7 : &mut self,
520 7 : scheduler: &mut Scheduler,
521 7 : context: &ScheduleContext,
522 7 : ) -> Result<(bool, NodeId), ScheduleError> {
523 : // No work to do if we already have an attached tenant
524 7 : if let Some(node_id) = self.intent.attached {
525 0 : return Ok((false, node_id));
526 7 : }
527 :
528 7 : if let Some(promote_secondary) = scheduler.node_preferred(&self.intent.secondary) {
529 : // Promote a secondary
530 1 : tracing::debug!("Promoted secondary {} to attached", promote_secondary);
531 1 : self.intent.promote_attached(scheduler, promote_secondary);
532 1 : Ok((true, promote_secondary))
533 : } else {
534 : // Pick a fresh node: either we had no secondaries or none were schedulable
535 6 : let node_id = scheduler.schedule_shard(&self.intent.secondary, context)?;
536 6 : tracing::debug!("Selected {} as attached", node_id);
537 6 : self.intent.set_attached(scheduler, Some(node_id));
538 6 : Ok((true, node_id))
539 : }
540 7 : }
541 :
542 8 : pub(crate) fn schedule(
543 8 : &mut self,
544 8 : scheduler: &mut Scheduler,
545 8 : context: &mut ScheduleContext,
546 8 : ) -> Result<(), ScheduleError> {
547 8 : let r = self.do_schedule(scheduler, context);
548 8 :
549 8 : context.avoid(&self.intent.all_pageservers());
550 8 : if let Some(attached) = self.intent.get_attached() {
551 7 : context.push_attached(*attached);
552 7 : }
553 :
554 8 : r
555 8 : }
556 :
557 8 : pub(crate) fn do_schedule(
558 8 : &mut self,
559 8 : scheduler: &mut Scheduler,
560 8 : context: &ScheduleContext,
561 8 : ) -> Result<(), ScheduleError> {
562 8 : // TODO: before scheduling new nodes, check if any existing content in
563 8 : // self.intent refers to pageservers that are offline, and pick other
564 8 : // pageservers if so.
565 8 :
566 8 : // TODO: respect the splitting bit on tenants: if they are currently splitting then we may not
567 8 : // change their attach location.
568 8 :
569 8 : match self.scheduling_policy {
570 7 : ShardSchedulingPolicy::Active | ShardSchedulingPolicy::Essential => {}
571 : ShardSchedulingPolicy::Pause | ShardSchedulingPolicy::Stop => {
572 : // Warn to make it obvious why other things aren't happening/working, if we skip scheduling
573 1 : tracing::warn!(tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug(),
574 0 : "Scheduling is disabled by policy {:?}", self.scheduling_policy);
575 1 : return Ok(());
576 : }
577 : }
578 :
579 : // Build the set of pageservers already in use by this tenant, to avoid scheduling
580 : // more work on the same pageservers we're already using.
581 7 : let mut modified = false;
582 7 :
583 7 : // Add/remove nodes to fulfil policy
584 7 : use PlacementPolicy::*;
585 7 : match self.policy {
586 7 : Attached(secondary_count) => {
587 7 : let retain_secondaries = if self.intent.attached.is_none()
588 7 : && scheduler.node_preferred(&self.intent.secondary).is_some()
589 : {
590 : // If we have no attached, and one of the secondaries is elegible to be promoted, retain
591 : // one more secondary than we usually would, as one of them will become attached futher down this function.
592 1 : secondary_count + 1
593 : } else {
594 6 : secondary_count
595 : };
596 :
597 7 : while self.intent.secondary.len() > retain_secondaries {
598 0 : // We have no particular preference for one secondary location over another: just
599 0 : // arbitrarily drop from the end
600 0 : self.intent.pop_secondary(scheduler);
601 0 : modified = true;
602 0 : }
603 :
604 : // Should have exactly one attached, and N secondaries
605 7 : let (modified_attached, attached_node_id) =
606 7 : self.schedule_attached(scheduler, context)?;
607 7 : modified |= modified_attached;
608 7 :
609 7 : let mut used_pageservers = vec![attached_node_id];
610 13 : while self.intent.secondary.len() < secondary_count {
611 6 : let node_id = scheduler.schedule_shard(&used_pageservers, context)?;
612 6 : self.intent.push_secondary(scheduler, node_id);
613 6 : used_pageservers.push(node_id);
614 6 : modified = true;
615 : }
616 : }
617 : Secondary => {
618 0 : if let Some(node_id) = self.intent.get_attached() {
619 0 : // Populate secondary by demoting the attached node
620 0 : self.intent.demote_attached(scheduler, *node_id);
621 0 : modified = true;
622 0 : } else if self.intent.secondary.is_empty() {
623 0 : // Populate secondary by scheduling a fresh node
624 0 : let node_id = scheduler.schedule_shard(&[], context)?;
625 0 : self.intent.push_secondary(scheduler, node_id);
626 0 : modified = true;
627 0 : }
628 0 : while self.intent.secondary.len() > 1 {
629 0 : // We have no particular preference for one secondary location over another: just
630 0 : // arbitrarily drop from the end
631 0 : self.intent.pop_secondary(scheduler);
632 0 : modified = true;
633 0 : }
634 : }
635 : Detached => {
636 : // Never add locations in this mode
637 0 : if self.intent.get_attached().is_some() || !self.intent.get_secondary().is_empty() {
638 0 : self.intent.clear(scheduler);
639 0 : modified = true;
640 0 : }
641 : }
642 : }
643 :
644 7 : if modified {
645 7 : self.sequence.0 += 1;
646 7 : }
647 :
648 7 : Ok(())
649 8 : }
650 :
651 : /// Reschedule this tenant shard to one of its secondary locations. Returns a scheduling error
652 : /// if the swap is not possible and leaves the intent state in its original state.
653 : ///
654 : /// Arguments:
655 : /// `attached_to`: the currently attached location matching the intent state (may be None if the
656 : /// shard is not attached)
657 : /// `promote_to`: an optional secondary location of this tenant shard. If set to None, we ask
658 : /// the scheduler to recommend a node
659 0 : pub(crate) fn reschedule_to_secondary(
660 0 : &mut self,
661 0 : promote_to: Option<NodeId>,
662 0 : scheduler: &mut Scheduler,
663 0 : ) -> Result<(), ScheduleError> {
664 0 : let promote_to = match promote_to {
665 0 : Some(node) => node,
666 0 : None => match scheduler.node_preferred(self.intent.get_secondary()) {
667 0 : Some(node) => node,
668 : None => {
669 0 : return Err(ScheduleError::ImpossibleConstraint);
670 : }
671 : },
672 : };
673 :
674 0 : assert!(self.intent.get_secondary().contains(&promote_to));
675 :
676 0 : if let Some(node) = self.intent.get_attached() {
677 0 : let demoted = self.intent.demote_attached(scheduler, *node);
678 0 : if !demoted {
679 0 : return Err(ScheduleError::ImpossibleConstraint);
680 0 : }
681 0 : }
682 :
683 0 : self.intent.promote_attached(scheduler, promote_to);
684 0 :
685 0 : // Increment the sequence number for the edge case where a
686 0 : // reconciler is already running to avoid waiting on the
687 0 : // current reconcile instead of spawning a new one.
688 0 : self.sequence = self.sequence.next();
689 0 :
690 0 : Ok(())
691 0 : }
692 :
693 : /// Optimize attachments: if a shard has a secondary location that is preferable to
694 : /// its primary location based on soft constraints, switch that secondary location
695 : /// to be attached.
696 15 : #[instrument(skip_all, fields(tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug()))]
697 : pub(crate) fn optimize_attachment(
698 : &self,
699 : nodes: &HashMap<NodeId, Node>,
700 : schedule_context: &ScheduleContext,
701 : ) -> Option<ScheduleOptimization> {
702 : let attached = (*self.intent.get_attached())?;
703 : if self.intent.secondary.is_empty() {
704 : // We can only do useful work if we have both attached and secondary locations: this
705 : // function doesn't schedule new locations, only swaps between attached and secondaries.
706 : return None;
707 : }
708 :
709 : let current_affinity_score = schedule_context.get_node_affinity(attached);
710 : let current_attachment_count = schedule_context.get_node_attachments(attached);
711 :
712 : // Generate score for each node, dropping any un-schedulable nodes.
713 : let all_pageservers = self.intent.all_pageservers();
714 : let mut scores = all_pageservers
715 : .iter()
716 30 : .flat_map(|node_id| {
717 30 : let node = nodes.get(node_id);
718 30 : if node.is_none() {
719 0 : None
720 30 : } else if matches!(
721 30 : node.unwrap().get_scheduling(),
722 : NodeSchedulingPolicy::Filling
723 : ) {
724 : // If the node is currently filling, don't count it as a candidate to avoid,
725 : // racing with the background fill.
726 0 : None
727 30 : } else if matches!(node.unwrap().may_schedule(), MaySchedule::No) {
728 0 : None
729 : } else {
730 30 : let affinity_score = schedule_context.get_node_affinity(*node_id);
731 30 : let attachment_count = schedule_context.get_node_attachments(*node_id);
732 30 : Some((*node_id, affinity_score, attachment_count))
733 : }
734 30 : })
735 : .collect::<Vec<_>>();
736 :
737 : // Sort precedence:
738 : // 1st - prefer nodes with the lowest total affinity score
739 : // 2nd - prefer nodes with the lowest number of attachments in this context
740 : // 3rd - if all else is equal, sort by node ID for determinism in tests.
741 30 : scores.sort_by_key(|i| (i.1, i.2, i.0));
742 :
743 : if let Some((preferred_node, preferred_affinity_score, preferred_attachment_count)) =
744 : scores.first()
745 : {
746 : if attached != *preferred_node {
747 : // The best alternative must be more than 1 better than us, otherwise we could end
748 : // up flapping back next time we're called (e.g. there's no point migrating from
749 : // a location with score 1 to a score zero, because on next location the situation
750 : // would be the same, but in reverse).
751 : if current_affinity_score > *preferred_affinity_score + AffinityScore(1)
752 : || current_attachment_count > *preferred_attachment_count + 1
753 : {
754 : tracing::info!(
755 : "Identified optimization: migrate attachment {attached}->{preferred_node} (secondaries {:?})",
756 : self.intent.get_secondary()
757 : );
758 : return Some(ScheduleOptimization {
759 : sequence: self.sequence,
760 : action: ScheduleOptimizationAction::MigrateAttachment(MigrateAttachment {
761 : old_attached_node_id: attached,
762 : new_attached_node_id: *preferred_node,
763 : }),
764 : });
765 : }
766 : } else {
767 : tracing::debug!(
768 : "Node {} is already preferred (score {:?})",
769 : preferred_node,
770 : preferred_affinity_score
771 : );
772 : }
773 : }
774 :
775 : // Fall-through: we didn't find an optimization
776 : None
777 : }
778 :
779 12 : #[instrument(skip_all, fields(tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug()))]
780 : pub(crate) fn optimize_secondary(
781 : &self,
782 : scheduler: &mut Scheduler,
783 : schedule_context: &ScheduleContext,
784 : ) -> Option<ScheduleOptimization> {
785 : if self.intent.secondary.is_empty() {
786 : // We can only do useful work if we have both attached and secondary locations: this
787 : // function doesn't schedule new locations, only swaps between attached and secondaries.
788 : return None;
789 : }
790 :
791 : for secondary in self.intent.get_secondary() {
792 : let Some(affinity_score) = schedule_context.nodes.get(secondary) else {
793 : // We're already on a node unaffected any affinity constraints,
794 : // so we won't change it.
795 : continue;
796 : };
797 :
798 : // Let the scheduler suggest a node, where it would put us if we were scheduling afresh
799 : // This implicitly limits the choice to nodes that are available, and prefers nodes
800 : // with lower utilization.
801 : let Ok(candidate_node) =
802 : scheduler.schedule_shard(&self.intent.all_pageservers(), schedule_context)
803 : else {
804 : // A scheduling error means we have no possible candidate replacements
805 : continue;
806 : };
807 :
808 : let candidate_affinity_score = schedule_context
809 : .nodes
810 : .get(&candidate_node)
811 : .unwrap_or(&AffinityScore::FREE);
812 :
813 : // The best alternative must be more than 1 better than us, otherwise we could end
814 : // up flapping back next time we're called.
815 : if *candidate_affinity_score + AffinityScore(1) < *affinity_score {
816 : // If some other node is available and has a lower score than this node, then
817 : // that other node is a good place to migrate to.
818 : tracing::info!(
819 : "Identified optimization: replace secondary {secondary}->{candidate_node} (current secondaries {:?})",
820 : self.intent.get_secondary()
821 : );
822 : return Some(ScheduleOptimization {
823 : sequence: self.sequence,
824 : action: ScheduleOptimizationAction::ReplaceSecondary(ReplaceSecondary {
825 : old_node_id: *secondary,
826 : new_node_id: candidate_node,
827 : }),
828 : });
829 : }
830 : }
831 :
832 : None
833 : }
834 :
835 : /// Return true if the optimization was really applied: it will not be applied if the optimization's
836 : /// sequence is behind this tenant shard's
837 9 : pub(crate) fn apply_optimization(
838 9 : &mut self,
839 9 : scheduler: &mut Scheduler,
840 9 : optimization: ScheduleOptimization,
841 9 : ) -> bool {
842 9 : if optimization.sequence != self.sequence {
843 0 : return false;
844 9 : }
845 9 :
846 9 : metrics::METRICS_REGISTRY
847 9 : .metrics_group
848 9 : .storage_controller_schedule_optimization
849 9 : .inc();
850 9 :
851 9 : match optimization.action {
852 : ScheduleOptimizationAction::MigrateAttachment(MigrateAttachment {
853 4 : old_attached_node_id,
854 4 : new_attached_node_id,
855 4 : }) => {
856 4 : self.intent.demote_attached(scheduler, old_attached_node_id);
857 4 : self.intent
858 4 : .promote_attached(scheduler, new_attached_node_id);
859 4 : }
860 : ScheduleOptimizationAction::ReplaceSecondary(ReplaceSecondary {
861 5 : old_node_id,
862 5 : new_node_id,
863 5 : }) => {
864 5 : self.intent.remove_secondary(scheduler, old_node_id);
865 5 : self.intent.push_secondary(scheduler, new_node_id);
866 5 : }
867 : }
868 :
869 9 : true
870 9 : }
871 :
872 : /// Query whether the tenant's observed state for attached node matches its intent state, and if so,
873 : /// yield the node ID. This is appropriate for emitting compute hook notifications: we are checking that
874 : /// the node in question is not only where we intend to attach, but that the tenant is indeed already attached there.
875 : ///
876 : /// Reconciliation may still be needed for other aspects of state such as secondaries (see [`Self::dirty`]): this
877 : /// funciton should not be used to decide whether to reconcile.
878 0 : pub(crate) fn stably_attached(&self) -> Option<NodeId> {
879 0 : if let Some(attach_intent) = self.intent.attached {
880 0 : match self.observed.locations.get(&attach_intent) {
881 0 : Some(loc) => match &loc.conf {
882 0 : Some(conf) => match conf.mode {
883 : LocationConfigMode::AttachedMulti
884 : | LocationConfigMode::AttachedSingle
885 : | LocationConfigMode::AttachedStale => {
886 : // Our intent and observed state agree that this node is in an attached state.
887 0 : Some(attach_intent)
888 : }
889 : // Our observed config is not an attached state
890 0 : _ => None,
891 : },
892 : // Our observed state is None, i.e. in flux
893 0 : None => None,
894 : },
895 : // We have no observed state for this node
896 0 : None => None,
897 : }
898 : } else {
899 : // Our intent is not to attach
900 0 : None
901 : }
902 0 : }
903 :
904 0 : fn dirty(&self, nodes: &Arc<HashMap<NodeId, Node>>) -> bool {
905 0 : let mut dirty_nodes = HashSet::new();
906 :
907 0 : if let Some(node_id) = self.intent.attached {
908 : // Maybe panic: it is a severe bug if we try to attach while generation is null.
909 0 : let generation = self
910 0 : .generation
911 0 : .expect("Attempted to enter attached state without a generation");
912 0 :
913 0 : let wanted_conf =
914 0 : attached_location_conf(generation, &self.shard, &self.config, &self.policy);
915 0 : match self.observed.locations.get(&node_id) {
916 0 : Some(conf) if conf.conf.as_ref() == Some(&wanted_conf) => {}
917 0 : Some(_) | None => {
918 0 : dirty_nodes.insert(node_id);
919 0 : }
920 : }
921 0 : }
922 :
923 0 : for node_id in &self.intent.secondary {
924 0 : let wanted_conf = secondary_location_conf(&self.shard, &self.config);
925 0 : match self.observed.locations.get(node_id) {
926 0 : Some(conf) if conf.conf.as_ref() == Some(&wanted_conf) => {}
927 0 : Some(_) | None => {
928 0 : dirty_nodes.insert(*node_id);
929 0 : }
930 : }
931 : }
932 :
933 0 : for node_id in self.observed.locations.keys() {
934 0 : if self.intent.attached != Some(*node_id) && !self.intent.secondary.contains(node_id) {
935 0 : // We have observed state that isn't part of our intent: need to clean it up.
936 0 : dirty_nodes.insert(*node_id);
937 0 : }
938 : }
939 :
940 0 : dirty_nodes.retain(|node_id| {
941 0 : nodes
942 0 : .get(node_id)
943 0 : .map(|n| n.is_available())
944 0 : .unwrap_or(false)
945 0 : });
946 0 :
947 0 : !dirty_nodes.is_empty()
948 0 : }
949 :
950 : #[allow(clippy::too_many_arguments)]
951 0 : #[instrument(skip_all, fields(tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug()))]
952 : pub(crate) fn get_reconcile_needed(
953 : &mut self,
954 : pageservers: &Arc<HashMap<NodeId, Node>>,
955 : ) -> ReconcileNeeded {
956 : // If there are any ambiguous observed states, and the nodes they refer to are available,
957 : // we should reconcile to clean them up.
958 : let mut dirty_observed = false;
959 : for (node_id, observed_loc) in &self.observed.locations {
960 : let node = pageservers
961 : .get(node_id)
962 : .expect("Nodes may not be removed while referenced");
963 : if observed_loc.conf.is_none() && node.is_available() {
964 : dirty_observed = true;
965 : break;
966 : }
967 : }
968 :
969 : let active_nodes_dirty = self.dirty(pageservers);
970 :
971 : // Even if there is no pageserver work to be done, if we have a pending notification to computes,
972 : // wake up a reconciler to send it.
973 : let do_reconcile =
974 : active_nodes_dirty || dirty_observed || self.pending_compute_notification;
975 :
976 : if !do_reconcile {
977 : tracing::debug!("Not dirty, no reconciliation needed.");
978 : return ReconcileNeeded::No;
979 : }
980 :
981 : // If we are currently splitting, then never start a reconciler task: the splitting logic
982 : // requires that shards are not interfered with while it runs. Do this check here rather than
983 : // up top, so that we only log this message if we would otherwise have done a reconciliation.
984 : if !matches!(self.splitting, SplitState::Idle) {
985 : tracing::info!("Refusing to reconcile, splitting in progress");
986 : return ReconcileNeeded::No;
987 : }
988 :
989 : // Reconcile already in flight for the current sequence?
990 : if let Some(handle) = &self.reconciler {
991 : if handle.sequence == self.sequence {
992 : tracing::info!(
993 : "Reconciliation already in progress for sequence {:?}",
994 : self.sequence,
995 : );
996 : return ReconcileNeeded::WaitExisting(ReconcilerWaiter {
997 : tenant_shard_id: self.tenant_shard_id,
998 : seq_wait: self.waiter.clone(),
999 : error_seq_wait: self.error_waiter.clone(),
1000 : error: self.last_error.clone(),
1001 : seq: self.sequence,
1002 : });
1003 : }
1004 : }
1005 :
1006 : // Pre-checks done: finally check whether we may actually do the work
1007 : match self.scheduling_policy {
1008 : ShardSchedulingPolicy::Active
1009 : | ShardSchedulingPolicy::Essential
1010 : | ShardSchedulingPolicy::Pause => {}
1011 : ShardSchedulingPolicy::Stop => {
1012 : // We only reach this point if there is work to do and we're going to skip
1013 : // doing it: warn it obvious why this tenant isn't doing what it ought to.
1014 : tracing::warn!("Skipping reconcile for policy {:?}", self.scheduling_policy);
1015 : return ReconcileNeeded::No;
1016 : }
1017 : }
1018 :
1019 : ReconcileNeeded::Yes
1020 : }
1021 :
1022 : /// Ensure the sequence number is set to a value where waiting for this value will make us wait
1023 : /// for the next reconcile: i.e. it is ahead of all completed or running reconcilers.
1024 : ///
1025 : /// Constructing a ReconcilerWaiter with the resulting sequence number gives the property
1026 : /// that the waiter will not complete until some future Reconciler is constructed and run.
1027 0 : fn ensure_sequence_ahead(&mut self) {
1028 0 : // Find the highest sequence for which a Reconciler has previously run or is currently
1029 0 : // running
1030 0 : let max_seen = std::cmp::max(
1031 0 : self.reconciler
1032 0 : .as_ref()
1033 0 : .map(|r| r.sequence)
1034 0 : .unwrap_or(Sequence(0)),
1035 0 : std::cmp::max(self.waiter.load(), self.error_waiter.load()),
1036 0 : );
1037 0 :
1038 0 : if self.sequence <= max_seen {
1039 0 : self.sequence = max_seen.next();
1040 0 : }
1041 0 : }
1042 :
1043 : /// Create a waiter that will wait for some future Reconciler that hasn't been spawned yet.
1044 : ///
1045 : /// This is appropriate when you can't spawn a reconciler (e.g. due to resource limits), but
1046 : /// you would like to wait on the next reconciler that gets spawned in the background.
1047 0 : pub(crate) fn future_reconcile_waiter(&mut self) -> ReconcilerWaiter {
1048 0 : self.ensure_sequence_ahead();
1049 0 :
1050 0 : ReconcilerWaiter {
1051 0 : tenant_shard_id: self.tenant_shard_id,
1052 0 : seq_wait: self.waiter.clone(),
1053 0 : error_seq_wait: self.error_waiter.clone(),
1054 0 : error: self.last_error.clone(),
1055 0 : seq: self.sequence,
1056 0 : }
1057 0 : }
1058 :
1059 : #[allow(clippy::too_many_arguments)]
1060 0 : #[instrument(skip_all, fields(tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug()))]
1061 : pub(crate) fn spawn_reconciler(
1062 : &mut self,
1063 : result_tx: &tokio::sync::mpsc::UnboundedSender<ReconcileResultRequest>,
1064 : pageservers: &Arc<HashMap<NodeId, Node>>,
1065 : compute_hook: &Arc<ComputeHook>,
1066 : reconciler_config: ReconcilerConfig,
1067 : service_config: &service::Config,
1068 : persistence: &Arc<Persistence>,
1069 : units: ReconcileUnits,
1070 : gate_guard: GateGuard,
1071 : cancel: &CancellationToken,
1072 : ) -> Option<ReconcilerWaiter> {
1073 : // Reconcile in flight for a stale sequence? Our sequence's task will wait for it before
1074 : // doing our sequence's work.
1075 : let old_handle = self.reconciler.take();
1076 :
1077 : // Build list of nodes from which the reconciler should detach
1078 : let mut detach = Vec::new();
1079 : for node_id in self.observed.locations.keys() {
1080 : if self.intent.get_attached() != &Some(*node_id)
1081 : && !self.intent.secondary.contains(node_id)
1082 : {
1083 : detach.push(
1084 : pageservers
1085 : .get(node_id)
1086 : .expect("Intent references non-existent pageserver")
1087 : .clone(),
1088 : )
1089 : }
1090 : }
1091 :
1092 : // Advance the sequence before spawning a reconciler, so that sequence waiters
1093 : // can distinguish between before+after the reconcile completes.
1094 : self.ensure_sequence_ahead();
1095 :
1096 : let reconciler_cancel = cancel.child_token();
1097 : let reconciler_intent = TargetState::from_intent(pageservers, &self.intent);
1098 : let mut reconciler = Reconciler {
1099 : tenant_shard_id: self.tenant_shard_id,
1100 : shard: self.shard,
1101 : placement_policy: self.policy.clone(),
1102 : generation: self.generation,
1103 : intent: reconciler_intent,
1104 : detach,
1105 : reconciler_config,
1106 : config: self.config.clone(),
1107 : observed: self.observed.clone(),
1108 : compute_hook: compute_hook.clone(),
1109 : service_config: service_config.clone(),
1110 : _gate_guard: gate_guard,
1111 : _resource_units: units,
1112 : cancel: reconciler_cancel.clone(),
1113 : persistence: persistence.clone(),
1114 : compute_notify_failure: false,
1115 : };
1116 :
1117 : let reconcile_seq = self.sequence;
1118 :
1119 : tracing::info!(seq=%reconcile_seq, "Spawning Reconciler for sequence {}", self.sequence);
1120 : let must_notify = self.pending_compute_notification;
1121 : let reconciler_span = tracing::info_span!(parent: None, "reconciler", seq=%reconcile_seq,
1122 : tenant_id=%reconciler.tenant_shard_id.tenant_id,
1123 : shard_id=%reconciler.tenant_shard_id.shard_slug());
1124 : metrics::METRICS_REGISTRY
1125 : .metrics_group
1126 : .storage_controller_reconcile_spawn
1127 : .inc();
1128 : let result_tx = result_tx.clone();
1129 : let join_handle = tokio::task::spawn(
1130 0 : async move {
1131 : // Wait for any previous reconcile task to complete before we start
1132 0 : if let Some(old_handle) = old_handle {
1133 0 : old_handle.cancel.cancel();
1134 0 : if let Err(e) = old_handle.handle.await {
1135 : // We can't do much with this other than log it: the task is done, so
1136 : // we may proceed with our work.
1137 0 : tracing::error!("Unexpected join error waiting for reconcile task: {e}");
1138 0 : }
1139 0 : }
1140 :
1141 : // Early check for cancellation before doing any work
1142 : // TODO: wrap all remote API operations in cancellation check
1143 : // as well.
1144 0 : if reconciler.cancel.is_cancelled() {
1145 0 : metrics::METRICS_REGISTRY
1146 0 : .metrics_group
1147 0 : .storage_controller_reconcile_complete
1148 0 : .inc(ReconcileCompleteLabelGroup {
1149 0 : status: ReconcileOutcome::Cancel,
1150 0 : });
1151 0 : return;
1152 0 : }
1153 :
1154 : // Attempt to make observed state match intent state
1155 0 : let result = reconciler.reconcile().await;
1156 :
1157 : // If we know we had a pending compute notification from some previous action, send a notification irrespective
1158 : // of whether the above reconcile() did any work
1159 0 : if result.is_ok() && must_notify {
1160 : // If this fails we will send the need to retry in [`ReconcileResult::pending_compute_notification`]
1161 0 : reconciler.compute_notify().await.ok();
1162 0 : }
1163 :
1164 : // Update result counter
1165 0 : let outcome_label = match &result {
1166 0 : Ok(_) => ReconcileOutcome::Success,
1167 0 : Err(ReconcileError::Cancel) => ReconcileOutcome::Cancel,
1168 0 : Err(_) => ReconcileOutcome::Error,
1169 : };
1170 :
1171 0 : metrics::METRICS_REGISTRY
1172 0 : .metrics_group
1173 0 : .storage_controller_reconcile_complete
1174 0 : .inc(ReconcileCompleteLabelGroup {
1175 0 : status: outcome_label,
1176 0 : });
1177 0 :
1178 0 : // Constructing result implicitly drops Reconciler, freeing any ReconcileUnits before the Service might
1179 0 : // try and schedule more work in response to our result.
1180 0 : let result = ReconcileResult {
1181 0 : sequence: reconcile_seq,
1182 0 : result,
1183 0 : tenant_shard_id: reconciler.tenant_shard_id,
1184 0 : generation: reconciler.generation,
1185 0 : observed: reconciler.observed,
1186 0 : pending_compute_notification: reconciler.compute_notify_failure,
1187 0 : };
1188 0 :
1189 0 : result_tx
1190 0 : .send(ReconcileResultRequest::ReconcileResult(result))
1191 0 : .ok();
1192 0 : }
1193 : .instrument(reconciler_span),
1194 : );
1195 :
1196 : self.reconciler = Some(ReconcilerHandle {
1197 : sequence: self.sequence,
1198 : handle: join_handle,
1199 : cancel: reconciler_cancel,
1200 : });
1201 :
1202 : Some(ReconcilerWaiter {
1203 : tenant_shard_id: self.tenant_shard_id,
1204 : seq_wait: self.waiter.clone(),
1205 : error_seq_wait: self.error_waiter.clone(),
1206 : error: self.last_error.clone(),
1207 : seq: self.sequence,
1208 : })
1209 : }
1210 :
1211 : /// Get a waiter for any reconciliation in flight, but do not start reconciliation
1212 : /// if it is not already running
1213 0 : pub(crate) fn get_waiter(&self) -> Option<ReconcilerWaiter> {
1214 0 : if self.reconciler.is_some() {
1215 0 : Some(ReconcilerWaiter {
1216 0 : tenant_shard_id: self.tenant_shard_id,
1217 0 : seq_wait: self.waiter.clone(),
1218 0 : error_seq_wait: self.error_waiter.clone(),
1219 0 : error: self.last_error.clone(),
1220 0 : seq: self.sequence,
1221 0 : })
1222 : } else {
1223 0 : None
1224 : }
1225 0 : }
1226 :
1227 : /// Called when a ReconcileResult has been emitted and the service is updating
1228 : /// our state: if the result is from a sequence >= my ReconcileHandle, then drop
1229 : /// the handle to indicate there is no longer a reconciliation in progress.
1230 0 : pub(crate) fn reconcile_complete(&mut self, sequence: Sequence) {
1231 0 : if let Some(reconcile_handle) = &self.reconciler {
1232 0 : if reconcile_handle.sequence <= sequence {
1233 0 : self.reconciler = None;
1234 0 : }
1235 0 : }
1236 0 : }
1237 :
1238 : /// If we had any state at all referring to this node ID, drop it. Does not
1239 : /// attempt to reschedule.
1240 : ///
1241 : /// Returns true if we modified the node's intent state.
1242 0 : pub(crate) fn deref_node(&mut self, node_id: NodeId) -> bool {
1243 0 : let mut intent_modified = false;
1244 0 :
1245 0 : // Drop if this node was our attached intent
1246 0 : if self.intent.attached == Some(node_id) {
1247 0 : self.intent.attached = None;
1248 0 : intent_modified = true;
1249 0 : }
1250 :
1251 : // Drop from the list of secondaries, and check if we modified it
1252 0 : let had_secondaries = self.intent.secondary.len();
1253 0 : self.intent.secondary.retain(|n| n != &node_id);
1254 0 : intent_modified |= self.intent.secondary.len() != had_secondaries;
1255 0 :
1256 0 : debug_assert!(!self.intent.all_pageservers().contains(&node_id));
1257 :
1258 0 : intent_modified
1259 0 : }
1260 :
1261 0 : pub(crate) fn set_scheduling_policy(&mut self, p: ShardSchedulingPolicy) {
1262 0 : self.scheduling_policy = p;
1263 0 : }
1264 :
1265 0 : pub(crate) fn get_scheduling_policy(&self) -> &ShardSchedulingPolicy {
1266 0 : &self.scheduling_policy
1267 0 : }
1268 :
1269 0 : pub(crate) fn set_last_error(&mut self, sequence: Sequence, error: ReconcileError) {
1270 0 : // Ordering: always set last_error before advancing sequence, so that sequence
1271 0 : // waiters are guaranteed to see a Some value when they see an error.
1272 0 : *(self.last_error.lock().unwrap()) = Some(Arc::new(error));
1273 0 : self.error_waiter.advance(sequence);
1274 0 : }
1275 :
1276 0 : pub(crate) fn from_persistent(
1277 0 : tsp: TenantShardPersistence,
1278 0 : intent: IntentState,
1279 0 : ) -> anyhow::Result<Self> {
1280 0 : let tenant_shard_id = tsp.get_tenant_shard_id()?;
1281 0 : let shard_identity = tsp.get_shard_identity()?;
1282 :
1283 0 : Ok(Self {
1284 0 : tenant_shard_id,
1285 0 : shard: shard_identity,
1286 0 : sequence: Sequence::initial(),
1287 0 : generation: tsp.generation.map(|g| Generation::new(g as u32)),
1288 0 : policy: serde_json::from_str(&tsp.placement_policy).unwrap(),
1289 0 : intent,
1290 0 : observed: ObservedState::new(),
1291 0 : config: serde_json::from_str(&tsp.config).unwrap(),
1292 0 : reconciler: None,
1293 0 : splitting: tsp.splitting,
1294 0 : waiter: Arc::new(SeqWait::new(Sequence::initial())),
1295 0 : error_waiter: Arc::new(SeqWait::new(Sequence::initial())),
1296 0 : last_error: Arc::default(),
1297 0 : pending_compute_notification: false,
1298 0 : delayed_reconcile: false,
1299 0 : scheduling_policy: serde_json::from_str(&tsp.scheduling_policy).unwrap(),
1300 0 : })
1301 0 : }
1302 :
1303 0 : pub(crate) fn to_persistent(&self) -> TenantShardPersistence {
1304 0 : TenantShardPersistence {
1305 0 : tenant_id: self.tenant_shard_id.tenant_id.to_string(),
1306 0 : shard_number: self.tenant_shard_id.shard_number.0 as i32,
1307 0 : shard_count: self.tenant_shard_id.shard_count.literal() as i32,
1308 0 : shard_stripe_size: self.shard.stripe_size.0 as i32,
1309 0 : generation: self.generation.map(|g| g.into().unwrap_or(0) as i32),
1310 0 : generation_pageserver: self.intent.get_attached().map(|n| n.0 as i64),
1311 0 : placement_policy: serde_json::to_string(&self.policy).unwrap(),
1312 0 : config: serde_json::to_string(&self.config).unwrap(),
1313 0 : splitting: SplitState::default(),
1314 0 : scheduling_policy: serde_json::to_string(&self.scheduling_policy).unwrap(),
1315 0 : }
1316 0 : }
1317 : }
1318 :
1319 : #[cfg(test)]
1320 : pub(crate) mod tests {
1321 : use pageserver_api::{
1322 : controller_api::NodeAvailability,
1323 : shard::{ShardCount, ShardNumber},
1324 : };
1325 : use utils::id::TenantId;
1326 :
1327 : use crate::scheduler::test_utils::make_test_nodes;
1328 :
1329 : use super::*;
1330 :
1331 7 : fn make_test_tenant_shard(policy: PlacementPolicy) -> TenantShard {
1332 7 : let tenant_id = TenantId::generate();
1333 7 : let shard_number = ShardNumber(0);
1334 7 : let shard_count = ShardCount::new(1);
1335 7 :
1336 7 : let tenant_shard_id = TenantShardId {
1337 7 : tenant_id,
1338 7 : shard_number,
1339 7 : shard_count,
1340 7 : };
1341 7 : TenantShard::new(
1342 7 : tenant_shard_id,
1343 7 : ShardIdentity::new(
1344 7 : shard_number,
1345 7 : shard_count,
1346 7 : pageserver_api::shard::ShardStripeSize(32768),
1347 7 : )
1348 7 : .unwrap(),
1349 7 : policy,
1350 7 : )
1351 7 : }
1352 :
1353 1 : fn make_test_tenant(policy: PlacementPolicy, shard_count: ShardCount) -> Vec<TenantShard> {
1354 1 : let tenant_id = TenantId::generate();
1355 1 :
1356 1 : (0..shard_count.count())
1357 4 : .map(|i| {
1358 4 : let shard_number = ShardNumber(i);
1359 4 :
1360 4 : let tenant_shard_id = TenantShardId {
1361 4 : tenant_id,
1362 4 : shard_number,
1363 4 : shard_count,
1364 4 : };
1365 4 : TenantShard::new(
1366 4 : tenant_shard_id,
1367 4 : ShardIdentity::new(
1368 4 : shard_number,
1369 4 : shard_count,
1370 4 : pageserver_api::shard::ShardStripeSize(32768),
1371 4 : )
1372 4 : .unwrap(),
1373 4 : policy.clone(),
1374 4 : )
1375 4 : })
1376 1 : .collect()
1377 1 : }
1378 :
1379 : /// Test the scheduling behaviors used when a tenant configured for HA is subject
1380 : /// to nodes being marked offline.
1381 : #[test]
1382 1 : fn tenant_ha_scheduling() -> anyhow::Result<()> {
1383 1 : // Start with three nodes. Our tenant will only use two. The third one is
1384 1 : // expected to remain unused.
1385 1 : let mut nodes = make_test_nodes(3);
1386 1 :
1387 1 : let mut scheduler = Scheduler::new(nodes.values());
1388 1 : let mut context = ScheduleContext::default();
1389 1 :
1390 1 : let mut tenant_shard = make_test_tenant_shard(PlacementPolicy::Attached(1));
1391 1 : tenant_shard
1392 1 : .schedule(&mut scheduler, &mut context)
1393 1 : .expect("we have enough nodes, scheduling should work");
1394 1 :
1395 1 : // Expect to initially be schedule on to different nodes
1396 1 : assert_eq!(tenant_shard.intent.secondary.len(), 1);
1397 1 : assert!(tenant_shard.intent.attached.is_some());
1398 :
1399 1 : let attached_node_id = tenant_shard.intent.attached.unwrap();
1400 1 : let secondary_node_id = *tenant_shard.intent.secondary.iter().last().unwrap();
1401 1 : assert_ne!(attached_node_id, secondary_node_id);
1402 :
1403 : // Notifying the attached node is offline should demote it to a secondary
1404 1 : let changed = tenant_shard
1405 1 : .intent
1406 1 : .demote_attached(&mut scheduler, attached_node_id);
1407 1 : assert!(changed);
1408 1 : assert!(tenant_shard.intent.attached.is_none());
1409 1 : assert_eq!(tenant_shard.intent.secondary.len(), 2);
1410 :
1411 : // Update the scheduler state to indicate the node is offline
1412 1 : nodes
1413 1 : .get_mut(&attached_node_id)
1414 1 : .unwrap()
1415 1 : .set_availability(NodeAvailability::Offline);
1416 1 : scheduler.node_upsert(nodes.get(&attached_node_id).unwrap());
1417 1 :
1418 1 : // Scheduling the node should promote the still-available secondary node to attached
1419 1 : tenant_shard
1420 1 : .schedule(&mut scheduler, &mut context)
1421 1 : .expect("active nodes are available");
1422 1 : assert_eq!(tenant_shard.intent.attached.unwrap(), secondary_node_id);
1423 :
1424 : // The original attached node should have been retained as a secondary
1425 1 : assert_eq!(
1426 1 : *tenant_shard.intent.secondary.iter().last().unwrap(),
1427 1 : attached_node_id
1428 1 : );
1429 :
1430 1 : tenant_shard.intent.clear(&mut scheduler);
1431 1 :
1432 1 : Ok(())
1433 1 : }
1434 :
1435 : #[test]
1436 1 : fn intent_from_observed() -> anyhow::Result<()> {
1437 1 : let nodes = make_test_nodes(3);
1438 1 : let mut scheduler = Scheduler::new(nodes.values());
1439 1 :
1440 1 : let mut tenant_shard = make_test_tenant_shard(PlacementPolicy::Attached(1));
1441 1 :
1442 1 : tenant_shard.observed.locations.insert(
1443 1 : NodeId(3),
1444 1 : ObservedStateLocation {
1445 1 : conf: Some(LocationConfig {
1446 1 : mode: LocationConfigMode::AttachedMulti,
1447 1 : generation: Some(2),
1448 1 : secondary_conf: None,
1449 1 : shard_number: tenant_shard.shard.number.0,
1450 1 : shard_count: tenant_shard.shard.count.literal(),
1451 1 : shard_stripe_size: tenant_shard.shard.stripe_size.0,
1452 1 : tenant_conf: TenantConfig::default(),
1453 1 : }),
1454 1 : },
1455 1 : );
1456 1 :
1457 1 : tenant_shard.observed.locations.insert(
1458 1 : NodeId(2),
1459 1 : ObservedStateLocation {
1460 1 : conf: Some(LocationConfig {
1461 1 : mode: LocationConfigMode::AttachedStale,
1462 1 : generation: Some(1),
1463 1 : secondary_conf: None,
1464 1 : shard_number: tenant_shard.shard.number.0,
1465 1 : shard_count: tenant_shard.shard.count.literal(),
1466 1 : shard_stripe_size: tenant_shard.shard.stripe_size.0,
1467 1 : tenant_conf: TenantConfig::default(),
1468 1 : }),
1469 1 : },
1470 1 : );
1471 1 :
1472 1 : tenant_shard.intent_from_observed(&mut scheduler);
1473 1 :
1474 1 : // The highest generationed attached location gets used as attached
1475 1 : assert_eq!(tenant_shard.intent.attached, Some(NodeId(3)));
1476 : // Other locations get used as secondary
1477 1 : assert_eq!(tenant_shard.intent.secondary, vec![NodeId(2)]);
1478 :
1479 1 : scheduler.consistency_check(nodes.values(), [&tenant_shard].into_iter())?;
1480 :
1481 1 : tenant_shard.intent.clear(&mut scheduler);
1482 1 : Ok(())
1483 1 : }
1484 :
1485 : #[test]
1486 1 : fn scheduling_mode() -> anyhow::Result<()> {
1487 1 : let nodes = make_test_nodes(3);
1488 1 : let mut scheduler = Scheduler::new(nodes.values());
1489 1 :
1490 1 : let mut tenant_shard = make_test_tenant_shard(PlacementPolicy::Attached(1));
1491 1 :
1492 1 : // In pause mode, schedule() shouldn't do anything
1493 1 : tenant_shard.scheduling_policy = ShardSchedulingPolicy::Pause;
1494 1 : assert!(tenant_shard
1495 1 : .schedule(&mut scheduler, &mut ScheduleContext::default())
1496 1 : .is_ok());
1497 1 : assert!(tenant_shard.intent.all_pageservers().is_empty());
1498 :
1499 : // In active mode, schedule() works
1500 1 : tenant_shard.scheduling_policy = ShardSchedulingPolicy::Active;
1501 1 : assert!(tenant_shard
1502 1 : .schedule(&mut scheduler, &mut ScheduleContext::default())
1503 1 : .is_ok());
1504 1 : assert!(!tenant_shard.intent.all_pageservers().is_empty());
1505 :
1506 1 : tenant_shard.intent.clear(&mut scheduler);
1507 1 : Ok(())
1508 1 : }
1509 :
1510 : #[test]
1511 1 : fn optimize_attachment() -> anyhow::Result<()> {
1512 1 : let nodes = make_test_nodes(3);
1513 1 : let mut scheduler = Scheduler::new(nodes.values());
1514 1 :
1515 1 : let mut shard_a = make_test_tenant_shard(PlacementPolicy::Attached(1));
1516 1 : let mut shard_b = make_test_tenant_shard(PlacementPolicy::Attached(1));
1517 1 :
1518 1 : // Initially: both nodes attached on shard 1, and both have secondary locations
1519 1 : // on different nodes.
1520 1 : shard_a.intent.set_attached(&mut scheduler, Some(NodeId(1)));
1521 1 : shard_a.intent.push_secondary(&mut scheduler, NodeId(2));
1522 1 : shard_b.intent.set_attached(&mut scheduler, Some(NodeId(1)));
1523 1 : shard_b.intent.push_secondary(&mut scheduler, NodeId(3));
1524 1 :
1525 1 : let mut schedule_context = ScheduleContext::default();
1526 1 : schedule_context.avoid(&shard_a.intent.all_pageservers());
1527 1 : schedule_context.push_attached(shard_a.intent.get_attached().unwrap());
1528 1 : schedule_context.avoid(&shard_b.intent.all_pageservers());
1529 1 : schedule_context.push_attached(shard_b.intent.get_attached().unwrap());
1530 1 :
1531 1 : let optimization_a = shard_a.optimize_attachment(&nodes, &schedule_context);
1532 1 :
1533 1 : // Either shard should recognize that it has the option to switch to a secondary location where there
1534 1 : // would be no other shards from the same tenant, and request to do so.
1535 1 : assert_eq!(
1536 1 : optimization_a,
1537 1 : Some(ScheduleOptimization {
1538 1 : sequence: shard_a.sequence,
1539 1 : action: ScheduleOptimizationAction::MigrateAttachment(MigrateAttachment {
1540 1 : old_attached_node_id: NodeId(1),
1541 1 : new_attached_node_id: NodeId(2)
1542 1 : })
1543 1 : })
1544 1 : );
1545 :
1546 : // Note that these optimizing two shards in the same tenant with the same ScheduleContext is
1547 : // mutually exclusive (the optimization of one invalidates the stats) -- it is the responsibility
1548 : // of [`Service::optimize_all`] to avoid trying
1549 : // to do optimizations for multiple shards in the same tenant at the same time. Generating
1550 : // both optimizations is just done for test purposes
1551 1 : let optimization_b = shard_b.optimize_attachment(&nodes, &schedule_context);
1552 1 : assert_eq!(
1553 1 : optimization_b,
1554 1 : Some(ScheduleOptimization {
1555 1 : sequence: shard_b.sequence,
1556 1 : action: ScheduleOptimizationAction::MigrateAttachment(MigrateAttachment {
1557 1 : old_attached_node_id: NodeId(1),
1558 1 : new_attached_node_id: NodeId(3)
1559 1 : })
1560 1 : })
1561 1 : );
1562 :
1563 : // Applying these optimizations should result in the end state proposed
1564 1 : shard_a.apply_optimization(&mut scheduler, optimization_a.unwrap());
1565 1 : assert_eq!(shard_a.intent.get_attached(), &Some(NodeId(2)));
1566 1 : assert_eq!(shard_a.intent.get_secondary(), &vec![NodeId(1)]);
1567 1 : shard_b.apply_optimization(&mut scheduler, optimization_b.unwrap());
1568 1 : assert_eq!(shard_b.intent.get_attached(), &Some(NodeId(3)));
1569 1 : assert_eq!(shard_b.intent.get_secondary(), &vec![NodeId(1)]);
1570 :
1571 1 : shard_a.intent.clear(&mut scheduler);
1572 1 : shard_b.intent.clear(&mut scheduler);
1573 1 :
1574 1 : Ok(())
1575 1 : }
1576 :
1577 : #[test]
1578 1 : fn optimize_secondary() -> anyhow::Result<()> {
1579 1 : let nodes = make_test_nodes(4);
1580 1 : let mut scheduler = Scheduler::new(nodes.values());
1581 1 :
1582 1 : let mut shard_a = make_test_tenant_shard(PlacementPolicy::Attached(1));
1583 1 : let mut shard_b = make_test_tenant_shard(PlacementPolicy::Attached(1));
1584 1 :
1585 1 : // Initially: both nodes attached on shard 1, and both have secondary locations
1586 1 : // on different nodes.
1587 1 : shard_a.intent.set_attached(&mut scheduler, Some(NodeId(1)));
1588 1 : shard_a.intent.push_secondary(&mut scheduler, NodeId(3));
1589 1 : shard_b.intent.set_attached(&mut scheduler, Some(NodeId(2)));
1590 1 : shard_b.intent.push_secondary(&mut scheduler, NodeId(3));
1591 1 :
1592 1 : let mut schedule_context = ScheduleContext::default();
1593 1 : schedule_context.avoid(&shard_a.intent.all_pageservers());
1594 1 : schedule_context.push_attached(shard_a.intent.get_attached().unwrap());
1595 1 : schedule_context.avoid(&shard_b.intent.all_pageservers());
1596 1 : schedule_context.push_attached(shard_b.intent.get_attached().unwrap());
1597 1 :
1598 1 : let optimization_a = shard_a.optimize_secondary(&mut scheduler, &schedule_context);
1599 1 :
1600 1 : // Since there is a node with no locations available, the node with two locations for the
1601 1 : // same tenant should generate an optimization to move one away
1602 1 : assert_eq!(
1603 1 : optimization_a,
1604 1 : Some(ScheduleOptimization {
1605 1 : sequence: shard_a.sequence,
1606 1 : action: ScheduleOptimizationAction::ReplaceSecondary(ReplaceSecondary {
1607 1 : old_node_id: NodeId(3),
1608 1 : new_node_id: NodeId(4)
1609 1 : })
1610 1 : })
1611 1 : );
1612 :
1613 1 : shard_a.apply_optimization(&mut scheduler, optimization_a.unwrap());
1614 1 : assert_eq!(shard_a.intent.get_attached(), &Some(NodeId(1)));
1615 1 : assert_eq!(shard_a.intent.get_secondary(), &vec![NodeId(4)]);
1616 :
1617 1 : shard_a.intent.clear(&mut scheduler);
1618 1 : shard_b.intent.clear(&mut scheduler);
1619 1 :
1620 1 : Ok(())
1621 1 : }
1622 :
1623 : // Optimize til quiescent: this emulates what Service::optimize_all does, when
1624 : // called repeatedly in the background.
1625 1 : fn optimize_til_idle(
1626 1 : nodes: &HashMap<NodeId, Node>,
1627 1 : scheduler: &mut Scheduler,
1628 1 : shards: &mut [TenantShard],
1629 1 : ) {
1630 1 : let mut loop_n = 0;
1631 : loop {
1632 7 : let mut schedule_context = ScheduleContext::default();
1633 7 : let mut any_changed = false;
1634 :
1635 28 : for shard in shards.iter() {
1636 28 : schedule_context.avoid(&shard.intent.all_pageservers());
1637 28 : if let Some(attached) = shard.intent.get_attached() {
1638 28 : schedule_context.push_attached(*attached);
1639 28 : }
1640 : }
1641 :
1642 13 : for shard in shards.iter_mut() {
1643 13 : let optimization = shard.optimize_attachment(nodes, &schedule_context);
1644 13 : if let Some(optimization) = optimization {
1645 2 : shard.apply_optimization(scheduler, optimization);
1646 2 : any_changed = true;
1647 2 : break;
1648 11 : }
1649 11 :
1650 11 : let optimization = shard.optimize_secondary(scheduler, &schedule_context);
1651 11 : if let Some(optimization) = optimization {
1652 4 : shard.apply_optimization(scheduler, optimization);
1653 4 : any_changed = true;
1654 4 : break;
1655 7 : }
1656 : }
1657 :
1658 7 : if !any_changed {
1659 1 : break;
1660 6 : }
1661 6 :
1662 6 : // Assert no infinite loop
1663 6 : loop_n += 1;
1664 6 : assert!(loop_n < 1000);
1665 : }
1666 1 : }
1667 :
1668 : /// Test the balancing behavior of shard scheduling: that it achieves a balance, and
1669 : /// that it converges.
1670 : #[test]
1671 1 : fn optimize_add_nodes() -> anyhow::Result<()> {
1672 1 : let nodes = make_test_nodes(4);
1673 1 :
1674 1 : // Only show the scheduler a couple of nodes
1675 1 : let mut scheduler = Scheduler::new([].iter());
1676 1 : scheduler.node_upsert(nodes.get(&NodeId(1)).unwrap());
1677 1 : scheduler.node_upsert(nodes.get(&NodeId(2)).unwrap());
1678 1 :
1679 1 : let mut shards = make_test_tenant(PlacementPolicy::Attached(1), ShardCount::new(4));
1680 1 : let mut schedule_context = ScheduleContext::default();
1681 5 : for shard in &mut shards {
1682 4 : assert!(shard
1683 4 : .schedule(&mut scheduler, &mut schedule_context)
1684 4 : .is_ok());
1685 : }
1686 :
1687 : // We should see equal number of locations on the two nodes.
1688 1 : assert_eq!(scheduler.get_node_shard_count(NodeId(1)), 4);
1689 1 : assert_eq!(scheduler.get_node_attached_shard_count(NodeId(1)), 2);
1690 :
1691 1 : assert_eq!(scheduler.get_node_shard_count(NodeId(2)), 4);
1692 1 : assert_eq!(scheduler.get_node_attached_shard_count(NodeId(2)), 2);
1693 :
1694 : // Add another two nodes: we should see the shards spread out when their optimize
1695 : // methods are called
1696 1 : scheduler.node_upsert(nodes.get(&NodeId(3)).unwrap());
1697 1 : scheduler.node_upsert(nodes.get(&NodeId(4)).unwrap());
1698 1 : optimize_til_idle(&nodes, &mut scheduler, &mut shards);
1699 1 :
1700 1 : assert_eq!(scheduler.get_node_shard_count(NodeId(1)), 2);
1701 1 : assert_eq!(scheduler.get_node_attached_shard_count(NodeId(1)), 1);
1702 :
1703 1 : assert_eq!(scheduler.get_node_shard_count(NodeId(2)), 2);
1704 1 : assert_eq!(scheduler.get_node_attached_shard_count(NodeId(2)), 1);
1705 :
1706 1 : assert_eq!(scheduler.get_node_shard_count(NodeId(3)), 2);
1707 1 : assert_eq!(scheduler.get_node_attached_shard_count(NodeId(3)), 1);
1708 :
1709 1 : assert_eq!(scheduler.get_node_shard_count(NodeId(4)), 2);
1710 1 : assert_eq!(scheduler.get_node_attached_shard_count(NodeId(4)), 1);
1711 :
1712 4 : for shard in shards.iter_mut() {
1713 4 : shard.intent.clear(&mut scheduler);
1714 4 : }
1715 :
1716 1 : Ok(())
1717 1 : }
1718 : }
|