Search in sources :

Example 26 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project crate by crate.

the class InternalEngine method innerNoOp.

private NoOpResult innerNoOp(final NoOp noOp) throws IOException {
    assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
    assert noOp.seqNo() > SequenceNumbers.NO_OPS_PERFORMED;
    final long seqNo = noOp.seqNo();
    try (Releasable ignored = noOpKeyedLock.acquire(seqNo)) {
        NoOpResult noOpResult;
        final Optional<Exception> preFlightError = preFlightCheckForNoOp(noOp);
        if (preFlightError.isPresent()) {
            noOpResult = new NoOpResult(SequenceNumbers.UNASSIGNED_PRIMARY_TERM, SequenceNumbers.UNASSIGNED_SEQ_NO, preFlightError.get());
        } else {
            markSeqNoAsSeen(noOp.seqNo());
            if (softDeleteEnabled && hasBeenProcessedBefore(noOp) == false) {
                try {
                    final ParsedDocument tombstone = engineConfig.getTombstoneDocSupplier().newNoopTombstoneDoc(noOp.reason());
                    tombstone.updateSeqID(noOp.seqNo(), noOp.primaryTerm());
                    // A noop tombstone does not require a _version but it's added to have a fully dense docvalues for the version field.
                    // 1L is selected to optimize the compression because it might probably be the most common value in version field.
                    tombstone.version().setLongValue(1L);
                    assert tombstone.docs().size() == 1 : "Tombstone should have a single doc [" + tombstone + "]";
                    final ParseContext.Document doc = tombstone.docs().get(0);
                    assert doc.getField(SeqNoFieldMapper.TOMBSTONE_NAME) != null : "Noop tombstone document but _tombstone field is not set [" + doc + " ]";
                    doc.add(softDeletesField);
                    indexWriter.addDocument(doc);
                } catch (final Exception ex) {
                    /*
                         * Document level failures when adding a no-op are unexpected, we likely hit something fatal such as the Lucene
                         * index being corrupt, or the Lucene document limit. We have already issued a sequence number here so this is
                         * fatal, fail the engine.
                         */
                    if (ex instanceof AlreadyClosedException == false && indexWriter.getTragicException() == null) {
                        failEngine("no-op origin[" + noOp.origin() + "] seq#[" + noOp.seqNo() + "] failed at document level", ex);
                    }
                    throw ex;
                }
            }
            noOpResult = new NoOpResult(noOp.primaryTerm(), noOp.seqNo());
            if (noOp.origin().isFromTranslog() == false && noOpResult.getResultType() == Result.Type.SUCCESS) {
                final Translog.Location location = translog.add(new Translog.NoOp(noOp.seqNo(), noOp.primaryTerm(), noOp.reason()));
                noOpResult.setTranslogLocation(location);
            }
        }
        localCheckpointTracker.markSeqNoAsProcessed(noOpResult.getSeqNo());
        if (noOpResult.getTranslogLocation() == null) {
            // the op is coming from the translog (and is hence persisted already) or it does not have a sequence number
            assert noOp.origin().isFromTranslog() || noOpResult.getSeqNo() == SequenceNumbers.UNASSIGNED_SEQ_NO;
            localCheckpointTracker.markSeqNoAsPersisted(noOpResult.getSeqNo());
        }
        noOpResult.setTook(System.nanoTime() - noOp.startTime());
        noOpResult.freeze();
        return noOpResult;
    }
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) ParseContext(org.elasticsearch.index.mapper.ParseContext) Releasable(org.elasticsearch.common.lease.Releasable) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) TranslogCorruptedException(org.elasticsearch.index.translog.TranslogCorruptedException) IOException(java.io.IOException) Translog(org.elasticsearch.index.translog.Translog)

Example 27 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project crate by crate.

the class InternalEngine method trimUnreferencedTranslogFiles.

