anmolnar commented on PR #2338:
URL: https://github.com/apache/zookeeper/pull/2338#issuecomment-3573195690

   I've another idea folks. Wdyt about the following?
   ```java
       private InetSocketAddress resolve(InetSocketAddress address) {
           try {
               String curHostString = address.getHostString();
               List<InetAddress> ra = new 
ArrayList<>(Arrays.asList(this.resolver.getAllByName(curHostString)));
               if (ra.isEmpty()) {
                   return address;
               }
               Map<Boolean, List<InetAddress>> grouped = ra.stream()
                   .collect(Collectors.groupingBy(ia -> ia instanceof 
Inet6Address));
               List<InetAddress> resolvedAddresses;
               if 
(Boolean.parseBoolean(System.getProperty("java.net.preferIPv6Addresses")) && 
!grouped.get(Boolean.TRUE).isEmpty()) {
                   resolvedAddresses = grouped.get(Boolean.TRUE);   // we 
prefer v6, and we have at least one v6 address available
               } else {
                   resolvedAddresses = grouped.get(Boolean.FALSE);  // we 
prefer v4 or we only have v4 addresses available
               }
               Collections.shuffle(resolvedAddresses);
               return new InetSocketAddress(resolvedAddresses.get(0), 
address.getPort());
           } catch (UnknownHostException e) {
               LOG.error("Unable to resolve address: {}", address.toString(), 
e);
               return address;
           }
       }
   ```
   This way we keep the random pick behavior, but first we group the IPs based 
on JVM setting. This might be the most direct solution of the problem and it 
doesn't rely on `getAllByName()` behavior, but at the same time doesn't follow 
it if influenced by some other setting.
   
   I think we don't need feature flag with this approach.


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

Reply via email to