-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-9223. Use protobuf for SnapshotDiffJobCodec #8503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
54842d4
f09a925
9dfe382
6db1be4
4402ff7
db2e8df
fd68132
7b25069
1ddf5db
46796a1
1cc7964
d897d24
9bbeaf9
af93172
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 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 java.io.IOException; | ||
| import org.apache.hadoop.hdds.utils.db.Codec; | ||
|
|
||
| /** | ||
| * Codec to serialize / deserialize SnapshotDiffJob. | ||
| */ | ||
| public class OldSnapshotDiffJobCodecForTesting | ||
peterxcli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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,73 @@ | ||||||
| /* | ||||||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||||||
| * contributor license agreements. See the NOTICE file distributed with | ||||||
| * this work for additional information regarding copyright ownership. | ||||||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||
| * (the "License"); you may not use this file except in compliance with | ||||||
| * the License. You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| * See the License for the specific language governing permissions and | ||||||
| * limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| package org.apache.hadoop.ozone.om.helpers; | ||||||
|
|
||||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
|
|
||||||
| 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; | ||||||
|
|
||||||
| /** | ||||||
| * Testing serialization of SnapshotDiffJobCodec objects to/from RocksDB. | ||||||
| */ | ||||||
| public class TestOmSnapshotDiffJobCodec { | ||||||
| private final OldSnapshotDiffJobCodecForTesting oldCodec | ||||||
| = new OldSnapshotDiffJobCodecForTesting(); | ||||||
| private final Codec<SnapshotDiffJob> newCodec = SnapshotDiffJob.getCodec(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
|
|
||||||
| @Test | ||||||
| public void testOldJsonSerializedDataCanBeReadByNewCodec() throws Exception { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just wondering: does the fallback have to be triggered when |
||||||
| // 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); | ||||||
szetszwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| // 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()); | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@szetszwo I’m not sure when it’s appropriate to override toPersistedFormat or toPersistedFormatImpl in a Codec implementation—their use seems inconsistent across the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but I think this is ok as most of the codec do implement this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toPersistedFormatImplis for the case that it needs to throw an exception E other than CodecException since the defaulttoPersistedFormatwill convert E to CodecException.