Search in sources :

Example 1 with DownloadingInfo

use of com.emc.storageos.coordinator.client.model.DownloadingInfo in project coprhd-controller by CoprHD.

the class UpgradeService method checkDownloadProgress.

/**
 * For download progress monitoring, The zookeeper structure used in the setNodeGlobalScopeInfo() and getNodeGlobalScopeInfo() is
 * /sites/(site_uuid)/config/downloadinfo/(svcId)
 * Each node has a entry in the coordinator indicated by its svcId.
 * The remote download and internode download are monitored in the same way, because the process is the same in the UpgradeImageCommon
 * class.
 * Every second if the newly downloaded bytes are more that 1MB, we update the progress entry in the coordinator.
 * For the cancel function, it first check the progress entries in the coordinator to see if there is a download in progress, if there
 * is, get the
 * version from the entry, and erase this version from the target RepositoryInfo object in the coordinator. This operation will
 * terminate the ongoing download process.
 */
/**
 * Check the version downloading progress. the downloading could be from remote repository
 *
 * @return image downloading progress
 * @throws Exception
 */
@GET
@Path("image/download/progress/")
@CheckPermission(roles = { Role.SYSTEM_ADMIN, Role.RESTRICTED_SYSTEM_ADMIN })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public DownloadProgress checkDownloadProgress(@QueryParam("site") String siteId, @Context HttpHeaders headers) throws Exception {
    _log.info("checkDownloadProgress()");
    DownloadProgress progress = new DownloadProgress();
    DownloadingInfo targetDownloadInfo = _coordinator.getTargetInfo(DownloadingInfo.class);
    if (targetDownloadInfo == null || targetDownloadInfo._status == DownloadStatus.CANCELLED) {
        // return empty progress. No download in progress
        return progress;
    }
    progress.setImageSize(targetDownloadInfo._size);
    for (String svcId : _coordinator.getAllNodes(siteId)) {
        DownloadingInfo downloadInfo = _coordinator.getNodeGlobalScopeInfo(DownloadingInfo.class, siteId, DOWNLOADINFO_KIND, svcId);
        if (null == downloadInfo) {
            progress.addNodeProgress(svcId, new NodeProgress(0, DownloadStatus.NORMAL, 0, 0));
        } else {
            int downloadErrorCount = downloadInfo._errorCounter.get(0);
            int checksumErrorCount = downloadInfo._errorCounter.get(1);
            progress.addNodeProgress(svcId, new NodeProgress(downloadInfo.downloadedBytes, downloadInfo._status, downloadErrorCount, checksumErrorCount));
        }
    }
    return progress;
}
Also used : DownloadingInfo(com.emc.storageos.coordinator.client.model.DownloadingInfo) DownloadProgress(com.emc.vipr.model.sys.DownloadProgress) NodeProgress(com.emc.vipr.model.sys.NodeProgress) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 2 with DownloadingInfo

use of com.emc.storageos.coordinator.client.model.DownloadingInfo in project coprhd-controller by CoprHD.

the class UpgradeService method cancelInstallingOrUploadingImage.

/**
 * Cancel installing a version or uploading a version. Remove it from the target RepositoryInfo
 *
 * @prereq There should be image downloading in progress.
 * @return Cluster state information
 * @throws IOException
 */
