Search in sources :

Example 6 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class MultiPrimaryProtocol method newProxy.

@Override
public <S> ProxyClient<S> newProxy(String primitiveName, PrimitiveType primitiveType, Class<S> serviceType, ServiceConfig serviceConfig, PartitionService partitionService) {
    PartitionGroup partitionGroup = partitionService.getPartitionGroup(this);
    if (partitionGroup == null) {
        throw new ConfigurationException("No Raft partition group matching the configured protocol exists");
    }
    Collection<SessionClient> partitions = partitionGroup.getPartitions().stream().map(partition -> ((PrimaryBackupPartition) partition).getClient().sessionBuilder(primitiveName, primitiveType, serviceConfig).withConsistency(config.getConsistency()).withReplication(config.getReplication()).withRecovery(config.getRecovery()).withNumBackups(config.getBackups()).withMaxRetries(config.getMaxRetries()).withRetryDelay(config.getRetryDelay()).build()).collect(Collectors.toList());
    return new DefaultProxyClient<>(primitiveName, primitiveType, this, serviceType, partitions, config.getPartitioner());
}
Also used : PartitionGroup(io.atomix.primitive.partition.PartitionGroup) ProxyProtocol(io.atomix.primitive.protocol.ProxyProtocol) ProxyClient(io.atomix.primitive.proxy.ProxyClient) PrimaryBackupPartition(io.atomix.protocols.backup.partition.PrimaryBackupPartition) Collection(java.util.Collection) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Collectors(java.util.stream.Collectors) ConfigurationException(io.atomix.utils.config.ConfigurationException) ServiceConfig(io.atomix.primitive.service.ServiceConfig) PrimitiveType(io.atomix.primitive.PrimitiveType) PartitionService(io.atomix.primitive.partition.PartitionService) PrimitiveProtocol(io.atomix.primitive.protocol.PrimitiveProtocol) DefaultProxyClient(io.atomix.primitive.proxy.impl.DefaultProxyClient) SessionClient(io.atomix.primitive.session.SessionClient) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) DefaultProxyClient(io.atomix.primitive.proxy.impl.DefaultProxyClient) ConfigurationException(io.atomix.utils.config.ConfigurationException) SessionClient(io.atomix.primitive.session.SessionClient) PrimaryBackupPartition(io.atomix.protocols.backup.partition.PrimaryBackupPartition)

Example 7 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class Namespace method buildRegistrationBlocks.

@SuppressWarnings("unchecked")
private static List<RegistrationBlock> buildRegistrationBlocks(NamespaceConfig config) {
    List<Pair<Class<?>[], Serializer<?>>> types = new ArrayList<>();
    List<RegistrationBlock> blocks = new ArrayList<>();
    blocks.addAll(Namespaces.BASIC.registeredBlocks);
    for (NamespaceTypeConfig type : config.getTypes()) {
        try {
            if (type.getId() == null) {
                types.add(Pair.of(new Class[] { type.getType() }, type.getSerializer() != null ? type.getSerializer().newInstance() : null));
            } else {
                blocks.add(new RegistrationBlock(type.getId(), Collections.singletonList(Pair.of(new Class[] { type.getType() }, type.getSerializer().newInstance()))));
            }
        } catch (InstantiationException | IllegalAccessException e) {
            throw new ConfigurationException("Failed to instantiate serializer from configuration", e);
        }
    }
    blocks.add(new RegistrationBlock(FLOATING_ID, types));
    return blocks;
}
Also used : ConfigurationException(io.atomix.utils.config.ConfigurationException) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair)

Example 8 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class AtomixTest method testPrimitiveConfigurations.

