Search in sources :

Example 6 with IdGeneratorFactory

use of org.neo4j.internal.id.IdGeneratorFactory in project neo4j by neo4j.

the class IdGeneratorMigrator method migrateIdFiles.

private void migrateIdFiles(DatabaseLayout directoryLayout, DatabaseLayout migrationLayout, RecordFormats oldFormat, RecordFormats newFormat, ProgressReporter progress, CursorContext cursorContext) throws IOException {
    // The store .id files needs to be migrated. At this point some of them have been sort-of-migrated, i.e. merely ported
    // to the new format, but just got the highId and nothing else. Regardless we want to do a proper migration here,
    // which not only means creating an empty id file w/ only highId. No, it also means scanning the stores and properly
    // updating its freelist so that from this point no ids will be lost, ever.
    // For every store type: if store is in migrationLayout use that, else use the one from the dbLayout because it will
    // be of the current format already. Then scan it and create the .id file in the migration layout.
    List<StoreType> storesInDbDirectory = new ArrayList<>();
    List<StoreType> storesInMigrationDirectory = new ArrayList<>();
    for (StoreType storeType : StoreType.values()) {
        // See if it exists in migration directory, otherwise it must be in the db directory
        List<StoreType> list = fileSystem.fileExists(migrationLayout.file(storeType.getDatabaseFile())) ? storesInMigrationDirectory : storesInDbDirectory;
        list.add(storeType);
    }
    progress.start(storesInDbDirectory.size() + storesInMigrationDirectory.size());
    // Rebuild the .id files from the legacy stores that haven't been upgraded, i.e. if they remained unchanged
    // Make them end up in upgrade/<store>.id so that they won't overwrite the origin .id file before the upgrade is completed
    IdGeneratorFactory rebuiltIdGeneratorsFromOldStore = new DefaultIdGeneratorFactory(fileSystem, immediate(), directoryLayout.getDatabaseName()) {

        @Override
        public IdGenerator open(PageCache pageCache, Path filename, IdType idType, LongSupplier highIdScanner, long maxId, DatabaseReadOnlyChecker readOnlyChecker, Config config, CursorContext cursorContext, ImmutableSet<OpenOption> openOptions) throws IOException {
            Path redirectedFilename = migrationLayout.databaseDirectory().resolve(filename.getFileName().toString());
            return super.open(pageCache, redirectedFilename, idType, highIdScanner, maxId, readOnlyChecker, config, cursorContext, openOptions);
        }

        @Override
        public IdGenerator create(PageCache pageCache, Path fileName, IdType idType, long highId, boolean throwIfFileExists, long maxId, DatabaseReadOnlyChecker readOnlyChecker, Config config, CursorContext cursorContext, ImmutableSet<OpenOption> openOptions) {
            throw new IllegalStateException("The store file should exist and therefore all calls should be to open, not create");
        }
    };
    startAndTriggerRebuild(directoryLayout, oldFormat, rebuiltIdGeneratorsFromOldStore, storesInDbDirectory, progress, cursorContext);
    // Build the ones from the migration directory, those stores that have been migrated
    // Before doing this we will have to create empty stores for those that are missing, otherwise some of the stores
    // that we need to open will complain because some of their "sub-stores" doesn't exist. They will be empty, it's fine...
    // and we will not read from them at all. They will just sit there and allow their parent store to be opened.
    // We'll remove them after we have built the id files
    IdGeneratorFactory rebuiltIdGeneratorsFromNewStore = new DefaultIdGeneratorFactory(fileSystem, immediate(), migrationLayout.getDatabaseName());
    Set<Path> placeHolderStoreFiles = createEmptyPlaceHolderStoreFiles(migrationLayout, newFormat);
    startAndTriggerRebuild(migrationLayout, newFormat, rebuiltIdGeneratorsFromNewStore, storesInMigrationDirectory, progress, cursorContext);
    for (Path emptyPlaceHolderStoreFile : placeHolderStoreFiles) {
        fileSystem.deleteFile(emptyPlaceHolderStoreFile);
    }
}
Also used : Path(java.nio.file.Path) Config(org.neo4j.configuration.Config) ArrayList(java.util.ArrayList) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) IdGeneratorFactory(org.neo4j.internal.id.IdGeneratorFactory) ScanOnOpenReadOnlyIdGeneratorFactory(org.neo4j.internal.id.ScanOnOpenReadOnlyIdGeneratorFactory) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) CursorContext(org.neo4j.io.pagecache.context.CursorContext) IdType(org.neo4j.internal.id.IdType) StoreType(org.neo4j.kernel.impl.store.StoreType) ImmutableSet(org.eclipse.collections.api.set.ImmutableSet) DatabaseReadOnlyChecker(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker) LongSupplier(java.util.function.LongSupplier) PageCache(org.neo4j.io.pagecache.PageCache)

