Search in sources :

Example 56 with PropertyKey

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);
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) PropertyKey(org.apache.helix.PropertyKey) IdealState(org.apache.helix.model.IdealState) HashSet(java.util.HashSet)

Example 57 with PropertyKey

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;
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) PropertyKey(org.apache.helix.PropertyKey) ParticipantHistory(org.apache.helix.model.ParticipantHistory)

Example 58 with PropertyKey

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);
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HelixManager(org.apache.helix.HelixManager) Resource(org.apache.helix.model.Resource) ArrayList(java.util.ArrayList) PartitionStateMap(org.apache.helix.controller.common.PartitionStateMap) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) PartitionStateMap(org.apache.helix.controller.common.PartitionStateMap) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey) ClusterConfig(org.apache.helix.model.ClusterConfig)

Example 59 with PropertyKey

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;
}
Also used : PropertyKey(org.apache.helix.PropertyKey) ParticipantHistory(org.apache.helix.model.ParticipantHistory)

Example 60 with PropertyKey

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()));
}
Also used : HelixManager(org.apache.helix.HelixManager) Error(org.apache.helix.model.Error) Date(java.util.Date) StatusUpdate(org.apache.helix.model.StatusUpdate) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

PropertyKey (org.apache.helix.PropertyKey)60 HelixDataAccessor (org.apache.helix.HelixDataAccessor)31 ZNRecord (org.apache.helix.ZNRecord)24 ArrayList (java.util.ArrayList)17 Test (org.testng.annotations.Test)17 Builder (org.apache.helix.PropertyKey.Builder)16 Message (org.apache.helix.model.Message)16 HelixManager (org.apache.helix.HelixManager)15 IdealState (org.apache.helix.model.IdealState)10 StringWriter (java.io.StringWriter)8 Criteria (org.apache.helix.Criteria)8 CurrentState (org.apache.helix.model.CurrentState)8 HelixProperty (org.apache.helix.HelixProperty)7 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)7 ExternalView (org.apache.helix.model.ExternalView)7 LiveInstance (org.apache.helix.model.LiveInstance)7 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)7 SerializationConfig (org.codehaus.jackson.map.SerializationConfig)7 HashSet (java.util.HashSet)6 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)6