Search in sources :

Example 1 with RandomValues

use of org.neo4j.values.storable.RandomValues in project neo4j by neo4j.

the class FulltextIndexConsistencyCheckIT method consistencyCheckerMustBeAbleToRunOnStoreWithFulltextIndexes.

@Test
void consistencyCheckerMustBeAbleToRunOnStoreWithFulltextIndexes() throws Exception {
    GraphDatabaseService db = createDatabase();
    Label[] labels = IntStream.range(1, 7).mapToObj(i -> Label.label("LABEL" + i)).toArray(Label[]::new);
    RelationshipType[] relTypes = IntStream.range(1, 5).mapToObj(i -> RelationshipType.withName("REL" + i)).toArray(RelationshipType[]::new);
    String[] propertyKeys = IntStream.range(1, 7).mapToObj(i -> "PROP" + i).toArray(String[]::new);
    RandomValues randomValues = random.randomValues();
    try (Transaction tx = db.beginTx()) {
        int nodeCount = 1000;
        List<Node> nodes = new ArrayList<>(nodeCount);
        for (int i = 0; i < nodeCount; i++) {
            Label[] nodeLabels = random.ints(random.nextInt(labels.length), 0, labels.length).distinct().mapToObj(x -> labels[x]).toArray(Label[]::new);
            Node node = tx.createNode(nodeLabels);
            Stream.of(propertyKeys).forEach(p -> node.setProperty(p, random.nextBoolean() ? p : randomValues.nextValue().asObject()));
            nodes.add(node);
            int localRelCount = Math.min(nodes.size(), 5);
            random.ints(localRelCount, 0, localRelCount).distinct().mapToObj(x -> node.createRelationshipTo(nodes.get(x), relTypes[random.nextInt(relTypes.length)])).forEach(r -> Stream.of(propertyKeys).forEach(p -> r.setProperty(p, random.nextBoolean() ? p : randomValues.nextValue().asObject())));
        }
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        for (int i = 1; i < labels.length; i++) {
            tx.execute(format(NODE_CREATE, "nodes" + i, asStrList(Arrays.stream(labels).limit(i).map(Label::name).toArray(String[]::new)), asStrList(Arrays.copyOf(propertyKeys, i)))).close();
        }
        for (int i = 1; i < relTypes.length; i++) {
            tx.execute(format(RELATIONSHIP_CREATE, "rels" + i, asStrList(Arrays.stream(relTypes).limit(i).map(RelationshipType::name).toArray(String[]::new)), asStrList(Arrays.copyOf(propertyKeys, i)))).close();
        }
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(2, TimeUnit.MINUTES);
        tx.commit();
    }
    managementService.shutdown();
    assertIsConsistent(checkConsistency());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RandomExtension(org.neo4j.test.extension.RandomExtension) Date(java.util.Date) Config(org.neo4j.configuration.Config) Disabled(org.junit.jupiter.api.Disabled) FailingGenericNativeIndexProviderFactory(org.neo4j.kernel.impl.index.schema.FailingGenericNativeIndexProviderFactory) NullLogProvider(org.neo4j.logging.NullLogProvider) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) RandomValues(org.neo4j.values.storable.RandomValues) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RandomRule(org.neo4j.test.rule.RandomRule) AbstractBaseRecord(org.neo4j.kernel.impl.store.record.AbstractBaseRecord) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) SKIP_ONLINE_UPDATES(org.neo4j.kernel.impl.index.schema.FailingGenericNativeIndexProviderFactory.FailureType.SKIP_ONLINE_UPDATES) IndexUpdateMode(org.neo4j.kernel.impl.api.index.IndexUpdateMode) IndexDefinitionImpl(org.neo4j.kernel.impl.coreapi.schema.IndexDefinitionImpl) Transaction(org.neo4j.graphdb.Transaction) TokenIndexProviderFactory(org.neo4j.kernel.impl.index.schema.TokenIndexProviderFactory) RecordStorageEngine(org.neo4j.internal.recordstorage.RecordStorageEngine) IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) Neo4jLayoutExtension(org.neo4j.test.extension.Neo4jLayoutExtension) String.format(java.lang.String.format) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) RelationshipType(org.neo4j.graphdb.RelationshipType) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) INDEX_PROVIDERS_FILTER(org.neo4j.test.TestDatabaseManagementServiceBuilder.INDEX_PROVIDERS_FILTER) IntStream(java.util.stream.IntStream) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RELATIONSHIP_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.RELATIONSHIP_CREATE) Label(org.neo4j.graphdb.Label) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) NODE_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.NODE_CREATE) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) IndexType(org.neo4j.graphdb.schema.IndexType) Values(org.neo4j.values.storable.Values) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NeoStores(org.neo4j.kernel.impl.store.NeoStores) Inject(org.neo4j.test.extension.Inject) DependencyResolver(org.neo4j.common.DependencyResolver) Files(java.nio.file.Files) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) IOException(java.io.IOException) ProgressMonitorFactory(org.neo4j.internal.helpers.progress.ProgressMonitorFactory) TimeUnit(java.util.concurrent.TimeUnit) ConsistencyCheckIncompleteException(org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException) AfterEach(org.junit.jupiter.api.AfterEach) Relationship(org.neo4j.graphdb.Relationship) FulltextIndexProceduresUtil.asStrList(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.asStrList) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConsistencyFlags(org.neo4j.consistency.checking.full.ConsistencyFlags) RecordLoad(org.neo4j.kernel.impl.store.record.RecordLoad) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) ArrayList(java.util.ArrayList) RandomValues(org.neo4j.values.storable.RandomValues) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 2 with RandomValues

