-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ClientConnectionManagerImpl.java
762 lines (660 loc) · 30 KB
/
ClientConnectionManagerImpl.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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 com.hazelcast.client.connection.nio;
import com.hazelcast.client.AuthenticationException;
import com.hazelcast.client.ClientExtension;
import com.hazelcast.client.HazelcastClientNotActiveException;
import com.hazelcast.client.HazelcastClientOfflineException;
import com.hazelcast.client.config.ClientNetworkConfig;
import com.hazelcast.client.connection.AddressProvider;
import com.hazelcast.client.connection.AddressTranslator;
import com.hazelcast.client.connection.ClientConnectionManager;
import com.hazelcast.client.connection.ClientConnectionStrategy;
import com.hazelcast.client.impl.ClientTypes;
import com.hazelcast.client.impl.client.ClientPrincipal;
import com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.protocol.AuthenticationStatus;
import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.ClientAuthenticationCodec;
import com.hazelcast.client.impl.protocol.codec.ClientAuthenticationCustomCodec;
import com.hazelcast.client.spi.ClientContext;
import com.hazelcast.client.spi.impl.ClientExecutionServiceImpl;
import com.hazelcast.client.spi.impl.ClientInvocation;
import com.hazelcast.client.spi.impl.ClientInvocationFuture;
import com.hazelcast.config.SSLConfig;
import com.hazelcast.config.SocketInterceptorConfig;
import com.hazelcast.core.ExecutionCallback;
import com.hazelcast.core.HazelcastException;
import com.hazelcast.instance.BuildInfoProvider;
import com.hazelcast.internal.networking.Channel;
import com.hazelcast.internal.networking.ChannelErrorHandler;
import com.hazelcast.internal.networking.nio.NioNetworking;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.logging.ILogger;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.ClassLoaderUtil;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.nio.SocketInterceptor;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.security.Credentials;
import com.hazelcast.security.ICredentialsFactory;
import com.hazelcast.security.UsernamePasswordCredentials;
import com.hazelcast.spi.properties.HazelcastProperties;
import com.hazelcast.spi.serialization.SerializationService;
import com.hazelcast.util.AddressUtil;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import static com.hazelcast.client.spi.properties.ClientProperty.ALLOW_INVOCATIONS_WHEN_DISCONNECTED;
import static com.hazelcast.client.spi.properties.ClientProperty.IO_BALANCER_INTERVAL_SECONDS;
import static com.hazelcast.client.spi.properties.ClientProperty.IO_INPUT_THREAD_COUNT;
import static com.hazelcast.client.spi.properties.ClientProperty.IO_OUTPUT_THREAD_COUNT;
import static com.hazelcast.nio.IOUtil.closeResource;
import static com.hazelcast.util.ExceptionUtil.rethrow;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
/**
* Implementation of {@link ClientConnectionManager}.
*/
@SuppressWarnings("checkstyle:classdataabstractioncoupling")
public class ClientConnectionManagerImpl implements ClientConnectionManager {
private static final int DEFAULT_SSL_THREAD_COUNT = 3;
protected final AtomicInteger connectionIdGen = new AtomicInteger();
protected volatile boolean alive;
private final ILogger logger;
private final int connectionTimeoutMillis;
private final HazelcastClientInstanceImpl client;
private final SocketInterceptor socketInterceptor;
private final ClientExecutionServiceImpl executionService;
private final AddressTranslator addressTranslator;
private final ConcurrentMap<Address, ClientConnection> activeConnections = new ConcurrentHashMap<Address, ClientConnection>();
private final ConcurrentMap<Address, AuthenticationFuture> connectionsInProgress =
new ConcurrentHashMap<Address, AuthenticationFuture>();
private final Collection<ConnectionListener> connectionListeners = new CopyOnWriteArrayList<ConnectionListener>();
private final boolean allowInvokeWhenDisconnected;
private final ICredentialsFactory credentialsFactory;
private final NioNetworking networking;
private final HeartbeatManager heartbeat;
private final ClusterConnector clusterConnector;
private final long authenticationTimeout;
private volatile ClientPrincipal principal;
private final ClientConnectionStrategy connectionStrategy;
// accessed only in synchronized block
private final LinkedList<Integer> outboundPorts = new LinkedList<Integer>();
private final int outboundPortCount;
private volatile Credentials lastCredentials;
public ClientConnectionManagerImpl(HazelcastClientInstanceImpl client, AddressTranslator addressTranslator,
Collection<AddressProvider> addressProviders) {
allowInvokeWhenDisconnected = client.getProperties().getBoolean(ALLOW_INVOCATIONS_WHEN_DISCONNECTED);
this.client = client;
this.addressTranslator = addressTranslator;
this.logger = client.getLoggingService().getLogger(ClientConnectionManager.class);
ClientNetworkConfig networkConfig = client.getClientConfig().getNetworkConfig();
final int connTimeout = networkConfig.getConnectionTimeout();
this.connectionTimeoutMillis = connTimeout == 0 ? Integer.MAX_VALUE : connTimeout;
this.executionService = (ClientExecutionServiceImpl) client.getClientExecutionService();
this.networking = initNetworking(client);
this.socketInterceptor = initSocketInterceptor(networkConfig.getSocketInterceptorConfig());
this.credentialsFactory = client.getCredentialsFactory();
this.connectionStrategy = initializeStrategy(client);
this.outboundPorts.addAll(getOutboundPorts(networkConfig));
this.outboundPortCount = outboundPorts.size();
this.heartbeat = new HeartbeatManager(this, client);
this.authenticationTimeout = heartbeat.getHeartbeatTimeout();
this.clusterConnector = new ClusterConnector(client, this, connectionStrategy, addressProviders);
}
private Collection<Integer> getOutboundPorts(ClientNetworkConfig networkConfig) {
Collection<Integer> outboundPorts = networkConfig.getOutboundPorts();
Collection<String> outboundPortDefinitions = networkConfig.getOutboundPortDefinitions();
return AddressUtil.getOutboundPorts(outboundPorts, outboundPortDefinitions);
}
private ClientConnectionStrategy initializeStrategy(HazelcastClientInstanceImpl client) {
ClientConnectionStrategy strategy;
//internal property
String className = client.getProperties().get("hazelcast.client.connection.strategy.classname");
if (className != null) {
try {
ClassLoader configClassLoader = client.getClientConfig().getClassLoader();
return ClassLoaderUtil.newInstance(configClassLoader, className);
} catch (Exception e) {
throw rethrow(e);
}
} else {
strategy = new DefaultClientConnectionStrategy();
}
return strategy;
}
public NioNetworking getNetworking() {
return networking;
}
protected NioNetworking initNetworking(HazelcastClientInstanceImpl client) {
HazelcastProperties properties = client.getProperties();
SSLConfig sslConfig = client.getClientConfig().getNetworkConfig().getSSLConfig();
boolean sslEnabled = sslConfig != null && sslConfig.isEnabled();
int configuredInputThreads = properties.getInteger(IO_INPUT_THREAD_COUNT);
int configuredOutputThreads = properties.getInteger(IO_OUTPUT_THREAD_COUNT);
int inputThreads;
if (configuredInputThreads == -1) {
inputThreads = sslEnabled ? DEFAULT_SSL_THREAD_COUNT : 1;
} else {
inputThreads = configuredInputThreads;
}
int outputThreads;
if (configuredOutputThreads == -1) {
outputThreads = sslEnabled ? DEFAULT_SSL_THREAD_COUNT : 1;
} else {
outputThreads = configuredOutputThreads;
}
return new NioNetworking(
new NioNetworking.Context()
.loggingService(client.getLoggingService())
.metricsRegistry(client.getMetricsRegistry())
.threadNamePrefix(client.getName())
.errorHandler(new ClientConnectionChannelErrorHandler())
.inputThreadCount(inputThreads)
.outputThreadCount(outputThreads)
.balancerIntervalSeconds(properties.getInteger(IO_BALANCER_INTERVAL_SECONDS))
.channelInitializer(client.getClientExtension().createChannelInitializer()));
}
private SocketInterceptor initSocketInterceptor(SocketInterceptorConfig sic) {
if (sic != null && sic.isEnabled()) {
ClientExtension clientExtension = client.getClientExtension();
return clientExtension.createSocketInterceptor();
}
return null;
}
public ClientConnectionStrategy getConnectionStrategy() {
return connectionStrategy;
}
@Override
public Collection<ClientConnection> getActiveConnections() {
return activeConnections.values();
}
@Override
public boolean isAlive() {
return alive;
}
public synchronized void start(ClientContext clientContext) {
if (alive) {
return;
}
alive = true;
startNetworking();
heartbeat.start();
connectionStrategy.init(clientContext);
connectionStrategy.start();
}
protected void startNetworking() {
networking.start();
}
public synchronized void shutdown() {
if (!alive) {
return;
}
alive = false;
for (Connection connection : activeConnections.values()) {
connection.close("Hazelcast client is shutting down", null);
}
clusterConnector.shutdown();
stopNetworking();
connectionListeners.clear();
heartbeat.shutdown();
connectionStrategy.shutdown();
credentialsFactory.destroy();
}
@Override
public ClientPrincipal getPrincipal() {
return principal;
}
private void setPrincipal(ClientPrincipal principal) {
this.principal = principal;
}
protected void stopNetworking() {
networking.shutdown();
}
@Override
public Connection getActiveConnection(Address target) {
if (target == null) {
return null;
}
return activeConnections.get(target);
}
@Override
public Connection getOrConnect(Address address) throws IOException {
return getOrConnect(address, false);
}
@Override
public Connection getOrTriggerConnect(Address target, boolean acquiresResources) throws IOException {
Connection connection = getConnection(target, false, acquiresResources);
if (connection != null) {
return connection;
}
triggerConnect(target, false);
return null;
}
private Connection getConnection(Address target, boolean asOwner, boolean acquiresResources) throws IOException {
checkAllowed(target, asOwner, acquiresResources);
if (target == null) {
throw new IllegalStateException("Address can not be null");
}
ClientConnection connection = activeConnections.get(target);
if (connection != null) {
if (!asOwner) {
return connection;
}
if (connection.isAuthenticatedAsOwner()) {
return connection;
}
}
return null;
}
private void checkAllowed(Address target, boolean asOwner, boolean acquiresResources) throws IOException {
if (!alive) {
throw new HazelcastClientNotActiveException("ConnectionManager is not active!");
}
if (asOwner) {
connectionStrategy.beforeConnectToCluster(target);
//opening an owner connection is always allowed
return;
}
try {
connectionStrategy.beforeGetConnection(target);
} catch (HazelcastClientOfflineException e) {
if (allowInvokeWhenDisconnected && !acquiresResources) {
//invocations that does not acquire resources are allowed to invoke when disconnected
return;
}
throw e;
}
if (getOwnerConnection() == null) {
if (allowInvokeWhenDisconnected && !acquiresResources) {
//invocations that does not acquire resources are allowed to invoke when disconnected
return;
}
throw new IOException("Owner connection is not available!");
}
}
@Override
public Address getOwnerConnectionAddress() {
return clusterConnector.getOwnerConnectionAddress();
}
Connection getOrConnect(Address address, boolean asOwner) {
try {
while (true) {
ClientConnection connection = (ClientConnection) getConnection(address, asOwner, true);
if (connection != null) {
return connection;
}
AuthenticationFuture future = triggerConnect(address, asOwner);
connection = (ClientConnection) future.get();
if (!asOwner) {
return connection;
}
if (connection.isAuthenticatedAsOwner()) {
return connection;
}
}
} catch (Throwable e) {
throw rethrow(e);
}
}
private AuthenticationFuture triggerConnect(Address target, boolean asOwner) {
if (!asOwner) {
connectionStrategy.beforeOpenConnection(target);
}
AuthenticationFuture future = new AuthenticationFuture();
AuthenticationFuture oldFuture = connectionsInProgress.putIfAbsent(target, future);
if (oldFuture == null) {
executionService.execute(new InitConnectionTask(target, asOwner, future));
return future;
}
return oldFuture;
}
@Override
public ClientConnection getOwnerConnection() {
Address ownerConnectionAddress = clusterConnector.getOwnerConnectionAddress();
if (ownerConnectionAddress == null) {
return null;
}
return (ClientConnection) getActiveConnection(ownerConnectionAddress);
}
private void fireConnectionAddedEvent(ClientConnection connection) {
for (ConnectionListener connectionListener : connectionListeners) {
connectionListener.connectionAdded(connection);
}
connectionStrategy.onConnect(connection);
}
private void fireConnectionRemovedEvent(ClientConnection connection) {
if (connection.isAuthenticatedAsOwner()) {
clusterConnector.disconnectFromCluster(connection);
}
for (ConnectionListener listener : connectionListeners) {
listener.connectionRemoved(connection);
}
connectionStrategy.onDisconnect(connection);
}
private boolean useAnyOutboundPort() {
return outboundPortCount == 0;
}
private int acquireOutboundPort() {
if (outboundPortCount == 0) {
return 0;
}
synchronized (outboundPorts) {
final Integer port = outboundPorts.removeFirst();
outboundPorts.addLast(port);
return port;
}
}
private void bindSocketToPort(Socket socket) throws IOException {
if (useAnyOutboundPort()) {
SocketAddress socketAddress = new InetSocketAddress(0);
socket.bind(socketAddress);
} else {
int retryCount = outboundPortCount * 2;
IOException ex = null;
for (int i = 0; i < retryCount; i++) {
int port = acquireOutboundPort();
if (port == 0) {
// fast-path for ephemeral range - no need to bind
return;
}
SocketAddress socketAddress = new InetSocketAddress(port);
try {
socket.bind(socketAddress);
return;
} catch (IOException e) {
ex = e;
logger.finest("Could not bind port[ " + port + "]: " + e.getMessage());
}
}
throw ex;
}
}
protected ClientConnection createSocketConnection(final Address remoteAddress) throws IOException {
SocketChannel socketChannel = null;
try {
socketChannel = SocketChannel.open();
Socket socket = socketChannel.socket();
bindSocketToPort(socket);
Channel channel = networking.register(socketChannel, true);
channel.connect(remoteAddress.getInetSocketAddress(), connectionTimeoutMillis);
ClientConnection connection
= new ClientConnection(client, connectionIdGen.incrementAndGet(), channel);
socketChannel.configureBlocking(true);
if (socketInterceptor != null) {
socketInterceptor.onConnect(socket);
}
channel.start();
return connection;
} catch (Exception e) {
closeResource(socketChannel);
throw rethrow(e, IOException.class);
}
}
void onClose(Connection connection) {
removeFromActiveConnections(connection);
}
private void removeFromActiveConnections(Connection connection) {
Address endpoint = connection.getEndPoint();
if (endpoint == null) {
if (logger.isFinestEnabled()) {
logger.finest("Destroying " + connection + ", but it has end-point set to null "
+ "-> not removing it from a connection map");
}
return;
}
if (activeConnections.remove(endpoint, connection)) {
logger.info("Removed connection to endpoint: " + endpoint + ", connection: " + connection);
fireConnectionRemovedEvent((ClientConnection) connection);
} else {
if (logger.isFinestEnabled()) {
logger.finest("Destroying a connection, but there is no mapping " + endpoint + " -> " + connection
+ " in the connection map.");
}
}
}
@Override
public void addConnectionListener(ConnectionListener connectionListener) {
connectionListeners.add(connectionListener);
}
private void authenticate(final Address target, final ClientConnection connection, final boolean asOwner,
final AuthenticationFuture future) {
final ClientPrincipal principal = getPrincipal();
ClientMessage clientMessage = encodeAuthenticationRequest(asOwner, client.getSerializationService(), principal);
ClientInvocation clientInvocation = new ClientInvocation(client, clientMessage, null, connection);
ClientInvocationFuture invocationFuture = clientInvocation.invokeUrgent();
ScheduledFuture timeoutTaskFuture = executionService.schedule(
new TimeoutAuthenticationTask(invocationFuture), authenticationTimeout, MILLISECONDS);
invocationFuture.andThen(new AuthCallback(connection, asOwner, target, future, timeoutTaskFuture));
}
private ClientMessage encodeAuthenticationRequest(boolean asOwner, SerializationService ss, ClientPrincipal principal) {
byte serializationVersion = ((InternalSerializationService) ss).getVersion();
String uuid = null;
String ownerUuid = null;
if (principal != null) {
uuid = principal.getUuid();
ownerUuid = principal.getOwnerUuid();
}
ClientMessage clientMessage;
Credentials credentials = credentialsFactory.newCredentials();
lastCredentials = credentials;
if (credentials.getClass().equals(UsernamePasswordCredentials.class)) {
UsernamePasswordCredentials cr = (UsernamePasswordCredentials) credentials;
clientMessage = ClientAuthenticationCodec
.encodeRequest(cr.getUsername(), cr.getPassword(), uuid, ownerUuid, asOwner, ClientTypes.JAVA,
serializationVersion, BuildInfoProvider.getBuildInfo().getVersion());
} else {
Data data = ss.toData(credentials);
clientMessage = ClientAuthenticationCustomCodec.encodeRequest(data, uuid, ownerUuid,
asOwner, ClientTypes.JAVA, serializationVersion, BuildInfoProvider.getBuildInfo().getVersion());
}
return clientMessage;
}
private void onAuthenticated(Address target, ClientConnection connection) {
ClientConnection oldConnection = activeConnections.put(connection.getEndPoint(), connection);
if (oldConnection == null) {
if (logger.isFinestEnabled()) {
logger.finest("Authentication succeeded for " + connection
+ " and there was no old connection to this end-point");
}
fireConnectionAddedEvent(connection);
} else {
if (logger.isFinestEnabled()) {
logger.finest("Re-authentication succeeded for " + connection);
}
assert connection.equals(oldConnection);
}
connectionsInProgress.remove(target);
logger.info("Authenticated with server " + connection.getEndPoint() + ", server version:" + connection
.getConnectedServerVersionString() + " Local address: " + connection.getLocalSocketAddress());
/* check if connection is closed by remote before authentication complete, if that is the case
we need to remove it back from active connections.
Race description from https://github.com/hazelcast/hazelcast/pull/8832.(A little bit changed)
- open a connection client -> member
- send auth message
- receive auth reply -> reply processing is offloaded to an executor. Did not start to run yet.
- member closes the connection -> the connection is trying to removed from map
but it was not there to begin with
- the executor start processing the auth reply -> it put the connection to the connection map.
- we end up with a closed connection in activeConnections map */
if (!connection.isAlive()) {
removeFromActiveConnections(connection);
}
}
private void onAuthenticationFailed(Address target, ClientConnection connection, Throwable cause) {
if (logger.isFinestEnabled()) {
logger.finest("Authentication of " + connection + " failed.", cause);
}
connection.close(null, cause);
connectionsInProgress.remove(target);
}
public Credentials getLastCredentials() {
return lastCredentials;
}
Collection<Address> getPossibleMemberAddresses() {
return clusterConnector.getPossibleMemberAddresses();
}
private class TimeoutAuthenticationTask implements Runnable {
private final ClientInvocationFuture future;
TimeoutAuthenticationTask(ClientInvocationFuture future) {
this.future = future;
}
@Override
public void run() {
if (future.isDone()) {
return;
}
future.complete(new TimeoutException("Authentication response did not come back in "
+ authenticationTimeout + " millis"));
}
}
private class InitConnectionTask implements Runnable {
private final Address target;
private final boolean asOwner;
private final AuthenticationFuture future;
InitConnectionTask(Address target, boolean asOwner, AuthenticationFuture future) {
this.target = target;
this.asOwner = asOwner;
this.future = future;
}
@Override
public void run() {
ClientConnection connection;
try {
connection = getConnection(target);
} catch (Exception e) {
logger.finest(e);
future.onFailure(e);
connectionsInProgress.remove(target);
return;
}
try {
authenticate(target, connection, asOwner, future);
} catch (Exception e) {
future.onFailure(e);
connection.close("Failed to authenticate connection", e);
connectionsInProgress.remove(target);
}
}
private ClientConnection getConnection(Address target) throws IOException {
ClientConnection connection = activeConnections.get(target);
if (connection != null) {
return connection;
}
Address address = addressTranslator.translate(target);
if (address == null) {
throw new NullPointerException("Address Translator " + addressTranslator.getClass()
+ " could not translate address " + target);
}
return createSocketConnection(address);
}
}
private class ClientConnectionChannelErrorHandler implements ChannelErrorHandler {
@Override
public void onError(Channel channel, Throwable cause) {
if (channel == null) {
logger.severe(cause);
} else {
if (cause instanceof OutOfMemoryError) {
logger.severe(cause);
}
ClientConnection connection = (ClientConnection) channel.attributeMap().get(ClientConnection.class);
if (cause instanceof EOFException) {
connection.close("Connection closed by the other side", cause);
} else {
connection.close("Exception in " + connection + ", thread=" + Thread.currentThread().getName(), cause);
}
}
}
}
@Override
public void connectToCluster() {
clusterConnector.connectToCluster();
}
@Override
public Future<Void> connectToClusterAsync() {
return clusterConnector.connectToClusterAsync();
}
private class AuthCallback implements ExecutionCallback<ClientMessage> {
private final ClientConnection connection;
private final boolean asOwner;
private final Address target;
private final AuthenticationFuture future;
private final ScheduledFuture timeoutTaskFuture;
AuthCallback(ClientConnection connection, boolean asOwner, Address target,
AuthenticationFuture future, ScheduledFuture timeoutTaskFuture) {
this.connection = connection;
this.asOwner = asOwner;
this.target = target;
this.future = future;
this.timeoutTaskFuture = timeoutTaskFuture;
}
@Override
public void onResponse(ClientMessage response) {
timeoutTaskFuture.cancel(true);
ClientAuthenticationCodec.ResponseParameters result;
try {
result = ClientAuthenticationCodec.decodeResponse(response);
} catch (HazelcastException e) {
onFailure(e);
return;
}
AuthenticationStatus authenticationStatus = AuthenticationStatus.getById(result.status);
switch (authenticationStatus) {
case AUTHENTICATED:
connection.setConnectedServerVersion(result.serverHazelcastVersion);
connection.setRemoteEndpoint(result.address);
if (asOwner) {
connection.setIsAuthenticatedAsOwner();
ClientPrincipal principal = new ClientPrincipal(result.uuid, result.ownerUuid);
setPrincipal(principal);
//setting owner connection is moved to here(before onAuthenticated/before connected event)
//so that invocations that requires owner connection on this connection go through
clusterConnector.setOwnerConnectionAddress(connection.getEndPoint());
logger.info("Setting " + connection + " as owner with principal " + principal);
}
onAuthenticated(target, connection);
future.onSuccess(connection);
break;
case CREDENTIALS_FAILED:
onFailure(new AuthenticationException("Invalid credentials! Principal: " + principal));
break;
default:
onFailure(new AuthenticationException("Authentication status code not supported. status: "
+ authenticationStatus));
}
}
@Override
public void onFailure(Throwable t) {
timeoutTaskFuture.cancel(true);
onAuthenticationFailed(target, connection, t);
future.onFailure(t);
}
}
}