use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestHelixAdminCli method testInstanceGroupTags.
@Test
public void testInstanceGroupTags() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
String command = "-zkSvr " + ZK_ADDR + " -addCluster " + clusterName;
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
command = "-zkSvr localhost:2183 -addResource " + clusterName + " db_11 12 MasterSlave";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
for (int i = 0; i < 6; i++) {
command = "-zkSvr " + ZK_ADDR + " -addNode " + clusterName + " localhost:123" + i;
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
}
for (int i = 0; i < 2; i++) {
command = "-zkSvr " + ZK_ADDR + " -addInstanceTag " + clusterName + " localhost_123" + i + " tag1";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
}
for (int i = 2; i < 6; i++) {
command = "-zkSvr " + ZK_ADDR + " -addInstanceTag " + clusterName + " localhost_123" + i + " tag2";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
}
command = "-zkSvr " + ZK_ADDR + " -rebalance " + clusterName + " db_11 2 -instanceGroupTag tag1";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
IdealState dbIs = accessor.getProperty(accessor.keyBuilder().idealStates("db_11"));
Set<String> hosts = new HashSet<String>();
for (String p : dbIs.getPartitionSet()) {
for (String hostName : dbIs.getInstanceStateMap(p).keySet()) {
InstanceConfig config = accessor.getProperty(accessor.keyBuilder().instanceConfig(hostName));
Assert.assertTrue(config.containsTag("tag1"));
hosts.add(hostName);
}
}
Assert.assertEquals(hosts.size(), 2);
command = "-zkSvr " + ZK_ADDR + " -dropResource " + clusterName + " db_11 ";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
// re-add and rebalance
command = "-zkSvr " + ZK_ADDR + " -addResource " + clusterName + " db_11 48 MasterSlave";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
command = "-zkSvr " + ZK_ADDR + " -rebalance " + clusterName + " db_11 3 -instanceGroupTag tag2";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
dbIs = accessor.getProperty(accessor.keyBuilder().idealStates("db_11"));
hosts = new HashSet<String>();
for (String p : dbIs.getPartitionSet()) {
for (String hostName : dbIs.getInstanceStateMap(p).keySet()) {
InstanceConfig config = accessor.getProperty(accessor.keyBuilder().instanceConfig(hostName));
Assert.assertTrue(config.containsTag("tag2"));
hosts.add(hostName);
}
}
Assert.assertEquals(hosts.size(), 4);
command = "-zkSvr " + ZK_ADDR + " -dropResource " + clusterName + " db_11 ";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
for (int i = 3; i <= 3; i++) {
command = "-zkSvr " + ZK_ADDR + " -removeInstanceTag " + clusterName + " localhost_123" + i + " tag2";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
}
// re-add and rebalance
command = "-zkSvr " + ZK_ADDR + " -addResource " + clusterName + " db_11 48 MasterSlave";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
command = "-zkSvr " + ZK_ADDR + " -rebalance " + clusterName + " db_11 3 -instanceGroupTag tag2";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
dbIs = accessor.getProperty(accessor.keyBuilder().idealStates("db_11"));
hosts = new HashSet<String>();
for (String p : dbIs.getPartitionSet()) {
for (String hostName : dbIs.getInstanceStateMap(p).keySet()) {
InstanceConfig config = accessor.getProperty(accessor.keyBuilder().instanceConfig(hostName));
Assert.assertTrue(config.containsTag("tag2"));
hosts.add(hostName);
}
}
Assert.assertEquals(hosts.size(), 3);
// rebalance with key prefix
command = "-zkSvr " + ZK_ADDR + " -rebalance " + clusterName + " db_11 2 -key alias";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestHelixAdminCli method testDeactivateCluster.
@Test
public void testDeactivateCluster() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
String grandClusterName = clusterName + "_grand";
final int n = 6;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
MockParticipantManager[] participants = new MockParticipantManager[n];
ClusterDistributedController[] controllers = new ClusterDistributedController[2];
setupCluster(clusterName, grandClusterName, n, participants, controllers);
String command = "-zkSvr " + ZK_ADDR + " -activateCluster " + clusterName + " " + grandClusterName + " true";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Thread.sleep(500);
// deactivate cluster
command = "-zkSvr " + ZK_ADDR + " -activateCluster " + clusterName + " " + grandClusterName + " false";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
String path = accessor.keyBuilder().controllerLeader().getPath();
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
if (!_gZkClient.exists(path)) {
break;
}
}
Assert.assertFalse(_gZkClient.exists(path), "leader should be gone after deactivate the cluster");
command = "-zkSvr " + ZK_ADDR + " -dropCluster " + clusterName;
try {
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Assert.fail("dropCluster should fail since there are still instances running");
} catch (Exception e) {
// OK
}
for (int i = 0; i < participants.length; i++) {
participants[i].syncStop();
}
command = "-zkSvr localhost:2183 -dropCluster " + clusterName;
ClusterSetup.processCommandLineArgs(command.split("\\s"));
for (int i = 0; i < controllers.length; i++) {
controllers[i].syncStop();
}
command = "-zkSvr localhost:2183 -dropCluster " + grandClusterName;
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestDisableResource method test.
@Test
public void test() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
final int n = 5;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
10, // number of nodes
n, // replicas
3, "MasterSlave", // do rebalance
true);
String instanceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/resourceGroups/" + "TestDB0";
// Disable TestDB0
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.enableResource);
paramMap.put(JsonParameters.ENABLED, Boolean.toString(false));
TestHelixAdminScenariosRest.assertSuccessPostOperation(instanceUrl, paramMap, false);
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
Assert.assertFalse(idealState.isEnabled());
// Re-enable TestDB0
paramMap.put(JsonParameters.ENABLED, Boolean.toString(true));
TestHelixAdminScenariosRest.assertSuccessPostOperation(instanceUrl, paramMap, false);
idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
Assert.assertTrue(idealState.isEnabled());
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestConstraint method testMsgConstraint.
@Test
public void testMsgConstraint() {
String className = getShortClassName();
System.out.println("START testMsgConstraint() at " + new Date(System.currentTimeMillis()));
String clusterName = "CLUSTER_" + className + "_msg";
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
ZNRecord record = new ZNRecord("testMsgConstraint");
// constraint0:
// "MESSAGE_TYPE=STATE_TRANSITION,CONSTRAINT_VALUE=ANY"
record.setMapField("constraint0", new TreeMap<String, String>());
record.getMapField("constraint0").put("MESSAGE_TYPE", "STATE_TRANSITION");
record.getMapField("constraint0").put("CONSTRAINT_VALUE", "ANY");
ConstraintItem constraint0 = new ConstraintItem(record.getMapField("constraint0"));
// constraint1:
// "MESSAGE_TYPE=STATE_TRANSITION,TRANSITION=OFFLINE-SLAVE,CONSTRAINT_VALUE=ANY"
record.setMapField("constraint1", new TreeMap<String, String>());
record.getMapField("constraint1").put("MESSAGE_TYPE", "STATE_TRANSITION");
record.getMapField("constraint1").put("TRANSITION", "OFFLINE-SLAVE");
record.getMapField("constraint1").put("CONSTRAINT_VALUE", "50");
ConstraintItem constraint1 = new ConstraintItem(record.getMapField("constraint1"));
// constraint2:
// "MESSAGE_TYPE=STATE_TRANSITION,TRANSITION=OFFLINE-SLAVE,INSTANCE=.*,RESOURCE=TestDB,CONSTRAINT_VALUE=2";
record.setMapField("constraint2", new TreeMap<String, String>());
record.getMapField("constraint2").put("MESSAGE_TYPE", "STATE_TRANSITION");
record.getMapField("constraint2").put("TRANSITION", "OFFLINE-SLAVE");
record.getMapField("constraint2").put("INSTANCE", ".*");
record.getMapField("constraint2").put("RESOURCE", "TestDB");
record.getMapField("constraint2").put("CONSTRAINT_VALUE", "2");
ConstraintItem constraint2 = new ConstraintItem(record.getMapField("constraint2"));
// constraint3:
// "MESSAGE_TYPE=STATE_TRANSITION,TRANSITION=OFFLINE-SLAVE,INSTANCE=localhost_12918,RESOURCE=.*,CONSTRAINT_VALUE=1";
record.setMapField("constraint3", new TreeMap<String, String>());
record.getMapField("constraint3").put("MESSAGE_TYPE", "STATE_TRANSITION");
record.getMapField("constraint3").put("TRANSITION", "OFFLINE-SLAVE");
record.getMapField("constraint3").put("INSTANCE", "localhost_12919");
record.getMapField("constraint3").put("RESOURCE", ".*");
record.getMapField("constraint3").put("CONSTRAINT_VALUE", "1");
ConstraintItem constraint3 = new ConstraintItem(record.getMapField("constraint3"));
// constraint4:
// "MESSAGE_TYPE=STATE_TRANSITION,TRANSITION=OFFLINE-SLAVE,INSTANCE=.*,RESOURCE=.*,CONSTRAINT_VALUE=10"
record.setMapField("constraint4", new TreeMap<String, String>());
record.getMapField("constraint4").put("MESSAGE_TYPE", "STATE_TRANSITION");
record.getMapField("constraint4").put("TRANSITION", "OFFLINE-SLAVE");
record.getMapField("constraint4").put("INSTANCE", ".*");
record.getMapField("constraint4").put("RESOURCE", ".*");
record.getMapField("constraint4").put("CONSTRAINT_VALUE", "10");
ConstraintItem constraint4 = new ConstraintItem(record.getMapField("constraint4"));
// constraint5:
// "MESSAGE_TYPE=STATE_TRANSITION,TRANSITION=OFFLINE-SLAVE,INSTANCE=localhost_12918,RESOURCE=TestDB,CONSTRAINT_VALUE=5"
record.setMapField("constraint5", new TreeMap<String, String>());
record.getMapField("constraint5").put("MESSAGE_TYPE", "STATE_TRANSITION");
record.getMapField("constraint5").put("TRANSITION", "OFFLINE-SLAVE");
record.getMapField("constraint5").put("INSTANCE", "localhost_12918");
record.getMapField("constraint5").put("RESOURCE", "TestDB");
record.getMapField("constraint5").put("CONSTRAINT_VALUE", "5");
ConstraintItem constraint5 = new ConstraintItem(record.getMapField("constraint5"));
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
accessor.setProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString()), new ClusterConstraints(record));
record = accessor.getProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString())).getRecord();
ClusterConstraints constraint = new ClusterConstraints(record);
// System.out.println("constraint: " + constraint);
// message1
Message msg1 = createMessage(MessageType.STATE_TRANSITION, "msgId-001", "OFFLINE", "SLAVE", "TestDB", "localhost_12918");
Map<ConstraintAttribute, String> msgAttr = ClusterConstraints.toConstraintAttributes(msg1);
Set<ConstraintItem> matches = constraint.match(msgAttr);
System.out.println(msg1 + " matches(" + matches.size() + "): " + matches);
Assert.assertEquals(matches.size(), 5);
Assert.assertTrue(contains(matches, constraint0));
Assert.assertTrue(contains(matches, constraint1));
Assert.assertTrue(contains(matches, constraint2));
Assert.assertTrue(contains(matches, constraint4));
Assert.assertTrue(contains(matches, constraint5));
// message2
Message msg2 = createMessage(MessageType.STATE_TRANSITION, "msgId-002", "OFFLINE", "SLAVE", "TestDB", "localhost_12919");
msgAttr = ClusterConstraints.toConstraintAttributes(msg2);
matches = constraint.match(msgAttr);
System.out.println(msg2 + " matches(" + matches.size() + "): " + matches);
Assert.assertEquals(matches.size(), 5);
Assert.assertTrue(contains(matches, constraint0));
Assert.assertTrue(contains(matches, constraint1));
Assert.assertTrue(contains(matches, constraint2));
Assert.assertTrue(contains(matches, constraint3));
Assert.assertTrue(contains(matches, constraint4));
System.out.println("END testMsgConstraint() at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestConstraint method testStateConstraint.
@Test
public void testStateConstraint() {
String className = getShortClassName();
System.out.println("START testStateConstraint() at " + new Date(System.currentTimeMillis()));
String clusterName = "CLUSTER_" + className + "_state";
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
ZNRecord record = new ZNRecord("testStateConstraint");
// constraint0:
// "STATE=MASTER,CONSTRAINT_VALUE=1"
record.setMapField("constraint0", new TreeMap<String, String>());
record.getMapField("constraint0").put("STATE", "MASTER");
record.getMapField("constraint0").put("CONSTRAINT_VALUE", "1");
ConstraintItem constraint0 = new ConstraintItem(record.getMapField("constraint0"));
// constraint1:
// "STATE=MASTER,RESOURCE=TestDB,CONSTRAINT_VALUE=5"
record.setMapField("constraint1", new TreeMap<String, String>());
record.getMapField("constraint1").put("STATE", "MASTER");
record.getMapField("constraint1").put("RESOURCE", "TestDB");
record.getMapField("constraint1").put("CONSTRAINT_VALUE", "1");
ConstraintItem constraint1 = new ConstraintItem(record.getMapField("constraint1"));
// constraint2:
// "STATE=MASTER,RESOURCE=.*,CONSTRAINT_VALUE=2"
record.setMapField("constraint2", new TreeMap<String, String>());
record.getMapField("constraint2").put("STATE", "MASTER");
record.getMapField("constraint2").put("RESOURCE", ".*");
record.getMapField("constraint2").put("CONSTRAINT_VALUE", "2");
ConstraintItem constraint2 = new ConstraintItem(record.getMapField("constraint2"));
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
accessor.setProperty(keyBuilder.constraint(ConstraintType.STATE_CONSTRAINT.toString()), new ClusterConstraints(record));
record = accessor.getProperty(keyBuilder.constraint(ConstraintType.STATE_CONSTRAINT.toString())).getRecord();
ClusterConstraints constraint = new ClusterConstraints(record);
// System.out.println("constraint: " + constraint);
// state1: hit rule2
Map<ConstraintAttribute, String> stateAttr1 = new HashMap<ConstraintAttribute, String>();
stateAttr1.put(ConstraintAttribute.STATE, "MASTER");
stateAttr1.put(ConstraintAttribute.RESOURCE, "TestDB");
Set<ConstraintItem> matches = constraint.match(stateAttr1);
System.out.println(stateAttr1 + " matches(" + matches.size() + "): " + matches);
Assert.assertEquals(matches.size(), 3);
Assert.assertTrue(contains(matches, constraint0));
Assert.assertTrue(contains(matches, constraint1));
Assert.assertTrue(contains(matches, constraint2));
// matches = selectConstraints(matches, stateAttr1);
// System.out.println(stateAttr1 + " matches(" + matches.size() + "): " + matches);
// Assert.assertEquals(matches.size(), 2);
// Assert.assertTrue(contains(matches, constraint0));
// Assert.assertTrue(contains(matches, constraint1));
// state2: not hit any rules
Map<ConstraintAttribute, String> stateAttr2 = new HashMap<ConstraintAttribute, String>();
stateAttr2.put(ConstraintAttribute.STATE, "MASTER");
stateAttr2.put(ConstraintAttribute.RESOURCE, "MyDB");
matches = constraint.match(stateAttr2);
System.out.println(stateAttr2 + " matches(" + matches.size() + "): " + matches);
Assert.assertEquals(matches.size(), 2);
Assert.assertTrue(contains(matches, constraint0));
Assert.assertTrue(contains(matches, constraint2));
// matches = selectConstraints(matches, stateAttr2);
// System.out.println(stateAttr2 + " matches(" + matches.size() + "): " + matches);
// Assert.assertEquals(matches.size(), 2);
// Assert.assertTrue(contains(matches, constraint0));
// Assert.assertTrue(contains(matches, constraint2));
System.out.println("END testStateConstraint() at " + new Date(System.currentTimeMillis()));
}
Aggregations