use of org.neo4j.values.storable.RandomValues in project neo4j by neo4j.

the class IndexPopulationStressTest method updater.

private Runnable updater(AtomicReferenceArray<List<ValueIndexEntryUpdate<?>>> lastBatches, CountDownLatch insertersDone, ReadWriteLock updateLock, Collection<ValueIndexEntryUpdate<?>> updates) {
    return throwing(() -> {
        // Entity ids that have been removed, so that additions can reuse them
        List<Long> removed = new ArrayList<>();
        RandomValues randomValues = RandomValues.create(new Random(random.seed() + THREADS));
        while (insertersDone.getCount() > 0) {
            // Do updates now and then
            Thread.sleep(10);
            updateLock.writeLock().lock();
            try (IndexUpdater updater = populator.newPopulatingUpdater(nodePropertyAccessor, NULL)) {
                for (int i = 0; i < THREADS; i++) {
                    List<ValueIndexEntryUpdate<?>> batch = lastBatches.get(i);
                    if (batch != null) {
                        ValueIndexEntryUpdate<?> update = null;
                        switch(randomValues.nextInt(3)) {
                            case // add
                            0:
                                if (!removed.isEmpty()) {
                                    Long id = removed.remove(randomValues.nextInt(removed.size()));
                                    update = add(id, descriptor, valueGenerator.apply(randomValues));
                                }
                                break;
                            case // remove
                            1:
                                ValueIndexEntryUpdate<?> removal = batch.get(randomValues.nextInt(batch.size()));
                                update = remove(removal.getEntityId(), descriptor, removal.values());
                                removed.add(removal.getEntityId());
                                break;
                            case // change
                            2:
                                removal = batch.get(randomValues.nextInt(batch.size()));
                                change(removal.getEntityId(), descriptor, removal.values(), toArray(valueGenerator.apply(randomValues)));
                                break;
                            default:
                                throw new IllegalArgumentException();
                        }
                        if (update != null) {
                            updater.process(update);
                            updates.add(update);
                        }
                    }
                }
            } finally {
                updateLock.writeLock().unlock();
            }
        }
    });
}
Also used : Random(java.util.Random) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArrayList(java.util.ArrayList) ValueIndexEntryUpdate(org.neo4j.storageengine.api.ValueIndexEntryUpdate) RandomValues(org.neo4j.values.storable.RandomValues) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater)

Example 3 with RandomValues

use of org.neo4j.values.storable.RandomValues in project neo4j by neo4j.

the class ConcurrentUpdateIT method populateDbWithConcurrentUpdates.

