use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsTrackerTest method shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation.
@Test
public void shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation() throws Exception {
// given
CountsOracle oracle = someData();
final int firstTransaction = 2, secondTransaction = 3;
try (Lifespan life = new Lifespan()) {
CountsTracker tracker = life.add(newTracker());
oracle.update(tracker, firstTransaction);
tracker.rotate(firstTransaction);
}
// when
final CountsOracle delta = new CountsOracle();
{
CountsOracle.Node n1 = delta.node(1);
// Label 4 has not been used before...
CountsOracle.Node n2 = delta.node(1, 4);
delta.relationship(n1, 1, n2);
// relationshipType 2 has not been used before...
delta.relationship(n2, 2, n1);
}
delta.update(oracle);
try (Lifespan life = new Lifespan()) {
final Barrier.Control barrier = new Barrier.Control();
CountsTracker tracker = life.add(new CountsTracker(resourceManager.logProvider(), resourceManager.fileSystem(), resourceManager.pageCache(), Config.empty(), resourceManager.testPath()) {
@Override
protected boolean include(CountsKey countsKey, ReadableBuffer value) {
barrier.reached();
return super.include(countsKey, value);
}
});
Future<Void> task = threading.execute((ThrowingFunction<CountsTracker, Void, RuntimeException>) t -> {
try {
delta.update(t, secondTransaction);
t.rotate(secondTransaction);
} catch (IOException e) {
throw new AssertionError(e);
}
return null;
}, tracker);
// then
barrier.await();
oracle.verify(tracker);
barrier.release();
task.get();
oracle.verify(tracker);
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsTrackerTest method shouldUpdateCountsOnExistingStore.
@Test
public void shouldUpdateCountsOnExistingStore() throws Exception {
// given
CountsOracle oracle = someData();
int firstTx = 2, secondTx = 3;
try (Lifespan life = new Lifespan()) {
CountsTracker tracker = life.add(newTracker());
oracle.update(tracker, firstTx);
tracker.rotate(firstTx);
oracle.verify(tracker);
// when
CountsOracle delta = new CountsOracle();
{
CountsOracle.Node n1 = delta.node(1);
// Label 4 has not been used before...
CountsOracle.Node n2 = delta.node(1, 4);
delta.relationship(n1, 1, n2);
// relationshipType 2 has not been used before...
delta.relationship(n2, 2, n1);
}
delta.update(tracker, secondTx);
delta.update(oracle);
// then
oracle.verify(tracker);
// when
tracker.rotate(secondTx);
}
// then
try (Lifespan life = new Lifespan()) {
oracle.verify(life.add(newTracker()));
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class AbstractKeyValueStoreTest method shouldNotPickCorruptStoreFile.
@Test
public void shouldNotPickCorruptStoreFile() throws Exception {
// given
Store store = createTestStore();
RotationStrategy rotation = store.rotationStrategy;
// when
File[] files = new File[10];
{
Pair<File, KeyValueStoreFile> file = rotation.create(EMPTY_DATA_PROVIDER, 1);
files[0] = file.first();
for (int txId = 2, i = 1; i < files.length; txId <<= 1, i++) {
KeyValueStoreFile old = file.other();
final int data = txId;
file = rotation.next(file.first(), Headers.headersBuilder().put(TX_ID, (long) txId).headers(), data(new Entry() {
@Override
public void write(WritableBuffer key, WritableBuffer value) {
key.putByte(0, (byte) 'f');
key.putByte(1, (byte) 'o');
key.putByte(2, (byte) 'o');
value.putInt(0, data);
}
}));
old.close();
files[i] = file.first();
}
file.other().close();
}
// Corrupt the last files
try (StoreChannel channel = resourceManager.fileSystem().open(files[9], "rw")) {
// ruin the header
channel.position(16);
ByteBuffer value = ByteBuffer.allocate(16);
value.put((byte) 0);
value.flip();
channel.writeAll(value);
}
try (StoreChannel channel = resourceManager.fileSystem().open(files[8], "rw")) {
// ruin the header
channel.position(32);
ByteBuffer value = ByteBuffer.allocate(16);
value.put((byte) 17);
value.flip();
channel.writeAll(value);
}
try (StoreChannel channel = resourceManager.fileSystem().open(files[7], "rw")) {
// ruin the header
channel.position(32 + 32 + 32 + 16);
ByteBuffer value = ByteBuffer.allocate(16);
value.putLong(0);
value.putLong(0);
value.flip();
channel.writeAll(value);
}
// then
try (Lifespan life = new Lifespan()) {
life.add(store);
assertEquals(64L, store.headers().get(TX_ID).longValue());
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsComputerTest method rebuildCounts.
private void rebuildCounts(long lastCommittedTransactionId) throws IOException {
cleanupCountsForRebuilding();
StoreFactory storeFactory = new StoreFactory(dir, pageCache, fs, NullLogProvider.getInstance());
try (Lifespan life = new Lifespan();
NeoStores neoStores = storeFactory.openAllNeoStores()) {
NodeStore nodeStore = neoStores.getNodeStore();
RelationshipStore relationshipStore = neoStores.getRelationshipStore();
int highLabelId = (int) neoStores.getLabelTokenStore().getHighId();
int highRelationshipTypeId = (int) neoStores.getRelationshipTypeTokenStore().getHighId();
CountsComputer countsComputer = new CountsComputer(lastCommittedTransactionId, nodeStore, relationshipStore, highLabelId, highRelationshipTypeId);
CountsTracker countsTracker = createCountsTracker();
life.add(countsTracker.setInitializer(countsComputer));
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class RecoveryCorruptedTransactionLogIT method startWithoutProblemsIfRotationForcedBeforeFileEnd.
@Test
void startWithoutProblemsIfRotationForcedBeforeFileEnd() throws IOException {
DatabaseManagementService managementService = databaseFactory.build();
GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
logFiles = buildDefaultLogFiles(getStoreId(database));
generateTransaction(database);
managementService.shutdown();
try (Lifespan lifespan = new Lifespan(logFiles)) {
Path originalFile = logFiles.getLogFile().getHighestLogFile();
// in it its current position.
try (StoreFileChannel writeChannel = fileSystem.write(originalFile)) {
writeChannel.position(writeChannel.size());
for (int i = 0; i < 10; i++) {
writeChannel.writeAll(ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0 }));
}
}
logFiles.getLogFile().rotate();
}
startStopDatabase();
assertThat(logProvider).doesNotContainMessage("Fail to read transaction log version 0.");
}
Aggregations