Beispiel #1
0
  private void handle(final ExpungeImageMsg msg) {
    final ExpungeImageReply reply = new ExpungeImageReply();
    final ImageBackupStorageRefVO ref =
        CollectionUtils.find(
            self.getBackupStorageRefs(),
            new Function<ImageBackupStorageRefVO, ImageBackupStorageRefVO>() {
              @Override
              public ImageBackupStorageRefVO call(ImageBackupStorageRefVO arg) {
                return arg.getBackupStorageUuid().equals(msg.getBackupStorageUuid()) ? arg : null;
              }
            });

    if (ref == null) {
      logger.debug(
          String.format(
              "cannot find reference for the image[uuid:%s] on the backup storage[uuid:%s], assume it's been deleted",
              self.getUuid(), msg.getBackupStorageUuid()));
      bus.reply(msg, reply);
      return;
    }

    DeleteBitsOnBackupStorageMsg dmsg = new DeleteBitsOnBackupStorageMsg();
    dmsg.setBackupStorageUuid(ref.getBackupStorageUuid());
    dmsg.setInstallPath(ref.getInstallPath());
    bus.makeTargetServiceIdByResourceUuid(
        dmsg, BackupStorageConstant.SERVICE_ID, dmsg.getBackupStorageUuid());
    bus.send(
        dmsg,
        new CloudBusCallBack(msg) {
          @Override
          public void run(MessageReply r) {
            if (!r.isSuccess()) {
              // TODO
              logger.warn(
                  String.format(
                      "failed to delete image[uuid:%s, name:%s] from backup storage[uuid:%s] because %s, need to garbage collect it",
                      self.getUuid(), self.getName(), r.getError(), ref.getBackupStorageUuid()));
              reply.setError(r.getError());
            } else {
              returnBackupStorageCapacity(ref.getBackupStorageUuid(), self.getSize());
              dbf.remove(ref);
              logger.debug(
                  String.format(
                      "successfully expunged the image[uuid: %s, name: %s] on the backup storage[uuid: %s]",
                      self.getUuid(), self.getName(), ref.getBackupStorageUuid()));
              self = dbf.findByUuid(self.getUuid(), ImageVO.class);
              if (self.getBackupStorageRefs().isEmpty()) {
                logger.debug(
                    String.format(
                        "the image[uuid:%s, name:%s] has been expunged on all backup storage, remove it from database",
                        self.getUuid(), self.getName()));
                dbf.remove(self);
              }
            }

            bus.reply(msg, reply);
          }
        });
  }
Beispiel #2
0
  private void handle(final APIExpungeImageMsg msg) {
    List<String> bsUuids = new ArrayList<String>();
    if (msg.getBackupStorageUuids() == null || msg.getBackupStorageUuids().isEmpty()) {
      bsUuids =
          CollectionUtils.transformToList(
              self.getBackupStorageRefs(),
              new Function<String, ImageBackupStorageRefVO>() {
                @Override
                public String call(ImageBackupStorageRefVO arg) {
                  return ImageStatus.Deleted == arg.getStatus() ? arg.getBackupStorageUuid() : null;
                }
              });

      if (bsUuids.isEmpty()) {
        throw new OperationFailureException(
            errf.stringToOperationError(
                String.format(
                    "the image[uuid:%s, name:%s] is not deleted on any backup storage",
                    self.getUuid(), self.getName())));
      }
    } else {
      for (final String bsUuid : msg.getBackupStorageUuids()) {
        ImageBackupStorageRefVO ref =
            CollectionUtils.find(
                self.getBackupStorageRefs(),
                new Function<ImageBackupStorageRefVO, ImageBackupStorageRefVO>() {
                  @Override
                  public ImageBackupStorageRefVO call(ImageBackupStorageRefVO arg) {
                    return arg.getBackupStorageUuid().equals(bsUuid) ? arg : null;
                  }
                });

        if (ref == null) {
          throw new OperationFailureException(
              errf.stringToInvalidArgumentError(
                  String.format(
                      "the image[uuid:%s, name:%s] is not on the backup storage[uuid:%s]",
                      self.getUuid(), self.getName(), bsUuid)));
        }

        if (ref.getStatus() != ImageStatus.Deleted) {
          throw new OperationFailureException(
              errf.stringToInvalidArgumentError(
                  String.format(
                      "the image[uuid:%s, name:%s] is not deleted on the backup storage[uuid:%s]",
                      self.getUuid(), self.getName(), bsUuid)));
        }

        bsUuids.add(bsUuid);
      }
    }

    List<ExpungeImageMsg> emsgs =
        CollectionUtils.transformToList(
            bsUuids,
            new Function<ExpungeImageMsg, String>() {
              @Override
              public ExpungeImageMsg call(String arg) {
                ExpungeImageMsg emsg = new ExpungeImageMsg();
                emsg.setBackupStorageUuid(arg);
                emsg.setImageUuid(self.getUuid());
                bus.makeTargetServiceIdByResourceUuid(
                    emsg, ImageConstant.SERVICE_ID, self.getUuid());
                return emsg;
              }
            });

    final List<String> finalBsUuids = bsUuids;
    final APIExpungeImageEvent evt = new APIExpungeImageEvent(msg.getId());
    bus.send(
        emsgs,
        new CloudBusListCallBack(msg) {
          @Override
          public void run(List<MessageReply> replies) {
            for (MessageReply r : replies) {
              if (!r.isSuccess()) {
                String bsUuid = finalBsUuids.get(replies.indexOf(r));
                // TODO
                logger.warn(
                    String.format(
                        "failed to expunge the image[uuid:%s, name:%s] on the backup storage[uuid:%s]"
                            + ", %s",
                        self.getUuid(), self.getName(), bsUuid, r.getError()));
              }
            }

            bus.publish(evt);
          }
        });
  }
