@Override public Qualifier<C> getQualifier() { return delegate.getQualifier(); }
@Override public R execute(C command, NodeContext nodeContext) { LogWriter logWriter = nodeContext.getService(LogWriter.class); try { log.debug("Execute command {} on {}", command, nodeContext.getId()); return delegate.execute(command, nodeContext); } finally { log.debug("Flushing results execution command {} on {}", command, nodeContext.getId()); logWriter.flush(); } } }
private static <C extends Command<R>, R extends Serializable> void executeCommand(CommandExecutor<C, R> executor, ZNode executorNode, final QueueEntry<C, R> entry, final NodeContext nodeContext) { String relativePath = entry.getResultPath().substring(executorNode.getPath().length() + 1); final ZNode output = executorNode.child(relativePath); final NodeCommandExecutionListener<C> listener = entry.getListener(); try { C command = entry.getCommand(); listener.onCommandExecutionStarted(command, nodeContext); R result = executor.execute(command, nodeContext); log.debug("Command {} executed", command); listener.onCommandExecuted(command); output.setObject(CommandExecutionResult.success(result)); } catch (Throwable throwable) { // todo add fail event log.error("error during task execution", throwable); output.setObject(CommandExecutionResult.fail(throwable)); } }
public Set<Qualifier<Command<Serializable>>> getQualifiers() { Set<Qualifier<Command<Serializable>>> qualifiers = Sets.newHashSet(); for (CommandExecutor commandExecutor : getExecutors()) { qualifiers.add(commandExecutor.getQualifier()); } return qualifiers; }
@Override public boolean canExecuteCommands(NodeId nodeId, Set<Qualifier<?>> qualifiers) throws CoordinatorException { if (qualifiers == null || qualifiers.isEmpty()) { return true; } Set<CommandExecutor<?, ?>> commandExecutors = nodes.get(nodeId).getSecond(); if (commandExecutors == null) throw new CoordinatorException("Node with id " + nodeId + " is not found"); Set<Qualifier<?>> qualifiersCopy = Sets.newHashSet(qualifiers); for (CommandExecutor<?, ?> commandExecutor : commandExecutors) { if (qualifiersCopy.remove(commandExecutor.getQualifier()) && qualifiersCopy.isEmpty()) { return true; } } return false; }
@Override public void registerNode(NodeContext nodeContext, Set<Worker> workers, final StatusChangeListener listener) { log.info("Going to register node {} with {} workers", nodeContext.getId(), workers.size()); Set<CommandExecutor<?, ?>> executors = Sets.newHashSet(); Set<Qualifier<?>> qualifiers = Sets.newHashSet(); for (Worker worker : workers) { for (CommandExecutor<?, ?> executor : worker.getExecutors()) { Qualifier<?> qualifier = executor.getQualifier(); if (qualifiers.contains(qualifier)) { throw new CoordinatorException("Executor for qualifier " + qualifier + " is already registered"); } executors.add(executor); } } nodes.put(nodeContext.getId(), Pair.of(nodeContext, executors)); }
private <C extends Command<R>, R extends Serializable> CommandExecutor<C, R> getCommandExecutor(C command){ for (CommandExecutor<?, ?> commandExecutor : nodePair.getSecond()) { final CommandExecutor<C, R> executor = (CommandExecutor<C, R>) commandExecutor; if (executor.getQualifier().equals(Qualifier.of(command))) { return executor; } } throw new CoordinatorException("Command " + command + " is not available on " + nodeId.getIdentifier()); } };
public void init() { final Map<Qualifier, CommandExecutor> commandsRunner = Maps.newHashMap(); for (CommandExecutor commandExecutor : worker.getExecutors()) { commandsRunner.put(commandExecutor.getQualifier(), commandExecutor); } AsyncRunner<Command<Serializable>, Serializable> incomingCommandRunner = new AsyncRunner<Command<Serializable>, Serializable>() { @Override public void run(final Command<Serializable> command, final AsyncCallback<Serializable> callback) { executor.execute(new Runnable() { @Override public void run() { try { Serializable result = commandsRunner.get(Qualifier.of(command)).execute(command, nodeContext); callback.onSuccess(result); } catch (Throwable e) { callback.onFailure(e); } } }); } }; exchangeClient = new ExchangeClient(executor, incomingCommandRunner, nodeContext); exchangeClient.setNodeContext(nodeContext); exchangeClient.setHttpClient(httpClient); exchangeClient.setUrlBase(urlBase); exchangeClient.setUrlExchangePack(urlExchangePack); exchangeClient.setUrlRegistration(urlRegistration); }
Qualifier<?> qualifier = executor.getQualifier(); if (qualifiers.contains(qualifier)) { throw new CoordinatorException("Executor for qualifier " + qualifier + " is already registered");
private <C extends Command<R>, R extends Serializable> void registerExecutor(final NodeContext nodeContext, final CommandExecutor<C, R> executor, ZNode node) { final ZNode executorNode = node.createChild(znode().withPath(nodeNameOf(executor.getQualifier()))); final ZNode queueNode = executorNode.createChild(znode().withPath("queue")); executorNode.createChild(znode().withPath("result")); log.debug("Created znodes for executor {}", executorNode.getPath()); queueNode.addChildrenWatcher(new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() != Event.EventType.NodeChildrenChanged) { return; } synchronized (lock) { if (log.isDebugEnabled()) { log.debug("Children changed {} event type {}", queueNode.getPath(), event.getType()); } List<QueueEntry<C, R>> entries = getEntries(queueNode, this); for (final QueueEntry<C, R> entry : entries) { Runnable run = new Runnable() { @Override public void run() { executeCommand(executor, executorNode, entry, nodeContext); } }; ZookeeperCoordinator.this.executor.execute(run); } } } }); }