gpt4 book ai didi

com.griddynamics.jagger.coordinator.zookeeper.ZNode类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 12:26:55 27 4
gpt4 key购买 nike

本文整理了Java中com.griddynamics.jagger.coordinator.zookeeper.ZNode类的一些代码示例,展示了ZNode类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZNode类的具体详情如下:
包路径:com.griddynamics.jagger.coordinator.zookeeper.ZNode
类名称:ZNode

ZNode介绍

[英]A simple API for interaction with zookeeper's znodes.
[中]一个与zookeeper的znodes交互的简单API。

代码示例

代码示例来源:origin: griddynamics/jagger

@Override
public Set<NodeId> getAvailableNodes(NodeType type) {
  Set<NodeId> result = Sets.newHashSet();
  ZNode typeNode = rootNode.child(CoordinationUtil.nodeNameOf(type));
  for (ZNode node : typeNode.children()) {
    if (node.hasChild(CoordinationUtil.AVAILABLE_NODE_NAME)) {
      result.add(NodeId.of(type, node.getShortPath()));
    }
  }
  return result;
}

代码示例来源:origin: griddynamics/jagger

@Override
public void terminate() {
  log.debug("termination signal received");
  if(zooKeeperServer != null) {
    try {
      if (zoo != null) {
        zoo.root().child(rootNode).removeWithChildren();
      }
      zooKeeperServer.shutdown();
    } catch (Exception e) {
      log.warn("Error during zookeeper termination. Message: {}", e.getMessage());
    }
  }
}

代码示例来源:origin: griddynamics/jagger

@Override
public void waitForInitialization() {
  log.info("Waiting for coordination znode structure structure initialization");
  while (true) {
    boolean initialized = rootNode.exists() && rootNode.hasChild(CoordinationUtil.STATUSES_NODE_NAME);
    if (initialized) {
      log.info("Coordination znode structure initialized");
      break;
    }
    try {
      Thread.sleep(INITIALIZATION_SLEEP_PERIOD);
      log.info("Znode structure is not initialized. Waiting {} ms", INITIALIZATION_SLEEP_PERIOD);
    } catch (InterruptedException e) {
      log.warn("Sleep interrupted", e);
    }
  }
}

代码示例来源:origin: griddynamics/jagger

private <C extends Command<R>, R extends Serializable> List<QueueEntry<C, R>> getEntries(ZNode queueNode, Watcher watcher) {
  List<QueueEntry<C, R>> result = Lists.newLinkedList();
  List<ZNode> children = queueNode.firstLevelChildren(watcher);
  Collections.sort(children, new Comparator<ZNode>() {
    @Override
    public int compare(ZNode first, ZNode second) {
      return first.getPath().compareTo(second.getPath());
    }
  });
  for (ZNode child : children) {
    QueueEntry<C, R> entry = child.getObject(QueueEntry.class, classLoaderHolder.get());
    child.remove();
    result.add(entry);
  }
  return result;
}

代码示例来源:origin: griddynamics/jagger

