Search in sources :

Example 51 with ClusterMap

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)));
            }
        }
    }
}
Also used : ResponseHandler(com.github.ambry.commons.ResponseHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ClusterMap(com.github.ambry.clustermap.ClusterMap) DataNodeId(com.github.ambry.clustermap.DataNodeId)

Aggregations

ClusterMap (com.github.ambry.clustermap.ClusterMap)51 VerifiableProperties (com.github.ambry.config.VerifiableProperties)37 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)32 ArrayList (java.util.ArrayList)29 Properties (java.util.Properties)27 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)25 HashMap (java.util.HashMap)25 Map (java.util.Map)24 Test (org.junit.Test)24 PartitionId (com.github.ambry.clustermap.PartitionId)23 BlobIdFactory (com.github.ambry.commons.BlobIdFactory)23 List (java.util.List)23 MockPartitionId (com.github.ambry.clustermap.MockPartitionId)20 DataNodeId (com.github.ambry.clustermap.DataNodeId)19 StoreKeyFactory (com.github.ambry.store.StoreKeyFactory)18 ClusterAgentsFactory (com.github.ambry.clustermap.ClusterAgentsFactory)17 BlobId (com.github.ambry.commons.BlobId)17 MetricRegistry (com.codahale.metrics.MetricRegistry)16 MockStoreKeyConverterFactory (com.github.ambry.store.MockStoreKeyConverterFactory)16 File (java.io.File)16