DefaultSentinelHelloCollector implements SentinelHelloCollector { @VisibleForTesting protected Set<SentinelHello> checkToAdd(String clusterId, String shardId, String sentinelMonitorName, Set<HostPort> sentinels, Set<SentinelHello> hellos, HostPort masterAddr, QuorumConfig quorumConfig) { if(masterAddr == null){ logger.warn("[checkToAdd][no monitor name]{}, {}", clusterId, shardId); return Sets.newHashSet(); } if (StringUtil.isEmpty(sentinelMonitorName)) { logger.warn("[checkToAdd][no monitor name]{}, {}", clusterId, shardId); return Sets.newHashSet(); } if (hellos.size() >= quorumConfig.getTotal()) { return Sets.newHashSet(); } if (!checkMasterConsistent(masterAddr, hellos)) { logger.info("[collect][master not consistent]{}, {}, {}", sentinelMonitorName, masterAddr, hellos); return Sets.newHashSet(); } Set<HostPort> currentSentinels = new HashSet<>(); hellos.forEach((hello -> currentSentinels.add(hello.getSentinelAddr()))); Set<SentinelHello> toAdd = new HashSet<>(); int toAddSize = quorumConfig.getTotal() - hellos.size(); int i = 0; for (HostPort hostPort : sentinels) { if (!currentSentinels.contains(hostPort)) { i++; if(i > toAddSize){ break; } toAdd.add(new SentinelHello(hostPort, masterAddr, sentinelMonitorName)); } } return toAdd; }  @PostConstruct void postConstruct(); @PreDestroy void preDestroy(); @Override void onAction(SentinelActionContext context); @Override void stopWatch(HealthCheckAction action); @Override boolean worksfor(ActionContext t); @VisibleForTesting void setSessionManager(DefaultRedisSessionManager sessionManager); @VisibleForTesting void setAlertManager(AlertManager alertManager);  }
@Test public void testAdd(){ Set<SentinelHello> hellos = Sets.newHashSet( new SentinelHello(new HostPort("127.0.0.1", 5000), master, monitorName) ); Set<SentinelHello> toAdd = sentinelCollector.checkToAdd("cluster1", "shard1", monitorName, masterSentinels, hellos, master, quorumConfig); Assert.assertEquals(4, toAdd.size()); quorumConfig.setTotal(3); toAdd = sentinelCollector.checkToAdd("cluster1", "shard1", monitorName, masterSentinels, hellos, master, quorumConfig); Assert.assertEquals(2, toAdd.size()); }
@Test public void testEmptySentinelLogDueToDoubleMasterInOneShard() throws Exception { String clusterId = "clusterId", shardId = "shardId"; monitorName = shardId; masterSentinels = Sets.newHashSet( new HostPort("127.0.0.1", 5000), new HostPort("127.0.0.1", 5001), new HostPort("127.0.0.1", 5002), new HostPort("127.0.0.1", 5003), new HostPort("127.0.0.1", 5004) ); sentinelCollector = spy(sentinelCollector); server = startServer(master.getPort(), "*5\r\n" + "$6\r\nkeeper\r\n" + "$9\r\nlocalhost\r\n" + ":6379\r\n" + "$9\r\nconnected\r\n" + ":477\r\n"); Set<SentinelHello> hellos = sentinelCollector.checkToAdd(clusterId, shardId, monitorName, masterSentinels, Sets.newHashSet(), master, quorumConfig); Assert.assertTrue(hellos.isEmpty()); }
@Test public void testEmptySentinelLogDueToDoubleMasterInOneShard2() throws Exception { String clusterId = "clusterId", shardId = "shardId"; monitorName = shardId; masterSentinels = Sets.newHashSet( new HostPort("127.0.0.1", 5000), new HostPort("127.0.0.1", 5001), new HostPort("127.0.0.1", 5002), new HostPort("127.0.0.1", 5003), new HostPort("127.0.0.1", 5004) ); sentinelCollector = spy(sentinelCollector); server = startServer(master.getPort(), "*3\r\n" + "$6\r\nmaster\r\n" + ":43\r\n" + "*3\r\n" + "$9\r\n127.0.0.1\r\n" + "$4\r\n6479\r\n" + "$1\r\n0\r\n"); Set<SentinelHello> hellos = sentinelCollector.checkToAdd(clusterId, shardId, monitorName, masterSentinels, Sets.newHashSet(), master, quorumConfig); Assert.assertEquals(masterSentinels.size(), hellos.size()); }