ShowMissingDiskStoresFunction implements InternalFunction<Void> { @Override public void execute(FunctionContext<Void> context) { final Set<PersistentMemberPattern> memberMissingIDs = new HashSet<>(); Set<ColocatedRegionDetails> missingColocatedRegions = new HashSet<>(); if (context == null) { throw new RuntimeException(); } try { final InternalCache cache = (InternalCache) context.getCache(); if (cache != null && !cache.isClosed()) { final DistributedMember member = cache.getMyId(); PersistentMemberManager mm = cache.getPersistentMemberManager(); Map<String, Set<PersistentMemberID>> waitingRegions = mm.getWaitingRegions(); for (Set<PersistentMemberID> entry : waitingRegions.values()) { for (PersistentMemberID id : entry) { memberMissingIDs.add(new PersistentMemberPattern(id)); } } Set<PartitionedRegion> prs = cache.getPartitionedRegions(); for (PartitionedRegion pr : prs) { List<String> missingChildRegions = pr.getMissingColocatedChildren(); for (String child : missingChildRegions) { missingColocatedRegions.add(new ColocatedRegionDetails(member.getHost(), member.getName(), pr.getFullPath(), child)); } } } } catch (Exception e) { context.getResultSender().sendException(e); } if (memberMissingIDs.isEmpty() && missingColocatedRegions.isEmpty()) { context.getResultSender().lastResult(null); } else { if (!memberMissingIDs.isEmpty()) { if (missingColocatedRegions.isEmpty()) { context.getResultSender().lastResult(memberMissingIDs); } else { context.getResultSender().sendResult(memberMissingIDs); } } if (!missingColocatedRegions.isEmpty()) { context.getResultSender().lastResult(missingColocatedRegions); } } }  @Override void execute(FunctionContext<Void> context); @Override String getId();  }
@Test public void execute_resultsContains_null_whenNoMissingDiskStores() { when(cache.getPersistentMemberManager()).thenReturn(persistentMemberManager); showMissingDiskStoresFunction.execute(functionContext); assertThat(resultSender.getResults()) .as("results collection") .hasSize(1); assertThat(resultSender.getResults().get(0)) .as("results element [0]: null") .isNull(); }
@Test public void execute_throwsRuntimeException_whenFunctionContextIsNull() { Throwable thrown = catchThrowable(() -> showMissingDiskStoresFunction.execute(null)); assertThat(thrown) .as("throwable thrown by execute") .isInstanceOf(RuntimeException.class) .hasMessage(null); }
@Test public void execute_resultsContains_null_whenCacheIsClosed() { when(cache.isClosed()).thenReturn(true); showMissingDiskStoresFunction.execute(functionContext); assertThat(resultSender.getResults()) .as("results collection") .hasSize(1); assertThat(resultSender.getResults().get(0)) .as("results element [0]: null") .isNull(); }
@Test public void execute_resultsContains_MissingDiskStores() throws UnknownHostException { PersistentMemberID persistentMemberId1 = persistentMemberID("/directory1", 1, (short) 1); PersistentMemberID persistentMemberId2 = persistentMemberID("/directory2", 2, (short) 2); Map<String, Set<PersistentMemberID>> waitingRegions = new HashMap<>(); waitingRegions.put("region1", new HashSet<>(asList(persistentMemberId1, persistentMemberId2))); when(cache.getPersistentMemberManager()).thenReturn(persistentMemberManager); when(persistentMemberManager.getWaitingRegions()).thenReturn(waitingRegions); showMissingDiskStoresFunction.execute(functionContext); List<Set<?>> results = getResults(functionContext.getResultSender()); assertThat(results) .as("results collection") .hasSize(1); assertThat(missingDiskStores(results.get(0))) .as("results element [0]: missingDiskStores") .hasSize(2) .containsExactlyInAnyOrder( new PersistentMemberPattern(persistentMemberId1), new PersistentMemberPattern(persistentMemberId2)); }
@Test public void execute_resultsContains_MissingColocatedRegions() { InternalDistributedMember member = distributedMember("host1", "name1"); when(cache.getMyId()).thenReturn(member); when(cache.getPartitionedRegions()).thenReturn(asSet(region1, region2)); when(cache.getPersistentMemberManager()).thenReturn(persistentMemberManager); when(region1.getFullPath()).thenReturn(SEPARATOR + "pr1"); when(region1.getMissingColocatedChildren()).thenReturn(asList("child1", "child2")); showMissingDiskStoresFunction.execute(functionContext); List<Set<?>> results = getResults(functionContext.getResultSender()); assertThat(results) .as("results collection") .hasSize(1); assertThat(missingColocatedRegions(results.get(0))) .as("results element [0]: missingColocatedRegions") .hasSize(2) .containsExactlyInAnyOrder( new ColocatedRegionDetails("host1", "name1", SEPARATOR + "pr1", "child1"), new ColocatedRegionDetails("host1", "name1", SEPARATOR + "pr1", "child2")); }
@Test public void execute_resultsContains_missingDiskStores_andMissingColocatedRegions() throws UnknownHostException { InternalDistributedMember member = distributedMember("host2", "name2"); PersistentMemberID persistentMemberId1 = persistentMemberID("/directory1", 1, (short) 1); PersistentMemberID persistentMemberId2 = persistentMemberID("/directory2", 2, (short) 2); Map<String, Set<PersistentMemberID>> waitingRegions = new HashMap<>(); waitingRegions.put("region2", asSet(persistentMemberId1, persistentMemberId2)); when(cache.getMyId()).thenReturn(member); when(cache.getPartitionedRegions()).thenReturn(asSet(region1, region2)); when(cache.getPersistentMemberManager()).thenReturn(persistentMemberManager); when(persistentMemberManager.getWaitingRegions()).thenReturn(waitingRegions); when(region2.getFullPath()).thenReturn(SEPARATOR + "pr2"); when(region2.getMissingColocatedChildren()).thenReturn(asList("child1", "child2")); showMissingDiskStoresFunction.execute(functionContext); List<Set<?>> results = getResults(functionContext.getResultSender()); assertThat(results) .as("results collection") .hasSize(2); assertThat(missingDiskStores(results.get(0))) .as("results element [0]: missingDiskStores") .hasSize(2) .containsExactlyInAnyOrder(new PersistentMemberPattern(persistentMemberId1), new PersistentMemberPattern(persistentMemberId2)); assertThat(missingColocatedRegions(results.get(1))) .as("results element [1]: missingColocatedRegions") .hasSize(2) .containsExactlyInAnyOrder( new ColocatedRegionDetails("host2", "name2", SEPARATOR + "pr2", "child1"), new ColocatedRegionDetails("host2", "name2", SEPARATOR + "pr2", "child2")); }
@Test public void execute_resultsContains_exceptionCaughtByFunction() { RuntimeException exceptionCaughtByFunction = new NullPointerException("message"); when(cache.getPersistentMemberManager()).thenThrow(exceptionCaughtByFunction); showMissingDiskStoresFunction.execute(functionContext); assertThat(resultSender.getThrowable()) .as("throwable thrown by execute") .isSameAs(exceptionCaughtByFunction); }