use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsRotationTest method shouldCreateEmptyCountsTrackerStoreWhenCreatingDatabase.
@Test
public void shouldCreateEmptyCountsTrackerStoreWhenCreatingDatabase() throws IOException {
// GIVEN
GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
// WHEN
db.shutdown();
// THEN
assertTrue(fs.fileExists(alphaStoreFile()));
assertFalse(fs.fileExists(betaStoreFile()));
try (Lifespan life = new Lifespan()) {
CountsTracker store = life.add(createCountsTracker(pageCache));
assertEquals(BASE_TX_ID, store.txId());
assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
assertEquals(0, store.totalEntriesStored());
assertEquals(0, allRecords(store).size());
}
try (Lifespan life = new Lifespan()) {
CountsTracker store = life.add(createCountsTracker(pageCache));
assertEquals(BASE_TX_ID, store.txId());
assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
assertEquals(0, store.totalEntriesStored());
assertEquals(0, allRecords(store).size());
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsRotationTest method shouldRotateCountsStoreWhenRotatingLog.
@Test
public void shouldRotateCountsStoreWhenRotatingLog() throws IOException {
// GIVEN
GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
// WHEN doing a transaction (actually two, the label-mini-tx also counts)
try (Transaction tx = db.beginTx()) {
db.createNode(B);
tx.success();
}
// and rotating the log (which implies flushing)
checkPoint(db);
// and creating another node after it
try (Transaction tx = db.beginTx()) {
db.createNode(C);
tx.success();
}
// THEN
assertTrue(fs.fileExists(alphaStoreFile()));
assertTrue(fs.fileExists(betaStoreFile()));
final PageCache pageCache = db.getDependencyResolver().resolveDependency(PageCache.class);
try (Lifespan life = new Lifespan()) {
CountsTracker store = life.add(createCountsTracker(pageCache));
// NOTE since the rotation happens before the second transaction is committed we do not see those changes
// in the stats
// a transaction for creating the label and a transaction for the node
assertEquals(BASE_TX_ID + 1 + 1, store.txId());
assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
// one for all nodes and one for the created "B" label
assertEquals(1 + 1, store.totalEntriesStored());
assertEquals(1 + 1, allRecords(store).size());
}
// on the other hand the tracker should read the correct value by merging data on disk and data in memory
final CountsTracker tracker = db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getCounts();
assertEquals(1 + 1, tracker.nodeCount(-1, newDoubleLongRegister()).readSecond());
final LabelTokenHolder holder = db.getDependencyResolver().resolveDependency(LabelTokenHolder.class);
int labelId = holder.getIdByName(C.name());
assertEquals(1, tracker.nodeCount(labelId, newDoubleLongRegister()).readSecond());
db.shutdown();
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsTrackerTest method shouldStoreCounts.
@Test
public void shouldStoreCounts() throws Exception {
// given
CountsOracle oracle = someData();
// when
try (Lifespan life = new Lifespan()) {
CountsTracker tracker = life.add(newTracker());
oracle.update(tracker, 2);
tracker.rotate(2);
}
// 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 shouldPickTheUncorruptedStoreWhenTruncatingAfterTheHeader.
@Test
public void shouldPickTheUncorruptedStoreWhenTruncatingAfterTheHeader() throws IOException {
/*
* The problem was that if we were succesfull in writing the header but failing immediately after, we would
* read 0 as counter for entry data and pick the corrupted store thinking that it was simply empty.
*/
Store store = createTestStore();
Pair<File, KeyValueStoreFile> file = store.rotationStrategy.create(EMPTY_DATA_PROVIDER, 1);
Pair<File, KeyValueStoreFile> next = store.rotationStrategy.next(file.first(), Headers.headersBuilder().put(TX_ID, (long) 42).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, 42);
}
}));
file.other().close();
File correct = next.first();
Pair<File, KeyValueStoreFile> nextNext = store.rotationStrategy.next(correct, Headers.headersBuilder().put(TX_ID, (long) 43).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, 42);
}
}, new Entry() {
@Override
public void write(WritableBuffer key, WritableBuffer value) {
key.putByte(0, (byte) 'b');
key.putByte(1, (byte) 'a');
key.putByte(2, (byte) 'r');
value.putInt(0, 4242);
}
}));
next.other().close();
File corrupted = nextNext.first();
nextNext.other().close();
try (StoreChannel channel = resourceManager.fileSystem().open(corrupted, "rw")) {
channel.truncate(16 * 4);
}
// then
try (Lifespan life = new Lifespan()) {
life.add(store);
assertNotNull(store.get("foo"));
assertEquals(42L, store.headers().get(TX_ID).longValue());
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class AbstractKeyValueStoreTest method shouldPickFileWithGreatestTransactionId.
@Test
public void shouldPickFileWithGreatestTransactionId() throws Exception {
try (Lifespan life = new Lifespan()) {
Store store = life.add(createTestStore());
// when
for (long txId = 2; txId <= 10; txId++) {
store.updater(txId).get().close();
store.prepareRotation(txId).rotate();
}
}
// then
try (Lifespan life = new Lifespan()) {
Store store = life.add(createTestStore());
assertEquals(10L, store.headers().get(TX_ID).longValue());
}
}
Aggregations