Skip to content

Commit 112d7c3

Browse files
committed
log failure at an interval
1 parent 619bf84 commit 112d7c3

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

src/main/java/com/uid2/operator/vertx/OperatorShutdownHandler.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
public class OperatorShutdownHandler {
1717
private static final Logger LOGGER = LoggerFactory.getLogger(OperatorShutdownHandler.class);
1818
private static final int SALT_FAILURE_LOG_INTERVAL_MINUTES = 10;
19+
private static final int KEYSET_KEY_FAILURE_LOG_INTERVAL_MINUTES = 10;
1920
private final Duration attestShutdownWaitTime;
2021
private final Duration saltShutdownWaitTime;
2122
private final Duration keysetKeyShutdownWaitTime;
2223
private final AtomicReference<Instant> attestFailureStartTime = new AtomicReference<>(null);
2324
private final AtomicReference<Instant> saltFailureStartTime = new AtomicReference<>(null);
2425
private final AtomicReference<Instant> keysetKeyFailureStartTime = new AtomicReference<>(null);
2526
private final AtomicReference<Instant> lastSaltFailureLogTime = new AtomicReference<>(null);
27+
private final AtomicReference<Instant> lastKeysetKeyFailureLogTime = new AtomicReference<>(null);
2628
private final Clock clock;
2729
private final ShutdownService shutdownService;
2830

@@ -61,18 +63,17 @@ public void logSaltFailureAtInterval() {
6163
public void handleKeysetKeyRefreshResponse(Boolean success) {
6264
if (success) {
6365
keysetKeyFailureStartTime.set(null);
66+
lastKeysetKeyFailureLogTime.set(null);
6467
LOGGER.debug("keyset keys sync successful");
6568
} else {
6669
Instant t = keysetKeyFailureStartTime.get();
6770
if (t == null) {
6871
keysetKeyFailureStartTime.set(clock.instant());
72+
lastKeysetKeyFailureLogTime.set(clock.instant());
6973
LOGGER.warn("keyset keys sync started failing. shutdown timer started");
7074
} else {
75+
logKeysetKeyFailureProgressAtInterval(t);
7176
Duration elapsed = Duration.between(t, clock.instant());
72-
LOGGER.debug("keyset keys sync still failing - elapsed time: {}d {}h {}m",
73-
elapsed.toDays(),
74-
elapsed.toHoursPart(),
75-
elapsed.toMinutesPart());
7677
if (elapsed.compareTo(this.keysetKeyShutdownWaitTime) > 0) {
7778
LOGGER.error("keyset keys have been failing to sync for too long. shutting down operator");
7879
this.shutdownService.Shutdown(1);
@@ -81,6 +82,18 @@ public void handleKeysetKeyRefreshResponse(Boolean success) {
8182
}
8283
}
8384

85+
private void logKeysetKeyFailureProgressAtInterval(Instant failureStartTime) {
86+
Instant lastLogTime = lastKeysetKeyFailureLogTime.get();
87+
if (lastLogTime == null || clock.instant().isAfter(lastLogTime.plus(KEYSET_KEY_FAILURE_LOG_INTERVAL_MINUTES, ChronoUnit.MINUTES))) {
88+
Duration elapsed = Duration.between(failureStartTime, clock.instant());
89+
LOGGER.warn("keyset keys sync still failing - elapsed time: {}d {}h {}m",
90+
elapsed.toDays(),
91+
elapsed.toHoursPart(),
92+
elapsed.toMinutesPart());
93+
lastKeysetKeyFailureLogTime.set(clock.instant());
94+
}
95+
}
96+
8497
public void handleAttestResponse(Pair<AttestationResponseCode, String> response) {
8598
if (response.left() == AttestationResponseCode.AttestationFailure) {
8699
LOGGER.error("core attestation failed with AttestationFailure, shutting down operator, core response: {}", response.right());

src/test/java/com/uid2/operator/OperatorShutdownHandlerTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,29 @@ void keysetKeyNoShutdownWhenAlwaysSuccessful(VertxTestContext testContext) {
211211
verify(shutdownService, never()).Shutdown(anyInt());
212212
testContext.completeNow();
213213
}
214+
215+
@Test
216+
void keysetKeyLogProgressAtInterval(VertxTestContext testContext) {
217+
ListAppender<ILoggingEvent> logWatcher = new ListAppender<>();
218+
logWatcher.start();
219+
((Logger) LoggerFactory.getLogger(OperatorShutdownHandler.class)).addAppender(logWatcher);
220+
221+
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
222+
long warnLogCount1 = logWatcher.list.stream().filter(log ->
223+
log.getFormattedMessage().contains("keyset keys sync still failing")).count();
224+
225+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(30, ChronoUnit.MINUTES));
226+
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
227+
long warnLogCount2 = logWatcher.list.stream().filter(log ->
228+
log.getFormattedMessage().contains("keyset keys sync still failing")).count();
229+
Assertions.assertEquals(warnLogCount1, warnLogCount2);
230+
231+
when(clock.instant()).thenAnswer(i -> Instant.now().plus(61, ChronoUnit.MINUTES));
232+
this.operatorShutdownHandler.handleKeysetKeyRefreshResponse(false);
233+
long warnLogCount3 = logWatcher.list.stream().filter(log ->
234+
log.getFormattedMessage().contains("keyset keys sync still failing")).count();
235+
Assertions.assertTrue(warnLogCount3 > warnLogCount2);
236+
237+
testContext.completeNow();
238+
}
214239
}

0 commit comments

Comments
 (0)