1818package com .datastax .oss .driver .internal .core .metadata ;
1919
2020import com .datastax .oss .driver .api .core .metadata .EndPoint ;
21- import com .datastax .oss .driver .api .core .metadata .Node ;
2221import com .datastax .oss .driver .internal .core .context .InternalDriverContext ;
2322import com .datastax .oss .driver .internal .core .metadata .token .TokenFactory ;
2423import com .datastax .oss .driver .internal .core .metadata .token .TokenFactoryRegistry ;
2524import com .datastax .oss .driver .shaded .guava .common .annotations .VisibleForTesting ;
2625import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableList ;
2726import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableMap ;
27+ import java .util .ArrayList ;
2828import java .util .HashMap ;
29+ import java .util .HashSet ;
30+ import java .util .List ;
2931import java .util .Map ;
3032import java .util .Set ;
3133import java .util .UUID ;
@@ -63,22 +65,31 @@ public Result compute(
6365 TokenFactory tokenFactory = null ;
6466
6567 Map <UUID , DefaultNode > newNodes = new HashMap <>();
68+ // Contact point nodes don't have host ID as well as other info yet, so we fill them with node
69+ // info found on first match by endpoint
70+ Set <EndPoint > matchedContactPoints = new HashSet <>();
71+ List <DefaultNode > addedNodes = new ArrayList <>();
6672
6773 for (NodeInfo nodeInfo : nodeInfos ) {
6874 UUID hostId = nodeInfo .getHostId ();
6975 if (newNodes .containsKey (hostId )) {
7076 LOG .warn (
7177 "[{}] Found duplicate entries with host_id {} in system.peers, "
72- + "keeping only the first one" ,
78+ + "keeping only the first one {} " ,
7379 logPrefix ,
74- hostId );
80+ hostId ,
81+ newNodes .get (hostId ));
7582 } else {
7683 EndPoint endPoint = nodeInfo .getEndPoint ();
77- DefaultNode node = findIn (contactPoints , endPoint );
78- if (node == null ) {
84+ DefaultNode contactPointNode = findContactPointNode (endPoint );
85+ DefaultNode node ;
86+ if (contactPointNode == null || matchedContactPoints .contains (endPoint )) {
7987 node = new DefaultNode (endPoint , context );
88+ addedNodes .add (node );
8089 LOG .debug ("[{}] Adding new node {}" , logPrefix , node );
8190 } else {
91+ matchedContactPoints .add (contactPointNode .getEndPoint ());
92+ node = contactPointNode ;
8293 LOG .debug ("[{}] Copying contact point {}" , logPrefix , node );
8394 }
8495 if (tokenMapEnabled && tokenFactory == null && nodeInfo .getPartitioner () != null ) {
@@ -90,14 +101,11 @@ public Result compute(
90101 }
91102
92103 ImmutableList .Builder <Object > eventsBuilder = ImmutableList .builder ();
93-
94- for (DefaultNode newNode : newNodes .values ()) {
95- if (findIn (contactPoints , newNode .getEndPoint ()) == null ) {
96- eventsBuilder .add (NodeStateEvent .added (newNode ));
97- }
104+ for (DefaultNode addedNode : addedNodes ) {
105+ eventsBuilder .add (NodeStateEvent .added (addedNode ));
98106 }
99107 for (DefaultNode contactPoint : contactPoints ) {
100- if (findIn ( newNodes . values (), contactPoint .getEndPoint ()) == null ) {
108+ if (! matchedContactPoints . contains ( contactPoint .getEndPoint ())) {
101109 eventsBuilder .add (NodeStateEvent .removed (contactPoint ));
102110 }
103111 }
@@ -108,10 +116,10 @@ public Result compute(
108116 eventsBuilder .build ());
109117 }
110118
111- private DefaultNode findIn ( Iterable <? extends Node > nodes , EndPoint endPoint ) {
112- for (Node node : nodes ) {
119+ private DefaultNode findContactPointNode ( EndPoint endPoint ) {
120+ for (DefaultNode node : contactPoints ) {
113121 if (node .getEndPoint ().equals (endPoint )) {
114- return ( DefaultNode ) node ;
122+ return node ;
115123 }
116124 }
117125 return null ;
0 commit comments