Skip to content

[FSSDK-11169] Implement Decision Service methods to handle CMAB #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions pkg/decision/composite_experiment_service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* Copyright 2019-2025, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -40,11 +40,19 @@ func WithOverrideStore(overrideStore ExperimentOverrideStore) CESOptionFunc {
}
}

// WithCmabService adds a CMAB service
func WithCmabService(cmabService CmabService) CESOptionFunc {
return func(f *CompositeExperimentService) {
f.cmabService = cmabService
}
}

// CompositeExperimentService bridges together the various experiment decision services that ship by default with the SDK
type CompositeExperimentService struct {
experimentServices []ExperimentService
overrideStore ExperimentOverrideStore
userProfileService UserProfileService
cmabService CmabService
logger logging.OptimizelyLogProducer
}

Expand All @@ -53,7 +61,8 @@ func NewCompositeExperimentService(sdkKey string, options ...CESOptionFunc) *Com
// These decision services are applied in order:
// 1. Overrides (if supplied)
// 2. Whitelist
// 3. Bucketing (with User profile integration if supplied)
// 3. CMAB (if experiment is a CMAB experiment)
// 4. Bucketing (with User profile integration if supplied)
compositeExperimentService := &CompositeExperimentService{logger: logging.GetLogger(sdkKey, "CompositeExperimentService")}
for _, opt := range options {
opt(compositeExperimentService)
Expand All @@ -68,6 +77,12 @@ func NewCompositeExperimentService(sdkKey string, options ...CESOptionFunc) *Com
experimentServices = append([]ExperimentService{overrideService}, experimentServices...)
}

// Add CMAB service if available
if compositeExperimentService.cmabService != nil {
cmabService := NewExperimentCmabService(compositeExperimentService.cmabService, logging.GetLogger(sdkKey, "ExperimentCmabService"))
experimentServices = append(experimentServices, cmabService)
}

experimentBucketerService := NewExperimentBucketerService(logging.GetLogger(sdkKey, "ExperimentBucketerService"))
if compositeExperimentService.userProfileService != nil {
persistingExperimentService := NewPersistingExperimentService(compositeExperimentService.userProfileService, experimentBucketerService, logging.GetLogger(sdkKey, "PersistingExperimentService"))
Expand Down
4 changes: 3 additions & 1 deletion pkg/decision/entities.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2019-2021, Optimizely, Inc. and contributors *
* Copyright 2019-2025, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -55,6 +55,8 @@ const (
Rollout Source = "rollout"
// FeatureTest - the decision came from a feature test
FeatureTest Source = "feature-test"
// Cmab - the decision came from a CMAB service
Cmab Source = "cmab"
)

// Decision contains base information about a decision
Expand Down
97 changes: 97 additions & 0 deletions pkg/decision/experiment_cmab_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/****************************************************************************
* Copyright 2025, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

// Package decision //
package decision

import (
"errors"
"fmt"

"github.com/optimizely/go-sdk/v2/pkg/decide"
"github.com/optimizely/go-sdk/v2/pkg/decision/reasons"
"github.com/optimizely/go-sdk/v2/pkg/entities"
"github.com/optimizely/go-sdk/v2/pkg/logging"
)

// ExperimentCmabService makes decisions for CMAB experiments
type ExperimentCmabService struct {
cmabService CmabService
logger logging.OptimizelyLogProducer
}

// NewExperimentCmabService creates a new instance of ExperimentCmabService
func NewExperimentCmabService(cmabService CmabService, logger logging.OptimizelyLogProducer) *ExperimentCmabService {
return &ExperimentCmabService{
cmabService: cmabService,
logger: logger,
}
}

// GetDecision returns a decision for the given experiment and user context
func (s *ExperimentCmabService) GetDecision(decisionContext ExperimentDecisionContext, userContext entities.UserContext, options *decide.Options) (decision ExperimentDecision, decisionReasons decide.DecisionReasons, err error) {
decisionReasons = decide.NewDecisionReasons(options)
experiment := decisionContext.Experiment
projectConfig := decisionContext.ProjectConfig

// Check if experiment is nil or not a CMAB experiment
if experiment == nil || !isCmab(*experiment) {
message := "Not a CMAB experiment, skipping CMAB decision service"
decisionReasons.AddInfo(message)
return decision, decisionReasons, nil
}

// Check if CMAB service is available
if s.cmabService == nil {
message := "CMAB service is not available"
decisionReasons.AddInfo(message)
return decision, decisionReasons, errors.New(message)
}

// Get CMAB decision
cmabDecision, err := s.cmabService.GetDecision(projectConfig, userContext, experiment.ID, options)
if err != nil {
message := fmt.Sprintf("Failed to get CMAB decision: %v", err)
decisionReasons.AddInfo(message)
return decision, decisionReasons, fmt.Errorf("failed to get CMAB decision: %w", err)
}

// Find variation by ID
for _, variation := range experiment.Variations {
if variation.ID != cmabDecision.VariationID {
continue
}

// Create a copy of the variation to avoid memory aliasing
variationCopy := variation
decision.Variation = &variationCopy
decision.Reason = reasons.CmabVariationAssigned
message := fmt.Sprintf("User bucketed into variation %s by CMAB service", variation.Key)
decisionReasons.AddInfo(message)
return decision, decisionReasons, nil
}

// If we get here, the variation ID returned by CMAB service was not found
message := fmt.Sprintf("variation with ID %s not found in experiment %s", cmabDecision.VariationID, experiment.ID)
decisionReasons.AddInfo(message)
return decision, decisionReasons, fmt.Errorf("variation with ID %s not found in experiment %s", cmabDecision.VariationID, experiment.ID)

}

// isCmab is a helper method to check if an experiment is a CMAB experiment
func isCmab(experiment entities.Experiment) bool {
return experiment.Cmab != nil
}
Loading