Example 7 with IdGeneratorFactory

use of org.neo4j.internal.id.IdGeneratorFactory in project neo4j by neo4j.

the class BigStoreIT method setHighId.

private void setHighId(IdType type, long highId) {
    IdGeneratorFactory idGeneratorFactory = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(IdGeneratorFactory.class);
    IdGenerator idGenerator = idGeneratorFactory.get(type);
    idGenerator.setHighId(highId);
    idGenerator.markHighestWrittenAtHighId();
}
Also used : IdGeneratorFactory(org.neo4j.internal.id.IdGeneratorFactory) IdGenerator(org.neo4j.internal.id.IdGenerator)

Example 8 with IdGeneratorFactory

use of org.neo4j.internal.id.IdGeneratorFactory in project neo4j by neo4j.

the class NeoStoresTest method newStoreFactory.

private static StoreFactory newStoreFactory(DatabaseLayout databaseLayout, PageCache pageCache, FileSystemAbstraction fs) {
    RecordFormats recordFormats = RecordFormatSelector.defaultFormat();
    Config config = Config.defaults();
    IdGeneratorFactory idGeneratorFactory = new DefaultIdGeneratorFactory(fs, immediate(), databaseLayout.getDatabaseName());
    return new StoreFactory(databaseLayout, config, idGeneratorFactory, pageCache, fs, recordFormats, LOG_PROVIDER, PageCacheTracer.NULL, writable(), immutable.empty());
}
Also used : RecordFormats(org.neo4j.kernel.impl.store.format.RecordFormats) Config(org.neo4j.configuration.Config) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) IdGeneratorFactory(org.neo4j.internal.id.IdGeneratorFactory)

Example 9 with IdGeneratorFactory

use of org.neo4j.internal.id.IdGeneratorFactory in project neo4j by neo4j.

the class DatabaseFileListingTest method shouldCloseIndexSnapshots.

@Test
void shouldCloseIndexSnapshots() throws Exception {
    // Given
    IndexingService indexingService = mock(IndexingService.class);
    DatabaseLayout databaseLayout = mock(DatabaseLayout.class);
    when(databaseLayout.metadataStore()).thenReturn(mock(Path.class));
    LogFiles logFiles = mock(LogFiles.class);
    filesInStoreDirAre(databaseLayout, STANDARD_STORE_DIR_FILES, STANDARD_STORE_DIR_DIRECTORIES);
    StorageEngine storageEngine = mock(StorageEngine.class);
    IdGeneratorFactory idGeneratorFactory = mock(IdGeneratorFactory.class);
    DatabaseFileListing fileListing = new DatabaseFileListing(databaseLayout, logFiles, indexingService, storageEngine, idGeneratorFactory);
    ResourceIterator<Path> indexSnapshot = indexFilesAre(indexingService, new String[] { "schema/index/my.index" });
    ResourceIterator<StoreFileMetadata> result = fileListing.builder().excludeLogFiles().build();
    // When
    result.close();
    // Then
    verify(indexSnapshot).close();
}
Also used : Path(java.nio.file.Path) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) IdGeneratorFactory(org.neo4j.internal.id.IdGeneratorFactory) StoreFileMetadata(org.neo4j.storageengine.api.StoreFileMetadata) StorageEngine(org.neo4j.storageengine.api.StorageEngine) Test(org.junit.jupiter.api.Test)

Example 10 with IdGeneratorFactory

use of org.neo4j.internal.id.IdGeneratorFactory in project neo4j by neo4j.

the class IdContextFactoryBuilderTest method createContextWithCustomIdGeneratorFactoryWhenProvided.

