Search in sources :

Example 26 with HelixManager

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

the class DummyProcess method main.

public static void main(String[] args) throws Exception {
    String cmType = "zk";
    String zkConnectString = "localhost:2181";
    String clusterName = "testCluster";
    String instanceName = "localhost_8900";
    String cvFileStr = null;
    // String rootNs = null;
    int delay = 0;
    if (args.length > 0) {
        CommandLine cmd = processCommandLineArgs(args);
        zkConnectString = cmd.getOptionValue(zkServer);
        clusterName = cmd.getOptionValue(cluster);
        String host = cmd.getOptionValue(hostAddress);
        String portString = cmd.getOptionValue(hostPort);
        int port = Integer.parseInt(portString);
        instanceName = host + "_" + port;
        cmType = cmd.getOptionValue(helixManagerType);
        if (cmd.hasOption(transDelay)) {
            try {
                delay = Integer.parseInt(cmd.getOptionValue(transDelay));
                if (delay < 0) {
                    throw new Exception("delay must be positive");
                }
            } catch (Exception e) {
                e.printStackTrace();
                delay = 0;
            }
        }
    }
    // Espresso_driver.py will consume this
    logger.info("Dummy process started, instanceName:" + instanceName);
    DummyProcess process = new DummyProcess(zkConnectString, clusterName, instanceName, cmType, delay);
    HelixManager manager = process.start();
    try {
        Thread.currentThread().join();
    } catch (InterruptedException e) {
        // ClusterManagerFactory.disconnectManagers(instanceName);
        logger.info("participant:" + instanceName + ", " + Thread.currentThread().getName() + " interrupted");
    // if (manager != null)
    // {
    // manager.disconnect();
    // }
    } finally {
        if (manager != null) {
            manager.disconnect();
        }
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) HelixManager(org.apache.helix.HelixManager) ParseException(org.apache.commons.cli.ParseException)

Example 27 with HelixManager

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

the class StoreAccessDiffNodeTransition method doTransition.

@Override
public void doTransition(Message message, NotificationContext context) {
    HelixManager manager = context.getManager();
    ZkHelixPropertyStore<ZNRecord> store = manager.getHelixPropertyStore();
    final String setPath = "/TEST_PERF/set/" + message.getPartitionName();
    final String updatePath = "/TEST_PERF/update/" + message.getPartitionName();
    // final String key = message.getPartitionName();
    try {
        // get/set once
        ZNRecord record = null;
        try {
            record = store.get(setPath, null, 0);
        } catch (ZkNoNodeException e) {
        // record = new ZNRecord(setPath);
        }
        if (record == null) {
            record = new ZNRecord(setPath);
        }
        record.setSimpleField("setTimestamp", "" + System.currentTimeMillis());
        store.set(setPath, record, AccessOption.PERSISTENT);
        // update once
        store.update(updatePath, new DataUpdater<ZNRecord>() {

            @Override
            public ZNRecord update(ZNRecord currentData) {
                if (currentData == null) {
                    currentData = new ZNRecord(updatePath);
                }
                currentData.setSimpleField("updateTimestamp", "" + System.currentTimeMillis());
                return currentData;
            }
        }, AccessOption.PERSISTENT);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZNRecord(org.apache.helix.ZNRecord) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException)

Example 28 with HelixManager

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

the class StoreAccessOneNodeTransition method doTransition.

@Override
public void doTransition(Message message, NotificationContext context) {
    HelixManager manager = context.getManager();
    ZkHelixPropertyStore<ZNRecord> store = manager.getHelixPropertyStore();
    final String setPath = "/TEST_PERF/set";
    final String updatePath = "/TEST_PERF/update";
    final String key = message.getPartitionName();
    try {
        // get/set once
        ZNRecord record = null;
        try {
            record = store.get(setPath, null, 0);
        } catch (ZkNoNodeException e) {
            record = new ZNRecord(setPath);
        }
        record.setSimpleField("setTimestamp", "" + System.currentTimeMillis());
        store.set(setPath, record, AccessOption.PERSISTENT);
        // update once
        store.update(updatePath, new DataUpdater<ZNRecord>() {

            @Override
            public ZNRecord update(ZNRecord currentData) {
                if (currentData == null) {
                    currentData = new ZNRecord(updatePath);
                }
                currentData.setSimpleField(key, "" + System.currentTimeMillis());
                return currentData;
            }
        }, AccessOption.PERSISTENT);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZNRecord(org.apache.helix.ZNRecord) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException)

Example 29 with HelixManager

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

the class TaskAdmin method main.

/**
 * Parses the first argument as a driver command and the rest of the
 * arguments are parsed based on that command. Constructs a Helix
 * message and posts it to the controller
 */
public static void main(String[] args) throws Exception {
    String[] cmdArgs = Arrays.copyOfRange(args, 1, args.length);
    CommandLine cl = parseOptions(cmdArgs, constructOptions(), args[0]);
    String zkAddr = cl.getOptionValue(ZK_ADDRESS);
    String clusterName = cl.getOptionValue(CLUSTER_NAME_OPTION);
    String workflow = cl.getOptionValue(RESOURCE_OPTION);
    if (zkAddr == null || clusterName == null || workflow == null) {
        printUsage(constructOptions(), "[cmd]");
        throw new IllegalArgumentException("zk, cluster, and resource must all be non-null for all commands");
    }
    HelixManager helixMgr = HelixManagerFactory.getZKHelixManager(clusterName, "Admin", InstanceType.ADMINISTRATOR, zkAddr);
    helixMgr.connect();
    TaskDriver driver = new TaskDriver(helixMgr);
    try {
        TaskDriver.DriverCommand cmd = TaskDriver.DriverCommand.valueOf(args[0]);
        switch(cmd) {
            case start:
                if (cl.hasOption(WORKFLOW_FILE_OPTION)) {
                    driver.start(Workflow.parse(new File(cl.getOptionValue(WORKFLOW_FILE_OPTION))));
                } else {
                    throw new IllegalArgumentException("Workflow file is required to start flow.");
                }
                break;
            case stop:
                driver.stop(workflow);
                break;
            case resume:
                driver.resume(workflow);
                break;
            case delete:
                driver.delete(workflow);
                break;
            case list:
                list(driver, workflow);
                break;
            case flush:
                driver.flushQueue(workflow);
                break;
            case clean:
                driver.cleanupQueue(workflow);
                break;
            default:
                throw new IllegalArgumentException("Unknown command " + args[0]);
        }
    } catch (IllegalArgumentException e) {
        LOG.error("Unknown driver command " + args[0]);
        throw e;
    }
    helixMgr.disconnect();
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) HelixManager(org.apache.helix.HelixManager) TaskDriver(org.apache.helix.task.TaskDriver) File(java.io.File)

Example 30 with HelixManager

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

the class HelixStateTransitionHandler method handleMessage.

@Override
public HelixTaskResult handleMessage() {
    NotificationContext context = _notificationContext;
    Message message = _message;
    synchronized (_stateModel) {
        HelixTaskResult taskResult = new HelixTaskResult();
        HelixManager manager = context.getManager();
        _statusUpdateUtil.logInfo(message, HelixStateTransitionHandler.class, "Message handling task begin execute", manager);
        message.setExecuteStartTimeStamp(new Date().getTime());
        try {
            preHandleMessage();
            invoke(manager, context, taskResult, message);
        } catch (HelixStateMismatchException e) {
            // Simply log error and return from here if State mismatch.
            // The current state of the state model is intact.
            taskResult.setSuccess(false);
            taskResult.setMessage(e.toString());
            taskResult.setException(e);
        } catch (Exception e) {
            String errorMessage = "Exception while executing a state transition task " + message.getPartitionName();
            logger.error(errorMessage, e);
            if (e.getCause() != null && e.getCause() instanceof InterruptedException) {
                e = (InterruptedException) e.getCause();
            }
            if (e instanceof HelixRollbackException || (e.getCause() != null && e.getCause() instanceof HelixRollbackException)) {
                // TODO : Support cancel to any state
                logger.info("Rollback happened of state transition on resource \"" + _message.getResourceName() + "\" partition \"" + _message.getPartitionName() + "\" from \"" + _message.getFromState() + "\" to \"" + _message.getToState() + "\"");
                taskResult.setCancelled(true);
            } else {
                _statusUpdateUtil.logError(message, HelixStateTransitionHandler.class, e, errorMessage, manager);
                taskResult.setSuccess(false);
                taskResult.setMessage(e.toString());
                taskResult.setException(e);
                taskResult.setInterrupted(e instanceof InterruptedException);
            }
        }
        taskResult.setCompleteTime(System.currentTimeMillis());
        // add task result to context for postHandling
        context.add(MapKey.HELIX_TASK_RESULT.toString(), taskResult);
        postHandleMessage();
        return taskResult;
    }
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) HelixRollbackException(org.apache.helix.HelixRollbackException) Date(java.util.Date) HelixException(org.apache.helix.HelixException) HelixRollbackException(org.apache.helix.HelixRollbackException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

HelixManager (org.apache.helix.HelixManager)115 Test (org.testng.annotations.Test)49 HelixDataAccessor (org.apache.helix.HelixDataAccessor)35 ZNRecord (org.apache.helix.ZNRecord)28 Message (org.apache.helix.model.Message)23 PropertyKey (org.apache.helix.PropertyKey)20 Date (java.util.Date)18 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)17 Builder (org.apache.helix.PropertyKey.Builder)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)12 HelixException (org.apache.helix.HelixException)11 ExternalView (org.apache.helix.model.ExternalView)11 NotificationContext (org.apache.helix.NotificationContext)10 LiveInstance (org.apache.helix.model.LiveInstance)10 IdealState (org.apache.helix.model.IdealState)9 Criteria (org.apache.helix.Criteria)8 HelixAdmin (org.apache.helix.HelixAdmin)8 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)8 StringWriter (java.io.StringWriter)7