Search in sources :

Example 11 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class ConstraintIndexCreator method createUniquenessConstraintIndex.

/**
     * You MUST hold a schema write lock before you call this method.
     * However the schema 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
     * SCHEMA 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 SCHEMA WRITE lock</li>
     * <li>Await index population to complete</li>
     * <li>Acquire the SCHEMA 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 long createUniquenessConstraintIndex(KernelStatement state, SchemaReadOperations schema, LabelSchemaDescriptor descriptor) throws TransactionFailureException, CreateConstraintFailureException, DropIndexFailureException, UniquePropertyValueValidationException {
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
    NewIndexDescriptor index = createConstraintIndex(descriptor);
    boolean success = false;
    boolean reacquiredSchemaLock = false;
    Client locks = state.locks().pessimistic();
    try {
        long indexId = schema.indexGetCommittedId(state, index);
        // Release the SCHEMA 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
        releaseSchemaLock(locks);
        awaitConstrainIndexPopulation(constraint, indexId);
        // Index population was successful, but at this point we don't know if the uniqueness constraint holds.
        // Acquire SCHEMA 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.
        acquireSchemaLock(state, locks);
        reacquiredSchemaLock = true;
        indexingService.getIndexProxy(indexId).verifyDeferredConstraints(propertyAccessor);
        success = true;
        return indexId;
    } catch (SchemaRuleNotFoundException | IndexNotFoundKernelException e) {
        throw new IllegalStateException(String.format("Index (%s) that we just created does not exist.", descriptor));
    } catch (IndexEntryConflictException e) {
        throw new UniquePropertyValueValidationException(constraint, VERIFICATION, e);
    } catch (InterruptedException | IOException e) {
        throw new CreateConstraintFailureException(constraint, e);
    } finally {
        if (!success) {
            if (!reacquiredSchemaLock) {
                acquireSchemaLock(state, locks);
            }
            dropUniquenessConstraintIndex(index);
        }
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) SchemaRuleNotFoundException(org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) IOException(java.io.IOException) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) Client(org.neo4j.kernel.impl.locking.Locks.Client) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 12 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class BatchInserterImpl method repopulateAllIndexes.

private void repopulateAllIndexes() throws IOException, IndexEntryConflictException {
    if (!labelsTouched) {
        return;
    }
    final IndexRule[] rules = getIndexesNeedingPopulation();
    final IndexPopulator[] populators = new IndexPopulator[rules.length];
    // the store is uncontended at this point, so creating a local LockService is safe.
    final NewIndexDescriptor[] descriptors = new NewIndexDescriptor[rules.length];
    for (int i = 0; i < rules.length; i++) {
        IndexRule rule = rules[i];
        descriptors[i] = rule.getIndexDescriptor();
        populators[i] = schemaIndexProviders.apply(rule.getProviderDescriptor()).getPopulator(rule.getId(), descriptors[i], new IndexSamplingConfig(config));
        populators[i].create();
    }
    Visitor<NodeUpdates, IOException> propertyUpdateVisitor = updates -> {
        for (int i = 0; i < descriptors.length; i++) {
            Optional<IndexEntryUpdate> update = updates.forIndex(descriptors[i].schema());
            if (update.isPresent()) {
                try {
                    populators[i].add(Collections.singletonList(update.get()));
                } catch (IndexEntryConflictException conflict) {
                    throw conflict.notAllowed(descriptors[i]);
                }
            }
        }
        return true;
    };
    List<NewIndexDescriptor> descriptorList = Arrays.asList(descriptors);
    int[] labelIds = descriptorList.stream().mapToInt(index -> index.schema().getLabelId()).toArray();
    int[] propertyKeyIds = descriptorList.stream().flatMapToInt(d -> Arrays.stream(d.schema().getPropertyIds())).toArray();
    InitialNodeLabelCreationVisitor labelUpdateVisitor = new InitialNodeLabelCreationVisitor();
    StoreScan<IOException> storeScan = indexStoreView.visitNodes(labelIds, (propertyKeyId) -> PrimitiveIntCollections.contains(propertyKeyIds, propertyKeyId), propertyUpdateVisitor, labelUpdateVisitor, true);
    storeScan.run();
    for (IndexPopulator populator : populators) {
        populator.verifyDeferredConstraints(indexStoreView);
        populator.close(true);
    }
    labelUpdateVisitor.close();
}
Also used : InternalIndexState(org.neo4j.kernel.api.index.InternalIndexState) Arrays(java.util.Arrays) SimpleKernelContext(org.neo4j.kernel.impl.spi.SimpleKernelContext) PropertyKeyTokenStore(org.neo4j.kernel.impl.store.PropertyKeyTokenStore) BatchRelationship(org.neo4j.unsafe.batchinsert.BatchRelationship) Iterators(org.neo4j.helpers.collection.Iterators) IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) RelationshipGroupGetter(org.neo4j.kernel.impl.transaction.state.RelationshipGroupGetter) PrimitiveIntCollections(org.neo4j.collection.primitive.PrimitiveIntCollections) RelationshipPropertyExistenceConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition) NodeLabelsField.parseLabelsField(org.neo4j.kernel.impl.store.NodeLabelsField.parseLabelsField) NoOpClient(org.neo4j.kernel.impl.locking.NoOpClient) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) CountsComputer(org.neo4j.kernel.impl.store.CountsComputer) PropertyStore.encodeString(org.neo4j.kernel.impl.store.PropertyStore.encodeString) Map(java.util.Map) PageCacheTracer(org.neo4j.io.pagecache.tracing.PageCacheTracer) NodeMultiPropertyDescriptor(org.neo4j.kernel.api.schema.NodeMultiPropertyDescriptor) DefaultSchemaIndexProviderMap(org.neo4j.kernel.impl.transaction.state.DefaultSchemaIndexProviderMap) KernelExtensions(org.neo4j.kernel.extension.KernelExtensions) IndexDefinitionImpl(org.neo4j.kernel.impl.coreapi.schema.IndexDefinitionImpl) NullLog(org.neo4j.logging.NullLog) NodeUpdates(org.neo4j.kernel.api.index.NodeUpdates) HighestSelectionStrategy(org.neo4j.kernel.extension.dependency.HighestSelectionStrategy) LabelTokenRecord(org.neo4j.kernel.impl.store.record.LabelTokenRecord) DefaultIdGeneratorFactory(org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory) RecordCursors(org.neo4j.kernel.impl.store.RecordCursors) SchemaStore(org.neo4j.kernel.impl.store.SchemaStore) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Locks(org.neo4j.kernel.impl.locking.Locks) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint) KernelExtensionFactory(org.neo4j.kernel.extension.KernelExtensionFactory) LabelScanStoreProvider(org.neo4j.kernel.impl.api.scan.LabelScanStoreProvider) RelationshipType(org.neo4j.graphdb.RelationshipType) NodeStore(org.neo4j.kernel.impl.store.NodeStore) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) IndexCreatorImpl(org.neo4j.kernel.impl.coreapi.schema.IndexCreatorImpl) RelationshipTypeToken(org.neo4j.kernel.impl.core.RelationshipTypeToken) RecordFormatSelector(org.neo4j.kernel.impl.store.format.RecordFormatSelector) LabelScanStore(org.neo4j.kernel.api.labelscan.LabelScanStore) PageCacheLifecycle(org.neo4j.kernel.impl.pagecache.PageCacheLifecycle) IteratorWrapper(org.neo4j.helpers.collection.IteratorWrapper) ArrayList(java.util.ArrayList) InternalSchemaActions(org.neo4j.kernel.impl.coreapi.schema.InternalSchemaActions) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) DirectRecordAccessSet(org.neo4j.unsafe.batchinsert.DirectRecordAccessSet) SchemaCache(org.neo4j.kernel.impl.api.store.SchemaCache) PropertyStore(org.neo4j.kernel.impl.store.PropertyStore) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException) PrimitiveLongCollections(org.neo4j.collection.primitive.PrimitiveLongCollections) DatabaseInfo(org.neo4j.kernel.impl.factory.DatabaseInfo) UnsatisfiedDependencyStrategies(org.neo4j.kernel.extension.UnsatisfiedDependencyStrategies) Listener(org.neo4j.kernel.impl.util.Listener) PrimitiveLongCollections.map(org.neo4j.collection.primitive.PrimitiveLongCollections.map) BaseNodeConstraintCreator(org.neo4j.kernel.impl.coreapi.schema.BaseNodeConstraintCreator) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) NewIndexDescriptorFactory(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory) IOException(java.io.IOException) Label.label(org.neo4j.graphdb.Label.label) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) IoPrimitiveUtils.safeCastLongToInt(org.neo4j.kernel.impl.util.IoPrimitiveUtils.safeCastLongToInt) File(java.io.File) Iterables(org.neo4j.helpers.collection.Iterables) ConstraintDescriptorFactory(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptorFactory) IndexConfigStore(org.neo4j.kernel.impl.index.IndexConfigStore) RelationshipGroupRecord(org.neo4j.kernel.impl.store.record.RelationshipGroupRecord) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) SchemaIndexProviderMap(org.neo4j.kernel.impl.api.index.SchemaIndexProviderMap) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) NodeLabelUpdate(org.neo4j.kernel.api.labelscan.NodeLabelUpdate) NodePropertyExistenceConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexSamplingConfig(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) PageCursorTracerSupplier(org.neo4j.io.pagecache.tracing.cursor.PageCursorTracerSupplier) PropertyDeleter(org.neo4j.kernel.impl.transaction.state.PropertyDeleter) Log(org.neo4j.logging.Log) IdGeneratorFactory(org.neo4j.kernel.impl.store.id.IdGeneratorFactory) StoreScan(org.neo4j.kernel.impl.api.index.StoreScan) Dependencies(org.neo4j.kernel.impl.util.Dependencies) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) LabelTokenStore(org.neo4j.kernel.impl.store.LabelTokenStore) NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) RelationshipTypeTokenStore(org.neo4j.kernel.impl.store.RelationshipTypeTokenStore) DefinedProperty(org.neo4j.kernel.api.properties.DefinedProperty) Record(org.neo4j.kernel.impl.store.record.Record) PageCache(org.neo4j.io.pagecache.PageCache) UnderlyingStorageException(org.neo4j.kernel.impl.store.UnderlyingStorageException) PropertyCreator(org.neo4j.kernel.impl.transaction.state.PropertyCreator) Collection(java.util.Collection) IndexCreator(org.neo4j.graphdb.schema.IndexCreator) IndexEntryUpdate(org.neo4j.kernel.api.index.IndexEntryUpdate) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) KernelException(org.neo4j.kernel.api.exceptions.KernelException) LabelScanWriter(org.neo4j.kernel.api.labelscan.LabelScanWriter) DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) RecordStore(org.neo4j.kernel.impl.store.RecordStore) List(java.util.List) NamedLabelScanStoreSelectionStrategy(org.neo4j.kernel.extension.dependency.NamedLabelScanStoreSelectionStrategy) StoreLocker(org.neo4j.kernel.internal.StoreLocker) Entry(java.util.Map.Entry) Optional(java.util.Optional) EmbeddedGraphDatabase(org.neo4j.kernel.internal.EmbeddedGraphDatabase) Label(org.neo4j.graphdb.Label) SchemaDescriptorFactory(org.neo4j.kernel.api.schema_new.SchemaDescriptorFactory) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor) LogProvider(org.neo4j.logging.LogProvider) HashMap(java.util.HashMap) Token(org.neo4j.storageengine.api.Token) NotFoundException(org.neo4j.graphdb.NotFoundException) ConfiguringPageCacheFactory(org.neo4j.kernel.impl.pagecache.ConfiguringPageCacheFactory) UniquenessConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition) RecordFormats(org.neo4j.kernel.impl.store.format.RecordFormats) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) NodeLabels(org.neo4j.kernel.impl.store.NodeLabels) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) ConstraintCreator(org.neo4j.graphdb.schema.ConstraintCreator) NeoStores(org.neo4j.kernel.impl.store.NeoStores) StoreLogService(org.neo4j.kernel.impl.logging.StoreLogService) PropertyBlock(org.neo4j.kernel.impl.store.record.PropertyBlock) CountsTracker(org.neo4j.kernel.impl.store.counts.CountsTracker) RelationshipTypeTokenRecord(org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord) PrintStream(java.io.PrintStream) Config(org.neo4j.kernel.configuration.Config) ConstraintRule(org.neo4j.kernel.impl.store.record.ConstraintRule) RecordProxy(org.neo4j.kernel.impl.transaction.state.RecordAccess.RecordProxy) StandardConstraintSemantics(org.neo4j.kernel.impl.constraints.StandardConstraintSemantics) LockService(org.neo4j.kernel.impl.locking.LockService) RelationshipCreator(org.neo4j.kernel.impl.transaction.state.RelationshipCreator) Iterator(java.util.Iterator) LongFunction(java.util.function.LongFunction) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord) RecordAccess(org.neo4j.kernel.impl.transaction.state.RecordAccess) PropertyKeyTokenRecord(org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord) IdValidator(org.neo4j.kernel.impl.store.id.validation.IdValidator) PropertyTraverser(org.neo4j.kernel.impl.transaction.state.PropertyTraverser) PrimitiveRecord(org.neo4j.kernel.impl.store.record.PrimitiveRecord) NeoStoreIndexStoreView(org.neo4j.kernel.impl.transaction.state.storeview.NeoStoreIndexStoreView) Visitor(org.neo4j.helpers.collection.Visitor) Collections(java.util.Collections) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) IndexSamplingConfig(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig) Optional(java.util.Optional) IOException(java.io.IOException) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint) NodeUpdates(org.neo4j.kernel.api.index.NodeUpdates) IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 13 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class BatchInsertTest method uniquenessConstraintShouldBeCheckedOnBatchInserterShutdownAndFailIfViolated.

@Test
public void uniquenessConstraintShouldBeCheckedOnBatchInserterShutdownAndFailIfViolated() throws Exception {
    // Given
    Label label = label("Foo");
    String property = "Bar";
    String value = "Baz";
    BatchInserter inserter = newBatchInserter();
    // When
    inserter.createDeferredConstraint(label).assertPropertyIsUnique(property).create();
    inserter.createNode(Collections.<String, Object>singletonMap(property, value), label);
    inserter.createNode(Collections.<String, Object>singletonMap(property, value), label);
    // Then
    try {
        inserter.shutdown();
        fail("Node that violates uniqueness constraint was created by batch inserter");
    } catch (RuntimeException ex) {
        // good
        assertEquals(new IndexEntryConflictException(0, 1, value), ex.getCause());
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) Label(org.neo4j.graphdb.Label) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) Test(org.junit.Test)

Example 14 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class SimpleUniquenessVerifier method verify.

@Override
public void verify(PropertyAccessor accessor, int[] propKeyIds, List<Object> updatedPropertyValues) throws IndexEntryConflictException, IOException {
    try {
        DuplicateCheckingCollector collector = DuplicateCheckingCollector.forProperties(accessor, propKeyIds);
        for (Object propertyValue : updatedPropertyValues) {
            collector.reset();
            Query query = LuceneDocumentStructure.newSeekQuery(propertyValue);
            indexSearcher().search(query, collector);
        }
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IndexEntryConflictException) {
            throw (IndexEntryConflictException) cause;
        }
        throw e;
    }
}
Also used : Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) IOException(java.io.IOException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 15 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class ContractCheckingIndexProxyTest method shouldNotCloseWhileUpdating.

@Test(expected = /* THEN */
IllegalStateException.class)
public void shouldNotCloseWhileUpdating() throws IOException {
    // GIVEN
    final DoubleLatch latch = new DoubleLatch();
    final IndexProxy inner = new IndexProxyAdapter() {

        @Override
        public IndexUpdater newUpdater(IndexUpdateMode mode) {
            return super.newUpdater(mode);
        }
    };
    final IndexProxy outer = newContractCheckingIndexProxy(inner);
    outer.start();
    // WHEN
    runInSeparateThread(() -> {
        try (IndexUpdater updater = outer.newUpdater(IndexUpdateMode.ONLINE)) {
            updater.process(null);
            latch.startAndWaitForAllToStartAndFinish();
        } catch (IndexEntryConflictException e) {
            throw new RuntimeException(e);
        }
    });
    try {
        latch.waitForAllToStart();
        outer.close();
    } finally {
        latch.finish();
    }
}
Also used : SchemaIndexTestHelper.mockIndexProxy(org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy) DoubleLatch(org.neo4j.test.DoubleLatch) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) Test(org.junit.Test)

Aggregations

IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)21 IOException (java.io.IOException)9 Test (org.junit.jupiter.api.Test)5 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)5 UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)5 TermQuery (org.apache.lucene.search.TermQuery)4 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 List (java.util.List)3 Test (org.junit.Test)3 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Set (java.util.Set)2 Query (org.apache.lucene.search.Query)2