-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
DiscoverySpiTest.java
849 lines (698 loc) · 35.6 KB
/
DiscoverySpiTest.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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
* Copyright (c) 2008-2021, 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.spi.discovery;
import com.hazelcast.cluster.Address;
import com.hazelcast.cluster.Member;
import com.hazelcast.cluster.impl.MemberImpl;
import com.hazelcast.config.AwsConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.DiscoveryConfig;
import com.hazelcast.config.DiscoveryStrategyConfig;
import com.hazelcast.config.InterfacesConfig;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.MulticastConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.config.properties.PropertyDefinition;
import com.hazelcast.config.properties.PropertyTypeConverter;
import com.hazelcast.config.properties.SimplePropertyDefinition;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.BuildInfoProvider;
import com.hazelcast.instance.impl.Node;
import com.hazelcast.internal.partition.membergroup.DefaultMemberGroup;
import com.hazelcast.internal.partition.membergroup.MemberGroupFactory;
import com.hazelcast.internal.partition.membergroup.SPIAwareMemberGroupFactory;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.spi.discovery.impl.DefaultDiscoveryService;
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.partitiongroup.MemberGroup;
import com.hazelcast.spi.partitiongroup.PartitionGroupStrategy;
import com.hazelcast.spi.properties.ClusterProperty;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.version.MemberVersion;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import static com.hazelcast.config.PartitionGroupConfig.MemberGroupType.SPI;
import static com.hazelcast.config.properties.PropertyTypeConverter.BOOLEAN;
import static com.hazelcast.config.properties.PropertyTypeConverter.INTEGER;
import static com.hazelcast.config.properties.PropertyTypeConverter.STRING;
import static com.hazelcast.spi.discovery.DiscoverySpiTest.ParametrizedDiscoveryStrategyFactory.BOOL_PROPERTY;
import static com.hazelcast.test.Accessors.getNode;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(HazelcastParallelClassRunner.class)
@Category(QuickTest.class)
public class DiscoverySpiTest extends HazelcastTestSupport {
private static final MemberVersion VERSION = MemberVersion.of(BuildInfoProvider.getBuildInfo().getVersion());
private static final ILogger LOGGER = Logger.getLogger(DiscoverySpiTest.class);
@Test(expected = InvalidConfigurationException.class)
public void whenStrategyClassNameNotExist_thenFailFast() {
Config config = new Config();
config.setProperty(ClusterProperty.DISCOVERY_SPI_ENABLED.getName(), "true");
DiscoveryConfig discoveryConfig = new DiscoveryConfig();
discoveryConfig.addDiscoveryStrategyConfig(new DiscoveryStrategyConfig("non.existing.ClassName"));
config.getNetworkConfig().getJoin().setDiscoveryConfig(discoveryConfig);
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
createHazelcastInstance(config);
}
@Test
public void givenDiscoveryStrategyFactoryExistOnClassPath_whenTheSameFactoryIsConfiguredExplicitly_thenOnlyOneInstanceOfStrategyIsCreated() {
// ParametrizedDiscoveryStrategy has a static counter and throws an exception when its instantiated more than
// than once.
Config config = new Config();
config.setProperty(ClusterProperty.DISCOVERY_SPI_ENABLED.getName(), "true");
JoinConfig join = config.getNetworkConfig().getJoin();
join.getMulticastConfig().setEnabled(false);
DiscoveryConfig discoveryConfig = join.getDiscoveryConfig();
DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(new ParametrizedDiscoveryStrategyFactory());
strategyConfig.addProperty("bool-property", true);
discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
//this will fail when the discovery strategy throws an exception
createHazelcastInstance(config);
}
public static final class ParametrizedDiscoveryStrategy extends AbstractDiscoveryStrategy {
private static final AtomicInteger INSTANCE_COUNTER = new AtomicInteger();
public ParametrizedDiscoveryStrategy(ILogger logger, Map<String, Comparable> properties) {
super(logger, properties);
boolean parameterPassed = getOrDefault(BOOL_PROPERTY, false);
if (!parameterPassed) {
throw new AssertionError("configured parameter was not passed!");
}
if (INSTANCE_COUNTER.getAndIncrement() != 0) {
throw new AssertionError("only 1 instance of a discovery strategy should be created");
}
}
@Override
public Iterable<DiscoveryNode> discoverNodes() {
return null;
}
}
public static final class ParametrizedDiscoveryStrategyFactory implements DiscoveryStrategyFactory {
public static final PropertyDefinition BOOL_PROPERTY = new SimplePropertyDefinition("bool-property", true, BOOLEAN);
@Override
public Class<? extends DiscoveryStrategy> getDiscoveryStrategyType() {
return ParametrizedDiscoveryStrategy.class;
}
@Override
public DiscoveryStrategy newDiscoveryStrategy(DiscoveryNode discoveryNode, ILogger logger, Map<String, Comparable> properties) {
return new ParametrizedDiscoveryStrategy(logger, properties);
}
@Override
public Collection<PropertyDefinition> getConfigurationProperties() {
return Collections.singleton(BOOL_PROPERTY);
}
}
@Test
public void test_metadata_discovery_on_node_startup() throws Exception {
String xmlFileName = "test-hazelcast-discovery-spi-metadata.xml";
InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Config config = new XmlConfigBuilder(xmlResource).build();
TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(1);
try {
HazelcastInstance hazelcastInstance1 = instanceFactory.newHazelcastInstance(config);
assertNotNull(hazelcastInstance1);
Member localMember = hazelcastInstance1.getCluster().getLocalMember();
assertEquals("TEST", localMember.getAttribute("test-string"));
} finally {
instanceFactory.shutdownAll();
}
}
@Test
public void test_metadata_discovery_on_node_startup_overrides_what_is_configured_on_member() throws Exception {
final String overridenAttribute = "test-string";
String xmlFileName = "test-hazelcast-discovery-spi-metadata.xml";
InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Config config = new XmlConfigBuilder(xmlResource).build();
config.getMemberAttributeConfig().setAttribute(overridenAttribute, "config-property");
TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(1);
try {
HazelcastInstance hazelcastInstance1 = instanceFactory.newHazelcastInstance(config);
assertNotNull(hazelcastInstance1);
Member localMember = hazelcastInstance1.getCluster().getLocalMember();
assertEquals("TEST", localMember.getAttribute(overridenAttribute));
} finally {
instanceFactory.shutdownAll();
}
}
@Test
public void testSchema() throws Exception {
String xmlFileName = "test-hazelcast-discovery-spi.xml";
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaResource = DiscoverySpiTest.class.getClassLoader().getResource("hazelcast-config-4.0.xsd");
assertNotNull(schemaResource);
InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Source source = new StreamSource(xmlResource);
Schema schema = factory.newSchema(schemaResource);
Validator validator = schema.newValidator();
validator.validate(source);
}
@Test
public void testParsing() throws Exception {
String xmlFileName = "test-hazelcast-discovery-spi.xml";
InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Config config = new XmlConfigBuilder(xmlResource).build();
JoinConfig joinConfig = config.getNetworkConfig().getJoin();
AwsConfig awsConfig = joinConfig.getAwsConfig();
assertFalse(awsConfig.isEnabled());
TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig();
assertFalse(tcpIpConfig.isEnabled());
MulticastConfig multicastConfig = joinConfig.getMulticastConfig();
assertFalse(multicastConfig.isEnabled());
DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig();
assertTrue(discoveryConfig.isEnabled());
assertEquals(1, discoveryConfig.getDiscoveryStrategyConfigs().size());
DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next();
assertEquals(3, providerConfig.getProperties().size());
assertEquals("foo", providerConfig.getProperties().get("key-string"));
assertEquals("123", providerConfig.getProperties().get("key-int"));
assertEquals("true", providerConfig.getProperties().get("key-boolean"));
}
@Test
public void testNodeStartup() {
String xmlFileName = "test-hazelcast-discovery-spi.xml";
Config config = getDiscoverySPIConfig(xmlFileName, false);
try {
final HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance(config);
final HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config);
final HazelcastInstance hazelcastInstance3 = Hazelcast.newHazelcastInstance(config);
assertNotNull(hazelcastInstance1);
assertNotNull(hazelcastInstance2);
assertNotNull(hazelcastInstance3);
assertClusterSizeEventually(3, hazelcastInstance1, hazelcastInstance2, hazelcastInstance3);
} finally {
Hazelcast.shutdownAll();
}
}
@Test
public void testNodeFilter_from_xml() throws Exception {
String xmlFileName = "test-hazelcast-discovery-spi.xml";
InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Config config = new XmlConfigBuilder(xmlResource).build();
JoinConfig joinConfig = config.getNetworkConfig().getJoin();
DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig();
Address address = new Address("localhost", 5701);
DiscoveryServiceSettings settings = buildDiscoveryServiceSettings(address, discoveryConfig, DiscoveryMode.Client);
DiscoveryServiceProvider provider = new DefaultDiscoveryServiceProvider();
DiscoveryService discoveryService = provider.newDiscoveryService(settings);
discoveryService.start();
discoveryService.discoverNodes();
discoveryService.destroy();
Field nodeFilterField = DefaultDiscoveryService.class.getDeclaredField("nodeFilter");
nodeFilterField.setAccessible(true);
TestNodeFilter nodeFilter = (TestNodeFilter) nodeFilterField.get(discoveryService);
assertEquals(4, nodeFilter.getNodes().size());
}
@Test
public void test_AbstractDiscoveryStrategy_getOrNull() throws Exception {
PropertyDefinition first = new SimplePropertyDefinition("first", PropertyTypeConverter.STRING);
PropertyDefinition second = new SimplePropertyDefinition("second", BOOLEAN);
PropertyDefinition third = new SimplePropertyDefinition("third", PropertyTypeConverter.INTEGER);
PropertyDefinition fourth = new SimplePropertyDefinition("fourth", true, PropertyTypeConverter.STRING);
Map<String, Comparable> properties = new HashMap<String, Comparable>();
properties.put("first", "value-first");
properties.put("second", Boolean.FALSE);
properties.put("third", 100);
// system property > system environment > configuration
// property 'first' => "value-first"
// property 'second' => true
setEnvironment("test.second", "true");
// property 'third' => 300
setEnvironment("test.third", "200");
System.setProperty("test.third", "300");
// property 'fourth' => null
PropertyDiscoveryStrategy strategy = new PropertyDiscoveryStrategy(LOGGER, properties);
// without lookup of environment
assertEquals("value-first", strategy.getOrNull(first));
assertEquals(Boolean.FALSE, strategy.getOrNull(second));
assertEquals(100, ((Integer) strategy.getOrNull(third)).intValue());
assertNull(strategy.getOrNull(fourth));
// with lookup of environment (workaround to set environment doesn't work on all JDKs)
if (System.getenv("test.third") != null) {
assertEquals("value-first", strategy.getOrNull("test", first));
assertEquals(Boolean.TRUE, strategy.getOrNull("test", second));
assertEquals(300, ((Integer) strategy.getOrNull("test", third)).intValue());
assertNull(strategy.getOrNull("test", fourth));
}
}
@Test
public void test_AbstractDiscoveryStrategy_getOrDefault() throws Exception {
PropertyDefinition value = new SimplePropertyDefinition("value", PropertyTypeConverter.INTEGER);
Map<String, Comparable> properties = Collections.emptyMap();
PropertyDiscoveryStrategy strategy = new PropertyDiscoveryStrategy(LOGGER, properties);
assertEquals(1111, (long) strategy.getOrDefault(value, 1111));
assertEquals(1111, (long) strategy.getOrDefault("test", value, 1111));
}
@Test(expected = RuntimeException.class)
public void testSPIAwareMemberGroupFactoryInvalidConfig() throws Exception {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
try {
MemberGroupFactory groupFactory = new SPIAwareMemberGroupFactory(getNode(hazelcastInstance).getDiscoveryService());
Collection<Member> members = createMembers();
groupFactory.createMemberGroups(members);
} finally {
hazelcastInstance.shutdown();
}
}
@Test
public void testSPIAwareMemberGroupFactoryCreateMemberGroups() throws Exception {
String xmlFileName = "test-hazelcast-discovery-spi-metadata.xml";
Config config = getDiscoverySPIConfig(xmlFileName, false);
// we create this instance in order to fully create Node
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
Node node = getNode(hazelcastInstance);
assertNotNull(node);
MemberGroupFactory groupFactory = new SPIAwareMemberGroupFactory(node.getDiscoveryService());
Collection<Member> members = createMembers();
Collection<MemberGroup> memberGroups = groupFactory.createMemberGroups(members);
assertEquals("Member Groups: " + String.valueOf(memberGroups), 2, memberGroups.size());
for (MemberGroup memberGroup : memberGroups) {
assertEquals("Member Group: " + String.valueOf(memberGroup), 2, memberGroup.size());
}
hazelcastInstance.shutdown();
}
@Test
public void testSPIAwareMemberGroupFactoryCreateMemberGroups_withDeprecated() throws Exception {
String xmlFileName = "test-hazelcast-discovery-spi-metadata.xml";
Config config = getDiscoverySPIConfig(xmlFileName, true);
// we create this instance in order to fully create Node
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
Node node = getNode(hazelcastInstance);
assertNotNull(node);
MemberGroupFactory groupFactory = new SPIAwareMemberGroupFactory(node.getDiscoveryService());
Collection<Member> members = createMembers();
Collection<MemberGroup> memberGroups = groupFactory.createMemberGroups(members);
assertEquals("Member Groups: " + String.valueOf(memberGroups), 2, memberGroups.size());
for (MemberGroup memberGroup : memberGroups) {
assertEquals("Member Group: " + String.valueOf(memberGroup), 2, memberGroup.size());
}
hazelcastInstance.shutdown();
}
@Test(expected = IllegalArgumentException.class)
public void test_enabled_whenDiscoveryConfigIsNull() {
Config config = new Config();
config.setProperty(ClusterProperty.DISCOVERY_SPI_ENABLED.getName(), "true");
config.getNetworkConfig().getJoin().setDiscoveryConfig(null);
}
@Test
public void testCustomDiscoveryService_whenDiscoveredNodes_isNull() {
Config config = new Config();
config.setProperty(ClusterProperty.DISCOVERY_SPI_ENABLED.getName(), "true");
DiscoveryServiceProvider discoveryServiceProvider = new DiscoveryServiceProvider() {
public DiscoveryService newDiscoveryService(DiscoveryServiceSettings arg0) {
DiscoveryService mocked = mock(DiscoveryService.class);
when(mocked.discoverNodes()).thenReturn(null);
return mocked;
}
};
config.getNetworkConfig().getJoin().getDiscoveryConfig().setDiscoveryServiceProvider(discoveryServiceProvider);
try {
Hazelcast.newHazelcastInstance(config);
fail("Instance should not be started!");
} catch (IllegalStateException expected) {
}
}
@Test
public void testCustomDiscoveryService_whenDiscoveredNodes_isEmpty() {
Config config = new Config();
config.setProperty(ClusterProperty.DISCOVERY_SPI_ENABLED.getName(), "true");
final DiscoveryService discoveryService = mock(DiscoveryService.class);
DiscoveryServiceProvider discoveryServiceProvider = new DiscoveryServiceProvider() {
public DiscoveryService newDiscoveryService(DiscoveryServiceSettings arg0) {
when(discoveryService.discoverNodes()).thenReturn(Collections.<DiscoveryNode>emptyList());
return discoveryService;
}
};
config.getNetworkConfig().getJoin().getDiscoveryConfig().setDiscoveryServiceProvider(discoveryServiceProvider);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
try {
verify(discoveryService, atLeastOnce()).discoverNodes();
} finally {
instance.getLifecycleService().terminate();
}
}
@Test
public void testMemberGroup_givenSPIMemberGroupIsActived_whenInstanceStarting_wontThrowNPE() {
// this test has no assert. it's a regression test checking an instance can start when a SPI-driven member group
// strategy is configured. see #11681
Config config = new Config();
config.setProperty(ClusterProperty.DISCOVERY_SPI_ENABLED.getName(), "true");
JoinConfig joinConfig = config.getNetworkConfig().getJoin();
joinConfig.getMulticastConfig().setEnabled(false);
DiscoveryStrategyConfig discoveryStrategyConfig
= new DiscoveryStrategyConfig(MetadataProvidingDiscoveryStrategy.class.getName());
joinConfig.getDiscoveryConfig()
.addDiscoveryStrategyConfig(discoveryStrategyConfig);
config.getPartitionGroupConfig().setGroupType(SPI).setEnabled(true);
HazelcastInstance hazelcastInstance = null;
try {
// check the instance can actually be started
hazelcastInstance = Hazelcast.newHazelcastInstance(config);
} finally {
if (hazelcastInstance != null) {
hazelcastInstance.shutdown();
}
}
}
@Test
public void noBreakingChangeInPartitionGroupStrategyAbstractClass() {
TestBreakingChangesDiscoveryStrategy strategy = new TestBreakingChangesDiscoveryStrategy();
assertNull(strategy.getPartitionGroupStrategy());
}
static class TestBreakingChangesDiscoveryStrategy extends AbstractDiscoveryStrategy {
TestBreakingChangesDiscoveryStrategy() {
super(null, Collections.<String, Comparable>emptyMap());
}
@Override
public Iterable<DiscoveryNode> discoverNodes() {
return null;
}
}
@SuppressWarnings("unchecked")
private static void setEnvironment(String key, String value) throws Exception {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for (Class cl : classes) {
if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Map<String, String> map = (Map<String, String>) field.get(env);
map.put(key, value);
}
}
}
private DiscoveryServiceSettings buildDiscoveryServiceSettings(Address address, DiscoveryConfig config, DiscoveryMode mode) {
return new DiscoveryServiceSettings().setConfigClassLoader(DiscoverySpiTest.class.getClassLoader())
.setDiscoveryConfig(config).setDiscoveryMode(mode).setLogger(LOGGER)
.setDiscoveryNode(new SimpleDiscoveryNode(address));
}
private static class PropertyDiscoveryStrategy extends AbstractDiscoveryStrategy {
PropertyDiscoveryStrategy(ILogger logger, Map<String, Comparable> properties) {
super(logger, properties);
}
@Override
public Iterable<DiscoveryNode> discoverNodes() {
return null;
}
@Override
public <T extends Comparable> T getOrNull(PropertyDefinition property) {
return super.getOrNull(property);
}
@Override
public <T extends Comparable> T getOrNull(String prefix, PropertyDefinition property) {
return super.getOrNull(prefix, property);
}
@Override
public <T extends Comparable> T getOrDefault(PropertyDefinition property, T defaultValue) {
return super.getOrDefault(property, defaultValue);
}
@Override
public <T extends Comparable> T getOrDefault(String prefix, PropertyDefinition property, T defaultValue) {
return super.getOrDefault(prefix, property, defaultValue);
}
}
private static class TestDiscoveryStrategy implements DiscoveryStrategy {
@Override
public void start() {
}
@Override
public Collection<DiscoveryNode> discoverNodes() {
try {
List<DiscoveryNode> discoveryNodes = new ArrayList<DiscoveryNode>(4);
Address address = new Address("127.0.0.1", 50001);
discoveryNodes.add(new SimpleDiscoveryNode(address));
address = new Address("127.0.0.1", 50002);
discoveryNodes.add(new SimpleDiscoveryNode(address));
address = new Address("127.0.0.1", 50003);
discoveryNodes.add(new SimpleDiscoveryNode(address));
address = new Address("127.0.0.1", 50004);
discoveryNodes.add(new SimpleDiscoveryNode(address));
return discoveryNodes;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void destroy() {
}
@Override
public PartitionGroupStrategy getPartitionGroupStrategy(Collection<? extends Member> allMembers) {
return null;
}
@Override
public Map<String, String> discoverLocalMetadata() {
return Collections.emptyMap();
}
}
private static class DeprecatedTestDiscoveryStrategy extends TestDiscoveryStrategy {
@Override
public PartitionGroupStrategy getPartitionGroupStrategy() {
return null;
}
}
public static class TestDiscoveryStrategyFactory implements DiscoveryStrategyFactory {
private final Collection<PropertyDefinition> propertyDefinitions;
public TestDiscoveryStrategyFactory() {
List<PropertyDefinition> propertyDefinitions = new ArrayList<PropertyDefinition>();
propertyDefinitions.add(new SimplePropertyDefinition("key-string", PropertyTypeConverter.STRING));
propertyDefinitions.add(new SimplePropertyDefinition("key-int", PropertyTypeConverter.INTEGER));
propertyDefinitions.add(new SimplePropertyDefinition("key-boolean", BOOLEAN));
propertyDefinitions.add(new SimplePropertyDefinition("key-something", true, PropertyTypeConverter.STRING));
this.propertyDefinitions = Collections.unmodifiableCollection(propertyDefinitions);
}
@Override
public Class<? extends DiscoveryStrategy> getDiscoveryStrategyType() {
return TestDiscoveryStrategy.class;
}
@Override
public DiscoveryStrategy newDiscoveryStrategy(DiscoveryNode discoveryNode, ILogger logger,
Map<String, Comparable> properties) {
return new TestDiscoveryStrategy();
}
@Override
public Collection<PropertyDefinition> getConfigurationProperties() {
return propertyDefinitions;
}
}
public static class CollectingDiscoveryStrategyFactory implements DiscoveryStrategyFactory {
protected final List<DiscoveryNode> discoveryNodes;
CollectingDiscoveryStrategyFactory(List<DiscoveryNode> discoveryNodes) {
this.discoveryNodes = discoveryNodes;
}
@Override
public Class<? extends DiscoveryStrategy> getDiscoveryStrategyType() {
return CollectingDiscoveryStrategy.class;
}
@Override
public DiscoveryStrategy newDiscoveryStrategy(DiscoveryNode node, ILogger logger, Map<String, Comparable> properties) {
return new CollectingDiscoveryStrategy(node, discoveryNodes, logger, properties);
}
@Override
public Collection<PropertyDefinition> getConfigurationProperties() {
return null;
}
}
private static class DeprecatedCollectingDiscoveryStrategyFactory extends CollectingDiscoveryStrategyFactory {
DeprecatedCollectingDiscoveryStrategyFactory(List<DiscoveryNode> discoveryNodes) {
super(discoveryNodes);
}
@Override
public Class<? extends DiscoveryStrategy> getDiscoveryStrategyType() {
return DeprecatedCollectingDiscoveryStrategy.class;
}
@Override
public DiscoveryStrategy newDiscoveryStrategy(DiscoveryNode node, ILogger logger, Map<String, Comparable> properties) {
return new DeprecatedCollectingDiscoveryStrategy(node, discoveryNodes, logger, properties);
}
}
private static class CollectingDiscoveryStrategy extends AbstractDiscoveryStrategy {
private final List<DiscoveryNode> discoveryNodes;
private final DiscoveryNode discoveryNode;
CollectingDiscoveryStrategy(DiscoveryNode discoveryNode, List<DiscoveryNode> discoveryNodes, ILogger logger,
Map<String, Comparable> properties) {
super(logger, properties);
this.discoveryNodes = discoveryNodes;
this.discoveryNode = discoveryNode;
}
@Override
public void start() {
super.start();
discoveryNodes.add(discoveryNode);
getLogger();
getProperties();
}
// need to provide a custom impl
@Override
public PartitionGroupStrategy getPartitionGroupStrategy(Collection<? extends Member> allMembers) {
return new SPIPartitionGroupStrategy();
}
@Override
public Iterable<DiscoveryNode> discoverNodes() {
return new ArrayList<DiscoveryNode>(discoveryNodes);
}
@Override
public void destroy() {
super.destroy();
discoveryNodes.remove(discoveryNode);
}
}
private static class DeprecatedCollectingDiscoveryStrategy extends CollectingDiscoveryStrategy {
DeprecatedCollectingDiscoveryStrategy(DiscoveryNode discoveryNode, List<DiscoveryNode> discoveryNodes, ILogger logger,
Map<String, Comparable> properties) {
super(discoveryNode, discoveryNodes, logger, properties);
}
@Override
public PartitionGroupStrategy getPartitionGroupStrategy() {
return new SPIPartitionGroupStrategy();
}
}
public static class TestNodeFilter implements NodeFilter {
private final List<DiscoveryNode> nodes = new ArrayList<DiscoveryNode>();
@Override
public boolean test(DiscoveryNode candidate) {
nodes.add(candidate);
return true;
}
private List<DiscoveryNode> getNodes() {
return nodes;
}
}
public static class MetadataProvidingDiscoveryStrategyFactory implements DiscoveryStrategyFactory {
@Override
public Class<? extends DiscoveryStrategy> getDiscoveryStrategyType() {
return MetadataProvidingDiscoveryStrategy.class;
}
@Override
public DiscoveryStrategy newDiscoveryStrategy(DiscoveryNode discoveryNode, ILogger logger,
Map<String, Comparable> properties) {
return new MetadataProvidingDiscoveryStrategy(discoveryNode, logger, properties);
}
@Override
public Collection<PropertyDefinition> getConfigurationProperties() {
return asList((PropertyDefinition) new SimplePropertyDefinition("key-string", true, STRING),
new SimplePropertyDefinition("key-int", true, INTEGER),
new SimplePropertyDefinition("key-boolean", true, BOOLEAN));
}
}
private static class MetadataProvidingDiscoveryStrategy extends AbstractDiscoveryStrategy {
private final DiscoveryNode discoveryNode;
MetadataProvidingDiscoveryStrategy(DiscoveryNode discoveryNode, ILogger logger, Map<String, Comparable> properties) {
super(logger, properties);
this.discoveryNode = discoveryNode;
}
@Override
public Iterable<DiscoveryNode> discoverNodes() {
return Collections.singleton(discoveryNode);
}
@Override
public PartitionGroupStrategy getPartitionGroupStrategy(Collection<? extends Member> allMembers) {
return new SPIPartitionGroupStrategy();
}
@Override
public Map<String, String> discoverLocalMetadata() {
Map<String, String> metadata = new HashMap<>();
metadata.put("test-string", "TEST");
return metadata;
}
}
private static class DeprecatedMetadataProvidingDiscoveryStrategy extends MetadataProvidingDiscoveryStrategy {
DeprecatedMetadataProvidingDiscoveryStrategy(DiscoveryNode discoveryNode, ILogger logger, Map<String, Comparable> properties) {
super(discoveryNode, logger, properties);
}
@Override
public PartitionGroupStrategy getPartitionGroupStrategy() {
return new SPIPartitionGroupStrategy();
}
}
private static class SPIPartitionGroupStrategy implements PartitionGroupStrategy {
@Override
public Iterable<MemberGroup> getMemberGroups() {
List<MemberGroup> groups = new ArrayList<MemberGroup>();
try {
groups.add(new DefaultMemberGroup(createMembers()));
groups.add(new DefaultMemberGroup(createMembers()));
} catch (UnknownHostException e) {
e.printStackTrace();
}
return groups;
}
}
private static Collection<Member> createMembers() throws UnknownHostException {
Collection<Member> members = new HashSet<Member>();
InetAddress fakeAddress = InetAddress.getLocalHost();
members.add(new MemberImpl(new Address("192.192.0.1", fakeAddress, 5701), VERSION, true));
members.add(new MemberImpl(new Address("192.192.0.1", fakeAddress, 5702), VERSION, false));
members.add(new MemberImpl(new Address("download.hazelcast.org", fakeAddress, 5701), VERSION, false));
members.add(new MemberImpl(new Address("download.hazelcast.org", fakeAddress, 5702), VERSION, false));
return members;
}
private static Config getDiscoverySPIConfig(String xmlFileName, boolean isDeprecated) {
InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Config config = new XmlConfigBuilder(xmlResource).build();
config.getNetworkConfig().setPort(50001);
InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces();
interfaces.clear();
interfaces.setEnabled(true);
interfaces.addInterface("127.0.0.1");
List<DiscoveryNode> discoveryNodes = new CopyOnWriteArrayList<DiscoveryNode>();
DiscoveryStrategyFactory factory = isDeprecated ? new DeprecatedCollectingDiscoveryStrategyFactory(discoveryNodes)
: new CollectingDiscoveryStrategyFactory(discoveryNodes);
DiscoveryConfig discoveryConfig = config.getNetworkConfig().getJoin().getDiscoveryConfig();
discoveryConfig.getDiscoveryStrategyConfigs().clear();
DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
return config;
}
}