-
Notifications
You must be signed in to change notification settings - Fork 27
feat(datafile-cache): Add support for datafile cache service. #340
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,12 @@ client: | |
datafileURLTemplate: "https://localhost/v1/%s.json" | ||
eventURL: "https://logx.localhost.com/v1" | ||
sdkKeyRegex: "custom-regex" | ||
datafileCacheService: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add one more property enableDatafileCacheService and based on that, execute the logic. |
||
enabled: true | ||
services: | ||
redis: | ||
host: "localhost:6379" | ||
password: "123" | ||
userProfileService: | ||
default: "in-memory" | ||
services: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"sync" | ||
|
||
"github.com/optimizely/agent/config" | ||
"github.com/optimizely/agent/pkg/optimizely/datafilecacheservice" | ||
"github.com/optimizely/agent/plugins/userprofileservice" | ||
"github.com/optimizely/go-sdk/pkg/client" | ||
sdkconfig "github.com/optimizely/go-sdk/pkg/config" | ||
|
@@ -46,6 +47,14 @@ type OptlyCache struct { | |
wg sync.WaitGroup | ||
} | ||
|
||
type datafileCacheServiceKey = string | ||
|
||
// Represents keys in datafile cache services | ||
const ( | ||
enabled datafileCacheServiceKey = "enabled" | ||
redis datafileCacheServiceKey = "redis" | ||
) | ||
|
||
// NewCache returns a new implementation of OptlyCache interface backed by a concurrent map. | ||
func NewCache(ctx context.Context, conf config.ClientConfig, metricsRegistry *MetricsRegistry) *OptlyCache { | ||
|
||
|
@@ -58,7 +67,7 @@ func NewCache(ctx context.Context, conf config.ClientConfig, metricsRegistry *Me | |
cache := &OptlyCache{ | ||
ctx: ctx, | ||
wg: sync.WaitGroup{}, | ||
loader: defaultLoader(conf, metricsRegistry, userProfileServiceMap, cmLoader, event.NewBatchEventProcessor), | ||
loader: defaultLoader(ctx, conf, metricsRegistry, userProfileServiceMap, cmLoader, event.NewBatchEventProcessor), | ||
optlyMap: cmap.New(), | ||
userProfileServiceMap: userProfileServiceMap, | ||
} | ||
|
@@ -147,6 +156,7 @@ func regexValidator(sdkKeyRegex string) func(string) bool { | |
} | ||
|
||
func defaultLoader( | ||
ctx context.Context, | ||
conf config.ClientConfig, | ||
metricsRegistry *MetricsRegistry, | ||
userProfileServiceMap cmap.ConcurrentMap, | ||
|
@@ -186,25 +196,39 @@ func defaultLoader( | |
log.Info().Msg(message) | ||
} | ||
|
||
// Options for PollingProjectConfigManager | ||
options := []sdkconfig.OptionFunc{} | ||
|
||
// Check if datafile is already present in cache | ||
cachedDatafile, cacheService := getDatafileFromCacheService(ctx, sdkKey, conf) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a condition of cache is enabled then go into this method. |
||
if cachedDatafile != "" { | ||
// Set datafile in config manager so it uses the cached datafile for initialization | ||
options = append(options, sdkconfig.WithInitialDatafile([]byte(cachedDatafile))) | ||
} | ||
|
||
options = append(options, | ||
sdkconfig.WithPollingInterval(conf.PollingInterval), | ||
sdkconfig.WithDatafileURLTemplate(conf.DatafileURLTemplate), | ||
) | ||
|
||
if datafileAccessToken != "" { | ||
configManager = pcFactory( | ||
sdkKey, | ||
sdkconfig.WithPollingInterval(conf.PollingInterval), | ||
sdkconfig.WithDatafileURLTemplate(conf.DatafileURLTemplate), | ||
options = append(options, | ||
sdkconfig.WithDatafileAccessToken(datafileAccessToken), | ||
) | ||
} else { | ||
configManager = pcFactory( | ||
sdkKey, | ||
sdkconfig.WithPollingInterval(conf.PollingInterval), | ||
sdkconfig.WithDatafileURLTemplate(conf.DatafileURLTemplate), | ||
) | ||
} | ||
|
||
configManager = pcFactory(sdkKey, options...) | ||
|
||
if _, err := configManager.GetConfig(); err != nil { | ||
return &OptlyClient{}, err | ||
} | ||
|
||
// Set datafile in datafileCacheService if not present | ||
if cachedDatafile == "" && cacheService != nil { | ||
datafile := configManager.GetOptimizelyConfig().GetDatafile() | ||
cacheService.SetDatafileInCacheService(ctx, sdkKey, datafile) | ||
} | ||
|
||
q := event.NewInMemoryQueue(conf.QueueSize) | ||
ep := bpFactory( | ||
event.WithSDKKey(sdkKey), | ||
|
@@ -226,7 +250,7 @@ func defaultLoader( | |
} | ||
|
||
var clientUserProfileService decision.UserProfileService | ||
if clientUserProfileService = getUserProfileService(sdkKey, userProfileServiceMap, conf); clientUserProfileService != nil { | ||
if clientUserProfileService = getUserProfileService(ctx, sdkKey, userProfileServiceMap, conf); clientUserProfileService != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need to change this method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Earlier, We were giving |
||
clientOptions = append(clientOptions, client.WithUserProfileService(clientUserProfileService)) | ||
} | ||
|
||
|
@@ -237,8 +261,34 @@ func defaultLoader( | |
} | ||
} | ||
|
||
func getDatafileFromCacheService(ctx context.Context, sdkKey string, conf config.ClientConfig) (datafile string, cacheService datafilecacheservice.DatafileCacheService) { | ||
// Check whether datafileCacheService should be enabled | ||
if shouldEnable, ok := conf.DatafileCacheService[enabled].(bool); ok && shouldEnable { | ||
if services, ok := conf.DatafileCacheService["services"].(map[string]interface{}); ok { | ||
// In case of multiple cache services provided, use the first valid service | ||
for k, v := range services { | ||
bytes, err := json.Marshal(v) | ||
if err != nil { | ||
continue | ||
} | ||
switch k { | ||
case redis: | ||
var redisDatafileCache datafilecacheservice.RedisCacheService | ||
if err = json.Unmarshal(bytes, &redisDatafileCache); err != nil || redisDatafileCache.Address == "" { | ||
continue | ||
} | ||
return redisDatafileCache.GetDatafileFromCacheService(ctx, sdkKey), &redisDatafileCache | ||
default: | ||
// do nothing | ||
} | ||
} | ||
} | ||
} | ||
return "", nil | ||
} | ||
|
||
// Returns the registered userProfileService against the sdkKey | ||
func getUserProfileService(sdkKey string, userProfileServiceMap cmap.ConcurrentMap, conf config.ClientConfig) decision.UserProfileService { | ||
func getUserProfileService(ctx context.Context, sdkKey string, userProfileServiceMap cmap.ConcurrentMap, conf config.ClientConfig) decision.UserProfileService { | ||
|
||
intializeUPSWithName := func(upsName string) decision.UserProfileService { | ||
if clientConfigUPSMap, ok := conf.UserProfileService["services"].(map[string]interface{}); ok { | ||
|
@@ -256,6 +306,10 @@ func getUserProfileService(sdkKey string, userProfileServiceMap cmap.ConcurrentM | |
success = false | ||
} | ||
if success { | ||
// Pass context to ups if required, necessary for redis | ||
if ctxUps, ok := upsInstance.(userprofileservice.ContextUserProfileService); ok { | ||
ctxUps.AddContext(ctx) | ||
} | ||
log.Info().Msgf(`UserProfileService of type: "%s" created for sdkKey: "%s"`, upsName, sdkKey) | ||
return upsInstance | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
datafileCacheServices will be more relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to keep it similar to the naming convention we are following for
userProfileService
.