@Test
public void testPrimitiveConfigurations() throws Exception {
    IntStream.range(1, 4).forEach(i -> instances.add(Atomix.builder(getClass().getResource("/primitives.conf").getFile()).withMemberId(String.valueOf(i)).withHost("localhost").withPort(5000 + i).withProfiles(ConsensusProfile.builder().withMembers("1", "2", "3").withDataPath(new File(new File(DATA_DIR, "primitive-getters"), String.valueOf(i))).build()).build()));
    Futures.allOf(instances.stream().map(Atomix::start)).get(30, TimeUnit.SECONDS);
    Atomix atomix = Atomix.builder(getClass().getResource("/primitives.conf").getFile()).withHost("localhost").withPort(5000).build();
    instances.add(atomix);
    // try {
    // atomix.getAtomicCounter("foo");
    // fail();
    // } catch (IllegalStateException e) {
    // }
    atomix.start().get(30, TimeUnit.SECONDS);
    try {
        atomix.mapBuilder("foo").withProtocol(MultiRaftProtocol.builder("wrong").build()).get();
        fail();
    } catch (Exception e) {
        assertTrue(e instanceof ConfigurationException);
    }
    try {
        atomix.mapBuilder("bar").withProtocol(MultiRaftProtocol.builder("wrong").build()).build();
        fail();
    } catch (Exception e) {
        assertTrue(e instanceof ConfigurationException);
    }
    assertEquals(AtomicCounterType.instance(), atomix.getPrimitive("atomic-counter", AtomicCounterType.instance()).type());
    assertEquals("atomic-counter", atomix.getAtomicCounter("atomic-counter").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicCounter("atomic-counter").protocol()).group());
    assertEquals(AtomicMapType.instance(), atomix.getPrimitive("atomic-map", AtomicMapType.instance()).type());
    assertEquals("atomic-map", atomix.getAtomicMap("atomic-map").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicMap("atomic-map").protocol()).group());
    assertEquals(AtomicCounterMapType.instance(), atomix.getPrimitive("atomic-counter-map", AtomicCounterMapType.instance()).type());
    assertEquals("atomic-counter-map", atomix.getAtomicCounterMap("atomic-counter-map").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicCounterMap("atomic-counter-map").protocol()).group());
    assertEquals(AtomicDocumentTreeType.instance(), atomix.getPrimitive("atomic-document-tree", AtomicDocumentTreeType.instance()).type());
    assertEquals("atomic-document-tree", atomix.getAtomicDocumentTree("atomic-document-tree").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicDocumentTree("atomic-document-tree").protocol()).group());
    assertEquals(AtomicIdGeneratorType.instance(), atomix.getPrimitive("atomic-id-generator", AtomicIdGeneratorType.instance()).type());
    assertEquals("atomic-id-generator", atomix.getAtomicIdGenerator("atomic-id-generator").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicIdGenerator("atomic-id-generator").protocol()).group());
    assertEquals(AtomicLockType.instance(), atomix.getPrimitive("atomic-lock", AtomicLockType.instance()).type());
    assertEquals("atomic-lock", atomix.getAtomicLock("atomic-lock").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicLock("atomic-lock").protocol()).group());
    assertEquals(AtomicMultimapType.instance(), atomix.getPrimitive("atomic-multimap", AtomicMultimapType.instance()).type());
    assertEquals("atomic-multimap", atomix.getAtomicMultimap("atomic-multimap").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicMultimap("atomic-multimap").protocol()).group());
    assertEquals(AtomicNavigableMapType.instance(), atomix.getPrimitive("atomic-navigable-map", AtomicNavigableMapType.instance()).type());
    assertEquals("atomic-navigable-map", atomix.getAtomicNavigableMap("atomic-navigable-map").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicNavigableMap("atomic-navigable-map").protocol()).group());
    assertEquals(AtomicSemaphoreType.instance(), atomix.getPrimitive("atomic-semaphore", AtomicSemaphoreType.instance()).type());
    assertEquals("atomic-semaphore", atomix.getAtomicSemaphore("atomic-semaphore").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicSemaphore("atomic-semaphore").protocol()).group());
    assertEquals(AtomicSortedMapType.instance(), atomix.getPrimitive("atomic-sorted-map", AtomicSortedMapType.instance()).type());
    assertEquals("atomic-sorted-map", atomix.getAtomicSortedMap("atomic-sorted-map").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicSortedMap("atomic-sorted-map").protocol()).group());
    assertEquals(AtomicValueType.instance(), atomix.getPrimitive("atomic-value", AtomicValueType.instance()).type());
    assertEquals("atomic-value", atomix.getAtomicValue("atomic-value").name());
    assertEquals("two", ((ProxyProtocol) atomix.getAtomicValue("atomic-value").protocol()).group());
    assertEquals(DistributedCounterType.instance(), atomix.getPrimitive("counter", DistributedCounterType.instance()).type());
    assertEquals("counter", atomix.getCounter("counter").name());
    assertEquals("two", ((ProxyProtocol) atomix.getCounter("counter").protocol()).group());
    assertEquals(DistributedCyclicBarrierType.instance(), atomix.getPrimitive("cyclic-barrier", DistributedCyclicBarrierType.instance()).type());
    assertEquals("cyclic-barrier", atomix.getCyclicBarrier("cyclic-barrier").name());
    assertEquals("two", ((ProxyProtocol) atomix.getCyclicBarrier("cyclic-barrier").protocol()).group());
    assertEquals(LeaderElectionType.instance(), atomix.getPrimitive("leader-election", LeaderElectionType.instance()).type());
    assertEquals("leader-election", atomix.getLeaderElection("leader-election").name());
    assertEquals("two", ((ProxyProtocol) atomix.getLeaderElection("leader-election").protocol()).group());
    assertEquals(LeaderElectorType.instance(), atomix.getPrimitive("leader-elector", LeaderElectorType.instance()).type());
    assertEquals("leader-elector", atomix.getLeaderElector("leader-elector").name());
    assertEquals("two", ((ProxyProtocol) atomix.getLeaderElector("leader-elector").protocol()).group());
    assertEquals(DistributedListType.instance(), atomix.getPrimitive("list", DistributedListType.instance()).type());
    assertEquals("list", atomix.getList("list").name());
    assertEquals("two", ((ProxyProtocol) atomix.getList("list").protocol()).group());
    assertEquals(DistributedLockType.instance(), atomix.getPrimitive("lock", DistributedLockType.instance()).type());
    assertEquals("lock", atomix.getLock("lock").name());
    assertEquals("two", ((ProxyProtocol) atomix.getLock("lock").protocol()).group());
    assertEquals(DistributedMapType.instance(), atomix.getPrimitive("map", DistributedMapType.instance()).type());
    assertEquals("map", atomix.getMap("map").name());
    assertEquals("two", ((ProxyProtocol) atomix.getMap("map").protocol()).group());
    assertEquals(DistributedMultimapType.instance(), atomix.getPrimitive("multimap", DistributedMultimapType.instance()).type());
    assertEquals("multimap", atomix.getMultimap("multimap").name());
    assertEquals("two", ((ProxyProtocol) atomix.getMultimap("multimap").protocol()).group());
    assertEquals(DistributedMultisetType.instance(), atomix.getPrimitive("multiset", DistributedMultisetType.instance()).type());
    assertEquals("multiset", atomix.getMultiset("multiset").name());
    assertEquals("two", ((ProxyProtocol) atomix.getMultiset("multiset").protocol()).group());
    assertEquals(DistributedNavigableMapType.instance(), atomix.getPrimitive("navigable-map", DistributedNavigableMapType.instance()).type());
    assertEquals("navigable-map", atomix.getNavigableMap("navigable-map").name());
    assertEquals("two", ((ProxyProtocol) atomix.getNavigableMap("navigable-map").protocol()).group());
    assertEquals(DistributedNavigableSetType.instance(), atomix.getPrimitive("navigable-set", DistributedNavigableSetType.instance()).type());
    assertEquals("navigable-set", atomix.getNavigableSet("navigable-set").name());
    assertEquals("two", ((ProxyProtocol) atomix.getNavigableSet("navigable-set").protocol()).group());
    assertEquals(DistributedQueueType.instance(), atomix.getPrimitive("queue", DistributedQueueType.instance()).type());
    assertEquals("queue", atomix.getQueue("queue").name());
    assertEquals("two", ((ProxyProtocol) atomix.getQueue("queue").protocol()).group());
    assertEquals(DistributedSemaphoreType.instance(), atomix.getPrimitive("semaphore", DistributedSemaphoreType.instance()).type());
    assertEquals("semaphore", atomix.getSemaphore("semaphore").name());
    assertEquals("two", ((ProxyProtocol) atomix.getSemaphore("semaphore").protocol()).group());
    assertEquals(DistributedSetType.instance(), atomix.getPrimitive("set", DistributedSetType.instance()).type());
    assertEquals("set", atomix.getSet("set").name());
    assertEquals("two", ((ProxyProtocol) atomix.getSet("set").protocol()).group());
    assertEquals(DistributedSortedMapType.instance(), atomix.getPrimitive("sorted-map", DistributedSortedMapType.instance()).type());
    assertEquals("sorted-map", atomix.getSortedMap("sorted-map").name());
    assertEquals("two", ((ProxyProtocol) atomix.getSortedMap("sorted-map").protocol()).group());
    assertEquals(DistributedSortedSetType.instance(), atomix.getPrimitive("sorted-set", DistributedSortedSetType.instance()).type());
    assertEquals("sorted-set", atomix.getSortedSet("sorted-set").name());
    assertEquals("two", ((ProxyProtocol) atomix.getSortedSet("sorted-set").protocol()).group());
    assertEquals(DistributedValueType.instance(), atomix.getPrimitive("value", DistributedValueType.instance()).type());
    assertEquals("value", atomix.getValue("value").name());
    assertEquals("two", ((ProxyProtocol) atomix.getValue("value").protocol()).group());
    assertEquals(WorkQueueType.instance(), atomix.getPrimitive("work-queue", WorkQueueType.instance()).type());
    assertEquals("work-queue", atomix.getWorkQueue("work-queue").name());
    assertEquals("two", ((ProxyProtocol) atomix.getWorkQueue("work-queue").protocol()).group());
}
Also used : ConfigurationException(io.atomix.utils.config.ConfigurationException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException) ConfigurationException(io.atomix.utils.config.ConfigurationException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 9 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class PolymorphicConfigMapper method checkRemainingProperties.

@Override
protected void checkRemainingProperties(Set<String> missingProperties, List<String> availableProperties, String path, Class<?> clazz) {
    Properties properties = new Properties();
    properties.putAll(System.getProperties());
    List<String> cleanNames = missingProperties.stream().filter(propertyName -> !isPolymorphicType(clazz) || !polymorphicTypes.stream().anyMatch(type -> Objects.equals(type.getTypePath(), propertyName))).map(propertyName -> toPath(path, propertyName)).filter(propertyName -> !properties.containsKey(propertyName)).filter(propertyName -> properties.entrySet().stream().noneMatch(entry -> entry.getKey().toString().startsWith(propertyName + "."))).sorted().collect(Collectors.toList());
    if (!cleanNames.isEmpty()) {
        throw new ConfigurationException("Unknown properties present in configuration: " + Joiner.on(", ").join(cleanNames) + "\n" + "Available properties:\n- " + Joiner.on("\n- ").join(availableProperties));
    }
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) AtomixRegistry(io.atomix.core.AtomixRegistry) Config(com.typesafe.config.Config) TypedConfig(io.atomix.utils.config.TypedConfig) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Collectors(java.util.stream.Collectors) ConfigurationException(io.atomix.utils.config.ConfigurationException) ConfigMapper(io.atomix.utils.config.ConfigMapper) Objects(java.util.Objects) List(java.util.List) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) ConfigurationException(io.atomix.utils.config.ConfigurationException) Properties(java.util.Properties)

