Re: [PR] JAVA-3131: Add #retrieve method to EndPoint for when caller does not … [cassandra-java-driver]

2024-04-28 Thread via GitHub


absurdfarce closed pull request #1735: JAVA-3131: Add #retrieve method to 
EndPoint for when caller does not …
URL: https://github.com/apache/cassandra-java-driver/pull/1735


-- 
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: commits-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org



Re: [PR] JAVA-3131: Add #retrieve method to EndPoint for when caller does not … [cassandra-java-driver]

2024-04-28 Thread via GitHub


absurdfarce commented on PR #1735:
URL: 
https://github.com/apache/cassandra-java-driver/pull/1735#issuecomment-2081935262

   Replaced by https://github.com/apache/cassandra-java-driver/pull/1919.  
Review comments will be handled there.


-- 
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: commits-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org



Re: [PR] JAVA-3131: Add #retrieve method to EndPoint for when caller does not … [cassandra-java-driver]

2024-03-13 Thread via GitHub


SiyaoIsHiding commented on PR #1735:
URL: 
https://github.com/apache/cassandra-java-driver/pull/1735#issuecomment-1996315515

   Work taken over to https://github.com/apache/cassandra-java-driver/pull/1919


-- 
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: commits-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org



[PR] JAVA-3131: Add #retrieve method to EndPoint for when caller does not... [cassandra-java-driver]

2024-03-13 Thread via GitHub


SiyaoIsHiding opened a new pull request, #1919:
URL: https://github.com/apache/cassandra-java-driver/pull/1919

   …need the endpoint to be proactively resolved
   
   Refactor existing usages of EndPoint#resolve to use retrieve when resolved 
ip addresses are not needed.
   
   Taken over from https://github.com/apache/cassandra-java-driver/pull/1735.
   


-- 
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: commits-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org



Re: [PR] JAVA-3131: Add #retrieve method to EndPoint for when caller does not … [cassandra-java-driver]

2024-03-13 Thread via GitHub


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


##
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:
   Sorry, I am confused. Could you pls explain more?
   `InetAddress.getByAddress` does not do reverse lookup. 
`InetAddress.getByName` will look it up. We don't want it to look up. 
Therefore, I think `InetAddress.getByAddress` is the one we should use. :)



-- 
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: commits-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org



Re: [PR] JAVA-3131: Add #retrieve method to EndPoint for when caller does not … [cassandra-java-driver]

2023-11-03 Thread via GitHub


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.
+   *
+   * 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],