|
19 | 19 | public class ForestBuilder extends LoggingObject { |
20 | 20 |
|
21 | 21 | private ForestNamingStrategy forestNamingStrategy; |
| 22 | + private ReplicaBuilderStrategy replicaBuilderStrategy; |
22 | 23 |
|
23 | 24 | public ForestBuilder() { |
24 | 25 | this(new DefaultForestNamingStrategy()); |
25 | 26 | } |
26 | 27 |
|
27 | 28 | public ForestBuilder(ForestNamingStrategy forestNamingStrategy) { |
28 | 29 | this.forestNamingStrategy = forestNamingStrategy; |
| 30 | + this.replicaBuilderStrategy = new DistributedReplicaBuilderStrategy(); |
| 31 | + } |
| 32 | + |
| 33 | + public ForestBuilder(ForestNamingStrategy forestNamingStrategy, ReplicaBuilderStrategy replicaBuilderStrategy) { |
| 34 | + this.forestNamingStrategy = forestNamingStrategy; |
| 35 | + this.replicaBuilderStrategy = replicaBuilderStrategy; |
29 | 36 | } |
30 | 37 |
|
31 | 38 | /** |
@@ -122,98 +129,12 @@ public void addReplicasToForests(List<Forest> forests, ForestPlan forestPlan, Ap |
122 | 129 | replicaCount, databaseName, hostNames)); |
123 | 130 | } |
124 | 131 |
|
125 | | - List<String> dataDirectories = determineDataDirectories(forestPlan.getDatabaseName(), appConfig); |
126 | | - |
127 | | - if (appConfig.getReplicaForestDataDirectory() != null) { |
128 | | - dataDirectories = new ArrayList<>(); |
129 | | - dataDirectories.add(appConfig.getReplicaForestDataDirectory()); |
130 | | - } |
131 | | - Map<String, String> replicaDataDirectoryMap = appConfig.getDatabaseReplicaDataDirectories(); |
132 | | - if (replicaDataDirectoryMap != null && replicaDataDirectoryMap.containsKey(databaseName)) { |
133 | | - dataDirectories = new ArrayList<>(); |
134 | | - dataDirectories.add(replicaDataDirectoryMap.get(databaseName)); |
135 | | - } |
136 | | - |
137 | | - for (Forest f : forests) { |
138 | | - List<ForestReplica> replicas = new ArrayList<>(); |
139 | | - int hostPointer = hostNames.indexOf(f.getHost()); |
140 | | - int dataDirectoryPointer = dataDirectories.indexOf(f.getDataDirectory()); |
141 | | - for (int i = 1; i <= replicaCount; i++) { |
142 | | - ForestReplica replica = new ForestReplica(); |
143 | | - replica.setReplicaName(getForestReplicaName(databaseName, f.getForestName(), i, appConfig)); |
144 | | - replicas.add(replica); |
145 | | - |
146 | | - hostPointer++; |
147 | | - if (hostPointer == hostNames.size()) { |
148 | | - hostPointer = 0; |
149 | | - } |
150 | | - |
151 | | - dataDirectoryPointer++; |
152 | | - if (dataDirectoryPointer == dataDirectories.size()) { |
153 | | - dataDirectoryPointer = 0; |
154 | | - } |
155 | | - |
156 | | - replica.setHost(hostNames.get(hostPointer)); |
157 | | - |
158 | | - final String dataDir = dataDirectories.get(dataDirectoryPointer); |
159 | | - if (dataDir != null && dataDir.trim().length() > 0) { |
160 | | - replica.setDataDirectory(dataDir); |
161 | | - } |
162 | | - |
163 | | - configureReplica(replica, databaseName, appConfig); |
164 | | - } |
165 | | - |
166 | | - f.setForestReplica(replicas); |
167 | | - } |
168 | | - } |
169 | | - |
170 | | - /** |
171 | | - * Configures the fast and large data directories for a replica based on what's in AppConfig for the given |
172 | | - * database. |
173 | | - * |
174 | | - * @param replica |
175 | | - * @param databaseName |
176 | | - * @param appConfig |
177 | | - */ |
178 | | - protected void configureReplica(ForestReplica replica, String databaseName, AppConfig appConfig) { |
179 | | - // First set to the database-agnostic forest directories |
180 | | - replica.setFastDataDirectory(appConfig.getForestFastDataDirectory()); |
181 | | - replica.setLargeDataDirectory(appConfig.getForestLargeDataDirectory()); |
182 | | - |
183 | | - // Now set to the database-specific forest directories if set |
184 | | - if (databaseName != null) { |
185 | | - Map<String, String> map = appConfig.getDatabaseFastDataDirectories(); |
186 | | - if (map != null && map.containsKey(databaseName)) { |
187 | | - replica.setFastDataDirectory(map.get(databaseName)); |
188 | | - } |
189 | | - map = appConfig.getDatabaseLargeDataDirectories(); |
190 | | - if (map != null && map.containsKey(databaseName)) { |
191 | | - replica.setLargeDataDirectory(map.get(databaseName)); |
192 | | - } |
193 | | - } |
| 132 | + List<String> dataDirectories = determineDataDirectories(databaseName, appConfig); |
194 | 133 |
|
195 | | - // Now set to the replica forest directories if set |
196 | | - if (appConfig.getReplicaForestFastDataDirectory() != null) { |
197 | | - replica.setFastDataDirectory(appConfig.getReplicaForestFastDataDirectory()); |
198 | | - } |
199 | | - if (appConfig.getReplicaForestLargeDataDirectory() != null) { |
200 | | - replica.setLargeDataDirectory(appConfig.getReplicaForestLargeDataDirectory()); |
201 | | - } |
| 134 | + replicaBuilderStrategy.buildReplicas(forests, forestPlan, appConfig, dataDirectories, determineForestNamingStrategy(databaseName, appConfig)); |
202 | 135 |
|
203 | | - // And now set to the database-specific replica forest directories if set |
204 | | - if (databaseName != null) { |
205 | | - Map<String, String> map = appConfig.getDatabaseReplicaFastDataDirectories(); |
206 | | - if (map != null && map.containsKey(databaseName)) { |
207 | | - replica.setFastDataDirectory(map.get(databaseName)); |
208 | | - } |
209 | | - map = appConfig.getDatabaseReplicaLargeDataDirectories(); |
210 | | - if (map != null && map.containsKey(databaseName)) { |
211 | | - replica.setLargeDataDirectory(map.get(databaseName)); |
212 | | - } |
213 | | - } |
214 | 136 | } |
215 | 137 |
|
216 | | - |
217 | 138 | protected Forest newForest(ForestPlan forestPlan) { |
218 | 139 | String template = forestPlan.getTemplate(); |
219 | 140 | if (template == null) { |
@@ -288,18 +209,6 @@ protected String getForestName(String databaseName, int forestNumber, AppConfig |
288 | 209 | return determineForestNamingStrategy(databaseName, appConfig).getForestName(databaseName, forestNumber, appConfig); |
289 | 210 | } |
290 | 211 |
|
291 | | - /** |
292 | | - * |
293 | | - * @param databaseName |
294 | | - * @param forestName |
295 | | - * @param forestReplicaNumber |
296 | | - * @param appConfig |
297 | | - * @return |
298 | | - */ |
299 | | - protected String getForestReplicaName(String databaseName, String forestName, int forestReplicaNumber, AppConfig appConfig) { |
300 | | - return determineForestNamingStrategy(databaseName, appConfig).getReplicaName(databaseName, forestName, forestReplicaNumber, appConfig); |
301 | | - } |
302 | | - |
303 | 212 | /** |
304 | 213 | * |
305 | 214 | * @param databaseName |
|
0 commit comments