|
| 1 | +/* |
| 2 | + * Copyright © 2025 MarkLogic Corporation. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.appdeployer.command.forests; |
| 5 | + |
| 6 | +import com.marklogic.mgmt.api.forest.Forest; |
| 7 | +import com.marklogic.mgmt.api.forest.ForestReplica; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +class ForestReplicaPlanner { |
| 15 | + |
| 16 | + static class Host { |
| 17 | + String name; |
| 18 | + String zone; |
| 19 | + List<Forest> forests; |
| 20 | + |
| 21 | + Host(String hostName, String zone, List<Forest> forests) { |
| 22 | + this.name = hostName; |
| 23 | + this.zone = zone; |
| 24 | + this.forests = forests; |
| 25 | + } |
| 26 | + |
| 27 | + Host(String hostName, String zone, String... forests) { |
| 28 | + this.name = hostName; |
| 29 | + this.zone = zone; |
| 30 | + this.forests = new ArrayList<>(); |
| 31 | + for (String forest : forests) { |
| 32 | + this.forests.add(new Forest(hostName, forest)); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + static class ReplicaAssignment { |
| 38 | + String forest; |
| 39 | + String originalHost; |
| 40 | + List<String> replicaHosts; |
| 41 | + |
| 42 | + ReplicaAssignment(String forest, String originalHost) { |
| 43 | + this.forest = forest; |
| 44 | + this.originalHost = originalHost; |
| 45 | + this.replicaHosts = new ArrayList<>(); |
| 46 | + } |
| 47 | + |
| 48 | + void addReplicaHost(String host) { |
| 49 | + replicaHosts.add(host); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + static List<ReplicaAssignment> assignReplicas(List<Host> hosts, int replicaCount) { |
| 54 | + return assignReplicas(hosts, replicaCount, null); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param hosts |
| 59 | + * @param replicaCount |
| 60 | + * @param allAvailableHosts is not null for a scenario where e.g. forests for a database are only created on one |
| 61 | + * host, such as for a modules or schemas database. In that scenario, the caller needs to |
| 62 | + * pass in a list of all available hosts in the cluster, so that replicas can be created |
| 63 | + * on those hosts. |
| 64 | + * @return |
| 65 | + */ |
| 66 | + static List<ReplicaAssignment> assignReplicas(List<Host> hosts, int replicaCount, List<String> allAvailableHosts) { |
| 67 | + final List<Host> replicaHosts = buildReplicaHostsList(hosts, allAvailableHosts); |
| 68 | + final boolean ignoreZones = shouldIgnoreZones(replicaHosts); |
| 69 | + |
| 70 | + int differentZoneIndex = 0; |
| 71 | + int sameZoneIndex = 0; |
| 72 | + final List<ReplicaAssignment> assignments = new ArrayList<>(); |
| 73 | + |
| 74 | + for (final Host host : hosts) { |
| 75 | + int forestIndex = 0; |
| 76 | + for (Forest forest : host.forests) { |
| 77 | + ReplicaAssignment assignment = new ReplicaAssignment(forest.getForestName(), host.name); |
| 78 | + List<Host> eligibleHosts = buildEligibleHostsList(host, replicaHosts, ignoreZones); |
| 79 | + |
| 80 | + if (ignoreZones) { |
| 81 | + assignReplicasIgnoringZones(assignment, eligibleHosts, replicaCount, forestIndex); |
| 82 | + } else { |
| 83 | + assignReplicasWithZoneAwareness(assignment, eligibleHosts, host, replicaCount, differentZoneIndex, sameZoneIndex); |
| 84 | + differentZoneIndex += replicaCount; |
| 85 | + sameZoneIndex += replicaCount; |
| 86 | + } |
| 87 | + |
| 88 | + addReplicasToForest(forest, assignment); |
| 89 | + assignments.add(assignment); |
| 90 | + forestIndex++; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return assignments; |
| 95 | + } |
| 96 | + |
| 97 | + private static List<Host> buildReplicaHostsList(List<Host> hosts, List<String> allAvailableHosts) { |
| 98 | + List<Host> replicaHosts = new ArrayList<>(hosts); |
| 99 | + if (allAvailableHosts != null) { |
| 100 | + for (String availableHost : allAvailableHosts) { |
| 101 | + boolean hostAlreadyExists = hosts.stream().anyMatch(h -> h.name.equals(availableHost)); |
| 102 | + if (!hostAlreadyExists) { |
| 103 | + replicaHosts.add(new Host(availableHost, null, new ArrayList<>())); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return replicaHosts; |
| 108 | + } |
| 109 | + |
| 110 | + private static boolean shouldIgnoreZones(List<Host> replicaHosts) { |
| 111 | + return replicaHosts.stream().anyMatch(h -> h.zone == null); |
| 112 | + } |
| 113 | + |
| 114 | + private static List<Host> buildEligibleHostsList(Host sourceHost, List<Host> replicaHosts, boolean ignoreZones) { |
| 115 | + List<Host> eligibleHosts = new ArrayList<>(); |
| 116 | + if (ignoreZones) { |
| 117 | + int sourceIndex = replicaHosts.indexOf(sourceHost); |
| 118 | + for (int i = 1; i < replicaHosts.size(); i++) { |
| 119 | + Host candidate = replicaHosts.get((sourceIndex + i) % replicaHosts.size()); |
| 120 | + eligibleHosts.add(candidate); |
| 121 | + } |
| 122 | + } else { |
| 123 | + eligibleHosts = replicaHosts.stream() |
| 124 | + .filter(h -> !h.name.equals(sourceHost.name)) |
| 125 | + .toList(); |
| 126 | + } |
| 127 | + return eligibleHosts; |
| 128 | + } |
| 129 | + |
| 130 | + private static void assignReplicasIgnoringZones(ReplicaAssignment assignment, List<Host> eligibleHosts, int replicaCount, int forestIndex) { |
| 131 | + Set<String> usedHosts = new HashSet<>(); |
| 132 | + for (int i = 0; i < replicaCount && i < eligibleHosts.size(); i++) { |
| 133 | + Host targetHost = eligibleHosts.get((forestIndex + i) % eligibleHosts.size()); |
| 134 | + if (!usedHosts.contains(targetHost.name)) { |
| 135 | + assignment.addReplicaHost(targetHost.name); |
| 136 | + usedHosts.add(targetHost.name); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static void assignReplicasWithZoneAwareness(ReplicaAssignment assignment, List<Host> eligibleHosts, Host sourceHost, int replicaCount, int differentZoneIndex, int sameZoneIndex) { |
| 142 | + List<Host> differentZoneHosts = new ArrayList<>(); |
| 143 | + List<Host> sameZoneHosts = new ArrayList<>(); |
| 144 | + |
| 145 | + for (Host h : eligibleHosts) { |
| 146 | + if (h.zone.equals(sourceHost.zone)) { |
| 147 | + sameZoneHosts.add(h); |
| 148 | + } else { |
| 149 | + differentZoneHosts.add(h); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + Set<String> usedHosts = new HashSet<>(); |
| 154 | + int replicasAssigned = 0; |
| 155 | + |
| 156 | + // First, try to assign from different zones |
| 157 | + replicasAssigned = assignFromHostList(assignment, differentZoneHosts, replicaCount, differentZoneIndex, usedHosts, replicasAssigned); |
| 158 | + |
| 159 | + // If we still need more replicas, use same-zone hosts |
| 160 | + assignFromHostList(assignment, sameZoneHosts, replicaCount, sameZoneIndex, usedHosts, replicasAssigned); |
| 161 | + } |
| 162 | + |
| 163 | + private static int assignFromHostList(ReplicaAssignment assignment, List<Host> hostList, int replicaCount, int startIndex, Set<String> usedHosts, int replicasAssigned) { |
| 164 | + while (replicasAssigned < replicaCount && !hostList.isEmpty() && usedHosts.size() < hostList.size()) { |
| 165 | + Host targetHost = hostList.get(startIndex % hostList.size()); |
| 166 | + startIndex++; |
| 167 | + |
| 168 | + if (!usedHosts.contains(targetHost.name)) { |
| 169 | + assignment.addReplicaHost(targetHost.name); |
| 170 | + usedHosts.add(targetHost.name); |
| 171 | + replicasAssigned++; |
| 172 | + } |
| 173 | + } |
| 174 | + return replicasAssigned; |
| 175 | + } |
| 176 | + |
| 177 | + private static void addReplicasToForest(Forest forest, ReplicaAssignment assignment) { |
| 178 | + forest.setForestReplica(new ArrayList<>()); |
| 179 | + assignment.replicaHosts.forEach(replicaHost -> { |
| 180 | + ForestReplica replica = new ForestReplica(); |
| 181 | + replica.setHost(replicaHost); |
| 182 | + forest.getForestReplica().add(replica); |
| 183 | + }); |
| 184 | + } |
| 185 | +} |
0 commit comments