Skip to content

Commit

Permalink
Use classpath scanning to locate and register primitives, protocols, …
Browse files Browse the repository at this point in the history
…and partition groups.
  • Loading branch information
kuujo committed Jun 4, 2018
1 parent 9699ef1 commit 5dda460
Show file tree
Hide file tree
Showing 174 changed files with 2,394 additions and 2,523 deletions.
3 changes: 0 additions & 3 deletions agent/pom.xml
Expand Up @@ -110,9 +110,6 @@
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>registry.conf</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.atomix.agent.AtomixAgent</mainClass>
</transformer>
Expand Down
84 changes: 13 additions & 71 deletions core/src/main/java/io/atomix/core/Atomix.java
Expand Up @@ -34,8 +34,6 @@
import io.atomix.core.multimap.ConsistentMultimap;
import io.atomix.core.profile.Profile;
import io.atomix.core.queue.WorkQueue;
import io.atomix.core.registry.AtomixRegistry;
import io.atomix.core.registry.impl.ImmutableAtomixRegistry;
import io.atomix.core.semaphore.DistributedSemaphore;
import io.atomix.core.set.DistributedSet;
import io.atomix.core.transaction.TransactionBuilder;
Expand All @@ -45,7 +43,6 @@
import io.atomix.core.utils.config.PolymorphicConfigMapper;
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;
Expand All @@ -55,15 +52,13 @@
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;
Expand Down Expand Up @@ -103,6 +98,7 @@ public static AtomixConfig config() {
/**
* Returns a new Atomix configuration.
*
* @param classLoader the class loader
* @return a new Atomix configuration
*/
public static AtomixConfig config(ClassLoader classLoader) {
Expand Down Expand Up @@ -143,8 +139,7 @@ private static AtomixConfig config(String[] resources, ClassLoader classLoader,
registry,
new PartitionGroupConfigMapper(),
new PrimitiveConfigMapper(),
new PrimitiveProtocolConfigMapper(),
new ProfileMapper());
new PrimitiveProtocolConfigMapper());
return mapper.loadResources(AtomixConfig.class, resources);
}

