Search in sources :

Example 1 with UpgradeNotAllowedException

use of org.neo4j.storageengine.migration.UpgradeNotAllowedException in project neo4j by neo4j.

the class LogsUpgrader method upgrade.

public void upgrade(DatabaseLayout layout) {
    CommandReaderFactory commandReaderFactory = storageEngineFactory.commandReaderFactory();
    try (MetadataProvider store = getMetaDataStore()) {
        TransactionLogInitializer logInitializer = new TransactionLogInitializer(fs, store, commandReaderFactory, tracer);
        Path transactionLogsDirectory = layout.getTransactionLogsDirectory();
        Path legacyLogsDirectory = legacyLogsLocator.getTransactionLogsDirectory();
        boolean filesNeedsToMove = !transactionLogsDirectory.equals(legacyLogsDirectory);
        LogFiles logFiles = LogFilesBuilder.logFilesBasedOnlyBuilder(legacyLogsDirectory, fs).withCommandReaderFactory(commandReaderFactory).build();
        // Move log files to their intended directory, if they are not there already.
        Path[] legacyFiles = logFiles.logFiles();
        if (legacyFiles != null && legacyFiles.length > 0) {
            if (filesNeedsToMove) {
                for (Path legacyFile : legacyFiles) {
                    fs.copyFile(legacyFile, transactionLogsDirectory.resolve(legacyFile.getFileName()), EMPTY_COPY_OPTIONS);
                }
            }
            logInitializer.initializeExistingLogFiles(layout, transactionLogsDirectory, UPGRADE_CHECKPOINT);
            if (filesNeedsToMove) {
                for (Path legacyFile : legacyFiles) {
                    fs.deleteFile(legacyFile);
                }
            }
        } else {
            // We didn't find any files in the legacy location.
            // If the legacy location is the same as the intended location, then the log files are missing entirely.
            // Otherwise, we will have to check if the log files are already present in the intended location and try to initialize them there.
            logFiles = LogFilesBuilder.logFilesBasedOnlyBuilder(transactionLogsDirectory, fs).build();
            legacyFiles = logFiles.logFiles();
            if (legacyFiles != null && legacyFiles.length > 0) {
                // The log files are already at their intended location, so initialize them there.
                logInitializer.initializeExistingLogFiles(layout, transactionLogsDirectory, UPGRADE_CHECKPOINT);
            } else if (config.get(fail_on_missing_files)) {
                // recovered state or not.
                throw new UpgradeNotAllowedException();
            } else {
                // The log files are missing entirely, but we were told to not think of this as an error condition,
                // so we instead initialize an empty log file.
                logInitializer.initializeEmptyLogFile(layout, transactionLogsDirectory, UPGRADE_CHECKPOINT);
            }
        }
    } catch (Exception exception) {
        throw new StoreUpgrader.TransactionLogsRelocationException("Failure on attempt to move transaction logs into new location.", exception);
    }
}
Also used : Path(java.nio.file.Path) TransactionLogInitializer(org.neo4j.kernel.impl.transaction.log.files.TransactionLogInitializer) MetadataProvider(org.neo4j.storageengine.api.MetadataProvider) CommandReaderFactory(org.neo4j.storageengine.api.CommandReaderFactory) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) UpgradeNotAllowedException(org.neo4j.storageengine.migration.UpgradeNotAllowedException) IOException(java.io.IOException) UpgradeNotAllowedException(org.neo4j.storageengine.migration.UpgradeNotAllowedException)

Example 2 with UpgradeNotAllowedException

use of org.neo4j.storageengine.migration.UpgradeNotAllowedException 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();
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) StoreVersion(org.neo4j.storageengine.api.StoreVersion) CursorContext(org.neo4j.io.pagecache.context.CursorContext) UpgradeNotAllowedException(org.neo4j.storageengine.migration.UpgradeNotAllowedException)

Aggregations

Path (java.nio.file.Path)2 UpgradeNotAllowedException (org.neo4j.storageengine.migration.UpgradeNotAllowedException)2 IOException (java.io.IOException)1 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)1 CursorContext (org.neo4j.io.pagecache.context.CursorContext)1 LogFiles (org.neo4j.kernel.impl.transaction.log.files.LogFiles)1 TransactionLogInitializer (org.neo4j.kernel.impl.transaction.log.files.TransactionLogInitializer)1 CommandReaderFactory (org.neo4j.storageengine.api.CommandReaderFactory)1 MetadataProvider (org.neo4j.storageengine.api.MetadataProvider)1 StoreVersion (org.neo4j.storageengine.api.StoreVersion)1