Search in sources :

Example 81 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class ServerContext method getDataAccssor.

public HelixDataAccessor getDataAccssor(String clusterName) {
    synchronized (_helixDataAccessorPool) {
        if (!_helixDataAccessorPool.containsKey(clusterName)) {
            ZkBaseDataAccessor<ZNRecord> baseDataAccessor = new ZkBaseDataAccessor<>(getZkClient());
            _helixDataAccessorPool.put(clusterName, new ZKHelixDataAccessor(clusterName, InstanceType.ADMINISTRATOR, baseDataAccessor));
        }
        return _helixDataAccessorPool.get(clusterName);
    }
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 82 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class FileStoreStateModel method onBecomeMasterFromSlave.

/**
 * When the node becomes master, it will start accepting writes and increments
 * the epoch and starts logging the changes in a file
 * @param message
 * @param context
 * @throws Exception
 */
@Transition(from = "SLAVE", to = "MASTER")
public void onBecomeMasterFromSlave(final Message message, NotificationContext context) throws Exception {
    replicator.stop();
    System.out.println(_serverId + " transitioning from " + message.getFromState() + " to " + message.getToState() + " for " + _partition);
    ZkHelixPropertyStore<ZNRecord> helixPropertyStore = context.getManager().getHelixPropertyStore();
    String checkpointDirPath = instanceConfig.getRecord().getSimpleField("check_point_dir");
    CheckpointFile checkpointFile = new CheckpointFile(checkpointDirPath);
    final ChangeRecord lastRecordProcessed = checkpointFile.findLastRecordProcessed();
    DataUpdater<ZNRecord> updater = new HighWaterMarkUpdater(message, lastRecordProcessed);
    helixPropertyStore.update("/TRANSACTION_ID_METADATA" + "/" + message.getResourceName(), updater, AccessOption.PERSISTENT);
    Stat stat = new Stat();
    ;
    ZNRecord znRecord = helixPropertyStore.get("/TRANSACTION_ID_METADATA" + "/" + message.getResourceName(), stat, AccessOption.PERSISTENT);
    int startGen = Integer.parseInt(znRecord.getSimpleField("currentGen"));
    int startSeq = Integer.parseInt(znRecord.getSimpleField("currentGenStartSeq"));
    String fileStoreDir = instanceConfig.getRecord().getSimpleField("file_store_dir");
    String changeLogDir = instanceConfig.getRecord().getSimpleField("change_log_dir");
    generator = new ChangeLogGenerator(changeLogDir, startGen, startSeq);
    // To indicate that we need callbacks for changes that happen starting now
    long now = System.currentTimeMillis();
    service = new FileSystemWatchService(fileStoreDir, now, generator);
    service.start();
    System.out.println(_serverId + " transitioned from " + message.getFromState() + " to " + message.getToState() + " for " + _partition);
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZNRecord(org.apache.helix.ZNRecord) Transition(org.apache.helix.participant.statemachine.Transition)

Example 83 with ZNRecord

use of org.apache.helix.ZNRecord 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();
        }
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ZNRecord(org.apache.helix.ZNRecord) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 84 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class ScheduledTaskStateModel method onBecomeCompletedFromOffline.

@Transition(to = "COMPLETED", from = "OFFLINE")
public void onBecomeCompletedFromOffline(Message message, NotificationContext context) throws InterruptedException {
    logger.info(_partitionKey + " onBecomeCompletedFromOffline");
    // Construct the inner task message from the mapfields of scheduledTaskQueue resource group
    Map<String, String> messageInfo = message.getRecord().getMapField(Message.Attributes.INNER_MESSAGE.toString());
    ZNRecord record = new ZNRecord(_partitionKey);
    record.getSimpleFields().putAll(messageInfo);
    Message taskMessage = new Message(record);
    if (logger.isDebugEnabled()) {
        logger.debug(taskMessage.getRecord().getSimpleFields().toString());
    }
    MessageHandler handler = _executor.createMessageHandler(taskMessage, new NotificationContext(null));
    if (handler == null) {
        throw new HelixException("Task message " + taskMessage.getMsgType() + " handler not found, task id " + _partitionKey);
    }
    // Invoke the internal handler to complete the task
    handler.handleMessage();
    logger.info(_partitionKey + " onBecomeCompletedFromOffline completed");
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Message(org.apache.helix.model.Message) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) ZNRecord(org.apache.helix.ZNRecord)

Example 85 with ZNRecord

use of org.apache.helix.ZNRecord in project helix by apache.

the class ZKPathDataDumpTask method dump.

void dump(BaseDataAccessor<ZNRecord> accessor, String ancestorPath, long threshold, int maxLeafCount) {
    List<String> leafPaths = scanPath(accessor, ancestorPath);
    if (leafPaths.isEmpty()) {
        return;
    }
    Stat[] stats = accessor.getStats(leafPaths, 0);
    List<String> dumpPaths = Lists.newArrayList();
    long now = System.currentTimeMillis();
    for (int i = 0; i < stats.length; i++) {
        Stat stat = stats[i];
        if ((stats.length > maxLeafCount) || ((now - stat.getMtime()) > threshold)) {
            dumpPaths.add(leafPaths.get(i));
        }
    }
    if (!dumpPaths.isEmpty()) {
        LOG.info("Dump statusUpdates and errors records for paths: " + dumpPaths);
        List<ZNRecord> dumpRecords = accessor.get(dumpPaths, null, 0);
        for (ZNRecord record : dumpRecords) {
            if (record != null) {
                LOG.info(new String(_jsonSerializer.serialize(record)));
            }
        }
        // clean up
        accessor.remove(dumpPaths, 0);
        LOG.info("Remove statusUpdates and errors records for paths: " + dumpPaths);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

ZNRecord (org.apache.helix.ZNRecord)448 Test (org.testng.annotations.Test)186 ArrayList (java.util.ArrayList)117 Date (java.util.Date)111 HelixDataAccessor (org.apache.helix.HelixDataAccessor)91 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)78 Builder (org.apache.helix.PropertyKey.Builder)75 HashMap (java.util.HashMap)72 IdealState (org.apache.helix.model.IdealState)69 PropertyKey (org.apache.helix.PropertyKey)61 HelixException (org.apache.helix.HelixException)47 Map (java.util.Map)41 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)40 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)40 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)33 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)30 List (java.util.List)29 ZkClient (org.apache.helix.manager.zk.ZkClient)29 HelixAdmin (org.apache.helix.HelixAdmin)28 LiveInstance (org.apache.helix.model.LiveInstance)28