18
18
package com .datastax .oss .driver .internal .core .metadata ;
19
19
20
20
import com .datastax .oss .driver .api .core .metadata .EndPoint ;
21
- import com .datastax .oss .driver .api .core .metadata .Node ;
22
21
import com .datastax .oss .driver .internal .core .context .InternalDriverContext ;
23
22
import com .datastax .oss .driver .internal .core .metadata .token .TokenFactory ;
24
23
import com .datastax .oss .driver .internal .core .metadata .token .TokenFactoryRegistry ;
25
24
import com .datastax .oss .driver .shaded .guava .common .annotations .VisibleForTesting ;
26
25
import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableList ;
27
26
import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableMap ;
27
+ import java .util .ArrayList ;
28
28
import java .util .HashMap ;
29
+ import java .util .HashSet ;
30
+ import java .util .List ;
29
31
import java .util .Map ;
30
32
import java .util .Set ;
31
33
import java .util .UUID ;
@@ -63,22 +65,31 @@ public Result compute(
63
65
TokenFactory tokenFactory = null ;
64
66
65
67
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 <>();
66
72
67
73
for (NodeInfo nodeInfo : nodeInfos ) {
68
74
UUID hostId = nodeInfo .getHostId ();
69
75
if (newNodes .containsKey (hostId )) {
70
76
LOG .warn (
71
77
"[{}] Found duplicate entries with host_id {} in system.peers, "
72
- + "keeping only the first one" ,
78
+ + "keeping only the first one {} " ,
73
79
logPrefix ,
74
- hostId );
80
+ hostId ,
81
+ newNodes .get (hostId ));
75
82
} else {
76
83
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 )) {
79
87
node = new DefaultNode (endPoint , context );
88
+ addedNodes .add (node );
80
89
LOG .debug ("[{}] Adding new node {}" , logPrefix , node );
81
90
} else {
91
+ matchedContactPoints .add (contactPointNode .getEndPoint ());
92
+ node = contactPointNode ;
82
93
LOG .debug ("[{}] Copying contact point {}" , logPrefix , node );
83
94
}
84
95
if (tokenMapEnabled && tokenFactory == null && nodeInfo .getPartitioner () != null ) {
@@ -90,14 +101,11 @@ public Result compute(
90
101
}
91
102
92
103
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 ));
98
106
}
99
107
for (DefaultNode contactPoint : contactPoints ) {
100
- if (findIn ( newNodes . values (), contactPoint .getEndPoint ()) == null ) {
108
+ if (! matchedContactPoints . contains ( contactPoint .getEndPoint ())) {
101
109
eventsBuilder .add (NodeStateEvent .removed (contactPoint ));
102
110
}
103
111
}
@@ -108,10 +116,10 @@ public Result compute(
108
116
eventsBuilder .build ());
109
117
}
110
118
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 ) {
113
121
if (node .getEndPoint ().equals (endPoint )) {
114
- return ( DefaultNode ) node ;
122
+ return node ;
115
123
}
116
124
}
117
125
return null ;
0 commit comments