| 
					
				 | 
			
			
				@@ -0,0 +1,138 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.its.common.cluster.utils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public final class HaClusterEvenAllocator { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final List<String> servers; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final Map<String, List<String>> allocation; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public HaClusterEvenAllocator() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.servers = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.allocation = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void addServer(String server) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (server != null && !server.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.servers.add(server); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<String> get(String server) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return this.allocation.getOrDefault(server, Collections.emptyList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void allocate(List<String> clients) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.allocation.clear(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (String server : this.servers) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.allocation.put(server, new ArrayList<>()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int numClients = clients.size(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int numServers = this.servers.size(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int baseSize = 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++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this.allocation.get(server).add(clients.get(clientIndex++)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public static Map<String, List<String>> allocate(List<String> servers, List<String> clients) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, List<String>> allocation = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 서버별 리스트 초기화 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (String server : servers) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            allocation.put(server, new ArrayList<>()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 라운드 로빈 방식으로 순차적 분배 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (int i = 0; i < clients.size(); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String server = servers.get(i % servers.size()); // 서버 개수로 순환 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            allocation.get(server).add(clients.get(i)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return allocation; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public static Map<String, List<String>> allocator(List<String> servers, List<String> clients) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, List<String>> allocation = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 서버별 리스트 초기화 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (String server : servers) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            allocation.put(server, new ArrayList<>()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int serverIndex = 0; // 첫 번째 서버부터 시작 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 순차적으로 클라이언트를 서버에 할당 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (String client : clients) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String server = servers.get(serverIndex); // 현재 서버 선택 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            allocation.get(server).add(client); // 클라이언트 추가 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            serverIndex = (serverIndex + 1) % servers.size(); // 다음 서버로 이동 (마지막 서버 도달하면 처음으로) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return allocation; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public static Map<String, List<String>> sequenceAllocate(List<String> servers, List<String> clients) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, List<String>> allocation = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (String server : servers) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            allocation.put(server, new ArrayList<>()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int numClients = clients.size(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int numServers = servers.size(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int baseSize = 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++)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return allocation; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public static void main(String[] args) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<String> serverList = Arrays.asList("Server-1", "Server-2", "Server-3"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<String> clientList = Arrays.asList( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                "L01", "L02", "L03", "L04", "L05", "L06", "L07", "L08", "L09", "L10", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                "L11", "L12", "L13", "L14", "L15", "L16", "L17", "L18", "L19", "L20", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                "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" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        Map<String, List<String>> allocation = HaClusterEvenAllocator.allocate(serverList, clientList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, List<String>> allocation = HaClusterEvenAllocator.sequenceAllocate(serverList, clientList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 결과 출력 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        allocation.forEach((server, assignedClients) -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.info("server {} -> clients: {}", server, assignedClients); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        HaClusterEvenAllocator allocator = new HaClusterEvenAllocator(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        serverList.forEach(allocator::addServer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        allocator.allocate(clientList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String server = "Server-1"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<String> clients = allocator.get(server); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        clients.forEach(client -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.info("server {} -> client: {}", server, client); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |