Quellcode durchsuchen

update HaClusterEvenAllocator

HANTE vor 1 Monat
Ursprung
Commit
be19d215ee

+ 64 - 10
its-cluster/src/main/java/com/its/common/cluster/utils/HaClusterEvenAllocator.java

@@ -90,19 +90,72 @@ public final class HaClusterEvenAllocator {
 
         int numClients = clients.size();
         int numServers = servers.size();
-        int baseSize = numClients / numServers;  // 각 서버에 기본적으로 할당할 개수
-        int remainder = numClients % numServers; // 남는 개수 (마지막 서버에 할당)
-
+        int allocateSize = numClients / numServers;     // 각 서버에 기본적으로 할당할 개수
+        int remainder = numClients % numServers;        // 남는 개수 (마지막 서버에 할당)
         int clientIndex = 0;
-        for (int i = 0; i < numServers; i++) {
-            int allocationSize = baseSize + (i < remainder ? 1 : 0); // 마지막 서버에 여분 추가
-            String server = servers.get(i);
 
-            for (int j = 0; j < allocationSize; j++) {
-                allocation.get(server).add(clients.get(clientIndex++));
+        // 여분을 앞쪽 서버에 우선 배정하는 방식
+//        int startIdx = 0;
+        for (int ii = 0; ii < numServers; ii++) {
+            String server = servers.get(ii);
+            List<String> serverAllocation = allocation.get(server);
+
+//            int additional = (ii < remainder) ? 1 : 0;
+//            int endIdx = Math.min(startIdx + allocateSize + additional, numClients);
+//            for (int jj = startIdx; jj < endIdx; jj++) {
+//                serverAllocation.add(clients.get(jj));
+//            }
+//            startIdx = endIdx; // 다음 서버의 시작 인덱스 업데이트
+
+            // 기본적으로 모든 서버에 allocateSize 씩 배정됩니다.
+            // 남는 1개(remainder)는 i < remainder 조건에 따라 앞에서부터 순서대로 하나씩 더 추가돼요.
+            int allocationSize = allocateSize + (ii < remainder ? 1 : 0); // 마지막 서버에 여분 추가
+            for (int jj = 0; jj < allocationSize; jj++) {
+                serverAllocation.add(clients.get(clientIndex++));
             }
         }
 
+        // 여분을 뒤쪽 서버에 우선 배정하는 방식
+//        clientIndex = 0;
+//        for (int ii = 0; ii < numServers; ii++) {
+//            String server = servers.get(ii);
+//
+//            // 뒤에서부터 여분을 부여하기 위해 'numServers - i - 1' < remainder 조건 사용
+//            int extra = ((numServers - ii - 1) < remainder) ? 1 : 0;
+//            int allocationSize = allocateSize + extra;
+//
+//            List<String> serverAllocation = allocation.get(server);
+//            for (int j = 0; j < allocationSize && clientIndex < numClients; j++) {
+//                serverAllocation.add(clients.get(clientIndex++));
+//            }
+//        }
+
+        // 가장 적은 서버부터 추가로 할당하는 방식 (사전 정렬 기반 로드 밸런싱)
+        // 서버별 현재 할당 수를 추적하기 위한 Map
+//        Map<String, Integer> loadMap = new HashMap<>();
+//        for (String server : servers) {
+//            loadMap.put(server, 0);
+//        }
+//        clientIndex = 0;
+//        while (clientIndex < clients.size()) {
+//            // 현재까지 가장 적게 할당된 서버를 찾음
+//            String minLoadServer = Collections.min(loadMap.entrySet(), Map.Entry.comparingByValue()).getKey();
+//            allocation.get(minLoadServer).add(clients.get(clientIndex++));
+//            loadMap.put(minLoadServer, loadMap.get(minLoadServer) + 1);
+//        }
+
+//        int numClients = clients.size();
+//        int clusterSize = servers.size();
+//        int countPerCluster = (int)Math.ceil((double) numClients / clusterSize);
+//        log.info("numClients: {}, clusterSize: {}, countPerCluster: {}", numClients, clusterSize, countPerCluster);
+//        for (int ii = 0; ii < clusterSize; ii++) {
+//            int startIdx = ii * countPerCluster;
+//            int endIdx = Math.min(startIdx + countPerCluster, numClients);
+//            List<String> serverAllocation = allocation.get(servers.get(ii));
+//            for (int jj = startIdx; jj < endIdx; jj++) {
+//                serverAllocation.add(clients.get(jj));
+//            }
+//        }
         return allocation;
     }
 
@@ -114,8 +167,9 @@ public final class HaClusterEvenAllocator {
                 "L21", "L22", "L23", "L24", "L25", "L26", "L28", "L29", "L30", "L31",
                 "L32", "L33", "L34", "L35", "L36", "L37", "L39", "L40", "L41", "L42",
                 "L43", "L44", "L45", "L46", "L47", "L48", "L49", "L50", "L51", "L52",
-                "L53", "L54", "L55", "L56", "L57", "L58", "L59", "L60", "L65", "L66",
-                "L67", "L77", "L90", "L91", "L95"
+                "L53", "L54", "L55", "L56", "L57", "L58", "L59", "L60", "L65", "L66", "L67"
+//                , "L77", "L90"
+                //, "L91", "L95"
         );
 
 //        Map<String, List<String>> allocation = HaClusterEvenAllocator.allocate(serverList, clientList);