2020
2121package com .arangodb .internal .net ;
2222
23+ import java .util .Arrays ;
2324import java .util .Collection ;
2425import java .util .List ;
2526
27+ import org .slf4j .Logger ;
28+ import org .slf4j .LoggerFactory ;
29+
2630import com .arangodb .internal .util .HostUtils ;
2731
2832/**
2933 * @author Mark Vollmary
3034 *
3135 */
3236public class ExtendedHostResolver implements HostResolver {
37+
38+ private static final Logger LOGGER = LoggerFactory .getLogger (ExtendedHostResolver .class );
3339
3440 private static final long MAX_CACHE_TIME = 60 * 60 * 1000 ;
41+
3542 private EndpointResolver resolver ;
36- private final List <Host > hosts ;
43+ private HostSet hosts ;
44+
3745 private final Integer maxConnections ;
3846 private final ConnectionFactory connectionFactory ;
47+
3948 private long lastUpdate ;
4049
4150 public ExtendedHostResolver (final List <Host > hosts , final Integer maxConnections ,
4251 final ConnectionFactory connectionFactory ) {
4352 super ();
44- this .hosts = hosts ;
53+ this .hosts = new HostSet ( hosts ) ;
4554 this .maxConnections = maxConnections ;
4655 this .connectionFactory = connectionFactory ;
4756 lastUpdate = 0 ;
@@ -53,23 +62,40 @@ public void init(final EndpointResolver resolver) {
5362 }
5463
5564 @ Override
56- public List <Host > resolve (final boolean initial , final boolean closeConnections ) {
65+
66+ public HostSet resolve (final boolean initial , final boolean closeConnections ) {
67+
5768 if (!initial && isExpired ()) {
69+
5870 lastUpdate = System .currentTimeMillis ();
71+
5972 final Collection <String > endpoints = resolver .resolve (closeConnections );
73+ LOGGER .info ("Resolve " + endpoints .size () + " Endpoints" );
74+ LOGGER .debug ("Endpoints " + Arrays .deepToString (endpoints .toArray ()));
75+
6076 if (!endpoints .isEmpty ()) {
6177 hosts .clear ();
6278 }
79+
6380 for (final String endpoint : endpoints ) {
81+ LOGGER .debug ("Create HOST from " + endpoint );
82+
6483 if (endpoint .matches (".*://.+:[0-9]+" )) {
84+
6585 final String [] s = endpoint .replaceAll (".*://" , "" ).split (":" );
6686 if (s .length == 2 ) {
6787 final HostDescription description = new HostDescription (s [0 ], Integer .valueOf (s [1 ]));
68- hosts .add (HostUtils .createHost (description , maxConnections , connectionFactory ));
88+ hosts .addHost (HostUtils .createHost (description , maxConnections , connectionFactory ));
89+ } else {
90+ LOGGER .warn ("Skip Endpoint (Missung Port)" + endpoint );
6991 }
92+
93+ } else {
94+ LOGGER .warn ("Skip Endpoint (Format)" + endpoint );
7095 }
7196 }
7297 }
98+
7399 return hosts ;
74100 }
75101
0 commit comments