use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class LeaveAndBootstrapTest method testStateJumpToBootstrap.
@Test
public void testStateJumpToBootstrap() throws UnknownHostException {
StorageService ss = StorageService.instance;
TokenMetadata tmd = ss.getTokenMetadata();
tmd.clearUnsafe();
IPartitioner partitioner = RandomPartitioner.instance;
VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
ArrayList<Token> endpointTokens = new ArrayList<Token>();
ArrayList<Token> keyTokens = new ArrayList<Token>();
List<InetAddress> hosts = new ArrayList<InetAddress>();
List<UUID> hostIds = new ArrayList<UUID>();
// create a ring or 5 nodes
Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 7);
// node 2 leaves
ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(endpointTokens.get(2))));
// don't bother to test pending ranges here, that is extensively tested by other
// tests. Just check that the node is in appropriate lists.
assertTrue(tmd.isMember(hosts.get(2)));
assertTrue(tmd.isLeaving(hosts.get(2)));
assertTrue(tmd.getBootstrapTokens().isEmpty());
// Bootstrap the node immedidiately to keyTokens.get(4) without going through STATE_LEFT
Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(4))));
ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(4))));
assertFalse(tmd.isMember(hosts.get(2)));
assertFalse(tmd.isLeaving(hosts.get(2)));
assertEquals(hosts.get(2), tmd.getBootstrapTokens().get(keyTokens.get(4)));
// Bootstrap node hosts.get(3) to keyTokens.get(1)
Gossiper.instance.injectApplicationState(hosts.get(3), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(1))));
ss.onChange(hosts.get(3), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(1))));
assertFalse(tmd.isMember(hosts.get(3)));
assertFalse(tmd.isLeaving(hosts.get(3)));
assertEquals(hosts.get(2), tmd.getBootstrapTokens().get(keyTokens.get(4)));
assertEquals(hosts.get(3), tmd.getBootstrapTokens().get(keyTokens.get(1)));
// Bootstrap node hosts.get(2) further to keyTokens.get(3)
Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(3))));
ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(3))));
assertFalse(tmd.isMember(hosts.get(2)));
assertFalse(tmd.isLeaving(hosts.get(2)));
assertEquals(hosts.get(2), tmd.getBootstrapTokens().get(keyTokens.get(3)));
assertNull(tmd.getBootstrapTokens().get(keyTokens.get(4)));
assertEquals(hosts.get(3), tmd.getBootstrapTokens().get(keyTokens.get(1)));
// Go to normal again for both nodes
Gossiper.instance.injectApplicationState(hosts.get(3), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(2))));
Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(3))));
ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(keyTokens.get(3))));
ss.onChange(hosts.get(3), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(keyTokens.get(2))));
assertTrue(tmd.isMember(hosts.get(2)));
assertFalse(tmd.isLeaving(hosts.get(2)));
assertEquals(keyTokens.get(3), tmd.getToken(hosts.get(2)));
assertTrue(tmd.isMember(hosts.get(3)));
assertFalse(tmd.isLeaving(hosts.get(3)));
assertEquals(keyTokens.get(2), tmd.getToken(hosts.get(3)));
assertTrue(tmd.getBootstrapTokens().isEmpty());
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class LeaveAndBootstrapTest method newTestWriteEndpointsDuringLeave.
/**
* Test whether write endpoints is correct when the node is leaving. Uses
* StorageService.onChange and does not manipulate token metadata directly.
*/
@Test
public void newTestWriteEndpointsDuringLeave() throws Exception {
StorageService ss = StorageService.instance;
final int RING_SIZE = 6;
final int LEAVING_NODE = 3;
TokenMetadata tmd = ss.getTokenMetadata();
tmd.clearUnsafe();
IPartitioner partitioner = RandomPartitioner.instance;
VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
ArrayList<Token> endpointTokens = new ArrayList<Token>();
ArrayList<Token> keyTokens = new ArrayList<Token>();
List<InetAddress> hosts = new ArrayList<InetAddress>();
List<UUID> hostIds = new ArrayList<UUID>();
Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
Map<Token, List<InetAddress>> expectedEndpoints = new HashMap<Token, List<InetAddress>>();
for (String keyspaceName : Schema.instance.getNonLocalStrategyKeyspaces()) {
for (Token token : keyTokens) {
List<InetAddress> endpoints = new ArrayList<InetAddress>();
Iterator<Token> tokenIter = TokenMetadata.ringIterator(tmd.sortedTokens(), token, false);
while (tokenIter.hasNext()) {
endpoints.add(tmd.getEndpoint(tokenIter.next()));
}
expectedEndpoints.put(token, endpoints);
}
}
// Third node leaves
ss.onChange(hosts.get(LEAVING_NODE), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(endpointTokens.get(LEAVING_NODE))));
assertTrue(tmd.isLeaving(hosts.get(LEAVING_NODE)));
// because there is a tight race between submit and blockUntilFinished
Thread.sleep(100);
PendingRangeCalculatorService.instance.blockUntilFinished();
AbstractReplicationStrategy strategy;
for (String keyspaceName : Schema.instance.getNonLocalStrategyKeyspaces()) {
strategy = getStrategy(keyspaceName, tmd);
for (Token token : keyTokens) {
int replicationFactor = strategy.getReplicationFactor();
HashSet<InetAddress> actual = new HashSet<InetAddress>(tmd.getWriteEndpoints(token, keyspaceName, strategy.calculateNaturalEndpoints(token, tmd.cloneOnlyTokenMap())));
HashSet<InetAddress> expected = new HashSet<InetAddress>();
for (int i = 0; i < replicationFactor; i++) {
expected.add(expectedEndpoints.get(token).get(i));
}
// then we should expect it plus one extra for when it's gone
if (expected.contains(hosts.get(LEAVING_NODE)))
expected.add(expectedEndpoints.get(token).get(replicationFactor));
assertEquals("mismatched endpoint sets", expected, actual);
}
}
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class LeaveAndBootstrapTest method testStateJumpToLeft.
@Test
public void testStateJumpToLeft() throws UnknownHostException {
StorageService ss = StorageService.instance;
TokenMetadata tmd = ss.getTokenMetadata();
tmd.clearUnsafe();
IPartitioner partitioner = RandomPartitioner.instance;
VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
ArrayList<Token> endpointTokens = new ArrayList<Token>();
ArrayList<Token> keyTokens = new ArrayList<Token>();
List<InetAddress> hosts = new ArrayList<InetAddress>();
List<UUID> hostIds = new ArrayList<UUID>();
// create a ring of 6 nodes
Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 7);
// node hosts.get(2) goes jumps to left
ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.left(Collections.singleton(endpointTokens.get(2)), Gossiper.computeExpireTime()));
assertFalse(tmd.isMember(hosts.get(2)));
// node hosts.get(4) goes to bootstrap
Gossiper.instance.injectApplicationState(hosts.get(3), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(1))));
ss.onChange(hosts.get(3), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(1))));
assertFalse(tmd.isMember(hosts.get(3)));
assertEquals(1, tmd.getBootstrapTokens().size());
assertEquals(hosts.get(3), tmd.getBootstrapTokens().get(keyTokens.get(1)));
// and then directly to 'left'
Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(1))));
ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.left(Collections.singleton(keyTokens.get(1)), Gossiper.computeExpireTime()));
assertTrue(tmd.getBootstrapTokens().size() == 0);
assertFalse(tmd.isMember(hosts.get(2)));
assertFalse(tmd.isLeaving(hosts.get(2)));
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class MoveTest method addNetworkTopologyKeyspace.
private static void addNetworkTopologyKeyspace(String keyspaceName, Integer... replicas) throws Exception {
DatabaseDescriptor.setEndpointSnitch(new AbstractNetworkTopologySnitch() {
//Odd IPs are in DC1 and Even are in DC2. Endpoints upto .14 will have unique racks and
// then will be same for a set of three.
@Override
public String getRack(InetAddress endpoint) {
int ipLastPart = getIPLastPart(endpoint);
if (ipLastPart <= 14)
return UUID.randomUUID().toString();
else
return "RAC" + (ipLastPart % 3);
}
@Override
public String getDatacenter(InetAddress endpoint) {
if (getIPLastPart(endpoint) % 2 == 0)
return "DC2";
else
return "DC1";
}
private int getIPLastPart(InetAddress endpoint) {
String str = endpoint.toString();
int index = str.lastIndexOf(".");
return Integer.parseInt(str.substring(index + 1).trim());
}
});
final TokenMetadata tmd = StorageService.instance.getTokenMetadata();
tmd.clearUnsafe();
tmd.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.0.0.1"));
tmd.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.0.0.2"));
KeyspaceMetadata keyspace = KeyspaceMetadata.create(keyspaceName, KeyspaceParams.nts(configOptions(replicas)), Tables.of(TableMetadata.builder(keyspaceName, "CF1").addPartitionKeyColumn("key", BytesType.instance).build()));
MigrationManager.announceNewKeyspace(keyspace);
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class MoveTest method testMoveWithPendingRangesNetworkStrategyRackAwareThirtyNodes.
@Test
public void testMoveWithPendingRangesNetworkStrategyRackAwareThirtyNodes() throws Exception {
StorageService ss = StorageService.instance;
final int RING_SIZE = 60;
TokenMetadata tmd = ss.getTokenMetadata();
tmd.clearUnsafe();
VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
ArrayList<Token> endpointTokens = new ArrayList<>();
ArrayList<Token> keyTokens = new ArrayList<>();
List<InetAddress> hosts = new ArrayList<>();
List<UUID> hostIds = new ArrayList<>();
for (int i = 0; i < RING_SIZE / 2; i++) {
endpointTokens.add(new BigIntegerToken(String.valueOf(10 * i)));
endpointTokens.add(new BigIntegerToken(String.valueOf((10 * i) + 1)));
}
Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
PendingRangeCalculatorService.instance.blockUntilFinished();
//Moving Endpoint 127.0.0.37 in RAC1 with current token 180
int MOVING_NODE = 36;
moveHost(hosts.get(MOVING_NODE), 215, tmd, valueFactory);
assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(150, 151, "127.0.0.43"), generatePendingMapEntry(151, 160, "127.0.0.43"), generatePendingMapEntry(160, 161, "127.0.0.43"), generatePendingMapEntry(161, 170, "127.0.0.43"), generatePendingMapEntry(170, 171, "127.0.0.43"), generatePendingMapEntry(171, 180, "127.0.0.43"), generatePendingMapEntry(210, 211, "127.0.0.37"), generatePendingMapEntry(211, 215, "127.0.0.37")), Network_33_KeyspaceName);
finishMove(hosts.get(MOVING_NODE), 215, tmd);
//Moving it back to original spot
moveHost(hosts.get(MOVING_NODE), 180, tmd, valueFactory);
finishMove(hosts.get(MOVING_NODE), 180, tmd);
}
Aggregations