Skip to content

Commit 85dfda4

Browse files
shwstpprweizhouapache
authored andcommitted
server: fix snapshot physical size (apache#10216)
Signed-off-by: Abhishek Kumar <[email protected]> Co-authored-by: Wei Zhou <[email protected]>
1 parent b604d8f commit 85dfda4

File tree

4 files changed

+136
-23
lines changed

4 files changed

+136
-23
lines changed

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,9 @@
350350
import com.cloud.vm.dao.UserVmDetailsDao;
351351
import com.cloud.vm.dao.VMInstanceDao;
352352

353-
354-
355-
356-
357-
358353
@Component
359354
public class QueryManagerImpl extends MutualExclusiveIdsManagerBase implements QueryService, Configurable {
360-
355+
361356

362357
private static final String ID_FIELD = "id";
363358

@@ -5829,7 +5824,6 @@ private Pair<List<SnapshotJoinVO>, Integer> searchForSnapshotsWithParams(final L
58295824

58305825
Integer count = snapshotDataPair.second();
58315826
if (count == 0) {
5832-
// empty result
58335827
return snapshotDataPair;
58345828
}
58355829
List<SnapshotJoinVO> snapshotData = snapshotDataPair.first();
@@ -5839,7 +5833,6 @@ private Pair<List<SnapshotJoinVO>, Integer> searchForSnapshotsWithParams(final L
58395833
} else {
58405834
snapshots = snapshotJoinDao.searchBySnapshotStorePair(snapshotData.stream().map(SnapshotJoinVO::getSnapshotStorePair).toArray(String[]::new));
58415835
}
5842-
58435836
return new Pair<>(snapshots, count);
58445837
}
58455838

server/src/main/java/com/cloud/api/query/dao/SnapshotJoinDao.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,15 @@
2323
import org.apache.cloudstack.api.response.SnapshotResponse;
2424

2525
import com.cloud.api.query.vo.SnapshotJoinVO;
26-
import com.cloud.utils.Pair;
27-
import com.cloud.utils.db.Filter;
2826
import com.cloud.utils.db.GenericDao;
29-
import com.cloud.utils.db.SearchCriteria;
3027

3128
public interface SnapshotJoinDao extends GenericDao<SnapshotJoinVO, Long> {
3229

3330
SnapshotResponse newSnapshotResponse(ResponseObject.ResponseView view, boolean isShowUnique, SnapshotJoinVO snapshotJoinVO);
3431

3532
SnapshotResponse setSnapshotResponse(SnapshotResponse snapshotResponse, SnapshotJoinVO snapshot);
3633

37-
Pair<List<SnapshotJoinVO>, Integer> searchIncludingRemovedAndCount(final SearchCriteria<SnapshotJoinVO> sc, final Filter filter);
38-
3934
List<SnapshotJoinVO> searchBySnapshotStorePair(String... pairs);
35+
4036
List<SnapshotJoinVO> findByDistinctIds(Long zoneId, Long... ids);
4137
}

server/src/main/java/com/cloud/api/query/dao/SnapshotJoinDaoImpl.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package com.cloud.api.query.dao;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.Comparator;
2123
import java.util.HashMap;
2224
import java.util.List;
2325
import java.util.Map;
@@ -34,6 +36,8 @@
3436
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
3537
import org.apache.cloudstack.query.QueryService;
3638

39+
import org.apache.commons.collections.CollectionUtils;
40+
3741
import com.cloud.api.ApiDBUtils;
3842
import com.cloud.api.ApiResponseHelper;
3943
import com.cloud.api.query.vo.SnapshotJoinVO;
@@ -44,7 +48,6 @@
4448
import com.cloud.storage.VolumeVO;
4549
import com.cloud.user.Account;
4650
import com.cloud.user.AccountService;
47-
import com.cloud.utils.Pair;
4851
import com.cloud.utils.db.Filter;
4952
import com.cloud.utils.db.SearchBuilder;
5053
import com.cloud.utils.db.SearchCriteria;
@@ -192,13 +195,6 @@ public SnapshotResponse setSnapshotResponse(SnapshotResponse snapshotResponse, S
192195
return snapshotResponse;
193196
}
194197

195-
@Override
196-
public Pair<List<SnapshotJoinVO>, Integer> searchIncludingRemovedAndCount(final SearchCriteria<SnapshotJoinVO> sc, final Filter filter) {
197-
List<SnapshotJoinVO> objects = searchIncludingRemoved(sc, filter, null, false);
198-
Integer count = getDistinctCount(sc);
199-
return new Pair<>(objects, count);
200-
}
201-
202198
@Override
203199
public List<SnapshotJoinVO> searchBySnapshotStorePair(String... pairs) {
204200
// set detail batch query size
@@ -243,14 +239,33 @@ public List<SnapshotJoinVO> searchBySnapshotStorePair(String... pairs) {
243239
return uvList;
244240
}
245241

242+
protected List<SnapshotJoinVO> findById(Long zoneId, long id) {
243+
SearchBuilder<SnapshotJoinVO> sb = createSearchBuilder();
244+
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
245+
sb.and("zoneId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
246+
sb.done();
247+
SearchCriteria<SnapshotJoinVO> sc = sb.create();
248+
sc.setParameters("id", id);
249+
if (zoneId != null) {
250+
sc.setParameters("zoneId", zoneId);
251+
}
252+
List<SnapshotJoinVO> snapshotJoinVOS = search(sc, null);
253+
if (CollectionUtils.isEmpty(snapshotJoinVOS)) {
254+
return null;
255+
}
256+
snapshotJoinVOS.sort(Comparator.comparing(SnapshotJoinVO::getSnapshotStorePair));
257+
return Collections.singletonList(snapshotJoinVOS.get(0));
258+
}
259+
246260
@Override
247261
public List<SnapshotJoinVO> findByDistinctIds(Long zoneId, Long... ids) {
248262
if (ids == null || ids.length == 0) {
249263
return new ArrayList<>();
250264
}
251-
265+
if (ids.length == 1) {
266+
return findById(zoneId, ids[0]);
267+
}
252268
Filter searchFilter = new Filter(SnapshotJoinVO.class, "snapshotStorePair", QueryService.SortKeyAscending.value(), null, null);
253-
254269
SearchCriteria<SnapshotJoinVO> sc = snapshotIdsSearch.create();
255270
if (zoneId != null) {
256271
sc.setParameters("zoneId", zoneId);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api.query.dao;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
22+
import static org.mockito.Mockito.doReturn;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.never;
25+
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.when;
27+
28+
import java.util.Arrays;
29+
import java.util.Collections;
30+
import java.util.List;
31+
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.mockito.InjectMocks;
36+
import org.mockito.Mock;
37+
import org.mockito.Spy;
38+
import org.mockito.junit.MockitoJUnitRunner;
39+
40+
import com.cloud.api.query.vo.SnapshotJoinVO;
41+
import com.cloud.utils.db.SearchBuilder;
42+
import com.cloud.utils.db.SearchCriteria;
43+
44+
@RunWith(MockitoJUnitRunner.class)
45+
public class SnapshotJoinDaoImplTest {
46+
47+
@Spy
48+
@InjectMocks
49+
SnapshotJoinDaoImpl snapshotJoinDao = new SnapshotJoinDaoImpl();
50+
51+
@Mock
52+
SearchCriteria<SnapshotJoinVO> mockSearchCriteria;
53+
54+
@Before
55+
public void setUp() {
56+
SnapshotJoinVO mockSnap = mock(SnapshotJoinVO.class);
57+
SearchBuilder<SnapshotJoinVO> mockSearchBuilder = mock(SearchBuilder.class);
58+
when(mockSearchBuilder.entity()).thenReturn(mockSnap);
59+
doReturn(mockSearchBuilder).when(snapshotJoinDao).createSearchBuilder();
60+
when(mockSearchBuilder.create()).thenReturn(mockSearchCriteria);
61+
}
62+
63+
@Test
64+
public void testFindById_WithNullZoneId_EmptyResult() {
65+
Long zoneId = null;
66+
long id = 1L;
67+
doReturn(Collections.emptyList()).when(snapshotJoinDao).search(mockSearchCriteria, null);
68+
List<SnapshotJoinVO> result = snapshotJoinDao.findById(zoneId, id);
69+
assertNull(result);
70+
verify(mockSearchCriteria).setParameters("id", id);
71+
verify(mockSearchCriteria, never()).setParameters("zoneId", zoneId);
72+
}
73+
74+
@Test
75+
public void testFindById_WithValidZoneId_EmptyResult() {
76+
Long zoneId = 1L;
77+
long id = 1L;
78+
doReturn(Collections.emptyList()).when(snapshotJoinDao).search(mockSearchCriteria, null);
79+
List<SnapshotJoinVO> result = snapshotJoinDao.findById(zoneId, id);
80+
assertNull(result);
81+
verify(mockSearchCriteria).setParameters("id", id);
82+
verify(mockSearchCriteria).setParameters("zoneId", zoneId);
83+
}
84+
85+
@Test
86+
public void testFindById_WithValidResults() {
87+
Long zoneId = 1L;
88+
long id = 1L;
89+
SnapshotJoinVO snapshot1 = mock(SnapshotJoinVO.class);
90+
when(snapshot1.getSnapshotStorePair()).thenReturn("Primary_1");
91+
SnapshotJoinVO snapshot2 = mock(SnapshotJoinVO.class);
92+
when(snapshot2.getSnapshotStorePair()).thenReturn("Image_1");
93+
doReturn(Arrays.asList(snapshot1, snapshot2)).when(snapshotJoinDao).search(mockSearchCriteria, null);
94+
List<SnapshotJoinVO> result = snapshotJoinDao.findById(zoneId, id);
95+
assertNotNull(result);
96+
assertEquals(1, result.size());
97+
assertEquals("Image_1", result.get(0).getSnapshotStorePair());
98+
verify(mockSearchCriteria).setParameters("id", id);
99+
verify(mockSearchCriteria).setParameters("zoneId", zoneId);
100+
}
101+
102+
@Test
103+
public void testFindById_WithNullResults() {
104+
long id = 1L;
105+
doReturn(null).when(snapshotJoinDao).search(mockSearchCriteria, null);
106+
List<SnapshotJoinVO> result = snapshotJoinDao.findById(null, id);
107+
assertNull(result);
108+
}
109+
}

0 commit comments

Comments
 (0)