Expand Down Expand Up @@ -240,11 +235,11 @@ public Atomix(File configFile, ClassLoader classLoader) {

private Atomix(AtomixConfig config, AtomixRegistry registry) {
super(config.getClusterConfig());
config.getProfiles().forEach(profile -> registry.profileTypes().getProfileType(profile).newProfile().configure(config));
config.getProfiles().forEach(profile -> registry.profiles().getProfile(profile).configure(config));
this.executorService = Executors.newScheduledThreadPool(
Runtime.getRuntime().availableProcessors(),
Threads.namedThreads("atomix-primitive-%d", LOGGER));
this.registry = new ImmutableAtomixRegistry(registry);
this.registry = registry;
this.config = new DefaultConfigService(config.getPrimitives().values());
this.partitions = buildPartitionService(config, membershipService(), communicationService(), registry);
this.primitives = new CorePrimitivesService(
Expand Down Expand Up @@ -395,15 +390,10 @@ public <E> WorkQueue<E> getWorkQueue(String name) {
}

@Override
public <P extends DistributedPrimitive> P getPrimitive(String name, String primitiveType) {
public <P extends DistributedPrimitive> P getPrimitive(String name, PrimitiveType<?, ?, P> 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);
Expand Down Expand Up @@ -499,17 +489,12 @@ public String toString() {
* Builds the core partition group.
*/
@SuppressWarnings("unchecked")
private static ManagedPartitionGroup buildSystemPartitionGroup(AtomixConfig config, AtomixRegistry registry) {
PartitionGroupConfig partitionGroupConfig = config.getManagementGroup();
private static ManagedPartitionGroup buildSystemPartitionGroup(AtomixConfig config) {
PartitionGroupConfig<?> partitionGroupConfig = config.getManagementGroup();
if (partitionGroupConfig == null) {
return null;
}

PartitionGroupType partitionGroupType = registry.partitionGroupTypes().getGroupType(partitionGroupConfig.getType());
if (partitionGroupType == null) {
throw new ConfigurationException("Unknown partition group type " + partitionGroupConfig.getType());
}
return partitionGroupType.newGroup(partitionGroupConfig);
return partitionGroupConfig.getType().newPartitionGroup(partitionGroupConfig);
}

/**
Expand All @@ -522,19 +507,15 @@ private static ManagedPartitionService buildPartitionService(
ClusterCommunicationService messagingService,
AtomixRegistry registry) {
List<ManagedPartitionGroup> partitionGroups = new ArrayList<>();
for (PartitionGroupConfig partitionGroupConfig : config.getPartitionGroups().values()) {
PartitionGroupType partitionGroupType = registry.partitionGroupTypes().getGroupType(partitionGroupConfig.getType());
if (partitionGroupType == null) {
throw new ConfigurationException("Unknown partition group type " + partitionGroupConfig.getType());
}
partitionGroups.add(partitionGroupType.newGroup(partitionGroupConfig));
for (PartitionGroupConfig<?> partitionGroupConfig : config.getPartitionGroups().values()) {
partitionGroups.add(partitionGroupConfig.getType().newPartitionGroup(partitionGroupConfig));
}

return new DefaultPartitionService(
clusterMembershipService,
messagingService,
registry.primitiveTypes(),
buildSystemPartitionGroup(config, registry),
buildSystemPartitionGroup(config),
partitionGroups,
registry.partitionGroupTypes());
}
Expand Down Expand Up @@ -589,10 +570,7 @@ public Builder withProfiles(Profile... profiles) {
* @return the Atomix builder
*/
public Builder withProfiles(Collection<Profile> profiles) {
profiles.forEach(profile -> {
registry.profileTypes().addProfileType(profile.type());
config.addProfile(profile.type().name());
});
profiles.forEach(profile -> config.addProfile(profile.name()));
return this;
}

Expand All @@ -603,8 +581,7 @@ public Builder withProfiles(Collection<Profile> profiles) {
* @return the Atomix builder
*/
public Builder addProfile(Profile profile) {
registry.profileTypes().addProfileType(profile.type());
config.addProfile(profile.type().name());
config.addProfile(profile.name());
return this;
}

Expand Down Expand Up @@ -654,41 +631,6 @@ public Builder addPartitionGroup(ManagedPartitionGroup partitionGroup) {
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 -> registry.primitiveTypes().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) {
registry.primitiveTypes().addPrimitiveType(primitiveType);
return this;
}

@Override
public Builder withClusterName(String clusterName) {
super.withClusterName(clusterName);
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/io/atomix/core/AtomixConfig.java
Expand Up @@ -36,7 +36,7 @@ public class AtomixConfig implements Config {
private ClusterConfig cluster = new ClusterConfig();
private boolean enableShutdownHook;
private PartitionGroupConfig managementGroup;
private Map<String, PartitionGroupConfig> partitionGroups = new HashMap<>();
private Map<String, PartitionGroupConfig<?>> partitionGroups = new HashMap<>();
private Map<String, PrimitiveConfig> primitives = new HashMap<>();
private List<String> profiles = new ArrayList<>();

Expand Down Expand Up @@ -85,7 +85,7 @@ public AtomixConfig setEnableShutdownHook(boolean enableShutdownHook) {
*
* @return the system management partition group
*/
public PartitionGroupConfig getManagementGroup() {
public PartitionGroupConfig<?> getManagementGroup() {
return managementGroup;
}

Expand All @@ -95,7 +95,7 @@ public PartitionGroupConfig getManagementGroup() {
* @param managementGroup the system management partition group
* @return the Atomix configuration
*/
public AtomixConfig setManagementGroup(PartitionGroupConfig managementGroup) {
public AtomixConfig setManagementGroup(PartitionGroupConfig<?> managementGroup) {
managementGroup.setName(MANAGEMENT_GROUP_NAME);
this.managementGroup = managementGroup;
return this;
Expand All @@ -106,7 +106,7 @@ public AtomixConfig setManagementGroup(PartitionGroupConfig managementGroup) {
*
* @return the partition group configurations
*/
public Map<String, PartitionGroupConfig> getPartitionGroups() {
public Map<String, PartitionGroupConfig<?>> getPartitionGroups() {
return partitionGroups;
}

Expand All @@ -116,7 +116,7 @@ public Map<String, PartitionGroupConfig> getPartitionGroups() {
* @param partitionGroups the partition group configurations
* @return the Atomix configuration
*/
public AtomixConfig setPartitionGroups(Map<String, PartitionGroupConfig> partitionGroups) {
public AtomixConfig setPartitionGroups(Map<String, PartitionGroupConfig<?>> partitionGroups) {
partitionGroups.forEach((name, group) -> group.setName(name));
this.partitionGroups = partitionGroups;
return this;
Expand Down
Expand Up @@ -13,14 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.core.registry;
package io.atomix.core;

import io.atomix.core.profile.ProfileTypeRegistry;
import io.atomix.core.registry.impl.DefaultAtomixRegistry;
import io.atomix.core.impl.ClasspathScanningAtomixRegistry;
import io.atomix.core.profile.ProfileRegistry;
import io.atomix.primitive.PrimitiveTypeRegistry;
import io.atomix.primitive.partition.PartitionGroupTypeRegistry;
import io.atomix.primitive.protocol.PrimitiveProtocolTypeRegistry;
import io.atomix.utils.config.ConfigMapper;

/**
* Atomix registry.
Expand All @@ -43,9 +42,7 @@ static AtomixRegistry registry() {
* @return the registry instance
*/
static AtomixRegistry registry(ClassLoader classLoader) {
ConfigMapper mapper = new ConfigMapper(classLoader);
RegistryConfig config = mapper.loadResources(RegistryConfig.class, "registry");
return new DefaultAtomixRegistry(config);
return new ClasspathScanningAtomixRegistry(classLoader);
}

/**
Expand Down Expand Up @@ -74,6 +71,6 @@ static AtomixRegistry registry(ClassLoader classLoader) {
*
* @return the registered profile types
*/
ProfileTypeRegistry profileTypes();
ProfileRegistry profiles();

}

0 comments on commit 5dda460

Please sign in to comment.