forked from hazelcast/hazelcast
/
Node.java
827 lines (722 loc) · 32.8 KB
/
Node.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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
* 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.instance;
import com.hazelcast.client.impl.ClientEngineImpl;
import com.hazelcast.cluster.ClusterState;
import com.hazelcast.cluster.Joiner;
import com.hazelcast.cluster.impl.TcpIpJoiner;
import com.hazelcast.config.Config;
import com.hazelcast.config.DiscoveryConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.ListenerConfig;
import com.hazelcast.config.MemberAttributeConfig;
import com.hazelcast.config.UserCodeDeploymentConfig;
import com.hazelcast.core.ClientListener;
import com.hazelcast.core.DistributedObjectListener;
import com.hazelcast.core.HazelcastInstanceAware;
import com.hazelcast.core.LifecycleEvent.LifecycleState;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.core.Member;
import com.hazelcast.core.MembershipListener;
import com.hazelcast.core.MigrationListener;
import com.hazelcast.crdt.CRDTReplicationMigrationService;
import com.hazelcast.internal.ascii.TextCommandService;
import com.hazelcast.internal.ascii.TextCommandServiceImpl;
import com.hazelcast.internal.cluster.impl.ClusterJoinManager;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.internal.cluster.impl.ConfigCheck;
import com.hazelcast.internal.cluster.impl.DiscoveryJoiner;
import com.hazelcast.internal.cluster.impl.JoinRequest;
import com.hazelcast.internal.cluster.impl.MulticastJoiner;
import com.hazelcast.internal.cluster.impl.MulticastService;
import com.hazelcast.internal.cluster.impl.SplitBrainJoinMessage;
import com.hazelcast.internal.diagnostics.HealthMonitor;
import com.hazelcast.internal.dynamicconfig.DynamicConfigurationAwareConfig;
import com.hazelcast.internal.management.ManagementCenterService;
import com.hazelcast.internal.metrics.MetricsRegistry;
import com.hazelcast.internal.partition.InternalPartitionService;
import com.hazelcast.internal.partition.impl.InternalMigrationListener;
import com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.internal.usercodedeployment.UserCodeDeploymentClassLoader;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.LoggingServiceImpl;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.ClassLoaderUtil;
import com.hazelcast.nio.ConnectionManager;
import com.hazelcast.nio.Packet;
import com.hazelcast.partition.PartitionLostListener;
import com.hazelcast.security.Credentials;
import com.hazelcast.security.SecurityContext;
import com.hazelcast.security.SecurityService;
import com.hazelcast.spi.annotation.PrivateApi;
import com.hazelcast.spi.discovery.SimpleDiscoveryNode;
import com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider;
import com.hazelcast.spi.discovery.integration.DiscoveryMode;
import com.hazelcast.spi.discovery.integration.DiscoveryService;
import com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider;
import com.hazelcast.spi.discovery.integration.DiscoveryServiceSettings;
import com.hazelcast.spi.impl.NodeEngineImpl;
import com.hazelcast.spi.impl.proxyservice.impl.ProxyServiceImpl;
import com.hazelcast.spi.properties.HazelcastProperties;
import com.hazelcast.util.Clock;
import com.hazelcast.version.MemberVersion;
import com.hazelcast.version.Version;
import java.lang.reflect.Constructor;
import java.nio.channels.ServerSocketChannel;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.hazelcast.cluster.memberselector.MemberSelectors.DATA_MEMBER_SELECTOR;
import static com.hazelcast.instance.MemberImpl.NA_MEMBER_LIST_JOIN_VERSION;
import static com.hazelcast.instance.NodeShutdownHelper.shutdownNodeByFiringEvents;
import static com.hazelcast.internal.cluster.impl.MulticastService.createMulticastService;
import static com.hazelcast.nio.IOUtil.closeResource;
import static com.hazelcast.spi.properties.GroupProperty.DISCOVERY_SPI_ENABLED;
import static com.hazelcast.spi.properties.GroupProperty.DISCOVERY_SPI_PUBLIC_IP_ENABLED;
import static com.hazelcast.spi.properties.GroupProperty.GRACEFUL_SHUTDOWN_MAX_WAIT;
import static com.hazelcast.spi.properties.GroupProperty.LOGGING_TYPE;
import static com.hazelcast.spi.properties.GroupProperty.MAX_JOIN_SECONDS;
import static com.hazelcast.spi.properties.GroupProperty.SHUTDOWNHOOK_ENABLED;
import static com.hazelcast.spi.properties.GroupProperty.SHUTDOWNHOOK_POLICY;
import static com.hazelcast.util.EmptyStatement.ignore;
import static com.hazelcast.util.ExceptionUtil.rethrow;
import static com.hazelcast.util.StringUtil.isNullOrEmpty;
import static com.hazelcast.util.ThreadUtil.createThreadName;
import static java.lang.Thread.currentThread;
import static java.security.AccessController.doPrivileged;
@SuppressWarnings({"checkstyle:methodcount", "checkstyle:visibilitymodifier", "checkstyle:classdataabstractioncoupling",
"checkstyle:classfanoutcomplexity"})
@PrivateApi
public class Node {
private static final int THREAD_SLEEP_DURATION_MS = 500;
public final HazelcastInstanceImpl hazelcastInstance;
public final DynamicConfigurationAwareConfig config;
public final NodeEngineImpl nodeEngine;
public final ClientEngineImpl clientEngine;
public final InternalPartitionServiceImpl partitionService;
public final ClusterServiceImpl clusterService;
public final MulticastService multicastService;
public final DiscoveryService discoveryService;
public final TextCommandServiceImpl textCommandService;
public final LoggingServiceImpl loggingService;
public final ConnectionManager connectionManager;
public final Address address;
public final SecurityContext securityContext;
private final ILogger logger;
private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
private final NodeShutdownHookThread shutdownHookThread;
private final InternalSerializationService serializationService;
private final ClassLoader configClassLoader;
private final NodeExtension nodeExtension;
private final HazelcastProperties properties;
private final BuildInfo buildInfo;
private final HealthMonitor healthMonitor;
private final Joiner joiner;
private ManagementCenterService managementCenterService;
private volatile NodeState state;
/**
* Codebase version of Hazelcast being executed at this Node, as resolved by {@link BuildInfoProvider}.
* For example, when running on hazelcast-3.8.jar, this would resolve to {@code Version.of(3,8,0)}.
* A node's codebase version may be different than cluster version.
*/
private final MemberVersion version;
@SuppressWarnings({"checkstyle:executablestatementcount", "checkstyle:methodlength"})
public Node(HazelcastInstanceImpl hazelcastInstance, Config staticConfig, NodeContext nodeContext) {
DynamicConfigurationAwareConfig config = new DynamicConfigurationAwareConfig(staticConfig);
this.hazelcastInstance = hazelcastInstance;
this.config = config;
this.configClassLoader = getConfigClassloader(config);
this.properties = new HazelcastProperties(config);
String policy = properties.getString(SHUTDOWNHOOK_POLICY);
this.shutdownHookThread = new NodeShutdownHookThread("hz.ShutdownThread", policy);
// Calling getBuildInfo() instead of directly using BuildInfoProvider.BUILD_INFO.
// Version can be overridden via system property. That's why BuildInfo should be parsed for each Node.
this.buildInfo = BuildInfoProvider.getBuildInfo();
this.version = MemberVersion.of(buildInfo.getVersion());
String loggingType = properties.getString(LOGGING_TYPE);
loggingService = new LoggingServiceImpl(config.getGroupConfig().getName(), loggingType, buildInfo);
final AddressPicker addressPicker = nodeContext.createAddressPicker(this);
try {
addressPicker.pickAddress();
} catch (Throwable e) {
throw rethrow(e);
}
final ServerSocketChannel serverSocketChannel = addressPicker.getServerSocketChannel();
try {
boolean liteMember = config.isLiteMember();
address = addressPicker.getPublicAddress();
nodeExtension = nodeContext.createNodeExtension(this);
final Map<String, Object> memberAttributes = findMemberAttributes(config.getMemberAttributeConfig().asReadOnly());
MemberImpl localMember = new MemberImpl(address, version, true, nodeExtension.createMemberUuid(address),
memberAttributes, liteMember, NA_MEMBER_LIST_JOIN_VERSION, hazelcastInstance);
loggingService.setThisMember(localMember);
logger = loggingService.getLogger(Node.class.getName());
nodeExtension.printNodeInfo();
logGroupPasswordInfo();
nodeExtension.beforeStart();
serializationService = nodeExtension.createSerializationService();
securityContext = config.getSecurityConfig().isEnabled() ? nodeExtension.getSecurityContext() : null;
nodeEngine = new NodeEngineImpl(this);
config.setConfigurationService(nodeEngine.getConfigurationService());
config.onSecurityServiceUpdated(getSecurityService());
MetricsRegistry metricsRegistry = nodeEngine.getMetricsRegistry();
metricsRegistry.collectMetrics(nodeExtension);
healthMonitor = new HealthMonitor(this);
clientEngine = new ClientEngineImpl(this);
connectionManager = nodeContext.createConnectionManager(this, serverSocketChannel);
discoveryService = createDiscoveryService(
this.config.getNetworkConfig().getJoin().getDiscoveryConfig().getAsReadOnly(), localMember);
partitionService = new InternalPartitionServiceImpl(this);
clusterService = new ClusterServiceImpl(this, localMember);
textCommandService = new TextCommandServiceImpl(this);
multicastService = createMulticastService(addressPicker.getBindAddress(), this, config, logger);
joiner = nodeContext.createJoiner(this);
config.setClusterService(clusterService);
} catch (Throwable e) {
closeResource(serverSocketChannel);
try {
shutdownServices(true);
} catch (Throwable ignored) {
ignore(ignored);
}
throw rethrow(e);
}
}
private ClassLoader getConfigClassloader(Config config) {
UserCodeDeploymentConfig userCodeDeploymentConfig = config.getUserCodeDeploymentConfig();
ClassLoader classLoader;
if (userCodeDeploymentConfig.isEnabled()) {
ClassLoader parent = config.getClassLoader();
final ClassLoader theParent = parent == null ? Node.class.getClassLoader() : parent;
classLoader = doPrivileged(new PrivilegedAction<UserCodeDeploymentClassLoader>() {
@Override
public UserCodeDeploymentClassLoader run() {
return new UserCodeDeploymentClassLoader(theParent);
}
});
} else {
classLoader = config.getClassLoader();
}
return classLoader;
}
public DiscoveryService createDiscoveryService(DiscoveryConfig discoveryConfig, Member localMember) {
DiscoveryServiceProvider factory = discoveryConfig.getDiscoveryServiceProvider();
if (factory == null) {
factory = new DefaultDiscoveryServiceProvider();
}
ILogger logger = getLogger(DiscoveryService.class);
DiscoveryServiceSettings settings = new DiscoveryServiceSettings()
.setConfigClassLoader(configClassLoader)
.setLogger(logger)
.setDiscoveryMode(DiscoveryMode.Member)
.setDiscoveryConfig(discoveryConfig).setDiscoveryNode(
new SimpleDiscoveryNode(localMember.getAddress(), localMember.getAttributes()));
return factory.newDiscoveryService(settings);
}
@SuppressWarnings({"checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity"})
private void initializeListeners(Config config) {
for (final ListenerConfig listenerCfg : config.getListenerConfigs()) {
Object listener = listenerCfg.getImplementation();
if (listener == null) {
try {
listener = ClassLoaderUtil.newInstance(configClassLoader, listenerCfg.getClassName());
} catch (Exception e) {
logger.severe(e);
}
}
if (listener instanceof HazelcastInstanceAware) {
((HazelcastInstanceAware) listener).setHazelcastInstance(hazelcastInstance);
}
boolean known = false;
if (listener instanceof DistributedObjectListener) {
final ProxyServiceImpl proxyService = (ProxyServiceImpl) nodeEngine.getProxyService();
proxyService.addProxyListener((DistributedObjectListener) listener);
known = true;
}
if (listener instanceof MembershipListener) {
clusterService.addMembershipListener((MembershipListener) listener);
known = true;
}
if (listener instanceof MigrationListener) {
partitionService.addMigrationListener((MigrationListener) listener);
known = true;
}
if (listener instanceof PartitionLostListener) {
partitionService.addPartitionLostListener((PartitionLostListener) listener);
known = true;
}
if (listener instanceof LifecycleListener) {
hazelcastInstance.lifecycleService.addLifecycleListener((LifecycleListener) listener);
known = true;
}
if (listener instanceof ClientListener) {
String serviceName = ClientEngineImpl.SERVICE_NAME;
nodeEngine.getEventService().registerLocalListener(serviceName, serviceName, listener);
known = true;
}
if (listener instanceof InternalMigrationListener) {
final InternalPartitionServiceImpl partitionService =
(InternalPartitionServiceImpl) nodeEngine.getPartitionService();
partitionService.setInternalMigrationListener((InternalMigrationListener) listener);
known = true;
}
if (nodeExtension.registerListener(listener)) {
known = true;
}
if (listener != null && !known) {
final String error = "Unknown listener type: " + listener.getClass();
Throwable t = new IllegalArgumentException(error);
logger.warning(error, t);
}
}
}
public ManagementCenterService getManagementCenterService() {
return managementCenterService;
}
public InternalSerializationService getSerializationService() {
return serializationService;
}
public ClusterServiceImpl getClusterService() {
return clusterService;
}
public InternalPartitionService getPartitionService() {
return partitionService;
}
public Address getMasterAddress() {
return clusterService.getMasterAddress();
}
public Address getThisAddress() {
return address;
}
public MemberImpl getLocalMember() {
return clusterService.getLocalMember();
}
public boolean isMaster() {
return clusterService.isMaster();
}
public SecurityService getSecurityService() {
return nodeExtension.getSecurityService();
}
void start() {
nodeEngine.start();
initializeListeners(config);
hazelcastInstance.lifecycleService.fireLifecycleEvent(LifecycleState.STARTING);
clusterService.sendLocalMembershipEvent();
connectionManager.start();
if (config.getNetworkConfig().getJoin().getMulticastConfig().isEnabled()) {
final Thread multicastServiceThread = new Thread(multicastService,
createThreadName(hazelcastInstance.getName(), "MulticastThread"));
multicastServiceThread.start();
}
if (properties.getBoolean(DISCOVERY_SPI_ENABLED)) {
discoveryService.start();
// Discover local metadata from environment and merge into member attributes
mergeEnvironmentProvidedMemberMetadata();
}
if (properties.getBoolean(SHUTDOWNHOOK_ENABLED)) {
logger.finest("Adding ShutdownHook");
Runtime.getRuntime().addShutdownHook(shutdownHookThread);
}
state = NodeState.ACTIVE;
nodeExtension.beforeJoin();
join();
int clusterSize = clusterService.getSize();
if (config.getNetworkConfig().isPortAutoIncrement()
&& address.getPort() >= config.getNetworkConfig().getPort() + clusterSize) {
logger.warning("Config seed port is " + config.getNetworkConfig().getPort()
+ " and cluster size is " + clusterSize + ". Some of the ports seem occupied!");
}
try {
managementCenterService = new ManagementCenterService(hazelcastInstance);
} catch (Exception e) {
logger.warning("ManagementCenterService could not be constructed!", e);
}
nodeExtension.afterStart();
nodeExtension.sendPhoneHome();
healthMonitor.start();
}
@SuppressWarnings("checkstyle:npathcomplexity")
public void shutdown(final boolean terminate) {
long start = Clock.currentTimeMillis();
if (logger.isFinestEnabled()) {
logger.finest("We are being asked to shutdown when state = " + state);
}
if (!setShuttingDown()) {
waitIfAlreadyShuttingDown();
return;
}
if (!terminate) {
replicateCRDTs();
final int maxWaitSeconds = properties.getSeconds(GRACEFUL_SHUTDOWN_MAX_WAIT);
if (!partitionService.prepareToSafeShutdown(maxWaitSeconds, TimeUnit.SECONDS)) {
logger.warning("Graceful shutdown could not be completed in " + maxWaitSeconds + " seconds!");
}
} else {
logger.warning("Terminating forcefully...");
}
// set the joined=false first so that
// threads do not process unnecessary
// events, such as remove address
clusterService.resetJoinState();
try {
if (properties.getBoolean(SHUTDOWNHOOK_ENABLED)) {
Runtime.getRuntime().removeShutdownHook(shutdownHookThread);
}
} catch (Throwable ignored) {
ignore(ignored);
}
try {
discoveryService.destroy();
} catch (Throwable ignored) {
ignore(ignored);
}
try {
shutdownServices(terminate);
state = NodeState.SHUT_DOWN;
logger.info("Hazelcast Shutdown is completed in " + (Clock.currentTimeMillis() - start) + " ms.");
} finally {
if (state != NodeState.SHUT_DOWN) {
shuttingDown.compareAndSet(true, false);
}
}
}
/**
* Synchronously replicate the unreplicated CRDT states to a data member
* in the cluster.
*/
private void replicateCRDTs() {
final CRDTReplicationMigrationService replicationMigrationService
= nodeEngine.getService(CRDTReplicationMigrationService.SERVICE_NAME);
replicationMigrationService.syncReplicateDirtyCRDTs();
}
@SuppressWarnings("checkstyle:npathcomplexity")
private void shutdownServices(boolean terminate) {
if (nodeExtension != null) {
nodeExtension.beforeShutdown();
}
if (managementCenterService != null) {
managementCenterService.shutdown();
}
if (textCommandService != null) {
textCommandService.stop();
}
if (multicastService != null) {
logger.info("Shutting down multicast service...");
multicastService.stop();
}
if (connectionManager != null) {
logger.info("Shutting down connection manager...");
connectionManager.shutdown();
}
if (nodeEngine != null) {
logger.info("Shutting down node engine...");
nodeEngine.shutdown(terminate);
}
if (securityContext != null) {
securityContext.destroy();
}
if (serializationService != null) {
logger.finest("Destroying serialization service...");
serializationService.dispose();
}
if (nodeExtension != null) {
nodeExtension.shutdown();
}
if (healthMonitor != null) {
healthMonitor.stop();
}
}
private void mergeEnvironmentProvidedMemberMetadata() {
MemberImpl localMember = getLocalMember();
Map<String, Object> metadata = discoveryService.discoverLocalMetadata();
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
Object value = entry.getValue();
if (value instanceof Byte) {
localMember.setByteAttribute(entry.getKey(), (Byte) value);
} else if (value instanceof Short) {
localMember.setShortAttribute(entry.getKey(), (Short) value);
} else if (value instanceof Integer) {
localMember.setIntAttribute(entry.getKey(), (Integer) value);
} else if (value instanceof Long) {
localMember.setLongAttribute(entry.getKey(), (Long) value);
} else if (value instanceof Float) {
localMember.setFloatAttribute(entry.getKey(), (Float) value);
} else if (value instanceof Double) {
localMember.setDoubleAttribute(entry.getKey(), (Double) value);
} else if (value instanceof Boolean) {
localMember.setBooleanAttribute(entry.getKey(), (Boolean) value);
} else {
localMember.setStringAttribute(entry.getKey(), value.toString());
}
}
}
public boolean setShuttingDown() {
if (shuttingDown.compareAndSet(false, true)) {
state = NodeState.PASSIVE;
return true;
}
return false;
}
/**
* Indicates that node is not shutting down or it has not already shut down
*
* @return true if node is not shutting down or it has not already shut down
*/
public boolean isRunning() {
return !shuttingDown.get();
}
private void waitIfAlreadyShuttingDown() {
if (!shuttingDown.get()) {
return;
}
logger.info("Node is already shutting down... Waiting for shutdown process to complete...");
while (state != NodeState.SHUT_DOWN && shuttingDown.get()) {
try {
Thread.sleep(THREAD_SLEEP_DURATION_MS);
} catch (InterruptedException e) {
currentThread().interrupt();
logger.warning("Interrupted while waiting for shutdown!");
return;
}
}
if (state != NodeState.SHUT_DOWN) {
throw new IllegalStateException("Node failed to shutdown!");
}
}
public void changeNodeStateToActive() {
final ClusterState clusterState = clusterService.getClusterState();
if (clusterState == ClusterState.PASSIVE) {
throw new IllegalStateException("This method can be called only when cluster-state is not " + clusterState);
}
state = NodeState.ACTIVE;
}
public void changeNodeStateToPassive() {
final ClusterState clusterState = clusterService.getClusterState();
if (clusterState != ClusterState.PASSIVE) {
throw new IllegalStateException("This method can be called only when cluster-state is " + clusterState);
}
state = NodeState.PASSIVE;
}
/**
* Resets the internal cluster-state of the Node to be able to make it ready to join a new cluster.
* After this method is called,
* a new join process can be triggered by calling {@link #join()}.
* <p/>
* This method is called during merge process after a split-brain is detected.
*/
public void reset() {
state = NodeState.ACTIVE;
clusterService.resetJoinState();
joiner.reset();
}
public ILogger getLogger(String name) {
return loggingService.getLogger(name);
}
public ILogger getLogger(Class clazz) {
return loggingService.getLogger(clazz);
}
public HazelcastProperties getProperties() {
return properties;
}
public TextCommandService getTextCommandService() {
return textCommandService;
}
public ConnectionManager getConnectionManager() {
return connectionManager;
}
public ClassLoader getConfigClassLoader() {
return configClassLoader;
}
public NodeEngineImpl getNodeEngine() {
return nodeEngine;
}
public NodeExtension getNodeExtension() {
return nodeExtension;
}
public DiscoveryService getDiscoveryService() {
return discoveryService;
}
private enum ShutdownHookPolicy {
TERMINATE,
GRACEFUL
}
public class NodeShutdownHookThread extends Thread {
private final ShutdownHookPolicy policy;
NodeShutdownHookThread(String name, String policy) {
super(name);
this.policy = ShutdownHookPolicy.valueOf(policy);
}
@Override
public void run() {
try {
if (isRunning()) {
logger.info("Running shutdown hook... Current state: " + state);
switch (policy) {
case TERMINATE:
hazelcastInstance.getLifecycleService().terminate();
break;
case GRACEFUL:
hazelcastInstance.getLifecycleService().shutdown();
break;
default:
throw new IllegalArgumentException("Unimplemented shutdown hook policy: " + policy);
}
}
} catch (Exception e) {
logger.warning(e);
}
}
}
public SplitBrainJoinMessage createSplitBrainJoinMessage() {
MemberImpl localMember = getLocalMember();
boolean liteMember = localMember.isLiteMember();
Collection<Address> memberAddresses = clusterService.getMemberAddresses();
int dataMemberCount = clusterService.getSize(DATA_MEMBER_SELECTOR);
Version clusterVersion = clusterService.getClusterVersion();
int memberListVersion = clusterService.getMembershipManager().getMemberListVersion();
return new SplitBrainJoinMessage(Packet.VERSION, buildInfo.getBuildNumber(), version, address, localMember.getUuid(),
liteMember, createConfigCheck(), memberAddresses, dataMemberCount, clusterVersion, memberListVersion);
}
public JoinRequest createJoinRequest(boolean withCredentials) {
final Credentials credentials = (withCredentials && securityContext != null)
? securityContext.getCredentialsFactory().newCredentials() : null;
final Set<String> excludedMemberUuids = nodeExtension.getInternalHotRestartService().getExcludedMemberUuids();
MemberImpl localMember = getLocalMember();
return new JoinRequest(Packet.VERSION, buildInfo.getBuildNumber(), version, address,
localMember.getUuid(), localMember.isLiteMember(), createConfigCheck(), credentials,
localMember.getAttributes(), excludedMemberUuids);
}
public ConfigCheck createConfigCheck() {
String joinerType = joiner == null ? "" : joiner.getType();
return new ConfigCheck(config, joinerType);
}
public void join() {
if (clusterService.isJoined()) {
if (logger.isFinestEnabled()) {
logger.finest("Calling join on already joined node. ", new Exception("stacktrace"));
} else {
logger.warning("Calling join on already joined node. ");
}
return;
}
if (joiner == null) {
logger.warning("No join method is enabled! Starting standalone.");
ClusterJoinManager clusterJoinManager = clusterService.getClusterJoinManager();
clusterJoinManager.setThisMemberAsMaster();
return;
}
try {
clusterService.resetJoinState();
joiner.join();
} catch (Throwable e) {
logger.severe("Error while joining the cluster!", e);
}
if (!clusterService.isJoined()) {
long maxJoinTimeMillis = properties.getMillis(MAX_JOIN_SECONDS);
logger.severe("Could not join cluster in " + maxJoinTimeMillis + " ms. Shutting down now!");
shutdownNodeByFiringEvents(Node.this, true);
}
}
public Joiner getJoiner() {
return joiner;
}
Joiner createJoiner() {
JoinConfig join = config.getNetworkConfig().getJoin();
join.verify();
if (properties.getBoolean(DISCOVERY_SPI_ENABLED)) {
//TODO: Auto-Upgrade Multicast+AWS configuration!
logger.info("Activating Discovery SPI Joiner");
return new DiscoveryJoiner(this, discoveryService, properties.getBoolean(DISCOVERY_SPI_PUBLIC_IP_ENABLED));
} else {
if (join.getMulticastConfig().isEnabled() && multicastService != null) {
logger.info("Creating MulticastJoiner");
return new MulticastJoiner(this);
} else if (join.getTcpIpConfig().isEnabled()) {
logger.info("Creating TcpIpJoiner");
return new TcpIpJoiner(this);
} else if (join.getAwsConfig().isEnabled()) {
Class clazz;
try {
logger.info("Creating AWSJoiner");
clazz = Class.forName("com.hazelcast.cluster.impl.TcpIpJoinerOverAWS");
Constructor constructor = clazz.getConstructor(Node.class);
return (Joiner) constructor.newInstance(this);
} catch (Exception e) {
throw rethrow(e);
}
}
}
return null;
}
public String getThisUuid() {
return clusterService.getThisUuid();
}
public Config getConfig() {
return config;
}
/**
* Returns the node state.
*
* @return current state of the node
*/
public NodeState getState() {
return state;
}
/**
* Returns the codebase version of the node.
*
* @return codebase version of the node
*/
public MemberVersion getVersion() {
return version;
}
public boolean isLiteMember() {
return getLocalMember().isLiteMember();
}
@Override
public String toString() {
return "Node[" + hazelcastInstance.getName() + "]";
}
public BuildInfo getBuildInfo() {
return buildInfo;
}
private Map<String, Object> findMemberAttributes(MemberAttributeConfig attributeConfig) {
Map<String, Object> attributes = new HashMap<String, Object>(attributeConfig.getAttributes());
Properties properties = System.getProperties();
for (String key : properties.stringPropertyNames()) {
if (key.startsWith("hazelcast.member.attribute.")) {
String shortKey = key.substring("hazelcast.member.attribute.".length());
String value = properties.getProperty(key);
attributes.put(shortKey, value);
}
}
return attributes;
}
private void logGroupPasswordInfo() {
if (!isNullOrEmpty(config.getGroupConfig().getPassword())) {
logger.info("A non-empty group password is configured for the Hazelcast member."
+ " Starting with Hazelcast version 3.8.2, members with the same group name,"
+ " but with different group passwords (that do not use authentication) form a cluster."
+ " The group password configuration will be removed completely in a future release.");
}
}
}