Beispiel #3
0
  private void handle(APIRecoverImageMsg msg) {
    List<String> toRecoverBsUuids;
    if (msg.getBackupStorageUuids() == null || msg.getBackupStorageUuids().isEmpty()) {
      toRecoverBsUuids =
          CollectionUtils.transformToList(
              self.getBackupStorageRefs(),
              new Function<String, ImageBackupStorageRefVO>() {
                @Override
                public String call(ImageBackupStorageRefVO arg) {
                  return arg.getStatus() == ImageStatus.Deleted ? arg.getBackupStorageUuid() : null;
                }
              });

      if (toRecoverBsUuids.isEmpty()) {
        throw new OperationFailureException(
            errf.stringToOperationError(
                String.format(
                    "the image[uuid:%s, name:%s] is not deleted on any backup storage",
                    self.getUuid(), self.getName())));
      }
    } else {
      toRecoverBsUuids = new ArrayList<String>();
      for (final String bsUuid : msg.getBackupStorageUuids()) {
        ImageBackupStorageRefVO ref =
            CollectionUtils.find(
                self.getBackupStorageRefs(),
                new Function<ImageBackupStorageRefVO, ImageBackupStorageRefVO>() {
                  @Override
                  public ImageBackupStorageRefVO call(ImageBackupStorageRefVO arg) {
                    return bsUuid.equals(arg.getBackupStorageUuid()) ? arg : null;
                  }
                });

        if (ref == null) {
          throw new OperationFailureException(
              errf.stringToInvalidArgumentError(
                  String.format(
                      "the image[uuid:%s, name:%s] is not on the backup storage[uuid:%s]",
                      self.getUuid(), self.getName(), bsUuid)));
        }

        if (ref.getStatus() != ImageStatus.Deleted) {
          throw new OperationFailureException(
              errf.stringToInvalidArgumentError(
                  String.format(
                      "the image[uuid:%s, name:%s]'s status[%s] is not Deleted on the backup storage[uuid:%s]",
                      self.getUuid(), self.getName(), ref.getStatus(), bsUuid)));
        }

        toRecoverBsUuids.add(bsUuid);
      }
    }

    for (ImageBackupStorageRefVO ref : self.getBackupStorageRefs()) {
      if (toRecoverBsUuids.contains(ref.getBackupStorageUuid())) {
        ref.setStatus(ImageStatus.Ready);
        dbf.update(ref);
      }
    }

    self.setStatus(ImageStatus.Ready);
    self = dbf.updateAndRefresh(self);

    logger.debug(
        String.format(
            "successfully recovered the image[uuid:%s, name:%s] on the backup storage%s",
            self.getUuid(), self.getName(), toRecoverBsUuids));
    APIRecoverImageEvent evt = new APIRecoverImageEvent(msg.getId());
    evt.setInventory(getSelfInventory());
    bus.publish(evt);
  }