use of org.apache.helix.HelixManager in project helix by apache.
the class TestSwapInstance method TestSwap.
@Test
public void TestSwap() throws Exception {
HelixManager manager = _controller;
HelixDataAccessor helixAccessor = manager.getHelixDataAccessor();
_setupTool.addResourceToCluster(CLUSTER_NAME, "MyDB", 64, STATE_MODEL);
_setupTool.rebalanceStorageCluster(CLUSTER_NAME, "MyDB", _replica);
ZNRecord idealStateOld1 = new ZNRecord("TestDB");
ZNRecord idealStateOld2 = new ZNRecord("MyDB");
IdealState is1 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("TestDB"));
idealStateOld1.merge(is1.getRecord());
IdealState is2 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("MyDB"));
idealStateOld2.merge(is2.getRecord());
Assert.assertTrue(ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME)));
String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + 0);
ZKHelixAdmin tool = new ZKHelixAdmin(_gZkClient);
_setupTool.getClusterManagementTool().enableInstance(CLUSTER_NAME, instanceName, false);
boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
Assert.assertTrue(result);
String instanceName2 = PARTICIPANT_PREFIX + "_" + (START_PORT + 444);
_setupTool.addInstanceToCluster(CLUSTER_NAME, instanceName2);
boolean exception = false;
try {
_setupTool.swapInstance(CLUSTER_NAME, instanceName, instanceName2);
} catch (Exception e) {
exception = true;
}
Assert.assertTrue(exception);
_participants[0].syncStop();
Thread.sleep(1000);
exception = false;
try {
_setupTool.swapInstance(CLUSTER_NAME, instanceName, instanceName2);
} catch (Exception e) {
e.printStackTrace();
exception = true;
}
Assert.assertFalse(exception);
MockParticipantManager newParticipant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instanceName2);
newParticipant.syncStart();
result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
Assert.assertTrue(result);
is1 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("TestDB"));
is2 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("MyDB"));
for (String key : idealStateOld1.getMapFields().keySet()) {
for (String host : idealStateOld1.getMapField(key).keySet()) {
if (host.equals(instanceName)) {
Assert.assertTrue(idealStateOld1.getMapField(key).get(instanceName).equals(is1.getRecord().getMapField(key).get(instanceName2)));
} else {
Assert.assertTrue(idealStateOld1.getMapField(key).get(host).equals(is1.getRecord().getMapField(key).get(host)));
}
}
}
for (String key : idealStateOld1.getListFields().keySet()) {
Assert.assertEquals(idealStateOld1.getListField(key).size(), is1.getRecord().getListField(key).size());
for (int i = 0; i < idealStateOld1.getListField(key).size(); i++) {
String host = idealStateOld1.getListField(key).get(i);
String newHost = is1.getRecord().getListField(key).get(i);
if (host.equals(instanceName)) {
Assert.assertTrue(newHost.equals(instanceName2));
} else {
// System.out.println(key + " " + i+ " " + host + " "+newHost);
// System.out.println(idealStateOld1.getListField(key));
// System.out.println(is1.getRecord().getListField(key));
Assert.assertTrue(host.equals(newHost));
}
}
}
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TestControllerHistory method testControllerLeaderHistory.
@Test()
public void testControllerLeaderHistory() throws Exception {
HelixManager manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "admin", InstanceType.ADMINISTRATOR, ZK_ADDR);
manager.connect();
PropertyKey.Builder keyBuilder = new PropertyKey.Builder(CLUSTER_NAME);
PropertyKey propertyKey = keyBuilder.controllerLeaderHistory();
LeaderHistory leaderHistory = manager.getHelixDataAccessor().getProperty(propertyKey);
Assert.assertNotNull(leaderHistory);
List<String> list = leaderHistory.getRecord().getListField("HISTORY");
Assert.assertEquals(list.size(), 1);
for (int i = 0; i <= 12; i++) {
_controller.syncStop();
_controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, "Controller-" + i);
_controller.syncStart();
}
leaderHistory = manager.getHelixDataAccessor().getProperty(propertyKey);
Assert.assertNotNull(leaderHistory);
list = leaderHistory.getRecord().getListField("HISTORY");
Assert.assertEquals(list.size(), 10);
manager.disconnect();
}
use of org.apache.helix.HelixManager 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.HelixManager in project helix by apache.
the class TestClusterStartsup method testParticipantStartUp.
@Test()
public void testParticipantStartUp() throws Exception {
setupCluster();
String controllerMsgPath = PropertyPathBuilder.controllerMessage(CLUSTER_NAME);
_gZkClient.deleteRecursively(controllerMsgPath);
HelixManager manager = null;
try {
manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 1), InstanceType.PARTICIPANT, ZK_ADDR);
manager.connect();
Assert.fail("Should fail on connect() since cluster structure is not set up");
} catch (HelixException e) {
// OK
}
if (manager != null) {
AssertJUnit.assertFalse(manager.isConnected());
}
try {
manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 3), InstanceType.PARTICIPANT, ZK_ADDR);
manager.connect();
Assert.fail("Should fail on connect() since cluster structure is not set up");
} catch (HelixException e) {
// OK
}
if (manager != null) {
AssertJUnit.assertFalse(manager.isConnected());
}
setupCluster();
String stateModelPath = PropertyPathBuilder.stateModelDef(CLUSTER_NAME);
_gZkClient.deleteRecursively(stateModelPath);
try {
manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 1), InstanceType.PARTICIPANT, ZK_ADDR);
manager.connect();
Assert.fail("Should fail on connect() since cluster structure is not set up");
} catch (HelixException e) {
// OK
}
if (manager != null) {
AssertJUnit.assertFalse(manager.isConnected());
}
setupCluster();
String instanceStatusUpdatePath = PropertyPathBuilder.instanceStatusUpdate(CLUSTER_NAME, "localhost_" + (START_PORT + 1));
_gZkClient.deleteRecursively(instanceStatusUpdatePath);
try {
manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME, "localhost_" + (START_PORT + 1), InstanceType.PARTICIPANT, ZK_ADDR);
manager.connect();
Assert.fail("Should fail on connect() since cluster structure is not set up");
} catch (HelixException e) {
// OK
}
if (manager != null) {
AssertJUnit.assertFalse(manager.isConnected());
}
}
use of org.apache.helix.HelixManager in project helix by apache.
the class GenericLeaderStandbyModel method onBecomeStandbyFromLeader.
@Transition(to = "STANDBY", from = "LEADER")
public void onBecomeStandbyFromLeader(Message message, NotificationContext context) {
LOG.info("Become STANDBY from LEADER");
HelixManager manager = context.getManager();
if (manager == null) {
throw new IllegalArgumentException("Require HelixManager in notification conext");
}
Builder keyBuilder = new Builder(manager.getClusterName());
for (ChangeType notificationType : _notificationTypes) {
if (notificationType == ChangeType.LIVE_INSTANCE) {
manager.removeListener(keyBuilder.liveInstances(), _particHolder);
} else if (notificationType == ChangeType.CONFIG) {
manager.removeListener(keyBuilder.instanceConfigs(), _particHolder);
} else if (notificationType == ChangeType.EXTERNAL_VIEW) {
manager.removeListener(keyBuilder.externalViews(), _particHolder);
} else {
LOG.error("Unsupport notificationType:" + notificationType.toString());
}
}
}
Aggregations