Search in sources :

Example 1 with AWAIT_REFRESH

use of org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.AWAIT_REFRESH in project neo4j by neo4j.

the class FulltextProceduresTest method concurrentPopulationAndUpdatesToAnEventuallyConsistentIndexMustEventuallyResultInCorrectIndexState.

@Test
void concurrentPopulationAndUpdatesToAnEventuallyConsistentIndexMustEventuallyResultInCorrectIndexState() throws Exception {
    String oldValue = "red";
    String newValue = "green";
    // First we create the nodes and relationships with the property value "red".
    LongHashSet nodeIds = new LongHashSet();
    LongHashSet relIds = new LongHashSet();
    generateNodesAndRelationshipsWithProperty(200, nodeIds, relIds, oldValue);
    // Then, in two concurrent transactions, we create our indexes AND change all the property values to "green".
    CountDownLatch readyLatch = new CountDownLatch(2);
    BinaryLatch startLatch = new BinaryLatch();
    Runnable createIndexes = () -> {
        readyLatch.countDown();
        startLatch.await();
        try (Transaction tx = db.beginTx()) {
            tx.execute(format(NODE_CREATE, DEFAULT_NODE_IDX_NAME, asStrList(LABEL.name()), asStrList(PROP) + EVENTUALLY_CONSISTENT));
            tx.execute(format(RELATIONSHIP_CREATE, DEFAULT_REL_IDX_NAME, asStrList(REL.name()), asStrList(PROP) + EVENTUALLY_CONSISTENT));
            tx.commit();
        }
    };
    Runnable makeAllEntitiesGreen = () -> {
        try (Transaction tx = db.beginTx()) {
            // Prepare our transaction state first.
            nodeIds.forEach(nodeId -> tx.getNodeById(nodeId).setProperty(PROP, newValue));
            relIds.forEach(relId -> tx.getRelationshipById(relId).setProperty(PROP, newValue));
            // Okay, NOW we're ready to race!
            readyLatch.countDown();
            startLatch.await();
            tx.commit();
        }
    };
    ExecutorService executor = Executors.newFixedThreadPool(2);
    Future<?> future1 = executor.submit(createIndexes);
    Future<?> future2 = executor.submit(makeAllEntitiesGreen);
    readyLatch.await();
    startLatch.release();
    // Finally, when everything has settled down, we should see that all of the nodes and relationships are indexed with the value "green".
    try {
        future1.get();
        future2.get();
        awaitIndexesOnline();
        try (Transaction tx = db.beginTx()) {
            tx.execute(AWAIT_REFRESH).close();
        }
        assertQueryFindsIds(db, true, DEFAULT_NODE_IDX_NAME, newValue, nodeIds);
        assertQueryFindsIds(db, false, DEFAULT_REL_IDX_NAME, newValue, relIds);
    } finally {
        IOUtils.closeAllSilently(executor::shutdown);
    }
}
Also used : Arrays(java.util.Arrays) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Config(org.neo4j.configuration.Config) Value(org.neo4j.values.storable.Value) FulltextIndexProviderFactory(org.neo4j.kernel.impl.index.schema.FulltextIndexProviderFactory) NullLogProvider(org.neo4j.logging.NullLogProvider) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) Future(java.util.concurrent.Future) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) FulltextProcedures(org.neo4j.procedure.builtin.FulltextProcedures) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) IndexSettingImpl(org.neo4j.graphdb.schema.IndexSettingImpl) ThreadTestUtils(org.neo4j.test.ThreadTestUtils) Transaction(org.neo4j.graphdb.Transaction) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) MethodSource(org.junit.jupiter.params.provider.MethodSource) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) Result(org.neo4j.graphdb.Result) RepeatedPropertyInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedPropertyInSchemaException) Set(java.util.Set) DROP(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.DROP) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) RepeatedLabelInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedLabelInSchemaException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RelationshipType(org.neo4j.graphdb.RelationshipType) LongHashSet.newSetWith(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet.newSetWith) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) SHOW_FULLTEXT_INDEXES(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.SHOW_FULLTEXT_INDEXES) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RELATIONSHIP_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.RELATIONSHIP_CREATE) CsvSource(org.junit.jupiter.params.provider.CsvSource) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Label(org.neo4j.graphdb.Label) AWAIT_REFRESH(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.AWAIT_REFRESH) DB_AWAIT_INDEX(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.DB_AWAIT_INDEX) IOUtils(org.neo4j.io.IOUtils) ArrayUtils(org.apache.commons.lang3.ArrayUtils) EnumSource(org.junit.jupiter.params.provider.EnumSource) HashMap(java.util.HashMap) NODE_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.NODE_CREATE) TreeSet(java.util.TreeSet) Node(org.neo4j.graphdb.Node) IndexType(org.neo4j.graphdb.schema.IndexType) HashSet(java.util.HashSet) Iterables(org.neo4j.internal.helpers.collection.Iterables) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NON_ASCII_LETTERS(org.neo4j.kernel.api.impl.fulltext.analyzer.StandardFoldingAnalyzer.NON_ASCII_LETTERS) ExecutorService(java.util.concurrent.ExecutorService) LIST_AVAILABLE_ANALYZERS(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.LIST_AVAILABLE_ANALYZERS) Files(java.nio.file.Files) Iterables.stream(org.neo4j.internal.helpers.collection.Iterables.stream) ProgressMonitorFactory(org.neo4j.internal.helpers.progress.ProgressMonitorFactory) InputStreamReader(java.io.InputStreamReader) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) TimeUnit(java.util.concurrent.TimeUnit) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) RepeatedRelationshipTypeInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedRelationshipTypeInSchemaException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collectors.toList(java.util.stream.Collectors.toList) ASCIIFoldingFilter(org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter) Relationship(org.neo4j.graphdb.Relationship) FulltextIndexProceduresUtil.asStrList(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.asStrList) BufferedReader(java.io.BufferedReader) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexSetting(org.neo4j.graphdb.schema.IndexSetting) ConsistencyFlags(org.neo4j.consistency.checking.full.ConsistencyFlags) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Transaction(org.neo4j.graphdb.Transaction) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 String.format (java.lang.String.format)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1