Search in sources :

Example 1 with InstanceConfig

use of org.apache.cassandra.distributed.impl.InstanceConfig in project cassandra by apache.

the class ClusterUtils method updateAddress.

/**
 * Changes the instance's address to the new address.  This method should only be called while the instance is
 * down, else has undefined behavior.
 *
 * @param conf to update address for
 * @param address to set
 */
private static void updateAddress(IInstanceConfig conf, String address) {
    InetSocketAddress previous = conf.broadcastAddress();
    for (String key : Arrays.asList("broadcast_address", "listen_address", "broadcast_rpc_address", "rpc_address")) conf.set(key, address);
    // InstanceConfig caches InetSocketAddress -> InetAddressAndPort
    // this causes issues as startup now ignores config, so force reset it to pull from conf.
    // TODO remove the need to null out the cache...
    ((InstanceConfig) conf).unsetBroadcastAddressAndPort();
    // are a risk
    if (!conf.broadcastAddress().equals(previous)) {
        conf.networkTopology().put(conf.broadcastAddress(), NetworkTopology.dcAndRack(conf.localDatacenter(), conf.localRack()));
        try {
            Field field = NetworkTopology.class.getDeclaredField("map");
            field.setAccessible(true);
            Map<InetSocketAddress, NetworkTopology.DcAndRack> map = (Map<InetSocketAddress, NetworkTopology.DcAndRack>) field.get(conf.networkTopology());
            map.remove(previous);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new AssertionError(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) InstanceConfig(org.apache.cassandra.distributed.impl.InstanceConfig) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig) InetSocketAddress(java.net.InetSocketAddress) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with InstanceConfig

use of org.apache.cassandra.distributed.impl.InstanceConfig in project cassandra by apache.

the class ClusterUtils method addInstance.

/**
 * Create a new instance and add it to the cluster, without starting it.
 *
 * @param cluster to add to
 * @param dc the instance should be in
 * @param rack the instance should be in
 * @param fn function to add to the config before starting
 * @param <I> instance type
 * @return the instance added
 */
public static <I extends IInstance> I addInstance(AbstractCluster<I> cluster, String dc, String rack, Consumer<IInstanceConfig> fn) {
    Objects.requireNonNull(dc, "dc");
    Objects.requireNonNull(rack, "rack");
    InstanceConfig config = cluster.newInstanceConfig();
    // TODO adding new instances should be cleaner, currently requires you create the cluster with all
    // instances known about (at least to NetworkTopology and TokenStategy)
    // this is very hidden, so should be more explicit
    config.networkTopology().put(config.broadcastAddress(), NetworkTopology.dcAndRack(dc, rack));
    fn.accept(config);
    return cluster.bootstrap(config);
}
Also used : InstanceConfig(org.apache.cassandra.distributed.impl.InstanceConfig) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig)

Aggregations

IInstanceConfig (org.apache.cassandra.distributed.api.IInstanceConfig)2 InstanceConfig (org.apache.cassandra.distributed.impl.InstanceConfig)2 Field (java.lang.reflect.Field)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1