@Override
public void trimUnreferencedTranslogFiles() throws EngineException {
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        translog.trimUnreferencedReaders();
    } catch (AlreadyClosedException e) {
        failOnTragicEvent(e);
        throw e;
    } catch (Exception e) {
        try {
            failEngine("translog trimming failed", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw new EngineException(shardId, "failed to trim translog", e);
    }
}
Also used : AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ReleasableLock(org.elasticsearch.common.util.concurrent.ReleasableLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) TranslogCorruptedException(org.elasticsearch.index.translog.TranslogCorruptedException) IOException(java.io.IOException)

Example 28 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project crate by crate.

the class InternalTestCluster method assertSameDocIdsOnShards.

/**
 * Asserts that all shards with the same shardId should have document Ids.
 */
public void assertSameDocIdsOnShards() throws Exception {
    assertBusy(() -> {
        ClusterState state = client().admin().cluster().prepareState().get().getState();
        for (ObjectObjectCursor<String, IndexRoutingTable> indexRoutingTable : state.routingTable().indicesRouting()) {
            for (IntObjectCursor<IndexShardRoutingTable> indexShardRoutingTable : indexRoutingTable.value.shards()) {
                ShardRouting primaryShardRouting = indexShardRoutingTable.value.primaryShard();
                IndexShard primaryShard = getShardOrNull(state, primaryShardRouting);
                if (primaryShard == null) {
                    continue;
                }
                final List<DocIdSeqNoAndSource> docsOnPrimary;
                try {
                    docsOnPrimary = IndexShardTestCase.getDocIdAndSeqNos(primaryShard);
                } catch (AlreadyClosedException ex) {
                    continue;
                }
                for (ShardRouting replicaShardRouting : indexShardRoutingTable.value.replicaShards()) {
                    IndexShard replicaShard = getShardOrNull(state, replicaShardRouting);
                    if (replicaShard == null) {
                        continue;
                    }
                    final List<DocIdSeqNoAndSource> docsOnReplica;
                    try {
                        docsOnReplica = IndexShardTestCase.getDocIdAndSeqNos(replicaShard);
                    } catch (AlreadyClosedException ex) {
                        continue;
                    }
                    assertThat("out of sync shards: primary=[" + primaryShardRouting + "] num_docs_on_primary=[" + docsOnPrimary.size() + "] vs replica=[" + replicaShardRouting + "] num_docs_on_replica=[" + docsOnReplica.size() + "]", docsOnReplica, equalTo(docsOnPrimary));
                }
            }
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexShard(org.elasticsearch.index.shard.IndexShard) DocIdSeqNoAndSource(org.elasticsearch.index.engine.DocIdSeqNoAndSource) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 29 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project crate by crate.

the class InternalTestCluster method assertSameSyncIdSameDocs.

private void assertSameSyncIdSameDocs() {
    Map<String, Long> docsOnShards = new HashMap<>();
    final Collection<NodeAndClient> nodesAndClients = nodes.values();
    for (NodeAndClient nodeAndClient : nodesAndClients) {
        IndicesService indexServices = getInstance(IndicesService.class, nodeAndClient.name);
        for (IndexService indexService : indexServices) {
            for (IndexShard indexShard : indexService) {
                try {
                    CommitStats commitStats = indexShard.commitStats();
                    String syncId = commitStats.getUserData().get(Engine.SYNC_COMMIT_ID);
                    if (syncId != null) {
                        long liveDocsOnShard = commitStats.getNumDocs();
                        if (docsOnShards.get(syncId) != null) {
                            assertThat("sync id is equal but number of docs does not match on node " + nodeAndClient.name + ". expected " + docsOnShards.get(syncId) + " but got " + liveDocsOnShard, docsOnShards.get(syncId), equalTo(liveDocsOnShard));
                        } else {
                            docsOnShards.put(syncId, liveDocsOnShard);
                        }
                    }
                } catch (AlreadyClosedException e) {
                // the engine is closed or if the shard is recovering
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) IndexService(org.elasticsearch.index.IndexService) CommitStats(org.elasticsearch.index.engine.CommitStats) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException)

Example 30 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project crate by crate.

the class InternalTestCluster method assertSeqNos.

public void assertSeqNos() throws Exception {
    assertBusy(() -> {
        final ClusterState state = clusterService().state();
        for (ObjectObjectCursor<String, IndexRoutingTable> indexRoutingTable : state.routingTable().indicesRouting()) {
            for (IntObjectCursor<IndexShardRoutingTable> indexShardRoutingTable : indexRoutingTable.value.shards()) {
                ShardRouting primaryShardRouting = indexShardRoutingTable.value.primaryShard();
                final IndexShard primaryShard = getShardOrNull(state, primaryShardRouting);
                if (primaryShard == null) {
                    // just ignore - shard movement
                    continue;
                }
                final SeqNoStats primarySeqNoStats;
                final ObjectLongMap<String> syncGlobalCheckpoints;
                try {
                    primarySeqNoStats = primaryShard.seqNoStats();
                    syncGlobalCheckpoints = primaryShard.getInSyncGlobalCheckpoints();
                } catch (AlreadyClosedException ex) {
                    // shard is closed - just ignore
                    continue;
                }
                assertThat(primaryShardRouting + " should have set the global checkpoint", primarySeqNoStats.getGlobalCheckpoint(), not(equalTo(SequenceNumbers.UNASSIGNED_SEQ_NO)));
                for (ShardRouting replicaShardRouting : indexShardRoutingTable.value.replicaShards()) {
                    final IndexShard replicaShard = getShardOrNull(state, replicaShardRouting);
                    if (replicaShard == null) {
                        // just ignore - shard movement
                        continue;
                    }
                    final SeqNoStats seqNoStats;
                    try {
                        seqNoStats = replicaShard.seqNoStats();
                    } catch (AlreadyClosedException e) {
                        // shard is closed - just ignore
                        continue;
                    }
                    assertThat(replicaShardRouting + " seq_no_stats mismatch", seqNoStats, equalTo(primarySeqNoStats));
                    // the local knowledge on the primary of the global checkpoint equals the global checkpoint on the shard
                    assertThat(replicaShardRouting + " global checkpoint syncs mismatch", seqNoStats.getGlobalCheckpoint(), equalTo(syncGlobalCheckpoints.get(replicaShardRouting.allocationId().getId())));
                }
            }
        }
    }, 60, TimeUnit.SECONDS);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) IndexShard(org.elasticsearch.index.shard.IndexShard) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)79 IOException (java.io.IOException)53 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 MockDirectoryWrapper (org.apache.lucene.store.MockDirectoryWrapper)14 TranslogCorruptedException (org.elasticsearch.index.translog.TranslogCorruptedException)13 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 Document (org.apache.lucene.document.Document)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 ReleasableLock (org.elasticsearch.common.util.concurrent.ReleasableLock)10 UncheckedIOException (java.io.UncheckedIOException)9 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)9 EOFException (java.io.EOFException)8 ArrayList (java.util.ArrayList)7 FileNotFoundException (java.io.FileNotFoundException)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)6 NoSuchFileException (java.nio.file.NoSuchFileException)6 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6