Skip to content

Commit f2b209d

Browse files
committed
chore: Add interfaces for synchronizer/initializer.
1 parent adcaa0e commit f2b209d

File tree

4 files changed

+217
-2
lines changed

4 files changed

+217
-2
lines changed

lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/Version.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@ abstract class Version {
44
private Version() {}
55

66
// This constant is updated automatically by our Gradle script during a release, if the project version has changed
7-
// x-release-please-start-version
87
static final String SDK_VERSION = "7.10.2";
9-
// x-release-please-end
108
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.launchdarkly.sdk.server.datasources;
2+
import com.launchdarkly.sdk.internal.fdv2.sources.FDv2ChangeSet;
3+
import com.launchdarkly.sdk.server.interfaces.DataSourceStatusProvider;
4+
5+
/**
6+
* This type is currently experimental and not subject to semantic versioning.
7+
* <p>
8+
* The result type for FDv2 initializers and synchronizers. An FDv2 initializer produces a single result, while
9+
* an FDv2 synchronizer produces a stream of results.
10+
*/
11+
public class FDv2SourceResult {
12+
public enum State {
13+
/**
14+
* The data source has encountered an interruption and will attempt to reconnect.
15+
*/
16+
INTERRUPTED,
17+
/**
18+
* The data source has been shut down and will not produce any further results.
19+
*/
20+
SHUTDOWN,
21+
/**
22+
* The data source has encountered a terminal error and will not produce any further results.
23+
*/
24+
TERMINAL_ERROR,
25+
/**
26+
* The data source has been instructed to disconnect and will not produce any further results.
27+
*/
28+
GOODBYE,
29+
}
30+
31+
public enum ResultType {
32+
/**
33+
* The source has emitted a change set. This implies that the source is valid.
34+
*/
35+
CHANGE_SET,
36+
/**
37+
* The source is emitting a status which indicates a transition from being valid to being in some kind
38+
* of error state. The source will emit a CHANGE_SET if it becomes valid again.
39+
*/
40+
STATUS,
41+
}
42+
43+
/**
44+
* Represents a change in the status of the source.
45+
*/
46+
public static class Status {
47+
private final State state;
48+
private final DataSourceStatusProvider.ErrorInfo errorInfo;
49+
50+
public State getState() {
51+
return state;
52+
}
53+
54+
public DataSourceStatusProvider.ErrorInfo getErrorInfo() {
55+
return errorInfo;
56+
}
57+
58+
public Status(State state, DataSourceStatusProvider.ErrorInfo errorInfo) {
59+
this.state = state;
60+
this.errorInfo = errorInfo;
61+
}
62+
}
63+
private final FDv2ChangeSet changeSet;
64+
private final Status status;
65+
66+
private final ResultType resultType;
67+
68+
private FDv2SourceResult(FDv2ChangeSet changeSet, Status status, ResultType resultType) {
69+
this.changeSet = changeSet;
70+
this.status = status;
71+
this.resultType = resultType;
72+
}
73+
74+
public static FDv2SourceResult interrupted(DataSourceStatusProvider.ErrorInfo errorInfo) {
75+
return new FDv2SourceResult(null, new Status(State.INTERRUPTED, errorInfo), ResultType.STATUS);
76+
}
77+
78+
public static FDv2SourceResult shutdown(DataSourceStatusProvider.ErrorInfo errorInfo) {
79+
return new FDv2SourceResult(null, new Status(State.SHUTDOWN, errorInfo), ResultType.STATUS);
80+
}
81+
82+
public static FDv2SourceResult changeSet(FDv2ChangeSet changeSet) {
83+
return new FDv2SourceResult(changeSet, null, ResultType.CHANGE_SET);
84+
}
85+
86+
public ResultType getResultType() {
87+
return resultType;
88+
}
89+
90+
public Status getStatus() {
91+
return status;
92+
}
93+
94+
public FDv2ChangeSet getChangeSet() {
95+
return changeSet;
96+
}
97+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.launchdarkly.sdk.server.datasources;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
5+
/**
6+
* This type is currently experimental and not subject to semantic versioning.
7+
* <p>
8+
* Interface for an asynchronous data source initializer.
9+
* <p>
10+
* An initializer will run and produce a single result. If the initializer is successful, then it should emit a result
11+
* containing a change set. If the initializer fails, then it should emit a status result describing the error.
12+
* <p>
13+
* [START]
14+
* │
15+
* ▼
16+
* ┌─────────────┐
17+
* │ RUNNING │──┐
18+
* └─────────────┘ │
19+
* │ │ │ │ │
20+
* │ │ │ │ └──► SHUTDOWN ───► [END]
21+
* │ │ │ │
22+
* │ │ │ └─────► INTERRUPTED ───► [END]
23+
* │ │ │
24+
* │ │ └─────────► CHANGESET ───► [END]
25+
* │ │
26+
* │ └─────────────► TERMINAL_ERROR ───► [END]
27+
* │
28+
* └─────────────────► GOODBYE ───► [END]
29+
*
30+
* <code>
31+
* stateDiagram-v2
32+
* [*] --> RUNNING
33+
* RUNNING --> SHUTDOWN
34+
* RUNNING --> INTERRUPTED
35+
* RUNNING --> CHANGESET
36+
* RUNNING --> TERMINAL_ERROR
37+
* RUNNING --> GOODBYE
38+
* SHUTDOWN --> [*]
39+
* INTERRUPTED --> [*]
40+
* CHANGESET --> [*]
41+
* TERMINAL_ERROR --> [*]
42+
* GOODBYE --> [*]
43+
* </code>
44+
*/
45+
public interface Initializer {
46+
/**
47+
* Run the initializer to completion.
48+
* @return The result of the initializer.
49+
*/
50+
CompletableFuture<FDv2SourceResult> run();
51+
52+
/**
53+
* Shutdown the initializer. The initializer should emit a status event with a SHUTDOWN state as soon as possible.
54+
* If the initializer has already completed, or is in the process of completing, this method should have no effect.
55+
*/
56+
void shutdown();
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.launchdarkly.sdk.server.datasources;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
5+
/**
6+
* This type is currently experimental and not subject to semantic versioning.
7+
* <p>
8+
* Interface for an asynchronous data source synchronizer.
9+
* <p>
10+
* A synchronizer will run and produce a stream of results. When it experiences a temporary failure, it will emit a
11+
* status event indicating that it is INTERRUPTED, while it attempts to resolve its failure. When it receives data,
12+
* it should emit a result containing a change set. When the data source is shut down gracefully, it should emit a
13+
* status event indicating that it is SHUTDOWN.
14+
* <p>
15+
* [START]
16+
* │
17+
* ▼
18+
* ┌─────────────┐
19+
* ┌─►│ RUNNING │──┐
20+
* │ └─────────────┘ │
21+
* │ │ │ │ │ │
22+
* │ │ │ │ │ └──► SHUTDOWN ───► [END]
23+
* │ │ │ │ │
24+
* │ │ │ │ └──────► TERMINAL_ERROR ───► [END]
25+
* │ │ │ │
26+
* │ │ │ └──────────► GOODBYE ───► [END]
27+
* │ │ │
28+
* │ │ └──────────────► CHANGE_SET ───┐
29+
* │ │ │
30+
* │ └──────────────────► INTERRUPTED ──┤
31+
* │ │
32+
* └──────────────────────────────────────┘
33+
* <p>
34+
* <code>
35+
* stateDiagram-v2
36+
* [*] --> RUNNING
37+
* RUNNING --> SHUTDOWN
38+
* SHUTDOWN --> [*]
39+
* RUNNING --> TERMINAL_ERROR
40+
* TERMINAL_ERROR --> [*]
41+
* RUNNING --> GOODBYE
42+
* GOODBYE --> [*]
43+
* RUNNING --> CHANGE_SET
44+
* CHANGE_SET --> RUNNING
45+
* RUNNING --> INTERRUPTED
46+
* INTERRUPTED --> RUNNING
47+
* </code>
48+
*/
49+
interface Synchronizer {
50+
/**
51+
* Get the next result from the stream.
52+
* @return a future that will complete when the next result is available
53+
*/
54+
CompletableFuture<FDv2SourceResult> next();
55+
56+
/**
57+
* Shutdown the synchronizer. The synchronizer should emit a status event with a SHUTDOWN state as soon as possible
58+
* and then stop producing further results. If the synchronizer involves a resource, such as a network connection,
59+
* then those resources should be released.
60+
* If the synchronizer has already completed, or is in the process of completing, this method should have no effect.
61+
*/
62+
void shutdown();
63+
}

0 commit comments

Comments
 (0)