Skip to content

Commit

Permalink
Add configuration service to allow primitive configurations to be add…
Browse files Browse the repository at this point in the history
…ed, modified, and read.
  • Loading branch information
kuujo committed May 30, 2018
1 parent fbc9473 commit e5e1bac
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 20 deletions.
15 changes: 14 additions & 1 deletion core/src/main/java/io/atomix/core/Atomix.java
Expand Up @@ -20,6 +20,8 @@
import io.atomix.cluster.ClusterMembershipService; import io.atomix.cluster.ClusterMembershipService;
import io.atomix.cluster.Member; import io.atomix.cluster.Member;
import io.atomix.cluster.messaging.ClusterCommunicationService; 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.counter.AtomicCounter;
import io.atomix.core.election.LeaderElection; import io.atomix.core.election.LeaderElection;
import io.atomix.core.election.LeaderElector; import io.atomix.core.election.LeaderElector;
Expand Down Expand Up @@ -197,6 +199,7 @@ public static Builder builder(AtomixConfig config) {


private final ScheduledExecutorService executorService; private final ScheduledExecutorService executorService;
private final RegistryService registry; private final RegistryService registry;
private final ConfigService config;
private final ManagedPartitionService partitions; private final ManagedPartitionService partitions;
private final ManagedPrimitivesService primitives; private final ManagedPrimitivesService primitives;
private final boolean enableShutdownHook; private final boolean enableShutdownHook;
Expand Down Expand Up @@ -226,6 +229,7 @@ private Atomix(AtomixConfig config) {
Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(),
Threads.namedThreads("atomix-primitive-%d", LOGGER)); Threads.namedThreads("atomix-primitive-%d", LOGGER));
this.registry = new DefaultRegistryService(config.getRegistry()); this.registry = new DefaultRegistryService(config.getRegistry());
this.config = new DefaultConfigService(config.getPrimitives().values());
this.partitions = buildPartitionService(config, membershipService(), communicationService(), registry); this.partitions = buildPartitionService(config, membershipService(), communicationService(), registry);
this.primitives = new CorePrimitivesService( this.primitives = new CorePrimitivesService(
executorService(), executorService(),
Expand All @@ -234,7 +238,7 @@ private Atomix(AtomixConfig config) {
eventingService(), eventingService(),
partitionService(), partitionService(),
registryService(), registryService(),
config); configService());
this.enableShutdownHook = config.isEnableShutdownHook(); this.enableShutdownHook = config.isEnableShutdownHook();
} }


Expand All @@ -256,6 +260,15 @@ public RegistryService registryService() {
return registry; return registry;
} }


/**
* Returns the primitive configuration service.
*
* @return the primitive configuration service
*/
public ConfigService configService() {
return config;
}

