Search in sources :

Example 11 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project elasticsearch by elastic.

the class IndexShardTests method testIndexingOperationsListeners.

public void testIndexingOperationsListeners() throws IOException {
    IndexShard shard = newStartedShard(true);
    indexDoc(shard, "test", "0", "{\"foo\" : \"bar\"}");
    AtomicInteger preIndex = new AtomicInteger();
    AtomicInteger postIndexCreate = new AtomicInteger();
    AtomicInteger postIndexUpdate = new AtomicInteger();
    AtomicInteger postIndexException = new AtomicInteger();
    AtomicInteger preDelete = new AtomicInteger();
    AtomicInteger postDelete = new AtomicInteger();
    AtomicInteger postDeleteException = new AtomicInteger();
    shard.close("simon says", true);
    shard = reinitShard(shard, new IndexingOperationListener() {

        @Override
        public Engine.Index preIndex(ShardId shardId, Engine.Index operation) {
            preIndex.incrementAndGet();
            return operation;
        }

        @Override
        public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) {
            if (result.hasFailure() == false) {
                if (result.isCreated()) {
                    postIndexCreate.incrementAndGet();
                } else {
                    postIndexUpdate.incrementAndGet();
                }
            } else {
                postIndex(shardId, index, result.getFailure());
            }
        }

        @Override
        public void postIndex(ShardId shardId, Engine.Index index, Exception ex) {
            postIndexException.incrementAndGet();
        }

        @Override
        public Engine.Delete preDelete(ShardId shardId, Engine.Delete delete) {
            preDelete.incrementAndGet();
            return delete;
        }

        @Override
        public void postDelete(ShardId shardId, Engine.Delete delete, Engine.DeleteResult result) {
            if (result.hasFailure() == false) {
                postDelete.incrementAndGet();
            } else {
                postDelete(shardId, delete, result.getFailure());
            }
        }

        @Override
        public void postDelete(ShardId shardId, Engine.Delete delete, Exception ex) {
            postDeleteException.incrementAndGet();
        }
    });
    recoveryShardFromStore(shard);
    ParsedDocument doc = testParsedDocument("1", "test", null, new ParseContext.Document(), new BytesArray(new byte[] { 1 }), null);
    Engine.Index index = new Engine.Index(new Term("_uid", doc.uid()), doc);
    shard.index(index);
    assertEquals(1, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(0, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(0, preDelete.get());
    assertEquals(0, postDelete.get());
    assertEquals(0, postDeleteException.get());
    shard.index(index);
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(0, preDelete.get());
    assertEquals(0, postDelete.get());
    assertEquals(0, postDeleteException.get());
    Engine.Delete delete = new Engine.Delete("test", "1", new Term("_uid", doc.uid()));
    shard.delete(delete);
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(1, preDelete.get());
    assertEquals(1, postDelete.get());
    assertEquals(0, postDeleteException.get());
    shard.close("Unexpected close", true);
    // It will generate exception
    shard.state = IndexShardState.STARTED;
    try {
        shard.index(index);
        fail();
    } catch (AlreadyClosedException e) {
    }
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(1, preDelete.get());
    assertEquals(1, postDelete.get());
    assertEquals(0, postDeleteException.get());
    try {
        shard.delete(delete);
        fail();
    } catch (AlreadyClosedException e) {
    }
    assertEquals(2, preIndex.get());
    assertEquals(1, postIndexCreate.get());
    assertEquals(1, postIndexUpdate.get());
    assertEquals(0, postIndexException.get());
    assertEquals(1, preDelete.get());
    assertEquals(1, postDelete.get());
    assertEquals(0, postDeleteException.get());
    closeShards(shard);
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) Lucene.cleanLuceneIndex(org.elasticsearch.common.lucene.Lucene.cleanLuceneIndex) Term(org.apache.lucene.index.Term) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EngineException(org.elasticsearch.index.engine.EngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParseContext(org.elasticsearch.index.mapper.ParseContext) Engine(org.elasticsearch.index.engine.Engine)

Example 12 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project elasticsearch by elastic.

the class TranslogTests method testCloseConcurrently.

public void testCloseConcurrently() throws Throwable {
    final int opsPerThread = randomIntBetween(10, 200);
    int threadCount = 2 + randomInt(5);
    logger.info("testing with [{}] threads, each doing [{}] ops", threadCount, opsPerThread);
    final BlockingQueue<LocationOperation> writtenOperations = new ArrayBlockingQueue<>(threadCount * opsPerThread);
    Thread[] threads = new Thread[threadCount];
    final Exception[] threadExceptions = new Exception[threadCount];
    final CountDownLatch downLatch = new CountDownLatch(1);
    for (int i = 0; i < threadCount; i++) {
        final int threadId = i;
        threads[i] = new TranslogThread(translog, downLatch, opsPerThread, threadId, writtenOperations, threadExceptions);
        threads[i].setDaemon(true);
        threads[i].start();
    }
    downLatch.countDown();
    translog.close();
    for (int i = 0; i < threadCount; i++) {
        if (threadExceptions[i] != null) {
            if ((threadExceptions[i] instanceof AlreadyClosedException) == false) {
                throw threadExceptions[i];
            }
        }
        threads[i].join(60 * 1000);
    }
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EOFException(java.io.EOFException) InvalidPathException(java.nio.file.InvalidPathException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 13 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project elasticsearch by elastic.

the class TranslogTests method testFailFlush.

public void testFailFlush() throws IOException {
    Path tempDir = createTempDir();
    final FailSwitch fail = new FailSwitch();
    TranslogConfig config = getTranslogConfig(tempDir);
    Translog translog = getFailableTranslog(fail, config);
    List<Translog.Location> locations = new ArrayList<>();
    int opsSynced = 0;
    boolean failed = false;
    while (failed == false) {
        try {
            locations.add(translog.add(new Translog.Index("test", "" + opsSynced, Integer.toString(opsSynced).getBytes(Charset.forName("UTF-8")))));
            translog.sync();
            opsSynced++;
        } catch (MockDirectoryWrapper.FakeIOException ex) {
            failed = true;
            assertFalse(translog.isOpen());
        } catch (IOException ex) {
            failed = true;
            assertFalse(translog.isOpen());
            assertEquals("__FAKE__ no space left on device", ex.getMessage());
        }
        if (randomBoolean()) {
            fail.failAlways();
        } else {
            fail.failNever();
        }
    }
    fail.failNever();
    if (randomBoolean()) {
        try {
            locations.add(translog.add(new Translog.Index("test", "" + opsSynced, Integer.toString(opsSynced).getBytes(Charset.forName("UTF-8")))));
            fail("we are already closed");
        } catch (AlreadyClosedException ex) {
            assertNotNull(ex.getCause());
            if (ex.getCause() instanceof MockDirectoryWrapper.FakeIOException) {
                assertNull(ex.getCause().getMessage());
            } else {
                assertEquals(ex.getCause().getMessage(), "__FAKE__ no space left on device");
            }
        }
    }
    Translog.TranslogGeneration translogGeneration = translog.getGeneration();
    try {
        translog.newSnapshot();
        fail("already closed");
    } catch (AlreadyClosedException ex) {
        // all is well
        assertNotNull(ex.getCause());
        assertSame(translog.getTragicException(), ex.getCause());
    }
    try {
        translog.commit();
        fail("already closed");
    } catch (AlreadyClosedException ex) {
        assertNotNull(ex.getCause());
        assertSame(translog.getTragicException(), ex.getCause());
    }
    assertFalse(translog.isOpen());
    // we are closed
    translog.close();
    try (Translog tlog = new Translog(config, translogGeneration, () -> SequenceNumbersService.UNASSIGNED_SEQ_NO)) {
        assertEquals("lastCommitted must be 1 less than current", translogGeneration.translogFileGeneration + 1, tlog.currentFileGeneration());
        assertFalse(tlog.syncNeeded());
        Translog.Snapshot snapshot = tlog.newSnapshot();
        assertEquals(opsSynced, snapshot.totalOperations());
        for (int i = 0; i < opsSynced; i++) {
            assertEquals("expected operation" + i + " to be in the previous translog but wasn't", tlog.currentFileGeneration() - 1, locations.get(i).generation);
            Translog.Operation next = snapshot.next();
            assertNotNull("operation " + i + " must be non-null", next);
            assertEquals(i, Integer.parseInt(next.getSource().source.utf8ToString()));
        }
    }
}
Also used : Path(java.nio.file.Path) MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) Location(org.elasticsearch.index.translog.Translog.Location)

Example 14 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project elasticsearch by elastic.

the class StoreTests method testRefCount.

public void testRefCount() throws IOException {
    final ShardId shardId = new ShardId("index", "_na_", 1);
    DirectoryService directoryService = new LuceneManagedDirectoryService(random());
    IndexSettings indexSettings = INDEX_SETTINGS;
    Store store = new Store(shardId, indexSettings, directoryService, new DummyShardLock(shardId));
    int incs = randomIntBetween(1, 100);
    for (int i = 0; i < incs; i++) {
        if (randomBoolean()) {
            store.incRef();
        } else {
            assertTrue(store.tryIncRef());
        }
        store.ensureOpen();
    }
    for (int i = 0; i < incs; i++) {
        store.decRef();
        store.ensureOpen();
    }
    store.incRef();
    store.close();
    for (int i = 0; i < incs; i++) {
        if (randomBoolean()) {
            store.incRef();
        } else {
            assertTrue(store.tryIncRef());
        }
        store.ensureOpen();
    }
    for (int i = 0; i < incs; i++) {
        store.decRef();
        store.ensureOpen();
    }
    store.decRef();
    assertThat(store.refCount(), Matchers.equalTo(0));
    assertFalse(store.tryIncRef());
    try {
        store.incRef();
        fail(" expected exception");
    } catch (AlreadyClosedException ex) {
    }
    try {
        store.ensureOpen();
        fail(" expected exception");
    } catch (AlreadyClosedException ex) {
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexSettings(org.elasticsearch.index.IndexSettings) DummyShardLock(org.elasticsearch.test.DummyShardLock) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException)

Example 15 with AlreadyClosedException

use of org.apache.lucene.store.AlreadyClosedException in project elasticsearch by elastic.

the class StreamInput method readException.

public <T extends Exception> T readException() throws IOException {
    if (readBoolean()) {
        int key = readVInt();
        switch(key) {
            case 0:
                final int ord = readVInt();
                return (T) ElasticsearchException.readException(this, ord);
            case 1:
                String msg1 = readOptionalString();
                String resource1 = readOptionalString();
                return (T) readStackTrace(new CorruptIndexException(msg1, resource1, readException()), this);
            case 2:
                String resource2 = readOptionalString();
                int version2 = readInt();
                int minVersion2 = readInt();
                int maxVersion2 = readInt();
                return (T) readStackTrace(new IndexFormatTooNewException(resource2, version2, minVersion2, maxVersion2), this);
            case 3:
                String resource3 = readOptionalString();
                if (readBoolean()) {
                    int version3 = readInt();
                    int minVersion3 = readInt();
                    int maxVersion3 = readInt();
                    return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3, minVersion3, maxVersion3), this);
                } else {
                    String version3 = readOptionalString();
                    return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3), this);
                }
            case 4:
                return (T) readStackTrace(new NullPointerException(readOptionalString()), this);
            case 5:
                return (T) readStackTrace(new NumberFormatException(readOptionalString()), this);
            case 6:
                return (T) readStackTrace(new IllegalArgumentException(readOptionalString(), readException()), this);
            case 7:
                return (T) readStackTrace(new AlreadyClosedException(readOptionalString(), readException()), this);
            case 8:
                return (T) readStackTrace(new EOFException(readOptionalString()), this);
            case 9:
                return (T) readStackTrace(new SecurityException(readOptionalString(), readException()), this);
            case 10:
                return (T) readStackTrace(new StringIndexOutOfBoundsException(readOptionalString()), this);
            case 11:
                return (T) readStackTrace(new ArrayIndexOutOfBoundsException(readOptionalString()), this);
            case 12:
                return (T) readStackTrace(new FileNotFoundException(readOptionalString()), this);
            case 13:
                final int subclass = readVInt();
                final String file = readOptionalString();
                final String other = readOptionalString();
                final String reason = readOptionalString();
                // skip the msg - it's composed from file, other and reason
                readOptionalString();
                final Exception exception;
                switch(subclass) {
                    case 0:
                        exception = new NoSuchFileException(file, other, reason);
                        break;
                    case 1:
                        exception = new NotDirectoryException(file);
                        break;
                    case 2:
                        exception = new DirectoryNotEmptyException(file);
                        break;
                    case 3:
                        exception = new AtomicMoveNotSupportedException(file, other, reason);
                        break;
                    case 4:
                        exception = new FileAlreadyExistsException(file, other, reason);
                        break;
                    case 5:
                        exception = new AccessDeniedException(file, other, reason);
                        break;
                    case 6:
                        exception = new FileSystemLoopException(file);
                        break;
                    case 7:
                        exception = new FileSystemException(file, other, reason);
                        break;
                    default:
                        throw new IllegalStateException("unknown FileSystemException with index " + subclass);
                }
                return (T) readStackTrace(exception, this);
            case 14:
                return (T) readStackTrace(new IllegalStateException(readOptionalString(), readException()), this);
            case 15:
                return (T) readStackTrace(new LockObtainFailedException(readOptionalString(), readException()), this);
            case 16:
                return (T) readStackTrace(new InterruptedException(readOptionalString()), this);
            case 17:
                return (T) readStackTrace(new IOException(readOptionalString(), readException()), this);
            default:
                assert false : "no such exception for id: " + key;
        }
    }
    return null;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NotDirectoryException(java.nio.file.NotDirectoryException) FileSystemException(java.nio.file.FileSystemException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) EOFException(java.io.EOFException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) GeoPoint(org.elasticsearch.common.geo.GeoPoint) ElasticsearchException(org.elasticsearch.ElasticsearchException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) FileSystemLoopException(java.nio.file.FileSystemLoopException) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) AccessDeniedException(java.nio.file.AccessDeniedException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

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