Skip to content

Commit f997444

Browse files
author
Daman Arora
committed
Allow configurable secondary storage replica count for public and private templates
1 parent 40b9e4d commit f997444

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import java.util.ArrayList;
2020
import java.util.Collections;
2121
import java.util.Date;
22-
import java.util.HashSet;
22+
import java.util.HashMap;
2323
import java.util.LinkedList;
2424
import java.util.List;
25-
import java.util.Set;
25+
import java.util.Map;
2626
import java.util.concurrent.ExecutionException;
2727
import java.util.stream.Collectors;
2828

@@ -294,7 +294,10 @@ protected void createTemplateWithinZones(TemplateProfile profile, VMTemplateVO t
294294
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile);
295295
standardImageStoreAllocation(imageStores, template);
296296
} else {
297-
validateSecondaryStorageAndCreateTemplate(List.of(imageStore), template, null);
297+
int replicaLimit = isPrivateTemplate(template)
298+
? TemplateManager.PrivateTemplateSecStorageCopy.value()
299+
: TemplateManager.PublicTemplateSecStorageCopy.value();
300+
validateSecondaryStorageAndCreateTemplate(List.of(imageStore), template, new HashMap<>(), replicaLimit);
298301
}
299302
}
300303
}
@@ -308,16 +311,18 @@ protected List<DataStore> getImageStoresThrowsExceptionIfNotFound(long zoneId, T
308311
}
309312

310313
protected void standardImageStoreAllocation(List<DataStore> imageStores, VMTemplateVO template) {
311-
Set<Long> zoneSet = new HashSet<Long>();
314+
int replicaLimit = isPrivateTemplate(template)
315+
? TemplateManager.PrivateTemplateSecStorageCopy.value()
316+
: TemplateManager.PublicTemplateSecStorageCopy.value();
312317
Collections.shuffle(imageStores);
313-
validateSecondaryStorageAndCreateTemplate(imageStores, template, zoneSet);
318+
validateSecondaryStorageAndCreateTemplate(imageStores, template, new HashMap<>(), replicaLimit);
314319
}
315320

316-
protected void validateSecondaryStorageAndCreateTemplate(List<DataStore> imageStores, VMTemplateVO template, Set<Long> zoneSet) {
321+
protected void validateSecondaryStorageAndCreateTemplate(List<DataStore> imageStores, VMTemplateVO template, Map<Long, Integer> zoneCopyCount, int replicaLimit) {
317322
for (DataStore imageStore : imageStores) {
318323
Long zoneId = imageStore.getScope().getScopeId();
319324

320-
if (!isZoneAndImageStoreAvailable(imageStore, zoneId, zoneSet, isPrivateTemplate(template))) {
325+
if (!isZoneAndImageStoreAvailable(imageStore, zoneId, zoneCopyCount, replicaLimit)) {
321326
continue;
322327
}
323328

server/src/main/java/com/cloud/template/TemplateAdapterBase.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
import java.util.Arrays;
2121
import java.util.Collections;
2222
import java.util.HashMap;
23-
import java.util.HashSet;
2423
import java.util.List;
2524
import java.util.Map;
2625
import java.util.Objects;
27-
import java.util.Set;
2826

2927
import javax.inject.Inject;
3028

@@ -169,7 +167,7 @@ protected DataStore verifyHeuristicRulesForZone(VMTemplateVO template, Long zone
169167
return heuristicRuleHelper.getImageStoreIfThereIsHeuristicRule(zoneId, heuristicType, template);
170168
}
171169

172-
protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId, Set<Long> zoneSet, boolean isTemplatePrivate) {
170+
protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId, Map<Long, Integer> zoneCopyCount, int replicaLimit) {
173171
if (zoneId == null) {
174172
logger.warn(String.format("Zone ID is null, cannot allocate ISO/template in image store [%s].", imageStore));
175173
return false;
@@ -191,19 +189,13 @@ protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId
191189
return false;
192190
}
193191

194-
if (zoneSet == null) {
195-
logger.info(String.format("Zone set is null; therefore, the ISO/template should be allocated in every secondary storage of zone [%s].", zone));
196-
return true;
197-
}
198-
199-
if (isTemplatePrivate && zoneSet.contains(zoneId)) {
200-
logger.info(String.format("The template is private and it is already allocated in a secondary storage in zone [%s]; therefore, image store [%s] will be skipped.",
201-
zone, imageStore));
192+
int currentCount = zoneCopyCount.getOrDefault(zoneId, 0);
193+
if (replicaLimit > 0 && currentCount >= replicaLimit) {
194+
logger.info("Replica limit of {} reached for zone [{}]; skipping image store [{}].", replicaLimit, zone, imageStore);
202195
return false;
203196
}
204197

205-
logger.info(String.format("Private template will be allocated in image store [%s] in zone [%s].", imageStore, zone));
206-
zoneSet.add(zoneId);
198+
zoneCopyCount.put(zoneId, currentCount + 1);
207199
return true;
208200
}
209201

@@ -212,12 +204,15 @@ protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId
212204
* {@link TemplateProfile#getZoneIdList()}.
213205
*/
214206
protected void postUploadAllocation(List<DataStore> imageStores, VMTemplateVO template, List<TemplateOrVolumePostUploadCommand> payloads) {
215-
Set<Long> zoneSet = new HashSet<>();
207+
int replicaLimit = isPrivateTemplate(template)
208+
? TemplateManager.PrivateTemplateSecStorageCopy.value()
209+
: TemplateManager.PublicTemplateSecStorageCopy.value();
210+
Map<Long, Integer> zoneCopyCount = new HashMap<>();
216211
Collections.shuffle(imageStores);
217212
for (DataStore imageStore : imageStores) {
218213
Long zoneId_is = imageStore.getScope().getScopeId();
219214

220-
if (!isZoneAndImageStoreAvailable(imageStore, zoneId_is, zoneSet, isPrivateTemplate(template))) {
215+
if (!isZoneAndImageStoreAvailable(imageStore, zoneId_is, zoneCopyCount, replicaLimit)) {
221216
continue;
222217
}
223218

0 commit comments

Comments
 (0)