DefaultXpipeMetaManager extends AbstractMetaManager implements XpipeMetaManager { @Override public boolean updateKeeperActive(String dc, String clusterId, String shardId, KeeperMeta activeKeeper) { if(!valid(activeKeeper)){ logger.info("[updateKeeperActive][keeper information unvalid]{}", activeKeeper); } ShardMeta shardMeta = getDirectShardMeta(dc, clusterId, shardId); if(shardMeta == null){ throw new MetaException(String.format("unfound keepers: %s %s %s", dc, clusterId, shardId)); } List<KeeperMeta> keepers = shardMeta.getKeepers(); boolean found = false; boolean changed = false; for(KeeperMeta keeperMeta : keepers){ if(keeperMeta.getIp().equals(activeKeeper.getIp()) && keeperMeta.getPort().equals(activeKeeper.getPort())){ found = true; if(!keeperMeta.isActive()){ logger.info("[updateKeeperActive][set keeper active]{}", keeperMeta); keeperMeta.setActive(true); changed = true; } }else{ if(keeperMeta.isActive()){ logger.info("[updateKeeperActive][set keeper unactive]{}", keeperMeta); keeperMeta.setActive(false); changed = true; } } } if(!found && valid(activeKeeper)){ changed = true; activeKeeper.setActive(true); activeKeeper.setParent(shardMeta); keepers.add(activeKeeper); } return changed; } DefaultXpipeMetaManager(XpipeMeta xpipeMeta); private  DefaultXpipeMetaManager(String fileName); static XpipeMetaManager buildFromFile(String fileName); static XpipeMetaManager buildFromMeta(XpipeMeta xpipeMeta); XpipeMeta load(String fileName); @Override String getActiveDc(String clusterId, String shardId); @Override Set<String> getBackupDcs(String clusterId, String shardId); @Override Set<String> getRelatedDcs(String clusterId, String shardId); @Override Set<String> getDcClusters(String dc); @Override ClusterMeta getClusterMeta(String dc, String clusterId); @Override ClusterType getClusterType(String clusterId); ClusterMeta getDirectClusterMeta(String dc, String clusterId); @Override ShardMeta getShardMeta(String dc, String clusterId, String shardId); @SuppressWarnings("unchecked") @Override List<KeeperMeta> getKeepers(String dc, String clusterId, String shardId); @SuppressWarnings("unchecked") @Override List<RedisMeta> getRedises(String dc, String clusterId, String shardId); @Override KeeperMeta getKeeperActive(String dc, String clusterId, String shardId); @Override List<KeeperMeta> getKeeperBackup(String dc, String clusterId, String shardId); @Override MetaDesc findMetaDesc(HostPort hostPort); @Override Pair<String, RedisMeta> getRedisMaster(String clusterId, String shardId); @Override boolean noneKeeperActive(String dc, String clusterId, String shardId); @Override void setSurviveKeepers(String dcId, String clusterId, String shardId, List<KeeperMeta> surviveKeepers); @Override boolean updateKeeperActive(String dc, String clusterId, String shardId, KeeperMeta activeKeeper); String getFileName(); @Override List<MetaServerMeta> getMetaServers(String dc); @Override ZkServerMeta getZkServerMeta(String dc); @Override Set<String> getDcs(); @Override boolean updateRedisMaster(String dc, String clusterId, String shardId, RedisMeta redisMaster); @Override boolean dcExists(String dc); @Override KeeperContainerMeta getKeeperContainer(String dc, KeeperMeta keeperMeta); @Override void update(DcMeta dcMeta); @Override void update(String dcId, ClusterMeta clusterMeta); @Override ClusterMeta removeCluster(String dcId, String clusterId); @Override DcMeta getDcMeta(String dcId); @Override String getDcZone(String dcId); @Override List<KeeperMeta> getAllSurviceKeepers(String dcId, String clusterId, String shardId); @Override boolean hasCluster(String dcId, String clusterId); @Override boolean hasShard(String dcId, String clusterId, String shardId); @Override SentinelMeta getSentinel(String dc, String clusterId, String shardId); @Override String getSentinelMonitorName(String dc, String clusterId, String shardId); @Override void primaryDcChanged(String dc, String clusterId, String shardId, String newPrimaryDc); @Override List<RouteMeta> routes(String currentDc, String tag); @Override RouteMeta randomRoute(String currentDc, String tag, Integer orgId, String dstDc); @Override List<ClusterMeta> getSpecificActiveDcClusters(String currentDc, String clusterActiveDc); @Override String toString();  }
@Test public void testUpdateKeeperActive() throws MetaException { List<KeeperMeta> backups = metaManager.getKeeperBackup(dc, clusterId1, shardId); Assert.assertNotNull(metaManager.getKeeperActive(dc, clusterId1, shardId)); ; KeeperMeta backup = backups.get(0); metaManager.updateKeeperActive(dc, clusterId1, shardId, backups.get(0)); KeeperMeta newActive = metaManager.getKeeperActive(dc, clusterId1, shardId); Assert.assertEquals(backup.getIp(), newActive.getIp()); Assert.assertEquals(backup.getPort(), newActive.getPort()); metaManager.updateKeeperActive(dc, clusterId1, shardId, new KeeperMeta()); Assert.assertNull(metaManager.getKeeperActive(dc, clusterId1, shardId)); }