Search in sources :

Example 46 with Releasable

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));
            }
        }
    }
}
Also used : Releasable(org.opensearch.common.lease.Releasable) BytesRef(org.apache.lucene.util.BytesRef)

Example 47 with Releasable

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()))));
    }
}
Also used : ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) IndexShard(org.opensearch.index.shard.IndexShard) ArrayList(java.util.ArrayList) IndexRequest(org.opensearch.action.index.IndexRequest) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) Releasable(org.opensearch.common.lease.Releasable) DeleteRequest(org.opensearch.action.delete.DeleteRequest)

Example 48 with Releasable

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));
}
Also used : PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Releasable(org.opensearch.common.lease.Releasable)

Example 49 with Releasable

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")));
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Releasable(org.opensearch.common.lease.Releasable) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) TimeoutException(java.util.concurrent.TimeoutException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException)

Example 50 with Releasable

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));
}
Also used : PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Releasable(org.opensearch.common.lease.Releasable) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Releasable (org.opensearch.common.lease.Releasable)161 ShardId (org.opensearch.index.shard.ShardId)50 IOException (java.io.IOException)45 CountDownLatch (java.util.concurrent.CountDownLatch)36 Settings (org.opensearch.common.settings.Settings)35 IndexingPressurePerShardStats (org.opensearch.index.stats.IndexingPressurePerShardStats)32 ExecutionException (java.util.concurrent.ExecutionException)30 ArrayList (java.util.ArrayList)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)27 OpenSearchException (org.opensearch.OpenSearchException)25 ThreadPool (org.opensearch.threadpool.ThreadPool)25 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)24 ActionListener (org.opensearch.action.ActionListener)23 IndexRequest (org.opensearch.action.index.IndexRequest)22 ShardRouting (org.opensearch.cluster.routing.ShardRouting)22 IndexingPressureStats (org.opensearch.index.stats.IndexingPressureStats)22 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)21 List (java.util.List)20 Translog (org.opensearch.index.translog.Translog)19