adoroszlai commented on code in PR #8789: URL: https://github.com/apache/ozone/pull/8789#discussion_r2619280549
########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/ContainerChecksums.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.hdds.scm.container; + +import java.util.Objects; +import net.jcip.annotations.Immutable; + +/** + * Wrapper for container checksums (data, metadata, etc.). + * Provides equality, hash, and hex string rendering. + */ +@Immutable +public final class ContainerChecksums { + + private static final ContainerChecksums UNKNOWN = + new ContainerChecksums(-1L, -1L); + + // Checksum of the data within the wrapper. + private final long dataChecksum; + private static final long UNSET_DATA_CHECKSUM = -1; + + // Checksum of the metadata within the wrapper. + private final long metadataChecksum; + private static final long UNSET_METADATA_CHECKSUM = -1; + + private ContainerChecksums(long dataChecksum, long metadataChecksum) { + this.dataChecksum = dataChecksum; + this.metadataChecksum = metadataChecksum; + } + + public static ContainerChecksums unknown() { + return UNKNOWN; + } + + public static ContainerChecksums of(long dataChecksum, long metadataChecksum) { + return new ContainerChecksums(dataChecksum, metadataChecksum); + } + + public long getDataChecksum() { + // UNSET_DATA_CHECKSUM is an internal placeholder, it should not be used outside this class. + if (needsDataChecksum()) { + return 0; + } + return dataChecksum; + } + + public boolean needsDataChecksum() { + return dataChecksum == UNSET_DATA_CHECKSUM; + } + + public long getMetadataChecksum() { + // UNSET_DATA_CHECKSUM is an internal placeholder, it should not be used outside this class. + if (needsMetadataChecksum()) { Review Comment: nit: this should mention `UNSET_METADATA_CHECKSUM`, also please move comment to `UNSET_METADATA_CHECKSUM` definition ########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/ContainerChecksums.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.hdds.scm.container; + +import java.util.Objects; +import net.jcip.annotations.Immutable; + +/** + * Wrapper for container checksums (data, metadata, etc.). + * Provides equality, hash, and hex string rendering. + */ +@Immutable +public final class ContainerChecksums { + + private static final ContainerChecksums UNKNOWN = + new ContainerChecksums(-1L, -1L); + + // Checksum of the data within the wrapper. + private final long dataChecksum; + private static final long UNSET_DATA_CHECKSUM = -1; + + // Checksum of the metadata within the wrapper. + private final long metadataChecksum; + private static final long UNSET_METADATA_CHECKSUM = -1; + + private ContainerChecksums(long dataChecksum, long metadataChecksum) { + this.dataChecksum = dataChecksum; + this.metadataChecksum = metadataChecksum; + } + + public static ContainerChecksums unknown() { + return UNKNOWN; + } + + public static ContainerChecksums of(long dataChecksum, long metadataChecksum) { + return new ContainerChecksums(dataChecksum, metadataChecksum); + } + + public long getDataChecksum() { + // UNSET_DATA_CHECKSUM is an internal placeholder, it should not be used outside this class. + if (needsDataChecksum()) { + return 0; + } + return dataChecksum; + } + + public boolean needsDataChecksum() { + return dataChecksum == UNSET_DATA_CHECKSUM; + } + + public long getMetadataChecksum() { + // UNSET_DATA_CHECKSUM is an internal placeholder, it should not be used outside this class. + if (needsMetadataChecksum()) { + return 0; + } + return metadataChecksum; + } + + public boolean needsMetadataChecksum() { + return metadataChecksum == UNSET_METADATA_CHECKSUM; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof ContainerChecksums)) { + return false; + } + ContainerChecksums that = (ContainerChecksums) obj; + return getDataChecksum() == that.getDataChecksum() && + getMetadataChecksum() == that.getMetadataChecksum(); + } + + @Override + public int hashCode() { + return Objects.hash(dataChecksum, metadataChecksum); Review Comment: `ContainerChecksums(0, X)` and `ContainerChecksums(-1, X)` are equal, but hash codes are different, which is a bad combination. Do we want to consider these unequal? Then `equals` should use the variables. Otherwise, if we want to consider these to be equal, and all other code passes 0 for unknown value, can we avoid using -1 internally? That would also make `needs...` methods unnecessary. ########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/ContainerChecksums.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.hdds.scm.container; + +import java.util.Objects; +import net.jcip.annotations.Immutable; + +/** + * Wrapper for container checksums (data, metadata, etc.). + * Provides equality, hash, and hex string rendering. + */ +@Immutable +public final class ContainerChecksums { + + private static final ContainerChecksums UNKNOWN = + new ContainerChecksums(-1L, -1L); Review Comment: nit: use `UNSET_DATA_CHECKSUM` and `UNSET_METADATA_CHECKSUM` ########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/scm/ContainerReplicaHistory.java: ########## @@ -84,23 +85,29 @@ public void setState(String state) { } public long getDataChecksum() { - return dataChecksum; + return getChecksums().getDataChecksum(); } - public void setDataChecksum(long dataChecksum) { - this.dataChecksum = dataChecksum; + public ContainerChecksums getChecksums() { + return checksums; + } + + public void setChecksums(ContainerChecksums checksums) { + this.checksums = checksums; } public static ContainerReplicaHistory fromProto( ContainerReplicaHistoryProto proto) { return new ContainerReplicaHistory(UUID.fromString(proto.getUuid()), proto.getFirstSeenTime(), proto.getLastSeenTime(), proto.getBcsId(), - proto.getState(), proto.getDataChecksum()); + proto.getState(), ContainerChecksums.of(proto.getDataChecksum(), 0L)); } public ContainerReplicaHistoryProto toProto() { return ContainerReplicaHistoryProto.newBuilder().setUuid(uuid.toString()) .setFirstSeenTime(firstSeenTime).setLastSeenTime(lastSeenTime) - .setBcsId(bcsId).setState(state).setDataChecksum(dataChecksum).build(); + .setBcsId(bcsId).setState(state) + .setDataChecksum(checksums != null ? checksums.getDataChecksum() : 0L) Review Comment: ```suggestion .setDataChecksum(checksums.getDataChecksum()) ``` ########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/ContainerChecksums.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.hdds.scm.container; + +import java.util.Objects; +import net.jcip.annotations.Immutable; + +/** + * Wrapper for container checksums (data, metadata, etc.). + * Provides equality, hash, and hex string rendering. + */ +@Immutable +public final class ContainerChecksums { + + private static final ContainerChecksums UNKNOWN = + new ContainerChecksums(-1L, -1L); + + // Checksum of the data within the wrapper. + private final long dataChecksum; + private static final long UNSET_DATA_CHECKSUM = -1; + + // Checksum of the metadata within the wrapper. + private final long metadataChecksum; + private static final long UNSET_METADATA_CHECKSUM = -1; + + private ContainerChecksums(long dataChecksum, long metadataChecksum) { + this.dataChecksum = dataChecksum; + this.metadataChecksum = metadataChecksum; + } + + public static ContainerChecksums unknown() { + return UNKNOWN; + } + + public static ContainerChecksums of(long dataChecksum, long metadataChecksum) { + return new ContainerChecksums(dataChecksum, metadataChecksum); + } + + public long getDataChecksum() { + // UNSET_DATA_CHECKSUM is an internal placeholder, it should not be used outside this class. + if (needsDataChecksum()) { Review Comment: nit: move comment to `UNSET_DATA_CHECKSUM` definition ########## hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/AbstractContainerReportHandler.java: ########## @@ -362,7 +362,7 @@ private void updateContainerReplica(final DatanodeDetails datanodeDetails, .setReplicaIndex(replicaProto.getReplicaIndex()) .setBytesUsed(replicaProto.getUsed()) .setEmpty(replicaProto.getIsEmpty()) - .setDataChecksum(replicaProto.getDataChecksum()) + .setChecksums(ContainerChecksums.of(replicaProto.getDataChecksum(), 0L)) Review Comment: Please replace all calls like this with `ContainerChecksums.dataOnly(...)` as [suggested earlier](https://github.com/apache/ozone/pull/8789#discussion_r2221522284). Rationale: - Avoid duplication of 0L as unknown value - Avoid mismatch between internal-only constant with value -1L - Easier to understand ########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/scm/ContainerReplicaHistory.java: ########## @@ -84,23 +85,29 @@ public void setState(String state) { } public long getDataChecksum() { - return dataChecksum; + return getChecksums().getDataChecksum(); } - public void setDataChecksum(long dataChecksum) { - this.dataChecksum = dataChecksum; + public ContainerChecksums getChecksums() { + return checksums; + } + + public void setChecksums(ContainerChecksums checksums) { + this.checksums = checksums; Review Comment: ```suggestion this.checksums = checksums != null ? checksums : ContainerChecksums.unknown(); ``` ########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/scm/ContainerReplicaHistory.java: ########## @@ -39,16 +40,16 @@ public class ContainerReplicaHistory { private long bcsId; private String state; - private long dataChecksum; + private ContainerChecksums checksums; public ContainerReplicaHistory(UUID id, Long firstSeenTime, - Long lastSeenTime, long bcsId, String state, long dataChecksum) { + Long lastSeenTime, long bcsId, String state, ContainerChecksums checksums) { this.uuid = id; this.firstSeenTime = firstSeenTime; this.lastSeenTime = lastSeenTime; this.bcsId = bcsId; this.state = state; - this.dataChecksum = dataChecksum; + this.checksums = checksums; Review Comment: ```suggestion setChecksums(checksums); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