@Test
void createContextWithCustomIdGeneratorFactoryWhenProvided() throws IOException {
    IdGeneratorFactory idGeneratorFactory = mock(IdGeneratorFactory.class);
    Config config = defaults();
    IdContextFactory contextFactory = IdContextFactoryBuilder.of(fs, jobScheduler, config, PageCacheTracer.NULL).withIdGenerationFactoryProvider(any -> idGeneratorFactory).build();
    DatabaseIdContext idContext = contextFactory.createIdContext(databaseIdRepository.getByName("database").get());
    IdGeneratorFactory bufferedGeneratorFactory = idContext.getIdGeneratorFactory();
    assertThat(idContext.getIdController()).isInstanceOf(BufferedIdController.class);
    assertThat(bufferedGeneratorFactory).isInstanceOf(BufferingIdGeneratorFactory.class);
    ((BufferingIdGeneratorFactory) bufferedGeneratorFactory).initialize(() -> mock(KernelTransactionsSnapshot.class));
    Path file = testDirectory.file("a");
    IdType idType = IdType.NODE;
    LongSupplier highIdSupplier = () -> 0;
    int maxId = 100;
    idGeneratorFactory.open(pageCache, file, idType, highIdSupplier, maxId, writable(), config, NULL, immutable.empty());
    verify(idGeneratorFactory).open(pageCache, file, idType, highIdSupplier, maxId, writable(), config, NULL, immutable.empty());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Sets.immutable(org.eclipse.collections.api.factory.Sets.immutable) LongSupplier(java.util.function.LongSupplier) IdGeneratorFactory(org.neo4j.internal.id.IdGeneratorFactory) Config.defaults(org.neo4j.configuration.Config.defaults) DatabaseIdFactory.from(org.neo4j.kernel.database.DatabaseIdFactory.from) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Config(org.neo4j.configuration.Config) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) Function(java.util.function.Function) BufferedIdController(org.neo4j.internal.id.BufferedIdController) TestDatabaseIdRepository(org.neo4j.kernel.database.TestDatabaseIdRepository) Inject(org.neo4j.test.extension.Inject) IdGenerator(org.neo4j.internal.id.IdGenerator) PageCacheTracer(org.neo4j.io.pagecache.tracing.PageCacheTracer) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) BufferingIdGeneratorFactory(org.neo4j.internal.id.BufferingIdGeneratorFactory) JobScheduler(org.neo4j.scheduler.JobScheduler) Path(java.nio.file.Path) DatabaseIdRepository(org.neo4j.kernel.database.DatabaseIdRepository) PageCache(org.neo4j.io.pagecache.PageCache) PageCacheExtension(org.neo4j.test.extension.pagecache.PageCacheExtension) DatabaseReadOnlyChecker.writable(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker.writable) TestDirectory(org.neo4j.test.rule.TestDirectory) IOException(java.io.IOException) UUID(java.util.UUID) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) IdType(org.neo4j.internal.id.IdType) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) KernelTransactionsSnapshot(org.neo4j.kernel.impl.api.KernelTransactionsSnapshot) Mockito.mock(org.mockito.Mockito.mock) Path(java.nio.file.Path) Config(org.neo4j.configuration.Config) BufferingIdGeneratorFactory(org.neo4j.internal.id.BufferingIdGeneratorFactory) IdGeneratorFactory(org.neo4j.internal.id.IdGeneratorFactory) BufferingIdGeneratorFactory(org.neo4j.internal.id.BufferingIdGeneratorFactory) LongSupplier(java.util.function.LongSupplier) KernelTransactionsSnapshot(org.neo4j.kernel.impl.api.KernelTransactionsSnapshot) IdType(org.neo4j.internal.id.IdType) Test(org.junit.jupiter.api.Test)

Aggregations

IdGeneratorFactory (org.neo4j.internal.id.IdGeneratorFactory)15 Path (java.nio.file.Path)9 DefaultIdGeneratorFactory (org.neo4j.internal.id.DefaultIdGeneratorFactory)9 Config (org.neo4j.configuration.Config)8 Test (org.junit.jupiter.api.Test)6 LongSupplier (java.util.function.LongSupplier)5 IdType (org.neo4j.internal.id.IdType)5 PageCache (org.neo4j.io.pagecache.PageCache)5 IOException (java.io.IOException)4 BufferedIdController (org.neo4j.internal.id.BufferedIdController)4 BufferingIdGeneratorFactory (org.neo4j.internal.id.BufferingIdGeneratorFactory)4 OpenOption (java.nio.file.OpenOption)3 Sets.immutable (org.eclipse.collections.api.factory.Sets.immutable)3 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)3 Mockito.mock (org.mockito.Mockito.mock)3 DatabaseReadOnlyChecker (org.neo4j.configuration.helpers.DatabaseReadOnlyChecker)3 DatabaseReadOnlyChecker.writable (org.neo4j.configuration.helpers.DatabaseReadOnlyChecker.writable)3 IdGenerator (org.neo4j.internal.id.IdGenerator)3 CursorContext (org.neo4j.io.pagecache.context.CursorContext)3 NULL (org.neo4j.io.pagecache.context.CursorContext.NULL)3