Jackie-Jiang commented on code in PR #8636: URL: https://github.com/apache/pinot/pull/8636#discussion_r867192427
########## pinot-core/src/test/java/org/apache/pinot/queries/RealTimeNativeVsLuceneTest.java: ########## @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.queries; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.apache.lucene.search.SearcherManager; +import org.apache.pinot.segment.local.realtime.impl.invertedindex.NativeMutableTextIndex; +import org.apache.pinot.segment.local.realtime.impl.invertedindex.RealtimeLuceneTextIndex; +import org.apache.pinot.segment.spi.IndexSegment; +import org.roaringbitmap.PeekableIntIterator; +import org.roaringbitmap.buffer.MutableRoaringBitmap; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + + +public class RealTimeNativeVsLuceneTest extends BaseQueriesTest { Review Comment: Rename ```suggestion public class NativeVsLuceneMutableTextIndexTest extends BaseQueriesTest { ``` ########## pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableTextIndexConcurrentTest.java: ########## @@ -0,0 +1,251 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.segment.local.realtime.impl.invertedindex; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + + +public class NativeMutableTextIndexConcurrentTest { + private ExecutorService _threadPool; + private Set<String> _resultSet; + + @BeforeClass + private void setup() { + _threadPool = Executors.newFixedThreadPool(10); + _resultSet = new ConcurrentSkipListSet<>(); + } + + @AfterClass + private void shutDown() { + _threadPool.shutdown(); + } + + @Test + public void testConcurrentWriteAndRead() + throws InterruptedException, IOException { + CountDownLatch countDownLatch = new CountDownLatch(2); + List<String> words = new ArrayList<>(); + words.add("ab"); + words.add("abba"); + words.add("aba"); + words.add("bab"); + words.add("cdd"); + words.add("efg"); + + try (NativeMutableTextIndex textIndex = new NativeMutableTextIndex("testFSTColumn")) { + _threadPool.submit(() -> { + try { + performReads(textIndex, words, 20, 200, countDownLatch); + } catch (InterruptedException e) { + e.printStackTrace(); Review Comment: We want to throw the exception (wrap it in `RuntimeException`) instead of just printing the stack trace. Same for other places ########## pinot-segment-local/src/test/java/org/apache/pinot/segment/local/utils/nativefst/mutablefst/MutableFSTConcurrentTest.java: ########## @@ -102,8 +100,54 @@ public void testConcurrentWriteAndRead() assertTrue("efg not found in result set", _resultSet.contains("efg")); } - private void performReads(MutableFST fst, List<String> words, int count, - long sleepTime) + @Test + public void testConcurrentLongWriteAndRead() + throws InterruptedException { + MutableFST mutableFST = new MutableFSTImpl(); + List<String> words = new ArrayList<>(); + CountDownLatch countDownLatch = new CountDownLatch(2); + + mutableFST.addPath("ab", 1); + + List<Pair<String, Integer>> wordsWithMetadata = new ArrayList<>(); + + // Add some write pressure + wordsWithMetadata.add(Pair.of("egegdgrbsbrsegzgzegzegegjntnmtj", 2)); + wordsWithMetadata.add(Pair.of("hrwbwefweg4wreghrtbrassregfesfefefefzew4ere", 2)); + wordsWithMetadata.add(Pair.of("easzegfegrertegbxzzez3erfezgzeddzdewstfefed", 2)); + wordsWithMetadata.add(Pair.of("tjntrhndsrsgezgrsxzetgteszetgezfzezedrefzdzdzdzdz", 2)); + wordsWithMetadata.add(Pair.of("abacxcvbnmlkjjhgfsaqwertyuioopzxcvbnmllkjshfgsfawieeiuefgeurfeoafa", 2)); + + words.add("abacxcvbnmlkjjhgfsaqwertyuioopzxcvbnmllkjshfgsfawieeiuefgeurfeoafa"); + + _threadPool.submit(() -> { + try { + performReads(mutableFST, words, 10, 10, countDownLatch); + } catch (InterruptedException e) { + e.printStackTrace(); Review Comment: Wrap it in `RuntimeException` ########## pinot-segment-local/src/test/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableTextIndexReaderWriterTest.java: ########## @@ -0,0 +1,62 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.segment.local.realtime.impl.invertedindex; + +import java.io.IOException; +import org.testng.Assert; +import org.testng.annotations.Test; + + +public class NativeMutableTextIndexReaderWriterTest { + + @Test + public void testIndexWriterReader() + throws IOException { + String[] uniqueValues = new String[4]; + uniqueValues[0] = "hello-world"; + uniqueValues[1] = "hello-world123"; + uniqueValues[2] = "still"; + uniqueValues[3] = "zoobar"; + + try (NativeMutableTextIndex textIndex = new NativeMutableTextIndex("testFSTColumn")) { + for (int i = 0; i < 4; i++) { + textIndex.add(uniqueValues[i]); + } + + int[] matchedDocIds = textIndex.getDocIds("hello.*").toArray(); + Assert.assertEquals(2, matchedDocIds.length); Review Comment: (minor) Import `Assert.assertEquals` ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/impl/invertedindex/NativeMutableTextIndex.java: ########## @@ -0,0 +1,121 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.segment.local.realtime.impl.invertedindex; + +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator; +import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFST; +import org.apache.pinot.segment.local.utils.nativefst.mutablefst.MutableFSTImpl; +import org.apache.pinot.segment.local.utils.nativefst.utils.RealTimeRegexpMatcher; +import org.apache.pinot.segment.spi.index.mutable.MutableTextIndex; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +public class NativeMutableTextIndex implements MutableTextIndex { + private final String _column; + private final MutableFST _mutableFST; + private final RealtimeInvertedIndex _invertedIndex; + //TODO: Move to mutable dictionary + private final Object2IntOpenHashMap<String> _termToDictIdMapping; + private final ReentrantReadWriteLock.ReadLock _readLock; + private final ReentrantReadWriteLock.WriteLock _writeLock; + + private int _nextDocId = 0; + private int _nextDictId = 0; + + public NativeMutableTextIndex(String column) { + _column = column; + _mutableFST = new MutableFSTImpl(); + _termToDictIdMapping = new Object2IntOpenHashMap<>(); + _invertedIndex = new RealtimeInvertedIndex(); + + ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); + _readLock = readWriteLock.readLock(); + _writeLock = readWriteLock.writeLock(); + } + + @Override + public void add(String document) { + List<String> tokens; + try { + tokens = analyze(document, new StandardAnalyzer(LuceneTextIndexCreator.ENGLISH_STOP_WORDS_SET)); Review Comment: Did some research, and I think `StandardAnalyzer` can be defined as constant. It should be thread safe in single writer context ########## pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/NativeRealtimeClusterIntegrationTest.java: ########## @@ -0,0 +1,214 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.integration.tests; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.annotation.Nullable; +import org.apache.avro.file.DataFileWriter; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericDatumWriter; +import org.apache.commons.io.FileUtils; +import org.apache.pinot.spi.config.table.FieldConfig; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.Schema; +import org.apache.pinot.util.TestUtils; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import static org.testng.AssertJUnit.fail; + + +public class NativeRealtimeClusterIntegrationTest extends BaseClusterIntegrationTest { Review Comment: Let's merge this into the existing realtime text index test ########## pinot-core/src/test/java/org/apache/pinot/queries/RealTimeNativeVsLuceneTest.java: ########## @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.queries; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.apache.lucene.search.SearcherManager; +import org.apache.pinot.segment.local.realtime.impl.invertedindex.NativeMutableTextIndex; +import org.apache.pinot.segment.local.realtime.impl.invertedindex.RealtimeLuceneTextIndex; +import org.apache.pinot.segment.spi.IndexSegment; +import org.roaringbitmap.PeekableIntIterator; +import org.roaringbitmap.buffer.MutableRoaringBitmap; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + + +public class RealTimeNativeVsLuceneTest extends BaseQueriesTest { Review Comment: This is not really a queries test. We should either make it a unit test for the text index, or make it similar to `NativeAndLuceneComparisonTest` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org