fix: clarify shadow refresh recovery contract

This commit is contained in:
zgw
2026-09-03 11:39:31 +08:00
parent c6a8b4f3b2
commit 5ed39162cc
3 changed files with 161 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package com.ruoyi.algorithm.shadow;
import java.time.Duration;
import java.time.Instant;
import java.time.Clock;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -17,6 +18,7 @@ public final class ShadowSnapshotCoordinator {
private final String configuredWarehouseId;
private final Duration snapshotMaxAge;
private final Clock clock;
private final AtomicReference<State> state = new AtomicReference<>(
new State(Phase.INITIALIZING, 0L, 0, null, null, null, null));
@@ -25,8 +27,13 @@ public final class ShadowSnapshotCoordinator {
}
public ShadowSnapshotCoordinator(String configuredWarehouseId, Duration snapshotMaxAge) {
this(configuredWarehouseId, snapshotMaxAge, Clock.systemUTC());
}
public ShadowSnapshotCoordinator(String configuredWarehouseId, Duration snapshotMaxAge, Clock clock) {
this.configuredWarehouseId = Objects.requireNonNull(configuredWarehouseId, "configuredWarehouseId").trim();
this.snapshotMaxAge = Objects.requireNonNull(snapshotMaxAge, "snapshotMaxAge");
this.clock = Objects.requireNonNull(clock, "clock");
if (snapshotMaxAge.isNegative() || snapshotMaxAge.isZero()) {
throw new IllegalArgumentException("snapshotMaxAge must be positive");
}
@@ -85,7 +92,7 @@ public final class ShadowSnapshotCoordinator {
return;
}
int nextActiveCount = current.activeLegacyCount() - 1;
Instant nextQuietSince = nextActiveCount == 0 ? Instant.now() : current.quietSince();
Instant nextQuietSince = nextActiveCount == 0 ? clock.instant() : current.quietSince();
State next = new State(current.phase(), current.epoch(), nextActiveCount, current.published(),
current.leasedEpoch(), nextQuietSince, current.lastFailure());
if (state.compareAndSet(current, next)) {