Search in sources :

Example 1 with CreateConstraintFailureException

use of org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException in project neo4j by neo4j.

the class KernelTransactionImplementation method commitTransaction.

private long commitTransaction() throws KernelException {
    boolean success = false;
    long txId = READ_ONLY_ID;
    TransactionListenersState listenersState = null;
    try (CommitEvent commitEvent = transactionEvent.beginCommitEvent()) {
        listenersState = eventListeners.beforeCommit(txState, this, storageReader);
        if (listenersState != null && listenersState.isFailed()) {
            Throwable cause = listenersState.failure();
            if (cause instanceof TransientFailureException) {
                throw (TransientFailureException) cause;
            }
            if (cause instanceof Status.HasStatus) {
                throw new TransactionFailureException(((Status.HasStatus) cause).status(), cause, cause.getMessage());
            }
            throw new TransactionFailureException(Status.Transaction.TransactionHookFailed, cause, cause.getMessage());
        }
        // Convert changes into commands and commit
        if (hasChanges()) {
            forceThawLocks();
            lockClient.prepareForCommit();
            // Gather up commands from the various sources
            HeapTrackingArrayList<StorageCommand> extractedCommands = HeapTrackingCollections.newArrayList(memoryTracker);
            storageEngine.createCommands(extractedCommands, txState, storageReader, commandCreationContext, lockClient, lockTracer(), lastTransactionIdWhenStarted, this::enforceConstraints, cursorContext, memoryTracker);
            /* Here's the deal: we track a quick-to-access hasChanges in transaction state which is true
                 * if there are any changes imposed by this transaction. Some changes made inside a transaction undo
                 * previously made changes in that same transaction, and so at some point a transaction may have
                 * changes and at another point, after more changes seemingly,
                 * the transaction may not have any changes.
                 * However, to track that "undoing" of the changes is a bit tedious, intrusive and hard to maintain
                 * and get right.... So to really make sure the transaction has changes we re-check by looking if we
                 * have produced any commands to add to the logical log.
                 */
            if (!extractedCommands.isEmpty()) {
                // Finish up the whole transaction representation
                PhysicalTransactionRepresentation transactionRepresentation = new PhysicalTransactionRepresentation(extractedCommands);
                long timeCommitted = clocks.systemClock().millis();
                transactionRepresentation.setHeader(EMPTY_BYTE_ARRAY, startTimeMillis, lastTransactionIdWhenStarted, timeCommitted, leaseClient.leaseId(), securityContext.subject());
                // Commit the transaction
                success = true;
                TransactionToApply batch = new TransactionToApply(transactionRepresentation, cursorContext);
                kernelTransactionMonitor.beforeApply();
                txId = commitProcess.commit(batch, commitEvent, INTERNAL);
                commitTime = timeCommitted;
            }
        }
        success = true;
        return txId;
    } catch (ConstraintValidationException | CreateConstraintFailureException e) {
        throw new ConstraintViolationTransactionFailureException(e.getUserMessage(tokenRead()), e);
    } finally {
        if (!success) {
            rollback(listenersState);
        } else {
            transactionId = txId;
            afterCommit(listenersState);
        }
        transactionMonitor.addHeapTransactionSize(memoryTracker.heapHighWaterMark());
        transactionMonitor.addNativeTransactionSize(memoryTracker.usedNativeMemory());
    }
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) ConstraintViolationTransactionFailureException(org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException) TransientFailureException(org.neo4j.graphdb.TransientFailureException) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ConstraintValidationException(org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) ConstraintViolationTransactionFailureException(org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException) CommitEvent(org.neo4j.kernel.impl.transaction.tracing.CommitEvent) TransactionListenersState(org.neo4j.kernel.internal.event.TransactionListenersState) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) CreateConstraintFailureException(org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 2 with CreateConstraintFailureException

use of org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException in project neo4j by neo4j.

the class ConstraintIndexCreator method createUniquenessConstraintIndex.

/**
 * You MUST hold a label write lock before you call this method.
 * However the label write lock is temporarily released while populating the index backing the constraint.
 * It goes a little like this:
 * <ol>
 * <li>Prerequisite: Getting here means that there's an open schema transaction which has acquired the
 * LABEL WRITE lock.</li>
 * <li>Index schema rule which is backing the constraint is created in a nested mini-transaction
 * which doesn't acquire any locking, merely adds tx state and commits so that the index rule is applied
 * to the store, which triggers the index population</li>
 * <li>Release the LABEL WRITE lock</li>
 * <li>Await index population to complete</li>
 * <li>Acquire the LABEL WRITE lock (effectively blocking concurrent transactions changing
 * data related to this constraint, and it so happens, most other transactions as well) and verify
 * the uniqueness of the built index</li>
 * <li>Leave this method, knowing that the uniqueness constraint rule will be added to tx state
 * and this tx committed, which will create the uniqueness constraint</li>
 * </ol>
 */
public IndexDescriptor createUniquenessConstraintIndex(KernelTransactionImplementation transaction, IndexBackedConstraintDescriptor constraint, IndexPrototype prototype) throws TransactionFailureException, CreateConstraintFailureException, UniquePropertyValueValidationException, AlreadyConstrainedException {
    String constraintString = constraint.userDescription(transaction.tokenRead());
    log.info("Starting constraint creation: %s.", constraintString);
    IndexDescriptor index;
    SchemaRead schemaRead = transaction.schemaRead();
    try {
        index = checkAndCreateConstraintIndex(schemaRead, transaction.tokenRead(), constraint, prototype);
    } catch (AlreadyConstrainedException e) {
        throw e;
    } catch (KernelException e) {
        throw new CreateConstraintFailureException(constraint, e);
    }
    boolean success = false;
    boolean reacquiredLabelLock = false;
    Client locks = transaction.lockClient();
    ResourceType keyType = constraint.schema().keyType();
    long[] lockingKeys = constraint.schema().lockingKeys();
    try {
        locks.acquireShared(transaction.lockTracer(), keyType, lockingKeys);
        IndexProxy proxy = indexingService.getIndexProxy(index);
        // Release the LABEL WRITE lock during index population.
        // At this point the integrity of the constraint to be created was checked
        // while holding the lock and the index rule backing the soon-to-be-created constraint
        // has been created. Now it's just the population left, which can take a long time
        locks.releaseExclusive(keyType, lockingKeys);
        awaitConstraintIndexPopulation(constraint, proxy, transaction);
        log.info("Constraint %s populated, starting verification.", constraintString);
        // Index population was successful, but at this point we don't know if the uniqueness constraint holds.
        // Acquire LABEL WRITE lock and verify the constraints here in this user transaction
        // and if everything checks out then it will be held until after the constraint has been
        // created and activated.
        locks.acquireExclusive(transaction.lockTracer(), keyType, lockingKeys);
        reacquiredLabelLock = true;
        try (NodePropertyAccessor propertyAccessor = new DefaultNodePropertyAccessor(transaction.newStorageReader(), transaction.cursorContext(), transaction.memoryTracker())) {
            indexingService.getIndexProxy(index).verifyDeferredConstraints(propertyAccessor);
        }
        log.info("Constraint %s verified.", constraintString);
        success = true;
        return index;
    } catch (IndexNotFoundKernelException e) {
        String indexString = index.userDescription(transaction.tokenRead());
        throw new TransactionFailureException(format("Index (%s) that we just created does not exist.", indexString), e);
    } catch (IndexEntryConflictException e) {
        throw new UniquePropertyValueValidationException(constraint, VERIFICATION, e, transaction.tokenRead());
    } catch (InterruptedException | IOException e) {
        throw new CreateConstraintFailureException(constraint, e);
    } finally {
        if (!success) {
            if (!reacquiredLabelLock) {
                locks.acquireExclusive(transaction.lockTracer(), keyType, lockingKeys);
            }
            if (indexStillExists(schemaRead, index)) {
                dropUniquenessConstraintIndex(index);
            }
        }
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) ResourceType(org.neo4j.lock.ResourceType) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IOException(java.io.IOException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) NodePropertyAccessor(org.neo4j.storageengine.api.NodePropertyAccessor) DefaultNodePropertyAccessor(org.neo4j.kernel.impl.transaction.state.storeview.DefaultNodePropertyAccessor) DefaultNodePropertyAccessor(org.neo4j.kernel.impl.transaction.state.storeview.DefaultNodePropertyAccessor) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexPopulationFailedKernelException(org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException) KernelException(org.neo4j.exceptions.KernelException) Client(org.neo4j.kernel.impl.locking.Locks.Client) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) CreateConstraintFailureException(org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 3 with CreateConstraintFailureException

use of org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException in project neo4j by neo4j.

the class Operations method indexBackedConstraintCreate.

@SuppressWarnings("unchecked")
private <T extends IndexBackedConstraintDescriptor> T indexBackedConstraintCreate(T constraint, IndexPrototype prototype) throws KernelException {
    try {
        if (allStoreHolder.constraintExists(constraint)) {
            throw new AlreadyConstrainedException(constraint, CONSTRAINT_CREATION, token);
        }
        if (prototype.getIndexType() != IndexType.BTREE) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index with index type " + prototype.getIndexType() + ".");
        }
        if (prototype.schema().isFulltextSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a full-text schema: " + prototype.schema().userDescription(token));
        }
        if (prototype.schema().isRelationshipTypeSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a relationship type schema: " + prototype.schema().userDescription(token));
        }
        if (prototype.schema().isAnyTokenSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using an any token schema: " + prototype.schema().userDescription(token));
        }
        if (!prototype.isUnique()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create index backed constraint using an index prototype that is not unique: " + prototype.userDescription(token));
        }
        IndexDescriptor index = constraintIndexCreator.createUniquenessConstraintIndex(ktx, constraint, prototype);
        if (!allStoreHolder.constraintExists(constraint)) {
            // This looks weird, but since we release the label lock while awaiting population of the index
            // backing this constraint there can be someone else getting ahead of us, creating this exact
            // constraint
            // before we do, so now getting out here under the lock we must check again and if it exists
            // we must at this point consider this an idempotent operation because we verified earlier
            // that it didn't exist and went on to create it.
            constraint = (T) constraint.withOwnedIndexId(index.getId());
            ktx.txState().constraintDoAdd(constraint, index);
        } else {
            constraint = (T) allStoreHolder.constraintsGetForSchema(constraint.schema());
        }
        return constraint;
    } catch (UniquePropertyValueValidationException | TransactionFailureException | AlreadyConstrainedException e) {
        throw new CreateConstraintFailureException(constraint, e);
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) CreateConstraintFailureException(org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)

Aggregations

TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)3 CreateConstraintFailureException (org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)3 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)2 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)2 UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)2 IOException (java.io.IOException)1 KernelException (org.neo4j.exceptions.KernelException)1 TransientFailureException (org.neo4j.graphdb.TransientFailureException)1 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)1 ConstraintViolationTransactionFailureException (org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException)1 ConstraintValidationException (org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException)1 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)1 Status (org.neo4j.kernel.api.exceptions.Status)1 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)1 IndexPopulationFailedKernelException (org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException)1 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)1 Client (org.neo4j.kernel.impl.locking.Locks.Client)1 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)1 DefaultNodePropertyAccessor (org.neo4j.kernel.impl.transaction.state.storeview.DefaultNodePropertyAccessor)1 CommitEvent (org.neo4j.kernel.impl.transaction.tracing.CommitEvent)1