@Test
void populateDbWithConcurrentUpdates() throws Exception {
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    GraphDatabaseService database = managementService.database(DEFAULT_DATABASE_NAME);
    try {
        RandomValues randomValues = RandomValues.create();
        int counter = 1;
        for (int j = 0; j < 100; j++) {
            try (Transaction transaction = database.beginTx()) {
                for (int i = 0; i < 5; i++) {
                    Node node = transaction.createNode(Label.label("label" + counter));
                    node.setProperty("property", randomValues.nextValue().asObject());
                }
                transaction.commit();
            }
            counter++;
        }
        int populatorCount = 5;
        ExecutorService executor = Executors.newFixedThreadPool(populatorCount);
        CountDownLatch startSignal = new CountDownLatch(1);
        AtomicBoolean endSignal = new AtomicBoolean();
        for (int i = 0; i < populatorCount; i++) {
            executor.submit(new Populator(database, counter, startSignal, endSignal));
        }
        try {
            try (Transaction transaction = database.beginTx()) {
                transaction.schema().indexFor(Label.label("label10")).on("property").create();
                transaction.commit();
            }
            startSignal.countDown();
            try (Transaction transaction = database.beginTx()) {
                transaction.schema().awaitIndexesOnline(populatorCount, TimeUnit.MINUTES);
                transaction.commit();
            }
        } finally {
            endSignal.set(true);
            executor.shutdown();
        // Basically we don't care to await their completion because they've done their job
        }
    } finally {
        managementService.shutdown();
        ConsistencyCheckService consistencyCheckService = new ConsistencyCheckService();
        Config config = Config.defaults(GraphDatabaseSettings.pagecache_memory, "8m");
        consistencyCheckService.runFullConsistencyCheck(databaseLayout, config, ProgressMonitorFactory.NONE, new Log4jLogProvider(System.out), false);
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Config(org.neo4j.configuration.Config) Log4jLogProvider(org.neo4j.logging.log4j.Log4jLogProvider) Node(org.neo4j.graphdb.Node) RandomValues(org.neo4j.values.storable.RandomValues) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Transaction(org.neo4j.graphdb.Transaction) ExecutorService(java.util.concurrent.ExecutorService) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 4 with RandomValues

use of org.neo4j.values.storable.RandomValues in project neo4j by neo4j.

the class IndexProvidedValuesNativeBTree10Test method createTestGraph.

@Override
public void createTestGraph(GraphDatabaseService graphDb) {
    try (Transaction tx = graphDb.beginTx()) {
        getEntityControl().createIndex(tx, TOKEN, PROP, PROP_INDEX);
        getEntityControl().createIndex(tx, TOKEN, PROP, PRIP, PROP_PRIP_INDEX);
        tx.commit();
    }
    try (Transaction tx = graphDb.beginTx()) {
        tx.schema().awaitIndexesOnline(5, MINUTES);
        tx.commit();
    }
    try (Transaction tx = graphDb.beginTx()) {
        RandomValues randomValues = randomRule.randomValues();
        ValueType[] allExceptNonSortable = RandomValues.excluding(ValueType.STRING, ValueType.STRING_ARRAY);
        for (int i = 0; i < N_ENTITIES; i++) {
            var node = getEntityControl().createEntity(tx, TOKEN);
            Value propValue = randomValues.nextValueOfTypes(allExceptNonSortable);
            node.setProperty(PROP, propValue.asObject());
            Value pripValue = randomValues.nextValueOfTypes(allExceptNonSortable);
            node.setProperty(PRIP, pripValue.asObject());
            singlePropValues.add(propValue);
            doublePropValues.add(ValueTuple.of(propValue, pripValue));
        }
        tx.commit();
    }
    singlePropValues.sort(Values.COMPARATOR);
    doublePropValues.sort(ValueTuple.COMPARATOR);
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ValueType(org.neo4j.values.storable.ValueType) Value(org.neo4j.values.storable.Value) RandomValues(org.neo4j.values.storable.RandomValues)

Example 5 with RandomValues

use of org.neo4j.values.storable.RandomValues in project neo4j by neo4j.

the class AbstractIndexProvidedOrderTest method createTestGraph.

@Override
public void createTestGraph(GraphDatabaseService graphDb) {
    try (Transaction tx = graphDb.beginTx()) {
        getEntityControl().createIndex(tx, TOKEN, PROPERTY_KEY, INDEX_NAME);
        tx.commit();
    }
    try (Transaction tx = graphDb.beginTx()) {
        tx.schema().awaitIndexesOnline(5, MINUTES);
        tx.commit();
    }
    RandomValues randomValues = randomRule.randomValues();
    ValueType[] allExceptNonOrderable = RandomValues.excluding(ValueType.STRING, ValueType.STRING_ARRAY, ValueType.GEOGRAPHIC_POINT, ValueType.GEOGRAPHIC_POINT_ARRAY, ValueType.GEOGRAPHIC_POINT_3D, ValueType.GEOGRAPHIC_POINT_3D_ARRAY, ValueType.CARTESIAN_POINT, ValueType.CARTESIAN_POINT_ARRAY, ValueType.CARTESIAN_POINT_3D, ValueType.CARTESIAN_POINT_3D_ARRAY);
    targetedTypes = randomValues.selection(allExceptNonOrderable, 1, allExceptNonOrderable.length, false);
    targetedTypes = ensureHighEnoughCardinality(targetedTypes);
    try (Transaction tx = graphDb.beginTx()) {
        for (int i = 0; i < N_ENTITIES; i++) {
            var node = getEntityControl().createEntity(tx, TOKEN);
            Value propValue;
            EntityValueTuple singleValue;
            do {
                propValue = randomValues.nextValueOfTypes(targetedTypes);
                singleValue = new EntityValueTuple(node.getId(), propValue);
            } while (singlePropValues.contains(singleValue));
            singlePropValues.add(singleValue);
            node.setProperty(PROPERTY_KEY, propValue.asObject());
        }
        tx.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ValueType(org.neo4j.values.storable.ValueType) Value(org.neo4j.values.storable.Value) RandomValues(org.neo4j.values.storable.RandomValues)

Aggregations

RandomValues (org.neo4j.values.storable.RandomValues)10 Transaction (org.neo4j.graphdb.Transaction)7 Node (org.neo4j.graphdb.Node)4 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)3 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)3 Value (org.neo4j.values.storable.Value)3 ValueType (org.neo4j.values.storable.ValueType)3 Random (java.util.Random)2 Config (org.neo4j.configuration.Config)2 ConsistencyCheckService (org.neo4j.consistency.ConsistencyCheckService)2 Label (org.neo4j.graphdb.Label)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)2 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1