use of org.opensearch.threadpool.Scheduler in project OpenSearch by opensearch-project.
the class GlobalCheckpointListenersTests method testFailingListenerAfterTimeout.
public void testFailingListenerAfterTimeout() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Logger mockLogger = mock(Logger.class);
doAnswer(invocationOnMock -> {
latch.countDown();
return null;
}).when(mockLogger).warn(any(String.class), any(RuntimeException.class));
final GlobalCheckpointListeners globalCheckpointListeners = new GlobalCheckpointListeners(shardId, scheduler, mockLogger);
final TimeValue timeout = TimeValue.timeValueMillis(randomIntBetween(1, 50));
globalCheckpointListeners.add(NO_OPS_PERFORMED, maybeMultipleInvocationProtectingListener((g, e) -> {
throw new RuntimeException("failure");
}), timeout);
latch.await();
final ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<RuntimeException> t = ArgumentCaptor.forClass(RuntimeException.class);
verify(mockLogger).warn(message.capture(), t.capture());
assertThat(message.getValue(), equalTo("error notifying global checkpoint listener of timeout"));
assertNotNull(t.getValue());
assertThat(t.getValue(), instanceOf(RuntimeException.class));
assertThat(t.getValue().getMessage(), equalTo("failure"));
}
use of org.opensearch.threadpool.Scheduler in project OpenSearch by opensearch-project.
the class GlobalCheckpointListenersTests method testFailingListenerReadyToBeNotified.
public void testFailingListenerReadyToBeNotified() {
final Logger mockLogger = mock(Logger.class);
final GlobalCheckpointListeners globalCheckpointListeners = new GlobalCheckpointListeners(shardId, scheduler, mockLogger);
final long globalCheckpoint = randomLongBetween(NO_OPS_PERFORMED + 1, Long.MAX_VALUE);
globalCheckpointListeners.globalCheckpointUpdated(globalCheckpoint);
final int numberOfListeners = randomIntBetween(0, 16);
final long[] globalCheckpoints = new long[numberOfListeners];
for (int i = 0; i < numberOfListeners; i++) {
final int index = i;
final boolean failure = randomBoolean();
globalCheckpointListeners.add(randomLongBetween(NO_OPS_PERFORMED, globalCheckpoint - 1), maybeMultipleInvocationProtectingListener((g, e) -> {
if (failure) {
globalCheckpoints[index] = Long.MIN_VALUE;
throw new RuntimeException("failure");
} else {
globalCheckpoints[index] = globalCheckpoint;
}
}), null);
// the listener should be notified immediately
if (failure) {
assertThat(globalCheckpoints[i], equalTo(Long.MIN_VALUE));
final ArgumentCaptor<ParameterizedMessage> message = ArgumentCaptor.forClass(ParameterizedMessage.class);
final ArgumentCaptor<RuntimeException> t = ArgumentCaptor.forClass(RuntimeException.class);
verify(mockLogger).warn(message.capture(), t.capture());
reset(mockLogger);
assertThat(message.getValue().getFormat(), equalTo("error notifying global checkpoint listener of updated global checkpoint [{}]"));
assertNotNull(message.getValue().getParameters());
assertThat(message.getValue().getParameters().length, equalTo(1));
assertThat(message.getValue().getParameters()[0], equalTo(globalCheckpoint));
assertNotNull(t.getValue());
assertThat(t.getValue().getMessage(), equalTo("failure"));
} else {
assertThat(globalCheckpoints[i], equalTo(globalCheckpoint));
}
}
}
use of org.opensearch.threadpool.Scheduler in project OpenSearch by opensearch-project.
the class GlobalCheckpointListenersTests method testNotificationOnClosedUsesExecutor.
public void testNotificationOnClosedUsesExecutor() throws IOException {
final AtomicInteger count = new AtomicInteger();
final Executor executor = command -> {
count.incrementAndGet();
command.run();
};
final GlobalCheckpointListeners globalCheckpointListeners = new GlobalCheckpointListeners(shardId, scheduler, logger);
globalCheckpointListeners.close();
final AtomicInteger notified = new AtomicInteger();
final int numberOfListeners = randomIntBetween(0, 16);
for (int i = 0; i < numberOfListeners; i++) {
globalCheckpointListeners.add(NO_OPS_PERFORMED, maybeMultipleInvocationProtectingListener(new TestGlobalCheckpointListener() {
@Override
public Executor executor() {
return executor;
}
@Override
public void accept(final long g, final Exception e) {
notified.incrementAndGet();
assertThat(g, equalTo(UNASSIGNED_SEQ_NO));
assertNotNull(e);
assertThat(e, instanceOf(IndexShardClosedException.class));
assertThat(((IndexShardClosedException) e).getShardId(), equalTo(shardId));
}
}), null);
}
assertThat(notified.get(), equalTo(numberOfListeners));
assertThat(count.get(), equalTo(numberOfListeners));
}
use of org.opensearch.threadpool.Scheduler in project OpenSearch by opensearch-project.
the class GlobalCheckpointListenersTests method testTimeout.
public void testTimeout() throws InterruptedException {
final Logger mockLogger = mock(Logger.class);
final GlobalCheckpointListeners globalCheckpointListeners = new GlobalCheckpointListeners(shardId, scheduler, mockLogger);
final TimeValue timeout = TimeValue.timeValueMillis(randomIntBetween(1, 50));
final AtomicBoolean notified = new AtomicBoolean();
final CountDownLatch latch = new CountDownLatch(1);
globalCheckpointListeners.add(NO_OPS_PERFORMED, maybeMultipleInvocationProtectingListener((g, e) -> {
try {
notified.set(true);
assertThat(g, equalTo(UNASSIGNED_SEQ_NO));
assertThat(e, instanceOf(TimeoutException.class));
assertThat(e, hasToString(containsString(timeout.getStringRep())));
final ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<TimeoutException> t = ArgumentCaptor.forClass(TimeoutException.class);
verify(mockLogger).trace(message.capture(), t.capture());
assertThat(message.getValue(), equalTo("global checkpoint listener timed out"));
assertThat(t.getValue(), hasToString(containsString(timeout.getStringRep())));
} catch (Exception caught) {
fail(e.getMessage());
} finally {
latch.countDown();
}
}), timeout);
latch.await();
assertTrue(notified.get());
}
use of org.opensearch.threadpool.Scheduler in project OpenSearch by opensearch-project.
the class GlobalCheckpointListenersTests method testFailingListenerOnClose.
public void testFailingListenerOnClose() throws IOException {
final Logger mockLogger = mock(Logger.class);
final GlobalCheckpointListeners globalCheckpointListeners = new GlobalCheckpointListeners(shardId, scheduler, mockLogger);
globalCheckpointListeners.globalCheckpointUpdated(NO_OPS_PERFORMED);
final int numberOfListeners = randomIntBetween(0, 16);
final boolean[] failures = new boolean[numberOfListeners];
final Exception[] exceptions = new Exception[numberOfListeners];
for (int i = 0; i < numberOfListeners; i++) {
final int index = i;
final boolean failure = randomBoolean();
failures[index] = failure;
globalCheckpointListeners.add(0, maybeMultipleInvocationProtectingListener((g, e) -> {
if (failure) {
throw new RuntimeException("failure");
} else {
exceptions[index] = e;
}
}), null);
}
globalCheckpointListeners.close();
for (int i = 0; i < numberOfListeners; i++) {
if (failures[i]) {
assertNull(exceptions[i]);
} else {
assertNotNull(exceptions[i]);
assertThat(exceptions[i], instanceOf(IndexShardClosedException.class));
assertThat(((IndexShardClosedException) exceptions[i]).getShardId(), equalTo(shardId));
}
}
int failureCount = 0;
for (int i = 0; i < numberOfListeners; i++) {
if (failures[i]) {
failureCount++;
}
}
if (failureCount > 0) {
final ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<RuntimeException> t = ArgumentCaptor.forClass(RuntimeException.class);
verify(mockLogger, times(failureCount)).warn(message.capture(), t.capture());
assertThat(message.getValue(), equalTo("error notifying global checkpoint listener of closed shard"));
assertNotNull(t.getValue());
assertThat(t.getValue().getMessage(), equalTo("failure"));
}
}
Aggregations