-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added ODPSegmentManager (#484)
## Summary Added an ODPSegmentManager which provides functionality to fetch segments from ODP server and cache and re-use them. ## Test plan - Manually tested thoroughly - Added new unit tests - Existing unit tests pass - All Full stack compatibility suite tests pass ## JIRA [OASIS-8383](https://optimizely.atlassian.net/browse/OASIS-8383)
- Loading branch information
1 parent
5866f7b
commit 1cf2d72
Showing
5 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
core-api/src/main/java/com/optimizely/ab/odp/ODPConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* 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 com.optimizely.ab.odp; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ODPConfig { | ||
|
||
private String apiKey; | ||
|
||
private String apiHost; | ||
|
||
private List<String> allSegments; | ||
|
||
public ODPConfig(String apiKey, String apiHost, List<String> allSegments) { | ||
this.apiKey = apiKey; | ||
this.apiHost = apiHost; | ||
this.allSegments = allSegments; | ||
} | ||
|
||
public ODPConfig(String apiKey, String apiHost) { | ||
this(apiKey, apiHost, Collections.emptyList()); | ||
} | ||
|
||
public synchronized Boolean isReady() { | ||
return !( | ||
this.apiKey == null || this.apiKey.isEmpty() | ||
|| this.apiHost == null || this.apiHost.isEmpty() | ||
); | ||
} | ||
|
||
public synchronized Boolean hasSegments() { | ||
return allSegments != null && !allSegments.isEmpty(); | ||
} | ||
|
||
public synchronized void setApiKey(String apiKey) { | ||
this.apiKey = apiKey; | ||
} | ||
|
||
public synchronized void setApiHost(String apiHost) { | ||
this.apiHost = apiHost; | ||
} | ||
|
||
public synchronized String getApiKey() { | ||
return apiKey; | ||
} | ||
|
||
public synchronized String getApiHost() { | ||
return apiHost; | ||
} | ||
|
||
public synchronized List<String> getAllSegments() { | ||
return allSegments; | ||
} | ||
|
||
public synchronized void setAllSegments(List<String> allSegments) { | ||
this.allSegments = allSegments; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
core-api/src/main/java/com/optimizely/ab/odp/ODPSegmentManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* 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 com.optimizely.ab.odp; | ||
|
||
import com.optimizely.ab.internal.Cache; | ||
import com.optimizely.ab.internal.DefaultLRUCache; | ||
import com.optimizely.ab.odp.parser.ResponseJsonParser; | ||
import com.optimizely.ab.odp.parser.ResponseJsonParserFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ODPSegmentManager { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ODPSegmentManager.class); | ||
|
||
private static final String SEGMENT_URL_PATH = "/v3/graphql"; | ||
|
||
private final ODPApiManager apiManager; | ||
|
||
private final ODPConfig odpConfig; | ||
|
||
private final Cache<List<String>> segmentsCache; | ||
|
||
public ODPSegmentManager(ODPConfig odpConfig, ODPApiManager apiManager) { | ||
this(odpConfig, apiManager, Cache.DEFAULT_MAX_SIZE, Cache.DEFAULT_TIMEOUT_SECONDS); | ||
} | ||
|
||
public ODPSegmentManager(ODPConfig odpConfig, ODPApiManager apiManager, Cache<List<String>> cache) { | ||
this.apiManager = apiManager; | ||
this.odpConfig = odpConfig; | ||
this.segmentsCache = cache; | ||
} | ||
|
||
public ODPSegmentManager(ODPConfig odpConfig, ODPApiManager apiManager, Integer cacheSize, Integer cacheTimeoutSeconds) { | ||
this.apiManager = apiManager; | ||
this.odpConfig = odpConfig; | ||
this.segmentsCache = new DefaultLRUCache<>(cacheSize, cacheTimeoutSeconds); | ||
} | ||
|
||
public List<String> getQualifiedSegments(String fsUserId) { | ||
return getQualifiedSegments(ODPUserKey.FS_USER_ID, fsUserId, Collections.emptyList()); | ||
} | ||
public List<String> getQualifiedSegments(String fsUserId, List<ODPSegmentOption> options) { | ||
return getQualifiedSegments(ODPUserKey.FS_USER_ID, fsUserId, options); | ||
} | ||
|
||
public List<String> getQualifiedSegments(ODPUserKey userKey, String userValue) { | ||
return getQualifiedSegments(userKey, userValue, Collections.emptyList()); | ||
} | ||
|
||
public List<String> getQualifiedSegments(ODPUserKey userKey, String userValue, List<ODPSegmentOption> options) { | ||
if (!odpConfig.isReady()) { | ||
logger.error("Audience segments fetch failed (ODP is not enabled)"); | ||
return Collections.emptyList(); | ||
} | ||
|
||
if (!odpConfig.hasSegments()) { | ||
logger.debug("No Segments are used in the project, Not Fetching segments. Returning empty list"); | ||
return Collections.emptyList(); | ||
} | ||
|
||
List<String> qualifiedSegments; | ||
String cacheKey = getCacheKey(userKey.getKeyString(), userValue); | ||
|
||
if (options.contains(ODPSegmentOption.RESET_CACHE)) { | ||
segmentsCache.reset(); | ||
} else if (!options.contains(ODPSegmentOption.IGNORE_CACHE)) { | ||
qualifiedSegments = segmentsCache.lookup(cacheKey); | ||
if (qualifiedSegments != null) { | ||
logger.debug("ODP Cache Hit. Returning segments from Cache."); | ||
return qualifiedSegments; | ||
} | ||
} | ||
|
||
logger.debug("ODP Cache Miss. Making a call to ODP Server."); | ||
|
||
ResponseJsonParser parser = ResponseJsonParserFactory.getParser(); | ||
String qualifiedSegmentsResponse = apiManager.fetchQualifiedSegments(odpConfig.getApiKey(), odpConfig.getApiHost() + SEGMENT_URL_PATH, userKey.getKeyString(), userValue, odpConfig.getAllSegments()); | ||
qualifiedSegments = parser.parseQualifiedSegments(qualifiedSegmentsResponse); | ||
|
||
if (qualifiedSegments != null && !options.contains(ODPSegmentOption.IGNORE_CACHE)) { | ||
segmentsCache.save(cacheKey, qualifiedSegments); | ||
} | ||
|
||
return qualifiedSegments; | ||
} | ||
|
||
private String getCacheKey(String userKey, String userValue) { | ||
return userKey + "-$-" + userValue; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
core-api/src/main/java/com/optimizely/ab/odp/ODPSegmentOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* 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 com.optimizely.ab.odp; | ||
|
||
public enum ODPSegmentOption { | ||
|
||
IGNORE_CACHE, | ||
|
||
RESET_CACHE; | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
core-api/src/main/java/com/optimizely/ab/odp/ODPUserKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* 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 com.optimizely.ab.odp; | ||
|
||
public enum ODPUserKey { | ||
|
||
VUID("vuid"), | ||
|
||
FS_USER_ID("fs_user_id"); | ||
|
||
private final String keyString; | ||
|
||
ODPUserKey(String keyString) { | ||
this.keyString = keyString; | ||
} | ||
|
||
public String getKeyString() { | ||
return keyString; | ||
} | ||
} |
Oops, something went wrong.