private synchronized void connect() throws IOException, KeeperException, InterruptedException, TTransportException { getLeader(); while (true) { log.debug("Connecting to oracle at " + currentLeader.getId()); String[] hostAndPort = currentLeader.getId().split(":"); String host = hostAndPort[0]; int port = Integer.parseInt(hostAndPort[1]); try { transport = new TFastFramedTransport(new TSocket(host, port)); transport.open(); TProtocol protocol = new TCompactProtocol(transport); client = new OracleService.Client(protocol); log.info("Connected to oracle at " + getOracle()); break; } catch (TTransportException e) { sleepRandom(); getLeader(); } catch (Exception e) { throw new RuntimeException(e); } } }
private boolean getLeaderAttempt() { Participant possibleLeader = null; try { possibleLeader = leaderSelector.getLeader(); } catch (KeeperException e) { log.debug("Exception throw in getLeaderAttempt()", e); } catch (Exception e) { throw new RuntimeException(e); } if (isLeader(possibleLeader)) { currentLeader = possibleLeader; return true; } return false; }
/** * It's possible an Oracle has gone into a bad state. Upon the leader being changed, we want to * update our state */ @Override public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception { if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED) || event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) { Participant participant = leaderSelector.getLeader(); synchronized (this) { if (isLeader(participant)) { currentLeader = leaderSelector.getLeader(); } else { currentLeader = null; } } } }
/** * Atomically closes current connection and connects to the current leader */ private synchronized void reconnect() throws InterruptedException, TTransportException, KeeperException, IOException { if (transport.isOpen()) { transport.close(); } connect(); }
/** * Attempt to retrieve a leader until one is found */ private void getLeader() { boolean found = getLeaderAttempt(); while (!found) { sleepRandom(); found = getLeaderAttempt(); } }
public OracleClient(Environment env) { this.env = env; responseTimer = MetricsUtil.getTimer(env.getConfiguration(), env.getSharedResources().getMetricRegistry(), env.getMetricNames().getOracleResponseTime()); stampsHistogram = MetricsUtil.getHistogram(env.getConfiguration(), env.getSharedResources() .getMetricRegistry(), env.getMetricNames().getOracleClientStamps()); timestampRetriever = new TimestampRetriever(); thread = new Thread(timestampRetriever); thread.setDaemon(true); thread.start(); }