Search in sources :

Example 1 with MemorySize

use of io.atomix.utils.memory.MemorySize in project atomix by atomix.

the class ConfigMapper method getValue.

protected Object getValue(Class<?> beanClass, Type parameterType, Class<?> parameterClass, Config config, String configPath, String configPropName) {
    if (parameterClass == Boolean.class || parameterClass == boolean.class) {
        try {
            return config.getBoolean(configPropName);
        } catch (ConfigException.WrongType e) {
            return Boolean.parseBoolean(config.getString(configPropName));
        }
    } else if (parameterClass == Integer.class || parameterClass == int.class) {
        try {
            return config.getInt(configPropName);
        } catch (ConfigException.WrongType e) {
            try {
                return Integer.parseInt(config.getString(configPropName));
            } catch (NumberFormatException e1) {
                throw e;
            }
        }
    } else if (parameterClass == Double.class || parameterClass == double.class) {
        try {
            return config.getDouble(configPropName);
        } catch (ConfigException.WrongType e) {
            try {
                return Double.parseDouble(config.getString(configPropName));
            } catch (NumberFormatException e1) {
                throw e;
            }
        }
    } else if (parameterClass == Long.class || parameterClass == long.class) {
        try {
            return config.getLong(configPropName);
        } catch (ConfigException.WrongType e) {
            try {
                return Long.parseLong(config.getString(configPropName));
            } catch (NumberFormatException e1) {
                throw e;
            }
        }
    } else if (parameterClass == String.class) {
        return config.getString(configPropName);
    } else if (parameterClass == Duration.class) {
        return config.getDuration(configPropName);
    } else if (parameterClass == MemorySize.class) {
        ConfigMemorySize size = config.getMemorySize(configPropName);
        return new MemorySize(size.toBytes());
    } else if (parameterClass == Object.class) {
        return config.getAnyRef(configPropName);
    } else if (parameterClass == List.class || parameterClass == Collection.class) {
        return getListValue(beanClass, parameterType, parameterClass, config, configPath, configPropName);
    } else if (parameterClass == Set.class) {
        return getSetValue(beanClass, parameterType, parameterClass, config, configPath, configPropName);
    } else if (parameterClass == Map.class) {
        return getMapValue(beanClass, parameterType, parameterClass, config, configPath, configPropName);
    } else if (parameterClass == Config.class) {
        return config.getConfig(configPropName);
    } else if (parameterClass == ConfigObject.class) {
        return config.getObject(configPropName);
    } else if (parameterClass == ConfigValue.class) {
        return config.getValue(configPropName);
    } else if (parameterClass == ConfigList.class) {
        return config.getList(configPropName);
    } else if (parameterClass == Class.class) {
        String className = config.getString(configPropName);
        try {
            return classLoader.loadClass(className);
        } catch (ClassNotFoundException e) {
            throw new ConfigurationException("Failed to load class: " + className);
        }
    } else if (parameterClass.isEnum()) {
        String value = config.getString(configPropName);
        String enumName = value.replace("-", "_").toUpperCase();
        @SuppressWarnings("unchecked") Enum enumValue = Enum.valueOf((Class<Enum>) parameterClass, enumName);
        try {
            Deprecated deprecated = enumValue.getDeclaringClass().getField(enumName).getAnnotation(Deprecated.class);
            if (deprecated != null) {
                LOGGER.warn("{}.{} = {} is deprecated!", configPath, configPropName, value);
            }
        } catch (NoSuchFieldException e) {
        }
        return enumValue;
    } else {
        return map(config.getConfig(configPropName), configPath, configPropName, parameterClass);
    }
}
Also used : ConfigMemorySize(com.typesafe.config.ConfigMemorySize) MemorySize(io.atomix.utils.memory.MemorySize) ConfigValue(com.typesafe.config.ConfigValue) HashSet(java.util.HashSet) Set(java.util.Set) Config(com.typesafe.config.Config) ConfigException(com.typesafe.config.ConfigException) Duration(java.time.Duration) ConfigMemorySize(com.typesafe.config.ConfigMemorySize) Collection(java.util.Collection) ConfigObject(com.typesafe.config.ConfigObject) ConfigList(com.typesafe.config.ConfigList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with MemorySize

use of io.atomix.utils.memory.MemorySize in project atomix by atomix.

the class AtomixConfigTest method testAtomixConfig.

@Test
public void testAtomixConfig() throws Exception {
    AtomixConfig config = Atomix.config(getClass().getClassLoader().getResource("test.conf").getPath());
    ClusterConfig cluster = config.getClusterConfig();
    assertEquals("test", cluster.getClusterId());
    MemberConfig node = cluster.getNodeConfig();
    assertEquals("one", node.getId().id());
    assertEquals("localhost:5000", node.getAddress().toString());
    assertEquals("foo", node.getZoneId());
    assertEquals("bar", node.getRackId());
    assertEquals("baz", node.getHostId());
    assertEquals("bar", node.getProperties().getProperty("foo"));
    assertEquals("baz", node.getProperties().getProperty("bar"));
    MulticastConfig multicast = cluster.getMulticastConfig();
    assertTrue(multicast.isEnabled());
    assertEquals("230.0.1.1", multicast.getGroup().getHostAddress());
    assertEquals(56789, multicast.getPort());
    HeartbeatMembershipProtocolConfig protocol = (HeartbeatMembershipProtocolConfig) cluster.getProtocolConfig();
    assertEquals(Duration.ofMillis(200), protocol.getHeartbeatInterval());
    assertEquals(12, protocol.getPhiFailureThreshold());
    assertEquals(Duration.ofSeconds(15), protocol.getFailureTimeout());
    MembershipConfig membership = cluster.getMembershipConfig();
    assertEquals(Duration.ofSeconds(1), membership.getBroadcastInterval());
    assertEquals(12, membership.getReachabilityThreshold());
    assertEquals(Duration.ofSeconds(15), membership.getReachabilityTimeout());
    MulticastDiscoveryConfig discovery = (MulticastDiscoveryConfig) cluster.getDiscoveryConfig();
    assertEquals(MulticastDiscoveryProvider.TYPE, discovery.getType());
    assertEquals(Duration.ofSeconds(1), discovery.getBroadcastInterval());
    assertEquals(12, discovery.getFailureThreshold());
    assertEquals(Duration.ofSeconds(15), discovery.getFailureTimeout());
    MessagingConfig messaging = cluster.getMessagingConfig();
    assertEquals(2, messaging.getInterfaces().size());
    assertEquals("127.0.0.1", messaging.getInterfaces().get(0));
    assertEquals("0.0.0.0", messaging.getInterfaces().get(1));
    assertEquals(5000, messaging.getPort().intValue());
    assertEquals(Duration.ofSeconds(10), messaging.getConnectTimeout());
    assertTrue(messaging.getTlsConfig().isEnabled());
    assertEquals("keystore.jks", messaging.getTlsConfig().getKeyStore());
    assertEquals("foo", messaging.getTlsConfig().getKeyStorePassword());
    assertEquals("truststore.jks", messaging.getTlsConfig().getTrustStore());
    assertEquals("bar", messaging.getTlsConfig().getTrustStorePassword());
    RaftPartitionGroupConfig managementGroup = (RaftPartitionGroupConfig) config.getManagementGroup();
    assertEquals(RaftPartitionGroup.TYPE, managementGroup.getType());
    assertEquals(1, managementGroup.getPartitions());
    assertEquals(Duration.ofSeconds(5), managementGroup.getElectionTimeout());
    assertEquals(Duration.ofMillis(500), managementGroup.getHeartbeatInterval());
    assertEquals(Duration.ofSeconds(10), managementGroup.getDefaultSessionTimeout());
    assertEquals(new MemorySize(1024 * 1024 * 16), managementGroup.getStorageConfig().getSegmentSize());
    RaftPartitionGroupConfig groupOne = (RaftPartitionGroupConfig) config.getPartitionGroups().get("one");
    assertEquals(RaftPartitionGroup.TYPE, groupOne.getType());
    assertEquals("one", groupOne.getName());
    assertEquals(7, groupOne.getPartitions());
    PrimaryBackupPartitionGroupConfig groupTwo = (PrimaryBackupPartitionGroupConfig) config.getPartitionGroups().get("two");
    assertEquals(PrimaryBackupPartitionGroup.TYPE, groupTwo.getType());
    assertEquals("two", groupTwo.getName());
    assertEquals(32, groupTwo.getPartitions());
    LogPartitionGroupConfig groupThree = (LogPartitionGroupConfig) config.getPartitionGroups().get("three");
    assertEquals(LogPartitionGroup.TYPE, groupThree.getType());
    assertEquals("three", groupThree.getName());
    assertEquals(3, groupThree.getPartitions());
    ConsensusProfileConfig consensusProfile = (ConsensusProfileConfig) config.getProfiles().get(0);
    assertEquals(ConsensusProfile.TYPE, consensusProfile.getType());
    assertEquals("management", consensusProfile.getManagementGroup());
    assertEquals("consensus", consensusProfile.getDataGroup());
    assertEquals(3, consensusProfile.getPartitions());
    assertTrue(consensusProfile.getMembers().containsAll(Arrays.asList("one", "two", "three")));
    DataGridProfileConfig dataGridProfile = (DataGridProfileConfig) config.getProfiles().get(1);
    assertEquals(DataGridProfile.TYPE, dataGridProfile.getType());
    assertEquals("management", dataGridProfile.getManagementGroup());
    assertEquals("data", dataGridProfile.getDataGroup());
    assertEquals(32, dataGridProfile.getPartitions());
    AtomicMapConfig fooDefaults = config.getPrimitiveDefault("atomic-map");
    assertEquals("atomic-map", fooDefaults.getType().name());
    assertEquals("two", ((MultiPrimaryProtocolConfig) fooDefaults.getProtocolConfig()).getGroup());
    AtomicMapConfig foo = config.getPrimitive("foo");
    assertEquals("atomic-map", foo.getType().name());
    assertTrue(foo.isNullValues());
    DistributedSetConfig bar = config.getPrimitive("bar");
    assertTrue(bar.getCacheConfig().isEnabled());
    MultiPrimaryProtocolConfig multiPrimary = (MultiPrimaryProtocolConfig) bar.getProtocolConfig();
    assertEquals(MultiPrimaryProtocol.TYPE, multiPrimary.getType());
    assertEquals(Replication.SYNCHRONOUS, multiPrimary.getReplication());
    assertEquals(Duration.ofSeconds(1), multiPrimary.getRetryDelay());
    AtomicValueConfig baz = config.getPrimitive("baz");
    MultiRaftProtocolConfig multiRaft = (MultiRaftProtocolConfig) baz.getProtocolConfig();
    assertEquals(ReadConsistency.SEQUENTIAL, multiRaft.getReadConsistency());
    assertEquals(Recovery.RECOVER, multiRaft.getRecoveryStrategy());
    assertEquals(Duration.ofSeconds(2), multiRaft.getRetryDelay());
    DistributedLogConfig log = config.getPrimitive("log");
    assertEquals("log", log.getType().name());
    DistributedLogProtocolConfig logConfig = (DistributedLogProtocolConfig) log.getProtocolConfig();
    assertEquals(DistributedLogProtocol.TYPE, logConfig.getType());
    assertEquals("three", logConfig.getGroup());
}
Also used : MultiRaftProtocolConfig(io.atomix.protocols.raft.MultiRaftProtocolConfig) MemorySize(io.atomix.utils.memory.MemorySize) MembershipConfig(io.atomix.cluster.MembershipConfig) DistributedLogConfig(io.atomix.core.log.DistributedLogConfig) MulticastDiscoveryConfig(io.atomix.cluster.discovery.MulticastDiscoveryConfig) DistributedSetConfig(io.atomix.core.set.DistributedSetConfig) DistributedLogProtocolConfig(io.atomix.protocols.log.DistributedLogProtocolConfig) HeartbeatMembershipProtocolConfig(io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig) AtomicValueConfig(io.atomix.core.value.AtomicValueConfig) MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) AtomicMapConfig(io.atomix.core.map.AtomicMapConfig) RaftPartitionGroupConfig(io.atomix.protocols.raft.partition.RaftPartitionGroupConfig) PrimaryBackupPartitionGroupConfig(io.atomix.protocols.backup.partition.PrimaryBackupPartitionGroupConfig) ConsensusProfileConfig(io.atomix.core.profile.ConsensusProfileConfig) MultiPrimaryProtocolConfig(io.atomix.protocols.backup.MultiPrimaryProtocolConfig) MulticastConfig(io.atomix.cluster.MulticastConfig) MemberConfig(io.atomix.cluster.MemberConfig) DataGridProfileConfig(io.atomix.core.profile.DataGridProfileConfig) ClusterConfig(io.atomix.cluster.ClusterConfig) LogPartitionGroupConfig(io.atomix.protocols.log.partition.LogPartitionGroupConfig) Test(org.junit.Test)

