You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2021/01/26 03:34:50 UTC

[GitHub] [lucene-solr] zhaih opened a new pull request #2246: LUCENE-9694: New tool for creating a deterministic index

zhaih opened a new pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246


   <!--
   _(If you are a project committer then you may remove some/all of the following template.)_
   
   Before creating a pull request, please file an issue in the ASF Jira system for Lucene or Solr:
   
   * https://issues.apache.org/jira/projects/LUCENE
   * https://issues.apache.org/jira/projects/SOLR
   
   You will need to create an account in Jira in order to create an issue.
   
   The title of the PR should reference the Jira issue number in the form:
   
   * LUCENE-####: <short description of problem or changes>
   * SOLR-####: <short description of problem or changes>
   
   LUCENE and SOLR must be fully capitalized. A short description helps people scanning pull requests for items they can work on.
   
   Properly referencing the issue in the title ensures that Jira is correctly updated with code review comments and commits. -->
   
   
   # Description
   
   Create a new tool `IndexRearranger`, which could rearrange a built index concurrently to desired segment number and document distribution
   
   # Solution
   
   Essentially combines `IndexWriter.addIndexes` and `FilterCodecReader` to select only certain documents into 1 segment
   
   # Tests
   
   Added one unit test testing rearranger.
   
   # Checklist
   
   Please review the following and check all that apply:
   
   - [x] I have reviewed the guidelines for [How to Contribute](https://wiki.apache.org/solr/HowToContribute) and my code conforms to the standards described there to the best of my ability.
   - [x] I have created a Jira issue and added the issue ID to my pull request title.
   - [x] I have given Solr maintainers [access](https://help.github.com/en/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork) to contribute to my PR branch. (optional but recommended)
   - [x] I have developed this patch against the `master` branch.
   - [x] I have run `./gradlew check`.
   - [x] I have added tests for my changes.
   - [ ] I have added documentation for the [Ref Guide](https://github.com/apache/lucene-solr/tree/master/solr/solr-ref-guide) (for Solr changes only).
   


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r564723593



##########
File path: lucene/misc/src/test/org/apache/lucene/misc/index/TestIndexRearranger.java
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.List;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestIndexRearranger extends LuceneTestCase {
+  public void testRearrange() throws Exception {
+    Directory inputDir = newDirectory();
+    IndexWriter w =
+        new IndexWriter(
+            inputDir,
+            new IndexWriterConfig(null)
+                .setOpenMode(IndexWriterConfig.OpenMode.CREATE)
+                .setMergePolicy(NoMergePolicy.INSTANCE)
+                .setIndexSort(new Sort(new SortField("ord", SortField.Type.INT))));
+    for (int i = 0; i < 100; i++) {
+      Document doc = new Document();
+      doc.add(new NumericDocValuesField("ord", i));
+      w.addDocument(doc);
+      if (i % 10 == 9) {
+        w.commit();
+      }
+    }
+    IndexReader reader = DirectoryReader.open(w);
+    assertEquals(10, reader.leaves().size());
+    reader.close();
+    w.close();
+
+    Directory outputDir = newDirectory();
+    IndexRearranger rearranger =
+        new IndexRearranger(
+            inputDir,
+            outputDir,
+            new IndexWriterConfig(null)
+                .setOpenMode(IndexWriterConfig.OpenMode.CREATE)
+                .setMergePolicy(NoMergePolicy.INSTANCE)
+                .setIndexSort(new Sort(new SortField("ord", SortField.Type.INT))),
+            List.of(new OddDocSelector(), new EvenDocSelector()));
+    rearranger.execute();
+    reader = DirectoryReader.open(outputDir);
+    assertEquals(2, reader.leaves().size());
+    LeafReader segment1 = reader.leaves().get(0).reader();
+    assertEquals(50, segment1.numDocs());
+    NumericDocValues numericDocValues = segment1.getNumericDocValues("ord");
+    assertTrue(numericDocValues.advanceExact(0));
+    boolean odd = numericDocValues.longValue() % 2 == 1;
+    for (int i = 1; i < 50; i++) {
+      assertTrue(numericDocValues.advanceExact(i));
+      if (odd) {
+        assertEquals(1, numericDocValues.longValue() % 2);
+      } else {
+        assertEquals(0, numericDocValues.longValue() % 2);
+      }
+    }
+    LeafReader segment2 = reader.leaves().get(0).reader();
+    assertEquals(50, segment2.numDocs());
+    numericDocValues = segment2.getNumericDocValues("ord");
+    assertTrue(numericDocValues.advanceExact(0));
+    odd = numericDocValues.longValue() % 2 == 1;
+    for (int i = 1; i < 50; i++) {
+      assertTrue(numericDocValues.advanceExact(i));
+      if (odd) {
+        assertEquals(1, numericDocValues.longValue() % 2);
+      } else {
+        assertEquals(0, numericDocValues.longValue() % 2);
+      }
+    }
+    reader.close();
+    inputDir.close();
+    outputDir.close();
+  }
+
+  private class OddDocSelector implements IndexRearranger.DocumentSelector {

Review comment:
       Yeah, in extreme case we might be able to optimize the search performance by changing document distribution




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] mikemccand commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
mikemccand commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r566885578



##########
File path: lucene/CHANGES.txt
##########
@@ -252,7 +252,8 @@ Other
 New Features
 ---------------------
 
-* LUCENE-9694: New tool for creating a deterministic index (Haoyu Zhai)
+* LUCENE-9694: New tool for creating a deterministic index to enabling benchmarking changes

Review comment:
       s/`enabling`/`enable`




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r566324738



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/BinaryDocValueSelector.java
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+
+/** Select documents using binary doc values */
+public class BinaryDocValueSelector implements IndexRearranger.DocumentSelector, Serializable {
+
+  private final String field;
+  private final HashSet<String> keySet;
+
+  public BinaryDocValueSelector(String field, HashSet<String> keySet) {
+    this.field = field;
+    this.keySet = keySet;
+  }
+
+  @Override
+  public BitSet getFilteredLiveDocs(CodecReader reader) throws IOException {
+    BinaryDocValues binaryDocValues = reader.getBinaryDocValues(field);
+    Bits oldLiveDocs = reader.getLiveDocs();
+    FixedBitSet bits = new FixedBitSet(reader.maxDoc());
+    for (int i = 0; i < reader.maxDoc(); i++) {
+      if (oldLiveDocs != null && oldLiveDocs.get(i) == false) {
+        continue;
+      }
+      if (binaryDocValues.advanceExact(i)
+          && keySet.contains(binaryDocValues.binaryValue().utf8ToString())) {
+        bits.set(i);
+      }
+    }
+    return bits;
+  }
+
+  public static List<IndexRearranger.DocumentSelector> createFromExistIndex(

Review comment:
       Good idea




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r564725198



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {

Review comment:
       Good idea!




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] mikemccand commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
mikemccand commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r564484147



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {
+  protected final Directory input, output;
+  protected final IndexWriterConfig config;
+  protected final List<DocumentSelector> documentSelectors;
+
+  public IndexRearranger(
+      Directory input,
+      Directory output,
+      IndexWriterConfig config,
+      List<DocumentSelector> documentSelectors) {
+    this.input = input;
+    this.output = output;
+    this.config = config;
+    this.documentSelectors = documentSelectors;
+  }
+
+  public void execute() throws Exception {
+    config.setMergePolicy(
+        NoMergePolicy.INSTANCE); // do not merge since one addIndexes call create one segment
+    try (IndexWriter writer = new IndexWriter(output, config);
+        IndexReader reader = DirectoryReader.open(input)) {
+      ExecutorService executor =
+          Executors.newFixedThreadPool(
+              Math.min(Runtime.getRuntime().availableProcessors(), documentSelectors.size()),
+              new NamedThreadFactory("rearranger"));
+      ArrayList<Future<Void>> futures = new ArrayList<>();
+      for (DocumentSelector record : documentSelectors) {
+        Callable<Void> addSegment =
+            () -> {
+              addOneSegment(writer, reader, record);
+              return null;
+            };
+        futures.add(executor.submit(addSegment));
+      }
+      for (Future<Void> future : futures) {

Review comment:
       Yay, concurrent!  (thread-per-segment).  And ironically if you "optimize" (force-merge to one segment) your index, you lose all concurrency :) That's fine ... just don't force-merge!

##########
File path: lucene/CHANGES.txt
##########
@@ -11,6 +11,8 @@ New Features
 
 * LUCENE-9004: Approximate nearest vector search via NSW graphs
 
+* LUCENE-9694: New tool for creating a deterministic index (Haoyu Zhai)

Review comment:
       Probably we could also back-port this to Lucene 8.9, right?  We are not using any Lucene 9.x only APIs?
   
   If so, can you move this CHANGES entry down to that section?  (Hmm, if it does not yet exist, you can create a skeletal one).

##########
File path: lucene/misc/src/test/org/apache/lucene/misc/index/TestIndexRearranger.java
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.List;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestIndexRearranger extends LuceneTestCase {
+  public void testRearrange() throws Exception {
+    Directory inputDir = newDirectory();
+    IndexWriter w =
+        new IndexWriter(
+            inputDir,
+            new IndexWriterConfig(null)
+                .setOpenMode(IndexWriterConfig.OpenMode.CREATE)
+                .setMergePolicy(NoMergePolicy.INSTANCE)
+                .setIndexSort(new Sort(new SortField("ord", SortField.Type.INT))));
+    for (int i = 0; i < 100; i++) {
+      Document doc = new Document();
+      doc.add(new NumericDocValuesField("ord", i));
+      w.addDocument(doc);
+      if (i % 10 == 9) {
+        w.commit();
+      }
+    }
+    IndexReader reader = DirectoryReader.open(w);
+    assertEquals(10, reader.leaves().size());
+    reader.close();
+    w.close();
+
+    Directory outputDir = newDirectory();
+    IndexRearranger rearranger =
+        new IndexRearranger(
+            inputDir,
+            outputDir,
+            new IndexWriterConfig(null)
+                .setOpenMode(IndexWriterConfig.OpenMode.CREATE)
+                .setMergePolicy(NoMergePolicy.INSTANCE)
+                .setIndexSort(new Sort(new SortField("ord", SortField.Type.INT))),
+            List.of(new OddDocSelector(), new EvenDocSelector()));
+    rearranger.execute();
+    reader = DirectoryReader.open(outputDir);
+    assertEquals(2, reader.leaves().size());
+    LeafReader segment1 = reader.leaves().get(0).reader();
+    assertEquals(50, segment1.numDocs());
+    NumericDocValues numericDocValues = segment1.getNumericDocValues("ord");
+    assertTrue(numericDocValues.advanceExact(0));
+    boolean odd = numericDocValues.longValue() % 2 == 1;
+    for (int i = 1; i < 50; i++) {
+      assertTrue(numericDocValues.advanceExact(i));
+      if (odd) {
+        assertEquals(1, numericDocValues.longValue() % 2);
+      } else {
+        assertEquals(0, numericDocValues.longValue() % 2);
+      }
+    }
+    LeafReader segment2 = reader.leaves().get(0).reader();
+    assertEquals(50, segment2.numDocs());
+    numericDocValues = segment2.getNumericDocValues("ord");
+    assertTrue(numericDocValues.advanceExact(0));
+    odd = numericDocValues.longValue() % 2 == 1;
+    for (int i = 1; i < 50; i++) {
+      assertTrue(numericDocValues.advanceExact(i));
+      if (odd) {
+        assertEquals(1, numericDocValues.longValue() % 2);
+      } else {
+        assertEquals(0, numericDocValues.longValue() % 2);
+      }
+    }
+    reader.close();
+    inputDir.close();
+    outputDir.close();
+  }
+
+  private class OddDocSelector implements IndexRearranger.DocumentSelector {

Review comment:
       This looks very similar to "shard splitting" implementations!
   
   I think an interesting `DocumentSelector` might be one which assigns documents into a "perfect" logarithmic staircase according to some base and exponent, e.g. target 7 segments with 1M docs, 7 segments with 100K docs, 7 segments with 10K docs.  This would allow us to take any index and rearrange it into arbitrary multi-segment situations.  Fun!

##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {
+  protected final Directory input, output;
+  protected final IndexWriterConfig config;
+  protected final List<DocumentSelector> documentSelectors;
+
+  public IndexRearranger(
+      Directory input,
+      Directory output,
+      IndexWriterConfig config,
+      List<DocumentSelector> documentSelectors) {

Review comment:
       Maybe we could (followon PR maybe) have some sort of utility class implementing `DocumentSelector` by opening an existing index, crawling its docids to segments based on some specified primary key field or so?
   
   Then this command-line tool could take a pointer to source index (to extract `DocumentSelector` per segment), name of primary key field, a pointer to input index to rewrite, and a pointer to filesystem where the rearranged index should be written?

##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {
+  protected final Directory input, output;
+  protected final IndexWriterConfig config;
+  protected final List<DocumentSelector> documentSelectors;
+
+  public IndexRearranger(
+      Directory input,
+      Directory output,
+      IndexWriterConfig config,
+      List<DocumentSelector> documentSelectors) {
+    this.input = input;
+    this.output = output;
+    this.config = config;
+    this.documentSelectors = documentSelectors;
+  }
+
+  public void execute() throws Exception {
+    config.setMergePolicy(
+        NoMergePolicy.INSTANCE); // do not merge since one addIndexes call create one segment
+    try (IndexWriter writer = new IndexWriter(output, config);
+        IndexReader reader = DirectoryReader.open(input)) {
+      ExecutorService executor =
+          Executors.newFixedThreadPool(
+              Math.min(Runtime.getRuntime().availableProcessors(), documentSelectors.size()),
+              new NamedThreadFactory("rearranger"));
+      ArrayList<Future<Void>> futures = new ArrayList<>();
+      for (DocumentSelector record : documentSelectors) {
+        Callable<Void> addSegment =
+            () -> {
+              addOneSegment(writer, reader, record);
+              return null;
+            };
+        futures.add(executor.submit(addSegment));
+      }
+      for (Future<Void> future : futures) {
+        future.get();
+      }
+      executor.shutdown();
+    }
+  }
+
+  private static void addOneSegment(IndexWriter writer, IndexReader reader, DocumentSelector record)

Review comment:
       Hmm maybe rename `record` to `selector`?

##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {

Review comment:
       Maybe add a `// TODO` here and roughly describe the alternative solution @s1monw had proposed on the dev list thread?  That solution sounded compelling but a bit tricky to implement by wrangling `IndexWriter` to flush/merge in exactly the right way, but, it would probably be faster than this two-phased approach.

##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of

Review comment:
       Add period after `output dir` before `Length`?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r564724657



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {
+  protected final Directory input, output;
+  protected final IndexWriterConfig config;
+  protected final List<DocumentSelector> documentSelectors;
+
+  public IndexRearranger(
+      Directory input,
+      Directory output,
+      IndexWriterConfig config,
+      List<DocumentSelector> documentSelectors) {
+    this.input = input;
+    this.output = output;
+    this.config = config;
+    this.documentSelectors = documentSelectors;
+  }
+
+  public void execute() throws Exception {
+    config.setMergePolicy(
+        NoMergePolicy.INSTANCE); // do not merge since one addIndexes call create one segment
+    try (IndexWriter writer = new IndexWriter(output, config);
+        IndexReader reader = DirectoryReader.open(input)) {
+      ExecutorService executor =
+          Executors.newFixedThreadPool(
+              Math.min(Runtime.getRuntime().availableProcessors(), documentSelectors.size()),
+              new NamedThreadFactory("rearranger"));
+      ArrayList<Future<Void>> futures = new ArrayList<>();
+      for (DocumentSelector record : documentSelectors) {
+        Callable<Void> addSegment =
+            () -> {
+              addOneSegment(writer, reader, record);
+              return null;
+            };
+        futures.add(executor.submit(addSegment));
+      }
+      for (Future<Void> future : futures) {
+        future.get();
+      }
+      executor.shutdown();
+    }
+  }
+
+  private static void addOneSegment(IndexWriter writer, IndexReader reader, DocumentSelector record)

Review comment:
       Ah good catch. Previously I named `DocumentSelector` as `SegmentRecord` and forgot to change this one after renaming.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r566362852



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/BinaryDocValueSelector.java
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+
+/** Select documents using binary doc values */
+public class BinaryDocValueSelector implements IndexRearranger.DocumentSelector, Serializable {
+
+  private final String field;
+  private final HashSet<String> keySet;
+
+  public BinaryDocValueSelector(String field, HashSet<String> keySet) {
+    this.field = field;
+    this.keySet = keySet;
+  }
+
+  @Override
+  public BitSet getFilteredLiveDocs(CodecReader reader) throws IOException {
+    BinaryDocValues binaryDocValues = reader.getBinaryDocValues(field);
+    Bits oldLiveDocs = reader.getLiveDocs();
+    FixedBitSet bits = new FixedBitSet(reader.maxDoc());
+    for (int i = 0; i < reader.maxDoc(); i++) {
+      if (oldLiveDocs != null && oldLiveDocs.get(i) == false) {
+        continue;
+      }
+      if (binaryDocValues.advanceExact(i)
+          && keySet.contains(binaryDocValues.binaryValue().utf8ToString())) {
+        bits.set(i);
+      }
+    }
+    return bits;
+  }
+
+  public static List<IndexRearranger.DocumentSelector> createFromExistIndex(
+      String field, Directory directory) throws IOException {
+    List<IndexRearranger.DocumentSelector> selectors = new ArrayList<>();
+    try (IndexReader reader = DirectoryReader.open(directory)) {
+      for (LeafReaderContext context : reader.leaves()) {
+        HashSet<String> keySet = new HashSet<>();
+        Bits liveDocs = context.reader().getLiveDocs();
+        BinaryDocValues binaryDocValues = context.reader().getBinaryDocValues(field);
+        for (int i = 0; i < context.reader().maxDoc(); i++) {
+          if (liveDocs != null && liveDocs.get(i) == false) {
+            continue;
+          }
+          if (binaryDocValues.advanceExact(i)) {
+            keySet.add(binaryDocValues.binaryValue().utf8ToString());
+          }

Review comment:
       Good idea!




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r564717618



##########
File path: lucene/CHANGES.txt
##########
@@ -11,6 +11,8 @@ New Features
 
 * LUCENE-9004: Approximate nearest vector search via NSW graphs
 
+* LUCENE-9694: New tool for creating a deterministic index (Haoyu Zhai)

Review comment:
       Good point. I'll create an 8.9 entry instead




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] mikemccand commented on pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
mikemccand commented on pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#issuecomment-769974955


   Thank you @zhaih!  This will be a very useful tool for realistic benchmarking!


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] mikemccand merged pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
mikemccand merged pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246


   


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r566324286



##########
File path: lucene/CHANGES.txt
##########
@@ -247,6 +247,13 @@ Other
 * LUCENE-9627: Remove unused Lucene50FieldInfosFormat codec and small refactor some codecs
   to separate reading header/footer from reading content of the file. (Ignacio Vera)
 
+======================= Lucene 8.9.0 =======================
+
+New Features
+---------------------
+
+* LUCENE-9694: New tool for creating a deterministic index (Haoyu Zhai)

Review comment:
       Sure, I'll add that, thanks!




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r564718926



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {
+  protected final Directory input, output;
+  protected final IndexWriterConfig config;
+  protected final List<DocumentSelector> documentSelectors;
+
+  public IndexRearranger(
+      Directory input,
+      Directory output,
+      IndexWriterConfig config,
+      List<DocumentSelector> documentSelectors) {

Review comment:
       Good idea. I didn't include that part for keeping generality, but I think it is also worth implementing an example `DocumentSelector` as you suggested, I'll add one in this PR.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] mikemccand commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
mikemccand commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r566271500



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/BinaryDocValueSelector.java
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+
+/** Select documents using binary doc values */
+public class BinaryDocValueSelector implements IndexRearranger.DocumentSelector, Serializable {
+
+  private final String field;
+  private final HashSet<String> keySet;
+
+  public BinaryDocValueSelector(String field, HashSet<String> keySet) {

Review comment:
       We could later (in follow-on issue) switch to Lucene's `CharArraySet` if heap usage of this possibly large set ever becomes a problem ... but it is not urgent since this tool need not be optimized.

##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/BinaryDocValueSelector.java
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+
+/** Select documents using binary doc values */
+public class BinaryDocValueSelector implements IndexRearranger.DocumentSelector, Serializable {
+
+  private final String field;
+  private final HashSet<String> keySet;
+
+  public BinaryDocValueSelector(String field, HashSet<String> keySet) {
+    this.field = field;
+    this.keySet = keySet;
+  }
+
+  @Override
+  public BitSet getFilteredLiveDocs(CodecReader reader) throws IOException {
+    BinaryDocValues binaryDocValues = reader.getBinaryDocValues(field);
+    Bits oldLiveDocs = reader.getLiveDocs();
+    FixedBitSet bits = new FixedBitSet(reader.maxDoc());
+    for (int i = 0; i < reader.maxDoc(); i++) {
+      if (oldLiveDocs != null && oldLiveDocs.get(i) == false) {
+        continue;
+      }
+      if (binaryDocValues.advanceExact(i)
+          && keySet.contains(binaryDocValues.binaryValue().utf8ToString())) {
+        bits.set(i);
+      }
+    }
+    return bits;
+  }
+
+  public static List<IndexRearranger.DocumentSelector> createFromExistIndex(

Review comment:
       Maybe `createFromExistingIndex`?

##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/BinaryDocValueSelector.java
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+
+/** Select documents using binary doc values */
+public class BinaryDocValueSelector implements IndexRearranger.DocumentSelector, Serializable {
+
+  private final String field;
+  private final HashSet<String> keySet;
+
+  public BinaryDocValueSelector(String field, HashSet<String> keySet) {
+    this.field = field;
+    this.keySet = keySet;
+  }
+
+  @Override
+  public BitSet getFilteredLiveDocs(CodecReader reader) throws IOException {
+    BinaryDocValues binaryDocValues = reader.getBinaryDocValues(field);
+    Bits oldLiveDocs = reader.getLiveDocs();
+    FixedBitSet bits = new FixedBitSet(reader.maxDoc());
+    for (int i = 0; i < reader.maxDoc(); i++) {
+      if (oldLiveDocs != null && oldLiveDocs.get(i) == false) {
+        continue;
+      }
+      if (binaryDocValues.advanceExact(i)
+          && keySet.contains(binaryDocValues.binaryValue().utf8ToString())) {
+        bits.set(i);
+      }
+    }
+    return bits;
+  }
+
+  public static List<IndexRearranger.DocumentSelector> createFromExistIndex(
+      String field, Directory directory) throws IOException {
+    List<IndexRearranger.DocumentSelector> selectors = new ArrayList<>();
+    try (IndexReader reader = DirectoryReader.open(directory)) {
+      for (LeafReaderContext context : reader.leaves()) {
+        HashSet<String> keySet = new HashSet<>();
+        Bits liveDocs = context.reader().getLiveDocs();
+        BinaryDocValues binaryDocValues = context.reader().getBinaryDocValues(field);
+        for (int i = 0; i < context.reader().maxDoc(); i++) {
+          if (liveDocs != null && liveDocs.get(i) == false) {
+            continue;
+          }
+          if (binaryDocValues.advanceExact(i)) {
+            keySet.add(binaryDocValues.binaryValue().utf8ToString());
+          }

Review comment:
       Maybe add `else throw exception`?

##########
File path: lucene/CHANGES.txt
##########
@@ -247,6 +247,13 @@ Other
 * LUCENE-9627: Remove unused Lucene50FieldInfosFormat codec and small refactor some codecs
   to separate reading header/footer from reading content of the file. (Ignacio Vera)
 
+======================= Lucene 8.9.0 =======================
+
+New Features
+---------------------
+
+* LUCENE-9694: New tool for creating a deterministic index (Haoyu Zhai)

Review comment:
       Thanks for adding `CHANGES` entry!
   
   Could we beef it up a bit, e.g.:
   
   ```
   LUCENE-9694: New tool for creating a deterministic index to enabling benchmarking changes on a consistent multi-segment index even when they require re-indexing.
   ```
   
   Or so?

##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/IndexRearranger.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.lucene.misc.index;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FilterCodecReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NoMergePolicy;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NamedThreadFactory;
+
+/**
+ * Copy and rearrange index according to document selectors, from input dir to output dir Length of
+ * documentSelectors determines how many segments there will be
+ */
+public class IndexRearranger {
+  protected final Directory input, output;
+  protected final IndexWriterConfig config;
+  protected final List<DocumentSelector> documentSelectors;
+
+  public IndexRearranger(
+      Directory input,
+      Directory output,
+      IndexWriterConfig config,
+      List<DocumentSelector> documentSelectors) {

Review comment:
       Awesome, thanks for adding this!




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


[GitHub] [lucene-solr] zhaih commented on a change in pull request #2246: LUCENE-9694: New tool for creating a deterministic index

Posted by GitBox <gi...@apache.org>.
zhaih commented on a change in pull request #2246:
URL: https://github.com/apache/lucene-solr/pull/2246#discussion_r566324474



##########
File path: lucene/misc/src/java/org/apache/lucene/misc/index/BinaryDocValueSelector.java
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.lucene.misc.index;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import org.apache.lucene.index.BinaryDocValues;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+
+/** Select documents using binary doc values */
+public class BinaryDocValueSelector implements IndexRearranger.DocumentSelector, Serializable {
+
+  private final String field;
+  private final HashSet<String> keySet;
+
+  public BinaryDocValueSelector(String field, HashSet<String> keySet) {

Review comment:
       ++




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org