Skip to content

Commit

Permalink
Support creating and configuring arbitrary primitive types via REST API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Apr 12, 2018
1 parent 49bd902 commit b5f89ed
Show file tree
Hide file tree
Showing 24 changed files with 144 additions and 169 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/io/atomix/core/Atomix.java
Expand Up @@ -298,6 +298,11 @@ public Collection<PrimitiveInfo> getPrimitives(PrimitiveType primitiveType) {
return context.primitives.getPrimitives(primitiveType);
}

@Override
public <P extends DistributedPrimitive> P getPrimitive(String name) {
return context.primitives.getPrimitive(name);
}

/**
* Starts the Atomix instance.
* <p>
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/io/atomix/core/PrimitivesService.java
Expand Up @@ -493,6 +493,15 @@ default TransactionBuilder transactionBuilder() {
*/
<E> WorkQueue<E> getWorkQueue(String name);

/**
* Returns a registered primitive.
*
* @param name the primitive name
* @param <P> the primitive type
* @return the primitive instance
*/
<P extends DistributedPrimitive> P getPrimitive(String name);

/**
* Returns a cached primitive.
*
Expand Down
Expand Up @@ -21,4 +21,7 @@
* Atomic counter configuration.
*/
public class AtomicCounterConfig extends PrimitiveConfig<AtomicCounterConfig> {
public AtomicCounterConfig() {
super(AtomicCounterType.instance());
}
}
Expand Up @@ -21,4 +21,7 @@
* Leader election configuration.
*/
public class LeaderElectionConfig extends PrimitiveConfig<LeaderElectionConfig> {
public LeaderElectionConfig() {
super(LeaderElectionType.instance());
}
}
Expand Up @@ -21,4 +21,7 @@
* Leader elector configuration.
*/
public class LeaderElectorConfig extends PrimitiveConfig<LeaderElectorConfig> {
public LeaderElectorConfig() {
super(LeaderElectorType.instance());
}
}
Expand Up @@ -21,4 +21,7 @@
* ID generator configuration.
*/
public class AtomicIdGeneratorConfig extends PrimitiveConfig<AtomicIdGeneratorConfig> {
public AtomicIdGeneratorConfig() {
super(AtomicIdGeneratorType.instance());
}
}
16 changes: 16 additions & 0 deletions core/src/main/java/io/atomix/core/impl/CorePrimitiveRegistry.java
Expand Up @@ -94,6 +94,22 @@ public Collection<PrimitiveInfo> getPrimitives(PrimitiveType primitiveType) {
.collect(Collectors.toList());
}

