Search in sources :

Example 1 with MetadataSnapshot

use of org.opensearch.index.store.Store.MetadataSnapshot in project OpenSearch by opensearch-project.

the class IndexShard method doCheckIndex.

private void doCheckIndex() throws IOException {
    long timeNS = System.nanoTime();
    if (!Lucene.indexExists(store.directory())) {
        return;
    }
    try (BytesStreamOutput os = new BytesStreamOutput();
        PrintStream out = new PrintStream(os, false, StandardCharsets.UTF_8.name())) {
        if ("checksum".equals(checkIndexOnStartup)) {
            // physical verification only: verify all checksums for the latest commit
            IOException corrupt = null;
            MetadataSnapshot metadata = snapshotStoreMetadata();
            for (Map.Entry<String, StoreFileMetadata> entry : metadata.asMap().entrySet()) {
                try {
                    Store.checkIntegrity(entry.getValue(), store.directory());
                    out.println("checksum passed: " + entry.getKey());
                } catch (IOException exc) {
                    out.println("checksum failed: " + entry.getKey());
                    exc.printStackTrace(out);
                    corrupt = exc;
                }
            }
            out.flush();
            if (corrupt != null) {
                logger.warn("check index [failure]\n{}", os.bytes().utf8ToString());
                throw corrupt;
            }
        } else {
            // full checkindex
            final CheckIndex.Status status = store.checkIndex(out);
            out.flush();
            if (!status.clean) {
                if (state == IndexShardState.CLOSED) {
                    // ignore if closed....
                    return;
                }
                logger.warn("check index [failure]\n{}", os.bytes().utf8ToString());
                throw new IOException("index check failure");
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("check index [success]\n{}", os.bytes().utf8ToString());
        }
    }
    recoveryState.getVerifyIndex().checkIndexTime(Math.max(0, TimeValue.nsecToMSec(System.nanoTime() - timeNS)));
}
Also used : PrintStream(java.io.PrintStream) IOException(java.io.IOException) StoreFileMetadata(org.opensearch.index.store.StoreFileMetadata) Map(java.util.Map) ObjectLongMap(com.carrotsearch.hppc.ObjectLongMap) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) CheckIndex(org.apache.lucene.index.CheckIndex) MetadataSnapshot(org.opensearch.index.store.Store.MetadataSnapshot)

Aggregations

ObjectLongMap (com.carrotsearch.hppc.ObjectLongMap)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Map (java.util.Map)1 CheckIndex (org.apache.lucene.index.CheckIndex)1 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)1 MetadataSnapshot (org.opensearch.index.store.Store.MetadataSnapshot)1 StoreFileMetadata (org.opensearch.index.store.StoreFileMetadata)1