PersistenceAdvisorImpl implements InternalPersistenceAdvisor { protected void removeOlderMembers(Set<PersistentMemberID> persistentMemberSet) { Map<DiskStoreID, PersistentMemberID> mostRecentMap = new HashMap<>(); List<PersistentMemberID> idsToRemove = new ArrayList<>(); for (PersistentMemberID persistentMember : persistentMemberSet) { DiskStoreID diskStoreId = persistentMember.getDiskStoreId(); PersistentMemberID mostRecent = mostRecentMap.get(diskStoreId); if (mostRecent == null) { mostRecentMap.put(diskStoreId, persistentMember); } else { PersistentMemberID older = persistentMember; boolean persistentMemberIsNewer = !persistentMember.isOlderOrEqualVersionOf(mostRecent); if (persistentMemberIsNewer) { older = mostRecent; mostRecentMap.put(diskStoreId, persistentMember); } idsToRemove.add(older); } } persistentMemberSet.removeAll(idsToRemove); } PersistenceAdvisorImpl(CacheDistributionAdvisor cacheDistributionAdvisor,
      DistributedLockService distributedLockService, PersistentMemberView persistentMemberView,
      String regionPath, DiskRegionStats diskRegionStats,
      PersistentMemberManager persistentMemberManager); @VisibleForTesting  PersistenceAdvisorImpl(CacheDistributionAdvisor cacheDistributionAdvisor,
      DistributedLockService distributedLockService, PersistentMemberView persistentMemberView,
      String regionPath, DiskRegionStats diskRegionStats,
      PersistentMemberManager persistentMemberManager,
      StartupStatus startupStatus,
      Transformer<PersistentMemberID, String> persistentMemberIdTransformer,
      CollectionTransformer<PersistentMemberID, String> collectionTransformer,
      PersistentStateQueryMessageSenderFactory persistentStateQueryMessageSenderFactory); @Override void initialize(); @Override PersistentStateQueryResults getMyStateOnMembers(Set<InternalDistributedMember> members); @Override PersistentMemberState getPersistedStateOfMember(PersistentMemberID id); @Override void updateMembershipView(InternalDistributedMember peer, boolean targetReinitializing); @Override void setOnline(boolean didGII, boolean atomicCreation, PersistentMemberID newId); @Override void setInitializing(PersistentMemberID newId); @Override PersistentMemberID generatePersistentID(); @Override PersistentMembershipView getMembershipView(); @Override Set<PersistentMemberID> getPersistedMembers(); @Override boolean checkMyStateOnMembers(Set<InternalDistributedMember> replicates); @VisibleForTesting static void setPersistenceAdvisorObserver(PersistenceAdvisorObserver o); @Override PersistentMemberID getPersistentIDIfOnline(); @Override PersistentMemberID getPersistentID(); @Override PersistentMemberID getInitializingID(); @Override void addListener(PersistentStateListener listener); @Override void removeListener(PersistentStateListener listener); @Override HashSet<PersistentMemberID> getPersistedOnlineOrEqualMembers(); @Override void prepareNewMember(InternalDistributedMember sender, PersistentMemberID oldId,
      PersistentMemberID newId); @Override void removeMember(PersistentMemberID id); @Override void markMemberOffline(InternalDistributedMember member, PersistentMemberID id); @Override CacheDistributionAdvisor getCacheDistributionAdvisor(); @Override void setWaitingOnMembers(Set<PersistentMemberID> allMembersToWaitFor,
      Set<PersistentMemberID> offlineMembersToWaitFor); @Override boolean isClosed(); void finishPendingDestroy(); @Override InitialImageAdvice getInitialImageAdvice(InitialImageAdvice previousAdvice,
      boolean hasDiskImageToRecoverFrom); @Override Set<PersistentMemberID> getMembersToWaitFor(
      Set<PersistentMemberID> previouslyOnlineMembers, Set<PersistentMemberID> offlineMembers); @Override boolean wasHosting(); Set<PersistentMemberID> getAllMembersToWaitFor(); @Override void logWaitingForMembers(); @Override void clearEqualMembers(); @Override void checkInterruptedByShutdownAll(); @Override void close(); @Override boolean acquireTieLock(); @Override void releaseTieLock(); void flushMembershipChanges(); @Override void persistMembersOfflineAndEqual(
      Map<InternalDistributedMember, PersistentMemberID> map); @Override DiskStoreID getDiskStoreID(); @Override boolean isOnline();  }