@POST
@Path("image/install/cancel/")
@CheckPermission(roles = { Role.SYSTEM_ADMIN, Role.RESTRICTED_SYSTEM_ADMIN })
public Response cancelInstallingOrUploadingImage() throws IOException {
    _log.info("cancelInstallingOrUploadingImage()");
    DownloadingInfo downloadInfo = null;
    boolean inProgressFlag = false;
    DownloadingInfo downloadTargetInfo;
    try {
        downloadTargetInfo = _coordinator.getTargetInfo(DownloadingInfo.class);
    } catch (Exception e1) {
        throw APIException.internalServerErrors.getObjectFromError("Target downloading info", "coordinator", e1);
    }
    if (null == downloadTargetInfo || DownloadStatus.CANCELLED == downloadTargetInfo._status) {
        // Check the target info of the
        // DownloadingInfo class to see if user
        // sent a cancel request
        // No previous installation/upload or installation/upload got cancelled, not in progress
        inProgressFlag = false;
    }
    for (String svcId : _coordinator.getAllNodes()) {
        DownloadingInfo tmpInfo;
        try {
            tmpInfo = _coordinator.getNodeGlobalScopeInfo(DownloadingInfo.class, DOWNLOADINFO_KIND, svcId);
        } catch (Exception e) {
            throw APIException.internalServerErrors.getObjectFromError("Node downloading info", "coordinator", e);
        }
        if (null != tmpInfo && tmpInfo._status != DownloadStatus.COMPLETED) {
            downloadInfo = tmpInfo;
            inProgressFlag = true;
            break;
        }
    }
    if (!inProgressFlag) {
        throw APIException.badRequests.noDownloadInProgress();
    }
    String installingVersion = downloadInfo._version;
    if (installingVersion.equals(VIPR_UNKNOWN_IMAGE_VERSION)) {
        throw ServiceUnavailableException.serviceUnavailable.versionOfTheImageIsUnknownSoFar();
    }
    _coordinator.setTargetInfo(downloadInfo.cancel(), false);
    return removeImage(installingVersion, "1");
}
Also used : DownloadingInfo(com.emc.storageos.coordinator.client.model.DownloadingInfo) RemoteRepositoryException(com.emc.storageos.systemservices.exceptions.RemoteRepositoryException) ServiceUnavailableException(com.emc.storageos.svcs.errorhandling.resources.ServiceUnavailableException) InvalidSoftwareVersionException(com.emc.storageos.coordinator.exceptions.InvalidSoftwareVersionException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) LocalRepositoryException(com.emc.storageos.systemservices.exceptions.LocalRepositoryException) IOException(java.io.IOException) CoordinatorClientException(com.emc.storageos.systemservices.exceptions.CoordinatorClientException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 3 with DownloadingInfo

use of com.emc.storageos.coordinator.client.model.DownloadingInfo in project coprhd-controller by CoprHD.

the class UpgradeImageCommon method start.

public boolean start() throws Exception {
    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    // chunk size
    int chunkSize = 0x10000;
    // main buffer used to read and write
    byte[] buffer = new byte[chunkSize];
    // trailer buffer
    byte[] trailerBuffer = new byte[TRAILER_LENGTH];
    // number of valid bytes in trailerBuffer
    int validBytesCnt = 0;
    int bytesRead = 0;
    long bytesWritten = 0;
    long bytesDownloaded = 0;
    long currentTime = System.currentTimeMillis();
    CoordinatorClientExt coordinator = _manager.getCoordinator();
    String svcId = coordinator.getMySvcId();
    DownloadingInfo targetDownloadingInfo = coordinator.getTargetInfo(DownloadingInfo.class);
    boolean trackProgress = false;
    long imageSize = 0;
    if (targetDownloadingInfo != null && _version.equals(targetDownloadingInfo._version)) {
        trackProgress = true;
        imageSize = targetDownloadingInfo._size;
    }
    ArrayList<Integer> counter = null;
    // counter
    if (trackProgress) {
        DownloadingInfo nodeDownloadingInfo = coordinator.getNodeGlobalScopeInfo(DownloadingInfo.class, DOWNLOADINFO_KIND, svcId);
        if (nodeDownloadingInfo == null || nodeDownloadingInfo._version != targetDownloadingInfo._version) {
            counter = targetDownloadingInfo._errorCounter;
        } else {
            counter = nodeDownloadingInfo._errorCounter;
        }
    }
    try {
        while (true) {
            bytesRead = _in.read(buffer);
            if (bytesRead == -1) {
                break;
            }
            int overFlow = validBytesCnt + bytesRead - TRAILER_LENGTH;
            if (overFlow > 0) {
                if (overFlow >= validBytesCnt) {
                    // compute sha for the valid bytes staying in cache from last read
                    sha1.update(trailerBuffer, 0, validBytesCnt);
                    // copy last trailer length bytes into cache
                    System.arraycopy(buffer, bytesRead - TRAILER_LENGTH, trailerBuffer, 0, TRAILER_LENGTH);
                    // sha the rest in buffer
                    sha1.update(buffer, 0, bytesRead - TRAILER_LENGTH);
                } else {
                    // sha the bytes which cannot be trailer
                    sha1.update(trailerBuffer, 0, overFlow);
                    // shift trailer to left
                    for (int i = overFlow; i < validBytesCnt; i++) {
                        trailerBuffer[i - overFlow] = trailerBuffer[i];
                    }
                    // add read bytes at the end of trailer buffer
                    System.arraycopy(buffer, 0, trailerBuffer, TRAILER_LENGTH - bytesRead, bytesRead);
                }
                // trailer must be full
                validBytesCnt = TRAILER_LENGTH;
            } else {
                // trailer does not overflow, add read bytes at the end
                System.arraycopy(buffer, 0, trailerBuffer, validBytesCnt, bytesRead);
                validBytesCnt += bytesRead;
            }
            _out.write(buffer, 0, bytesRead);
            bytesWritten += bytesRead;
            long timeNow = System.currentTimeMillis();
            if (bytesWritten - bytesDownloaded > DOWNLOAD_MONITORING_SIZE_INCREMENT && timeNow - currentTime > DOWNLOAD_MONITORING_TIME_INCREMENT) {
                if (trackProgress) {
                    DownloadingInfo tmpInfo = coordinator.getTargetInfo(DownloadingInfo.class);
                    bytesDownloaded = bytesWritten;
                    currentTime = timeNow;
                    _log.info("The bytesDownloaded is " + bytesDownloaded);
                    if (null != tmpInfo && DownloadStatus.CANCELLED == tmpInfo._status) {
                        // Check the target info of the
                        // DownloadingInfo class to see if user sent a
                        // cancel request
                        coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(_version, imageSize, 0, DownloadStatus.CANCELLED, counter), DOWNLOADINFO_KIND, svcId);
                        return false;
                    }
                    coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(_version, imageSize, bytesDownloaded, DownloadStatus.NORMAL, counter), DOWNLOADINFO_KIND, // reset the bytesDownloaded field
                    svcId);
                }
            }
        }
        _log.info(_prefix + "Downloaded " + bytesWritten + " bytes");
        byte[] sha1Bytes = sha1.digest();
        // bytes is downloaded just output the downloaded content to the logs
        if (bytesWritten > 0 && bytesWritten < MINIMUM_IMAGE_BYTES) {
            _log.error(_prefix + "Downloaded error: {}", new String(buffer, "UTF-8"));
            if (trackProgress) {
                int downloadErrorCount = counter.get(0);
                counter.set(0, ++downloadErrorCount);
                coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(_version, imageSize, 0, DownloadStatus.DOWNLOADERROR, counter), DOWNLOADINFO_KIND, // It indicate a download error
                svcId);
            }
            return false;
        } else if (!verifyChecksum(sha1Bytes, trailerBuffer, _log)) {
            if (trackProgress) {
                int checksumErrorCount = counter.get(1);
                counter.set(1, ++checksumErrorCount);
                coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(_version, imageSize, 0, DownloadStatus.CHECKSUMERROR, counter), DOWNLOADINFO_KIND, // It indicate that checksum failed
                svcId);
            }
            return false;
        }
        if (trackProgress) {
            DownloadingInfo tmpInfo = coordinator.getTargetInfo(DownloadingInfo.class);
            if (null != tmpInfo && DownloadStatus.CANCELLED == tmpInfo._status) {
                // Check the target info of the DownloadingInfo class
                // to see if user sent a cancel request
                coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(_version, imageSize, 0, DownloadStatus.CANCELLED, counter), DOWNLOADINFO_KIND, svcId);
                return false;
            }
            coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(_version, imageSize, imageSize, DownloadStatus.COMPLETED, counter), DOWNLOADINFO_KIND, // When it reaches here, it means the download is successful
            svcId);
        }
    } finally {
        _out.close();
        _in.close();
    }
    return true;
}
Also used : DownloadingInfo(com.emc.storageos.coordinator.client.model.DownloadingInfo) MessageDigest(java.security.MessageDigest)

