Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,17 @@ public Class<SnapshotDiffJob> getTypeClass() {

@Override
public byte[] toPersistedFormatImpl(SnapshotDiffJob object) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation no longer throws any exception. Change the method to

    public byte[] toPersistedFormat(SnapshotDiffJob object) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

return MAPPER.writeValueAsBytes(object);
return object.toProtoBuf().toByteArray();
}

@Override
public SnapshotDiffJob fromPersistedFormatImpl(byte[] rawData) throws IOException {
return MAPPER.readValue(rawData, SnapshotDiffJob.class);
try {
SnapshotDiffJobProto proto = SnapshotDiffJobProto.parseFrom(rawData);
return SnapshotDiffJob.getFromProtoBuf(proto);
} catch (IOException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's catch InvalidProtocolBufferException and add a comment:

      } catch (InvalidProtocolBufferException e) {
        // the rawData was in old format, fallback to the old implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

return MAPPER.readValue(rawData, SnapshotDiffJob.class);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.apache.hadoop.ozone.om.helpers;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hadoop.hdds.utils.db.Codec;
import java.io.IOException;


public class OldSnapshotDiffJobCodecForTesting
implements Codec<SnapshotDiffJob> {

private static final ObjectMapper MAPPER = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

@Override
public Class<SnapshotDiffJob> getTypeClass() {
return SnapshotDiffJob.class;
}

@Override
public byte[] toPersistedFormatImpl(SnapshotDiffJob object) throws IOException {
return MAPPER.writeValueAsBytes(object);
}

@Override
public SnapshotDiffJob fromPersistedFormatImpl(byte[] rawData) throws IOException {
return MAPPER.readValue(rawData, SnapshotDiffJob.class);
}

@Override
public SnapshotDiffJob copyObject(SnapshotDiffJob object) {
// Note: Not really a "copy". from OmDBDiffReportEntryCodec
return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.apache.hadoop.ozone.om.helpers;

import org.apache.hadoop.hdds.utils.db.Codec;
import org.apache.hadoop.ozone.snapshot.SnapshotDiffResponse.JobStatus;
import org.apache.hadoop.ozone.snapshot.SnapshotDiffResponse.SubStatus;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;


/**
* Testing serialization of SnapshotDiffJobCodec objects to/from RocksDB.
*/
public class TestOmSnapshotDiffJobCodec {
private final OldSnapshotDiffJobCodecForTesting oldCodec
= new OldSnapshotDiffJobCodecForTesting();
private final Codec<SnapshotDiffJob> newCodec = SnapshotDiffJob.getCodec();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
private final Codec<SnapshotDiffJob> newCodec = SnapshotDiffJob.getCodec();
private final SnapshotDiffJobCodec newCodec = new SnapshotDiffJobCodec();


@Test
public void testOldJsonSerializedDataCanBeReadByNewCodec() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SnapshotDiffJob.getFromProtoBuf(proto) is expected to throw exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getFromProtoBuf() appears to be a safe method that builds a POJO from a fully-parsed SnapshotDiffJobProto object. Since it relies on accessor methods like getCreationTime() and getVolume() which are expected to be present in a valid proto message, we don’t anticipate any checked exceptions being thrown during this process.

Please correct me if I missed anything or overlooked a potential edge case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just wondering: does the fallback have to be triggered when newCodec.fromPersistedFormatImpl(oldFormatData) is called in this test, since the Jackson and Protobuf formats are incompatible? Or could the Protobuf codec parse the Jackson-encoded binary, making the fallback merely a safeguard?

// Step 1: Construct a SnapshotDiffJob instance
SnapshotDiffJob original = new SnapshotDiffJob(
123456789L,
"job-001",
JobStatus.IN_PROGRESS,
"volA",
"buckB",
"snap1",
"snap2",
true,
false,
100L,
SubStatus.SST_FILE_DELTA_DAG_WALK,
0.0
);

// Step 2: Serialize using the old Jackson-based codec
byte[] oldFormatData = oldCodec.toPersistedFormatImpl(original);

// Step 3: Deserialize using the new default codec (with Protobuf + JSON fallback)
SnapshotDiffJob parsed = newCodec.fromPersistedFormatImpl(oldFormatData);

// Step 4: Verify critical fields remain consistent after round-trip
assertEquals(original.getJobId(), parsed.getJobId());
assertEquals(original.getStatus(), parsed.getStatus());
assertEquals(original.getVolume(), parsed.getVolume());
assertEquals(original.getBucket(), parsed.getBucket());
assertEquals(original.getFromSnapshot(), parsed.getFromSnapshot());
assertEquals(original.getToSnapshot(), parsed.getToSnapshot());
assertEquals(original.isForceFullDiff(), parsed.isForceFullDiff());
assertEquals(original.isNativeDiffDisabled(), parsed.isNativeDiffDisabled());
assertEquals(original.getSubStatus(), parsed.getSubStatus());
assertEquals(original.getTotalDiffEntries(), parsed.getTotalDiffEntries());

assertEquals(0.0, parsed.getKeysProcessedPct());
}
}
Loading