-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Fuzzy codec for doc id fields using a bloom filter
Signed-off-by: mgodwan <[email protected]>
- Loading branch information
Showing
21 changed files
with
1,419 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...src/main/java/org/opensearch/benchmark/index/codec/fuzzy/FilterConstructionBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.benchmark.index.codec.fuzzy; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.common.UUIDs; | ||
import org.opensearch.index.codec.fuzzy.FuzzySet; | ||
import org.opensearch.index.codec.fuzzy.FuzzySetFactory; | ||
import org.opensearch.index.codec.fuzzy.FuzzySetParameters; | ||
import org.opensearch.index.mapper.IdFieldMapper; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
@Fork(3) | ||
@Warmup(iterations = 2) | ||
@Measurement(iterations = 5, time = 60, timeUnit = TimeUnit.SECONDS) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
public class FilterConstructionBenchmark { | ||
|
||
private List<BytesRef> items; | ||
|
||
@Param({ "1000000", "10000000", "50000000" }) | ||
private int numIds; | ||
|
||
@Param({ "0.0511", "0.1023", "0.2047" }) | ||
private double fpp; | ||
|
||
private FuzzySetFactory fuzzySetFactory; | ||
private String fieldName; | ||
|
||
@Setup | ||
public void setupIds() { | ||
this.fieldName = IdFieldMapper.NAME; | ||
this.items = IntStream.range(0, numIds).mapToObj(i -> new BytesRef(UUIDs.base64UUID())).collect(Collectors.toList()); | ||
FuzzySetParameters parameters = new FuzzySetParameters(() -> fpp); | ||
this.fuzzySetFactory = new FuzzySetFactory(Map.of(fieldName, parameters)); | ||
} | ||
|
||
@Benchmark | ||
public FuzzySet buildFilter() throws IOException { | ||
return fuzzySetFactory.createFuzzySet(items.size(), fieldName, () -> items.iterator()); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...marks/src/main/java/org/opensearch/benchmark/index/codec/fuzzy/FilterLookupBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.benchmark.index.codec.fuzzy; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.common.UUIDs; | ||
import org.opensearch.index.codec.fuzzy.FuzzySet; | ||
import org.opensearch.index.codec.fuzzy.FuzzySetFactory; | ||
import org.opensearch.index.codec.fuzzy.FuzzySetParameters; | ||
import org.opensearch.index.mapper.IdFieldMapper; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
@Fork(3) | ||
@Warmup(iterations = 2) | ||
@Measurement(iterations = 5, time = 60, timeUnit = TimeUnit.SECONDS) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
public class FilterLookupBenchmark { | ||
|
||
@Param({ "50000000", "1000000" }) | ||
private int numItems; | ||
|
||
@Param({ "1000000" }) | ||
private int searchKeyCount; | ||
|
||
@Param({ "0.0511", "0.1023", "0.2047" }) | ||
private double fpp; | ||
|
||
private FuzzySet fuzzySet; | ||
private List<BytesRef> items; | ||
private Random random = new Random(); | ||
|
||
@Setup | ||
public void setupFilter() throws IOException { | ||
String fieldName = IdFieldMapper.NAME; | ||
items = IntStream.range(0, numItems).mapToObj(i -> new BytesRef(UUIDs.base64UUID())).collect(Collectors.toList()); | ||
FuzzySetParameters parameters = new FuzzySetParameters(() -> fpp); | ||
fuzzySet = new FuzzySetFactory(Map.of(fieldName, parameters)).createFuzzySet(numItems, fieldName, () -> items.iterator()); | ||
} | ||
|
||
@Benchmark | ||
public void contains_withExistingKeys(Blackhole blackhole) throws IOException { | ||
for (int i = 0; i < searchKeyCount; i++) { | ||
blackhole.consume(fuzzySet.contains(items.get(random.nextInt(items.size()))) == FuzzySet.Result.MAYBE); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void contains_withRandomKeys(Blackhole blackhole) throws IOException { | ||
for (int i = 0; i < searchKeyCount; i++) { | ||
blackhole.consume(fuzzySet.contains(new BytesRef(UUIDs.base64UUID()))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
server/src/main/java/org/opensearch/index/codec/fuzzy/AbstractFuzzySet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.codec.fuzzy; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.common.CheckedSupplier; | ||
|
||
import java.io.IOException; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* Encapsulates common behaviour implementation for a fuzzy set. | ||
*/ | ||
public abstract class AbstractFuzzySet implements FuzzySet { | ||
|
||
/** | ||
* Add an item to this fuzzy set. | ||
* @param value The value to be added | ||
*/ | ||
protected abstract void add(BytesRef value); | ||
|
||
/** | ||
* Add all items to the underlying set. | ||
* Implementations can choose to perform this using an optimized strategy based on the type of set. | ||
* @param valuesIteratorProvider Supplier for an iterator over All values which should be added to the set. | ||
*/ | ||
protected void addAll(CheckedSupplier<Iterator<BytesRef>, IOException> valuesIteratorProvider) throws IOException { | ||
Iterator<BytesRef> values = valuesIteratorProvider.get(); | ||
while (values.hasNext()) { | ||
add(values.next()); | ||
} | ||
} | ||
|
||
protected long generateKey(BytesRef value) { | ||
return MurmurHash64.INSTANCE.hash(value); | ||
} | ||
|
||
protected void assertAllElementsExist(CheckedSupplier<Iterator<BytesRef>, IOException> iteratorProvider) throws IOException { | ||
Iterator<BytesRef> iter = iteratorProvider.get(); | ||
int cnt = 0; | ||
while (iter.hasNext()) { | ||
BytesRef item = iter.next(); | ||
assert contains(item) == Result.MAYBE | ||
: "Expected Filter to return positive response for elements added to it. Elements matched: " + cnt; | ||
cnt++; | ||
} | ||
} | ||
} |
Oops, something went wrong.