vinothchandar commented on a change in pull request #1106: [HUDI-209] Implement 
JMX metrics reporter
URL: https://github.com/apache/incubator-hudi/pull/1106#discussion_r361340109
 
 

 ##########
 File path: hudi-client/src/main/java/org/apache/hudi/client/utils/NetUtils.java
 ##########
 @@ -0,0 +1,202 @@
+/*
+ * 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.hudi.client.utils;
+
+import org.apache.hudi.exception.HoodieException;
+
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+
+/**
+ * Utility for various network related tasks (such as finding free ports).
+ */
+public class NetUtils {
+  // ------------------------------------------------------------------------
+  //  Encoding of IP addresses for URLs
+  // ------------------------------------------------------------------------
+
+  /**
+   * Encodes an IP address properly as a URL string. This method makes sure 
that IPv6 addresses have
+   * the proper formatting to be included in URLs.
+   *
+   * @param address The IP address to encode.
+   * @return The proper URL string encoded IP address.
+   */
+  public static String ipAddressToUrlString(InetAddress address) {
+    if (address == null) {
+      throw new NullPointerException("address is null");
+    } else if (address instanceof Inet4Address) {
+      return address.getHostAddress();
+    } else if (address instanceof Inet6Address) {
+      return getIPv6UrlRepresentation((Inet6Address) address);
+    } else {
+      throw new IllegalArgumentException("Unrecognized type of InetAddress: " 
+ address);
+    }
+  }
+
+  /**
+   * Creates a compressed URL style representation of an Inet6Address.
+   *
+   * <p>This method copies and adopts code from Google's Guava library.
+   * We re-implement this here in order to reduce dependency on Guava. The 
Guava library has
+   * frequently caused dependency conflicts in the past.
+   */
+  private static String getIPv6UrlRepresentation(Inet6Address address) {
+    return getIPv6UrlRepresentation(address.getAddress());
+  }
+
+  /**
+   * Creates a compressed URL style representation of an Inet6Address.
+   *
+   * <p>This method copies and adopts code from Google's Guava library.
+   * We re-implement this here in order to reduce dependency on Guava. The 
Guava library has
+   * frequently caused dependency conflicts in the past.
+   */
+  private static String getIPv6UrlRepresentation(byte[] addressBytes) {
+    // first, convert bytes to 16 bit chunks
+    int[] hextets = new int[8];
+    for (int i = 0; i < hextets.length; i++) {
+      hextets[i] = (addressBytes[2 * i] & 0xFF) << 8 | (addressBytes[2 * i + 
1] & 0xFF);
+    }
+
+    // now, find the sequence of zeros that should be compressed
+    int bestRunStart = -1;
+    int bestRunLength = -1;
+    int runStart = -1;
+    for (int i = 0; i < hextets.length + 1; i++) {
+      if (i < hextets.length && hextets[i] == 0) {
+        if (runStart < 0) {
+          runStart = i;
+        }
+      } else if (runStart >= 0) {
+        int runLength = i - runStart;
+        if (runLength > bestRunLength) {
+          bestRunStart = runStart;
+          bestRunLength = runLength;
+        }
+        runStart = -1;
+      }
+    }
+    if (bestRunLength >= 2) {
+      Arrays.fill(hextets, bestRunStart, bestRunStart + bestRunLength, -1);
+    }
+
+    // convert into text form
+    StringBuilder buf = new StringBuilder(40);
+    buf.append('[');
+
+    boolean lastWasNumber = false;
+    for (int i = 0; i < hextets.length; i++) {
+      boolean thisIsNumber = hextets[i] >= 0;
+      if (thisIsNumber) {
+        if (lastWasNumber) {
+          buf.append(':');
+        }
+        buf.append(Integer.toHexString(hextets[i]));
+      } else {
+        if (i == 0 || lastWasNumber) {
+          buf.append("::");
+        }
+      }
+      lastWasNumber = thisIsNumber;
+    }
+    buf.append(']');
+    return buf.toString();
+  }
+
+  // ------------------------------------------------------------------------
+  //  Port range parsing
+  // ------------------------------------------------------------------------
+
+  /**
+   * Returns an iterator over available ports defined by the range definition.
+   *
+   * @param rangeDefinition String describing a single port, a range of ports 
or multiple ranges.
+   * @return Set of ports from the range definition
+   * @throws NumberFormatException If an invalid string is passed.
+   */
+  public static Iterator<Integer> getPortRangeFromString(String 
rangeDefinition)
 
 Review comment:
   Alternatively, can we just make the port a config and live with that for 
now? I would rather prefer that

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to