Skip to content

Commit 3903def

Browse files
committed
[CM-1580]: Implemented asynchronous curve logging by OnlineExperiment. Implemented related test cases.
1 parent bf476f8 commit 3903def

File tree

7 files changed

+118
-3
lines changed

7 files changed

+118
-3
lines changed

comet-java-client/src/main/java/ml/comet/experiment/impl/BaseExperimentAsync.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ void logAssetFileAsync(@NonNull File file, @NonNull String fileName, boolean ove
575575
* either successful or failure.
576576
*/
577577
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
578-
private void logAssetAsync(@NonNull final Asset asset, @NonNull ExperimentContext context,
578+
void logAssetAsync(@NonNull final Asset asset, @NonNull ExperimentContext context,
579579
@NonNull Optional<Action> onComplete) {
580580
this.logAssetAsync(getRestApiClient()::logAsset, asset, context, onComplete);
581581
}

comet-java-client/src/main/java/ml/comet/experiment/impl/OnlineExperimentImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import ml.comet.experiment.artifact.LoggedArtifact;
1010
import ml.comet.experiment.context.ExperimentContext;
1111
import ml.comet.experiment.exception.CometApiException;
12+
import ml.comet.experiment.impl.asset.AssetImpl;
1213
import ml.comet.experiment.impl.asset.AssetType;
1314
import ml.comet.experiment.impl.log.StdOutLogger;
1415
import ml.comet.experiment.impl.rest.ExperimentStatusResponse;
16+
import ml.comet.experiment.model.Curve;
1517
import ml.comet.experiment.model.GitMetaData;
1618
import org.awaitility.Awaitility;
1719
import org.slf4j.Logger;
@@ -47,12 +49,14 @@
4749
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_ASSET;
4850
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_ASSET_FOLDER;
4951
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_CODE_ASSET;
52+
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_CURVE_ASSET;
5053
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_MODEL_ASSET;
5154
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_MODEL_FOLDER;
5255
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_REMOTE_ASSET;
5356
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_LOG_TEXT_ASSET;
5457
import static ml.comet.experiment.impl.resources.LogMessages.TIMEOUT_FOR_EXPERIMENT_INVENTORY_CLEANUP;
5558
import static ml.comet.experiment.impl.resources.LogMessages.getString;
59+
import static ml.comet.experiment.impl.utils.AssetUtils.createAssetFromCurve;
5660

5761
/**
5862
* The implementation of the {@link OnlineExperiment} to work with Comet API asynchronously.
@@ -485,6 +489,24 @@ public void logText(String text) {
485489
this.logText(text, ExperimentContext.empty());
486490
}
487491

492+
@Override
493+
public void logCurve(@NonNull Curve curve, boolean overwrite, @NonNull ExperimentContext context) {
494+
AssetImpl asset = createAssetFromCurve(curve, overwrite);
495+
this.executeLogAction(() -> this.logAssetAsync(
496+
asset, context, this.getLogAssetOnCompleteAction()),
497+
this.assetsInProgress, getString(FAILED_TO_LOG_CURVE_ASSET));
498+
}
499+
500+
@Override
501+
public void logCurve(@NonNull Curve curve, boolean overwrite) {
502+
this.logCurve(curve, overwrite, ExperimentContext.empty());
503+
}
504+
505+
@Override
506+
public void logCurve(@NonNull Curve curve) {
507+
this.logCurve(curve, false);
508+
}
509+
488510
@Override
489511
public CompletableFuture<LoggedArtifact> logArtifact(Artifact artifact) throws ArtifactException {
490512
this.checkExperimentActiveState();

comet-java-client/src/main/java/ml/comet/experiment/impl/resources/LogMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class LogMessages {
5555
public static final String FAILED_TO_LOG_REMOTE_ASSET = "FAILED_TO_LOG_REMOTE_ASSET";
5656
public static final String FAILED_TO_LOG_CODE_ASSET = "FAILED_TO_LOG_CODE_ASSET";
5757
public static final String FAILED_TO_LOG_TEXT_ASSET = "FAILED_TO_LOG_TEXT_ASSET";
58+
public static final String FAILED_TO_LOG_CURVE_ASSET = "FAILED_TO_LOG_CURVE_ASSET";
5859
public static final String FAILED_TO_LOG_SOME_ASSET_FROM_FOLDER = "FAILED_TO_LOG_SOME_ASSET_FROM_FOLDER";
5960
public static final String ARTIFACT_NOT_FOUND = "ARTIFACT_NOT_FOUND";
6061
public static final String ARTIFACT_NOT_READY = "ARTIFACT_NOT_READY";

comet-java-client/src/main/resources/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ FAILED_TO_LOG_ASSET=Failed to log asset: %s
4444
FAILED_TO_LOG_REMOTE_ASSET=Failed to log remote asset: %s
4545
FAILED_TO_LOG_CODE_ASSET=Failed to log code asset: %s
4646
FAILED_TO_LOG_TEXT_ASSET=Failed to log text asset.
47+
FAILED_TO_LOG_CURVE_ASSET=Failed to log curve asset.
4748
ARTIFACT_NOT_FOUND=Artifact not found with options: %s
4849
ARTIFACT_NOT_READY=Artifact (%s) is not ready and cannot be accessed. You need to finalize it.
4950
ARTIFACT_HAS_NO_DETAILS=Artifact (%s) returned by server has no data.

comet-java-client/src/test/java/ml/comet/experiment/impl/ApiExperimentTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public void testLogTextShort() throws Exception {
111111
@Test
112112
public void testLogCurve() throws Exception {
113113
try (ApiExperiment apiExperiment = createApiExperiment()) {
114+
// check that no curve was logged
115+
assertTrue(apiExperiment.getAllAssetList().isEmpty());
116+
114117
String fileName = "someCurve";
115118
Curve curve = createCurve(fileName, 10);
116119
apiExperiment.logCurve(curve, true, SOME_FULL_CONTEXT);
@@ -149,8 +152,7 @@ public void testLogCurveOverwrite() throws Exception {
149152
}
150153

151154
// create new experiment and overwrite curve
152-
try (ApiExperiment apiExperiment = ExperimentBuilder.ApiExperiment()
153-
.withExistingExperimentKey(experimentKey).withApiKey(API_KEY).build()) {
155+
try (ApiExperiment apiExperiment = createApiExperiment(experimentKey)) {
154156
// overwrite created curve with bigger ones
155157
//
156158
Curve curve = createCurve(fileName, pointsCount * 2);

comet-java-client/src/test/java/ml/comet/experiment/impl/ExperimentTestFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,25 @@ static OnlineExperiment createOnlineExperiment() {
3030
.build();
3131
}
3232

33+
static OnlineExperiment createOnlineExperiment(String experimentKey) {
34+
return ExperimentBuilder.OnlineExperiment()
35+
.withExistingExperimentKey(experimentKey)
36+
.withApiKey(API_KEY)
37+
.build();
38+
}
39+
3340
static ApiExperiment createApiExperiment() {
3441
return ExperimentBuilder.ApiExperiment()
3542
.withProjectName(PROJECT_NAME)
3643
.withWorkspace(WORKSPACE_NAME)
3744
.withApiKey(API_KEY)
3845
.build();
3946
}
47+
48+
static ApiExperiment createApiExperiment(String experimentKey) {
49+
return ExperimentBuilder.ApiExperiment()
50+
.withExistingExperimentKey(experimentKey)
51+
.withApiKey(API_KEY)
52+
.build();
53+
}
4054
}

comet-java-client/src/test/java/ml/comet/experiment/impl/OnlineExperimentTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ml.comet.experiment.context.ExperimentContext;
88
import ml.comet.experiment.exception.CometGeneralException;
99
import ml.comet.experiment.impl.asset.LoggedExperimentAssetImpl;
10+
import ml.comet.experiment.model.Curve;
1011
import ml.comet.experiment.model.ExperimentMetadata;
1112
import ml.comet.experiment.model.GitMetaData;
1213
import ml.comet.experiment.model.Value;
@@ -30,6 +31,8 @@
3031
import static ml.comet.experiment.impl.TestUtils.SOME_FULL_CONTEXT;
3132
import static ml.comet.experiment.impl.TestUtils.SOME_METADATA;
3233
import static ml.comet.experiment.impl.TestUtils.awaitForCondition;
34+
import static ml.comet.experiment.impl.TestUtils.createCurve;
35+
import static ml.comet.experiment.impl.asset.AssetType.CURVE;
3336
import static ml.comet.experiment.impl.asset.AssetType.SOURCE_CODE;
3437
import static ml.comet.experiment.impl.asset.AssetType.TEXT_SAMPLE;
3538
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_REGISTER_EXPERIMENT;
@@ -594,6 +597,78 @@ public void testLogAndGetText() throws Exception {
594597
}
595598
}
596599

600+
@Test
601+
public void testLogAndGetCurve() throws Exception {
602+
try (OnlineExperimentImpl experiment = (OnlineExperimentImpl) createOnlineExperiment()) {
603+
// check that no curve was logged
604+
//
605+
assertTrue(experiment.getAllAssetList().isEmpty());
606+
607+
String fileName = "someCurve";
608+
int pointsCount = 10;
609+
Curve curve = createCurve(fileName, pointsCount);
610+
611+
experiment.logCurve(curve, false, SOME_FULL_CONTEXT);
612+
613+
awaitForCondition(() -> !experiment.getAssetList(CURVE.type()).isEmpty(),
614+
"Curve was not logged");
615+
616+
// check that CURVE asset was saved as expected
617+
List<LoggedExperimentAsset> assets = experiment.getAssetList(CURVE.type());
618+
assertEquals(1, assets.size(), "wrong number of assets returned");
619+
620+
LoggedExperimentAsset asset = assets.get(0);
621+
assertEquals(CURVE.type(), asset.getType(), "wrong type");
622+
assertEquals(0, asset.getMetadata().size(), "no metadata expected");
623+
ExperimentContext assetContext = ((LoggedExperimentAssetImpl) asset).getContext();
624+
assertEquals(SOME_FULL_CONTEXT.getStep(), assetContext.getStep(), "wrong context step");
625+
assertEquals(SOME_FULL_CONTEXT.getContext(), assetContext.getContext(), "wrong context ID");
626+
}
627+
}
628+
629+
@Test
630+
public void testLogAndGetCurveOverwrite() throws Exception {
631+
String experimentKey;
632+
String fileName = "someCurve";
633+
int pointsCount = 10;
634+
long size;
635+
try (OnlineExperimentImpl experiment = (OnlineExperimentImpl) createOnlineExperiment()) {
636+
Curve curve = createCurve(fileName, pointsCount);
637+
638+
experiment.logCurve(curve, false, SOME_FULL_CONTEXT);
639+
640+
awaitForCondition(() -> !experiment.getAssetList(CURVE.type()).isEmpty(),
641+
"Curve was not logged");
642+
643+
// check that CURVE asset was saved as expected
644+
List<LoggedExperimentAsset> assets = experiment.getAssetList(CURVE.type());
645+
assertEquals(1, assets.size(), "wrong number of assets returned");
646+
647+
size = assets.get(0).getSize().orElse((long) -1);
648+
assertTrue(size > 0, "wrong asset size");
649+
650+
experimentKey = experiment.getExperimentKey();
651+
}
652+
653+
// create new experiment and overwrite curve
654+
try (OnlineExperimentImpl experiment = (OnlineExperimentImpl) createOnlineExperiment(experimentKey)) {
655+
// overwrite created curve with bigger ones
656+
//
657+
Curve curve = createCurve(fileName, pointsCount * 2);
658+
659+
experiment.logCurve(curve, true, SOME_FULL_CONTEXT);
660+
661+
awaitForCondition(() -> !experiment.getAssetList(CURVE.type()).isEmpty(),
662+
"Curve was not logged");
663+
664+
List<LoggedExperimentAsset> assets = experiment.getAssetList(CURVE.type());
665+
assertEquals(1, assets.size(), "wrong number of assets returned");
666+
667+
long newSize = assets.get(0).getSize().orElse((long) -1);
668+
assertTrue(newSize > size);
669+
}
670+
}
671+
597672
@Test
598673
@Timeout(60)
599674
public void testCreateExperiment_wrongApiKey() {

0 commit comments

Comments
 (0)