Aggregations

MemorySize (io.atomix.utils.memory.MemorySize)2 Config (com.typesafe.config.Config)1 ConfigException (com.typesafe.config.ConfigException)1 ConfigList (com.typesafe.config.ConfigList)1 ConfigMemorySize (com.typesafe.config.ConfigMemorySize)1 ConfigObject (com.typesafe.config.ConfigObject)1 ConfigValue (com.typesafe.config.ConfigValue)1 ClusterConfig (io.atomix.cluster.ClusterConfig)1 MemberConfig (io.atomix.cluster.MemberConfig)1 MembershipConfig (io.atomix.cluster.MembershipConfig)1 MulticastConfig (io.atomix.cluster.MulticastConfig)1 MulticastDiscoveryConfig (io.atomix.cluster.discovery.MulticastDiscoveryConfig)1 MessagingConfig (io.atomix.cluster.messaging.MessagingConfig)1 HeartbeatMembershipProtocolConfig (io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig)1 DistributedLogConfig (io.atomix.core.log.DistributedLogConfig)1 AtomicMapConfig (io.atomix.core.map.AtomicMapConfig)1 ConsensusProfileConfig (io.atomix.core.profile.ConsensusProfileConfig)1 DataGridProfileConfig (io.atomix.core.profile.DataGridProfileConfig)1 DistributedSetConfig (io.atomix.core.set.DistributedSetConfig)1 AtomicValueConfig (io.atomix.core.value.AtomicValueConfig)1