Skip to content

Commit 0546377

Browse files
committed
Introduce translog no-op (iteration)
1 parent 36b9edb commit 0546377

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ public void writeVLong(long i) throws IOException {
223223
writeByte((byte) i);
224224
}
225225

226-
public static int lengthVLong(long i) {
227-
assert i >= 0;
228-
return 1 + (int) Math.floor(Math.log(i) / Math.log(2)) / 7;
229-
}
230-
231226
/**
232227
* Writes a long in a variable-length format. Writes between one and ten bytes.
233228
* Values are remapped by sliding the sign bit into the lsb and then encoded as an unsigned number
@@ -333,7 +328,7 @@ public void writeString(String str) throws IOException {
333328
// make sure any possible char can fit into the buffer in any possible iteration
334329
// we need at most 3 bytes so we flush the buffer once we have less than 3 bytes
335330
// left before we start another iteration
336-
if (offset > buffer.length-3) {
331+
if (offset > buffer.length - 3) {
337332
writeBytes(buffer, offset);
338333
offset = 0;
339334
}

core/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ TYPE operationType() {
11721172

11731173
@Override
11741174
public int estimatedSizeInBytes() {
1175-
return 2 * reason.length() + StreamOutput.lengthVLong(seqNo()) + StreamOutput.lengthVLong(primaryTerm());
1175+
return 2 * reason.length() + 2 * Long.BYTES;
11761176
}
11771177

11781178
}

core/src/main/java/org/elasticsearch/index/translog/Translog.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,8 @@ public Index(StreamInput in) throws IOException {
862862
this.autoGeneratedIdTimestamp = IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP;
863863
}
864864
if (format >= FORMAT_SEQ_NO) {
865-
seqNo = in.readVLong();
866-
primaryTerm = in.readVLong();
865+
seqNo = in.readLong();
866+
primaryTerm = in.readLong();
867867
}
868868
}
869869

@@ -955,8 +955,8 @@ public void writeTo(StreamOutput out) throws IOException {
955955

956956
out.writeByte(versionType.getValue());
957957
out.writeLong(autoGeneratedIdTimestamp);
958-
out.writeVLong(seqNo);
959-
out.writeVLong(primaryTerm);
958+
out.writeLong(seqNo);
959+
out.writeLong(primaryTerm);
960960
}
961961

962962
@Override
@@ -1160,8 +1160,8 @@ public String reason() {
11601160
}
11611161

11621162
NoOp(final StreamInput in) throws IOException {
1163-
seqNo = in.readVLong();
1164-
primaryTerm = in.readVLong();
1163+
seqNo = in.readLong();
1164+
primaryTerm = in.readLong();
11651165
reason = in.readString();
11661166
}
11671167

@@ -1176,8 +1176,8 @@ public NoOp(final long seqNo, final long primaryTerm, final String reason) {
11761176

11771177
@Override
11781178
public void writeTo(StreamOutput out) throws IOException {
1179-
out.writeVLong(seqNo);
1180-
out.writeVLong(primaryTerm);
1179+
out.writeLong(seqNo);
1180+
out.writeLong(primaryTerm);
11811181
out.writeString(reason);
11821182
}
11831183

@@ -1188,7 +1188,7 @@ public Type opType() {
11881188

11891189
@Override
11901190
public long estimateSize() {
1191-
return 2 * reason.length() + StreamOutput.lengthVLong(seqNo) + StreamOutput.lengthVLong(primaryTerm);
1191+
return 2 * reason.length() + 2 * Long.BYTES;
11921192
}
11931193

11941194
@Override

core/src/test/java/org/elasticsearch/index/translog/TranslogTests.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,10 @@ public void testStats() throws IOException {
318318
{
319319
final TranslogStats stats = stats();
320320
assertThat(stats.estimatedNumberOfOperations(), equalTo(4L));
321-
assertThat(
322-
stats.getTranslogSizeInBytes(),
323-
equalTo(165L + StreamOutput.lengthVLong(seqNo) + StreamOutput.lengthVLong(primaryTerm)));
321+
assertThat(stats.getTranslogSizeInBytes(), equalTo(181L));
324322
}
325323

326-
final long expectedSizeInBytes = 208L + StreamOutput.lengthVLong(seqNo) + StreamOutput.lengthVLong(primaryTerm);
324+
final long expectedSizeInBytes = 224L;
327325
translog.prepareCommit();
328326
{
329327
final TranslogStats stats = stats();
@@ -359,6 +357,24 @@ public void testStats() throws IOException {
359357
}
360358
}
361359

360+
public void testTotalTests() {
361+
final TranslogStats total = new TranslogStats();
362+
final int n = randomIntBetween(0, 16);
363+
final List<TranslogStats> statsList = new ArrayList<>(n);
364+
for (int i = 0; i < n; i++) {
365+
final TranslogStats stats = new TranslogStats(randomIntBetween(1, 4096), randomIntBetween(1, 1 << 20));
366+
statsList.add(stats);
367+
total.add(stats);
368+
}
369+
370+
assertThat(
371+
total.estimatedNumberOfOperations(),
372+
equalTo(statsList.stream().mapToLong(TranslogStats::estimatedNumberOfOperations).sum()));
373+
assertThat(
374+
total.getTranslogSizeInBytes(),
375+
equalTo(statsList.stream().mapToLong(TranslogStats::getTranslogSizeInBytes).sum()));
376+
}
377+
362378
public void testNegativeNumberOfOperations() {
363379
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new TranslogStats(-1, 1));
364380
assertThat(e, hasToString(containsString("numberOfOperations must be >= 0")));

0 commit comments

Comments
 (0)