forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeJoinDaoImpl.java
More file actions
340 lines (299 loc) · 14.6 KB
/
VolumeJoinDaoImpl.java
File metadata and controls
340 lines (299 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.api.query.dao;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import com.cloud.offering.DiskOffering;
import org.apache.cloudstack.annotation.AnnotationService;
import org.apache.cloudstack.annotation.dao.AnnotationDao;
import org.apache.cloudstack.api.ResponseObject.ResponseView;
import org.apache.cloudstack.api.response.VolumeResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.ApiResponseHelper;
import com.cloud.api.query.vo.VolumeJoinVO;
import com.cloud.offering.ServiceOffering;
import com.cloud.storage.Storage;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.storage.Volume;
import com.cloud.user.AccountManager;
import com.cloud.user.VmDiskStatisticsVO;
import com.cloud.user.dao.VmDiskStatisticsDao;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
@Component
public class VolumeJoinDaoImpl extends GenericDaoBaseWithTagInformation<VolumeJoinVO, VolumeResponse> implements VolumeJoinDao {
public static final Logger s_logger = Logger.getLogger(VolumeJoinDaoImpl.class);
@Inject
private ConfigurationDao _configDao;
@Inject
public AccountManager _accountMgr;
@Inject
private VmDiskStatisticsDao vmDiskStatsDao;
@Inject
private PrimaryDataStoreDao primaryDataStoreDao;
@Inject
private AnnotationDao annotationDao;
private final SearchBuilder<VolumeJoinVO> volSearch;
private final SearchBuilder<VolumeJoinVO> volIdSearch;
protected VolumeJoinDaoImpl() {
volSearch = createSearchBuilder();
volSearch.and("idIN", volSearch.entity().getId(), SearchCriteria.Op.IN);
volSearch.done();
volIdSearch = createSearchBuilder();
volIdSearch.and("id", volIdSearch.entity().getId(), SearchCriteria.Op.EQ);
volIdSearch.done();
_count = "select count(distinct id) from volume_view WHERE ";
}
@Override
public VolumeResponse newVolumeResponse(ResponseView view, VolumeJoinVO volume) {
VolumeResponse volResponse = new VolumeResponse();
volResponse.setId(volume.getUuid());
if (volume.getName() != null) {
volResponse.setName(volume.getName());
} else {
volResponse.setName("");
}
volResponse.setZoneId(volume.getDataCenterUuid());
volResponse.setZoneName(volume.getDataCenterName());
if (view == ResponseView.Full) {
volResponse.setClusterId(volume.getClusterUuid());
volResponse.setClusterName(volume.getClusterName());
volResponse.setPodId(volume.getPodUuid());
volResponse.setPodName(volume.getPodName());
}
if (volume.getVolumeType() != null) {
volResponse.setVolumeType(volume.getVolumeType().toString());
}
volResponse.setDeviceId(volume.getDeviceId());
long instanceId = volume.getVmId();
if (instanceId > 0 && volume.getState() != Volume.State.Destroy) {
volResponse.setVirtualMachineId(volume.getVmUuid());
volResponse.setVirtualMachineName(volume.getVmName());
if (volume.getVmState() != null) {
volResponse.setVirtualMachineState(volume.getVmState().toString());
}
if (volume.getVmDisplayName() != null) {
volResponse.setVirtualMachineDisplayName(volume.getVmDisplayName());
} else {
volResponse.setVirtualMachineDisplayName(volume.getVmName());
}
VmDiskStatisticsVO diskStats = vmDiskStatsDao.findBy(volume.getAccountId(), volume.getDataCenterId(), instanceId, volume.getId());
if (diskStats != null) {
volResponse.setDiskIORead(diskStats.getCurrentIORead());
volResponse.setDiskIOWrite(diskStats.getCurrentIOWrite());
volResponse.setDiskKbsRead((long) (diskStats.getCurrentBytesRead() / 1024.0));
volResponse.setDiskKbsWrite((long) (diskStats.getCurrentBytesWrite() / 1024.0));
}
}
if (volume.getProvisioningType() != null) {
volResponse.setProvisioningType(volume.getProvisioningType().toString());
}
// Show the virtual size of the volume
volResponse.setSize(volume.getSize());
volResponse.setMinIops(volume.getMinIops());
volResponse.setMaxIops(volume.getMaxIops());
volResponse.setCreated(volume.getCreated());
if (volume.getState() != null) {
volResponse.setState(volume.getState().toString());
}
if (volume.getState() == Volume.State.UploadOp) {
volResponse.setSize(volume.getVolumeStoreSize());
volResponse.setCreated(volume.getCreatedOnStore());
if (view == ResponseView.Full)
volResponse.setHypervisor(ApiDBUtils.getHypervisorTypeFromFormat(volume.getDataCenterId(), volume.getFormat()).toString());
if (volume.getDownloadState() != Status.DOWNLOADED) {
String volumeStatus = "Processing";
if (volume.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
if (volume.getDownloadPercent() == 100) {
volumeStatus = "Checking Volume";
} else {
volumeStatus = volume.getDownloadPercent() + "% Uploaded";
}
volResponse.setState("Uploading");
} else {
volumeStatus = volume.getErrorString();
if (volume.getDownloadState() == Status.NOT_DOWNLOADED) {
volResponse.setState("UploadNotStarted");
} else {
volResponse.setState("UploadError");
}
}
volResponse.setStatus(volumeStatus);
} else if (volume.getDownloadState() == Status.DOWNLOADED) {
volResponse.setStatus("Upload Complete");
volResponse.setState("Uploaded");
} else {
volResponse.setStatus("Successfully Installed");
}
}
if (view == ResponseView.Full) {
volResponse.setPath(volume.getPath());
}
// populate owner.
ApiResponseHelper.populateOwner(volResponse, volume);
if (volume.getDiskOfferingId() > 0) {
DiskOffering computeOnlyDiskOffering = ApiDBUtils.findComputeOnlyDiskOfferingById(volume.getDiskOfferingId());
if (computeOnlyDiskOffering != null) {
ServiceOffering serviceOffering = ApiDBUtils.findServiceOfferingByComputeOnlyDiskOffering(volume.getDiskOfferingId());
volResponse.setServiceOfferingId(String.valueOf(serviceOffering.getId()));
volResponse.setServiceOfferingName(serviceOffering.getName());
volResponse.setServiceOfferingDisplayText(serviceOffering.getDisplayText());
} else {
volResponse.setDiskOfferingId(volume.getDiskOfferingUuid());
volResponse.setDiskOfferingName(volume.getDiskOfferingName());
volResponse.setDiskOfferingDisplayText(volume.getDiskOfferingDisplayText());
}
if (view == ResponseView.Full) {
volResponse.setStorageType(volume.isUseLocalStorage() ? ServiceOffering.StorageType.local.toString() : ServiceOffering.StorageType.shared.toString());
}
volResponse.setBytesReadRate(volume.getBytesReadRate());
volResponse.setBytesWriteRate(volume.getBytesReadRate());
volResponse.setIopsReadRate(volume.getIopsWriteRate());
volResponse.setIopsWriteRate(volume.getIopsWriteRate());
}
// return hypervisor and storage pool info for ROOT and Resource domain only
if (view == ResponseView.Full) {
if (volume.getState() != Volume.State.UploadOp) {
if (volume.getHypervisorType() != null) {
volResponse.setHypervisor(volume.getHypervisorType().toString());
} else {
volResponse.setHypervisor(ApiDBUtils.getHypervisorTypeFromFormat(volume.getDataCenterId(), volume.getFormat()).toString());
}
}
Long poolId = volume.getPoolId();
String poolName = (poolId == null) ? "none" : volume.getPoolName();
volResponse.setStoragePoolName(poolName);
volResponse.setStoragePoolId(volume.getPoolUuid());
if (poolId != null) {
StoragePoolVO poolVO = primaryDataStoreDao.findById(poolId);
if (poolVO != null) {
volResponse.setStorageType(poolVO.isLocal() ? ServiceOffering.StorageType.local.toString() : ServiceOffering.StorageType.shared.toString());
if (poolVO.getParent() != 0L) {
StoragePoolVO datastoreClusterVO = primaryDataStoreDao.findById(poolVO.getParent());
volResponse.setStoragePoolName(datastoreClusterVO.getName());
volResponse.setStoragePoolId(datastoreClusterVO.getUuid());
}
}
}
}
volResponse.setAttached(volume.getAttached());
volResponse.setDestroyed(volume.getState() == Volume.State.Destroy);
boolean isExtractable = true;
if (volume.getVolumeType() != Volume.Type.DATADISK) { // Datadisk don't
// have any
// template
// dependence.
if (volume.getTemplateId() > 0) { // For ISO based volumes template
// = null and we allow extraction
// of all ISO based volumes
isExtractable = volume.isExtractable() && volume.getTemplateType() != Storage.TemplateType.SYSTEM;
}
}
// update tag information
long tag_id = volume.getTagId();
if (tag_id > 0) {
addTagInformation(volume, volResponse);
}
volResponse.setHasAnnotation(annotationDao.hasAnnotations(volume.getUuid(), AnnotationService.EntityType.VOLUME.name(),
_accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())));
volResponse.setExtractable(isExtractable);
volResponse.setDisplayVolume(volume.isDisplayVolume());
volResponse.setChainInfo(volume.getChainInfo());
volResponse.setTemplateId(volume.getTemplateUuid());
volResponse.setTemplateName(volume.getTemplateName());
volResponse.setTemplateDisplayText(volume.getTemplateDisplayText());
volResponse.setIsoId(volume.getIsoUuid());
volResponse.setIsoName(volume.getIsoName());
volResponse.setIsoDisplayText(volume.getIsoDisplayText());
// set async job
if (volume.getJobId() != null) {
volResponse.setJobId(volume.getJobUuid());
volResponse.setJobStatus(volume.getJobStatus());
}
volResponse.setObjectName("volume");
volResponse.setExternalUuid(volume.getExternalUuid());
return volResponse;
}
@Override
public VolumeResponse setVolumeResponse(ResponseView view, VolumeResponse volData, VolumeJoinVO vol) {
long tag_id = vol.getTagId();
if (tag_id > 0) {
addTagInformation(vol, volData);
}
if (volData.hasAnnotation() == null) {
volData.setHasAnnotation(annotationDao.hasAnnotations(vol.getUuid(), AnnotationService.EntityType.VOLUME.name(),
_accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())));
}
return volData;
}
@Override
public List<VolumeJoinVO> newVolumeView(Volume vol) {
SearchCriteria<VolumeJoinVO> sc = volIdSearch.create();
sc.setParameters("id", vol.getId());
return searchIncludingRemoved(sc, null, null, false);
}
@Override
public List<VolumeJoinVO> searchByIds(Long... volIds) {
// set detail batch query size
int DETAILS_BATCH_SIZE = 2000;
String batchCfg = _configDao.getValue("detail.batch.query.size");
if (batchCfg != null) {
DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
}
// query details by batches
List<VolumeJoinVO> uvList = new ArrayList<VolumeJoinVO>();
// query details by batches
int curr_index = 0;
if (volIds.length > DETAILS_BATCH_SIZE) {
while ((curr_index + DETAILS_BATCH_SIZE) <= volIds.length) {
Long[] ids = new Long[DETAILS_BATCH_SIZE];
for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
ids[k] = volIds[j];
}
SearchCriteria<VolumeJoinVO> sc = volSearch.create();
sc.setParameters("idIN", ids);
List<VolumeJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
if (vms != null) {
uvList.addAll(vms);
}
curr_index += DETAILS_BATCH_SIZE;
}
}
if (curr_index < volIds.length) {
int batch_size = (volIds.length - curr_index);
// set the ids value
Long[] ids = new Long[batch_size];
for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
ids[k] = volIds[j];
}
SearchCriteria<VolumeJoinVO> sc = volSearch.create();
sc.setParameters("idIN", ids);
List<VolumeJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
if (vms != null) {
uvList.addAll(vms);
}
}
return uvList;
}
}