use of ml.comet.experiment.artifact.ArtifactException in project comet-java-sdk by comet-ml.
the class LoggedArtifactImpl method download.
@Override
public DownloadedArtifact download(@NonNull Path folder, @NonNull AssetOverwriteStrategy overwriteStrategy) throws ArtifactException {
// create downloaded artifact
DownloadedArtifactImpl artifact = new DownloadedArtifactImpl(this);
// read all assets associated with this artifact
Collection<LoggedArtifactAsset> assets = this.getAssets();
artifact.addLoggedAssets(assets);
// check if there is assets to be downloaded
int assetsToDownload = assets.stream().filter(loggedArtifactAsset -> !loggedArtifactAsset.isRemote()).mapToInt(value -> 1).sum();
if (assetsToDownload == 0) {
// show warning and return
this.logger.warn(getString(ARTIFACT_HAS_NO_ASSETS_TO_DOWNLOAD, this.getFullName()));
return artifact;
}
this.logger.info(getString(START_DOWNLOAD_ARTIFACT_ASSETS, assetsToDownload));
// create parallel execution flow with errors delaying
// allowing processing of items even if some of them failed
Observable<ArtifactAsset> observable = Observable.fromStream(assets.stream()).filter(loggedArtifactAsset -> !loggedArtifactAsset.isRemote()).flatMap(loggedArtifactAsset -> Observable.just(loggedArtifactAsset).subscribeOn(// make it parallel on IO scheduler
Schedulers.io()).map(asset -> asset.download(folder, overwriteStrategy)), true);
// subscribe and wait for processing results
CompletableFuture<Void> result = new CompletableFuture<>();
observable.doOnNext(// update artifact asset
artifact::updateAsset).ignoreElements().blockingSubscribe(() -> {
logger.info(getString(ARTIFACT_ASSETS_DOWNLOAD_COMPLETED, this.getFullName(), assetsToDownload, folder));
result.complete(null);
}, throwable -> {
logger.error(getString(FAILED_TO_DOWNLOAD_ARTIFACT_ASSETS, this.getFullName(), folder), throwable);
result.completeExceptionally(throwable);
});
// check if any exception was raised during download and raise ArtifactException
try {
result.get();
} catch (ExecutionException ex) {
throw new ArtifactException(getString(FAILED_TO_DOWNLOAD_ARTIFACT_ASSETS, this.getFullName(), folder), ex.getCause());
} catch (InterruptedException ex) {
throw new ArtifactException(getString(FAILED_TO_DOWNLOAD_ARTIFACT_ASSETS, this.getFullName(), folder), ex);
}
return artifact;
}
use of ml.comet.experiment.artifact.ArtifactException in project comet-java-sdk by comet-ml.
the class BaseExperiment method upsertArtifact.
/**
* Synchronously upsert provided Comet artifact into Comet backend.
*
* @param artifact the {@link ArtifactImpl} instance.
* @return the {@link ArtifactEntry} describing saved artifact.
* @throws ArtifactException if operation failed.
*/
ArtifactEntry upsertArtifact(@NonNull final Artifact artifact) throws ArtifactException {
try {
ArtifactImpl artifactImpl = (ArtifactImpl) artifact;
ArtifactRequest request = createArtifactUpsertRequest(artifactImpl);
ArtifactEntry response = validateAndGetExperimentKey().concatMap(experimentKey -> getRestApiClient().upsertArtifact(request, experimentKey)).blockingGet();
if (StringUtils.isBlank(response.getPreviousVersion())) {
getLogger().info(getString(ARTIFACT_VERSION_CREATED_WITHOUT_PREVIOUS, artifactImpl.getName(), response.getCurrentVersion()));
} else {
getLogger().info(getString(ARTIFACT_VERSION_CREATED_WITH_PREVIOUS, artifactImpl.getName(), response.getCurrentVersion(), response.getPreviousVersion()));
}
return response;
} catch (Throwable e) {
throw new ArtifactException(getString(FAILED_TO_UPSERT_ARTIFACT, artifact), e);
}
}
use of ml.comet.experiment.artifact.ArtifactException in project comet-java-sdk by comet-ml.
the class ArtifactSupportTest method testLogAndDownloadArtifactAsset_failForRemote.
@Test
@Timeout(value = 300, unit = SECONDS)
public void testLogAndDownloadArtifactAsset_failForRemote() throws IOException {
Path tmpDir = Files.createTempDirectory("testLogAndDownloadArtifactAsset");
try (OnlineExperimentImpl experiment = (OnlineExperimentImpl) createOnlineExperiment()) {
ArtifactImpl artifact = createArtifact();
// add remote assets
//
URI firstAssetLink = new URI("s3://bucket/folder/firstAssetFile.extension");
String firstAssetFileName = "firstAssetFileName";
artifact.addRemoteAsset(firstAssetLink, firstAssetFileName);
// log artifact and check results
//
CompletableFuture<LoggedArtifact> futureArtifact = experiment.logArtifact(artifact);
LoggedArtifact loggedArtifact = futureArtifact.get(60, SECONDS);
// get artifact details from server
//
LoggedArtifact loggedArtifactFromServer = experiment.getArtifact(loggedArtifact.getName(), loggedArtifact.getWorkspace(), loggedArtifact.getVersion());
// get logged assets and download to local dir
//
Collection<LoggedArtifactAsset> loggedAssets = loggedArtifactFromServer.getAssets();
assertEquals(1, loggedAssets.size(), "wrong number of assets returned");
LoggedArtifactAsset asset = loggedAssets.iterator().next();
assertNotNull(asset);
ArtifactException ex = assertThrows(ArtifactException.class, () -> asset.download(tmpDir));
assertEquals(getString(REMOTE_ASSET_CANNOT_BE_DOWNLOADED, asset), ex.getMessage());
} catch (Throwable t) {
fail(t);
} finally {
PathUtils.delete(tmpDir);
}
}
use of ml.comet.experiment.artifact.ArtifactException in project comet-java-sdk by comet-ml.
the class BaseExperiment method updateArtifactVersionState.
/**
* Synchronously updates the state associated with Comet artifact version.
*
* @param artifactVersionId the artifact version identifier.
* @param state the state to be associated.
* @throws ArtifactException is operation failed.
*/
void updateArtifactVersionState(@NonNull String artifactVersionId, @NonNull ArtifactVersionState state) throws ArtifactException {
try {
ArtifactRequest request = createArtifactVersionStateRequest(artifactVersionId, state);
sendSynchronously(getRestApiClient()::updateArtifactState, request);
} catch (Throwable e) {
throw new ArtifactException(getString(FAILED_TO_UPDATE_ARTIFACT_VERSION_STATE, artifactVersionId), e);
}
}
use of ml.comet.experiment.artifact.ArtifactException in project comet-java-sdk by comet-ml.
the class BaseExperiment method getArtifactVersionDetail.
/**
* Synchronously retrieves all data about a specific Artifact Version.
*
* @param options the {@link GetArtifactOptions} defining query options.
* @return the {@link LoggedArtifact} instance holding all data about a specific Artifact Version.
* @throws ArtifactNotFoundException if artifact is not found or no artifact data returned.
* @throws InvalidArtifactStateException if artifact was not closed or has empty artifact data returned.
* @throws ArtifactException if failed to get artifact due to the unexpected error.
*/
LoggedArtifact getArtifactVersionDetail(@NonNull GetArtifactOptions options) throws ArtifactNotFoundException, InvalidArtifactStateException, ArtifactException {
try {
ArtifactVersionDetail detail = validateAndGetExperimentKey().concatMap(experimentKey -> getRestApiClient().getArtifactVersionDetail(options, experimentKey)).blockingGet();
ArtifactDto artifactDto = detail.getArtifact();
if (artifactDto == null) {
throw new InvalidArtifactStateException(getString(ARTIFACT_HAS_NO_DETAILS, options));
}
return detail.copyToLoggedArtifact(new LoggedArtifactImpl(artifactDto.getArtifactName(), artifactDto.getArtifactType(), this));
} catch (CometApiException apiException) {
switch(apiException.getSdkErrorCode()) {
case noArtifactFound:
throw new ArtifactNotFoundException(getString(ARTIFACT_NOT_FOUND, options), apiException);
case artifactVersionStateNotClosed:
case artifactVersionStateNotClosedErrorOccurred:
throw new InvalidArtifactStateException(getString(ARTIFACT_NOT_READY, options), apiException);
default:
throw new ArtifactException(getString(GET_ARTIFACT_FAILED_UNEXPECTEDLY, options), apiException);
}
} catch (Throwable e) {
throw new ArtifactException(getString(GET_ARTIFACT_FAILED_UNEXPECTEDLY, options), e);
}
}
Aggregations