Example #1
0
      {
        for (VmNicVO nic : self.getVmNics()) {
          l3NicMacMap.put(nic.getL3NetworkUuid(), nic.getMac());
        }

        SimpleQuery<ApplianceVmFirewallRuleVO> q = dbf.createQuery(ApplianceVmFirewallRuleVO.class);
        q.add(ApplianceVmFirewallRuleVO_.applianceVmUuid, Op.EQ, self.getUuid());
        total = q.count();
      }
  private String steppingAllocate(long s, long e, int total, String rangeUuid) {
    int step = 254;
    int failureCount = 0;
    int failureCheckPoint = 5;

    while (s < e) {
      // if failing failureCheckPoint times, the range is probably full,
      // we check the range.
      // why don't we check before steppingAllocate()? because in that case we
      // have to count the used IP every time allocating a IP, and count operation
      // is a full scan in DB, which is very costly
      if (failureCheckPoint == failureCount++) {
        SimpleQuery<UsedIpVO> q = dbf.createQuery(UsedIpVO.class);
        q.add(UsedIpVO_.ipRangeUuid, Op.EQ, rangeUuid);
        long count = q.count();
        if (count == total) {
          logger.debug(
              String.format("ip range[uuid:%s] has no ip available, try next one", rangeUuid));
          return null;
        } else {
          failureCount = 0;
        }
      }

      long te = s + step;
      te = te > e ? e : te;
      SimpleQuery<UsedIpVO> q = dbf.createQuery(UsedIpVO.class);
      q.select(UsedIpVO_.ipInLong);
      q.add(UsedIpVO_.ipInLong, Op.GTE, s);
      q.add(UsedIpVO_.ipInLong, Op.LTE, te);
      q.add(UsedIpVO_.ipRangeUuid, Op.EQ, rangeUuid);
      List<Long> used = q.listValue();
      if (te - s + 1 == used.size()) {
        s += step;
        continue;
      }

      Collections.sort(used);

      return NetworkUtils.randomAllocateIpv4Address(s, te, used);
    }

    return null;
  }
Example #3
0
  @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();

    if (count < 2) {
      return;
    }

    q = dbf.createQuery(LocalStorageResourceRefVO.class);
    q.select(LocalStorageResourceRefVO_.hostUuid);
    q.add(LocalStorageResourceRefVO_.resourceUuid, Op.EQ, vm.getRootVolumeUuid());
    String rootHost = q.findValue();

    q = dbf.createQuery(LocalStorageResourceRefVO.class);
    q.select(LocalStorageResourceRefVO_.hostUuid);
    q.add(LocalStorageResourceRefVO_.resourceUuid, Op.EQ, volume.getUuid());
    String dataHost = q.findValue();

    if (!rootHost.equals(dataHost)) {
      throw new OperationFailureException(
          errf.stringToOperationError(
              String.format(
                  "cannot attach the data volume[uuid:%s] to the vm[uuid:%s]. Both vm's root volume and the data volume are"
                      + " on local primary storage, but they are on different hosts. The root volume[uuid:%s] is on the host[uuid:%s] but the data volume[uuid: %s]"
                      + " is on the host[uuid: %s]",
                  volume.getUuid(),
                  vm.getUuid(),
                  vm.getRootVolumeUuid(),
                  rootHost,
                  volume.getUuid(),
                  dataHost)));
    }
  }