use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.
the class LiveVersionMapTests method testRandomlyIndexDeleteAndRefresh.
public void testRandomlyIndexDeleteAndRefresh() throws Exception {
final LiveVersionMap versionMap = new LiveVersionMap();
final BytesRef uid = uid("1");
final long versions = between(10, 1000);
VersionValue latestVersion = null;
for (long i = 0; i < versions; i++) {
if (randomBoolean()) {
versionMap.beforeRefresh();
versionMap.afterRefresh(randomBoolean());
}
if (randomBoolean()) {
versionMap.enforceSafeAccess();
}
try (Releasable ignore = versionMap.acquireLock(uid)) {
if (randomBoolean()) {
latestVersion = new DeleteVersionValue(randomNonNegativeLong(), randomLong(), randomLong(), randomLong());
versionMap.putDeleteUnderLock(uid, (DeleteVersionValue) latestVersion);
assertThat(versionMap.getUnderLock(uid), equalTo(latestVersion));
} else if (randomBoolean()) {
latestVersion = new IndexVersionValue(randomTranslogLocation(), randomNonNegativeLong(), randomLong(), randomLong());
versionMap.maybePutIndexUnderLock(uid, (IndexVersionValue) latestVersion);
if (versionMap.isSafeAccessRequired()) {
assertThat(versionMap.getUnderLock(uid), equalTo(latestVersion));
} else {
assertThat(versionMap.getUnderLock(uid), nullValue());
}
}
if (versionMap.getUnderLock(uid) != null) {
assertThat(versionMap.getUnderLock(uid), equalTo(latestVersion));
}
}
}
}
use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.
the class RecoveryDuringReplicationTests method testAddNewReplicas.
public void testAddNewReplicas() throws Exception {
AtomicBoolean stopped = new AtomicBoolean();
List<Thread> threads = new ArrayList<>();
Runnable stopIndexing = () -> {
try {
stopped.set(true);
for (Thread thread : threads) {
thread.join();
}
} catch (Exception e) {
throw new AssertionError(e);
}
};
try (ReplicationGroup shards = createGroup(between(0, 1));
Releasable ignored = stopIndexing::run) {
shards.startAll();
boolean appendOnly = randomBoolean();
AtomicInteger docId = new AtomicInteger();
int numThreads = between(1, 3);
for (int i = 0; i < numThreads; i++) {
Thread thread = new Thread(() -> {
while (stopped.get() == false) {
try {
int nextId = docId.incrementAndGet();
if (appendOnly) {
String id = randomBoolean() ? Integer.toString(nextId) : null;
shards.index(new IndexRequest(index.getName()).id(id).source("{}", XContentType.JSON));
} else if (frequently()) {
String id = Integer.toString(frequently() ? nextId : between(0, nextId));
shards.index(new IndexRequest(index.getName()).id(id).source("{}", XContentType.JSON));
} else {
String id = Integer.toString(between(0, nextId));
shards.delete(new DeleteRequest(index.getName()).id(id));
}
if (randomInt(100) < 10) {
shards.getPrimary().flush(new FlushRequest());
}
if (randomInt(100) < 5) {
shards.getPrimary().forceMerge(new ForceMergeRequest().flush(randomBoolean()).maxNumSegments(1));
}
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
});
threads.add(thread);
thread.start();
}
// we flush quite often
assertBusy(() -> assertThat(docId.get(), greaterThanOrEqualTo(50)), 60, TimeUnit.SECONDS);
shards.getPrimary().sync();
IndexShard newReplica = shards.addReplica();
shards.recoverReplica(newReplica);
// we flush quite often
assertBusy(() -> assertThat(docId.get(), greaterThanOrEqualTo(100)), 60, TimeUnit.SECONDS);
stopIndexing.run();
assertBusy(() -> assertThat(getDocIdAndSeqNos(newReplica), equalTo(getDocIdAndSeqNos(shards.getPrimary()))));
}
}
use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.
the class IndexShardOperationPermitsTests method testActiveOperationsCount.
public void testActiveOperationsCount() throws ExecutionException, InterruptedException {
PlainActionFuture<Releasable> future1 = new PlainActionFuture<>();
permits.acquire(future1, ThreadPool.Names.GENERIC, true, "");
assertTrue(future1.isDone());
assertThat(permits.getActiveOperationsCount(), equalTo(1));
PlainActionFuture<Releasable> future2 = new PlainActionFuture<>();
permits.acquire(future2, ThreadPool.Names.GENERIC, true, "");
assertTrue(future2.isDone());
assertThat(permits.getActiveOperationsCount(), equalTo(2));
future1.get().close();
assertThat(permits.getActiveOperationsCount(), equalTo(1));
// check idempotence
future1.get().close();
assertThat(permits.getActiveOperationsCount(), equalTo(1));
future2.get().close();
assertThat(permits.getActiveOperationsCount(), equalTo(0));
try (Releasable ignored = blockAndWait()) {
assertThat(permits.getActiveOperationsCount(), equalTo(IndexShard.OPERATIONS_BLOCKED));
}
PlainActionFuture<Releasable> future3 = new PlainActionFuture<>();
permits.acquire(future3, ThreadPool.Names.GENERIC, true, "");
assertTrue(future3.isDone());
assertThat(permits.getActiveOperationsCount(), equalTo(1));
future3.get().close();
assertThat(permits.getActiveOperationsCount(), equalTo(0));
}
use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.
the class IndexShardOperationPermitsTests method testAsyncBlockOperationsOnFailure.
public void testAsyncBlockOperationsOnFailure() throws InterruptedException {
final AtomicReference<Exception> reference = new AtomicReference<>();
final CountDownLatch onFailureLatch = new CountDownLatch(1);
permits.asyncBlockOperations(new ActionListener<Releasable>() {
@Override
public void onResponse(Releasable releasable) {
try (Releasable ignored = releasable) {
throw new RuntimeException("simulated");
}
}
@Override
public void onFailure(final Exception e) {
reference.set(e);
onFailureLatch.countDown();
}
}, 10, TimeUnit.MINUTES);
onFailureLatch.await();
assertThat(reference.get(), instanceOf(RuntimeException.class));
assertThat(reference.get(), hasToString(containsString("simulated")));
}
use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.
the class IndexShardOperationPermitsTests method testOperationsIfClosed.
public void testOperationsIfClosed() {
PlainActionFuture<Releasable> future = new PlainActionFuture<>();
permits.close();
permits.acquire(future, ThreadPool.Names.GENERIC, true, "");
ExecutionException exception = expectThrows(ExecutionException.class, future::get);
assertThat(exception.getCause(), instanceOf(IndexShardClosedException.class));
}
Aggregations