Search in sources :

Example 6 with CheckIndex

use of org.apache.lucene.index.CheckIndex in project lucene-solr by apache.

the class TestUtil method checkIndex.

/** If failFast is true, then throw the first exception when index corruption is hit, instead of moving on to other fields/segments to
   *  look for any other corruption.  */
public static CheckIndex.Status checkIndex(Directory dir, boolean crossCheckTermVectors, boolean failFast, ByteArrayOutputStream output) throws IOException {
    if (output == null) {
        output = new ByteArrayOutputStream(1024);
    }
    // some tests e.g. exception tests become much more complicated if they have to close the writer
    try (CheckIndex checker = new CheckIndex(dir, NoLockFactory.INSTANCE.obtainLock(dir, "bogus"))) {
        checker.setCrossCheckTermVectors(crossCheckTermVectors);
        checker.setFailFast(failFast);
        checker.setInfoStream(new PrintStream(output, false, IOUtils.UTF_8), false);
        CheckIndex.Status indexStatus = checker.checkIndex(null);
        if (indexStatus == null || indexStatus.clean == false) {
            System.out.println("CheckIndex failed");
            System.out.println(output.toString(IOUtils.UTF_8));
            throw new RuntimeException("CheckIndex failed");
        } else {
            if (LuceneTestCase.INFOSTREAM) {
                System.out.println(output.toString(IOUtils.UTF_8));
            }
            return indexStatus;
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CheckIndex(org.apache.lucene.index.CheckIndex)

Example 7 with CheckIndex

use of org.apache.lucene.index.CheckIndex in project zm-mailbox by Zimbra.

the class LuceneIndex method verify.

/**
     * Run a sanity check for the index. Callers are responsible to make sure the index is not opened by any writer.
     *
     * @param out info stream where messages should go. If null, no messages are printed.
     * @return true if no problems were found, otherwise false
     * @throws IOException failed to verify, but it doesn't necessarily mean the index is corrupted.
     */
@Override
public boolean verify(PrintStream out) throws IOException {
    if (!IndexReader.indexExists(luceneDirectory)) {
        out.println("index does not exist or no segments file found: " + luceneDirectory.getDirectory());
        return true;
    }
    CheckIndex check = new CheckIndex(luceneDirectory);
    if (out != null) {
        check.setInfoStream(out);
    }
    CheckIndex.Status status = check.checkIndex();
    return status.clean;
}
Also used : CheckIndex(org.apache.lucene.index.CheckIndex)

Example 8 with CheckIndex

use of org.apache.lucene.index.CheckIndex in project zm-mailbox by Zimbra.

the class LuceneViewer method doCheck.

private static void doCheck(CommandLine cl) throws Exception {
    Console console = new Console(cl.hasOption(CLI.O_VERBOSE));
    String indexDir = cl.getOptionValue(CLI.O_INPUT);
    console.info("Checking index " + indexDir);
    Directory dir = null;
    try {
        dir = LuceneDirectory.open(new File(indexDir));
    } catch (Throwable t) {
        console.info("ERROR: could not open directory \"" + indexDir + "\"; exiting");
        t.printStackTrace(System.out);
        System.exit(1);
    }
    CheckIndex checker = new CheckIndex(dir);
    checker.setInfoStream(System.out);
    Status result = checker.checkIndex();
    console.info("Result:" + (result.clean ? "clean" : "not clean"));
}
Also used : Status(org.apache.lucene.index.CheckIndex.Status) File(java.io.File) CheckIndex(org.apache.lucene.index.CheckIndex) Directory(org.apache.lucene.store.Directory)

Example 9 with CheckIndex

use of org.apache.lucene.index.CheckIndex in project jackrabbit-oak by apache.

the class IndexConsistencyChecker method checkIndexDirectory.

private void checkIndexDirectory(DirectoryStatus dirStatus, NodeState idx, IndexDefinition defn, File workDir, String dirName, Closer closer) throws IOException {
    File idxDir = createWorkDir(workDir, dirName);
    Directory sourceDir = new OakDirectory(new ReadOnlyBuilder(idx), dirName, defn, true);
    Directory targetDir = FSDirectory.open(idxDir);
    closer.register(sourceDir);
    closer.register(targetDir);
    boolean clean = true;
    for (String file : sourceDir.listAll()) {
        log.debug("[{}][{}] Checking {}", indexPath, dirName, file);
        try {
            sourceDir.copy(targetDir, file, file, IOContext.DEFAULT);
        } catch (FileNotFoundException ignore) {
            dirStatus.missingFiles.add(file);
            clean = false;
            log.warn("[{}][{}] File {} missing", indexPath, dirName, file);
        }
        if (targetDir.fileLength(file) != sourceDir.fileLength(file)) {
            FileSizeStatus fileStatus = new FileSizeStatus(file, targetDir.fileLength(file), sourceDir.fileLength(file));
            dirStatus.filesWithSizeMismatch.add(fileStatus);
            clean = false;
            log.warn("[{}][{}] File size mismatch {}", indexPath, dirName, fileStatus);
        } else {
            dirStatus.size += sourceDir.fileLength(file);
            log.debug("[{}][{}] File {} is consistent", indexPath, dirName, file);
        }
    }
    if (clean) {
        log.debug("[{}][{}] Directory content found to be consistent. Proceeding to IndexCheck", indexPath, dirName);
        CheckIndex ci = new CheckIndex(targetDir);
        if (printStream != null) {
            ci.setInfoStream(printStream, verbose);
        } else if (log.isDebugEnabled()) {
            ci.setInfoStream(new LoggingPrintStream(log), log.isTraceEnabled());
        }
        dirStatus.status = ci.checkIndex();
        dirStatus.clean = dirStatus.status.clean;
        log.debug("[{}][{}] IndexCheck was successful. Proceeding to open DirectoryReader", indexPath, dirName);
    }
    if (dirStatus.clean) {
        DirectoryReader dirReader = DirectoryReader.open(targetDir);
        dirStatus.numDocs = dirReader.numDocs();
        log.debug("[{}][{}] DirectoryReader can be opened", indexPath, dirName);
        closer.register(dirReader);
    }
}
Also used : ReadOnlyBuilder(org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder) DirectoryReader(org.apache.lucene.index.DirectoryReader) FileNotFoundException(java.io.FileNotFoundException) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) File(java.io.File) CheckIndex(org.apache.lucene.index.CheckIndex) OakDirectory(org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 10 with CheckIndex

use of org.apache.lucene.index.CheckIndex in project lucene-solr by apache.

the class CheckHdfsIndex method doMain.

// actual main: returns exit code instead of terminating JVM (for easy testing)
@SuppressForbidden(reason = "System.out required: command line tool")
protected static int doMain(String[] args) throws IOException, InterruptedException {
    CheckIndex.Options opts;
    try {
        opts = CheckIndex.parseOptions(args);
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage());
        return 1;
    }
    if (!CheckIndex.assertsOn()) {
        System.out.println("\nNOTE: testing will be more thorough if you run java with '-ea:org.apache.lucene...', so assertions are enabled");
    }
    if (opts.getDirImpl() != null) {
        System.out.println("\nIgnoring specified -dir-impl, instead using " + HdfsDirectory.class.getSimpleName());
    }
    System.out.println("\nOpening index @ " + opts.getIndexPath() + "\n");
    Directory directory;
    try {
        directory = new HdfsDirectory(new Path(opts.getIndexPath()), getConf());
    } catch (IOException e) {
        System.out.println("ERROR: could not open hdfs directory \"" + opts.getIndexPath() + "\"; exiting");
        e.printStackTrace(System.out);
        return 1;
    }
    try (Directory dir = directory;
        CheckIndex checker = new CheckIndex(dir)) {
        opts.setOut(System.out);
        return checker.doCheck(opts);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) HdfsDirectory(org.apache.solr.store.hdfs.HdfsDirectory) IOException(java.io.IOException) CheckIndex(org.apache.lucene.index.CheckIndex) HdfsDirectory(org.apache.solr.store.hdfs.HdfsDirectory) Directory(org.apache.lucene.store.Directory) SuppressForbidden(org.apache.lucene.util.SuppressForbidden)

Aggregations

CheckIndex (org.apache.lucene.index.CheckIndex)10 IOException (java.io.IOException)5 PrintStream (java.io.PrintStream)5 Directory (org.apache.lucene.store.Directory)5 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 DirectoryReader (org.apache.lucene.index.DirectoryReader)2 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 ByteSizeValue (org.elasticsearch.common.unit.ByteSizeValue)2 ShardId (org.elasticsearch.index.shard.ShardId)2 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1