2
2
3
3
import cloud .eppo .api .Configuration ;
4
4
import java .util .concurrent .CompletableFuture ;
5
- import java .util .concurrent .CompletionException ;
6
5
import java .util .concurrent .ExecutionException ;
7
6
import org .jetbrains .annotations .NotNull ;
8
7
import org .slf4j .Logger ;
@@ -39,12 +38,8 @@ public void setInitialConfiguration(@NotNull Configuration configuration) {
39
38
throw new IllegalStateException ("Initial configuration has already been set" );
40
39
}
41
40
42
- try {
43
- configurationStore .saveConfiguration (configuration ).join ();
44
- initialConfigSet = true ;
45
- } catch (CompletionException e ) {
46
- log .error ("Error setting initial configuration" , e );
47
- }
41
+ initialConfigSet =
42
+ configurationStore .saveConfiguration (configuration ).thenApply (v -> true ).join ();
48
43
}
49
44
50
45
// Asynchronously sets the initial configuration.
@@ -54,23 +49,32 @@ public CompletableFuture<Void> setInitialConfiguration(
54
49
throw new IllegalStateException ("Configuration future has already been set" );
55
50
}
56
51
this .configurationFuture =
57
- configurationFuture .thenAccept (
58
- (config ) -> {
59
- synchronized (configurationStore ) {
60
- if (config == null ) {
61
- log .debug ("Initial configuration future returned null" );
62
- } else if (remoteFetchFuture != null
63
- && remoteFetchFuture .isDone ()
64
- && !remoteFetchFuture .isCompletedExceptionally ()) {
65
- // Don't clobber a successful fetch.
66
- log .debug ("Fetch successfully beat the initial config; not clobbering" );
67
- } else {
68
- log .debug ("saving initial configuration" );
69
- configurationStore .saveConfiguration (config );
70
- initialConfigSet = true ;
71
- }
72
- }
73
- });
52
+ configurationFuture
53
+ .thenAccept (
54
+ (config ) -> {
55
+ synchronized (configurationStore ) {
56
+ if (config == null || config .isEmpty ()) {
57
+ log .debug ("Initial configuration future returned empty/null" );
58
+ } else if (remoteFetchFuture != null
59
+ && remoteFetchFuture .isDone ()
60
+ && !remoteFetchFuture .isCompletedExceptionally ()) {
61
+ // Don't clobber a successful fetch.
62
+ log .debug ("Fetch has completed; ignoring initial config load." );
63
+ } else {
64
+ log .debug ("saving initial configuration" );
65
+ initialConfigSet =
66
+ configurationStore
67
+ .saveConfiguration (config )
68
+ .thenApply ((s ) -> true )
69
+ .join ();
70
+ }
71
+ }
72
+ })
73
+ .exceptionally (
74
+ (e ) -> {
75
+ log .error ("Error setting initial config" , e );
76
+ return null ;
77
+ });
74
78
return this .configurationFuture ;
75
79
}
76
80
@@ -91,7 +95,7 @@ void fetchAndSaveFromRemote() {
91
95
configBuilder .banditParameters (banditParametersJsonBytes );
92
96
}
93
97
94
- configurationStore .saveConfiguration (configBuilder .build ());
98
+ configurationStore .saveConfiguration (configBuilder .build ()). join () ;
95
99
}
96
100
97
101
/** Loads configuration asynchronously from the API server, off-thread. */
@@ -128,15 +132,9 @@ CompletableFuture<Void> fetchAndSaveFromRemoteAsync() {
128
132
configBuilder .banditParameters (banditParametersJsonBytes );
129
133
}
130
134
}
131
- return configBuilder .build ();
132
- }
133
- })
134
- .thenApply (
135
- configuration -> {
136
- synchronized (configurationStore ) {
137
- configurationStore .saveConfiguration (configuration );
135
+
136
+ return configurationStore .saveConfiguration (configBuilder .build ()).join ();
138
137
}
139
- return null ;
140
138
});
141
139
return remoteFetchFuture ;
142
140
}
0 commit comments