log.info("Going to register node {} with {} workers", nodeContext.getId(), workers.size());
ZNode typeNode = rootNode.child(CoordinationUtil.nodeNameOf(nodeContext.getId().getType()));
if (typeNode.hasChild(nodeContext.getId().getIdentifier())) {
  typeNode.child(nodeContext.getId().getIdentifier()).removeWithChildren();
ZNode node = typeNode.createChild(znode().withPath(nodeContext.getId().getIdentifier()));
rootNode.addNodeWatcher(new Watcher() {
  @Override
  public void process(WatchedEvent event) {
ZNode statuses = rootNode.child(CoordinationUtil.STATUSES_NODE_NAME);
statuses.createChild(znode().ephemeralSequential().withDataObject(nodeContext.getId()));
  Collection<NodeId> nodeIds = Sets.newHashSet();
  StatusWatcher statusWatcher = new StatusWatcher(statuses, lock, nodeIds, listener);
  List<ZNode> nodes = statuses.children(statusWatcher);
  for (ZNode zNode : nodes) {
    nodeIds.add(zNode.getObject(NodeId.class));
node.createChild(znode().withPath(CoordinationUtil.AVAILABLE_NODE_NAME));

代码示例来源:origin: griddynamics/jagger

public void initialize() {
  ZooKeeperFactory zooKeeperFactory = new ZooKeeperFactory();
  zooKeeperFactory.setConnectString(endpoint);
  zooKeeperFactory.setSessionTimeout(sessionTimeout);
  log.info("Connect to {} endpoint with timeout {}", endpoint, sessionTimeout);
  IZookeeper zooKeeper = null;
  try {
    zooKeeper = zooKeeperFactory.create();
    zoo = new Zoo(zooKeeperFactory.create());
    // TODO: timeout only 40000. svi.
    if (zoo.root().hasChild(rootNode)) {
      log.info("ZNode [" + rootNode + "] was found.");
      zoo.root().child(rootNode).removeWithChildren();
      log.info("ZNode [" + rootNode + "] with children nodes were removed.");
    }
    zoo.root().createChild(znode().withPath(rootNode));
    log.info("ZNode [" + rootNode + "] was created.");
  } finally {
    if (zooKeeper != null) {
      try {
        zooKeeper.close();
      } catch (InterruptedException e) {
        // do nothing
      }
    }
  }
}

代码示例来源:origin: griddynamics/jagger

@Override
public void lock() {
  while (true) {
    ZNode node = lockDir().createChild(znode().ephemeralSequential());
    final String currentNodeName = node.getShortPath();
    int currentFlag = Integer.valueOf(currentNodeName);
    final List<ZNode> children = lockDir().children();
    int lowestNodeVal = Integer.MAX_VALUE;
    int nextNodeFlag = -1;
      String childPath = child.getShortPath();
      int childFlag = Integer.valueOf(childPath);
    boolean hasChild = lockDir().hasChild(nextNodePath, new Watcher() {
      @Override
      public void process(WatchedEvent event) {
    node.remove();

代码示例来源:origin: griddynamics/jagger

@Override
public <C extends Command<R>, R extends Serializable> void run(final C command, final NodeCommandExecutionListener<C> listener, final AsyncCallback<R> callback) {
  ZNode commandNode = rootNode.child(nodeId.getType().name().toLowerCase()).child(nodeId.getIdentifier()).child(command.getClass().getName());
  ZNode queueNode = commandNode.child("queue");
  ZNode resultNode = commandNode.child("result");
  final ZNode outputNode = resultNode.createChild(znode().persistentSequential());
  outputNode.addNodeWatcher(new Watcher() {
    @Override
    public void process(WatchedEvent event) {
  queueNode.createChild(
      znode()
          .persistentSequential()
          .withDataObject(new QueueEntry<C, R>(command, listener, outputNode.getPath()))
  );
  log.debug("command {} is ready to be executed", command);

代码示例来源:origin: griddynamics/jagger

@Override
public boolean canExecuteCommands(NodeId nodeId, Set<Qualifier<?>> qualifiers) {
  ZNode typeNode = rootNode.child(CoordinationUtil.nodeNameOf(nodeId.getType()));
  String identifier = nodeId.getIdentifier();
  if (!typeNode.hasChild(identifier)) {
    throw new CoordinatorException("Node with id " + nodeId + " is not found");
  }
  ZNode node = typeNode.child(identifier);
  if (!node.hasChild(CoordinationUtil.AVAILABLE_NODE_NAME)) {
    return false;
  }
  for (Qualifier<?> qualifier : qualifiers) {
    if (!node.hasChild(nodeNameOf(qualifier))) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: griddynamics/jagger

@Override
public void initialize() {
  log.info("Going to initialize required znode structure in zookeeper");
  for (NodeType type : NodeType.values()) {
    String child = CoordinationUtil.nodeNameOf(type);
    if (!rootNode.hasChild(child)) {
      rootNode.createChild(znode().withPath(child));
      log.info("Created Zookeeper node {}", child);
    }
  }

  if (!rootNode.hasChild(CoordinationUtil.STATUSES_NODE_NAME)) {
    rootNode.createChild(znode().withPath(CoordinationUtil.STATUSES_NODE_NAME));
    log.info("Created Zookeeper node {}", CoordinationUtil.STATUSES_NODE_NAME);
  }
  log.info("Successfully initialized");
}

代码示例来源:origin: griddynamics/jagger

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));
  }
}

代码示例来源:origin: griddynamics/jagger

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);
        }
      }
    }
  });
}

代码示例来源:origin: griddynamics/jagger

@Override
  public void process(WatchedEvent event) {
    log.debug("command {} execution done", command);
    CommandExecutionResult result = outputNode.getObject(CommandExecutionResult.class);
    switch (result.getStatus()) {
      case SUCCEEDED:
        log.debug("success");
        callback.onSuccess((R) result.getResult());
        break;
      case FAILED:
        Throwable e = result.getException();
        log.error("fail", e);
        callback.onFailure(e);
        break;
      default:
        throw new IllegalStateException("Unknown status");
    }
    outputNode.removeWithChildren();
  }
});

代码示例来源:origin: griddynamics/jagger

@Override
public boolean isLockable() {
  return node.hasChild(lockPath);
}

代码示例来源:origin: griddynamics/jagger

private ZNode lockDir() {
  return node.child(lockPath);
}

代码示例来源:origin: griddynamics/jagger

public void run() {
    lock.lock();
    try {
      List<ZNode> children = node.children();
      Collection<NodeId> newIds = Sets.newHashSet();
      for (ZNode child : children) {
        newIds.add(child.getObject(NodeId.class));
      }
      Collection<NodeId> copy = Sets.newHashSet(newIds);
      newIds.removeAll(currentIds);
      currentIds.removeAll(copy);
      for (NodeId newId : newIds) {
        statusChangeListener.onNodeStatusChanged(newId, NodeStatus.AVAILABLE);
      }
      for (NodeId newId : currentIds) {
        statusChangeListener.onNodeStatusChanged(newId, NodeStatus.DISCONNECTED);
      }
      currentIds = copy;
    } finally {
      lock.unlock();
    }
  }
};

代码示例来源:origin: griddynamics/jagger

@Override
public void removeWithChildren() {
  for (ZNode zNode : children()) {
    zNode.removeWithChildren();
  }
    remove();
}

代码示例来源:origin: griddynamics/jagger

@Override
public void makeLockable() {
  node.createChild(znode().withPath(lockPath));
}

代码示例来源:origin: griddynamics/jagger

@Override
public void waitForReady() {
  while (true) {
    try {
      rootNode.exists();
      break;
    } catch (Throwable e) {
      // do nothing
    }
    try {
      Thread.sleep(INITIALIZATION_SLEEP_PERIOD);
      log.info("Znode structure is not initialized. Waiting {} ms", INITIALIZATION_SLEEP_PERIOD);
    } catch (InterruptedException e) {
      log.warn("Sleep interrupted", e);
    }
  }
}

代码示例来源:origin: griddynamics/jagger

node.addChildrenWatcher(this);

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com