Skip to content

Commit f6802e9

Browse files
server: fix duplicated records for templates if there are multiple zones (#8933)
1 parent 0fa71f5 commit f6802e9

2 files changed

Lines changed: 12 additions & 15 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,21 @@ protected void createTemplateWithinZones(TemplateProfile profile, VMTemplateVO t
306306
zonesIds = _dcDao.listAllZones().stream().map(DataCenterVO::getId).collect(Collectors.toList());
307307
}
308308

309-
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zonesIds, profile);
310309

311310
for (long zoneId : zonesIds) {
312311
DataStore imageStore = verifyHeuristicRulesForZone(template, zoneId);
313312

314313
if (imageStore == null) {
314+
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile);
315315
standardImageStoreAllocation(imageStores, template);
316316
} else {
317317
validateSecondaryStorageAndCreateTemplate(List.of(imageStore), template, null);
318318
}
319319
}
320320
}
321321

322-
protected List<DataStore> getImageStoresThrowsExceptionIfNotFound(List<Long> zonesIds, TemplateProfile profile) {
323-
List<DataStore> imageStores = storeMgr.getImageStoresByZoneIds(zonesIds.toArray(new Long[0]));
322+
protected List<DataStore> getImageStoresThrowsExceptionIfNotFound(long zoneId, TemplateProfile profile) {
323+
List<DataStore> imageStores = storeMgr.getImageStoresByZoneIds(zoneId);
324324
if (imageStores == null || imageStores.size() == 0) {
325325
throw new CloudRuntimeException(String.format("Unable to find image store to download the template [%s].", profile.getTemplate()));
326326
}
@@ -426,7 +426,7 @@ public List<TemplateOrVolumePostUploadCommand> doInTransaction(TransactionStatus
426426
List<TemplateOrVolumePostUploadCommand> payloads = new LinkedList<>();
427427

428428
if (imageStore == null) {
429-
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(List.of(zoneId), profile);
429+
List<DataStore> imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile);
430430
postUploadAllocation(imageStores, template, payloads);
431431
} else {
432432
postUploadAllocation(List.of(imageStore), template, payloads);

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public class HypervisorTemplateAdapterTest {
151151

152152
private AutoCloseable closeable;
153153

154+
private static final long zoneId = 1L;
155+
154156
@Before
155157
public void before() {
156158
closeable = MockitoAnnotations.openMocks(this);
@@ -323,7 +325,6 @@ public void createTemplateWithinZonesTestZoneIdsNullShouldCallListAllZones() {
323325
VMTemplateVO vmTemplateVOMock = Mockito.mock(VMTemplateVO.class);
324326

325327
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(null);
326-
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
327328

328329
_adapter.createTemplateWithinZones(templateProfileMock, vmTemplateVOMock);
329330

@@ -337,7 +338,7 @@ public void createTemplateWithinZonesTestZoneIdsNotNullShouldNotCallListAllZones
337338
List<Long> zoneIds = List.of(1L);
338339

339340
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds);
340-
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
341+
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(Long.class), Mockito.any(TemplateProfile.class));
341342
Mockito.doReturn(null).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong());
342343
Mockito.doNothing().when(_adapter).standardImageStoreAllocation(Mockito.isNull(), Mockito.any(VMTemplateVO.class));
343344

@@ -350,10 +351,10 @@ public void createTemplateWithinZonesTestZoneIdsNotNullShouldNotCallListAllZones
350351
public void createTemplateWithinZonesTestZoneDoesNotHaveActiveHeuristicRulesShouldCallStandardImageStoreAllocation() {
351352
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
352353
VMTemplateVO vmTemplateVOMock = Mockito.mock(VMTemplateVO.class);
353-
List<Long> zoneIds = List.of(1L);
354+
List<Long> zoneIds = List.of(zoneId);
354355

355356
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds);
356-
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
357+
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(Long.class), Mockito.any(TemplateProfile.class));
357358
Mockito.doReturn(null).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong());
358359
Mockito.doNothing().when(_adapter).standardImageStoreAllocation(Mockito.isNull(), Mockito.any(VMTemplateVO.class));
359360

@@ -370,7 +371,6 @@ public void createTemplateWithinZonesTestZoneWithHeuristicRuleShouldCallValidate
370371
List<Long> zoneIds = List.of(1L);
371372

372373
Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds);
373-
Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class));
374374
Mockito.doReturn(dataStoreMock).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong());
375375
Mockito.doNothing().when(_adapter).validateSecondaryStorageAndCreateTemplate(Mockito.any(List.class), Mockito.any(VMTemplateVO.class), Mockito.isNull());
376376

@@ -382,34 +382,31 @@ public void createTemplateWithinZonesTestZoneWithHeuristicRuleShouldCallValidate
382382
@Test(expected = CloudRuntimeException.class)
383383
public void getImageStoresThrowsExceptionIfNotFoundTestNullImageStoreShouldThrowCloudRuntimeException() {
384384
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
385-
List<Long> zoneIds = List.of(1L);
386385

387386
Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(null);
388387

389-
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock);
388+
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock);
390389
}
391390

392391
@Test(expected = CloudRuntimeException.class)
393392
public void getImageStoresThrowsExceptionIfNotFoundTestEmptyImageStoreShouldThrowCloudRuntimeException() {
394393
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
395-
List<Long> zoneIds = List.of(1L);
396394
List<DataStore> imageStoresList = new ArrayList<>();
397395

398396
Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(imageStoresList);
399397

400-
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock);
398+
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock);
401399
}
402400

403401
@Test
404402
public void getImageStoresThrowsExceptionIfNotFoundTestNonEmptyImageStoreShouldNotThrowCloudRuntimeException() {
405403
TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class);
406-
List<Long> zoneIds = List.of(1L);
407404
DataStore dataStoreMock = Mockito.mock(DataStore.class);
408405
List<DataStore> imageStoresList = List.of(dataStoreMock);
409406

410407
Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(imageStoresList);
411408

412-
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock);
409+
_adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock);
413410
}
414411

415412
@Test

0 commit comments

Comments
 (0)