use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestControllerLiveLock method test.
@Test
public void test() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 12;
final int p = 256;
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
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
p, // number of nodes
n, // replicas
1, "LeaderStandby", RebalanceMode.FULL_AUTO, // do rebalance
true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// start participants
Random random = new Random();
MockParticipantManager[] participants = new MockParticipantManager[n];
for (int i = 0; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
Thread.sleep(Math.abs(random.nextInt()) % 500 + 500);
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// make sure all partitions are assigned and no partitions is assigned to STANDBY state
result = TestHelper.verify(new TestHelper.Verifier() {
@Override
public boolean verify() throws Exception {
ExternalView extView = accessor.getProperty(keyBuilder.externalView("TestDB0"));
for (int i = 0; i < p; i++) {
String partition = "TestDB0_" + i;
Map<String, String> map = extView.getRecord().getMapField(partition);
if (map == null || map.size() != 1) {
return false;
}
}
return true;
}
}, 10 * 1000);
if (!result) {
ExternalView extView = accessor.getProperty(keyBuilder.externalView("TestDB0"));
for (int i = 0; i < p; i++) {
String partition = "TestDB0_" + i;
Map<String, String> map = extView.getRecord().getMapField(partition);
if (map == null || map.size() != 1) {
LOG.error(partition + ": " + map);
}
}
}
Assert.assertTrue(result);
// clean up
controller.syncStop();
for (int i = 0; i < n; i++) {
participants[i].syncStop();
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestDistributedControllerManager method simpleIntegrationTest.
@Test
public void simpleIntegrationTest() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 2;
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
4, // number of nodes
n, // replicas
2, "MasterSlave", // do rebalance
true);
HelixManager[] distributedControllers = new HelixManager[n];
for (int i = 0; i < n; i++) {
int port = 12918 + i;
distributedControllers[i] = new ZKHelixManager(clusterName, "localhost_" + port, InstanceType.CONTROLLER_PARTICIPANT, ZK_ADDR);
distributedControllers[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
distributedControllers[i].connect();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// disconnect first distributed-controller, and verify second takes leadership
distributedControllers[0].disconnect();
// verify leader changes to localhost_12919
Thread.sleep(100);
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
Assert.assertNotNull(leader);
Assert.assertEquals(leader.getId(), "localhost_12919");
// clean up
distributedControllers[1].disconnect();
Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12919")));
Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestDistributedControllerManager method expireController.
/**
* expire a controller and make sure the other takes the leadership
* @param expireController
* @param newController
* @throws Exception
*/
void expireController(ClusterDistributedController expireController, ClusterDistributedController newController) throws Exception {
String clusterName = expireController.getClusterName();
LOG.info("Expiring distributedController: " + expireController.getInstanceName() + ", session: " + expireController.getSessionId() + " ...");
String oldSessionId = expireController.getSessionId();
ZkTestHelper.expireSession(expireController.getZkClient());
String newSessionId = expireController.getSessionId();
LOG.debug("Expired distributedController: " + expireController.getInstanceName() + ", oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// verify leader changes to localhost_12919
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNotNull(accessor.getProperty(keyBuilder.liveInstance(expireController.getInstanceName())));
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
Assert.assertNotNull(leader);
Assert.assertEquals(leader.getId(), newController.getInstanceName());
// check expired-controller has 2 handlers: message and data-accessor
LOG.debug(expireController.getInstanceName() + " handlers: " + TestHelper.printHandlers(expireController));
List<CallbackHandler> handlers = expireController.getHandlers();
Assert.assertEquals(handlers.size(), 2, "Distributed controller should have 1 handler (message) after lose leadership, but was " + handlers.size());
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestHelixDataAccessor method beforeClass.
@BeforeClass
public void beforeClass() {
_zkClient = new MockZkClient(ZK_ADDR);
baseDataAccessor = new ZkBaseDataAccessor<>(_zkClient);
accessor = new ZKHelixDataAccessor("HELIX", baseDataAccessor);
Map<String, HelixProperty> paths = new TreeMap<>();
propertyKeys = new ArrayList<>();
for (int i = 0; i < 5; i++) {
PropertyKey key = accessor.keyBuilder().idealStates("RESOURCE" + i);
propertyKeys.add(key);
paths.put(key.getPath(), new HelixProperty("RESOURCE" + i));
accessor.setProperty(key, paths.get(key.getPath()));
}
List<HelixProperty> data = accessor.getProperty(new ArrayList<>(propertyKeys), true);
Assert.assertEquals(data.size(), 5);
PropertyKey key = accessor.keyBuilder().idealStates("RESOURCE6");
propertyKeys.add(key);
_zkClient.putData(key.getPath(), null);
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestDrop method assertEmptyCSandEV.
/**
* Assert externalView and currentState for each participant are empty
* @param clusterName
* @param db
* @param participants
*/
private void assertEmptyCSandEV(String clusterName, String db, MockParticipantManager[] participants) {
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNull(accessor.getProperty(keyBuilder.externalView(db)));
for (MockParticipantManager participant : participants) {
String instanceName = participant.getInstanceName();
String sessionId = participant.getSessionId();
Assert.assertNull(accessor.getProperty(keyBuilder.currentState(instanceName, sessionId, db)));
}
}
Aggregations