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);
}
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());
}
}
}
}
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));
}
}
}
Aggregations