Example 10 with ConfigurationException

use of io.atomix.utils.config.ConfigurationException in project atomix by atomix.

the class PolymorphicConfigMapper method newInstance.

@Override
@SuppressWarnings("unchecked")
protected <T> T newInstance(Config config, String key, Class<T> clazz) {
    T instance;
    // If the class is a polymorphic type, look up the type mapper and get the concrete type.
    if (isPolymorphicType(clazz)) {
        PolymorphicTypeMapper typeMapper = polymorphicTypes.stream().filter(mapper -> mapper.getConfigClass().isAssignableFrom(clazz)).filter(mapper -> (mapper.getTypePath() != null && config.hasPath(mapper.getTypePath())) || mapper.getTypePath() == null).findFirst().orElse(null);
        if (typeMapper == null) {
            throw new ConfigurationException("Cannot instantiate abstract type " + clazz.getName());
        }
        String typeName = typeMapper.getTypePath() != null ? config.getString(typeMapper.getTypePath()) : key;
        Class<? extends TypedConfig<?>> concreteClass = typeMapper.getConcreteClass(registry, typeName);
        if (concreteClass == null) {
            throw new ConfigurationException("Unknown " + key + " type '" + typeName + "'");
        }
        try {
            instance = (T) concreteClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new ConfigurationException(concreteClass.getName() + " needs a public no-args constructor to be used as a bean", e);
        }
    } else {
        try {
            instance = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new ConfigurationException(clazz.getName() + " needs a public no-args constructor to be used as a bean", e);
        }
    }
    return instance;
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) AtomixRegistry(io.atomix.core.AtomixRegistry) Config(com.typesafe.config.Config) TypedConfig(io.atomix.utils.config.TypedConfig) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Collectors(java.util.stream.Collectors) ConfigurationException(io.atomix.utils.config.ConfigurationException) ConfigMapper(io.atomix.utils.config.ConfigMapper) Objects(java.util.Objects) List(java.util.List) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) ConfigurationException(io.atomix.utils.config.ConfigurationException)

Aggregations

ConfigurationException (io.atomix.utils.config.ConfigurationException)11 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 PrimitiveType (io.atomix.primitive.PrimitiveType)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3 PartitionGroup (io.atomix.primitive.partition.PartitionGroup)3 PartitionService (io.atomix.primitive.partition.PartitionService)3 List (java.util.List)3 Set (java.util.Set)3 Joiner (com.google.common.base.Joiner)2 Config (com.typesafe.config.Config)2 AtomixRegistry (io.atomix.core.AtomixRegistry)2 PartitionGroupMembership (io.atomix.primitive.partition.PartitionGroupMembership)2 PartitionGroupMembershipEvent (io.atomix.primitive.partition.PartitionGroupMembershipEvent)2 PrimitiveProtocol (io.atomix.primitive.protocol.PrimitiveProtocol)2 ConfigMapper (io.atomix.utils.config.ConfigMapper)2 TypedConfig (io.atomix.utils.config.TypedConfig)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Map (java.util.Map)2