-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify MultiSnapshot#SeqNoset #27547
Changes from 7 commits
0b96f39
0f6f3b8
d5c9142
c9a4901
14aca62
0e6a31e
0df0474
55e041d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
public CountedBitSet(short numBits) { | ||
assert numBits > 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a hard illegal argument exception? |
||
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"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This empty line can go. |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can (and should) be final. |
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think "Not implemented yet" adds anything other the exception type (and could be misleading if we never intend to implement). |
||
} | ||
|
||
@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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need an implementation for this method however I think this could be |
||
} | ||
|
||
// Exposed for testing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally think these comments do not add anything over the fact that we can use the IDE to see where a method is used. If it used in tests and only tests then we already know it is exposed for tests. |
||
boolean isInternalBitsetReleased() { | ||
return bitset == null; | ||
} | ||
} |
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)); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a hard illegal argument exception.