Skip to content

Commit 313a72f

Browse files
authored
Refactor translog reading and writing functions (elastic#95110)
And a test function that is useful Relates ES-5058
1 parent 78acb49 commit 313a72f

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -576,14 +576,7 @@ TranslogWriter createWriter(
576576
public Location add(final Operation operation) throws IOException {
577577
final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(bigArrays);
578578
try {
579-
final long start = out.position();
580-
out.skip(Integer.BYTES);
581-
writeOperationNoSize(new BufferedChecksumStreamOutput(out), operation);
582-
final long end = out.position();
583-
final int operationSize = (int) (end - Integer.BYTES - start);
584-
out.seek(start);
585-
out.writeInt(operationSize);
586-
out.seek(end);
579+
writeOperationWithSize(out, operation);
587580
final BytesReference bytes = out.bytes();
588581
try (ReleasableLock ignored = readLock.acquire()) {
589582
ensureOpen();
@@ -1525,7 +1518,7 @@ public static List<Operation> readOperations(StreamInput input, String source) t
15251518
return operations;
15261519
}
15271520

1528-
static Translog.Operation readOperation(BufferedChecksumStreamInput in) throws IOException {
1521+
public static Translog.Operation readOperation(BufferedChecksumStreamInput in) throws IOException {
15291522
final Translog.Operation operation;
15301523
try {
15311524
final int opSize = in.readInt();
@@ -1592,6 +1585,17 @@ public static void writeOperationNoSize(BufferedChecksumStreamOutput out, Transl
15921585
out.writeInt((int) checksum);
15931586
}
15941587

1588+
public static void writeOperationWithSize(BytesStreamOutput out, Translog.Operation op) throws IOException {
1589+
final long start = out.position();
1590+
out.skip(Integer.BYTES);
1591+
writeOperationNoSize(new BufferedChecksumStreamOutput(out), op);
1592+
final long end = out.position();
1593+
final int operationSize = (int) (end - Integer.BYTES - start);
1594+
out.seek(start);
1595+
out.writeInt(operationSize);
1596+
out.seek(end);
1597+
}
1598+
15951599
/**
15961600
* Gets the minimum generation that could contain any sequence number after the specified sequence number, or the current generation if
15971601
* there is no generation that could any such sequence number.

server/src/test/java/org/elasticsearch/indices/recovery/RecoverySourceHandlerTests.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public class RecoverySourceHandlerTests extends MapperServiceTestCase {
132132
"index",
133133
Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, org.elasticsearch.Version.CURRENT).build()
134134
);
135+
private static final BytesArray TRANSLOG_OPERATION_SOURCE = new BytesArray("{}".getBytes(StandardCharsets.UTF_8));
135136
private final ShardId shardId = new ShardId(INDEX_SETTINGS.getIndex(), 1);
136137
private final ClusterSettings service = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
137138
private final RecoveryPlannerService recoveryPlannerService = PeerOnlyRecoveryPlannerService.INSTANCE;
@@ -1955,29 +1956,33 @@ public void close() {
19551956
};
19561957
}
19571958

1959+
public static Translog.Operation generateOperation(long seqNo) {
1960+
final Translog.Operation op;
1961+
if (randomBoolean()) {
1962+
op = new Translog.Index(
1963+
"id",
1964+
seqNo,
1965+
randomNonNegativeLong(),
1966+
randomNonNegativeLong(),
1967+
TRANSLOG_OPERATION_SOURCE,
1968+
randomBoolean() ? randomAlphaOfLengthBetween(1, 5) : null,
1969+
randomNonNegativeLong()
1970+
);
1971+
} else if (randomBoolean()) {
1972+
op = new Translog.Delete("id", seqNo, randomNonNegativeLong(), randomNonNegativeLong());
1973+
} else {
1974+
op = new Translog.NoOp(seqNo, randomNonNegativeLong(), "test");
1975+
}
1976+
return op;
1977+
}
1978+
19581979
private static List<Translog.Operation> generateOperations(int numOps) {
19591980
final List<Translog.Operation> operations = new ArrayList<>(numOps);
19601981
final BytesArray source = new BytesArray("{}".getBytes(StandardCharsets.UTF_8));
19611982
final Set<Long> seqNos = new HashSet<>();
19621983
for (int i = 0; i < numOps; i++) {
19631984
final long seqNo = randomValueOtherThanMany(n -> seqNos.add(n) == false, ESTestCase::randomNonNegativeLong);
1964-
final Translog.Operation op;
1965-
if (randomBoolean()) {
1966-
op = new Translog.Index(
1967-
"id",
1968-
seqNo,
1969-
randomNonNegativeLong(),
1970-
randomNonNegativeLong(),
1971-
source,
1972-
randomBoolean() ? randomAlphaOfLengthBetween(1, 5) : null,
1973-
randomNonNegativeLong()
1974-
);
1975-
} else if (randomBoolean()) {
1976-
op = new Translog.Delete("id", seqNo, randomNonNegativeLong(), randomNonNegativeLong());
1977-
} else {
1978-
op = new Translog.NoOp(seqNo, randomNonNegativeLong(), "test");
1979-
}
1980-
operations.add(op);
1985+
operations.add(generateOperation(seqNo));
19811986
}
19821987
return operations;
19831988
}

0 commit comments

Comments
 (0)