Skip to content

Commit 9722a0f

Browse files
committed
Change
1 parent c666120 commit 9722a0f

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

server/src/main/java/org/elasticsearch/index/translog/TranslogHeaderWriter.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,14 @@ public static void writeIndexHeader(RecyclerBytesStreamOutput buffer, Translog.I
4040
int uidVIntLen = RecyclerBytesStreamOutput.vIntLength(uidLen);
4141
BytesRef page = buffer.tryGetPageForWrite(FIXED_INDEX_HEADER_SIZE + uidLen + uidVIntLen);
4242
if (page != null) {
43-
writeFastIndexHeader(buffer, index, page, uidLen, uidVIntLen);
43+
writeFastIndexHeader(buffer, index, page, uidVIntLen);
4444
} else {
4545
writeSlowIndexHeader(buffer, index);
4646
}
4747
}
4848

49-
private static void writeFastIndexHeader(
50-
RecyclerBytesStreamOutput buffer,
51-
Translog.Index index,
52-
BytesRef page,
53-
int uidLen,
54-
int uidVIntLen
55-
) throws IOException {
49+
private static void writeFastIndexHeader(RecyclerBytesStreamOutput buffer, Translog.Index index, BytesRef page, int uidVIntLen)
50+
throws IOException {
5651
BytesRef uid = index.uid();
5752
String routing = index.routing();
5853

@@ -67,7 +62,7 @@ private static void writeFastIndexHeader(
6762
ByteUtils.writeLongBE(index.primaryTerm(), bytes, off + 30);
6863
StreamOutput.putVInt(bytes, uid.length, off + 38);
6964
System.arraycopy(uid.bytes, uid.offset, bytes, off + 38 + uidVIntLen, uid.length);
70-
bytes[off + 38 + uidVIntLen + uidLen] = index.routing() == null ? (byte) 0 : (byte) 1;
65+
bytes[off + 38 + uidVIntLen + uid.length] = index.routing() == null ? (byte) 0 : (byte) 1;
7166

7267
long variableLengthStart = buffer.position();
7368
// Write variable length items in header
@@ -82,7 +77,8 @@ private static void writeFastIndexHeader(
8277
buffer.writeVInt(sourceLength);
8378

8479
int variableLengthSize = (int) (buffer.position() - variableLengthStart);
85-
int sizeOfOperation = FIXED_INDEX_HEADER_SIZE - Integer.BYTES + variableLengthSize + sourceLength + Integer.BYTES;
80+
int sizeOfOperation = FIXED_INDEX_HEADER_SIZE - Integer.BYTES + uidVIntLen + uid.length + variableLengthSize + sourceLength
81+
+ Integer.BYTES;
8682
ByteUtils.writeIntBE(sizeOfOperation, bytes, off);
8783
}
8884

@@ -100,15 +96,19 @@ private static void writeSlowIndexHeader(RecyclerBytesStreamOutput buffer, Trans
10096
}
10197

10298
public static void writeDeleteHeader(RecyclerBytesStreamOutput buffer, Translog.Delete delete) throws IOException {
103-
BytesRef page = buffer.tryGetPageForWrite(FIXED_DELETE_HEADER_SIZE);
99+
int uidLen = delete.uid().length;
100+
int uidVIntLen = RecyclerBytesStreamOutput.vIntLength(uidLen);
101+
BytesRef page = buffer.tryGetPageForWrite(FIXED_DELETE_HEADER_SIZE + uidLen + uidVIntLen);
104102
if (page != null) {
105-
writeFastDeleteHeader(buffer, delete, page);
103+
writeFastDeleteHeader(delete, page, uidVIntLen);
106104
} else {
107105
writeSlowDeleteHeader(buffer, delete);
108106
}
109107
}
110108

111-
private static void writeFastDeleteHeader(RecyclerBytesStreamOutput buffer, Translog.Delete delete, BytesRef page) throws IOException {
109+
private static void writeFastDeleteHeader(Translog.Delete delete, BytesRef page, int uidVIntLen) throws IOException {
110+
BytesRef uid = delete.uid();
111+
112112
int off = page.offset;
113113
byte[] bytes = page.bytes;
114114
bytes[off + 4] = Translog.Operation.Type.DELETE.id();
@@ -117,13 +117,10 @@ private static void writeFastDeleteHeader(RecyclerBytesStreamOutput buffer, Tran
117117
ByteUtils.writeLongBE(delete.version(), bytes, off + 6);
118118
ByteUtils.writeLongBE(delete.seqNo(), bytes, off + 14);
119119
ByteUtils.writeLongBE(delete.primaryTerm(), bytes, off + 22);
120+
StreamOutput.putVInt(bytes, uid.length, off + FIXED_DELETE_HEADER_SIZE);
121+
System.arraycopy(uid.bytes, uid.offset, bytes, off + FIXED_DELETE_HEADER_SIZE + uidVIntLen, uid.length);
120122

121-
long variableLengthStart = buffer.position();
122-
// Write variable length items in header
123-
buffer.writeBytesRef(delete.uid());
124-
int variableLengthSize = (int) (buffer.position() - variableLengthStart);
125-
// The total operation size is the header size + 4 bytes for checksum
126-
int sizeOfOperation = FIXED_DELETE_HEADER_SIZE - Integer.BYTES + variableLengthSize + Integer.BYTES;
123+
int sizeOfOperation = FIXED_DELETE_HEADER_SIZE - Integer.BYTES + uidVIntLen + uid.length + Integer.BYTES;
127124
ByteUtils.writeIntBE(sizeOfOperation, bytes, off);
128125
}
129126

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ public void testStats() throws Exception {
485485
waitForPositiveAge();
486486
final TranslogStats stats = stats();
487487
assertThat(stats.estimatedNumberOfOperations(), equalTo(1));
488-
assertThat(stats.getTranslogSizeInBytes(), equalTo(156L + sourceLength));
488+
assertThat(stats.getTranslogSizeInBytes(), equalTo(157L + sourceLength));
489489
assertThat(stats.getUncommittedOperations(), equalTo(1));
490-
assertThat(stats.getUncommittedSizeInBytes(), equalTo(101L + sourceLength));
490+
assertThat(stats.getUncommittedSizeInBytes(), equalTo(102L + sourceLength));
491491
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
492492
}
493493

@@ -496,9 +496,9 @@ public void testStats() throws Exception {
496496
waitForPositiveAge();
497497
final TranslogStats stats = stats();
498498
assertThat(stats.estimatedNumberOfOperations(), equalTo(2));
499-
assertThat(stats.getTranslogSizeInBytes(), equalTo(192L + sourceLength));
499+
assertThat(stats.getTranslogSizeInBytes(), equalTo(194L + sourceLength));
500500
assertThat(stats.getUncommittedOperations(), equalTo(2));
501-
assertThat(stats.getUncommittedSizeInBytes(), equalTo(137L + sourceLength));
501+
assertThat(stats.getUncommittedSizeInBytes(), equalTo(139L + sourceLength));
502502
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
503503
}
504504

@@ -507,9 +507,9 @@ public void testStats() throws Exception {
507507
waitForPositiveAge();
508508
final TranslogStats stats = stats();
509509
assertThat(stats.estimatedNumberOfOperations(), equalTo(3));
510-
assertThat(stats.getTranslogSizeInBytes(), equalTo(228L + sourceLength));
510+
assertThat(stats.getTranslogSizeInBytes(), equalTo(231L + sourceLength));
511511
assertThat(stats.getUncommittedOperations(), equalTo(3));
512-
assertThat(stats.getUncommittedSizeInBytes(), equalTo(173L + sourceLength));
512+
assertThat(stats.getUncommittedSizeInBytes(), equalTo(176L + sourceLength));
513513
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
514514
}
515515

@@ -518,9 +518,9 @@ public void testStats() throws Exception {
518518
waitForPositiveAge();
519519
final TranslogStats stats = stats();
520520
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
521-
assertThat(stats.getTranslogSizeInBytes(), equalTo(270L + sourceLength));
521+
assertThat(stats.getTranslogSizeInBytes(), equalTo(273L + sourceLength));
522522
assertThat(stats.getUncommittedOperations(), equalTo(4));
523-
assertThat(stats.getUncommittedSizeInBytes(), equalTo(215L + sourceLength));
523+
assertThat(stats.getUncommittedSizeInBytes(), equalTo(218L + sourceLength));
524524
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
525525
}
526526

@@ -529,9 +529,9 @@ public void testStats() throws Exception {
529529
waitForPositiveAge();
530530
final TranslogStats stats = stats();
531531
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
532-
assertThat(stats.getTranslogSizeInBytes(), equalTo(325L + sourceLength));
532+
assertThat(stats.getTranslogSizeInBytes(), equalTo(328L + sourceLength));
533533
assertThat(stats.getUncommittedOperations(), equalTo(4));
534-
assertThat(stats.getUncommittedSizeInBytes(), equalTo(270L + sourceLength));
534+
assertThat(stats.getUncommittedSizeInBytes(), equalTo(273L + sourceLength));
535535
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
536536
}
537537

@@ -541,7 +541,7 @@ public void testStats() throws Exception {
541541
stats.writeTo(out);
542542
final TranslogStats copy = new TranslogStats(out.bytes().streamInput());
543543
assertThat(copy.estimatedNumberOfOperations(), equalTo(4));
544-
assertThat(copy.getTranslogSizeInBytes(), equalTo(325L + sourceLength));
544+
assertThat(copy.getTranslogSizeInBytes(), equalTo(328L + sourceLength));
545545

546546
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
547547
builder.startObject();
@@ -556,7 +556,7 @@ public void testStats() throws Exception {
556556
"uncommitted_size_in_bytes": %s,
557557
"earliest_last_modified_age": %s
558558
}
559-
}""", 325L + sourceLength, 270L + sourceLength, stats.getEarliestLastModifiedAge()))));
559+
}""", 328L + sourceLength, 273L + sourceLength, stats.getEarliestLastModifiedAge()))));
560560
}
561561
}
562562
translog.getDeletionPolicy().setLocalCheckpointOfSafeCommit(randomLongBetween(3, Long.MAX_VALUE));

0 commit comments

Comments
 (0)