DefaultXpipeMetaManager extends AbstractMetaManager implements XpipeMetaManager { @Override public RouteMeta randomRoute(String currentDc, String tag, Integer orgId, String dstDc) { logger.debug("[randomRoute]currentDc: {}, tag: {}, orgId: {}, dstDc: {}", currentDc, tag, orgId, dstDc); List<RouteMeta> routes = routes(currentDc, tag); if(routes == null || routes.isEmpty()){ return null; } logger.debug("[randomRoute]routes: {}", routes); List<RouteMeta> dstDcRoutes = new LinkedList<>(); routes.forEach(routeMeta -> { if(routeMeta.getDstDc().equalsIgnoreCase(dstDc)){ dstDcRoutes.add(routeMeta); } }); if(dstDcRoutes.isEmpty()){ logger.debug("[randomRoute]dst dc empty: {}", routes); return null; } List<RouteMeta> resultsCandidates = new LinkedList<>(); dstDcRoutes.forEach(routeMeta -> { if(ObjectUtils.equals(routeMeta.getOrgId(), orgId)){ resultsCandidates.add(routeMeta); } }); if(!resultsCandidates.isEmpty()){ return random(resultsCandidates); } dstDcRoutes.forEach(routeMeta -> { if(OrgUtil.isDefaultOrg(routeMeta.getOrgId())){ resultsCandidates.add(routeMeta); } }); return random(resultsCandidates); } 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 testRandomRoute(){ ClusterMeta clusterMeta1 = metaManager.getClusterMeta(dcBak2, clusterId1); Set<RouteMeta> routes = new HashSet<>(); for(int i=0;i<100;i++){ RouteMeta routeMeta = metaManager.metaRandomRoutes(dcBak2, clusterMeta1.getOrgId(), clusterMeta1.getActiveDc()); routes.add(routeMeta); Assert.assertTrue(routeMeta.getId() >= 1 && routeMeta.getId() <=3 ); } Assert.assertEquals(3, routes.size()); ClusterMeta clusterMeta2 = metaManager.getClusterMeta(dcBak2, clusterId2); for(int i=0;i<100;i++){ RouteMeta routeMeta = metaManager.metaRandomRoutes(dcBak2, clusterMeta2.getOrgId(), clusterMeta2.getActiveDc()); Assert.assertEquals(new Integer(4), routeMeta.getId()); } clusterMeta1 = metaManager.getClusterMeta(dcBak1, clusterId1); RouteMeta routeMeta = metaManager.metaRandomRoutes(dcBak1, clusterMeta1.getOrgId(), clusterMeta1.getActiveDc()); Assert.assertNull(routeMeta); }