absurdfarce commented on code in PR #1735:
URL: 
https://github.com/apache/cassandra-java-driver/pull/1735#discussion_r1381861392


##########
core/src/test/java/com/datastax/oss/driver/internal/core/metadata/SniEndPointTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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 com.datastax.oss.driver.internal.core.metadata;
+
+import static com.datastax.oss.driver.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.ArrayUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Spy;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SniEndPointTest {
+  private static InetSocketAddress SNI_ADDRESS =
+      InetSocketAddress.createUnresolved("unittest.host", 12345);
+  private static String SERVER_NAME = "unittest.server.name";
+
+  @Spy private SniEndPoint sniEndPoint = new SniEndPoint(SNI_ADDRESS, 
SERVER_NAME);
+
+  private static InetAddress[] createAddresses(String... addrs) {
+    return Stream.of(addrs)
+        .map(
+            addr -> {
+              try {
+                int[] comp = 
Arrays.stream(addr.split("\\.")).mapToInt(Integer::parseInt).toArray();
+                return InetAddress.getByAddress(
+                    new byte[] {(byte) comp[0], (byte) comp[1], (byte) 
comp[2], (byte) comp[3]});

Review Comment:
   Is there a particular reason to jump through the splitting/to Int/toArray 
ops here rather than just using InetAddress.getByName()?  I don't think it does 
a reverse lookup if you just give it a string containing an IP address but I 
could be wrong.



##########
core/src/main/java/com/datastax/oss/driver/internal/core/metadata/DefaultEndPoint.java:
##########
@@ -38,6 +38,11 @@ public DefaultEndPoint(InetSocketAddress address) {
   @NonNull
   @Override
   public InetSocketAddress resolve() {
+    return retrieve();

Review Comment:
   Nit: save yourself the method call overhead here and just return address 
directly... ?



##########
core/src/main/java/com/datastax/oss/driver/api/core/metadata/EndPoint.java:
##########
@@ -40,6 +40,17 @@ public interface EndPoint {
   @NonNull
   SocketAddress resolve();
 
+  /**
+   * Returns a possibly unresolved instance to a socket address.
+   *
+   * <p>This should be called when the address does not need to be proactively 
resolved. For example
+   * if the node hostname or port number is needed.
+   */
+  @NonNull
+  default SocketAddress retrieve() {
+    return resolve();
+  }

Review Comment:
   I'm wondering if it makes sense to provide a default implementation for the 
new method.  It's unlikely we'll be adding a new endpoint type anytime soon but 
failing to distinguish between "look up the addresses again" and "give me the 
addresses you got last time you looked them up" is what got us into this 
situation in the first place.  I kinda feel like having this as a default might 
lend itself to landing back in exactly that situation.



##########
core/src/main/java/com/datastax/oss/driver/internal/core/metadata/SniEndPoint.java:
##########
@@ -64,14 +69,32 @@ public InetSocketAddress resolve() {
       // The order of the returned address is unspecified. Sort by IP to make 
sure we get a true
       // round-robin
       Arrays.sort(aRecords, IP_COMPARATOR);
-      int index = (aRecords.length == 1) ? 0 : (int) OFFSET.getAndIncrement() 
% aRecords.length;
-      return new InetSocketAddress(aRecords[index], proxyAddress.getPort());
+
+      // get next offset value, reset OFFSET if wrapped around to negative
+      int nextOffset = OFFSET.getAndIncrement();
+      if (nextOffset < 0) {
+        // if negative set OFFSET to 1 and nextOffset to 0, else simulate 
getAndIncrement()
+        nextOffset = OFFSET.updateAndGet(v -> v < 0 ? 1 : v + 1) - 1;
+      }
+
+      return new InetSocketAddress(aRecords[nextOffset % aRecords.length], 
proxyAddress.getPort());
     } catch (UnknownHostException e) {
       throw new IllegalArgumentException(
           "Could not resolve proxy address " + proxyAddress.getHostName(), e);
     }
   }
 
+  @VisibleForTesting
+  InetAddress[] resolveARecords() throws UnknownHostException {
+    // moving static call to method to allow mocking in tests
+    return InetAddress.getAllByName(proxyAddress.getHostName());
+  }
+
+  @Override
+  public InetSocketAddress retrieve() {
+    return proxyAddress;

Review Comment:
   Don't we want to cache the last value returned by resolve() and return 
_that_ here instead?



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