Search in sources :

Example 1 with KernelVersion

use of org.neo4j.kernel.KernelVersion in project neo4j by neo4j.

the class Operations method assertTokenAndRelationshipPropertyIndexesSupported.

private void assertTokenAndRelationshipPropertyIndexesSupported(String message) {
    KernelVersion currentStoreVersion = kernelVersionRepository.kernelVersion();
    if (currentStoreVersion.isAtLeast(KernelVersion.VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED)) {
        // new or upgraded store, good to go
        return;
    }
    // store version is old
    KernelVersion currentDbmsVersion = dbmsRuntimeRepository.getVersion().kernelVersion();
    if (currentDbmsVersion.isAtLeast(KernelVersion.VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED)) {
        // we will double check kernel version during commit
        return;
    }
    throw new UnsupportedOperationException(format("%s Version was %s, but required version for operation is %s. Please upgrade dbms using 'dbms.upgrade()'.", message, currentDbmsVersion.name(), KernelVersion.VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED.name()));
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion)

Example 2 with KernelVersion

use of org.neo4j.kernel.KernelVersion in project neo4j by neo4j.

the class IntegrityValidator method validateSchemaRule.

void validateSchemaRule(SchemaRule schemaRule) throws TransactionFailureException {
    Preconditions.checkState(indexValidator != null, "No index validator installed");
    KernelVersion currentVersion = neoStores.getMetaDataStore().kernelVersion();
    if (currentVersion.isLessThan(VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED)) {
        if (schemaRule instanceof IndexDescriptor) {
            IndexDescriptor index = (IndexDescriptor) schemaRule;
            if (index.isTokenIndex() || isBtreeRelationshipPropertyIndex(index)) {
                throw new TransactionFailureException(Status.General.UpgradeRequired, "Index operation on index '%s' not allowed. " + "Required kernel version for this transaction is %s, but actual version was %s.", index, VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED.name(), currentVersion.name());
            }
        }
    }
    if (schemaRule instanceof ConstraintDescriptor) {
        ConstraintDescriptor constraint = (ConstraintDescriptor) schemaRule;
        if (constraint.isIndexBackedConstraint()) {
            long ownedIndex = constraint.asIndexBackedConstraint().ownedIndexId();
            try {
                indexValidator.validateIndex(ownedIndex);
            } catch (KernelException e) {
                // and have recovery performed. It's the safest bet to avoid loosing data.
                throw new TransactionFailureException(Status.Transaction.TransactionValidationFailed, e, "Index validation of " + schemaRule + " failed, specifically for its owned index " + ownedIndex, e);
            }
        }
    }
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) ConstraintViolationTransactionFailureException(org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) KernelException(org.neo4j.exceptions.KernelException)

Example 3 with KernelVersion

use of org.neo4j.kernel.KernelVersion in project neo4j by neo4j.

the class DatabaseUpgradeTransactionIT method assertUpgradeTransactionInOrder.

private void assertUpgradeTransactionInOrder(KernelVersion from, KernelVersion to, long fromTxId) throws Exception {
    LogicalTransactionStore lts = db.getDependencyResolver().resolveDependency(LogicalTransactionStore.class);
    ArrayList<KernelVersion> transactionVersions = new ArrayList<>();
    ArrayList<CommittedTransactionRepresentation> transactions = new ArrayList<>();
    try (TransactionCursor transactionCursor = lts.getTransactions(fromTxId + 1)) {
        while (transactionCursor.next()) {
            CommittedTransactionRepresentation representation = transactionCursor.get();
            transactions.add(representation);
            transactionVersions.add(representation.getStartEntry().getVersion());
        }
    }
    // at least upgrade transaction and the triggering transaction
    assertThat(transactionVersions).hasSizeGreaterThanOrEqualTo(2);
    // Sorted means everything is in order
    assertThat(transactionVersions).isSortedAccordingTo(Comparator.comparingInt(KernelVersion::version));
    // First should be "from" version
    assertThat(transactionVersions.get(0)).isEqualTo(from);
    // And last the "to" version
    assertThat(transactionVersions.get(transactionVersions.size() - 1)).isEqualTo(to);
    CommittedTransactionRepresentation upgradeTransaction = transactions.get(transactionVersions.indexOf(to));
    PhysicalTransactionRepresentation physicalRep = (PhysicalTransactionRepresentation) upgradeTransaction.getTransactionRepresentation();
    physicalRep.accept(element -> {
        assertThat(element).isInstanceOf(Command.MetaDataCommand.class);
        return true;
    });
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) TransactionCursor(org.neo4j.kernel.impl.transaction.log.TransactionCursor) Command(org.neo4j.internal.recordstorage.Command) ArrayList(java.util.ArrayList) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)

