Search in sources :

Example 46 with KernelException

use of org.neo4j.exceptions.KernelException in project neo4j by neo4j.

the class FullCheckIntegrationTest method createFixture.

private GraphStoreFixture createFixture() {
    return new GraphStoreFixture(getRecordFormatName(), testDirectory) {

        @Override
        protected void generateInitialData(GraphDatabaseService db) {
            // because many tests rely on sort order for those token ids.
            try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
                KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
                TokenWrite tokenWrite = ktx.tokenWrite();
                label1 = tokenWrite.labelGetOrCreateForName("label1");
                label2 = tokenWrite.labelGetOrCreateForName("label2");
                label3 = tokenWrite.labelGetOrCreateForName("label3");
                tokenWrite.labelGetOrCreateForName("label4");
                draconian = tokenWrite.labelGetOrCreateForName("draconian");
                key1 = tokenWrite.propertyKeyGetOrCreateForName(PROP1);
                mandatory = tokenWrite.propertyKeyGetOrCreateForName("mandatory");
                C = tokenWrite.relationshipTypeGetOrCreateForName("C");
                T = tokenWrite.relationshipTypeGetOrCreateForName("T");
                M = tokenWrite.relationshipTypeGetOrCreateForName("M");
                tx.commit();
            } catch (KernelException e) {
                throw new RuntimeException(e);
            }
            // Create indexes
            try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
                tx.schema().indexFor(label("label3")).on(PROP1).create();
                tx.schema().indexFor(label("label3")).on(PROP1).on(PROP2).create();
                tx.schema().constraintFor(label("label4")).assertPropertyIsUnique(PROP1).create();
                tx.commit();
            }
            try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
                tx.schema().awaitIndexesOnline(2, TimeUnit.MINUTES);
            }
            // Create initial data
            try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
                Node node1 = set(tx.createNode(label("label1")));
                Node node2 = set(tx.createNode(label("label2")), property(PROP1, VALUE1));
                node1.createRelationshipTo(node2, withName("C"));
                // Just to create one more rel type
                tx.createNode().createRelationshipTo(tx.createNode(), withName("T"));
                indexedNodes.add(set(tx.createNode(label("label3")), property(PROP1, VALUE1)).getId());
                indexedNodes.add(set(tx.createNode(label("label3")), property(PROP1, VALUE1), property(PROP2, VALUE2)).getId());
                set(tx.createNode(label("label4")), property(PROP1, VALUE1));
                tx.commit();
            }
        }

        @Override
        protected Map<Setting<?>, Object> getConfig() {
            return getSettings();
        }
    };
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) Setting(org.neo4j.graphdb.config.Setting) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelException(org.neo4j.exceptions.KernelException) GraphStoreFixture(org.neo4j.consistency.checking.GraphStoreFixture)

Example 47 with KernelException

use of org.neo4j.exceptions.KernelException in project neo4j by neo4j.

the class FullCheckIntegrationTest method shouldReportInvalidConstraintBackReferences.

@Test
void shouldReportInvalidConstraintBackReferences() throws Exception {
    // given
    AtomicReference<IndexDescriptor> descriptor = new AtomicReference<>();
    fixture.apply(new GraphStoreFixture.Transaction() {

        @Override
        protected void transactionData(GraphStoreFixture.TransactionDataBuilder tx, GraphStoreFixture.IdGenerator next) throws KernelException {
            int ruleId1 = (int) next.schema();
            int ruleId2 = (int) next.schema();
            int labelId = next.label();
            int propertyKeyId = next.propertyKey();
            SchemaRecord before1 = new SchemaRecord(ruleId1);
            SchemaRecord before2 = new SchemaRecord(ruleId2);
            SchemaRecord after1 = cloneRecord(before1).initialize(true, 0);
            SchemaRecord after2 = cloneRecord(before2).initialize(true, 0);
            IndexDescriptor rule1 = constraintIndexRule(ruleId1, labelId, propertyKeyId, DESCRIPTOR, ruleId2);
            rule1 = tx.completeConfiguration(rule1);
            ConstraintDescriptor rule2 = uniquenessConstraintRule(ruleId2, labelId, propertyKeyId, ruleId2);
            serializeRule(rule1, after1, tx, next);
            serializeRule(rule2, after2, tx, next);
            tx.nodeLabel(labelId, "label", false);
            tx.propertyKey(propertyKeyId, "property", false);
            tx.createSchema(before1, after1, rule1);
            tx.createSchema(before2, after2, rule2);
            descriptor.set(rule1);
        }
    });
    fixture.indexingService().activateIndex(descriptor.get());
    // when
    ConsistencySummaryStatistics stats = check();
    // then
    on(stats).verify(RecordType.SCHEMA, 2).andThatsAllFolks();
}
Also used : TransactionDataBuilder(org.neo4j.consistency.checking.GraphStoreFixture.TransactionDataBuilder) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) IdGenerator(org.neo4j.consistency.checking.GraphStoreFixture.IdGenerator) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) KernelException(org.neo4j.exceptions.KernelException) GraphStoreFixture(org.neo4j.consistency.checking.GraphStoreFixture) ConsistencySummaryStatistics(org.neo4j.consistency.report.ConsistencySummaryStatistics) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 48 with KernelException

