use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZkClusterManager method testLiveInstanceInfoProvider.
@Test
public void testLiveInstanceInfoProvider() throws Exception {
System.out.println("START " + className + ".testLiveInstanceInfoProvider() at " + new Date(System.currentTimeMillis()));
final String clusterName = CLUSTER_PREFIX + "_" + className + "_liveInstanceInfoProvider";
class provider implements LiveInstanceInfoProvider {
boolean _flag = false;
public provider(boolean genSessionId) {
_flag = genSessionId;
}
@Override
public ZNRecord getAdditionalLiveInstanceInfo() {
ZNRecord record = new ZNRecord("info");
record.setSimpleField("simple", "value");
List<String> listFieldVal = new ArrayList<String>();
listFieldVal.add("val1");
listFieldVal.add("val2");
listFieldVal.add("val3");
record.setListField("list", listFieldVal);
Map<String, String> mapFieldVal = new HashMap<String, String>();
mapFieldVal.put("k1", "val1");
mapFieldVal.put("k2", "val2");
mapFieldVal.put("k3", "val3");
record.setMapField("map", mapFieldVal);
if (_flag) {
record.setSimpleField("SESSION_ID", "value");
record.setSimpleField("LIVE_INSTANCE", "value");
record.setSimpleField("Others", "value");
}
return record;
}
}
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
int[] ids = { 0, 1, 2, 3, 4, 5 };
setupInstances(clusterName, ids);
// ///////////////////
ZKHelixManager manager = new ZKHelixManager(clusterName, "localhost_0", InstanceType.PARTICIPANT, ZK_ADDR);
manager.connect();
HelixDataAccessor accessor = manager.getHelixDataAccessor();
LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_0"));
Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 0);
Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 0);
Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 3);
manager = new ZKHelixManager(clusterName, "localhost_1", InstanceType.PARTICIPANT, ZK_ADDR);
manager.setLiveInstanceInfoProvider(new provider(false));
manager.connect();
accessor = manager.getHelixDataAccessor();
liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_1"));
Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 4);
manager = new ZKHelixManager(clusterName, "localhost_2", InstanceType.PARTICIPANT, ZK_ADDR);
manager.setLiveInstanceInfoProvider(new provider(true));
manager.connect();
accessor = manager.getHelixDataAccessor();
liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_2"));
Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
Assert.assertFalse(liveInstance.getSessionId().equals("value"));
Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
// //////////////////////////////////
MockParticipantManager manager2 = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_3");
manager2.setLiveInstanceInfoProvider(new provider(true));
manager2.connect();
accessor = manager2.getHelixDataAccessor();
liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_3"));
Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
Assert.assertFalse(liveInstance.getSessionId().equals("value"));
Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
String sessionId = liveInstance.getSessionId();
ZkTestHelper.expireSession(manager2.getZkClient());
Thread.sleep(1000);
liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance("localhost_3"));
Assert.assertTrue(liveInstance.getRecord().getListFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getMapFields().size() == 1);
Assert.assertTrue(liveInstance.getRecord().getSimpleFields().size() == 5);
Assert.assertFalse(liveInstance.getSessionId().equals("value"));
Assert.assertFalse(liveInstance.getLiveInstance().equals("value"));
Assert.assertFalse(sessionId.equals(liveInstance.getSessionId()));
System.out.println("END " + className + ".testLiveInstanceInfoProvider() at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZkFlapping method testControllerFlapping.
@Test
public void testControllerFlapping() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
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
32, // number of nodes
1, // replicas
1, "MasterSlave", false);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
controller.syncStart();
final ZkClient client = controller.getZkClient();
final ZkStateCountListener listener = new ZkStateCountListener();
client.subscribeStateChanges(listener);
final AtomicInteger expectDisconnectCnt = new AtomicInteger(0);
final int n = ZKHelixManager.MAX_DISCONNECT_THRESHOLD;
for (int i = 0; i < n; i++) {
String oldSessionId = ZkTestHelper.getSessionId(client);
ZkTestHelper.simulateZkStateDisconnected(client);
expectDisconnectCnt.incrementAndGet();
// wait until we get invoked by zk state change to disconnected
TestHelper.verify(new Verifier() {
@Override
public boolean verify() throws Exception {
return listener.count == expectDisconnectCnt.get();
}
}, 30 * 1000);
String newSessionId = ZkTestHelper.getSessionId(client);
Assert.assertEquals(newSessionId, oldSessionId);
}
// make sure controller is NOT disconnected
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
Assert.assertNotNull(leader, "Leader should exist after " + n + " disconnects");
// trigger flapping
ZkTestHelper.simulateZkStateDisconnected(client);
// wait until we get invoked by zk state change to disconnected
boolean success = TestHelper.verify(new Verifier() {
@Override
public boolean verify() throws Exception {
return client.getShutdownTrigger();
}
}, 30 * 1000);
Assert.assertTrue(success, "The " + (n + 1) + "th disconnect event should trigger ZkHelixManager#disonnect");
// make sure controller is disconnected
success = TestHelper.verify(new TestHelper.Verifier() {
@Override
public boolean verify() throws Exception {
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
return leader == null;
}
}, 5 * 1000);
Assert.assertTrue(success, "Leader should be gone after " + (n + 1) + " disconnects");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZkFlapping method testCloseZkClientInZkClientEventThread.
@Test
public void testCloseZkClientInZkClientEventThread() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
final CountDownLatch waitCallback = new CountDownLatch(1);
final ZkClient client = new ZkClient(ZK_ADDR, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
String path = String.format("/%s", clusterName);
client.createEphemeral(path);
client.subscribeDataChanges(path, new IZkDataListener() {
@Override
public void handleDataDeleted(String dataPath) throws Exception {
}
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
client.close();
waitCallback.countDown();
}
});
client.writeData(path, new ZNRecord("test"));
waitCallback.await();
Assert.assertFalse(_gZkClient.exists(path), "Ephemeral node: " + path + " should be removed after ZkClient#close() in its own event-thread");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZkFlapping method testParticipantFlapping.
@Test
public void testParticipantFlapping() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
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
32, // number of nodes
1, // replicas
1, "MasterSlave", false);
final String instanceName = "localhost_12918";
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participant.syncStart();
final ZkClient client = participant.getZkClient();
final ZkStateCountListener listener = new ZkStateCountListener();
client.subscribeStateChanges(listener);
final AtomicInteger expectDisconnectCnt = new AtomicInteger(0);
final int n = ZKHelixManager.MAX_DISCONNECT_THRESHOLD;
for (int i = 0; i < n; i++) {
String oldSessionId = ZkTestHelper.getSessionId(client);
ZkTestHelper.simulateZkStateDisconnected(client);
expectDisconnectCnt.incrementAndGet();
// wait until we get invoked by zk state change to disconnected
TestHelper.verify(new Verifier() {
@Override
public boolean verify() throws Exception {
return listener.count == expectDisconnectCnt.get();
}
}, 30 * 1000);
String newSessionId = ZkTestHelper.getSessionId(client);
Assert.assertEquals(newSessionId, oldSessionId);
}
client.unsubscribeStateChanges(listener);
// make sure participant is NOT disconnected
LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instanceName));
Assert.assertNotNull(liveInstance, "Live-instance should exist after " + n + " disconnects");
// trigger flapping
ZkTestHelper.simulateZkStateDisconnected(client);
// wait until we get invoked by zk state change to disconnected
boolean success = TestHelper.verify(new Verifier() {
@Override
public boolean verify() throws Exception {
return client.getShutdownTrigger();
}
}, 30 * 1000);
Assert.assertTrue(success, "The " + (n + 1) + "th disconnect event should trigger ZkHelixManager#disonnect");
// make sure participant is disconnected
success = TestHelper.verify(new TestHelper.Verifier() {
@Override
public boolean verify() throws Exception {
LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instanceName));
return liveInstance == null;
}
}, 3 * 1000);
Assert.assertTrue(success, "Live-instance should be gone after " + (n + 1) + " disconnects");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZkHelixAdmin method testAddRemoveMsgConstraint.
// test add/remove message constraint
@Test
public void testAddRemoveMsgConstraint() {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
HelixAdmin tool = new ZKHelixAdmin(_gZkClient);
tool.addCluster(clusterName, true);
Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient), "Cluster should be setup");
// test admin.getMessageConstraints()
ClusterConstraints constraints = tool.getConstraints(clusterName, ConstraintType.MESSAGE_CONSTRAINT);
Assert.assertNull(constraints, "message-constraint should NOT exist for cluster: " + className);
// remove non-exist constraint
try {
tool.removeConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1");
// will leave a null message-constraint znode on zk
} catch (Exception e) {
Assert.fail("Should not throw exception when remove a non-exist constraint.");
}
// add a message constraint
ConstraintItemBuilder builder = new ConstraintItemBuilder();
builder.addConstraintAttribute(ConstraintAttribute.RESOURCE.toString(), "MyDB").addConstraintAttribute(ConstraintAttribute.CONSTRAINT_VALUE.toString(), "1");
tool.setConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1", builder.build());
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
constraints = accessor.getProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString()));
Assert.assertNotNull(constraints, "message-constraint should exist");
ConstraintItem item = constraints.getConstraintItem("constraint1");
Assert.assertNotNull(item, "message-constraint for constraint1 should exist");
Assert.assertEquals(item.getConstraintValue(), "1");
Assert.assertEquals(item.getAttributeValue(ConstraintAttribute.RESOURCE), "MyDB");
// test admin.getMessageConstraints()
constraints = tool.getConstraints(clusterName, ConstraintType.MESSAGE_CONSTRAINT);
Assert.assertNotNull(constraints, "message-constraint should exist");
item = constraints.getConstraintItem("constraint1");
Assert.assertNotNull(item, "message-constraint for constraint1 should exist");
Assert.assertEquals(item.getConstraintValue(), "1");
Assert.assertEquals(item.getAttributeValue(ConstraintAttribute.RESOURCE), "MyDB");
// remove a exist message-constraint
tool.removeConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1");
constraints = accessor.getProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString()));
Assert.assertNotNull(constraints, "message-constraint should exist");
item = constraints.getConstraintItem("constraint1");
Assert.assertNull(item, "message-constraint for constraint1 should NOT exist");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations