-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify MultiSnapshot#SeqNoset (#27547)
Today, we maintain two sets in a SeqNoSet: ongoing sets and completed sets. We can remove the completed sets and use only the ongoing sets by releasing the internal bitset of a CountedBitSet when all its bits are set. This behaves like two sets but simpler. This commit also makes CountedBitSet as a drop-in replacement for BitSet. Relates #27268
- Loading branch information
Showing
4 changed files
with
210 additions
and
75 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
core/src/main/java/org/elasticsearch/index/translog/CountedBitSet.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,106 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.index.translog; | ||
|
||
import org.apache.lucene.util.BitSet; | ||
import org.apache.lucene.util.FixedBitSet; | ||
|
||
/** | ||
* A {@link CountedBitSet} wraps a {@link FixedBitSet} but automatically releases the internal bitset | ||
* when all bits are set to reduce memory usage. This structure can work well for sequence numbers | ||
* from translog as these numbers are likely to form contiguous ranges (eg. filling all bits). | ||
*/ | ||
final class CountedBitSet extends BitSet { | ||
private short onBits; // Number of bits are set. | ||
private FixedBitSet bitset; | ||
|
||
CountedBitSet(short numBits) { | ||
assert numBits > 0; | ||
this.onBits = 0; | ||
this.bitset = new FixedBitSet(numBits); | ||
} | ||
|
||
@Override | ||
public boolean get(int index) { | ||
assert 0 <= index && index < this.length(); | ||
assert bitset == null || onBits < bitset.length() : "Bitset should be released when all bits are set"; | ||
|
||
return bitset == null ? true : bitset.get(index); | ||
} | ||
|
||
@Override | ||
public void set(int index) { | ||
assert 0 <= index && index < this.length(); | ||
assert bitset == null || onBits < bitset.length() : "Bitset should be released when all bits are set"; | ||
|
||
// Ignore set when bitset is full. | ||
if (bitset != null) { | ||
boolean wasOn = bitset.getAndSet(index); | ||
if (wasOn == false) { | ||
onBits++; | ||
// Once all bits are set, we can simply just return YES for all indexes. | ||
// This allows us to clear the internal bitset and use null check as the guard. | ||
if (onBits == bitset.length()) { | ||
bitset = null; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void clear(int startIndex, int endIndex) { | ||
throw new UnsupportedOperationException("Not implemented yet"); | ||
} | ||
|
||
@Override | ||
public void clear(int index) { | ||
throw new UnsupportedOperationException("Not implemented yet"); | ||
} | ||
|
||
@Override | ||
public int cardinality() { | ||
return onBits; | ||
} | ||
|
||
@Override | ||
public int length() { | ||
return bitset == null ? onBits : bitset.length(); | ||
} | ||
|
||
@Override | ||
public int prevSetBit(int index) { | ||
throw new UnsupportedOperationException("Not implemented yet"); | ||
} | ||
|
||
@Override | ||
public int nextSetBit(int index) { | ||
throw new UnsupportedOperationException("Not implemented yet"); | ||
} | ||
|
||
@Override | ||
public long ramBytesUsed() { | ||
throw new UnsupportedOperationException("Not implemented yet"); | ||
} | ||
|
||
// Exposed for testing | ||
boolean isInternalBitsetReleased() { | ||
return bitset == null; | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
core/src/test/java/org/elasticsearch/index/translog/CountedBitSetTests.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,97 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.index.translog; | ||
|
||
import org.apache.lucene.util.FixedBitSet; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class CountedBitSetTests extends ESTestCase { | ||
|
||
public void testCompareToFixedBitset() { | ||
int numBits = (short) randomIntBetween(8, 4096); | ||
final FixedBitSet fixedBitSet = new FixedBitSet(numBits); | ||
final CountedBitSet countedBitSet = new CountedBitSet((short) numBits); | ||
|
||
for (int i = 0; i < numBits; i++) { | ||
if (randomBoolean()) { | ||
fixedBitSet.set(i); | ||
countedBitSet.set(i); | ||
} | ||
assertThat(countedBitSet.cardinality(), equalTo(fixedBitSet.cardinality())); | ||
assertThat(countedBitSet.length(), equalTo(fixedBitSet.length())); | ||
} | ||
|
||
for (int i = 0; i < numBits; i++) { | ||
assertThat(countedBitSet.get(i), equalTo(fixedBitSet.get(i))); | ||
} | ||
} | ||
|
||
public void testReleaseInternalBitSet() { | ||
int numBits = (short) randomIntBetween(8, 4096); | ||
final CountedBitSet countedBitSet = new CountedBitSet((short) numBits); | ||
final List<Integer> values = IntStream.range(0, numBits).boxed().collect(Collectors.toList()); | ||
|
||
for (int i = 1; i < numBits; i++) { | ||
final int value = values.get(i); | ||
assertThat(countedBitSet.get(value), equalTo(false)); | ||
assertThat(countedBitSet.isInternalBitsetReleased(), equalTo(false)); | ||
|
||
countedBitSet.set(value); | ||
|
||
assertThat(countedBitSet.get(value), equalTo(true)); | ||
assertThat(countedBitSet.isInternalBitsetReleased(), equalTo(false)); | ||
assertThat(countedBitSet.length(), equalTo(numBits)); | ||
assertThat(countedBitSet.cardinality(), equalTo(i)); | ||
} | ||
|
||
// The missing piece to fill all bits. | ||
{ | ||
final int value = values.get(0); | ||
assertThat(countedBitSet.get(value), equalTo(false)); | ||
assertThat(countedBitSet.isInternalBitsetReleased(), equalTo(false)); | ||
|
||
countedBitSet.set(value); | ||
|
||
assertThat(countedBitSet.get(value), equalTo(true)); | ||
assertThat(countedBitSet.isInternalBitsetReleased(), equalTo(true)); | ||
assertThat(countedBitSet.length(), equalTo(numBits)); | ||
assertThat(countedBitSet.cardinality(), equalTo(numBits)); | ||
} | ||
|
||
// Tests with released internal bitset. | ||
final int iterations = iterations(1000, 10000); | ||
for (int i = 0; i < iterations; i++) { | ||
final int value = randomInt(numBits - 1); | ||
assertThat(countedBitSet.get(value), equalTo(true)); | ||
assertThat(countedBitSet.isInternalBitsetReleased(), equalTo(true)); | ||
assertThat(countedBitSet.length(), equalTo(numBits)); | ||
assertThat(countedBitSet.cardinality(), equalTo(numBits)); | ||
if (frequently()) { | ||
assertThat(countedBitSet.get(value), equalTo(true)); | ||
} | ||
} | ||
} | ||
} |
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