@Override
public PrimitiveInfo getPrimitive(String name) {
try {
return primitives.get(name)
.thenApply(value -> value == null ? null : value.map(type -> new PrimitiveInfo(name, PrimitiveTypes.getPrimitiveType(type))).value())
.get(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PrimitiveException.Interrupted();
} catch (TimeoutException e) {
throw new PrimitiveException.Timeout();
} catch (ExecutionException e) {
throw new PrimitiveException(e.getCause());
}
}

@Override
public CompletableFuture<PrimitiveRegistry> start() {
return partitionGroup.getPartitions()
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/io/atomix/core/impl/CorePrimitivesService.java
Expand Up @@ -180,6 +180,22 @@ public <B extends DistributedPrimitiveBuilder<B, C, P>, C extends PrimitiveConfi
return primitiveType.newPrimitiveBuilder(name, managementService);
}

@Override
@SuppressWarnings("unchecked")
public <P extends DistributedPrimitive> P getPrimitive(String name) {
try {
return (P) cache.get(name, () -> {
PrimitiveInfo info = primitiveRegistry.getPrimitive(name);
if (info == null) {
return null;
}
return info.type().newPrimitiveBuilder(name, (PrimitiveConfig) config.getPrimitive(name), managementService).build();
});
} catch (ExecutionException e) {
throw new AtomixRuntimeException(e);
}
}

@Override
@SuppressWarnings("unchecked")
public <C extends PrimitiveConfig<C>, P extends DistributedPrimitive> P getPrimitive(
Expand Down
Expand Up @@ -21,4 +21,7 @@
* Distributed lock configuration.
*/
public class DistributedLockConfig extends PrimitiveConfig<DistributedLockConfig> {
public DistributedLockConfig() {
super(DistributedLockType.instance());
}
}
Expand Up @@ -21,4 +21,7 @@
* Atomic counter map configuration.
*/
public class AtomicCounterMapConfig extends PrimitiveConfig<AtomicCounterMapConfig> {
public AtomicCounterMapConfig() {
super(AtomicCounterMapType.instance());
}
}
Expand Up @@ -23,6 +23,10 @@
public class ConsistentMapConfig extends PrimitiveConfig<ConsistentMapConfig> {
private boolean nullValues = false;

public ConsistentMapConfig() {
super(ConsistentMapType.instance());
}

/**
* Enables null values in the map.
*
Expand Down
Expand Up @@ -21,4 +21,7 @@
* Consistent tree-map configuration.
*/
public class ConsistentTreeMapConfig extends PrimitiveConfig<ConsistentTreeMapConfig> {
public ConsistentTreeMapConfig() {
super(ConsistentTreeMapType.instance());
}
}
Expand Up @@ -21,4 +21,7 @@
* Consistent multimap configuration.
*/
public class ConsistentMultimapConfig extends PrimitiveConfig<ConsistentMultimapConfig> {
public ConsistentMultimapConfig() {
super(ConsistentMultimapType.instance());
}
}
3 changes: 3 additions & 0 deletions core/src/main/java/io/atomix/core/queue/WorkQueueConfig.java
Expand Up @@ -21,4 +21,7 @@
* Work queue configuration.
*/
public class WorkQueueConfig extends PrimitiveConfig<WorkQueueConfig> {
public WorkQueueConfig() {
super(WorkQueueType.instance());
}
}
Expand Up @@ -21,4 +21,7 @@
* Distributed set configuration.
*/
public class DistributedSetConfig extends PrimitiveConfig<DistributedSetConfig> {
public DistributedSetConfig() {
super(DistributedSetType.instance());
}
}
Expand Up @@ -25,6 +25,10 @@
public class TransactionConfig extends PrimitiveConfig<TransactionConfig> {
private Isolation isolation = Isolation.READ_COMMITTED;

public TransactionConfig() {
super(TransactionType.instance());
}

/**
* Sets the transaction isolation level.
*
Expand Down
Expand Up @@ -15,10 +15,14 @@
*/
package io.atomix.core.transaction;

import io.atomix.core.map.ConsistentMapType;
import io.atomix.primitive.PrimitiveConfig;

/**
* Transactional map configuration.
*/
public class TransactionalMapConfig extends PrimitiveConfig {
public TransactionalMapConfig() {
super(ConsistentMapType.instance());
}
}
Expand Up @@ -15,10 +15,14 @@
*/
package io.atomix.core.transaction;

import io.atomix.core.set.DistributedSetType;
import io.atomix.primitive.PrimitiveConfig;

/**
* Transactional set configuration.
*/
public class TransactionalSetConfig extends PrimitiveConfig {
public TransactionalSetConfig() {
super(DistributedSetType.instance());
}
}
Expand Up @@ -24,6 +24,10 @@
public class DocumentTreeConfig extends PrimitiveConfig<DocumentTreeConfig> {
private Ordering ordering;

public DocumentTreeConfig() {
super(DocumentTreeType.instance());
}

/**
* Sets the ordering of the tree nodes.
* <p>
Expand Down
Expand Up @@ -21,4 +21,7 @@
* Atomic value configuration.
*/
public class AtomicValueConfig extends PrimitiveConfig<AtomicValueConfig> {
public AtomicValueConfig() {
super(AtomicValueType.instance());
}
}
14 changes: 14 additions & 0 deletions primitive/src/main/java/io/atomix/primitive/PrimitiveConfig.java
Expand Up @@ -23,12 +23,26 @@
public abstract class PrimitiveConfig<C extends PrimitiveConfig<C>> {
private static final int DEFAULT_CACHE_SIZE = 1000;

private final PrimitiveType primitiveType;
private SerializerConfig serializerConfig;
private PrimitiveProtocolConfig protocolConfig;
private boolean cacheEnabled = false;
private int cacheSize = DEFAULT_CACHE_SIZE;
private boolean readOnly = false;

protected PrimitiveConfig(PrimitiveType primitiveType) {
this.primitiveType = primitiveType;
}

/**
* Returns the primitive type.
*
* @return the primitive type
*/
public PrimitiveType getType() {
return primitiveType;
}

/**
* Returns the serializer configuration.
*
Expand Down
Expand Up @@ -47,4 +47,12 @@ public interface PrimitiveRegistry {
*/
Collection<PrimitiveInfo> getPrimitives(PrimitiveType primitiveType);

/**
* Returns the info for a single primitive.
*
* @param name the primitive name
* @return the primitive info
*/
PrimitiveInfo getPrimitive(String name);

}
Expand Up @@ -20,6 +20,7 @@
import io.atomix.utils.Generics;
import io.atomix.utils.Identifier;

import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ default Class<? extends DistributedPrimitive> primitiveClass() {
*
* @return the primitive resource factory
*/
default Supplier<PrimitiveResource> resourceFactory() {
default Function<P, PrimitiveResource> resourceFactory() {
return null;
}

Expand Down

0 comments on commit b5f89ed

Please sign in to comment.