diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/FreeIdKeeper.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/FreeIdKeeper.java index 8309faa08a8a6..bac3985228255 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/FreeIdKeeper.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/FreeIdKeeper.java @@ -27,7 +27,7 @@ import org.neo4j.io.fs.StoreChannel; import org.neo4j.kernel.impl.store.UnderlyingStorageException; -import static org.neo4j.kernel.impl.store.id.IdFile.NO_RESULT; +import static org.neo4j.kernel.impl.store.id.IdContainer.NO_RESULT; /** * Instances of this class maintain a list of free ids with the potential to overflow to disk if the number diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdFile.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdContainer.java similarity index 96% rename from community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdFile.java rename to community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdContainer.java index 53b7d744df4b4..f319f08f474a8 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdFile.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdContainer.java @@ -29,7 +29,7 @@ import org.neo4j.kernel.impl.store.UnderlyingStorageException; -public class IdFile +public class IdContainer { public static final long NO_RESULT = -1; @@ -52,7 +52,7 @@ public class IdFile private long initialHighId; - public IdFile( FileSystemAbstraction fs, File file, int grabSize, boolean aggressiveReuse ) + public IdContainer( FileSystemAbstraction fs, File file, int grabSize, boolean aggressiveReuse ) { if ( grabSize < 1 ) { @@ -219,7 +219,7 @@ public void delete() } /** - * @return next free id or {@link IdFile#NO_RESULT} if not available + * @return next free id or {@link IdContainer#NO_RESULT} if not available */ public long getReusableId() { @@ -276,7 +276,7 @@ static void createEmptyIdFile( FileSystemAbstraction fs, File file, long highId, @Override public String toString() { - return "IdFile{" + "file=" + file + ", fs=" + fs + ", fileChannel=" + fileChannel + ", defragCount=" + + return "IdContainer{" + "file=" + file + ", fs=" + fs + ", fileChannel=" + fileChannel + ", defragCount=" + freeIdKeeper.getCount() + ", grabSize=" + grabSize + ", aggressiveReuse=" + aggressiveReuse + ", closed=" + closed + '}'; } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdGeneratorImpl.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdGeneratorImpl.java index d50c10689f7c1..6bfb7b6d75e5b 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdGeneratorImpl.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/id/IdGeneratorImpl.java @@ -67,9 +67,7 @@ public class IdGeneratorImpl implements IdGenerator public static final long INTEGER_MINUS_ONE = 0xFFFFFFFFL; // 4294967295L; private final long max; - - private IdFile idFile; - + private final IdContainer idContainer; private long highId = INTEGER_MINUS_ONE; /** @@ -102,10 +100,9 @@ public IdGeneratorImpl( FileSystemAbstraction fs, File file, int grabSize, long long highId ) { this.max = max; - - this.idFile = new IdFile( fs, file, grabSize, aggressiveReuse ); - this.idFile.init(); - this.highId = max( idFile.getInitialHighId(), highId ); + this.idContainer = new IdContainer( fs, file, grabSize, aggressiveReuse ); + this.idContainer.init(); + this.highId = max( idContainer.getInitialHighId(), highId ); } /** @@ -123,8 +120,8 @@ public IdGeneratorImpl( FileSystemAbstraction fs, File file, int grabSize, long public synchronized long nextId() { assertStillOpen(); - long nextDefragId = idFile.getReusableId(); - if ( nextDefragId != IdFile.NO_RESULT ) + long nextDefragId = idContainer.getReusableId(); + if ( nextDefragId != IdContainer.NO_RESULT ) { return nextDefragId; } @@ -147,7 +144,7 @@ public synchronized IdRange nextIdBatch( int size ) long[] defragIds = new long[size]; while ( count < size ) { - long id = idFile.getReusableId(); + long id = idContainer.getReusableId(); if ( id == -1 ) { break; @@ -213,7 +210,7 @@ public synchronized long getHighestPossibleIdInUse() @Override public synchronized void freeId( long id ) { - idFile.assertStillOpen(); + idContainer.assertStillOpen(); if ( IdValidator.isReservedId( id ) ) { @@ -224,7 +221,7 @@ public synchronized void freeId( long id ) { throw new IllegalArgumentException( "Illegal id[" + id + "], highId is " + highId ); } - idFile.freeId( id ); + idContainer.freeId( id ); } /** @@ -239,7 +236,7 @@ public synchronized void freeId( long id ) @Override public synchronized void close() { - idFile.close( highId ); + idContainer.close( highId ); } /** @@ -252,12 +249,12 @@ public synchronized void close() public static void createGenerator( FileSystemAbstraction fs, File fileName, long highId, boolean throwIfFileExists ) { - IdFile.createEmptyIdFile( fs, fileName, highId, throwIfFileExists ); + IdContainer.createEmptyIdFile( fs, fileName, highId, throwIfFileExists ); } public static long readHighId( FileSystemAbstraction fileSystem, File file ) throws IOException { - return IdFile.readHighId( fileSystem, file ); + return IdContainer.readHighId( fileSystem, file ); } @Override @@ -269,23 +266,23 @@ public synchronized long getNumberOfIdsInUse() @Override public synchronized long getDefragCount() { - return idFile.getFreeIdCount(); + return idContainer.getFreeIdCount(); } @Override public synchronized void delete() { - idFile.delete(); + idContainer.delete(); } private void assertStillOpen() { - idFile.assertStillOpen(); + idContainer.assertStillOpen(); } @Override public String toString() { - return "IdGeneratorImpl " + hashCode() + " [max=" + max + ", idFile=" + idFile + "]"; + return "IdGeneratorImpl " + hashCode() + " [max=" + max + ", idContainer=" + idContainer + "]"; } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/AbstractDynamicStoreTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/AbstractDynamicStoreTest.java index f3e4f3c0c9132..985a673ee3fbd 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/AbstractDynamicStoreTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/AbstractDynamicStoreTest.java @@ -83,6 +83,7 @@ public void dynamicRecordCursorReadsInUseRecords() DynamicRecord first = createDynamicRecord( 1, store, 0 ); DynamicRecord second = createDynamicRecord( 2, store, 0 ); DynamicRecord third = createDynamicRecord( 3, store, 10 ); + store.setHighId( 3 ); first.setNextBlock( second.getId() ); store.updateRecord( first ); @@ -108,6 +109,7 @@ public void dynamicRecordCursorReadsNotInUseRecords() DynamicRecord first = createDynamicRecord( 1, store, 0 ); DynamicRecord second = createDynamicRecord( 2, store, 0 ); DynamicRecord third = createDynamicRecord( 3, store, 10 ); + store.setHighId( 3 ); first.setNextBlock( second.getId() ); store.updateRecord( first ); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/FreeIdKeeperTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/FreeIdKeeperTest.java index 9a5ec16bf3b82..f400c2f4a46cb 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/FreeIdKeeperTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/FreeIdKeeperTest.java @@ -40,7 +40,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; -import static org.neo4j.kernel.impl.store.id.IdFile.NO_RESULT; +import static org.neo4j.kernel.impl.store.id.IdContainer.NO_RESULT; public class FreeIdKeeperTest { @@ -53,7 +53,7 @@ public void newlyConstructedInstanceShouldReportProperDefaultValues() throws Exc // Given StoreChannel channel = mock( StoreChannel.class ); int threshold = 10; - FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); + FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, threshold ); // when // then @@ -107,7 +107,7 @@ public void shouldOnlyOverflowWhenThresholdIsReached() throws Exception StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) ); int threshold = 10; - FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); + FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, threshold ); reset( channel ); // because we get the position in the constructor, we need to reset all calls on the spy // when @@ -249,7 +249,7 @@ public void shouldStoreAndRestoreIds() throws Exception StoreChannel channel = getStoreChannel(); int threshold = 10; - FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); + FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, threshold ); Set freeIds = new HashSet<>(); // stack guarantees are not maintained between restarts // when @@ -271,7 +271,7 @@ public void shouldStoreAndRestoreIds() throws Exception channel.close(); // and then we open a new one over the same file channel = fs.get().open( new File( "id.file" ), "rw" ); - keeper = getFreeIdKeeper( channel, threshold ); + keeper = getFreeIdKeeperAggressive( channel, threshold ); // then // the count should be returned correctly @@ -292,7 +292,7 @@ public void shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse() throws Exceptio StoreChannel channel = getStoreChannel(); int threshold = 10; - FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); + FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); // when keeper.freeId( 1 ); @@ -309,7 +309,7 @@ public void shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse() throws StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) ); int threshold = 10; - FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); + FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); // when // enough ids are persisted to overflow @@ -332,7 +332,7 @@ public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFals StoreChannel channel = getStoreChannel(); int threshold = 10; - FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); + FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); Set freeIds = new HashSet<>(); for ( long i = 0; i < threshold; i++ ) { @@ -343,7 +343,7 @@ public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFals channel.close(); // and then we open a new one over the same file channel = fs.get().open( new File( "id.file" ), "rw" ); - keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); + keeper = getFreeIdKeeper( channel, threshold ); // when // we release some ids that spill to disk @@ -372,7 +372,7 @@ public void shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFr StoreChannel channel = getStoreChannel(); int threshold = 10; - FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); + FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); Set freeIds = new HashSet<>(); for ( long i = 0; i < threshold; i++ ) { @@ -383,7 +383,7 @@ public void shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFr channel.close(); // and then we open a new one over the same file channel = fs.get().open( new File( "id.file" ), "rw" ); - keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); + keeper = getFreeIdKeeper( channel, threshold ); // when - then // we exhaust all ids restored diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/IdFileTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/IdContainerTest.java similarity index 70% rename from community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/IdFileTest.java rename to community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/IdContainerTest.java index 5950c4e1da643..6698fbb3a15cb 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/IdFileTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/id/IdContainerTest.java @@ -38,7 +38,7 @@ import static org.junit.Assert.fail; -public class IdFileTest +public class IdContainerTest { @Rule public final TestDirectory testDirectory = TestDirectory.testDirectory(); @@ -59,16 +59,16 @@ public void shouldDeleteIfOpen() throws Exception { // GIVEN createEmptyFile(); - IdFile idFile = new IdFile( fs, file, 100, false ); - idFile.init(); + IdContainer idContainer = new IdContainer( fs, file, 100, false ); + idContainer.init(); // WHEN - idFile.delete(); + idContainer.delete(); // THEN assertFalse( fs.fileExists( file ) ); - idFile.close( 0 ); + idContainer.close( 0 ); } @Test @@ -76,12 +76,12 @@ public void shouldDeleteIfClosed() throws Exception { // GIVEN createEmptyFile(); - IdFile idFile = new IdFile( fs, file, 100, false ); - idFile.init(); - idFile.close( 0 ); + IdContainer idContainer = new IdContainer( fs, file, 100, false ); + idContainer.init(); + idContainer.close( 0 ); // WHEN - idFile.delete(); + idContainer.delete(); // THEN assertFalse( fs.fileExists( file ) ); @@ -94,13 +94,13 @@ public void shouldForceStickyMark() throws Exception createEmptyFile(); // WHEN opening the id generator, where the jvm crashes right after - IdFile idFile = new IdFile( fs, file, 100, false ); - idFile.init(); + IdContainer idContainer = new IdContainer( fs, file, 100, false ); + idContainer.init(); // THEN try { - IdFile.readHighId( fs, file ); + IdContainer.readHighId( fs, file ); fail( "Should have thrown, saying something with sticky generator" ); } catch ( InvalidIdGeneratorException e ) @@ -109,7 +109,7 @@ public void shouldForceStickyMark() throws Exception } finally { - idFile.close( 0 ); + idContainer.close( 0 ); } } @@ -117,31 +117,31 @@ public void shouldForceStickyMark() throws Exception public void shouldTruncateTheFileIfOverwriting() throws Exception { // GIVEN - IdFile.createEmptyIdFile( fs, file, 30, false ); - IdFile idFile = new IdFile( fs, file, 5, false ); - idFile.init(); + IdContainer.createEmptyIdFile( fs, file, 30, false ); + IdContainer idContainer = new IdContainer( fs, file, 5, false ); + idContainer.init(); for ( int i = 0; i < 17; i++ ) { - idFile.freeId( i ); + idContainer.freeId( i ); } - idFile.close( 30 ); - assertThat( (int) fs.getFileSize( file ), greaterThan( IdFile.HEADER_SIZE ) ); + idContainer.close( 30 ); + assertThat( (int) fs.getFileSize( file ), greaterThan( IdContainer.HEADER_SIZE ) ); // WHEN - IdFile.createEmptyIdFile( fs, file, 30, false ); + IdContainer.createEmptyIdFile( fs, file, 30, false ); // THEN - assertEquals( IdFile.HEADER_SIZE, (int) fs.getFileSize( file ) ); - assertEquals( 30, IdFile.readHighId( fs, file ) ); - idFile = new IdFile( fs, file, 5, false ); - idFile.init(); - assertEquals( 30, idFile.getInitialHighId() ); + assertEquals( IdContainer.HEADER_SIZE, (int) fs.getFileSize( file ) ); + assertEquals( 30, IdContainer.readHighId( fs, file ) ); + idContainer = new IdContainer( fs, file, 5, false ); + idContainer.init(); + assertEquals( 30, idContainer.getInitialHighId() ); - idFile.close( 30 ); + idContainer.close( 30 ); } private void createEmptyFile() { - IdFile.createEmptyIdFile( fs, file, 42, false ); + IdContainer.createEmptyIdFile( fs, file, 42, false ); } } diff --git a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/core/state/machines/id/ReplicatedIdGenerator.java b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/core/state/machines/id/ReplicatedIdGenerator.java index 9c018770ed5ea..34417b2d1ef73 100644 --- a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/core/state/machines/id/ReplicatedIdGenerator.java +++ b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/core/state/machines/id/ReplicatedIdGenerator.java @@ -23,7 +23,7 @@ import java.util.function.BooleanSupplier; import org.neo4j.io.fs.FileSystemAbstraction; -import org.neo4j.kernel.impl.store.id.IdFile; +import org.neo4j.kernel.impl.store.id.IdContainer; import org.neo4j.kernel.impl.store.id.IdGenerator; import org.neo4j.kernel.impl.store.id.IdRange; import org.neo4j.kernel.impl.store.id.IdType; @@ -43,7 +43,7 @@ class ReplicatedIdGenerator implements IdGenerator private volatile long highId; private volatile IdRangeIterator idQueue = EMPTY_ID_RANGE_ITERATOR; - private IdFile idFile; + private IdContainer idContainer; ReplicatedIdGenerator( FileSystemAbstraction fs, File file, IdType idType, long highId, ReplicatedIdRangeAcquirer acquirer, LogProvider logProvider, int grabSize, boolean aggressiveReuse, @@ -54,14 +54,14 @@ class ReplicatedIdGenerator implements IdGenerator this.acquirer = acquirer; this.log = logProvider.getLog( getClass() ); this.freeIdCondition = freeIdCondition; - idFile = new IdFile( fs, file, grabSize, aggressiveReuse ); - idFile.init(); + idContainer = new IdContainer( fs, file, grabSize, aggressiveReuse ); + idContainer.init(); } @Override public void close() { - idFile.close( highId ); + idContainer.close( highId ); } @Override @@ -69,7 +69,7 @@ public void freeId( long id ) { if ( freeIdCondition.getAsBoolean() ) { - idFile.freeId( id ); + idContainer.freeId( id ); } } @@ -94,14 +94,14 @@ public long getHighestPossibleIdInUse() @Override public long getNumberOfIdsInUse() { - return highId - idFile.getFreeIdCount(); + return highId - idContainer.getFreeIdCount(); } @Override public synchronized long nextId() { - long id = idFile.getReusableId(); - if ( id != IdFile.NO_RESULT ) + long id = idContainer.getReusableId(); + if ( id != IdContainer.NO_RESULT ) { return id; } @@ -154,13 +154,13 @@ private IdRange respectingHighId( IdRange idRange ) @Override public long getDefragCount() { - return idFile.getFreeIdCount(); + return idContainer.getFreeIdCount(); } @Override public void delete() { - idFile.delete(); + idContainer.delete(); } @Override