Search in sources :

Example 6 with Location

use of org.elasticsearch.index.translog.Translog.Location in project elasticsearch by elastic.

the class TranslogTests method testTragicEventCanBeAnyException.

public void testTragicEventCanBeAnyException() throws IOException {
    Path tempDir = createTempDir();
    final FailSwitch fail = new FailSwitch();
    TranslogConfig config = getTranslogConfig(tempDir);
    Translog translog = getFailableTranslog(fail, config, false, true, null);
    // writes pretty big docs so we cross buffer boarders regularly
    LineFileDocs lineFileDocs = new LineFileDocs(random());
    translog.add(new Translog.Index("test", "1", lineFileDocs.nextDoc().toString().getBytes(Charset.forName("UTF-8"))));
    fail.failAlways();
    try {
        Translog.Location location = translog.add(new Translog.Index("test", "2", lineFileDocs.nextDoc().toString().getBytes(Charset.forName("UTF-8"))));
        if (randomBoolean()) {
            translog.ensureSynced(location);
        } else {
            translog.sync();
        }
        //TODO once we have a mock FS that can simulate we can also fail on plain sync
        fail("WTF");
    } catch (UnknownException ex) {
    // w00t
    } catch (TranslogException ex) {
        assertTrue(ex.getCause() instanceof UnknownException);
    }
    assertFalse(translog.isOpen());
    assertTrue(translog.getTragicException() instanceof UnknownException);
}
Also used : Path(java.nio.file.Path) Location(org.elasticsearch.index.translog.Translog.Location) LineFileDocs(org.apache.lucene.util.LineFileDocs)

Example 7 with Location

use of org.elasticsearch.index.translog.Translog.Location in project elasticsearch by elastic.

the class TranslogTests method testLocationHashCodeEquals.

public void testLocationHashCodeEquals() throws IOException {
    List<Translog.Location> locations = new ArrayList<>();
    List<Translog.Location> locations2 = new ArrayList<>();
    int translogOperations = randomIntBetween(10, 100);
    try (Translog translog2 = create(createTempDir())) {
        for (int op = 0; op < translogOperations; op++) {
            locations.add(translog.add(new Translog.Index("test", "" + op, Integer.toString(op).getBytes(Charset.forName("UTF-8")))));
            locations2.add(translog2.add(new Translog.Index("test", "" + op, Integer.toString(op).getBytes(Charset.forName("UTF-8")))));
        }
        int iters = randomIntBetween(10, 100);
        for (int i = 0; i < iters; i++) {
            Translog.Location location = RandomPicks.randomFrom(random(), locations);
            for (Translog.Location loc : locations) {
                if (loc == location) {
                    assertTrue(loc.equals(location));
                    assertEquals(loc.hashCode(), location.hashCode());
                } else {
                    assertFalse(loc.equals(location));
                }
            }
            for (int j = 0; j < translogOperations; j++) {
                assertTrue(locations.get(j).equals(locations2.get(j)));
                assertEquals(locations.get(j).hashCode(), locations2.get(j).hashCode());
            }
        }
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Location(org.elasticsearch.index.translog.Translog.Location) Location(org.elasticsearch.index.translog.Translog.Location)

Example 8 with Location

use of org.elasticsearch.index.translog.Translog.Location in project elasticsearch by elastic.

the class TranslogTests method testSyncUpTo.

public void testSyncUpTo() throws IOException {
    int translogOperations = randomIntBetween(10, 100);
    int count = 0;
    for (int op = 0; op < translogOperations; op++) {
        final Translog.Location location = translog.add(new Translog.Index("test", "" + op, Integer.toString(++count).getBytes(Charset.forName("UTF-8"))));
        if (randomBoolean()) {
            assertTrue("at least one operation pending", translog.syncNeeded());
            assertTrue("this operation has not been synced", translog.ensureSynced(location));
            // we are the last location so everything should be synced
            assertFalse("the last call to ensureSycned synced all previous ops", translog.syncNeeded());
            translog.add(new Translog.Index("test", "" + op, Integer.toString(++count).getBytes(Charset.forName("UTF-8"))));
            assertTrue("one pending operation", translog.syncNeeded());
            // not syncing now
            assertFalse("this op has been synced before", translog.ensureSynced(location));
            assertTrue("we only synced a previous operation yet", translog.syncNeeded());
        }
        if (rarely()) {
            translog.commit();
            // not syncing now
            assertFalse("location is from a previous translog - already synced", translog.ensureSynced(location));
            assertFalse("no sync needed since no operations in current translog", translog.syncNeeded());
        }
        if (randomBoolean()) {
            translog.sync();
            assertFalse("translog has been synced already", translog.ensureSynced(location));
        }
    }
}
Also used : Location(org.elasticsearch.index.translog.Translog.Location)

Aggregations

Location (org.elasticsearch.index.translog.Translog.Location)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6 ArrayList (java.util.ArrayList)5 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.hasToString (org.hamcrest.Matchers.hasToString)3 EOFException (java.io.EOFException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IOException (java.io.IOException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1