@@ -83,7 +83,7 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
8383 @ Nonnull OptimizelyUserContext user ,
8484 @ Nonnull ProjectConfig projectConfig ,
8585 @ Nonnull List <OptimizelyDecideOption > options ,
86- @ Nonnull UserProfile userProfile ,
86+ @ Nullable UserProfileTracker userProfileTracker ,
8787 @ Nullable DecisionReasons reasons ) {
8888 if (reasons == null ) {
8989 reasons = DefaultDecisionReasons .newInstance ();
@@ -111,8 +111,8 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
111111 return new DecisionResponse (variation , reasons );
112112 }
113113
114- if (userProfile != null ) {
115- decisionVariation = getStoredVariation (experiment , userProfile , projectConfig );
114+ if (userProfileTracker != null ) {
115+ decisionVariation = getStoredVariation (experiment , userProfileTracker . userProfile , projectConfig );
116116 reasons .merge (decisionVariation .getReasons ());
117117 variation = decisionVariation .getResult ();
118118 // return the stored variation if it exists
@@ -131,8 +131,9 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
131131 variation = decisionVariation .getResult ();
132132
133133 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 ;
136137 } else {
137138 logger .debug ("This decision will not be saved since the UserProfileService is null." );
138139 }
@@ -164,16 +165,19 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
164165
165166 // fetch the user profile map from the user profile service
166167 boolean ignoreUPS = options .contains (OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE );
167- UserProfile userProfile = null ;
168+ // UserProfile userProfile = null;
169+ UserProfileTracker userProfileTracker = null ;
168170
169171 if (userProfileService != null && !ignoreUPS ) {
170- userProfile = getUserProfile (user .getUserId (), reasons );
172+ UserProfile userProfile = getUserProfile (user .getUserId (), reasons );
173+ userProfileTracker = new UserProfileTracker (userProfile , false );
171174 }
172175
173- DecisionResponse <Variation > response = getVariation (experiment , user , projectConfig , options , userProfile , reasons );
174176
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 );
177181 }
178182 return response ;
179183 }
@@ -254,6 +258,16 @@ private UserProfile getUserProfile(String userId, DecisionReasons reasons) {
254258 return userProfile ;
255259 }
256260
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+
257271 /**
258272 * Get the variation the user is bucketed into for the FeatureFlag
259273 *
@@ -271,10 +285,11 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
271285 DecisionReasons upsReasons = DefaultDecisionReasons .newInstance ();
272286
273287 boolean ignoreUPS = options .contains (OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE );
274- UserProfile userProfile = null ;
288+ UserProfileTracker userProfileTracker = null ;
275289
276290 if (userProfileService != null && !ignoreUPS ) {
277- userProfile = getUserProfile (user .getUserId (), upsReasons );
291+ UserProfile userProfile = getUserProfile (user .getUserId (), upsReasons );
292+ userProfileTracker = new UserProfileTracker (userProfile , false );
278293 }
279294
280295 List <DecisionResponse <FeatureDecision >> decisions = new ArrayList <>();
@@ -283,7 +298,7 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
283298 DecisionReasons reasons = DefaultDecisionReasons .newInstance ();
284299 reasons .merge (upsReasons );
285300
286- DecisionResponse <FeatureDecision > decisionVariationResponse = getVariationFromExperiment (projectConfig , featureFlag , user , options , userProfile );
301+ DecisionResponse <FeatureDecision > decisionVariationResponse = getVariationFromExperiment (projectConfig , featureFlag , user , options , userProfileTracker );
287302 reasons .merge (decisionVariationResponse .getReasons ());
288303
289304 FeatureDecision decision = decisionVariationResponse .getResult ();
@@ -309,8 +324,8 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
309324 decisions .add (new DecisionResponse (decision , reasons ));
310325 }
311326
312- if (userProfileService != null && !ignoreUPS ) {
313- saveUserProfile (userProfile );
327+ if (userProfileService != null && !ignoreUPS && userProfileTracker . profileUpdated ) {
328+ saveUserProfile (userProfileTracker . userProfile );
314329 }
315330
316331 return decisions ;
@@ -360,13 +375,14 @@ DecisionResponse<FeatureDecision> getVariationFromExperiment(@Nonnull ProjectCon
360375 @ Nonnull FeatureFlag featureFlag ,
361376 @ Nonnull OptimizelyUserContext user ,
362377 @ Nonnull List <OptimizelyDecideOption > options ,
363- @ Nullable UserProfile userProfile ) {
378+ @ Nullable UserProfileTracker userProfileTracker ) {
364379 DecisionReasons reasons = DefaultDecisionReasons .newInstance ();
365380 if (!featureFlag .getExperimentIds ().isEmpty ()) {
366381 for (String experimentId : featureFlag .getExperimentIds ()) {
367382 Experiment experiment = projectConfig .getExperimentIdMapping ().get (experimentId );
368383
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 );
370386 reasons .merge (decisionVariation .getReasons ());
371387 Variation variation = decisionVariation .getResult ();
372388
@@ -777,7 +793,7 @@ public DecisionResponse<Variation> getVariationFromExperimentRule(@Nonnull Proje
777793 @ Nonnull Experiment rule ,
778794 @ Nonnull OptimizelyUserContext user ,
779795 @ Nonnull List <OptimizelyDecideOption > options ,
780- @ Nullable UserProfile userProfile ) {
796+ @ Nullable UserProfileTracker userProfileTracker ) {
781797 DecisionReasons reasons = DefaultDecisionReasons .newInstance ();
782798
783799 String ruleKey = rule != null ? rule .getKey () : null ;
@@ -792,7 +808,7 @@ public DecisionResponse<Variation> getVariationFromExperimentRule(@Nonnull Proje
792808 return new DecisionResponse (variation , reasons );
793809 }
794810 //regular decision
795- DecisionResponse <Variation > decisionResponse = getVariation (rule , user , projectConfig , options , userProfile , null );
811+ DecisionResponse <Variation > decisionResponse = getVariation (rule , user , projectConfig , options , userProfileTracker , null );
796812 reasons .merge (decisionResponse .getReasons ());
797813
798814 variation = decisionResponse .getResult ();
0 commit comments