use of org.apache.helix.PropertyKey in project helix by apache.
the class ClusterDataCache method refreshIdealStates.
private Map<String, IdealState> refreshIdealStates(HelixDataAccessor accessor) {
long startTime = System.currentTimeMillis();
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Set<PropertyKey> currentIdealStateKeys = new HashSet<>();
for (String idealState : accessor.getChildNames(keyBuilder.idealStates())) {
currentIdealStateKeys.add(keyBuilder.idealStates(idealState));
}
Set<PropertyKey> cachedKeys = new HashSet<>();
Map<PropertyKey, IdealState> cachedIdealStateMap = Maps.newHashMap();
for (String idealState : _idealStateCacheMap.keySet()) {
cachedKeys.add(keyBuilder.idealStates(idealState));
cachedIdealStateMap.put(keyBuilder.idealStates(idealState), _idealStateCacheMap.get(idealState));
}
cachedKeys.retainAll(currentIdealStateKeys);
Set<PropertyKey> reloadKeys = new HashSet<>(currentIdealStateKeys);
reloadKeys.removeAll(cachedKeys);
Map<PropertyKey, IdealState> updatedMap = BasicClusterDataCache.updateReloadProperties(accessor, new LinkedList<>(reloadKeys), new ArrayList<>(cachedKeys), cachedIdealStateMap);
Map<String, IdealState> newIdealStateMap = Maps.newHashMap();
for (IdealState idealState : updatedMap.values()) {
newIdealStateMap.put(idealState.getResourceName(), idealState);
}
long endTime = System.currentTimeMillis();
LOG.info("Refresh idealStates for cluster " + _clusterName + ", took " + (endTime - startTime) + " ms");
return Collections.unmodifiableMap(newIdealStateMap);
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class ClusterDataCache method updateOfflineInstanceHistory.
private void updateOfflineInstanceHistory(HelixDataAccessor accessor) {
List<String> offlineNodes = new ArrayList<>(_instanceConfigMap.keySet());
offlineNodes.removeAll(_liveInstanceMap.keySet());
_instanceOfflineTimeMap = new HashMap<>();
for (String instance : offlineNodes) {
Builder keyBuilder = accessor.keyBuilder();
PropertyKey propertyKey = keyBuilder.participantHistory(instance);
ParticipantHistory history = accessor.getProperty(propertyKey);
if (history == null) {
history = new ParticipantHistory(instance);
}
if (history.getLastOfflineTime() == ParticipantHistory.ONLINE) {
history.reportOffline();
// persist history back to ZK.
if (!accessor.setProperty(propertyKey, history)) {
LOG.error("Fails to persist participant online history back to ZK!");
}
}
_instanceOfflineTimeMap.put(instance, history.getLastOfflineTime());
}
_updateInstanceOfflineTime = false;
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class TargetExteralViewCalcStage method process.
@Override
public void process(ClusterEvent event) throws Exception {
ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
ClusterConfig clusterConfig = cache.getClusterConfig();
if (cache.isTaskCache() || !clusterConfig.isTargetExternalViewEnabled()) {
return;
}
HelixManager helixManager = event.getAttribute(AttributeName.helixmanager.name());
HelixDataAccessor accessor = helixManager.getHelixDataAccessor();
if (!accessor.getBaseDataAccessor().exists(accessor.keyBuilder().targetExternalViews().getPath(), AccessOption.PERSISTENT)) {
accessor.getBaseDataAccessor().create(accessor.keyBuilder().targetExternalViews().getPath(), null, AccessOption.PERSISTENT);
}
BestPossibleStateOutput bestPossibleAssignments = event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
IntermediateStateOutput intermediateAssignments = event.getAttribute(AttributeName.INTERMEDIATE_STATE.name());
Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
List<PropertyKey> keys = new ArrayList<>();
List<ExternalView> targetExternalViews = new ArrayList<>();
for (String resourceName : bestPossibleAssignments.resourceSet()) {
if (cache.getIdealState(resourceName) == null || cache.getIdealState(resourceName).isExternalViewDisabled()) {
continue;
}
Resource resource = resourceMap.get(resourceName);
if (resource != null) {
PartitionStateMap partitionStateMap = intermediateAssignments.getPartitionStateMap(resourceName);
Map<String, Map<String, String>> intermediateAssignment = convertToMapFields(partitionStateMap.getStateMap());
Map<String, List<String>> preferenceLists = bestPossibleAssignments.getPreferenceLists(resourceName);
boolean needPersist = false;
ExternalView targetExternalView = cache.getTargetExternalView(resourceName);
if (targetExternalView == null) {
targetExternalView = new ExternalView(resourceName);
targetExternalView.getRecord().getSimpleFields().putAll(cache.getIdealState(resourceName).getRecord().getSimpleFields());
needPersist = true;
}
if (preferenceLists != null && !targetExternalView.getRecord().getListFields().equals(preferenceLists)) {
targetExternalView.getRecord().setListFields(preferenceLists);
needPersist = true;
}
if (intermediateAssignment != null && !targetExternalView.getRecord().getMapFields().equals(intermediateAssignment)) {
targetExternalView.getRecord().setMapFields(intermediateAssignment);
needPersist = true;
}
if (needPersist) {
keys.add(accessor.keyBuilder().targetExternalView(resourceName));
targetExternalViews.add(targetExternalView);
cache.updateTargetExternalView(resourceName, targetExternalView);
}
}
}
accessor.setChildren(keys, targetExternalViews);
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class ParticipantManager method getHistory.
private ParticipantHistory getHistory() {
PropertyKey propertyKey = _keyBuilder.participantHistory(_instanceName);
ParticipantHistory history = _dataAccessor.getProperty(propertyKey);
if (history == null) {
history = new ParticipantHistory(_instanceName);
}
return history;
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class TestZKPathDataDumpTask method test.
@Test
public void test() throws Exception {
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
2, // number of nodes
n, // replicas
1, "MasterSlave", // do rebalance
true);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
BaseDataAccessor<ZNRecord> baseAccessor = accessor.getBaseDataAccessor();
HelixManager manager = mock(HelixManager.class);
when(manager.getHelixDataAccessor()).thenReturn(accessor);
when(manager.getClusterName()).thenReturn(clusterName);
// run dump task without statusUpdates and errors, should not remove any existing
// statusUpdate/error paths
ZKPathDataDumpTask task = new ZKPathDataDumpTask(manager, 0L, 0L, Integer.MAX_VALUE);
task.run();
PropertyKey controllerStatusUpdateKey = keyBuilder.controllerTaskStatuses();
Assert.assertTrue(baseAccessor.exists(controllerStatusUpdateKey.getPath(), 0));
PropertyKey controllerErrorKey = keyBuilder.controllerTaskErrors();
Assert.assertTrue(baseAccessor.exists(controllerErrorKey.getPath(), 0));
PropertyKey statusUpdateKey = keyBuilder.stateTransitionStatus("localhost_12918");
Assert.assertTrue(baseAccessor.exists(statusUpdateKey.getPath(), 0));
PropertyKey errorKey = keyBuilder.stateTransitionErrors("localhost_12918");
// add participant status updates and errors
statusUpdateKey = keyBuilder.stateTransitionStatus("localhost_12918", "session_0", "TestDB0", "TestDB0_0");
accessor.setProperty(statusUpdateKey, new StatusUpdate(new ZNRecord("statusUpdate")));
errorKey = keyBuilder.stateTransitionError("localhost_12918", "session_0", "TestDB0", "TestDB0_0");
accessor.setProperty(errorKey, new Error(new ZNRecord("error")));
// add controller status updates and errors
controllerStatusUpdateKey = keyBuilder.controllerTaskStatus("session_0", "TestDB");
accessor.setProperty(controllerStatusUpdateKey, new StatusUpdate(new ZNRecord("controllerStatusUpdate")));
controllerErrorKey = keyBuilder.controllerTaskError("TestDB_error");
accessor.setProperty(controllerErrorKey, new Error(new ZNRecord("controllerError")));
// run dump task, should remove existing statusUpdate/error paths
task.run();
Assert.assertFalse(baseAccessor.exists(controllerStatusUpdateKey.getPath(), 0));
Assert.assertFalse(baseAccessor.exists(controllerErrorKey.getPath(), 0));
Assert.assertFalse(baseAccessor.exists(statusUpdateKey.getPath(), 0));
Assert.assertFalse(baseAccessor.exists(errorKey.getPath(), 0));
controllerStatusUpdateKey = keyBuilder.controllerTaskStatuses();
Assert.assertTrue(baseAccessor.exists(controllerStatusUpdateKey.getPath(), 0));
controllerErrorKey = keyBuilder.controllerTaskErrors();
Assert.assertTrue(baseAccessor.exists(controllerErrorKey.getPath(), 0));
statusUpdateKey = keyBuilder.stateTransitionStatus("localhost_12918");
Assert.assertTrue(baseAccessor.exists(statusUpdateKey.getPath(), 0));
errorKey = keyBuilder.stateTransitionErrors("localhost_12918");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations