use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class CustomCodeInvoker method callParticipantCode.
private void callParticipantCode(NotificationContext context) {
// therefore, double check to make sure only one participant invokes the code
if (context.getType() == Type.CALLBACK) {
HelixManager manager = context.getManager();
// DataAccessor accessor = manager.getDataAccessor();
HelixDataAccessor accessor = manager.getHelixDataAccessor();
Builder keyBuilder = accessor.keyBuilder();
String instance = manager.getInstanceName();
String sessionId = manager.getSessionId();
// get resource name from partition key: "PARTICIPANT_LEADER_XXX_0"
String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_'));
CurrentState curState = accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName));
if (curState == null) {
return;
}
String state = curState.getState(_partitionKey);
if (state == null || !state.equalsIgnoreCase("LEADER")) {
return;
}
}
try {
_callback.onCallback(context);
} catch (Exception e) {
LOG.error("Error invoking callback:" + _callback, e);
}
}
use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class HelixCustomCodeRunner method start.
/**
* This method will be invoked when there is a change in any subscribed
* notificationTypes
* @throws Exception
*/
public void start() throws Exception {
if (_callback == null || _notificationTypes == null || _notificationTypes.size() == 0 || _resourceName == null) {
throw new IllegalArgumentException("Require callback | notificationTypes | resourceName");
}
LOG.info("Register participantLeader on " + _notificationTypes + " using " + _resourceName);
_stateModelFty = new GenericLeaderStandbyStateModelFactory(_callback, _notificationTypes);
StateMachineEngine stateMach = _manager.getStateMachineEngine();
stateMach.registerStateModelFactory(LEADER_STANDBY, _stateModelFty, _resourceName);
ZkClient zkClient = null;
try {
// manually add ideal state for participant leader using LeaderStandby
// model
zkClient = new ZkClient(_zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
HelixDataAccessor accessor = new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = new IdealState(_resourceName);
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
idealState.setNumPartitions(1);
idealState.setStateModelDefRef(LEADER_STANDBY);
idealState.setStateModelFactoryName(_resourceName);
List<String> prefList = new ArrayList<String>(Arrays.asList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
idealState.getRecord().setListField(_resourceName + "_0", prefList);
List<String> idealStates = accessor.getChildNames(keyBuilder.idealStates());
while (idealStates == null || !idealStates.contains(_resourceName)) {
accessor.setProperty(keyBuilder.idealStates(_resourceName), idealState);
idealStates = accessor.getChildNames(keyBuilder.idealStates());
}
LOG.info("Set idealState for participantLeader:" + _resourceName + ", idealState:" + idealState);
} finally {
if (zkClient != null && zkClient.getConnection() != null) {
zkClient.close();
}
}
}
use of org.apache.helix.HelixDataAccessor in project helix by apache.
the class RoutingTableProvider method refresh.
public void refresh(List<ExternalView> externalViewList, NotificationContext changeContext) {
HelixDataAccessor accessor = changeContext.getManager().getHelixDataAccessor();
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
List<InstanceConfig> configList = accessor.getChildValues(keyBuilder.instanceConfigs());
List<LiveInstance> liveInstances = accessor.getChildValues(keyBuilder.liveInstances());
refresh(externalViewList, configList, liveInstances);
}
use of org.apache.helix.HelixDataAccessor 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.HelixDataAccessor 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()));
}
Aggregations