/
TcpDiscoveryImpl.java
329 lines (275 loc) · 9.78 KB
/
TcpDiscoveryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* 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 org.apache.ignite.spi.discovery.tcp;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedDeque;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.internal.IgniteInterruptedCheckedException;
import org.apache.ignite.internal.util.typedef.internal.LT;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.spi.IgniteSpiContext;
import org.apache.ignite.spi.IgniteSpiException;
import org.apache.ignite.spi.IgniteSpiThread;
import org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage;
import org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode;
import org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage;
import org.jetbrains.annotations.Nullable;
/**
*
*/
abstract class TcpDiscoveryImpl {
/** Response OK. */
protected static final int RES_OK = 1;
/** Response CONTINUE JOIN. */
protected static final int RES_CONTINUE_JOIN = 100;
/** Response WAIT. */
protected static final int RES_WAIT = 200;
/** */
protected final TcpDiscoverySpi spi;
/** */
protected final IgniteLogger log;
/** */
protected TcpDiscoveryNode locNode;
/** Debug mode. */
protected boolean debugMode;
/** Debug messages history. */
private int debugMsgHist = 512;
/** Received messages. */
@SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized")
protected ConcurrentLinkedDeque<String> debugLog;
/**
* @param spi Adapter.
*/
TcpDiscoveryImpl(TcpDiscoverySpi spi) {
this.spi = spi;
log = spi.log;
}
/**
* This method is intended for troubleshooting purposes only.
*
* @param debugMode {code True} to start SPI in debug mode.
*/
public void setDebugMode(boolean debugMode) {
this.debugMode = debugMode;
}
/**
* This method is intended for troubleshooting purposes only.
*
* @param debugMsgHist Message history log size.
*/
public void setDebugMessageHistory(int debugMsgHist) {
this.debugMsgHist = debugMsgHist;
}
/**
* @param discoMsg Discovery message.
* @param msg Message.
*/
protected void debugLog(@Nullable TcpDiscoveryAbstractMessage discoMsg, String msg) {
assert debugMode;
String msg0 = new SimpleDateFormat("[HH:mm:ss,SSS]").format(new Date(System.currentTimeMillis())) +
'[' + Thread.currentThread().getName() + "][" + getLocalNodeId() +
"-" + locNode.internalOrder() + "] " +
msg;
debugLog.add(msg0);
int delta = debugLog.size() - debugMsgHist;
for (int i = 0; i < delta && debugLog.size() > debugMsgHist; i++)
debugLog.poll();
}
/**
* @return Local node ID.
*/
public UUID getLocalNodeId() {
return spi.locNode.id();
}
/**
* @return Configured node ID (actual node ID can be different if client reconnects).
*/
public UUID getConfiguredNodeId() {
return spi.cfgNodeId;
}
/**
* @param msg Error message.
* @param e Exception.
*/
protected void onException(String msg, Exception e){
spi.getExceptionRegistry().onException(msg, e);
}
/**
* Called when a local node either received from or sent to a remote node a message.
*/
protected void onMessageExchanged() {
// No-op
}
/**
* @param log Logger.
*/
public abstract void dumpDebugInfo(IgniteLogger log);
/**
* @return SPI state string.
*/
public abstract String getSpiState();
/**
* @return Message worker queue current size.
*/
public abstract int getMessageWorkerQueueSize();
/**
* @return Coordinator ID.
*/
public abstract UUID getCoordinator();
/**
* @return Collection of remote nodes.
*/
public abstract Collection<ClusterNode> getRemoteNodes();
/**
* @param nodeId Node id.
* @return Node with given ID or {@code null} if node is not found.
*/
@Nullable public abstract ClusterNode getNode(UUID nodeId);
/**
* @param nodeId Node id.
* @return {@code true} if node alive, {@code false} otherwise.
*/
public abstract boolean pingNode(UUID nodeId);
/**
* Tells discovery SPI to disconnect from topology.
*
* @throws IgniteSpiException If failed.
*/
public abstract void disconnect() throws IgniteSpiException;
/**
* @param msg Message.
* @throws IgniteException If failed.
*/
public abstract void sendCustomEvent(DiscoverySpiCustomMessage msg) throws IgniteException;
/**
* @param nodeId Node id.
* @param warning Warning message to be shown on all nodes.
*/
public abstract void failNode(UUID nodeId, @Nullable String warning);
/**
* @param gridName Grid name.
* @throws IgniteSpiException If failed.
*/
public abstract void spiStart(@Nullable String gridName) throws IgniteSpiException;
/**
* @throws IgniteSpiException If failed.
*/
public abstract void spiStop() throws IgniteSpiException;
/**
* @param spiCtx Spi context.
* @throws IgniteSpiException If failed.
*/
public abstract void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException;
/**
* @param t Thread.
* @return Status as string.
*/
protected static String threadStatus(Thread t) {
if (t == null)
return "N/A";
return t.isAlive() ? "alive" : "dead";
}
/**
* <strong>FOR TEST ONLY!!!</strong>
* <p>
* Simulates this node failure by stopping service threads. So, node will become
* unresponsive.
* <p>
* This method is intended for test purposes only.
*/
abstract void simulateNodeFailure();
/**
* FOR TEST PURPOSE ONLY!
*/
public abstract void brakeConnection();
/**
* <strong>FOR TEST ONLY!!!</strong>
*
* @return Worker thread.
*/
protected abstract IgniteSpiThread workerThread();
/**
* @throws IgniteSpiException If failed.
*/
@SuppressWarnings("BusyWait")
protected final void registerLocalNodeAddress() throws IgniteSpiException {
// Make sure address registration succeeded.
// ... but limit it if join timeout is configured.
long start = spi.getJoinTimeout() > 0 ? U.currentTimeMillis() : 0;
while (true) {
try {
spi.ipFinder.initializeLocalAddresses(
U.resolveAddresses(spi.getAddressResolver(), locNode.socketAddresses()));
// Success.
break;
}
catch (IllegalStateException e) {
throw new IgniteSpiException("Failed to register local node address with IP finder: " +
locNode.socketAddresses(), e);
}
catch (IgniteSpiException e) {
LT.error(log, e, "Failed to register local node address in IP finder on start " +
"(retrying every 2000 ms).");
}
if (start > 0 && (U.currentTimeMillis() - start) > spi.getJoinTimeout())
throw new IgniteSpiException(
"Failed to register local addresses with IP finder within join timeout " +
"(make sure IP finder configuration is correct, and operating system firewalls are disabled " +
"on all host machines, or consider increasing 'joinTimeout' configuration property) " +
"[joinTimeout=" + spi.getJoinTimeout() + ']');
try {
U.sleep(2000);
}
catch (IgniteInterruptedCheckedException e) {
throw new IgniteSpiException("Thread has been interrupted.", e);
}
}
}
/**
* @param ackTimeout Acknowledgement timeout.
* @return {@code True} if acknowledgement timeout is less or equal to
* maximum acknowledgement timeout, {@code false} otherwise.
*/
protected boolean checkAckTimeout(long ackTimeout) {
if (ackTimeout > spi.getMaxAckTimeout()) {
LT.warn(log, null, "Acknowledgement timeout is greater than maximum acknowledgement timeout " +
"(consider increasing 'maxAckTimeout' configuration property) " +
"[ackTimeout=" + ackTimeout + ", maxAckTimeout=" + spi.getMaxAckTimeout() + ']');
return false;
}
return true;
}
/**
* @param addrs Addresses.
*/
protected static List<String> toOrderedList(Collection<InetSocketAddress> addrs) {
List<String> res = new ArrayList<>(addrs.size());
for (InetSocketAddress addr : addrs)
res.add(addr.toString());
Collections.sort(res);
return res;
}
}