Skip to content

Commit

Permalink
refac: modify configSyncDetails (#396)
Browse files Browse the repository at this point in the history
the idea here is it's a bit odd for people who use this api like this:
`configSyncDetails.getDetails().isConfigSpecReady();`

so rather that wrap previous `initDetails`, create its own
`configSyncDetails`
  • Loading branch information
weihao-statsig authored Oct 21, 2024
1 parent f576480 commit f3fb46a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/statsig/sdk/Evaluator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ internal class Evaluator(
specStore.shutdown()
}

suspend fun syncConfigSpecs(): FailureDetails? {
suspend fun syncConfigSpecs(): ConfigSyncDetails {
if (!isInitialized) {
return FailureDetails(FailureReason.EMPTY_SPEC)
return ConfigSyncDetails(0L, false, FailureDetails(FailureReason.EMPTY_SPEC), 0L)
}
return specStore.syncConfigSpecs()
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/com/statsig/sdk/InitializationDetails.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ data class InitializationDetails(
)

data class ConfigSyncDetails(
@SerializedName("details")
var details: InitializationDetails
@SerializedName("duration")
var duration: Long,
@SerializedName("configSpecReady")
var configSpecReady: Boolean,
@SerializedName("failureDetails")
var failureDetails: FailureDetails? = null,
@SerializedName("lcut")
var lcut: Long? = null,
)

data class FailureDetails(
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/com/statsig/sdk/SpecStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ internal class SpecStore(
this.specUpdater.shutdown()
}

suspend fun syncConfigSpecs(): FailureDetails? {
return initializeSpecs()
suspend fun syncConfigSpecs(): ConfigSyncDetails {
val startTime = System.currentTimeMillis()
val failureDetails = initializeSpecs()
val endTime = System.currentTimeMillis()
return ConfigSyncDetails(
duration = endTime - startTime,
configSpecReady = failureDetails == null,
failureDetails,
specUpdater.lastUpdateTime
)
}

fun setDownloadedConfigs(downloadedConfig: APIDownloadedConfigs, isFromBootstrap: Boolean = false): Boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/statsig/sdk/Statsig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ class Statsig {
@JvmStatic
fun syncConfigSpecs(): CompletableFuture<ConfigSyncDetails> {
if (!checkInitialized()) {
return CompletableFuture.completedFuture(ConfigSyncDetails(InitializationDetails(0, false, false)))
return CompletableFuture.completedFuture(ConfigSyncDetails(0, false, null))
}
return statsigServer.syncConfigSpecs()
}
Expand Down
21 changes: 6 additions & 15 deletions src/main/kotlin/com/statsig/sdk/StatsigServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -586,25 +586,16 @@ private class StatsigServerImpl() :
}

override fun syncConfigSpecs(): CompletableFuture<ConfigSyncDetails> {
val startTime = System.currentTimeMillis()
return statsigScope.future {
errorBoundary.capture("syncConfigSpecs", {
val failureDetails = evaluator.syncConfigSpecs()
ConfigSyncDetails(
InitializationDetails(
System.currentTimeMillis() - setupStartTime,
isSDKReady = isSDKInitialized(),
configSpecReady = failureDetails == null,
failureDetails,
)
)
evaluator.syncConfigSpecs()
}, {
ConfigSyncDetails(
InitializationDetails(
System.currentTimeMillis() - setupStartTime,
isSDKReady = isSDKInitialized(),
configSpecReady = false,
FailureDetails(FailureReason.INTERNAL_ERROR),
)
duration = System.currentTimeMillis() - startTime,
configSpecReady = false,
FailureDetails(FailureReason.INTERNAL_ERROR),
lcut = 0L,
)
})
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/statsig/sdk/StatsigReinitializeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public MockResponse dispatch(@NotNull RecordedRequest request) {
}
});

server.start(1080);
server.start(9874);

options = new StatsigOptions();
options.setApi(server.url("/v1").toString());
Expand Down Expand Up @@ -80,8 +80,8 @@ public void testReinitialize() throws Exception {
CompletableFuture<ConfigSyncDetails> details2Future = Statsig.syncConfigSpecs();
ConfigSyncDetails details2 = details2Future.get();
Assert.assertNotNull(details2);
Assert.assertTrue(details2.getDetails().isSDKReady());
Assert.assertFalse(details2.getDetails().getConfigSpecReady()); // Second try should also fail
Assert.assertFalse(details2.getConfigSpecReady()); // Second try should also fail
Assert.assertNotNull(details2.getLcut());
Assert.assertFalse(Statsig.checkGateSync(user, "always_on_gate"));
Assert.assertFalse(Statsig.checkGateSync(user, "on_for_statsig_email"));
Layer layer2 = Statsig.getLayerSync(user, "c_layer_with_holdout");
Expand All @@ -90,9 +90,9 @@ public void testReinitialize() throws Exception {
CompletableFuture<ConfigSyncDetails> details3Future = Statsig.syncConfigSpecs();
ConfigSyncDetails details3 = details3Future.get();
Assert.assertNotNull(details3);
Assert.assertTrue(details3.getDetails().isSDKReady());
Assert.assertTrue(details3.getDetails().getConfigSpecReady()); // Third try should succeed with 200 response
Assert.assertNull(details3.getDetails().getFailureDetails()); // No failure details expected for successful init
Assert.assertTrue(details3.getConfigSpecReady()); // Third try should succeed with 200 response
Assert.assertNull(details3.getFailureDetails()); // No failure details expected for successful init
Assert.assertNotNull(details3.getLcut());
Assert.assertTrue(Statsig.checkGateSync(user, "always_on_gate"));
Assert.assertTrue(Statsig.checkGateSync(user, "on_for_statsig_email"));
Layer layer3 = Statsig.getLayerSync(user, "c_layer_with_holdout");
Expand Down

0 comments on commit f3fb46a

Please sign in to comment.