/
Atomix.java
729 lines (645 loc) · 22.1 KB
/
Atomix.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
/*
* Copyright 2017-present Open Networking Foundation
*
* 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 io.atomix.core;
import com.google.common.collect.Streams;
import io.atomix.cluster.AtomixCluster;
import io.atomix.cluster.ClusterMembershipService;
import io.atomix.cluster.Member;
import io.atomix.cluster.messaging.ClusterCommunicationService;
import io.atomix.core.config.ConfigService;
import io.atomix.core.config.impl.DefaultConfigService;
import io.atomix.core.counter.AtomicCounter;
import io.atomix.core.election.LeaderElection;
import io.atomix.core.election.LeaderElector;
import io.atomix.core.generator.AtomicIdGenerator;
import io.atomix.core.impl.CorePrimitivesService;
import io.atomix.core.lock.DistributedLock;
import io.atomix.core.map.AtomicCounterMap;
import io.atomix.core.map.ConsistentMap;
import io.atomix.core.map.ConsistentTreeMap;
import io.atomix.core.multimap.ConsistentMultimap;
import io.atomix.core.profile.Profile;
import io.atomix.core.queue.WorkQueue;
import io.atomix.core.registry.RegistryService;
import io.atomix.core.registry.impl.DefaultRegistryService;
import io.atomix.core.semaphore.DistributedSemaphore;
import io.atomix.core.set.DistributedSet;
import io.atomix.core.transaction.TransactionBuilder;
import io.atomix.core.tree.DocumentTree;
import io.atomix.core.utils.config.PartitionGroupConfigMapper;
import io.atomix.core.utils.config.PrimitiveConfigMapper;
import io.atomix.core.utils.config.PrimitiveProtocolConfigMapper;
import io.atomix.core.utils.config.ProfileMapper;
import io.atomix.core.value.AtomicValue;
import io.atomix.primitive.DistributedPrimitive;
import io.atomix.primitive.DistributedPrimitiveBuilder;
import io.atomix.primitive.PrimitiveConfig;
import io.atomix.primitive.PrimitiveInfo;
import io.atomix.primitive.PrimitiveType;
import io.atomix.primitive.partition.ManagedPartitionGroup;
import io.atomix.primitive.partition.ManagedPartitionService;
import io.atomix.primitive.partition.PartitionGroupConfig;
import io.atomix.primitive.partition.PartitionGroupType;
import io.atomix.primitive.partition.PartitionService;
import io.atomix.primitive.partition.impl.DefaultPartitionService;
import io.atomix.utils.concurrent.Futures;
import io.atomix.utils.concurrent.SingleThreadContext;
import io.atomix.utils.concurrent.ThreadContext;
import io.atomix.utils.concurrent.Threads;
import io.atomix.utils.config.ConfigMapper;
import io.atomix.utils.config.ConfigurationException;
import io.atomix.utils.net.Address;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Stream;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Atomix!
*/
public class Atomix extends AtomixCluster implements PrimitivesService {
static final String[] DEFAULT_RESOURCES = new String[]{"atomix.conf", "atomix.json", "atomix.properties", "defaults.conf"};
private static String[] withDefaultResources(String config) {
return Streams.concat(Stream.of(config), Stream.of(DEFAULT_RESOURCES)).toArray(String[]::new);
}
/**
* Returns a new Atomix configuration.
*
* @return a new Atomix configuration
*/
public static AtomixConfig config() {
return config(Thread.currentThread().getContextClassLoader());
}
/**
* Returns a new Atomix configuration.
*
* @return a new Atomix configuration
*/
public static AtomixConfig config(ClassLoader classLoader) {
return config(DEFAULT_RESOURCES, classLoader);
}
/**
* Returns a new Atomix configuration from the given resource.
*
* @param resource the resource from which to return a new Atomix configuration
* @return a new Atomix configuration from the given resource
*/
public static AtomixConfig config(String resource) {
return config(resource, Thread.currentThread().getContextClassLoader());
}
/**
* Returns a new Atomix configuration from the given resource.
*
* @param resource the resource from which to return a new Atomix configuration
* @param classLoader the class loader
* @return a new Atomix configuration from the given resource
*/
public static AtomixConfig config(String resource, ClassLoader classLoader) {
return config(withDefaultResources(resource), classLoader);
}
/**
* Returns a new Atomix configuration from the given resources.
*
* @param resources the resources from which to return a new Atomix configuration
* @param classLoader the class loader
* @return a new Atomix configuration from the given resource
*/
private static AtomixConfig config(String[] resources, ClassLoader classLoader) {
ConfigMapper mapper = new ConfigMapper(
classLoader,
new PartitionGroupConfigMapper(),
new PrimitiveConfigMapper(),
new PrimitiveProtocolConfigMapper(),
new ProfileMapper());
return mapper.loadResources(AtomixConfig.class, resources);
}
/**
* Returns a new Atomix builder.
*
* @return a new Atomix builder
*/
public static Builder builder() {
return builder(Thread.currentThread().getContextClassLoader());
}
/**
* Returns a new Atomix builder.
*
* @param classLoader the class loader
* @return a new Atomix builder
*/
public static Builder builder(ClassLoader classLoader) {
return builder(config(DEFAULT_RESOURCES, classLoader));
}
/**
* Returns a new Atomix builder.
*
* @param config the Atomix configuration
* @return a new Atomix builder
*/
public static Builder builder(String config) {
return builder(config, Thread.currentThread().getContextClassLoader());
}
/**
* Returns a new Atomix builder.
*
* @param config the Atomix configuration
* @param classLoader the class loader
* @return a new Atomix builder
*/
public static Builder builder(String config, ClassLoader classLoader) {
return new Builder(config(withDefaultResources(config), classLoader));
}
/**
* Returns a new Atomix builder.
*
* @param config the Atomix configuration
* @return a new Atomix builder
*/
public static Builder builder(AtomixConfig config) {
return new Builder(config);
}
protected static final Logger LOGGER = LoggerFactory.getLogger(Atomix.class);
private final ScheduledExecutorService executorService;
private final RegistryService registry;
private final ConfigService config;
private final ManagedPartitionService partitions;
private final ManagedPrimitivesService primitives;
private final boolean enableShutdownHook;
private final ThreadContext threadContext = new SingleThreadContext("atomix-%d");
private Thread shutdownHook = null;
public Atomix(String configFile) {
this(configFile, Thread.currentThread().getContextClassLoader());
}
public Atomix(String configFile, ClassLoader classLoader) {
this(config(withDefaultResources(configFile), classLoader));
}
public Atomix(File configFile) {
this(configFile, Thread.currentThread().getContextClassLoader());
}
public Atomix(File configFile, ClassLoader classLoader) {
this(configFile.getAbsolutePath(), classLoader);
}
private Atomix(AtomixConfig config) {
super(config.getClusterConfig());
config.getProfiles().forEach(profile -> profile.configure(config));
this.executorService = Executors.newScheduledThreadPool(
Runtime.getRuntime().availableProcessors(),
Threads.namedThreads("atomix-primitive-%d", LOGGER));
this.registry = new DefaultRegistryService(config.getRegistry());
this.config = new DefaultConfigService(config.getPrimitives().values());
this.partitions = buildPartitionService(config, membershipService(), communicationService(), registry);
this.primitives = new CorePrimitivesService(
executorService(),
membershipService(),
communicationService(),
eventingService(),
partitionService(),
registryService(),
configService());
this.enableShutdownHook = config.isEnableShutdownHook();
}
/**
* Returns the core Atomix executor service.
*
* @return the core Atomix executor service
*/
public ScheduledExecutorService executorService() {
return executorService;
}
/**
* Returns the type registry service.
*
* @return the type registry service
*/
public RegistryService registryService() {
return registry;
}
/**
* Returns the primitive configuration service.
*
* @return the primitive configuration service
*/
public ConfigService configService() {
return config;
}
/**
* Returns the partition service.
*
* @return the partition service
*/
public PartitionService partitionService() {
return partitions;
}
/**
* Returns the primitives service.
*
* @return the primitives service
*/
public PrimitivesService primitivesService() {
return primitives;
}
@Override
public TransactionBuilder transactionBuilder(String name) {
return primitives.transactionBuilder(name);
}
@Override
public <B extends DistributedPrimitiveBuilder<B, C, P>, C extends PrimitiveConfig<C>, P extends DistributedPrimitive> B primitiveBuilder(
String name,
PrimitiveType<B, C, P> primitiveType) {
return primitives.primitiveBuilder(name, primitiveType);
}
@Override
public <K, V> ConsistentMap<K, V> getConsistentMap(String name) {
return primitives.getConsistentMap(name);
}
@Override
public <V> DocumentTree<V> getDocumentTree(String name) {
return primitives.getDocumentTree(name);
}
@Override
public <V> ConsistentTreeMap<V> getTreeMap(String name) {
return primitives.getTreeMap(name);
}
@Override
public <K, V> ConsistentMultimap<K, V> getConsistentMultimap(String name) {
return primitives.getConsistentMultimap(name);
}
@Override
public <K> AtomicCounterMap<K> getAtomicCounterMap(String name) {
return primitives.getAtomicCounterMap(name);
}
@Override
public <E> DistributedSet<E> getSet(String name) {
return primitives.getSet(name);
}
@Override
public AtomicCounter getAtomicCounter(String name) {
return primitives.getAtomicCounter(name);
}
@Override
public AtomicIdGenerator getAtomicIdGenerator(String name) {
return primitives.getAtomicIdGenerator(name);
}
@Override
public <V> AtomicValue<V> getAtomicValue(String name) {
return primitives.getAtomicValue(name);
}
@Override
public <T> LeaderElection<T> getLeaderElection(String name) {
return primitives.getLeaderElection(name);
}
@Override
public <T> LeaderElector<T> getLeaderElector(String name) {
return primitives.getLeaderElector(name);
}
@Override
public DistributedLock getLock(String name) {
return primitives.getLock(name);
}
@Override
public DistributedSemaphore getSemaphore(String name) {
return primitives.getSemaphore(name);
}
@Override
public <E> WorkQueue<E> getWorkQueue(String name) {
return primitives.getWorkQueue(name);
}
@Override
public <P extends DistributedPrimitive> P getPrimitive(String name, String primitiveType) {
return primitives.getPrimitive(name, primitiveType);
}
@Override
public <P extends DistributedPrimitive> P getPrimitive(String name, String primitiveType, PrimitiveConfig primitiveConfig) {
return primitives.getPrimitive(name, primitiveType, primitiveConfig);
}
@Override
public <C extends PrimitiveConfig<C>, P extends DistributedPrimitive> P getPrimitive(String name, PrimitiveType<?, C, P> primitiveType, C primitiveConfig) {
return primitives.getPrimitive(name, primitiveType, primitiveConfig);
}
@Override
public Collection<PrimitiveInfo> getPrimitives() {
return primitives.getPrimitives();
}
@Override
public Collection<PrimitiveInfo> getPrimitives(PrimitiveType primitiveType) {
return primitives.getPrimitives(primitiveType);
}
@Override
public <P extends DistributedPrimitive> P getPrimitive(String name) {
return primitives.getPrimitive(name);
}
/**
* Starts the Atomix instance.
* <p>
* The returned future will be completed once this instance completes startup. Note that in order to complete startup,
* all partitions must be able to form. For Raft partitions, that requires that a majority of the nodes in each
* partition be started concurrently.
*
* @return a future to be completed once the instance has completed startup
*/
@Override
public synchronized CompletableFuture<Void> start() {
if (closeFuture != null) {
return Futures.exceptionalFuture(new IllegalStateException("Atomix instance " +
(closeFuture.isDone() ? "shutdown" : "shutting down")));
}
return super.start().thenRun(() -> {
if (enableShutdownHook) {
if (shutdownHook == null) {
shutdownHook = new Thread(() -> super.stop().join());
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
}
});
}
@Override
protected CompletableFuture<Void> startServices() {
return super.startServices()
.thenComposeAsync(v -> partitions.start(), threadContext)
.thenComposeAsync(v -> primitives.start(), threadContext)
.thenApply(v -> null);
}
@Override
public synchronized CompletableFuture<Void> stop() {
if (shutdownHook != null) {
try {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
shutdownHook = null;
} catch (IllegalStateException e) {
// JVM shutting down
}
}
return super.stop();
}
@Override
@SuppressWarnings("unchecked")
protected CompletableFuture<Void> stopServices() {
return primitives.stop()
.exceptionally(e -> null)
.thenComposeAsync(v -> partitions.stop(), threadContext)
.exceptionally(e -> null)
.thenComposeAsync(v -> super.stopServices(), threadContext);
}
@Override
protected CompletableFuture<Void> completeShutdown() {
executorService.shutdownNow();
threadContext.close();
return super.completeShutdown();
}
@Override
public String toString() {
return toStringHelper(this)
.add("partitions", partitionService())
.toString();
}
/**
* Builds the core partition group.
*/
@SuppressWarnings("unchecked")
private static ManagedPartitionGroup buildSystemPartitionGroup(AtomixConfig config) {
PartitionGroupConfig partitionGroupConfig = config.getManagementGroup();
if (partitionGroupConfig == null) {
return null;
}
PartitionGroupType partitionGroupType = config.getRegistry().getPartitionGroupTypes().get(partitionGroupConfig.getType());
if (partitionGroupType == null) {
throw new ConfigurationException("Unknown partition group type " + partitionGroupConfig.getType());
}
return partitionGroupType.newGroup(partitionGroupConfig);
}
/**
* Builds a partition service.
*/
@SuppressWarnings("unchecked")
private static ManagedPartitionService buildPartitionService(
AtomixConfig config,
ClusterMembershipService clusterMembershipService,
ClusterCommunicationService messagingService,
RegistryService registryService) {
List<ManagedPartitionGroup> partitionGroups = new ArrayList<>();
for (PartitionGroupConfig partitionGroupConfig : config.getPartitionGroups().values()) {
PartitionGroupType partitionGroupType = config.getRegistry().getPartitionGroupTypes().get(partitionGroupConfig.getType());
if (partitionGroupType == null) {
throw new ConfigurationException("Unknown partition group type " + partitionGroupConfig.getType());
}
partitionGroups.add(partitionGroupType.newGroup(partitionGroupConfig));
}
return new DefaultPartitionService(
clusterMembershipService,
messagingService,
registryService.primitiveTypes(),
buildSystemPartitionGroup(config),
partitionGroups,
registryService.partitionGroupTypes());
}
/**
* Atomix builder.
*/
public static class Builder extends AtomixCluster.Builder {
private final AtomixConfig config;
private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
private Builder() {
this(new AtomixConfig());
}
private Builder(AtomixConfig config) {
super(config.getClusterConfig());
this.config = checkNotNull(config);
}
/**
* Sets the Atomix class loader.
*
* @param classLoader the class loader
* @return the Atomix builder
*/
public Builder withClassLoader(ClassLoader classLoader) {
this.classLoader = checkNotNull(classLoader, "classLoader cannot be null");
return this;
}
/**
* Enables the shutdown hook.
*
* @return the Atomix builder
*/
public Builder withShutdownHookEnabled() {
return withShutdownHook(true);
}
/**
* Enables the shutdown hook.
*
* @param enabled if <code>true</code> a shutdown hook will be registered
* @return the Atomix builder
*/
public Builder withShutdownHook(boolean enabled) {
config.setEnableShutdownHook(enabled);
return this;
}
/**
* Sets the Atomix profiles.
*
* @param profiles the profiles
* @return the Atomix builder
*/
public Builder withProfiles(Profile... profiles) {
return withProfiles(Arrays.asList(checkNotNull(profiles)));
}
/**
* Sets the Atomix profiles.
*
* @param profiles the profiles
* @return the Atomix builder
*/
public Builder withProfiles(Collection<Profile> profiles) {
profiles.forEach(config::addProfile);
return this;
}
/**
* Adds an Atomix profile.
*
* @param profile the profile to add
* @return the Atomix builder
*/
public Builder addProfile(Profile profile) {
config.addProfile(profile);
return this;
}
/**
* Sets the system management partition group.
*
* @param systemManagementGroup the system management partition group
* @return the Atomix builder
*/
public Builder withManagementGroup(ManagedPartitionGroup systemManagementGroup) {
config.setManagementGroup(systemManagementGroup.config());
return this;
}
/**
* Sets the partition groups.
*
* @param partitionGroups the partition groups
* @return the Atomix builder
* @throws NullPointerException if the partition groups are null
*/
public Builder withPartitionGroups(ManagedPartitionGroup... partitionGroups) {
return withPartitionGroups(Arrays.asList(checkNotNull(partitionGroups, "partitionGroups cannot be null")));
}
/**
* Sets the partition groups.
*
* @param partitionGroups the partition groups
* @return the Atomix builder
* @throws NullPointerException if the partition groups are null
*/
public Builder withPartitionGroups(Collection<ManagedPartitionGroup> partitionGroups) {
partitionGroups.forEach(group -> config.addPartitionGroup(group.config()));
return this;
}
/**
* Adds a partition group.
*
* @param partitionGroup the partition group to add
* @return the Atomix builder
* @throws NullPointerException if the partition group is null
*/
public Builder addPartitionGroup(ManagedPartitionGroup partitionGroup) {
config.addPartitionGroup(partitionGroup.config());
return this;
}
/**
* Sets the primitive types.
*
* @param primitiveTypes the primitive types
* @return the Atomix builder
* @throws NullPointerException if the primitive types is {@code null}
*/
public Builder withPrimitiveTypes(PrimitiveType... primitiveTypes) {
return withPrimitiveTypes(Arrays.asList(primitiveTypes));
}
/**
* Sets the primitive types.
*
* @param primitiveTypes the primitive types
* @return the Atomix builder
* @throws NullPointerException if the primitive types is {@code null}
*/
public Builder withPrimitiveTypes(Collection<PrimitiveType> primitiveTypes) {
primitiveTypes.forEach(type -> config.getRegistry().addPrimitiveType(type));
return this;
}
/**
* Adds a primitive type.
*
* @param primitiveType the primitive type to add
* @return the Atomix builder
* @throws NullPointerException if the primitive type is {@code null}
*/
public Builder addPrimitiveType(PrimitiveType primitiveType) {
config.getRegistry().addPrimitiveType(primitiveType);
return this;
}
@Override
public Builder withClusterName(String clusterName) {
super.withClusterName(clusterName);
return this;
}
@Override
public Builder withLocalMember(Member localMember) {
super.withLocalMember(localMember);
return this;
}
@Override
public Builder withMembers(Member... members) {
super.withMembers(members);
return this;
}
@Override
public Builder withMembers(Collection<Member> members) {
super.withMembers(members);
return this;
}
@Override
public Builder withMulticastEnabled() {
super.withMulticastEnabled();
return this;
}
@Override
public Builder withMulticastEnabled(boolean multicastEnabled) {
super.withMulticastEnabled(multicastEnabled);
return this;
}
@Override
public Builder withMulticastAddress(Address address) {
super.withMulticastAddress(address);
return this;
}
/**
* Builds a new Atomix instance.
*
* @return a new Atomix instance
*/
@Override
public Atomix build() {
return new Atomix(config);
}
}
}