szetszwo commented on code in PR #6656:
URL: https://github.com/apache/ozone/pull/6656#discussion_r1597469945


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/protocol/DatanodeDetails.java:
##########
@@ -123,17 +125,17 @@ private DatanodeDetails(Builder b) {
   }
 
   public DatanodeDetails(DatanodeDetails datanodeDetails) {
-    super(datanodeDetails.getHostName(), datanodeDetails.getNetworkLocation(),
+    super(datanodeDetails.getHostNameAsByteString(), 
datanodeDetails.getNetworkLocationAsByteString(),
         datanodeDetails.getParent(), datanodeDetails.getLevel(),
         datanodeDetails.getCost());
     this.uuid = datanodeDetails.uuid;
-    this.uuidString = uuid.toString();
+    this.uuidString = new StringWithByteString(uuid.toString());

Review Comment:
   This should be:
   ```java
       this.uuidString = datanodeDetails.uuidString;
   ```



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/util/StringWithByteString.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.util;
+
+import com.google.protobuf.ByteString;
+import net.jcip.annotations.Immutable;
+
+/**
+ * Class to encapsulate and cache the conversion of a Java String to a 
ByteString.
+ */
+@Immutable
+public final class StringWithByteString {
+  private final String string;
+  private final ByteString bytes;
+
+  public StringWithByteString(String string) {
+    this(string, ByteString.copyFromUtf8(string));
+  }
+
+  public StringWithByteString(String string, ByteString bytes) {
+    this.string = string;
+    this.bytes = bytes;
+  }
+
+  @Override
+  public String toString() {
+    return string;
+  }

Review Comment:
   My fault -- it is better to call it `getString` so that it is easier to 
trace.  Fortunately, IdealIJ can "rename only currently method" (not "rename 
base method").



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/protocol/DatanodeDetails.java:
##########
@@ -671,7 +727,7 @@ public Builder setNetworkName(String name) {
      * @return DatanodeDetails.Builder
      */
     public Builder setNetworkLocation(String loc) {
-      this.networkLocation = loc;
+      this.networkLocation = new StringWithByteString(NetUtils.normalize(loc));

Review Comment:
   Since the `loc` is likely to be already normalized, we may add another  
`setNetworkLocation(String loc, ByteString locBytes)` method as below:
   ```java
       public Builder setNetworkLocation(String loc) {
         return setNetworkLocation(loc, null);
       }
   
       public Builder setNetworkLocation(String loc, ByteString locBytes) {
         final String normalized = NetUtils.normalize(loc);
         this.networkLocation = normalized.equals(loc) && locBytes != null
             ? new StringWithByteString(normalized, locBytes)
             : new StringWithByteString(normalized);
         return this;
       }
   ```



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/util/StringWithByteString.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.util;
+
+import com.google.protobuf.ByteString;
+import net.jcip.annotations.Immutable;
+
+/**
+ * Class to encapsulate and cache the conversion of a Java String to a 
ByteString.
+ */
+@Immutable
+public final class StringWithByteString {
+  private final String string;
+  private final ByteString bytes;
+
+  public StringWithByteString(String string) {
+    this(string, ByteString.copyFromUtf8(string));
+  }
+
+  public StringWithByteString(String string, ByteString bytes) {
+    this.string = string;
+    this.bytes = bytes;

Review Comment:
   Check null:
   ```java
       this.string = Objects.requireNonNull(string, "string == null");
       this.bytes = Objects.requireNonNull(bytes, "bytes == null");
   ```



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/NodeImpl.java:
##########
@@ -46,18 +47,22 @@ public class NodeImpl implements Node {
    * {@link NetConstants#PATH_SEPARATOR})
    * @param location this node's location
    */
-  public NodeImpl(String name, String location, int cost) {
-    if (name != null && name.contains(PATH_SEPARATOR_STR)) {
+  public NodeImpl(StringWithByteString name, StringWithByteString location, 
int cost) {
+    if (name != null && name.toString() != null && 
name.toString().contains(PATH_SEPARATOR_STR)) {

Review Comment:
   Let's enforce that `StringWithByteString.toString()` is never null?  We may 
add `Objects.requireNonNull(..)` in the `StringWithByteString` constructor.
   - If the original string is null, then `name == null`;
   - otherwise,  `name != null && name.toString() != null`.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/protocol/DatanodeDetails.java:
##########
@@ -660,7 +704,19 @@ public Builder setHostName(String host) {
      * @return DatanodeDetails.Builder
      */
     public Builder setNetworkName(String name) {
-      this.networkName = name;
+      this.networkName = new StringWithByteString(name);
+      return this;
+    }

Review Comment:
   This is no longer used.  Let's remove it?



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/util/StringWithByteString.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.util;
+
+import com.google.protobuf.ByteString;
+import net.jcip.annotations.Immutable;
+
+/**
+ * Class to encapsulate and cache the conversion of a Java String to a 
ByteString.
+ */
+@Immutable
+public final class StringWithByteString {
+  private final String string;
+  private final ByteString bytes;
+
+  public StringWithByteString(String string) {
+    this(string, ByteString.copyFromUtf8(string));
+  }

Review Comment:
   For handling null, let's check it to
   ```java
     public static StringWithByteString valueOf(String string) {
       return string != null? new StringWithByteString(string, 
ByteString.copyFromUtf8(string)) : null;
     }
   ```



-- 
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]

Reply via email to