test and fix TranslogStats - you wouldn't believe how hard it is to sum up two values

This commit is contained in:
Simon Willnauer 2015-10-08 09:24:22 +02:00
parent 890f607b95
commit 255338111b
3 changed files with 68 additions and 22 deletions

View File

@ -18,11 +18,10 @@
*/
package org.elasticsearch.index.translog;
import org.elasticsearch.action.support.ToXContentToBytes;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
@ -31,17 +30,23 @@ import java.io.IOException;
/**
*
*/
public class TranslogStats implements ToXContent, Streamable {
public class TranslogStats extends ToXContentToBytes implements Streamable {
private long translogSizeInBytes;
private int estimatedNumberOfOperations;
private int numberOfOperations;
public TranslogStats() {
}
public TranslogStats(int estimatedNumberOfOperations, long translogSizeInBytes) {
public TranslogStats(int numberOfOperations, long translogSizeInBytes) {
if (numberOfOperations < 0) {
throw new IllegalArgumentException("numberOfOperations must be >= 0");
}
if (translogSizeInBytes < 0) {
throw new IllegalArgumentException("translogSizeInBytes must be >= 0");
}
assert translogSizeInBytes >= 0 : "translogSizeInBytes must be >= 0, got [" + translogSizeInBytes + "]";
this.estimatedNumberOfOperations = estimatedNumberOfOperations;
this.numberOfOperations = numberOfOperations;
this.translogSizeInBytes = translogSizeInBytes;
}
@ -50,22 +55,22 @@ public class TranslogStats implements ToXContent, Streamable {
return;
}
this.estimatedNumberOfOperations += translogStats.estimatedNumberOfOperations;
this.translogSizeInBytes = +translogStats.translogSizeInBytes;
this.numberOfOperations += translogStats.numberOfOperations;
this.translogSizeInBytes += translogStats.translogSizeInBytes;
}
public ByteSizeValue translogSizeInBytes() {
return new ByteSizeValue(translogSizeInBytes);
public long getTranslogSizeInBytes() {
return translogSizeInBytes;
}
public long estimatedNumberOfOperations() {
return estimatedNumberOfOperations;
return numberOfOperations;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.TRANSLOG);
builder.field(Fields.OPERATIONS, estimatedNumberOfOperations);
builder.field(Fields.OPERATIONS, numberOfOperations);
builder.byteSizeField(Fields.SIZE_IN_BYTES, Fields.SIZE, translogSizeInBytes);
builder.endObject();
return builder;
@ -80,13 +85,13 @@ public class TranslogStats implements ToXContent, Streamable {
@Override
public void readFrom(StreamInput in) throws IOException {
estimatedNumberOfOperations = in.readVInt();
numberOfOperations = in.readVInt();
translogSizeInBytes = in.readVLong();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(estimatedNumberOfOperations);
out.writeVInt(numberOfOperations);
out.writeVLong(translogSizeInBytes);
}
}

View File

@ -965,4 +965,13 @@ public class ShadowEngineTests extends ESTestCase {
// (shadow engine is already shut down in the try-with-resources)
IOUtils.close(srStore, pEngine, pStore);
}
public void testNoTranslog() {
try {
replicaEngine.getTranslog();
fail("shadow engine has no translog");
} catch (UnsupportedOperationException ex) {
// all good
}
}
}

View File

@ -34,6 +34,7 @@ import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
@ -276,32 +277,63 @@ public class TranslogTests extends ESTestCase {
final long firstOperationPosition = translog.getFirstOperationPosition();
TranslogStats stats = stats();
assertThat(stats.estimatedNumberOfOperations(), equalTo(0l));
long lastSize = stats.translogSizeInBytes().bytes();
long lastSize = stats.getTranslogSizeInBytes();
assertThat((int) firstOperationPosition, greaterThan(CodecUtil.headerLength(TranslogWriter.TRANSLOG_CODEC)));
assertThat(lastSize, equalTo(firstOperationPosition));
TranslogStats total = new TranslogStats();
translog.add(new Translog.Index("test", "1", new byte[]{1}));
stats = stats();
total.add(stats);
assertThat(stats.estimatedNumberOfOperations(), equalTo(1l));
assertThat(stats.translogSizeInBytes().bytes(), greaterThan(lastSize));
lastSize = stats.translogSizeInBytes().bytes();
assertThat(stats.getTranslogSizeInBytes(), greaterThan(lastSize));
lastSize = stats.getTranslogSizeInBytes();
translog.add(new Translog.Delete(newUid("2")));
stats = stats();
total.add(stats);
assertThat(stats.estimatedNumberOfOperations(), equalTo(2l));
assertThat(stats.translogSizeInBytes().bytes(), greaterThan(lastSize));
lastSize = stats.translogSizeInBytes().bytes();
assertThat(stats.getTranslogSizeInBytes(), greaterThan(lastSize));
lastSize = stats.getTranslogSizeInBytes();
translog.add(new Translog.Delete(newUid("3")));
translog.prepareCommit();
stats = stats();
total.add(stats);
assertThat(stats.estimatedNumberOfOperations(), equalTo(3l));
assertThat(stats.translogSizeInBytes().bytes(), greaterThan(lastSize));
assertThat(stats.getTranslogSizeInBytes(), greaterThan(lastSize));
translog.commit();
stats = stats();
total.add(stats);
assertThat(stats.estimatedNumberOfOperations(), equalTo(0l));
assertThat(stats.translogSizeInBytes().bytes(), equalTo(firstOperationPosition));
assertThat(stats.getTranslogSizeInBytes(), equalTo(firstOperationPosition));
assertEquals(6, total.estimatedNumberOfOperations());
assertEquals(431, total.getTranslogSizeInBytes());
BytesStreamOutput out = new BytesStreamOutput();
total.writeTo(out);
TranslogStats copy = new TranslogStats();
copy.readFrom(StreamInput.wrap(out.bytes()));
assertEquals(6, copy.estimatedNumberOfOperations());
assertEquals(431, copy.getTranslogSizeInBytes());
assertEquals("\"translog\"{\n" +
" \"operations\" : 6,\n" +
" \"size_in_bytes\" : 431\n" +
"}", copy.toString().trim());
try {
new TranslogStats(1, -1);
fail("must be positive");
} catch (IllegalArgumentException ex) {
//all well
}
try {
new TranslogStats(-1, 1);
fail("must be positive");
} catch (IllegalArgumentException ex) {
//all well
}
}
@Test