use of org.neo4j.exceptions.KernelException in project neo4j by neo4j.

the class NodeGetUniqueFromIndexSeekIT method shouldBlockUniqueIndexSeekFromCompetingTransaction.

// ( timeout = 10_000 )
@Test
void shouldBlockUniqueIndexSeekFromCompetingTransaction() throws Exception {
    // This is the interleaving that we are trying to verify works correctly:
    // ----------------------------------------------------------------------
    // Thread1 (main)        : Thread2
    // create unique node    :
    // lookup(node)          :
    // open start latch ----->
    // |                  : lookup(node)
    // wait for T2 to block  :      |
    // :    *block*
    // commit --------------->   *unblock*
    // wait for T2 end latch :      |
    // : finish transaction
    // : open end latch
    // *unblock* <-------------‘
    // assert that we complete before timeout
    final DoubleLatch latch = new DoubleLatch();
    IndexDescriptor index = createUniquenessConstraint(labelId, propertyId1);
    Value value = Values.of("value");
    Write write = dataWriteInNewTransaction();
    long nodeId = write.nodeCreate();
    write.nodeAddLabel(nodeId, labelId);
    // This adds the node to the unique index and should take an index write lock
    write.nodeSetProperty(nodeId, propertyId1, value);
    Runnable runnableForThread2 = () -> {
        latch.waitForAllToStart();
        try (KernelTransaction tx = kernel.beginTransaction(KernelTransaction.Type.IMPLICIT, LoginContext.AUTH_DISABLED)) {
            try (NodeValueIndexCursor cursor = tx.cursors().allocateNodeValueIndexCursor(tx.cursorContext(), tx.memoryTracker())) {
                tx.dataRead().lockingNodeUniqueIndexSeek(index, cursor, exact(propertyId1, value));
            }
            tx.commit();
        } catch (KernelException e) {
            throw new RuntimeException(e);
        } finally {
            latch.finish();
        }
    };
    Thread thread2 = new Thread(runnableForThread2, "Transaction Thread 2");
    thread2.start();
    latch.startAndWaitForAllToStart();
    while ((thread2.getState() != Thread.State.TIMED_WAITING) && (thread2.getState() != Thread.State.WAITING)) {
        Thread.yield();
    }
    commit();
    latch.waitForAllToFinish();
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Value(org.neo4j.values.storable.Value) DoubleLatch(org.neo4j.test.DoubleLatch) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) KernelException(org.neo4j.exceptions.KernelException) Test(org.junit.jupiter.api.Test)

Example 49 with KernelException

use of org.neo4j.exceptions.KernelException in project neo4j by neo4j.

the class IndexIT method createIndexForAnotherLabelWhileHoldingSharedLockOnOtherLabel.

@Test
void createIndexForAnotherLabelWhileHoldingSharedLockOnOtherLabel() throws KernelException, ExecutionException, InterruptedException {
    TokenWrite tokenWrite = tokenWriteInNewTransaction();
    int label2 = tokenWrite.labelGetOrCreateForName("Label2");
    commit();
    Write write = dataWriteInNewTransaction();
    long nodeId = write.nodeCreate();
    write.nodeAddLabel(nodeId, label2);
    try (var ignored = captureTransaction()) {
        executorService.submit(() -> {
            try {
                schemaWriteInNewTransaction().indexCreate(schema, null);
                commit();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).get();
    }
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) ExecutionException(java.util.concurrent.ExecutionException) KernelException(org.neo4j.exceptions.KernelException) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 50 with KernelException

use of org.neo4j.exceptions.KernelException in project neo4j by neo4j.

the class ExceptionRepresentationTest method shouldExcludeLegacyFormatIfAsked.

@Test
void shouldExcludeLegacyFormatIfAsked() throws Exception {
    // Given
    ExceptionRepresentation rep = new ExceptionRepresentation(new KernelException(UnknownError, "Hello") {
    }, /*legacy*/
    false);
    // When
    JsonNode out = serialize(rep);
    // Then
    assertThat(out.get("errors").get(0).get("code").asText()).isEqualTo("Neo.DatabaseError.General.UnknownError");
    assertThat(out.get("errors").get(0).get("message").asText()).isEqualTo("Hello");
    assertThat(out.has("message")).isEqualTo(false);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) KernelException(org.neo4j.exceptions.KernelException) Test(org.junit.jupiter.api.Test)

Aggregations

KernelException (org.neo4j.exceptions.KernelException)58 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)22 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)21 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)16 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)15 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)13 TokenCapacityExceededKernelException (org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException)12 Test (org.junit.jupiter.api.Test)11 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)10 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)10 QueryExecutionKernelException (org.neo4j.kernel.impl.query.QueryExecutionKernelException)10 List (java.util.List)8 NotFoundException (org.neo4j.graphdb.NotFoundException)8 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)8 Arrays (java.util.Arrays)7 Collectors (java.util.stream.Collectors)7 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)7 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)7 IllegalTokenNameException (org.neo4j.internal.kernel.api.exceptions.schema.IllegalTokenNameException)7 ArrayList (java.util.ArrayList)6