Example 4 with KernelVersion

use of org.neo4j.kernel.KernelVersion in project neo4j by neo4j.

the class DatabaseUpgradeTransactionHandlerTest method doATransaction.

private void doATransaction(boolean doSomeSleeping) {
    if (!listenerUnregistered && listener != null) {
        try {
            Object state = listener.beforeCommit(mock(TransactionData.class), mock(KernelTransaction.class), mock(GraphDatabaseService.class));
            KernelVersion currentKernelVersion = this.currentKernelVersion;
            if (doSomeSleeping) {
                // This sleep is an enabler for upgrade vs transactions race, makes this way more likely to trigger
                Thread.sleep(ThreadLocalRandom.current().nextInt(3));
            }
            registeredTransactions.add(new RegisteredTransaction(currentKernelVersion, false));
            // At this point we cannot assert on a comparison between dbms runtime version and kernel version
            listener.afterCommit(mock(TransactionData.class), state, mock(GraphDatabaseService.class));
        } catch (Exception e) {
            ExceptionUtils.throwAsUncheckedException(e);
        }
    }
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TransactionData(org.neo4j.graphdb.event.TransactionData) IOException(java.io.IOException)

Example 5 with KernelVersion

use of org.neo4j.kernel.KernelVersion in project neo4j by neo4j.

the class DatabaseUpgradeTransactionHandlerTest method init.

private void init(KernelVersion initialKernelVersion, DbmsRuntimeVersion initialDbmsRuntimeVersion) {
    setKernelVersion(initialKernelVersion);
    setDbmsRuntime(initialDbmsRuntimeVersion);
    StorageEngine storageEngine = mock(StorageEngine.class);
    doAnswer(inv -> {
        KernelVersion toKernelVersion = inv.getArgument(0, KernelVersion.class);
        registeredTransactions.add(new RegisteredTransaction(toKernelVersion, true));
        return List.of(new FakeKernelVersionUpgradeCommand(toKernelVersion));
    }).when(storageEngine).createUpgradeCommands(any(), any());
    DbmsRuntimeRepository dbmsRuntimeRepository = mock(DbmsRuntimeRepository.class);
    doAnswer(inv -> currentDbmsRuntimeVersion).when(dbmsRuntimeRepository).getVersion();
    KernelVersionRepository kernelVersionRepository = this::getKernelVersion;
    DatabaseTransactionEventListeners databaseTransactionEventListeners = mock(DatabaseTransactionEventListeners.class);
    doAnswer(inv -> listener = inv.getArgument(0, InternalTransactionEventListener.class)).when(databaseTransactionEventListeners).registerTransactionEventListener(any());
    doAnswer(inv -> listenerUnregistered = true).when(databaseTransactionEventListeners).unregisterTransactionEventListener(any());
    DatabaseUpgradeTransactionHandler handler = new DatabaseUpgradeTransactionHandler(storageEngine, dbmsRuntimeRepository, kernelVersionRepository, databaseTransactionEventListeners, lock, logProvider);
    handler.registerUpgradeListener(commands -> setKernelVersion(((FakeKernelVersionUpgradeCommand) commands.iterator().next()).version));
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) DbmsRuntimeRepository(org.neo4j.dbms.database.DbmsRuntimeRepository) DatabaseTransactionEventListeners(org.neo4j.kernel.internal.event.DatabaseTransactionEventListeners) StorageEngine(org.neo4j.storageengine.api.StorageEngine) KernelVersionRepository(org.neo4j.storageengine.api.KernelVersionRepository)

Aggregations

KernelVersion (org.neo4j.kernel.KernelVersion)12 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 InMemoryClosableChannel (org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel)2 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)2 StoreId (org.neo4j.storageengine.api.StoreId)2 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Instant (java.time.Instant)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 UUID.randomUUID (java.util.UUID.randomUUID)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 LongStream (java.util.stream.LongStream)1 Stream (java.util.stream.Stream)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 ExceptionUtils.getRootCause (org.apache.commons.lang3.exception.ExceptionUtils.getRootCause)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1