private Member getLeader(String shardId) { return gondola.getShard(shardId).getLeader(); }
@POST @Path("/setLeader") public Map setLeader(@QueryParam("shardId") String shardId) { Map<Object, Object> result = new HashMap<>(); try { GondolaApplication.getRoutingFilter().getGondola().getShard(shardId).forceLeader(5000); result.put("success", true); } catch (Exception e) { result.put("success", false); result.put("reason", e.getMessage()); } return result; }
@POST @Path("/enable") public Map enable(@QueryParam("shardId") String shardId, @QueryParam("enable") boolean enable) { Map<Object, Object> result = new HashMap<>(); Member localMember = GondolaApplication.getRoutingFilter().getGondola().getShard(shardId).getLocalMember(); try { localMember.enable(enable); result.put("success", true); } catch (Exception e) { result.put("success", false); result.put("reason", e.getMessage()); } return result; }
/** * Protected Methods. * * @param shardId the shard id * @return the boolean */ protected boolean isLeaderInShard(String shardId) { return gondola.getShard(shardId).getLocalMember().isLeader(); }
public RoutingService(Gondola gondola, String shardId) { this.gondola = gondola; shard = gondola.getShard(shardId); hostId = gondola.getHostId(); this.shardId = shardId; memberId = shard.getLocalMember().getMemberId(); commitTimer = GondolaApplication.MyMetricsServletContextListener.METRIC_REGISTRY.timer("LogWriter"); }
private void waitDrainRaftLogs(String shardId) { boolean synced = false; long startTime = System.currentTimeMillis(); long checkTime = startTime; while (!synced) { try { Thread.sleep(500); long now = System.currentTimeMillis(); int commitIndex = gondola.getShard(shardId).getCommitIndex(); int appliedIndex = changeLogProcessor.getAppliedIndex(shardId); int diff = commitIndex - appliedIndex; if (now - checkTime > 10000) { checkTime = now; logger.warn("[{}] Recovery running for {} seconds, {} logs left, ci={}, ai={}", gondola.getHostId(), (now - startTime) / 1000, diff, commitIndex, appliedIndex); } synced = diff <= 0; } catch (Exception e) { logger.warn("[{}] Unknown error. message={}", gondola.getHostId(), e); } } }
public ChangeLogProcessorThread(String shardId) { setName("ChangeLogProcessor"); this.shardId = shardId; this.hostId = gondola.getHostId(); this.memberId = gondola.getShard(shardId).getLocalMember().getMemberId(); this.changeLogConsumer = services.get(shardId).provideChangeLogConsumer(); }
public void run() { Command command; shard = gondola.getShard(shardId); Timer timer = GondolaApplication.MyMetricsServletContextListener.METRIC_REGISTRY.timer("ChangeLogProcessor");
InterruptedException { trace("[{}] shardId={} un-followed shardId={}", gondola.getHostId(), shardId, masterShardId); Member.SlaveStatus status = gondola.getShard(shardId).getLocalMember().getSlaveStatus(); if (status == null) { return; gondola.getShard(shardId).getLocalMember().setSlave(-1); } catch (GondolaException e) { throw new ShardManagerException(e); if (gondola.getShard(shardId).getLocalMember().getSlaveStatus() == null) { return true;
private boolean setSlave(String shardId, int memberId, long timeoutMs) throws InterruptedException, ShardManagerException { try { Member localMember = gondola.getShard(shardId).getLocalMember(); Member.SlaveStatus slaveStatus = localMember.getSlaveStatus(); if (slaveStatus != null && slaveStatus.masterId == memberId && slaveStatus.running) { return true; } localMember.setSlave(memberId); return Utils.pollingWithTimeout(() -> { Member.SlaveStatus status = gondola.getShard(shardId).getLocalMember().getSlaveStatus(); if (slaveOperational(status)) { trace("[{}] Successfully connect to leader node={}", gondola.getHostId(), memberId); return true; } trace("[{}] Slave status={} role={}", gondola.getHostId(), status, gondola.getShard(shardId).getLocalRole()); return false; }, timeoutMs / POLLING_TIMES, timeoutMs); } catch (Exception e) { throw new ShardManagerException(e); } }
/** * Starts observer mode to remote shard. */ @Override public void startObserving(String shardId, String observedShardId, long timeoutMs) throws ShardManagerException, InterruptedException { boolean success = false; trace("[{}-{}] Try to follow shardId={} as slave...", gondola.getHostId(), gondola.getShard(shardId).getLocalMember().getMemberId(), observedShardId); List<Config.ConfigMember> membersInShard = config.getMembersInShard(observedShardId); for (Config.ConfigMember m : membersInShard) { if (success = setSlave(shardId, m.getMemberId(), timeoutMs / membersInShard.size())) { filter.getChangeLogProcessor().reset(shardId); trace("[{}-{}] Successfully to follow masterId={}", gondola.getHostId(), gondola.getShard(shardId).getLocalMember().getMemberId(), m.getMemberId()); break; } } if (!success) { logger.error("[{}-{}] Failed follow master={}", gondola.getHostId(), gondola.getShard(shardId).getLocalMember().getMemberId(), observedShardId); throw new ShardManagerException(FAILED_START_SLAVE); } observedShards.add(observedShardId); }
private boolean waitLogApproach(String shardId, long timeoutMs, int logPosDiff) throws ShardManagerException, InterruptedException { Shard shard = gondola.getShard(shardId); try { return Utils.pollingWithTimeout(() -> { if (shard.getCommitIndex() != 0 && shard.getCommitIndex() - filter.getChangeLogProcessor().getAppliedIndex(shardId) <= logPosDiff) { return true; } Member.SlaveStatus slaveStatus = shard.getLocalMember().getSlaveStatus(); if (!slaveOperational(slaveStatus)) { throw new ShardManagerException(MASTER_IS_GONE); } trace("[{}] {} Log status={}, ci={}, si={}, ai={} targetDiff={}", gondola.getHostId(), shardId, slaveOperational(slaveStatus) ? "RUNNING" : "DOWN", shard.getCommitIndex(), getSavedIndex(shard), filter.getChangeLogProcessor().getAppliedIndex(shardId), logPosDiff); return false; }, timeoutMs / POLLING_TIMES, timeoutMs); } catch (ExecutionException e) { throw new ShardManagerException(e); } }