@@ -83,7 +83,7 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
83
83
@ Nonnull OptimizelyUserContext user ,
84
84
@ Nonnull ProjectConfig projectConfig ,
85
85
@ Nonnull List <OptimizelyDecideOption > options ,
86
- @ Nonnull UserProfile userProfile ,
86
+ @ Nullable UserProfileTracker userProfileTracker ,
87
87
@ Nullable DecisionReasons reasons ) {
88
88
if (reasons == null ) {
89
89
reasons = DefaultDecisionReasons .newInstance ();
@@ -111,8 +111,8 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
111
111
return new DecisionResponse (variation , reasons );
112
112
}
113
113
114
- if (userProfile != null ) {
115
- decisionVariation = getStoredVariation (experiment , userProfile , projectConfig );
114
+ if (userProfileTracker != null ) {
115
+ decisionVariation = getStoredVariation (experiment , userProfileTracker . userProfile , projectConfig );
116
116
reasons .merge (decisionVariation .getReasons ());
117
117
variation = decisionVariation .getResult ();
118
118
// return the stored variation if it exists
@@ -131,8 +131,9 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
131
131
variation = decisionVariation .getResult ();
132
132
133
133
if (variation != null ) {
134
- if (userProfile != null ) {
135
- updateUserProfile (experiment , variation , userProfile );
134
+ if (userProfileTracker != null ) {
135
+ updateUserProfile (experiment , variation , userProfileTracker .userProfile );
136
+ userProfileTracker .profileUpdated = true ;
136
137
} else {
137
138
logger .debug ("This decision will not be saved since the UserProfileService is null." );
138
139
}
@@ -164,16 +165,19 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
164
165
165
166
// fetch the user profile map from the user profile service
166
167
boolean ignoreUPS = options .contains (OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE );
167
- UserProfile userProfile = null ;
168
+ // UserProfile userProfile = null;
169
+ UserProfileTracker userProfileTracker = null ;
168
170
169
171
if (userProfileService != null && !ignoreUPS ) {
170
- userProfile = getUserProfile (user .getUserId (), reasons );
172
+ UserProfile userProfile = getUserProfile (user .getUserId (), reasons );
173
+ userProfileTracker = new UserProfileTracker (userProfile , false );
171
174
}
172
175
173
- DecisionResponse <Variation > response = getVariation (experiment , user , projectConfig , options , userProfile , reasons );
174
176
175
- if (userProfileService != null && !ignoreUPS ) {
176
- saveUserProfile (userProfile );
177
+ DecisionResponse <Variation > response = getVariation (experiment , user , projectConfig , options , userProfileTracker , reasons );
178
+
179
+ if (userProfileService != null && !ignoreUPS && userProfileTracker .profileUpdated ) {
180
+ saveUserProfile (userProfileTracker .userProfile );
177
181
}
178
182
return response ;
179
183
}
@@ -254,6 +258,16 @@ private UserProfile getUserProfile(String userId, DecisionReasons reasons) {
254
258
return userProfile ;
255
259
}
256
260
261
+ static class UserProfileTracker {
262
+ public UserProfile userProfile ;
263
+ public boolean profileUpdated ;
264
+
265
+ UserProfileTracker (UserProfile userProfile , boolean profileUpdated ) {
266
+ this .userProfile = userProfile ;
267
+ this .profileUpdated = profileUpdated ;
268
+ }
269
+ }
270
+
257
271
/**
258
272
* Get the variation the user is bucketed into for the FeatureFlag
259
273
*
@@ -271,10 +285,11 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
271
285
DecisionReasons upsReasons = DefaultDecisionReasons .newInstance ();
272
286
273
287
boolean ignoreUPS = options .contains (OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE );
274
- UserProfile userProfile = null ;
288
+ UserProfileTracker userProfileTracker = null ;
275
289
276
290
if (userProfileService != null && !ignoreUPS ) {
277
- userProfile = getUserProfile (user .getUserId (), upsReasons );
291
+ UserProfile userProfile = getUserProfile (user .getUserId (), upsReasons );
292
+ userProfileTracker = new UserProfileTracker (userProfile , false );
278
293
}
279
294
280
295
List <DecisionResponse <FeatureDecision >> decisions = new ArrayList <>();
@@ -283,7 +298,7 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
283
298
DecisionReasons reasons = DefaultDecisionReasons .newInstance ();
284
299
reasons .merge (upsReasons );
285
300
286
- DecisionResponse <FeatureDecision > decisionVariationResponse = getVariationFromExperiment (projectConfig , featureFlag , user , options , userProfile );
301
+ DecisionResponse <FeatureDecision > decisionVariationResponse = getVariationFromExperiment (projectConfig , featureFlag , user , options , userProfileTracker );
287
302
reasons .merge (decisionVariationResponse .getReasons ());
288
303
289
304
FeatureDecision decision = decisionVariationResponse .getResult ();
@@ -309,8 +324,8 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
309
324
decisions .add (new DecisionResponse (decision , reasons ));
310
325
}
311
326
312
- if (userProfileService != null && !ignoreUPS ) {
313
- saveUserProfile (userProfile );
327
+ if (userProfileService != null && !ignoreUPS && userProfileTracker . profileUpdated ) {
328
+ saveUserProfile (userProfileTracker . userProfile );
314
329
}
315
330
316
331
return decisions ;
@@ -360,13 +375,14 @@ DecisionResponse<FeatureDecision> getVariationFromExperiment(@Nonnull ProjectCon
360
375
@ Nonnull FeatureFlag featureFlag ,
361
376
@ Nonnull OptimizelyUserContext user ,
362
377
@ Nonnull List <OptimizelyDecideOption > options ,
363
- @ Nullable UserProfile userProfile ) {
378
+ @ Nullable UserProfileTracker userProfileTracker ) {
364
379
DecisionReasons reasons = DefaultDecisionReasons .newInstance ();
365
380
if (!featureFlag .getExperimentIds ().isEmpty ()) {
366
381
for (String experimentId : featureFlag .getExperimentIds ()) {
367
382
Experiment experiment = projectConfig .getExperimentIdMapping ().get (experimentId );
368
383
369
- DecisionResponse <Variation > decisionVariation = getVariationFromExperimentRule (projectConfig , featureFlag .getKey (), experiment , user , options , userProfile );
384
+ DecisionResponse <Variation > decisionVariation =
385
+ getVariationFromExperimentRule (projectConfig , featureFlag .getKey (), experiment , user , options , userProfileTracker );
370
386
reasons .merge (decisionVariation .getReasons ());
371
387
Variation variation = decisionVariation .getResult ();
372
388
@@ -777,7 +793,7 @@ public DecisionResponse<Variation> getVariationFromExperimentRule(@Nonnull Proje
777
793
@ Nonnull Experiment rule ,
778
794
@ Nonnull OptimizelyUserContext user ,
779
795
@ Nonnull List <OptimizelyDecideOption > options ,
780
- @ Nullable UserProfile userProfile ) {
796
+ @ Nullable UserProfileTracker userProfileTracker ) {
781
797
DecisionReasons reasons = DefaultDecisionReasons .newInstance ();
782
798
783
799
String ruleKey = rule != null ? rule .getKey () : null ;
@@ -792,7 +808,7 @@ public DecisionResponse<Variation> getVariationFromExperimentRule(@Nonnull Proje
792
808
return new DecisionResponse (variation , reasons );
793
809
}
794
810
//regular decision
795
- DecisionResponse <Variation > decisionResponse = getVariation (rule , user , projectConfig , options , userProfile , null );
811
+ DecisionResponse <Variation > decisionResponse = getVariation (rule , user , projectConfig , options , userProfileTracker , null );
796
812
reasons .merge (decisionResponse .getReasons ());
797
813
798
814
variation = decisionResponse .getResult ();
0 commit comments