mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-03 16:05:28 +00:00
refactor(repartition): release source compaction pauses by region
Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit dc6faab429)
This commit is contained in:
@@ -1043,8 +1043,8 @@ pub enum MetadataCompaction {
|
||||
region_id: RegionId,
|
||||
remaining: Duration,
|
||||
},
|
||||
/// Release only if the latest Pause has this mailbox message ID.
|
||||
Release { region_id: RegionId, pause_id: u64 },
|
||||
/// Resume compaction on the source metadata region.
|
||||
Release { region_id: RegionId },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Display, PartialEq)]
|
||||
@@ -1494,7 +1494,7 @@ mod tests {
|
||||
"remaining": {"secs": 19, "nanos": 500000000}
|
||||
}}},
|
||||
{"MetadataCompaction": {"Release": {
|
||||
"region_id": 4294967298_u64, "pause_id": 42
|
||||
"region_id": 4294967298_u64
|
||||
}}}
|
||||
]);
|
||||
let region_id = RegionId::new(1, 2);
|
||||
@@ -1503,10 +1503,7 @@ mod tests {
|
||||
region_id,
|
||||
remaining: Duration::from_millis(19_500),
|
||||
}),
|
||||
Instruction::MetadataCompaction(MetadataCompaction::Release {
|
||||
region_id,
|
||||
pause_id: 42,
|
||||
}),
|
||||
Instruction::MetadataCompaction(MetadataCompaction::Release { region_id }),
|
||||
];
|
||||
assert_eq!(
|
||||
expected,
|
||||
|
||||
@@ -77,7 +77,6 @@ pub trait InstructionHandler: Send + Sync {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HandlerContext {
|
||||
mailbox_message_id: u64,
|
||||
pub region_server: RegionServer,
|
||||
pub downgrade_tasks: TaskTracker<()>,
|
||||
pub flush_tasks: TaskTracker<()>,
|
||||
@@ -89,7 +88,6 @@ impl HandlerContext {
|
||||
#[cfg(test)]
|
||||
pub fn new_for_test(region_server: RegionServer, kv_backend: KvBackendRef) -> Self {
|
||||
Self {
|
||||
mailbox_message_id: 0,
|
||||
region_server,
|
||||
downgrade_tasks: TaskTracker::new(),
|
||||
flush_tasks: TaskTracker::new(),
|
||||
@@ -287,7 +285,6 @@ impl HeartbeatResponseHandler for RegionHeartbeatResponseHandler {
|
||||
let mailbox = ctx.mailbox.clone();
|
||||
if let Some(handler) = self.build_handler(&instruction)? {
|
||||
let context = HandlerContext {
|
||||
mailbox_message_id: meta.id,
|
||||
region_server: self.region_server.clone(),
|
||||
downgrade_tasks: self.downgrade_tasks.clone(),
|
||||
flush_tasks: self.flush_tasks.clone(),
|
||||
@@ -300,6 +297,16 @@ impl HeartbeatResponseHandler for RegionHeartbeatResponseHandler {
|
||||
to = %meta.to,
|
||||
handler = %handler.as_ref_str(),
|
||||
));
|
||||
// Apply a one-way Release before dispatching the next Pause on this stream.
|
||||
if matches!(
|
||||
&instruction,
|
||||
Instruction::MetadataCompaction(
|
||||
common_meta::instruction::MetadataCompaction::Release { .. }
|
||||
)
|
||||
) {
|
||||
handler.handle(&context, instruction).trace(span).await;
|
||||
return Ok(HandleControl::Continue);
|
||||
}
|
||||
let _handle = common_runtime::spawn_global(async move {
|
||||
let reply = handler.handle(&context, instruction).trace(span).await;
|
||||
if let Some(reply) = reply
|
||||
|
||||
@@ -33,7 +33,6 @@ pub struct MetadataCompactionHandler {
|
||||
}
|
||||
|
||||
struct PauseLease {
|
||||
pause_id: u64,
|
||||
deadline: Instant,
|
||||
guard: Arc<OnceCell<CompactionGuard>>,
|
||||
cancel_expiry: oneshot::Sender<()>,
|
||||
@@ -43,23 +42,17 @@ impl MetadataCompactionHandler {
|
||||
fn lease(
|
||||
&self,
|
||||
region_id: RegionId,
|
||||
pause_id: u64,
|
||||
deadline: Instant,
|
||||
) -> (Arc<OnceCell<CompactionGuard>>, Instant) {
|
||||
let mut leases = self.leases.lock().unwrap();
|
||||
let lease = leases.entry(region_id).or_insert_with(|| PauseLease {
|
||||
pause_id,
|
||||
deadline,
|
||||
guard: Arc::new(OnceCell::new()),
|
||||
cancel_expiry: self.expire_at(region_id, deadline),
|
||||
});
|
||||
if pause_id > lease.pause_id {
|
||||
// Only the latest Pause can release the shared guard.
|
||||
lease.pause_id = pause_id;
|
||||
if deadline > lease.deadline {
|
||||
lease.deadline = deadline;
|
||||
lease.cancel_expiry = self.expire_at(region_id, deadline);
|
||||
}
|
||||
if deadline > lease.deadline {
|
||||
lease.deadline = deadline;
|
||||
lease.cancel_expiry = self.expire_at(region_id, deadline);
|
||||
}
|
||||
(lease.guard.clone(), lease.deadline)
|
||||
}
|
||||
@@ -87,7 +80,6 @@ impl MetadataCompactionHandler {
|
||||
&self,
|
||||
ctx: &HandlerContext,
|
||||
region_id: RegionId,
|
||||
pause_id: u64,
|
||||
remaining: std::time::Duration,
|
||||
) -> InstructionResult<()> {
|
||||
let deadline = Instant::now().checked_add(remaining).ok_or_else(|| {
|
||||
@@ -115,7 +107,7 @@ impl MetadataCompactionHandler {
|
||||
"Source region {region_id} is not writable"
|
||||
)));
|
||||
}
|
||||
let (lease, deadline) = self.lease(region_id, pause_id, deadline);
|
||||
let (lease, deadline) = self.lease(region_id, deadline);
|
||||
tokio::time::timeout_at(
|
||||
deadline,
|
||||
lease.get_or_try_init(|| metric.pause_metadata_compaction(region_id)),
|
||||
@@ -132,7 +124,7 @@ impl MetadataCompactionHandler {
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get(®ion_id)
|
||||
.is_none_or(|lease| lease.pause_id != pause_id)
|
||||
.is_none_or(|current| !Arc::ptr_eq(¤t.guard, &lease))
|
||||
|| Instant::now() >= deadline
|
||||
|| ctx.region_server.is_region_leader(region_id) != Some(true)
|
||||
{
|
||||
@@ -158,23 +150,14 @@ impl InstructionHandler for MetadataCompactionHandler {
|
||||
region_id,
|
||||
remaining,
|
||||
} => {
|
||||
let result = self
|
||||
.pause(ctx, region_id, ctx.mailbox_message_id, remaining)
|
||||
.await;
|
||||
let result = self.pause(ctx, region_id, remaining).await;
|
||||
Some(InstructionReply::MetadataCompaction(SimpleReply {
|
||||
result: result.is_ok(),
|
||||
error: result.err(),
|
||||
}))
|
||||
}
|
||||
MetadataCompaction::Release {
|
||||
region_id,
|
||||
pause_id,
|
||||
} => {
|
||||
if let Entry::Occupied(entry) = self.leases.lock().unwrap().entry(region_id)
|
||||
&& entry.get().pause_id == pause_id
|
||||
{
|
||||
entry.remove();
|
||||
}
|
||||
MetadataCompaction::Release { region_id } => {
|
||||
self.leases.lock().unwrap().remove(®ion_id);
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -219,37 +202,21 @@ mod tests {
|
||||
let ctx = HandlerContext::new_for_test(server, Arc::new(MemoryKvBackend::new()));
|
||||
let handler = MetadataCompactionHandler::default();
|
||||
handler
|
||||
.pause(&ctx, region_id, 1, Duration::from_secs(30))
|
||||
.pause(&ctx, region_id, Duration::from_secs(30))
|
||||
.await
|
||||
.unwrap();
|
||||
let first_deadline = handler.leases.lock().unwrap()[®ion_id].deadline;
|
||||
handler
|
||||
.pause(&ctx, region_id, 2, Duration::from_secs(60))
|
||||
.pause(&ctx, region_id, Duration::from_secs(60))
|
||||
.await
|
||||
.unwrap();
|
||||
let renewed = handler.leases.lock().unwrap()[®ion_id].deadline;
|
||||
assert!(renewed > first_deadline);
|
||||
handler
|
||||
.pause(&ctx, region_id, 2, Duration::from_secs(120))
|
||||
.pause(&ctx, region_id, Duration::from_secs(1))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(handler.leases.lock().unwrap()[®ion_id].deadline, renewed);
|
||||
handler
|
||||
.pause(&ctx, region_id, 3, Duration::from_secs(1))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(handler.leases.lock().unwrap()[®ion_id].deadline, renewed);
|
||||
for _ in 0..2 {
|
||||
handler
|
||||
.handle(
|
||||
&ctx,
|
||||
MetadataCompaction::Release {
|
||||
region_id,
|
||||
pause_id: 2,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let meta = RegionId::with_group_and_seq(32, METRIC_METADATA_REGION_GROUP, 1);
|
||||
let compact = || mito.handle_request(meta, RegionRequest::Compact(Default::default()));
|
||||
let err = compact().await.unwrap_err();
|
||||
@@ -266,17 +233,11 @@ mod tests {
|
||||
.await
|
||||
.unwrap();
|
||||
handler
|
||||
.handle(
|
||||
&ctx,
|
||||
MetadataCompaction::Release {
|
||||
region_id,
|
||||
pause_id: 3,
|
||||
},
|
||||
)
|
||||
.handle(&ctx, MetadataCompaction::Release { region_id })
|
||||
.await;
|
||||
compact().await.unwrap();
|
||||
handler
|
||||
.pause(&ctx, region_id, 4, Duration::from_millis(200))
|
||||
.pause(&ctx, region_id, Duration::from_millis(200))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(compact().await.is_err());
|
||||
|
||||
@@ -106,7 +106,7 @@ impl SyncRegion {
|
||||
region_id,
|
||||
remaining,
|
||||
});
|
||||
let (pause_id, reply) =
|
||||
let reply =
|
||||
Self::request(&ctx.mailbox, &ctx.server_addr, &source, pause, remaining).await?;
|
||||
let InstructionReply::MetadataCompaction(reply) = reply else {
|
||||
return error::UnexpectedInstructionReplySnafu {
|
||||
@@ -139,10 +139,8 @@ impl SyncRegion {
|
||||
let mut all_replied = true;
|
||||
let result = self.sync_regions(ctx, &mut all_replied).await;
|
||||
if all_replied {
|
||||
let release = Instruction::MetadataCompaction(MetadataCompaction::Release {
|
||||
region_id,
|
||||
pause_id,
|
||||
});
|
||||
let release =
|
||||
Instruction::MetadataCompaction(MetadataCompaction::Release { region_id });
|
||||
if let Err(err) = Self::release_source(ctx, &source, release).await {
|
||||
warn!(err; "Failed to release source metadata compaction for {region_id}");
|
||||
}
|
||||
@@ -238,14 +236,14 @@ impl SyncRegion {
|
||||
.into_iter()
|
||||
.zip(&peers)
|
||||
.map(|(reply, peer)| match reply {
|
||||
Ok((_, InstructionReply::SyncRegions(SyncRegionsReply { replies })))
|
||||
Ok(InstructionReply::SyncRegions(SyncRegionsReply { replies }))
|
||||
if Self::received_all(&instructions[*peer], &replies) =>
|
||||
{
|
||||
replies
|
||||
.iter()
|
||||
.try_for_each(|reply| Self::handle_sync_region_reply(reply, &now, peer))
|
||||
}
|
||||
Ok((_, reply)) => {
|
||||
Ok(reply) => {
|
||||
*all_replied = false;
|
||||
error::UnexpectedInstructionReplySnafu {
|
||||
mailbox_message: reply.to_string(),
|
||||
@@ -334,7 +332,7 @@ impl SyncRegion {
|
||||
peer: &Peer,
|
||||
instruction: Instruction,
|
||||
timeout: Duration,
|
||||
) -> Result<(u64, InstructionReply)> {
|
||||
) -> Result<InstructionReply> {
|
||||
let ch = Channel::Datanode(peer.id);
|
||||
let message = Self::message(server_addr, peer, &instruction)?;
|
||||
let now = std::time::Instant::now();
|
||||
@@ -356,7 +354,7 @@ impl SyncRegion {
|
||||
};
|
||||
|
||||
match receiver.await {
|
||||
Ok(msg) => Ok((msg.id, HeartbeatMailbox::json_reply(&msg)?)),
|
||||
Ok(msg) => HeartbeatMailbox::json_reply(&msg),
|
||||
Err(error::Error::MailboxChannelClosed { .. }) => error::RetryLaterSnafu {
|
||||
reason: format!(
|
||||
"Mailbox closed when sending sync region to datanode {:?}, elapsed: {:?}",
|
||||
@@ -552,7 +550,6 @@ mod tests {
|
||||
assert_eq!(region_id, RegionId::new(1024, 1));
|
||||
assert!(remaining > Duration::ZERO && remaining <= budget);
|
||||
assert!(target_rx.try_recv().is_err());
|
||||
let pause_id = msg.id;
|
||||
let mut ack = msg.clone();
|
||||
ack.payload = Some(api::v1::meta::mailbox_message::Payload::Json(
|
||||
serde_json::to_string(&InstructionReply::MetadataCompaction(SimpleReply {
|
||||
@@ -563,7 +560,7 @@ mod tests {
|
||||
));
|
||||
mailbox.on_recv(msg.id, Ok(ack)).await.unwrap();
|
||||
if outcome == "pause_rejected" {
|
||||
return (region_id, pause_id);
|
||||
return region_id;
|
||||
}
|
||||
let msg = target_rx
|
||||
.recv()
|
||||
@@ -589,14 +586,13 @@ mod tests {
|
||||
_ => Ok(new_sync_region_reply(msg.id, target, true, true, None)),
|
||||
};
|
||||
mailbox.on_recv(msg.id, reply).await.unwrap();
|
||||
(region_id, pause_id)
|
||||
region_id
|
||||
};
|
||||
let ((region_id, pause_id), result) =
|
||||
tokio::time::timeout(Duration::from_secs(5), async {
|
||||
tokio::join!(peers, sync.sync_with_paused_source(&mut ctx))
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let (region_id, result) = tokio::time::timeout(Duration::from_secs(5), async {
|
||||
tokio::join!(peers, sync.sync_with_paused_source(&mut ctx))
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result.is_ok(), outcome == "success");
|
||||
if outcome == "pause_rejected" {
|
||||
assert!(target_rx.try_recv().is_err());
|
||||
@@ -612,10 +608,7 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
HeartbeatMailbox::json_instruction(&msg).unwrap(),
|
||||
Instruction::MetadataCompaction(MetadataCompaction::Release {
|
||||
region_id,
|
||||
pause_id
|
||||
})
|
||||
Instruction::MetadataCompaction(MetadataCompaction::Release { region_id })
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user