/** /**
* Returns the partition service. * Returns the partition service.
* *
Expand Down
45 changes: 45 additions & 0 deletions core/src/main/java/io/atomix/core/config/ConfigService.java
@@ -0,0 +1,45 @@
/*
* Copyright 2018-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.config;

import io.atomix.primitive.PrimitiveConfig;

/**
* Configuration service.
*/
public interface ConfigService {

/**
* Returns the registered configuration for the given primitive.
*
* @param primitiveName the primitive name
* @param <C> the configuration type
* @return the primitive configuration
*/
<C extends PrimitiveConfig<C>> C getConfig(String primitiveName);

/**
* Adds a primitive configuration.
* <p>
* If a configuration is already registered it will not be overridden. The returned configuration represents the
* configuration that will be used when constructing a new primitive with the given name.
*
* @param config the configuration to add
* @return the registered primitive configuration
*/
PrimitiveConfig addConfig(PrimitiveConfig config);

}
@@ -0,0 +1,49 @@
/*
* Copyright 2018-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.config.impl;

import com.google.common.collect.Maps;
import io.atomix.core.config.ConfigService;
import io.atomix.primitive.PrimitiveConfig;

import java.util.Collection;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Default configuration service.
*/
public class DefaultConfigService implements ConfigService {
private final Map<String, PrimitiveConfig> configs = Maps.newConcurrentMap();

public DefaultConfigService(Collection<PrimitiveConfig> configs) {
configs.forEach(config -> this.configs.put(config.getName(), config));
}

@Override
@SuppressWarnings("unchecked")
public <C extends PrimitiveConfig<C>> C getConfig(String primitiveName) {
return (C) configs.get(primitiveName);
}

@Override
public PrimitiveConfig addConfig(PrimitiveConfig config) {
checkNotNull(config, "config cannot be null");
PrimitiveConfig previous = configs.putIfAbsent(config.getName(), config);
return previous != null ? previous : config;
}
}
38 changes: 19 additions & 19 deletions core/src/main/java/io/atomix/core/impl/CorePrimitivesService.java
Expand Up @@ -20,10 +20,10 @@
import io.atomix.cluster.ClusterMembershipService; import io.atomix.cluster.ClusterMembershipService;
import io.atomix.cluster.messaging.ClusterCommunicationService; import io.atomix.cluster.messaging.ClusterCommunicationService;
import io.atomix.cluster.messaging.ClusterEventingService; import io.atomix.cluster.messaging.ClusterEventingService;
import io.atomix.core.AtomixConfig;
import io.atomix.core.ManagedPrimitivesService; import io.atomix.core.ManagedPrimitivesService;
import io.atomix.core.PrimitiveTypes; import io.atomix.core.PrimitiveTypes;
import io.atomix.core.PrimitivesService; import io.atomix.core.PrimitivesService;
import io.atomix.core.config.ConfigService;
import io.atomix.core.counter.AtomicCounter; import io.atomix.core.counter.AtomicCounter;
import io.atomix.core.election.LeaderElection; import io.atomix.core.election.LeaderElection;
import io.atomix.core.election.LeaderElector; import io.atomix.core.election.LeaderElector;
Expand Down Expand Up @@ -73,7 +73,7 @@ public class CorePrimitivesService implements ManagedPrimitivesService {
private final PrimitiveManagementService managementService; private final PrimitiveManagementService managementService;
private final ManagedPrimitiveRegistry primitiveRegistry; private final ManagedPrimitiveRegistry primitiveRegistry;
private final ManagedTransactionService transactionService; private final ManagedTransactionService transactionService;
private final AtomixConfig config; private final ConfigService configService;
private final Cache<String, DistributedPrimitive> cache = CacheBuilder.newBuilder() private final Cache<String, DistributedPrimitive> cache = CacheBuilder.newBuilder()
.maximumSize(CACHE_SIZE) .maximumSize(CACHE_SIZE)
.build(); .build();
Expand All @@ -86,7 +86,7 @@ public CorePrimitivesService(
ClusterEventingService eventService, ClusterEventingService eventService,
PartitionService partitionService, PartitionService partitionService,
RegistryService registryService, RegistryService registryService,
AtomixConfig config) { ConfigService configService) {
this.primitiveRegistry = new CorePrimitiveRegistry(partitionService, registryService.primitiveTypes()); this.primitiveRegistry = new CorePrimitiveRegistry(partitionService, registryService.primitiveTypes());
this.managementService = new CorePrimitiveManagementService( this.managementService = new CorePrimitiveManagementService(
executorService, executorService,
Expand All @@ -98,7 +98,7 @@ public CorePrimitivesService(
registryService.primitiveTypes(), registryService.primitiveTypes(),
registryService.protocolTypes()); registryService.protocolTypes());
this.transactionService = new CoreTransactionService(managementService); this.transactionService = new CoreTransactionService(managementService);
this.config = checkNotNull(config); this.configService = checkNotNull(configService);
} }


@Override @Override
Expand All @@ -108,72 +108,72 @@ public TransactionBuilder transactionBuilder(String name) {


@Override @Override
public <K, V> ConsistentMap<K, V> getConsistentMap(String name) { public <K, V> ConsistentMap<K, V> getConsistentMap(String name) {
return getPrimitive(name, PrimitiveTypes.consistentMap(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.consistentMap(), configService.getConfig(name));
} }


@Override @Override
public <V> DocumentTree<V> getDocumentTree(String name) { public <V> DocumentTree<V> getDocumentTree(String name) {
return getPrimitive(name, PrimitiveTypes.documentTree(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.documentTree(), configService.getConfig(name));
} }


@Override @Override
public <V> ConsistentTreeMap<V> getTreeMap(String name) { public <V> ConsistentTreeMap<V> getTreeMap(String name) {
return getPrimitive(name, PrimitiveTypes.consistentTreeMap(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.consistentTreeMap(), configService.getConfig(name));
} }


@Override @Override
public <K, V> ConsistentMultimap<K, V> getConsistentMultimap(String name) { public <K, V> ConsistentMultimap<K, V> getConsistentMultimap(String name) {
return getPrimitive(name, PrimitiveTypes.consistentMultimap(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.consistentMultimap(), configService.getConfig(name));
} }


@Override @Override
public <K> AtomicCounterMap<K> getAtomicCounterMap(String name) { public <K> AtomicCounterMap<K> getAtomicCounterMap(String name) {
return getPrimitive(name, PrimitiveTypes.atomicCounterMap(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.atomicCounterMap(), configService.getConfig(name));
} }


@Override @Override
public <E> DistributedSet<E> getSet(String name) { public <E> DistributedSet<E> getSet(String name) {
return getPrimitive(name, PrimitiveTypes.set(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.set(), configService.getConfig(name));
} }


@Override @Override
public AtomicCounter getAtomicCounter(String name) { public AtomicCounter getAtomicCounter(String name) {
return getPrimitive(name, PrimitiveTypes.atomicCounter(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.atomicCounter(), configService.getConfig(name));
} }


@Override @Override
public AtomicIdGenerator getAtomicIdGenerator(String name) { public AtomicIdGenerator getAtomicIdGenerator(String name) {
return getPrimitive(name, PrimitiveTypes.atomicIdGenerator(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.atomicIdGenerator(), configService.getConfig(name));
} }


@Override @Override
public <V> AtomicValue<V> getAtomicValue(String name) { public <V> AtomicValue<V> getAtomicValue(String name) {
return getPrimitive(name, PrimitiveTypes.atomicValue(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.atomicValue(), configService.getConfig(name));
} }


@Override @Override
public <T> LeaderElection<T> getLeaderElection(String name) { public <T> LeaderElection<T> getLeaderElection(String name) {
return getPrimitive(name, PrimitiveTypes.leaderElection(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.leaderElection(), configService.getConfig(name));
} }


@Override @Override
public <T> LeaderElector<T> getLeaderElector(String name) { public <T> LeaderElector<T> getLeaderElector(String name) {
return getPrimitive(name, PrimitiveTypes.leaderElector(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.leaderElector(), configService.getConfig(name));
} }


@Override @Override
public DistributedLock getLock(String name) { public DistributedLock getLock(String name) {
return getPrimitive(name, PrimitiveTypes.lock(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.lock(), configService.getConfig(name));
} }


@Override @Override
public DistributedSemaphore getSemaphore(String name) { public DistributedSemaphore getSemaphore(String name) {
return getPrimitive(name, PrimitiveTypes.semaphore(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.semaphore(), configService.getConfig(name));
} }


@Override @Override
public <E> WorkQueue<E> getWorkQueue(String name) { public <E> WorkQueue<E> getWorkQueue(String name) {
return getPrimitive(name, PrimitiveTypes.workQueue(), config.getPrimitive(name)); return getPrimitive(name, PrimitiveTypes.workQueue(), configService.getConfig(name));
} }


@Override @Override
Expand All @@ -192,7 +192,7 @@ public <P extends DistributedPrimitive> P getPrimitive(String name) {
return null; return null;
} }


PrimitiveConfig primitiveConfig = config.getPrimitive(name); PrimitiveConfig primitiveConfig = configService.getConfig(name);
if (primitiveConfig == null) { if (primitiveConfig == null) {
primitiveConfig = info.type().newConfig(); primitiveConfig = info.type().newConfig();
} }
Expand Down

0 comments on commit e5e1bac

Please sign in to comment.