Search in sources :

Example 6 with IdGeneratorImpl

use of org.neo4j.kernel.impl.store.id.IdGeneratorImpl in project neo4j by neo4j.

the class IdGeneratorTest method testStickyGenerator.

@Test
public void testStickyGenerator() {
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGen = new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
        try {
            new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
            fail("Opening sticky id generator should throw exception");
        } catch (StoreFailureException e) {
        // good
        }
        closeIdGenerator(idGen);
    } finally {
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) File(java.io.File) Test(org.junit.Test)

Example 7 with IdGeneratorImpl

use of org.neo4j.kernel.impl.store.id.IdGeneratorImpl in project neo4j by neo4j.

the class IdGeneratorTest method testNextId.

@Test
public void testNextId() {
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
        for (long i = 0; i < 7; i++) {
            assertEquals(i, idGenerator.nextId());
        }
        idGenerator.freeId(1);
        idGenerator.freeId(3);
        idGenerator.freeId(5);
        assertEquals(7L, idGenerator.nextId());
        idGenerator.freeId(6);
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 5, 1000, false, 0);
        idGenerator.freeId(2);
        idGenerator.freeId(4);
        assertEquals(1L, idGenerator.nextId());
        idGenerator.freeId(1);
        assertEquals(3L, idGenerator.nextId());
        idGenerator.freeId(3);
        assertEquals(5L, idGenerator.nextId());
        idGenerator.freeId(5);
        assertEquals(6L, idGenerator.nextId());
        idGenerator.freeId(6);
        assertEquals(8L, idGenerator.nextId());
        idGenerator.freeId(8);
        assertEquals(9L, idGenerator.nextId());
        idGenerator.freeId(9);
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
        assertEquals(2L, idGenerator.nextId());
        assertEquals(4L, idGenerator.nextId());
        assertEquals(1L, idGenerator.nextId());
        assertEquals(3L, idGenerator.nextId());
        assertEquals(5L, idGenerator.nextId());
        assertEquals(6L, idGenerator.nextId());
        assertEquals(8L, idGenerator.nextId());
        assertEquals(9L, idGenerator.nextId());
        assertEquals(10L, idGenerator.nextId());
        assertEquals(11L, idGenerator.nextId());
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) File(java.io.File) Test(org.junit.Test)

Example 8 with IdGeneratorImpl

use of org.neo4j.kernel.impl.store.id.IdGeneratorImpl in project neo4j by neo4j.

the class IdGeneratorTest method grabSizeCannotBeNegative.

@Test(expected = IllegalArgumentException.class)
public void grabSizeCannotBeNegative() throws Exception {
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    new IdGeneratorImpl(fs, idGeneratorFile(), -1, 100, false, 0).close();
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) Test(org.junit.Test)

Example 9 with IdGeneratorImpl

use of org.neo4j.kernel.impl.store.id.IdGeneratorImpl in project neo4j by neo4j.

the class IdGeneratorTest method testOddAndEvenWorstCase.

@Test
public void testOddAndEvenWorstCase() {
    int capacity = 1024 * 8 + 1;
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 128, capacity * 2, false, 0);
        for (int i = 0; i < capacity; i++) {
            idGenerator.nextId();
        }
        Map<Long, Object> freedIds = new HashMap<>();
        for (long i = 1; i < capacity; i += 2) {
            idGenerator.freeId(i);
            freedIds.put(i, this);
        }
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 2000, capacity * 2, false, 0);
        long oldId = -1;
        for (int i = 0; i < capacity - 1; i += 2) {
            long id = idGenerator.nextId();
            if (freedIds.remove(id) == null) {
                throw new RuntimeException("Id=" + id + " prevId=" + oldId + " list.size()=" + freedIds.size());
            }
            oldId = id;
        }
        assertTrue(freedIds.values().size() == 0);
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (fs.fileExists(file)) {
            assertTrue(fs.deleteFile(file));
        }
    }
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 128, capacity * 2, false, 0);
        for (int i = 0; i < capacity; i++) {
            idGenerator.nextId();
        }
        Map<Long, Object> freedIds = new HashMap<>();
        for (long i = 0; i < capacity; i += 2) {
            idGenerator.freeId(i);
            freedIds.put(i, this);
        }
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 2000, capacity * 2, false, 0);
        for (int i = 0; i < capacity; i += 2) {
            assertEquals(this, freedIds.remove(idGenerator.nextId()));
        }
        assertEquals(0, freedIds.values().size());
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : HashMap(java.util.HashMap) IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) File(java.io.File) Test(org.junit.Test)

Example 10 with IdGeneratorImpl

use of org.neo4j.kernel.impl.store.id.IdGeneratorImpl in project neo4j by neo4j.

the class FreeIdsAfterRecoveryTest method shouldCompletelyRebuildIdGeneratorsAfterCrash.

@Test
public void shouldCompletelyRebuildIdGeneratorsAfterCrash() throws Exception {
    // GIVEN
    StoreFactory storeFactory = new StoreFactory(directory.directory(), pageCacheRule.getPageCache(fileSystemRule.get()), fileSystemRule.get(), NullLogProvider.getInstance());
    long highId;
    try (NeoStores stores = storeFactory.openAllNeoStores(true)) {
        // a node store with a "high" node
        NodeStore nodeStore = stores.getNodeStore();
        nodeStore.setHighId(20);
        nodeStore.updateRecord(node(nodeStore.nextId()));
        highId = nodeStore.getHighId();
    }
    // populating its .id file with a bunch of ids
    File nodeIdFile = new File(directory.directory(), StoreFile.NODE_STORE.fileName(StoreFileType.ID));
    IdGeneratorImpl idGenerator = new IdGeneratorImpl(fileSystemRule.get(), nodeIdFile, 10, 10_000, false, highId);
    for (long id = 0; id < 15; id++) {
        idGenerator.freeId(id);
    }
    idGenerator.close();
    // marking as sticky to simulate a crash
    try (StoreChannel channel = fileSystemRule.get().open(nodeIdFile, "rw")) {
        markAsSticky(channel, ByteBuffer.allocate(HEADER_SIZE));
    }
    // WHEN
    try (NeoStores stores = storeFactory.openAllNeoStores(true)) {
        NodeStore nodeStore = stores.getNodeStore();
        assertFalse(nodeStore.getStoreOk());
        // simulating what recovery does
        nodeStore.deleteIdGenerator();
        // recovery happens here...
        nodeStore.makeStoreOk();
        // THEN
        assertEquals(highId, nodeStore.nextId());
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) File(java.io.File) StoreFile(org.neo4j.kernel.impl.storemigration.StoreFile) Test(org.junit.Test)

Aggregations

IdGeneratorImpl (org.neo4j.kernel.impl.store.id.IdGeneratorImpl)18 Test (org.junit.Test)15 IdGenerator (org.neo4j.kernel.impl.store.id.IdGenerator)14 File (java.io.File)10 StoreChannel (org.neo4j.io.fs.StoreChannel)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 NodeRecordFormat (org.neo4j.kernel.impl.store.format.standard.NodeRecordFormat)1 PropertyKeyTokenRecordFormat (org.neo4j.kernel.impl.store.format.standard.PropertyKeyTokenRecordFormat)1 StoreFile (org.neo4j.kernel.impl.storemigration.StoreFile)1