Example 4 with DownloadingInfo

use of com.emc.storageos.coordinator.client.model.DownloadingInfo in project coprhd-controller by CoprHD.

the class UpgradeManager method getLeaderImage.

private File getLeaderImage(final SoftwareVersion version, final URI leaderEndpoint) throws SysClientException {
    final File file = new File(DOWNLOAD_DIR + '/' + version + SOFTWARE_IMAGE_SUFFIX);
    final String prefix = MessageFormat.format("Step3b(): path=\"{0}\" leaderEndpoint=\"{1}\": ", file, leaderEndpoint);
    log.info(prefix);
    if (isDownloadInProgress()) {
        return null;
    }
    if (file.exists()) {
        DownloadingInfo downloadingInfo;
        try {
            downloadingInfo = coordinator.getNodeGlobalScopeInfo(DownloadingInfo.class, DOWNLOADINFO_KIND, coordinator.getMySvcId());
            // if the downloading info is present and the version is the same then update the progress
            if (downloadingInfo != null && version.toString().equals(downloadingInfo._version)) {
                coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(downloadingInfo._version, downloadingInfo._size, downloadingInfo._size, DownloadStatus.COMPLETED, new ArrayList<Integer>(Arrays.asList(0, 0))), DOWNLOADINFO_KIND, coordinator.getMySvcId());
            }
        } catch (Exception e) {
            throw APIException.internalServerErrors.getObjectFromError("Node downloading info", "coordinator", e);
        }
        // Because the file exists, we set the downloadinfo directly to COMPLETED status
        log.info(prefix + "Success!");
        return file;
    }
    log.info(prefix + "Opening remote image stream");
    try {
        String uri = SysClientFactory.URI_GET_IMAGE + "?version=" + version;
        final InputStream in = SysClientFactory.getSysClient(leaderEndpoint).get(new URI(uri), InputStream.class, MediaType.APPLICATION_OCTET_STREAM);
        log.info(prefix + "Starting background download.");
        UpgradeImageDownloader.getInstance(this).startBackgroundDownload(prefix, file, in, uri, version.toString());
    } catch (URISyntaxException e) {
        log.error("Internal error occurred while prepareing get image URI: {}", e);
    }
    return null;
}
Also used : DownloadingInfo(com.emc.storageos.coordinator.client.model.DownloadingInfo) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException)

