Skip to content

Commit 6dbeb52

Browse files
committed
Refactor as per pr comments
1 parent e5bfeab commit 6dbeb52

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

engine/components-api/src/main/java/com/cloud/template/TemplateManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
public interface TemplateManager {
4646
static final String AllowPublicUserTemplatesCK = "allow.public.user.templates";
4747
static final String TemplatePreloaderPoolSizeCK = "template.preloader.pool.size";
48-
static final String PublicTemplateSecStorageCopyCK = "public.template.secstorage.copy";
49-
static final String PrivateTemplateSecStorageCopyCK = "private.template.secstorage.copy";
48+
static final String PublicTemplateSecStorageCopyCK = "secstorage.public.template.copy.max";
49+
static final String PrivateTemplateSecStorageCopyCK = "secstorage.private.template.copy.max";
5050

5151
static final ConfigKey<Boolean> AllowPublicUserTemplates = new ConfigKey<Boolean>("Advanced", Boolean.class, AllowPublicUserTemplatesCK, "true",
5252
"If false, users will not be able to create public Templates.", true, ConfigKey.Scope.Account);

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,8 @@ protected void createTemplateWithinZones(TemplateProfile profile, VMTemplateVO t
294294
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile);
295295
standardImageStoreAllocation(imageStores, template, zoneId);
296296
} else {
297-
int replicaLimit = isPrivateTemplate(template)
298-
? TemplateManager.PrivateTemplateSecStorageCopy.valueIn(zoneId)
299-
: TemplateManager.PublicTemplateSecStorageCopy.valueIn(zoneId);
300-
validateSecondaryStorageAndCreateTemplate(List.of(imageStore), template, new HashMap<>(), replicaLimit);
297+
int copyLimit = getSecStorageCopyLimit(template, zoneId);
298+
validateSecondaryStorageAndCreateTemplate(List.of(imageStore), template, new HashMap<>(), copyLimit);
301299
}
302300
}
303301
}
@@ -311,18 +309,16 @@ protected List<DataStore> getImageStoresThrowsExceptionIfNotFound(long zoneId, T
311309
}
312310

313311
protected void standardImageStoreAllocation(List<DataStore> imageStores, VMTemplateVO template, long zoneId) {
314-
int replicaLimit = isPrivateTemplate(template)
315-
? TemplateManager.PrivateTemplateSecStorageCopy.valueIn(zoneId)
316-
: TemplateManager.PublicTemplateSecStorageCopy.valueIn(zoneId);
312+
int copyLimit = getSecStorageCopyLimit(template, zoneId);
317313
Collections.shuffle(imageStores);
318-
validateSecondaryStorageAndCreateTemplate(imageStores, template, new HashMap<>(), replicaLimit);
314+
validateSecondaryStorageAndCreateTemplate(imageStores, template, new HashMap<>(), copyLimit);
319315
}
320316

321-
protected void validateSecondaryStorageAndCreateTemplate(List<DataStore> imageStores, VMTemplateVO template, Map<Long, Integer> zoneCopyCount, int replicaLimit) {
317+
protected void validateSecondaryStorageAndCreateTemplate(List<DataStore> imageStores, VMTemplateVO template, Map<Long, Integer> zoneCopyCount, int copyLimit) {
322318
for (DataStore imageStore : imageStores) {
323319
Long zoneId = imageStore.getScope().getScopeId();
324320

325-
if (!isZoneAndImageStoreAvailable(imageStore, zoneId, zoneCopyCount, replicaLimit)) {
321+
if (!isZoneAndImageStoreAvailable(imageStore, zoneId, zoneCopyCount, copyLimit)) {
326322
continue;
327323
}
328324

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ protected DataStore verifyHeuristicRulesForZone(VMTemplateVO template, Long zone
167167
return heuristicRuleHelper.getImageStoreIfThereIsHeuristicRule(zoneId, heuristicType, template);
168168
}
169169

170-
protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId, Map<Long, Integer> zoneCopyCount, int replicaLimit) {
170+
protected int getSecStorageCopyLimit(VMTemplateVO template, long zoneId) {
171+
return isPrivateTemplate(template)
172+
? TemplateManager.PrivateTemplateSecStorageCopy.valueIn(zoneId)
173+
: TemplateManager.PublicTemplateSecStorageCopy.valueIn(zoneId);
174+
}
175+
176+
protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId, Map<Long, Integer> zoneCopyCount, int copyLimit) {
171177
if (zoneId == null) {
172178
logger.warn(String.format("Zone ID is null, cannot allocate ISO/template in image store [%s].", imageStore));
173179
return false;
@@ -190,8 +196,8 @@ protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId
190196
}
191197

192198
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);
199+
if (copyLimit > 0 && currentCount >= copyLimit) {
200+
logger.info("Copy limit of {} reached for zone [{}]; skipping image store [{}].", copyLimit, zone, imageStore);
195201
return false;
196202
}
197203

@@ -204,16 +210,13 @@ protected boolean isZoneAndImageStoreAvailable(DataStore imageStore, Long zoneId
204210
* {@link TemplateProfile#getZoneIdList()}.
205211
*/
206212
protected void postUploadAllocation(List<DataStore> imageStores, VMTemplateVO template, List<TemplateOrVolumePostUploadCommand> payloads) {
207-
boolean isPrivate = isPrivateTemplate(template);
208213
Map<Long, Integer> zoneCopyCount = new HashMap<>();
209214
Collections.shuffle(imageStores);
210215
for (DataStore imageStore : imageStores) {
211216
Long zoneId_is = imageStore.getScope().getScopeId();
212-
int replicaLimit = zoneId_is == null ? 0 : (isPrivate
213-
? TemplateManager.PrivateTemplateSecStorageCopy.valueIn(zoneId_is)
214-
: TemplateManager.PublicTemplateSecStorageCopy.valueIn(zoneId_is));
217+
int copyLimit = zoneId_is == null ? 0 : getSecStorageCopyLimit(template, zoneId_is);
215218

216-
if (!isZoneAndImageStoreAvailable(imageStore, zoneId_is, zoneCopyCount, replicaLimit)) {
219+
if (!isZoneAndImageStoreAvailable(imageStore, zoneId_is, zoneCopyCount, copyLimit)) {
217220
continue;
218221
}
219222

server/src/test/java/com/cloud/template/HypervisorTemplateAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ public void isZoneAndImageStoreAvailableTestReplicaLimitReachedShouldReturnFalse
494494

495495
boolean result = _adapter.isZoneAndImageStoreAvailable(dataStoreMock, zoneId, zoneCopyCount, 1);
496496

497-
Mockito.verify(loggerMock, times(1)).info("Replica limit of {} reached for zone [{}]; skipping image store [{}].", 1, dataCenterVOMock, dataStoreMock);
497+
Mockito.verify(loggerMock, times(1)).info("Copy limit of {} reached for zone [{}]; skipping image store [{}].", 1, dataCenterVOMock, dataStoreMock);
498498
Assert.assertFalse(result);
499499
}
500500

0 commit comments

Comments
 (0)