use of com.github.ambry.clustermap.ClusterMap in project ambry by linkedin.
the class ReplicationManager method assignReplicasToThreadPool.
/**
* Partitions the list of data nodes between given set of replica threads for the given DC
*/
private void assignReplicasToThreadPool() {
for (Map.Entry<String, DataNodeRemoteReplicaInfos> mapEntry : dataNodeRemoteReplicaInfosPerDC.entrySet()) {
String datacenter = mapEntry.getKey();
DataNodeRemoteReplicaInfos dataNodeRemoteReplicaInfos = mapEntry.getValue();
Set<DataNodeId> dataNodesToReplicate = dataNodeRemoteReplicaInfos.getDataNodeIds();
int dataNodesCount = dataNodesToReplicate.size();
int replicaThreadCount = numberOfReplicaThreads.get(datacenter);
if (replicaThreadCount <= 0) {
logger.warn("Number of replica threads is smaller or equal to 0, not starting any replica threads for {} ", datacenter);
continue;
} else if (dataNodesCount == 0) {
logger.warn("Number of nodes to replicate from is 0, not starting any replica threads for {} ", datacenter);
continue;
}
// Divide the nodes between the replica threads if the number of replica threads is less than or equal to the
// number of nodes. Otherwise, assign one thread to one node.
logger.info("Number of replica threads to replicate from {}: {}", datacenter, replicaThreadCount);
logger.info("Number of dataNodes to replicate :", dataNodesCount);
if (dataNodesCount < replicaThreadCount) {
logger.warn("Number of replica threads: {} is more than the number of nodes to replicate from: {}", replicaThreadCount, dataNodesCount);
replicaThreadCount = dataNodesCount;
}
ResponseHandler responseHandler = new ResponseHandler(clusterMap);
int numberOfNodesPerThread = dataNodesCount / replicaThreadCount;
int remainingNodes = dataNodesCount % replicaThreadCount;
Iterator<DataNodeId> dataNodeIdIterator = dataNodesToReplicate.iterator();
for (int i = 0; i < replicaThreadCount; i++) {
// create the list of nodes for the replica thread
Map<DataNodeId, List<RemoteReplicaInfo>> replicasForThread = new HashMap<DataNodeId, List<RemoteReplicaInfo>>();
int nodesAssignedToThread = 0;
while (nodesAssignedToThread < numberOfNodesPerThread) {
DataNodeId dataNodeToReplicate = dataNodeIdIterator.next();
replicasForThread.put(dataNodeToReplicate, dataNodeRemoteReplicaInfos.getRemoteReplicaListForDataNode(dataNodeToReplicate));
dataNodeIdIterator.remove();
nodesAssignedToThread++;
}
if (remainingNodes > 0) {
DataNodeId dataNodeToReplicate = dataNodeIdIterator.next();
replicasForThread.put(dataNodeToReplicate, dataNodeRemoteReplicaInfos.getRemoteReplicaListForDataNode(dataNodeToReplicate));
dataNodeIdIterator.remove();
remainingNodes--;
}
boolean replicatingOverSsl = sslEnabledDatacenters.contains(datacenter);
String threadIdentity = "Replica Thread-" + (dataNodeId.getDatacenterName().equals(datacenter) ? "Intra-" : "Inter") + i + datacenter;
ReplicaThread replicaThread = new ReplicaThread(threadIdentity, replicasForThread, factory, clusterMap, correlationIdGenerator, dataNodeId, connectionPool, replicationConfig, replicationMetrics, notification, storeKeyFactory, replicationConfig.replicationValidateMessageStream, metricRegistry, replicatingOverSsl, datacenter, responseHandler);
if (replicaThreadPools.containsKey(datacenter)) {
replicaThreadPools.get(datacenter).add(replicaThread);
} else {
replicaThreadPools.put(datacenter, new ArrayList<>(Arrays.asList(replicaThread)));
}
}
}
}
Aggregations