Example 5 with DownloadingInfo

use of com.emc.storageos.coordinator.client.model.DownloadingInfo in project coprhd-controller by CoprHD.

the class UpgradeManager method getRemoteImage.

private File getRemoteImage(final SoftwareVersion version) throws RemoteRepositoryException {
    final File file = new File(DOWNLOAD_DIR + '/' + version + SOFTWARE_IMAGE_SUFFIX);
    String prefix = MessageFormat.format("Step3a: version={0} local path=\"{1}\": ", version, file);
    log.info(prefix);
    if (isDownloadInProgress()) {
        return null;
    }
    if (file.exists()) {
        DownloadingInfo downloadingInfo;
        try {
            downloadingInfo = coordinator.getTargetInfo(DownloadingInfo.class);
        } catch (Exception e) {
            throw APIException.internalServerErrors.getObjectFromError("Node downloading info", "coordinator", e);
        }
        coordinator.setNodeGlobalScopeInfo(new DownloadingInfo(downloadingInfo._version, downloadingInfo._size, downloadingInfo._size, DownloadStatus.COMPLETED, new ArrayList<>(Arrays.asList(0, 0))), DOWNLOADINFO_KIND, coordinator.getMySvcId());
        // Because the file exists, we set the downloadinfo directly to COMPLETED status
        log.info(prefix + "Success!");
        return file;
    }
    if (!tryRemoteDownload()) {
        return null;
    }
    final URL url = getRemoteImageURL(version);
    prefix = MessageFormat.format("Step3a: version={0} local path=\"{1}\" URL=\"{2}\": ", version, file, url.toString());
    log.info(prefix + "Opening remote image stream");
    final InputStream in = remoteRepository.getImageInputStream(url);
    log.info(prefix + "Starting background download.");
    UpgradeImageDownloader.getInstance(this).startBackgroundDownload(prefix, file, in, url.toString(), version.toString());
    return null;
}
Also used : DownloadingInfo(com.emc.storageos.coordinator.client.model.DownloadingInfo) InputStream(java.io.InputStream) File(java.io.File) URISyntaxException(java.net.URISyntaxException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) URL(java.net.URL)

Aggregations

DownloadingInfo (com.emc.storageos.coordinator.client.model.DownloadingInfo)6 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)4 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)3 File (java.io.File)3 Path (javax.ws.rs.Path)3 InvalidSoftwareVersionException (com.emc.storageos.coordinator.exceptions.InvalidSoftwareVersionException)2 ServiceUnavailableException (com.emc.storageos.svcs.errorhandling.resources.ServiceUnavailableException)2 CoordinatorClientException (com.emc.storageos.systemservices.exceptions.CoordinatorClientException)2 LocalRepositoryException (com.emc.storageos.systemservices.exceptions.LocalRepositoryException)2 RemoteRepositoryException (com.emc.storageos.systemservices.exceptions.RemoteRepositoryException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 RepositoryInfo (com.emc.storageos.coordinator.client.model.RepositoryInfo)1 SoftwareVersion (com.emc.storageos.coordinator.client.model.SoftwareVersion)1 ClusterInfo (com.emc.vipr.model.sys.ClusterInfo)1 DownloadProgress (com.emc.vipr.model.sys.DownloadProgress)1 NodeProgress (com.emc.vipr.model.sys.NodeProgress)1