use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.
the class HelixAgentMain method main.
public static void main(String[] args) throws Exception {
CommandLine cmd = processCommandLineArgs(args);
String zkAddress = cmd.getOptionValue(zkAddr);
String clusterName = cmd.getOptionValue(cluster);
String instance = cmd.getOptionValue(instanceName);
String stateModelName = cmd.getOptionValue(stateModel);
HelixManager manager = new ZKHelixManager(clusterName, instance, InstanceType.PARTICIPANT, zkAddress);
StateMachineEngine stateMach = manager.getStateMachineEngine();
stateMach.registerStateModelFactory(stateModelName, new AgentStateModelFactory());
Runtime.getRuntime().addShutdownHook(new HelixAgentShutdownHook(manager));
try {
manager.connect();
Thread.currentThread().join();
} catch (Exception e) {
LOG.error(e.toString());
} finally {
if (manager != null && manager.isConnected()) {
manager.disconnect();
}
}
}
use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.
the class TestSyncSessionToController method testSyncSessionToController.
@Test
public void testSyncSessionToController() throws Exception {
System.out.println("START testSyncSessionToController at " + new Date(System.currentTimeMillis()));
String clusterName = getShortClassName();
MockParticipantManager[] participants = new MockParticipantManager[5];
int resourceNb = 10;
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
resourceNb, // partitions per resource
1, // number of nodes
5, // replicas
1, "MasterSlave", // do rebalance
true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// start participants
for (int i = 0; i < 5; i++) {
String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
ZKHelixManager zkHelixManager = new ZKHelixManager(clusterName, "controllerMessageListener", InstanceType.CONTROLLER, ZK_ADDR);
zkHelixManager.connect();
MockMessageListener mockMessageListener = new MockMessageListener();
zkHelixManager.addControllerMessageListener(mockMessageListener);
PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
String path = keyBuilder.liveInstance("localhost_12918").getPath();
Stat stat = new Stat();
ZNRecord data = accessor.get(path, stat, 2);
data.getSimpleFields().put("SESSION_ID", "invalid-id");
accessor.set(path, data, 2);
Thread.sleep(2000);
Assert.assertTrue(mockMessageListener.isSessionSyncMessageSent());
}
use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.
the class TestEntropyFreeNodeBounce method createParticipant.
private HelixManager createParticipant(String clusterName, String instanceName) {
HelixManager participant = new ZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, ZK_ADDR);
participant.getStateMachineEngine().registerStateModelFactory("OnlineOffline", new MockStateModelFactory());
return participant;
}
use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.
the class TestZkReconnect method testZKDisconnectCallback.
@Test
public void testZKDisconnectCallback() throws Exception {
final int zkPort = TestHelper.getRandomPort();
final String zkAddr = String.format("localhost:%d", zkPort);
final ZkServer zkServer = TestHelper.startZkServer(zkAddr);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
final String clusterName = className + "_" + methodName;
// Init flag to check if callback is triggered
final AtomicReference<Boolean> flag = new AtomicReference<Boolean>(false);
// Setup cluster
LOG.info("Setup clusters");
ClusterSetup clusterSetup = new ClusterSetup(zkAddr);
clusterSetup.addCluster(clusterName, true);
// For fast test, set short timeout
System.setProperty("zk.connection.timeout", "2000");
System.setProperty("zk.connectionReEstablishment.timeout", "1000");
// Registers and starts controller, register listener for disconnect handling
LOG.info("Starts controller");
final ZKHelixManager controller = (ZKHelixManager) HelixManagerFactory.getZKHelixManager(clusterName, null, InstanceType.CONTROLLER, zkAddr, new HelixManagerStateListener() {
@Override
public void onConnected(HelixManager helixManager) throws Exception {
return;
}
@Override
public void onDisconnected(HelixManager helixManager, Throwable error) throws Exception {
Assert.assertEquals(helixManager.getClusterName(), clusterName);
flag.getAndSet(true);
}
});
try {
controller.connect();
ZkHelixPropertyStore propertyStore = controller.getHelixPropertyStore();
// 1. shutdown zkServer and check if handler trigger callback
zkServer.shutdown();
// Retry will fail, and flag should be set within onDisconnected handler
controller.handleSessionEstablishmentError(new Exception("For testing"));
Assert.assertTrue(flag.get());
try {
propertyStore.get("/", null, 0);
Assert.fail("propertyStore should be disconnected.");
} catch (IllegalStateException e) {
// Expected exception
System.out.println(e.getMessage());
}
// 2. restart zkServer and check if handler will recover connection
flag.getAndSet(false);
zkServer.start();
// Retry will succeed, and flag should not be set
controller.handleSessionEstablishmentError(new Exception("For testing"));
Assert.assertFalse(flag.get());
// New propertyStore should be in good state
propertyStore = controller.getHelixPropertyStore();
propertyStore.get("/", null, 0);
} finally {
controller.disconnect();
zkServer.shutdown();
System.clearProperty("zk.connection.timeout");
System.clearProperty("zk.connectionReEstablishment.timeout");
}
}
use of org.apache.helix.manager.zk.ZKHelixManager in project helix by apache.
the class TestParticipantManager 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 = 1;
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
1, "MasterSlave", // do rebalance
true);
HelixManager participant = new ZKHelixManager(clusterName, "localhost_12918", InstanceType.PARTICIPANT, ZK_ADDR);
participant.getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
participant.connect();
HelixManager controller = new ZKHelixManager(clusterName, "controller_0", InstanceType.CONTROLLER, ZK_ADDR);
controller.connect();
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// cleanup
controller.disconnect();
participant.disconnect();
// verify all live-instances and leader nodes are gone
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations