gpt4 book ai didi

org.jumbune.remoting.common.ZKUtils类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 09:32:22 29 4
gpt4 key购买 nike

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

ZKUtils介绍

[英]This class ZKUtils contains utility methods to communicate with zookeeper.
[中]此类ZKUtils包含与zookeeper通信的实用方法。

代码示例

代码示例来源:origin: Impetus/jumbune

/**
 * Sets the agent node data on znode.
 *
 * @param nodePath the node path
 * @param agent the Agent
 */
public static void setAgentData(String nodePath, AgentNode agent) {
  try {
    String hosts = null;
    CuratorConnector connector = CuratorConnector.getInstance(hosts);
    String json = convertAgentToGson(agent);
    connector.setData(nodePath, json.getBytes(Charset.forName("UTF-8")));
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);        
  }
}

代码示例来源:origin: Impetus/jumbune

/**
 * Gets the leader name node from zookeeper.
 * @param zkHosts 
 *
 * @return the leader name node from zookeeper
 */
public static byte[] getLeaderNameNodeFromZK(String[] zkHosts) {
  final int retryTimes = 10;
  byte[] nameHostName = null;
  int retryCount = 1;
  do {
    List<String> nameNodeNameList = getNodeChildrenList(zkHosts, ZKConstants.HADOOP_NODE_PATH);
    for (String nameNodeName : nameNodeNameList) {
      nameHostName = getActiveNameNode(zkHosts,
          ZKConstants.HADOOP_NODE_PATH + File.separator + nameNodeName + ZKConstants.ACTIVE_BREAD_CRUMB);
    }
    if (nameHostName == null) {
      try {
        LOGGER.warn("found active NN null from ZK, retrying after " + 100 * retryCount + " millisecs");
        Thread.sleep(100 * retryCount);
      } catch (InterruptedException e) {
      }
    }
    retryCount++;
  } while (nameHostName == null && retryCount <= retryTimes);
  return nameHostName;
}

代码示例来源:origin: Impetus/jumbune

public static String getActiveNNHost(String[] zkHosts){
  byte[] activeHost = null;
  String activeNameNode = null ;
  activeHost = ZKUtils.getLeaderNameNodeFromZK(zkHosts);
  try {
    ActiveNodeInfo activeNodeInfo = PARSER.parsePartialFrom(activeHost);
    activeNameNode = activeNodeInfo.getHostname();
  } catch (InvalidProtocolBufferException e) {
    LOGGER.error(e);            
  }
  return activeNameNode;
}

代码示例来源:origin: Impetus/jumbune

/**
 * Checks if is leader agent on nn.
 *
 * @return true, if is leader agent on nn
 */
private boolean isLeaderAgentOnNN() {
  AgentNode agent = null;
  try {
    agent = ZKUtils.getLeaderAgentfromZK(zkHosts, haConf);
    if(agent == null) {
      return false;
    }
  } catch (ConnectException e) {
    logger.error(e.getMessage(), e); 
  }
  return agent.getHost().equals(ZKUtils.getActiveNNHost(zkHosts));
}

代码示例来源:origin: Impetus/jumbune