@Test public void removeOlderMembersHandlesEmptySet() { Set<PersistentMemberID> aSet = new HashSet<>(); persistenceAdvisorImpl.removeOlderMembers(aSet); assertThat(aSet).isEmpty(); }
@Test public void removeOlderMembersWithEqualTimeStampsInDifferentDiskStores() { long timeStamp = 239874; Set<PersistentMemberID> aSet = new HashSet<>(); aSet.add(createPersistentMemberID(getNewDiskStoreID(), timeStamp)); aSet.add(createPersistentMemberID(getNewDiskStoreID(), timeStamp)); aSet.add(createPersistentMemberID(getNewDiskStoreID(), timeStamp)); aSet.add(createPersistentMemberID(getNewDiskStoreID(), timeStamp)); persistenceAdvisorImpl.removeOlderMembers(aSet); assertThat(aSet).hasSize(4); }
@Test public void removeOlderMembersWhenFirstInSetIsOlder() { DiskStoreID diskStoreID = getNewDiskStoreID(); Set<PersistentMemberID> aSet = new TreeSet<>(new PersistentMemberIDComparator()); aSet.add(createPersistentMemberID(diskStoreID, 1, SET_POSITION_1)); PersistentMemberID newest = createPersistentMemberID(diskStoreID, 2, SET_POSITION_2); aSet.add(newest); persistenceAdvisorImpl.removeOlderMembers(aSet); assertThat(aSet).containsExactly(newest); }
@Test public void removeOlderMembersWhenFirstInSetIsNewer() { DiskStoreID diskStoreID = getNewDiskStoreID(); Set<PersistentMemberID> aSet = new TreeSet<>(new PersistentMemberIDComparator()); PersistentMemberID newest = createPersistentMemberID(diskStoreID, 2, SET_POSITION_1); aSet.add(newest); aSet.add(createPersistentMemberID(diskStoreID, 1, SET_POSITION_2)); persistenceAdvisorImpl.removeOlderMembers(aSet); assertThat(aSet).containsExactly(newest); }
@Test public void removeOlderMembersWithMultipleRemovals() { DiskStoreID diskStoreID1 = getNewDiskStoreID(); DiskStoreID diskStoreID2 = getNewDiskStoreID(); DiskStoreID diskStoreID3 = getNewDiskStoreID(); Set<PersistentMemberID> aSet = new TreeSet<>(new PersistentMemberIDComparator()); PersistentMemberID id_1 = createPersistentMemberID(diskStoreID1, 10, SET_POSITION_1); PersistentMemberID id_2 = createPersistentMemberID(diskStoreID2, 10, SET_POSITION_2); PersistentMemberID id_3 = createPersistentMemberID(diskStoreID3, 40, SET_POSITION_3); PersistentMemberID id_4 = createPersistentMemberID(diskStoreID3, 10, SET_POSITION_4); PersistentMemberID id_5 = createPersistentMemberID(diskStoreID2, 50, SET_POSITION_5); PersistentMemberID id_6 = createPersistentMemberID(diskStoreID2, 70, SET_POSITION_6); PersistentMemberID id_7 = createPersistentMemberID(diskStoreID2, 60, SET_POSITION_7); aSet.add(id_1); aSet.add(id_2); aSet.add(id_3); aSet.add(id_4); aSet.add(id_5); aSet.add(id_6); aSet.add(id_7); persistenceAdvisorImpl.removeOlderMembers(aSet); assertThat(aSet).containsExactly(id_1, id_3, id_6); }