Skip to content

Commit 833e955

Browse files
authored
HDDS-13822. Add regression testing for OM epoch and txId calculation (#9188)
1 parent c21ec5d commit 833e955

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/TestOmUtils.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.junit.jupiter.api.Assertions.assertNull;
3232
import static org.junit.jupiter.api.Assertions.assertThrows;
3333
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
import static org.junit.jupiter.api.Assertions.fail;
3435
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3536

3637
import java.io.File;
@@ -257,5 +258,72 @@ public void testGetActiveNonListenerOMNodeIdsFiltering() {
257258
assertEquals(expected.size(), result.size());
258259
assertTrue(result.containsAll(expected));
259260
}
260-
}
261261

262+
@Test
263+
void testGetOMEpoch() {
264+
assertEquals(2, OmUtils.getOMEpoch());
265+
assertEquals(OmUtils.EPOCH_WHEN_RATIS_ENABLED, OmUtils.getOMEpoch());
266+
}
267+
268+
@Test
269+
void testAddEpochToTxId() {
270+
assertEquals(0L, OmUtils.addEpochToTxId(0, 0));
271+
assertEquals(1L << 62, OmUtils.addEpochToTxId(1, 0));
272+
assertEquals(2L << 62, OmUtils.addEpochToTxId(2, 0));
273+
assertEquals(3L << 62, OmUtils.addEpochToTxId(3, 0));
274+
275+
long txId = 12345L;
276+
long expected = (2L << 62) | (txId << 8);
277+
assertEquals(expected, OmUtils.addEpochToTxId(2, txId));
278+
279+
long maxTxId = OmUtils.MAX_TRXN_ID;
280+
long maxExpected = (2L << 62) | (maxTxId << 8);
281+
assertEquals(maxExpected, OmUtils.addEpochToTxId(2, maxTxId));
282+
283+
// Verify bit structure
284+
long result = OmUtils.addEpochToTxId(2, 0x123456789ABCDL);
285+
assertEquals(2L, result >>> 62);
286+
assertEquals(0x123456789ABCDL, (result & 0x3FFFFFFFFFFFFFFFL) >>> 8);
287+
}
288+
289+
// Intentionally no tests for getTxIdFromObjectId(); this helper is not
290+
// used in production paths and may be removed in the future.
291+
292+
@Test
293+
void testGetObjectIdFromTxId() {
294+
long txId = 12345L;
295+
long epoch = 2L;
296+
long expected = OmUtils.addEpochToTxId(epoch, txId);
297+
assertEquals(expected, OmUtils.getObjectIdFromTxId(epoch, txId));
298+
299+
for (long e = 0; e <= 3; e++) {
300+
long result = OmUtils.getObjectIdFromTxId(e, txId);
301+
assertEquals(e, result >>> 62);
302+
assertEquals(txId, (result & 0x3FFFFFFFFFFFFFFFL) >>> 8);
303+
}
304+
305+
long maxTxId = OmUtils.MAX_TRXN_ID;
306+
long maxResult = OmUtils.getObjectIdFromTxId(epoch, maxTxId);
307+
assertEquals(epoch, maxResult >>> 62);
308+
assertEquals(maxTxId, (maxResult & 0x3FFFFFFFFFFFFFFFL) >>> 8);
309+
}
310+
311+
@Test
312+
void testGetObjectIdFromTxIdValidation() {
313+
long validTxId = OmUtils.MAX_TRXN_ID;
314+
// Test valid case - should not throw exception
315+
try {
316+
OmUtils.getObjectIdFromTxId(2, validTxId);
317+
} catch (Exception e) {
318+
fail("Valid txId should not throw exception: " + e.getMessage());
319+
}
320+
321+
long invalidTxId = (1L << 54) - 1; // MAX_TRXN_ID + 1
322+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
323+
() -> OmUtils.getObjectIdFromTxId(2, invalidTxId));
324+
assertTrue(exception.getMessage().contains("TransactionID exceeds max limit"));
325+
}
326+
327+
// Consistency checks between epoch and txId are covered by
328+
// testAddEpochToTxId() and testGetObjectIdFromTxId().
329+
}

0 commit comments

Comments
 (0)