leaderAgentNode = getAgentData(zkHosts, ZKConstants.AGENT_LEADER_PATH);
boolean connectionFlag = true;
Socket socket = null;
  LOGGER.warn("connection failed for - " + leaderAgentNode.getHost()+":"+leaderAgentNode.getPort());
  connectionFlag = retryConnection(zkHosts, leaderAgentNode, haConf);		
} catch (IOException e) {
} finally {

代码示例来源:origin: Impetus/jumbune

/**
 * Gets the agent data from znode.
 * @param zkHosts 
 *
 * @param nodePath the node path
 * @return the agent node data
 */
public static AgentNode getAgentData(String[] zkHosts, String nodePath){		
  CuratorConnector connector = CuratorConnector.getInstance(zkHosts);
  byte data[] = null;
  final int retryTimes = 10;
  int retryCount = 1;
  AgentNode agent = null;
  try {
    do {
      data = connector.getDataBytes(nodePath);        
      if(data == null) {
        LOGGER.warn("found active Agent null from ZK, retrying after " + 100 * retryCount + " millisecs");
        Thread.sleep(100*retryCount);
      }
    retryCount++;
    } while(data == null && retryCount <= retryTimes);
    agent = convertGsonToAgent(new String(data, Charset.defaultCharset()));
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
  }
  return agent;
}

代码示例来源:origin: Impetus/jumbune

private static boolean retryConnection(String[] zkHosts, AgentNode leaderAgentNode, HAConfiguration haConf) {
  boolean connectionFlag = false; 
  Socket socket = null;
  int numRetries = haConf.getNumRetriesAgentConn();
  int reconnectMillis = haConf.getAgentConnMillis();
  for(int i = 1; i < numRetries && !connectionFlag; i++) {
    connectionFlag = true;
    leaderAgentNode = getAgentData(zkHosts, ZKConstants.AGENT_LEADER_PATH);
    try {
      socket = new Socket(leaderAgentNode.getHost(), leaderAgentNode.getPort());
    } catch (ConnectException e){
      connectionFlag = false;
      LOGGER.warn("connection failed for - " + leaderAgentNode.getHost()+":"+leaderAgentNode.getPort()+ " retrying in " + (i*reconnectMillis) +" millis");
    } catch (IOException e) {			
    } finally {
      if (socket != null && !socket.isClosed()) {
        try {
          socket.close();
        } catch (IOException e) {
        }
      }
    }
    try {
      Thread.sleep(i*reconnectMillis*1000);
    } catch (InterruptedException e) {		
    }
  }
  return connectionFlag;
}

代码示例来源:origin: Impetus/jumbune

/**
 * Update leader agent info.
 */
private void updateLeaderAgentInfo() {
  AgentNode agentNode = null;
  try {
    agentNode = ZKUtils.getLeaderAgentfromZK(zkHosts, haConf);
  } catch (ConnectException e1) {
  logger.error("Unable to find active agent(leader) from zk, queried on ensembles - " + Arrays.toString(zkHosts));
  }
  jac = new JumbuneAgentCommunicator(agentNode.getHost(), agentNode.getPort());
}

代码示例来源:origin: Impetus/jumbune

/**
 * Checks if is NN changed.
 *
 * @return true, if is NN changed
 */
private boolean isNNChanged() {
  return !HAUtil.getActiveNNHost(this.clusterName).equals(ZKUtils.getActiveNNHost(zkHosts));
}

代码示例来源:origin: Impetus/jumbune

@Override
public void run() {
  try {
    long startTimeInMilis = Calendar.getInstance().getTimeInMillis();
    LOGGER.info("Started rsync execution at "
        + Calendar.getInstance().getTime());
    // get agents node information from zookeeper
    List<AgentNode> agentNodes = ZKUtils.getAgents(new String[] {this.zkConnectionString});
    // iterate over agent list and run rsync command
    for (AgentNode agent : agentNodes) {
      LOGGER.debug("Agent Object :" + agent.toString());
      if (agent.getStatus().equals(AgentNodeStatus.LEADER)) {
        LOGGER.debug("Skipping rsync execution for Leader Agent Object :");
        continue;
      }
      if (agent.getPrivateKey() != null
          && !agent.getPrivateKey().trim().equals("")) {
        rsyncWithPasswordlessSSH(agent);
      }
    }
    LOGGER.info("Total rsync execution time : "+ (Calendar.getInstance().getTimeInMillis() - startTimeInMilis)+ "ms.");
  } catch (Exception e) {
    LOGGER.error("Rsync execution failed.", e);
  }
}

代码示例来源:origin: Impetus/jumbune

leaderAgentNode = getAgentData(zkHosts, ZKConstants.AGENT_LEADER_PATH);
boolean connectionFlag = true;
Socket socket = null;
  LOGGER.warn("connection failed for - " + leaderAgentNode.getHost()+":"+leaderAgentNode.getPort());
  connectionFlag = retryConnection(zkHosts, leaderAgentNode, haConf);		
} catch (IOException e) {
} finally {

代码示例来源:origin: Impetus/jumbune

/**
 * Update global state.
 */
private void updateGlobalState() {
  AgentNode agent = null;
  try {
    agent = ZKUtils.getLeaderAgentfromZK(zkHosts, haConf);
  } catch (ConnectException e) {
   logger.error(e.getMessage(), e); 
  }
  HAUtil.setActiveAgentHost(this.clusterName, agent.getHost());
  HAUtil.setActiveAgentPort(this.clusterName, agent.getPort());
  HAUtil.setActiveNNHost(this.clusterName, ZKUtils.getActiveNNHost(zkHosts));
}

代码示例来源:origin: Impetus/jumbune

/**
 * Gets the agent data from znode.
 * @param zkHosts 
 *
 * @param nodePath the node path
 * @return the agent node data
 */
public static AgentNode getAgentData(String[] zkHosts, String nodePath){		
  CuratorConnector connector = CuratorConnector.getInstance(zkHosts);
  byte data[] = null;
  final int retryTimes = 10;
  int retryCount = 1;
  AgentNode agent = null;
  try {
    do {
      data = connector.getDataBytes(nodePath);        
      if(data == null) {
        LOGGER.warn("found active Agent null from ZK, retrying after " + 100 * retryCount + " millisecs");
        Thread.sleep(100*retryCount);
      }
    retryCount++;
    } while(data == null && retryCount <= retryTimes);
    agent = convertGsonToAgent(new String(data, Charset.defaultCharset()));
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
  }
  return agent;
}

代码示例来源:origin: Impetus/jumbune

private static boolean retryConnection(String[] zkHosts, AgentNode leaderAgentNode, HAConfiguration haConf) {
  boolean connectionFlag = false; 
  Socket socket = null;
  int numRetries = haConf.getNumRetriesAgentConn();
  int reconnectMillis = haConf.getAgentConnMillis();
  for(int i = 1; i < numRetries && !connectionFlag; i++) {
    connectionFlag = true;
    leaderAgentNode = getAgentData(zkHosts, ZKConstants.AGENT_LEADER_PATH);
    try {
      socket = new Socket(leaderAgentNode.getHost(), leaderAgentNode.getPort());
    } catch (ConnectException e){
      connectionFlag = false;
      LOGGER.warn("connection failed for - " + leaderAgentNode.getHost()+":"+leaderAgentNode.getPort()+ " retrying in " + (i*reconnectMillis) +" millis");
    } catch (IOException e) {			
    } finally {
      if (socket != null && !socket.isClosed()) {
        try {
          socket.close();
        } catch (IOException e) {
        }
      }
    }
    try {
      Thread.sleep(i*reconnectMillis*1000);
    } catch (InterruptedException e) {		
    }
  }
  return connectionFlag;
}

代码示例来源:origin: Impetus/jumbune

private void updateLeaderAgentInfo() {
  AgentNode agentNode = null;
  try {
    agentNode = ZKUtils.getLeaderAgentfromZK(zkHosts, haConf);
  } catch (ConnectException e1) {
  logger.error("unable to find active agent(leader) from zk, queried on ensembles - " + Arrays.toString(zkHosts));
  }
  jac = new JumbuneAgentCommunicator(agentNode.getHost(), agentNode.getPort());
}

代码示例来源:origin: Impetus/jumbune

@Override
public void run() {
  String currentlyActiveNN = HAUtil.getActiveNNHost(this.clusterName);
  int failCount = 0;
  while (!stop) {
    if (!ZKUtils.getActiveNNHost(zkHosts).equals(currentlyActiveNN)) {
      failCount++;
    }
    try {
      Thread.sleep(5 * 1000);
    } catch (InterruptedException e) {
      logger.error("Thread interrupted while monitoring Namenode state");
    }
    if (failCount == nnZnodeRetries) {
      logger.warn("namenode dead !!! Applying recovery...");
      nnFailed = true;
      barrier.reset();
      break;
    }
  }
}

代码示例来源:origin: Impetus/jumbune

/**
 * Gets the leader name node from zookeeper.
 * @param zkHosts 
 *
 * @return the leader name node from zookeeper
 */
public static byte[] getLeaderNameNodeFromZK(String[] zkHosts) {
  final int retryTimes = 10;
  byte[] nameHostName = null;
  int retryCount = 1;
  do {
    List<String> nameNodeNameList = getNodeChildrenList(zkHosts, ZKConstants.HADOOP_NODE_PATH);
    for (String nameNodeName : nameNodeNameList) {
      nameHostName = getActiveNameNode(zkHosts,
          ZKConstants.HADOOP_NODE_PATH + File.separator + nameNodeName + ZKConstants.ACTIVE_BREAD_CRUMB);
    }
    if (nameHostName == null) {
      try {
        LOGGER.warn("found active NN null from ZK, retrying after " + 100 * retryCount + " millisecs");
        Thread.sleep(100 * retryCount);
      } catch (InterruptedException e) {
      }
    }
    retryCount++;
  } while (nameHostName == null && retryCount <= retryTimes);
  return nameHostName;
}

代码示例来源:origin: Impetus/jumbune

public static String getActiveNNHost(String[] zkHosts){
  byte[] activeHost = null;
  String activeNameNode = null ;
  activeHost = ZKUtils.getLeaderNameNodeFromZK(zkHosts);
  try {
    ActiveNodeInfo activeNodeInfo = PARSER.parsePartialFrom(activeHost);
    activeNameNode = activeNodeInfo.getHostname();
  } catch (InvalidProtocolBufferException e) {
    LOGGER.error(e);            
  }
  return activeNameNode;
}

代码示例来源:origin: Impetus/jumbune

/**
 * Sets the agent node data on znode.
 *
 * @param nodePath the node path
 * @param agent the Agent
 */
public static void setAgentData(String nodePath, AgentNode agent) {
  try {
    String hosts = null;
    CuratorConnector connector = CuratorConnector.getInstance(hosts);
    String json = convertAgentToGson(agent);
    connector.setData(nodePath, json.getBytes(Charset.forName("UTF-8")));
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);        
  }
}

代码示例来源:origin: Impetus/jumbune

/**
 * Force agent election.
 *
 * @return the agent node
 */
private AgentNode forceAgentElection() {
  AgentNode agent = null;
  try {
    agent = ZKUtils.getLeaderAgentfromZK(zkHosts, haConf);
    // send self destruct command to currently active agent so that it
    // relinquishes leadership
    shutdownAgent();
    while (agent != null && HAUtil.compareAgent(this.clusterName, agent.getHost(), agent.getPort())) {
      logger.debug("Blocking till a new leader agent is elected...");
      agent = ZKUtils.getLeaderAgentfromZK(zkHosts, haConf);
      Thread.sleep(1 * 1000);
    }
  } catch (InterruptedException e) {
  } catch (ConnectException e) {
    logger.error("forceAgentElection - ", e);
  }
  return agent;
}

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