public Map<String, List<String>> getTags(List<String> resourceUuids, Class resourceClass) { SimpleQuery<SystemTagVO> q = dbf.createQuery(SystemTagVO.class); q.select(SystemTagVO_.tag, SystemTagVO_.resourceUuid); q.add(SystemTagVO_.resourceType, Op.EQ, resourceClass.getSimpleName()); q.add(SystemTagVO_.resourceUuid, Op.IN, resourceUuids); q.add(SystemTagVO_.tag, useOp(), useTagFormat()); List<Tuple> ts = q.listTuple(); Map<String, List<String>> ret = new HashMap<>(); for (Tuple t : ts) { String uuid = t.get(1, String.class); List<String> tags = ret.get(uuid); if (tags == null) { tags = new ArrayList<>(); ret.put(uuid, tags); } tags.add(t.get(0, String.class)); } return ret; }
public boolean isExist(String hostUuid, String primaryStorageUuid) { SimpleQuery<LocalStorageHostRefVO> hq = dbf.createQuery(LocalStorageHostRefVO.class); hq.add(LocalStorageHostRefVO_.hostUuid, SimpleQuery.Op.EQ, hostUuid); hq.add(LocalStorageHostRefVO_.primaryStorageUuid, SimpleQuery.Op.EQ, primaryStorageUuid); return hq.isExists(); } }
private boolean checkIfNeedBackupStorageToDownloadImage(HostAllocatorSpec spec, List<HostVO> candidates) { List<String> clusterUuids = CollectionUtils.transformToList(candidates, new Function<String, HostVO>() { @Override public String call(HostVO arg) { return arg.getClusterUuid(); } }); SimpleQuery<PrimaryStorageClusterRefVO> pq = dbf.createQuery(PrimaryStorageClusterRefVO.class); pq.select(PrimaryStorageClusterRefVO_.primaryStorageUuid); pq.add(PrimaryStorageClusterRefVO_.clusterUuid, Op.IN, clusterUuids); List<String> psUuids = pq.listValue(); if (psUuids.isEmpty()) { return true; } SimpleQuery<ImageCacheVO> cq = dbf.createQuery(ImageCacheVO.class); cq.add(ImageCacheVO_.imageUuid, Op.EQ, spec.getImage().getUuid()); cq.add(ImageCacheVO_.primaryStorageUuid, Op.IN, psUuids); cq.groupBy(ImageCacheVO_.primaryStorageUuid); long count = cq.count(); return count != psUuids.size(); }
private List<Tuple> getDeletedVolumeManagedByUs() { int qun = 1000; SimpleQuery q = dbf.createQuery(VolumeVO.class); q.add(VolumeVO_.status, Op.EQ, VolumeStatus.Deleted); q.add(VolumeVO_.type, Op.EQ, VolumeType.Data); long amount = q.count(); int times = (int) (amount / qun) + (amount % qun != 0 ? 1 : 0); int start = 0; List<Tuple> ret = new ArrayList<Tuple>(); for (int i = 0; i < times; i++) { q = dbf.createQuery(VolumeVO.class); q.select(VolumeVO_.uuid, VolumeVO_.lastOpDate); q.add(VolumeVO_.status, Op.EQ, VolumeStatus.Deleted); q.add(VolumeVO_.type, Op.EQ, VolumeType.Data); q.setLimit(qun); q.setStart(start); List<Tuple> lst = q.listTuple(); start += qun; for (Tuple t : lst) { String uuid = t.get(0, String.class); if (!destMaker.isManagedByUs(uuid)) { continue; } ret.add(t); } } return ret; }
private void restartQueue(JobQueueVO qvo, String mgmtId) { SimpleQuery<JobQueueEntryVO> q = dbf.createQuery(JobQueueEntryVO.class); q.select(JobQueueEntryVO_.id, JobQueueEntryVO_.name); q.add(JobQueueEntryVO_.jobQueueId, SimpleQuery.Op.EQ, qvo.getId()); q.add(JobQueueEntryVO_.issuerManagementNodeId, SimpleQuery.Op.NULL); List<Tuple> ts = q.listTuple(); for (Tuple t : ts) { logger.debug(String.format("[Job Removed]: job[id:%s, name:%s] because its issuer management node[id:%s] became available", t.get(0), t.get(1), mgmtId)); q.add(JobQueueEntryVO_.state, SimpleQuery.Op.IN, JobState.Pending, JobState.Processing); q.add(JobQueueEntryVO_.jobQueueId, SimpleQuery.Op.EQ, qvo.getId()); q.orderBy(JobQueueEntryVO_.id, SimpleQuery.Od.ASC); long count = q.count(); if (count == 0) { logger.debug(String.format("[JobQueue Removed]: id:%s, no Pending or Processing job remaining in this queue, remove it", qvo.getId())); List<JobQueueEntryVO> es = q.list(); for (JobQueueEntryVO e : es) { if (e.getState() == JobState.Processing && !e.isRestartable()) {
private int getNextVolumeDeviceId(String vmUuid) { SimpleQuery<VolumeVO> q = dbf.createQuery(VolumeVO.class); q.select(VolumeVO_.deviceId); q.add(VolumeVO_.vmInstanceUuid, Op.EQ, vmUuid); q.add(VolumeVO_.deviceId, Op.NOT_NULL); q.orderBy(VolumeVO_.deviceId, Od.ASC); List<Integer> devIds = q.listValue(); BitSet full = new BitSet(devIds.size() + 1); devIds.forEach(full::set); return full.nextClearBit(0); }
protected String getHostnameFromBackupStorage(CephBackupStorageInventory inv) { SimpleQuery<CephBackupStorageMonVO> q = dbf.createQuery(CephBackupStorageMonVO.class); q.select(CephBackupStorageMonVO_.hostname); q.add(CephBackupStorageMonVO_.backupStorageUuid, SimpleQuery.Op.EQ, inv.getUuid()); q.add(CephBackupStorageMonVO_.status, SimpleQuery.Op.EQ, MonStatus.Connected); q.setLimit(1); String hostName = q.findValue(); DebugUtils.Assert(hostName!= null, String.format("cannot find hostName for ceph backup storage [uuid:%s]", inv.getUuid())); return hostName; }
private long getUsedVolumeSnapshotNum(String accountUuid) { SimpleQuery<AccountResourceRefVO> queryVolumeSnapshotNum = dbf.createQuery(AccountResourceRefVO.class); queryVolumeSnapshotNum.add(AccountResourceRefVO_.accountUuid, Op.EQ, accountUuid); queryVolumeSnapshotNum.add(AccountResourceRefVO_.resourceType, Op.EQ, VolumeSnapshotVO.class.getSimpleName()); return queryVolumeSnapshotNum.count(); }
@Override public void preAttachVolume(VmInstanceInventory vm, final VolumeInventory volume) { SimpleQuery<LocalStorageResourceRefVO> q = dbf.createQuery(LocalStorageResourceRefVO.class); q.add(LocalStorageResourceRefVO_.resourceUuid, Op.IN, list(vm.getRootVolumeUuid(), volume.getUuid())); q.groupBy(LocalStorageResourceRefVO_.hostUuid); long count = q.count(); q.select(LocalStorageResourceRefVO_.hostUuid); q.add(LocalStorageResourceRefVO_.resourceUuid, Op.EQ, vm.getRootVolumeUuid()); String rootHost = q.findValue(); q.select(LocalStorageResourceRefVO_.hostUuid); q.add(LocalStorageResourceRefVO_.resourceUuid, Op.EQ, volume.getUuid()); String dataHost = q.findValue();
protected boolean checkIpAvailability(String ip) { SimpleQuery<IpRangeVO> rq = dbf.createQuery(IpRangeVO.class); rq.select(IpRangeVO_.startIp, IpRangeVO_.endIp, IpRangeVO_.gateway); rq.add(IpRangeVO_.l3NetworkUuid, Op.EQ, self.getUuid()); List<Tuple> ts = rq.listTuple(); boolean inRange = false; boolean isGateway = false; for (Tuple t : ts) { String sip = t.get(0, String.class); String eip = t.get(1, String.class); String gw = t.get(2, String.class); if (ip.equals(gw)) { isGateway = true; break; } if (NetworkUtils.isInRange(ip, sip, eip)) { inRange = true; break; } } if (!inRange || isGateway) { // not an IP of this L3 or is a gateway return false; } else { SimpleQuery<UsedIpVO> q = dbf.createQuery(UsedIpVO.class); q.add(UsedIpVO_.l3NetworkUuid, Op.EQ, self.getUuid()); q.add(UsedIpVO_.ip, Op.EQ, ip); return !q.isExists(); } }
protected WorkFlowContext carryOnRestart() throws WorkFlowException { SimpleQuery<WorkFlowVO> query = dbf.createQuery(WorkFlowVO.class); query.add(WorkFlowVO_.chainUuid, Op.EQ, chainvo.getUuid()); query.orderBy(WorkFlowVO_.position, Od.DESC); List<WorkFlowVO> vos = query.list(); WorkFlowVO last = vos.get(0); assert last.getState() == WorkFlowState.Done || last.getState() == WorkFlowState.Processing : String.format("How can work flow[%s] in %s state when restart workflow chain[uuid:%s] !!?", last.getName(), last.getState(), chainvo.getUuid()); return run(WorkFlowContext.fromBytes(last.getContext()), last); }
private void trackOurs() { SimpleQuery<VirtualRouterVmVO> q = dbf.createQuery(VirtualRouterVmVO.class); q.select(VirtualRouterVmVO_.uuid); List<String> vrUuids = q.listValue(); List<String> toTrack = CollectionUtils.transformToList(vrUuids, new Function<String, String>() { @Override public String call(String arg) { return destinationMaker.isManagedByUs(arg) ? arg : null; } }); untrackAll(); track(toTrack); }
private Bucket getHostManagedByUs() { int qun = 10000; long amount = dbf.count(HostVO.class); int times = (int) (amount / qun) + (amount % qun != 0 ? 1 : 0); List<String> connected = new ArrayList<String>(); List<String> disconnected = new ArrayList<String>(); int start = 0; for (int i = 0; i < times; i++) { SimpleQuery<HostVO> q = dbf.createQuery(HostVO.class); q.select(HostVO_.uuid, HostVO_.status); q.setLimit(qun); q.setStart(start); List<Tuple> lst = q.listTuple(); start += qun; for (Tuple t : lst) { String huuid = t.get(0, String.class); if (!destMaker.isManagedByUs(huuid)) { continue; } HostStatus state = t.get(1, HostStatus.class); if (state == HostStatus.Connected) { connected.add(huuid); } else { // for Disconnected and Connecting, treat as Disconnected disconnected.add(huuid); } } } return Bucket.newBucket(connected, disconnected); }
@Transactional(readOnly = true) private LdapServerVO getLdapServer() { SimpleQuery<LdapServerVO> sq = dbf.createQuery(LdapServerVO.class); List<LdapServerVO> ldapServers = sq.list(); if (ldapServers.isEmpty()) { throw new CloudRuntimeException("No ldap server record in database."); } if (ldapServers.size() > 1) { throw new CloudRuntimeException("More than one ldap server record in database."); } return ldapServers.get(0); }
@Override public long count(Class<?> entityClass) { SimpleQuery<?> query = this.createQuery(entityClass); return query.count(); }
public Map<String, Quota.QuotaPair> makeQuotaPairs(String accountUuid) { SimpleQuery<QuotaVO> q = dbf.createQuery(QuotaVO.class); q.select(QuotaVO_.name, QuotaVO_.value); q.add(QuotaVO_.identityType, SimpleQuery.Op.EQ, AccountVO.class.getSimpleName()); q.add(QuotaVO_.identityUuid, SimpleQuery.Op.EQ, accountUuid); List<Tuple> ts = q.listTuple(); Map<String, Quota.QuotaPair> pairs = new HashMap<>(); for (Tuple t : ts) { String name = t.get(0, String.class); long value = t.get(1, Long.class); Quota.QuotaPair p = new Quota.QuotaPair(); p.setName(name); p.setValue(value); pairs.put(name, p); } return pairs; }