Skip to content

Instantly share code, notes, and snippets.

@DanielFroehlich
Created June 4, 2020 17:07
Show Gist options
  • Save DanielFroehlich/d35fcda68ce45ead9989dd176f1ca3db to your computer and use it in GitHub Desktop.
Save DanielFroehlich/d35fcda68ce45ead9989dd176f1ca3db to your computer and use it in GitHub Desktop.
rhdg de-dedupe segments
private int lastTopologyId = -1;
private Collection<Set<Integer>> lastListOfSegmentsPerServer = null;
/** Get a duplicate free list of segments per server.
* getSegmentPerServer contains primary and secondary segments. For a cache with numOwners>1, this means
* a single segment is located at multiple severs. If you want to access distinct segments (e.g. for dumping the
* whole cache), these duplicates need to be removed. That is done by this method.
* @param cacheTopologyInfo topology of cache as returned by RemoteCache.getCacheTopologyInfo()
* @return Collection of sets - for each server, a set of duplicate free segment numbers.
*/
public Collection<Set<Integer>> getSegmentsPerServerWithoutDuplicates(CacheTopologyInfo cacheTopologyInfo) {
if (cacheTopologyInfo.getTopologyId() == lastTopologyId && lastListOfSegmentsPerServer!=null) {
return lastListOfSegmentsPerServer;
}
int numSegments = cacheTopologyInfo.getNumSegments();
int numServers = cacheTopologyInfo.getSegmentsPerServer().size();
int currentServer = 0;
Set<Integer>[] newListOfSegmentsPerServer = (Set<Integer>[])(new Set[numServers]);
for (int i=0; i<numServers; i++) {
newListOfSegmentsPerServer[i] = new TreeSet<>();
}
Set<Integer>[] currentListOfSegmentsPerServer = (Set<Integer>[])(new Set[numServers]);
currentListOfSegmentsPerServer = cacheTopologyInfo.getSegmentsPerServer().values().toArray(currentListOfSegmentsPerServer);
for (int currentSegment=0; currentSegment<numSegments; currentSegment++) {
log.fine("Find a server for segment"+currentSegment);
while (! currentListOfSegmentsPerServer[currentServer].contains(currentSegment)) {
log.fine("Current server "+currentServer+"does not does contain segment, trying next one");
currentServer = (currentServer + 1) % numServers;
}
newListOfSegmentsPerServer[currentServer].add(currentSegment);
// To ensure equal distribution of segments across servers, we progress to next server on every segment:
currentServer = (currentServer + 1) % numServers;
}
lastListOfSegmentsPerServer = Arrays.asList(newListOfSegmentsPerServer);
lastTopologyId = cacheTopologyInfo.getTopologyId();
return lastListOfSegmentsPerServer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment