use of org.neo4j.io.layout.DatabaseLayout in project neo4j by neo4j.
the class StoreUpgrader method migrateIfNeeded.
/**
* Upgrade the store format, if it is not the latest version or is different from the configured desired format.
*
* @param layout The layout of the existing database store.
* @param forceUpgrade If {@code true}, the value of the {@link GraphDatabaseSettings#allow_upgrade} setting is ignored.
*/
public void migrateIfNeeded(DatabaseLayout layout, boolean forceUpgrade) throws IOException {
// nothing to migrate
if (!Files.exists(layout.databaseDirectory())) {
return;
}
try (var cursorContext = new CursorContext(pageCacheTracer.createPageCursorTracer(STORE_UPGRADE_TAG))) {
DatabaseLayout migrationStructure = DatabaseLayout.ofFlat(layout.file(MIGRATION_DIRECTORY));
cleanupLegacyLeftOverDirsIn(layout.databaseDirectory());
Path migrationStateFile = migrationStructure.file(MIGRATION_STATUS_FILE);
// if migration directory exists than we might have failed to move files into the store dir so do it again
if (hasCurrentVersion(storeVersionCheck, cursorContext) && !fileSystem.fileExists(migrationStateFile)) {
// No migration needed
return;
}
if (isUpgradeAllowed() || forceUpgrade) {
migrate(layout, migrationStructure, migrationStateFile, cursorContext);
} else {
Optional<String> storeVersion = storeVersionCheck.storeVersion(cursorContext);
if (storeVersion.isPresent()) {
StoreVersion version = storeVersionCheck.versionInformation(storeVersion.get());
if (version.hasCapability(IndexCapabilities.LuceneCapability.LUCENE_5)) {
throw new UpgradeNotAllowedException("Upgrade is required to migrate store to new major version.");
} else {
String configuredVersion = storeVersionCheck.configuredVersion();
if (configuredVersion != null && !version.isCompatibleWith(storeVersionCheck.versionInformation(configuredVersion))) {
throw new UpgradeNotAllowedException();
}
}
}
}
}
}
use of org.neo4j.io.layout.DatabaseLayout in project neo4j by neo4j.
the class FulltextIndexProviderTest method indexWithUnknownAnalyzerWillBeMarkedAsFailedOnStartup.
@Test
void indexWithUnknownAnalyzerWillBeMarkedAsFailedOnStartup() throws Exception {
// Create a full-text index.
long indexId;
try (KernelTransactionImplementation transaction = getKernelTransaction()) {
int[] propertyIds = { propIdHa };
SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.NODE, new int[] { labelIdHa }, propertyIds);
IndexPrototype prototype = IndexPrototype.forSchema(schema).withIndexType(FULLTEXT).withName(NAME);
SchemaWrite schemaWrite = transaction.schemaWrite();
IndexDescriptor index = schemaWrite.indexCreate(prototype);
indexId = index.getId();
transaction.success();
}
// Modify the full-text index such that it has an analyzer configured that does not exist.
controller.restartDbms(builder -> {
var cacheTracer = NULL;
FileSystemAbstraction fs = builder.getFileSystem();
DatabaseLayout databaseLayout = Neo4jLayout.of(builder.getHomeDirectory()).databaseLayout(DEFAULT_DATABASE_NAME);
DefaultIdGeneratorFactory idGenFactory = new DefaultIdGeneratorFactory(fs, RecoveryCleanupWorkCollector.ignore(), databaseLayout.getDatabaseName());
try (JobScheduler scheduler = JobSchedulerFactory.createInitialisedScheduler();
PageCache pageCache = StandalonePageCacheFactory.createPageCache(fs, scheduler, cacheTracer)) {
StoreFactory factory = new StoreFactory(databaseLayout, Config.defaults(), idGenFactory, pageCache, fs, NullLogProvider.getInstance(), cacheTracer, writable());
var cursorContext = CursorContext.NULL;
try (NeoStores neoStores = factory.openAllNeoStores(false)) {
TokenHolders tokens = StoreTokens.readOnlyTokenHolders(neoStores, CursorContext.NULL);
SchemaStore schemaStore = neoStores.getSchemaStore();
SchemaStorage storage = new SchemaStorage(schemaStore, tokens, () -> KernelVersion.LATEST);
IndexDescriptor index = (IndexDescriptor) storage.loadSingleSchemaRule(indexId, CursorContext.NULL);
Map<String, Value> indexConfigMap = new HashMap<>(index.getIndexConfig().asMap());
for (Map.Entry<String, Value> entry : indexConfigMap.entrySet()) {
if (entry.getKey().contains("analyzer")) {
// This analyzer does not exist!
entry.setValue(Values.stringValue("bla-bla-lyzer"));
}
}
index = index.withIndexConfig(IndexConfig.with(indexConfigMap));
storage.writeSchemaRule(index, cursorContext, INSTANCE);
schemaStore.flush(cursorContext);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return builder;
});
// Verify that the index comes up in a failed state.
try (Transaction tx = db.beginTx()) {
IndexDefinition index = tx.schema().getIndexByName(NAME);
Schema.IndexState indexState = tx.schema().getIndexState(index);
assertThat(indexState).isEqualTo(Schema.IndexState.FAILED);
String indexFailure = tx.schema().getIndexFailure(index);
assertThat(indexFailure).contains("bla-bla-lyzer");
}
// Verify that the failed index can be dropped.
try (Transaction tx = db.beginTx()) {
tx.schema().getIndexByName(NAME).drop();
assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
tx.commit();
}
try (Transaction tx = db.beginTx()) {
assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
}
controller.restartDbms();
try (Transaction tx = db.beginTx()) {
assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
}
}
use of org.neo4j.io.layout.DatabaseLayout in project neo4j by neo4j.
the class NeoStoresTest method reinitializeStores.
private void reinitializeStores(DatabaseLayout databaseLayout) {
Dependencies dependencies = new Dependencies();
Config config = Config.defaults(GraphDatabaseSettings.fail_on_missing_files, false);
dependencies.satisfyDependency(config);
closeStorageEngine();
IdGeneratorFactory idGeneratorFactory = new DefaultIdGeneratorFactory(fs, immediate(), databaseLayout.getDatabaseName());
TokenHolders tokenHolders = new TokenHolders(createReadOnlyTokenHolder(TokenHolder.TYPE_PROPERTY_KEY), createReadOnlyTokenHolder(TokenHolder.TYPE_LABEL), createReadOnlyTokenHolder(TokenHolder.TYPE_RELATIONSHIP_TYPE));
storageEngine = new RecordStorageEngine(databaseLayout, config, pageCache, fs, nullLogProvider(), tokenHolders, new DatabaseSchemaState(nullLogProvider()), new StandardConstraintRuleAccessor(), i -> i, NO_LOCK_SERVICE, mock(Health.class), idGeneratorFactory, new DefaultIdController(), immediate(), PageCacheTracer.NULL, true, INSTANCE, writable(), CommandLockVerification.Factory.IGNORE, LockVerificationMonitor.Factory.IGNORE);
life = new LifeSupport();
life.add(storageEngine);
life.add(storageEngine.schemaAndTokensLifecycle());
life.start();
NeoStores neoStores = storageEngine.testAccessNeoStores();
pStore = neoStores.getPropertyStore();
nodeStore = neoStores.getNodeStore();
storageReader = storageEngine.newReader();
}
use of org.neo4j.io.layout.DatabaseLayout in project neo4j by neo4j.
the class TokenScanWriteMonitor method dump.
public static void dump(FileSystemAbstraction fs, DatabaseLayout databaseLayout, Dumper dumper, TxFilter txFilter, EntityType entityType) throws IOException {
Path writeLogFile = writeLogBaseFile(databaseLayout, entityType);
String writeLogFileBaseName = writeLogFile.getFileName().toString();
Path[] files = fs.listFiles(databaseLayout.databaseDirectory(), name -> name.getFileName().toString().startsWith(writeLogFileBaseName));
Arrays.sort(files, comparing(file -> file.getFileName().toString().equals(writeLogFileBaseName) ? 0 : millisOf(file)));
long session = 0;
for (Path file : files) {
dumper.file(file);
session = dumpFile(fs, file, dumper, txFilter, session);
}
}
use of org.neo4j.io.layout.DatabaseLayout in project neo4j by neo4j.
the class DatabaseStartupTest method startDatabaseWithWrongTransactionFilesShouldFail.
@Test
void startDatabaseWithWrongTransactionFilesShouldFail() throws IOException {
// Create a store
DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
DatabaseLayout databaseLayout = db.databaseLayout();
try (Transaction tx = db.beginTx()) {
tx.createNode();
tx.commit();
}
managementService.shutdown();
// Change store id component
try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
ThreadPoolJobScheduler scheduler = new ThreadPoolJobScheduler();
PageCache pageCache = createPageCache(fileSystem, scheduler, PageCacheTracer.NULL)) {
long newTime = System.currentTimeMillis() + 1;
MetaDataStore.setRecord(pageCache, databaseLayout.metadataStore(), MetaDataStore.Position.TIME, newTime, databaseLayout.getDatabaseName(), NULL);
}
// Try to start
managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
try {
db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
assertFalse(db.isAvailable(10));
DatabaseStateService dbStateService = db.getDependencyResolver().resolveDependency(DatabaseStateService.class);
Optional<Throwable> cause = dbStateService.causeOfFailure(db.databaseId());
assertTrue(cause.isPresent());
assertTrue(cause.get().getCause().getMessage().contains("Mismatching store id"));
} finally {
managementService.shutdown();
}
}
Aggregations