Search in sources :

Example 1 with UniquePropertyValueValidationException

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

the class ConstraintEnforcingEntityOperations method validateNoExistingNodeWithExactValues.

private void validateNoExistingNodeWithExactValues(KernelStatement state, UniquenessConstraintDescriptor constraint, ExactPredicate[] propertyValues, long modifiedNode) throws ConstraintValidationException {
    try {
        NewIndexDescriptor index = constraint.ownedIndexDescriptor();
        assertIndexOnline(state, index);
        int labelId = index.schema().getLabelId();
        state.locks().optimistic().acquireExclusive(state.lockTracer(), INDEX_ENTRY, indexEntryResourceId(labelId, propertyValues));
        long existing = entityReadOperations.nodeGetFromUniqueIndexSeek(state, index, propertyValues);
        if (existing != NO_SUCH_NODE && existing != modifiedNode) {
            throw new UniquePropertyValueValidationException(constraint, VALIDATION, new IndexEntryConflictException(existing, NO_SUCH_NODE, OrderedPropertyValues.of(propertyValues)));
        }
    } catch (IndexNotFoundKernelException | IndexBrokenKernelException | IndexNotApplicableKernelException e) {
        throw new UnableToValidateConstraintException(constraint, e);
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) IndexNotApplicableKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) UnableToValidateConstraintException(org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException) IndexBrokenKernelException(org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 2 with UniquePropertyValueValidationException

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

the class IntegrityValidatorTest method shouldValidateUniquenessIndexes.

@Test
public void shouldValidateUniquenessIndexes() throws Exception {
    // Given
    NeoStores store = mock(NeoStores.class);
    IndexingService indexes = mock(IndexingService.class);
    IntegrityValidator validator = new IntegrityValidator(store, indexes);
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForLabel(1, 1);
    doThrow(new UniquePropertyValueValidationException(constraint, ConstraintValidationException.Phase.VERIFICATION, new RuntimeException())).when(indexes).validateIndex(2L);
    ConstraintRule record = ConstraintRule.constraintRule(1L, constraint, 2L);
    // When
    try {
        validator.validateSchemaRule(record);
        fail("Should have thrown integrity error.");
    } catch (Exception e) {
    // good
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) ConstraintRule(org.neo4j.kernel.impl.store.record.ConstraintRule) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) NeoStores(org.neo4j.kernel.impl.store.NeoStores) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) Test(org.junit.Test)

Example 3 with UniquePropertyValueValidationException

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

the class Operations method validateNoExistingNodeWithExactValues.

/**
 * Check so that there is not an existing node with the exact match of label and property
 */
private void validateNoExistingNodeWithExactValues(IndexBackedConstraintDescriptor constraint, PropertyIndexQuery.ExactPredicate[] propertyValues, long modifiedNode) throws UniquePropertyValueValidationException, UnableToValidateConstraintException {
    IndexDescriptor index = allStoreHolder.indexGetForName(constraint.getName());
    try (FullAccessNodeValueIndexCursor valueCursor = cursors.allocateFullAccessNodeValueIndexCursor(ktx.cursorContext(), memoryTracker);
        IndexReaders indexReaders = new IndexReaders(index, allStoreHolder)) {
        assertIndexOnline(index);
        SchemaDescriptor schema = index.schema();
        long[] labelIds = schema.lockingKeys();
        if (labelIds.length != 1) {
            throw new UnableToValidateConstraintException(constraint, new AssertionError(format("Constraint indexes are not expected to be multi-token indexes, " + "but the constraint %s was referencing an index with the following schema: %s.", constraint.userDescription(token), schema.userDescription(token))), token);
        }
        // Take a big fat lock, and check for existing node in index
        ktx.lockClient().acquireExclusive(ktx.lockTracer(), INDEX_ENTRY, indexEntryResourceId(labelIds[0], propertyValues));
        allStoreHolder.nodeIndexSeekWithFreshIndexReader(valueCursor, indexReaders.createReader(), propertyValues);
        if (valueCursor.next() && valueCursor.nodeReference() != modifiedNode) {
            throw new UniquePropertyValueValidationException(constraint, VALIDATION, new IndexEntryConflictException(valueCursor.nodeReference(), NO_SUCH_NODE, PropertyIndexQuery.asValueTuple(propertyValues)), token);
        }
    } catch (IndexNotFoundKernelException | IndexBrokenKernelException | IndexNotApplicableKernelException e) {
        throw new UnableToValidateConstraintException(constraint, e, token);
    }
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) UnableToValidateConstraintException(org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) IndexBrokenKernelException(org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 4 with UniquePropertyValueValidationException

use of org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException 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 5 with UniquePropertyValueValidationException

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

the class UniquenessConstraintValidationIT method shouldPreventConflictingDataInSameTransaction.

@Test
void shouldPreventConflictingDataInSameTransaction() throws Exception {
    // given
    constrainedNode("Label1", "key1", "value1");
    KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
    // when
    createLabeledNode(transaction, "Label1", "key1", "value2");
    try {
        createLabeledNode(transaction, "Label1", "key1", "value2");
        fail("expected exception");
    }// then
     catch (UniquePropertyValueValidationException e) {
        assertThat(e.getUserMessage(transaction.tokenRead())).contains("`key1` = 'value2'");
    }
    commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) Test(org.junit.jupiter.api.Test)

Aggregations

UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)12 Test (org.junit.jupiter.api.Test)5 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)5 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)3 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)3 IOException (java.io.IOException)2 TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)2 CreateConstraintFailureException (org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)2 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)2 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)2 IndexPopulationFailedKernelException (org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException)2 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)2 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)2 IndexBrokenKernelException (org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException)2 UnableToValidateConstraintException (org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException)2 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)2 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)2 IndexingService (org.neo4j.kernel.impl.api.index.IndexingService)2 Client (org.neo4j.kernel.impl.locking.Locks.Client)2