|
18 | 18 | package org.apache.hadoop.ozone.om.helpers; |
19 | 19 |
|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
21 | 22 |
|
| 23 | +import java.nio.ByteBuffer; |
22 | 24 | import org.apache.hadoop.hdds.utils.db.Codec; |
| 25 | +import org.apache.hadoop.hdds.utils.db.CodecBuffer; |
23 | 26 | import org.apache.hadoop.ozone.snapshot.SnapshotDiffResponse.JobStatus; |
24 | 27 | import org.apache.hadoop.ozone.snapshot.SnapshotDiffResponse.SubStatus; |
25 | 28 | import org.junit.jupiter.api.Test; |
@@ -70,4 +73,88 @@ public void testOldJsonSerializedDataCanBeReadByNewCodec() throws Exception { |
70 | 73 |
|
71 | 74 | assertEquals(0.0, parsed.getKeysProcessedPct()); |
72 | 75 | } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testCodecBufferSupport() throws Exception { |
| 79 | + assertTrue(newCodec.supportCodecBuffer()); |
| 80 | + |
| 81 | + SnapshotDiffJob original = new SnapshotDiffJob( |
| 82 | + System.currentTimeMillis(), |
| 83 | + "test-job-buffer", |
| 84 | + JobStatus.DONE, |
| 85 | + "testVol", |
| 86 | + "testBucket", |
| 87 | + "fromSnap", |
| 88 | + "toSnap", |
| 89 | + false, |
| 90 | + true, |
| 91 | + 500L, |
| 92 | + SubStatus.OBJECT_ID_MAP_GEN_FSO, |
| 93 | + 75.5 |
| 94 | + ); |
| 95 | + |
| 96 | + // Test with direct allocator |
| 97 | + try (CodecBuffer buffer = newCodec.toCodecBuffer(original, CodecBuffer.Allocator.getDirect())) { |
| 98 | + SnapshotDiffJob decoded = newCodec.fromCodecBuffer(buffer); |
| 99 | + assertSnapshotDiffJobEquals(original, decoded); |
| 100 | + } |
| 101 | + |
| 102 | + // Test with heap allocator |
| 103 | + try (CodecBuffer buffer = newCodec.toCodecBuffer(original, CodecBuffer.Allocator.getHeap())) { |
| 104 | + SnapshotDiffJob decoded = newCodec.fromCodecBuffer(buffer); |
| 105 | + assertSnapshotDiffJobEquals(original, decoded); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testCodecBufferBackwardCompatibility() throws Exception { |
| 111 | + |
| 112 | + SnapshotDiffJob original = new SnapshotDiffJob( |
| 113 | + 987654321L, |
| 114 | + "compat-job", |
| 115 | + JobStatus.FAILED, |
| 116 | + "volX", |
| 117 | + "buckY", |
| 118 | + "oldSnap", |
| 119 | + "newSnap", |
| 120 | + true, |
| 121 | + true, |
| 122 | + 0L, |
| 123 | + null, |
| 124 | + 0.0 |
| 125 | + ); |
| 126 | + original.setReason("Test failure reason"); |
| 127 | + |
| 128 | + |
| 129 | + byte[] jsonData = oldCodec.toPersistedFormatImpl(original); |
| 130 | + |
| 131 | + // Create a CodecBuffer from the JSON data |
| 132 | + // This simulates reading old format data from storage |
| 133 | + try (CodecBuffer buffer = CodecBuffer.Allocator.getHeap().apply(jsonData.length)) { |
| 134 | + buffer.put(ByteBuffer.wrap(jsonData)); |
| 135 | + |
| 136 | + // The new codec should handle JSON fallback in fromCodecBuffer |
| 137 | + SnapshotDiffJob decoded = newCodec.fromCodecBuffer(buffer); |
| 138 | + |
| 139 | + assertEquals(original.getJobId(), decoded.getJobId()); |
| 140 | + assertEquals(original.getStatus(), decoded.getStatus()); |
| 141 | + assertEquals(original.getReason(), decoded.getReason()); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void assertSnapshotDiffJobEquals(SnapshotDiffJob expected, SnapshotDiffJob actual) { |
| 146 | + assertEquals(expected.getCreationTime(), actual.getCreationTime()); |
| 147 | + assertEquals(expected.getJobId(), actual.getJobId()); |
| 148 | + assertEquals(expected.getStatus(), actual.getStatus()); |
| 149 | + assertEquals(expected.getVolume(), actual.getVolume()); |
| 150 | + assertEquals(expected.getBucket(), actual.getBucket()); |
| 151 | + assertEquals(expected.getFromSnapshot(), actual.getFromSnapshot()); |
| 152 | + assertEquals(expected.getToSnapshot(), actual.getToSnapshot()); |
| 153 | + assertEquals(expected.isForceFullDiff(), actual.isForceFullDiff()); |
| 154 | + assertEquals(expected.isNativeDiffDisabled(), actual.isNativeDiffDisabled()); |
| 155 | + assertEquals(expected.getTotalDiffEntries(), actual.getTotalDiffEntries()); |
| 156 | + assertEquals(expected.getSubStatus(), actual.getSubStatus()); |
| 157 | + assertEquals(expected.getKeysProcessedPct(), actual.getKeysProcessedPct(), 0.001); |
| 158 | + assertEquals(expected.getReason(), actual.getReason()); |
| 159 | + } |
73 | 160 | } |
0 commit comments