Skip to content

feat: Added ODPSegmentManager #484

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

Merged
merged 6 commits into from
Aug 18, 2022
Merged
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
74 changes: 74 additions & 0 deletions core-api/src/main/java/com/optimizely/ab/odp/ODPConfig.java
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thread-safety for all props?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made all getters and setters synchronized

}

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 core-api/src/main/java/com/optimizely/ab/odp/ODPSegmentManager.java
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add filtering for cases when odpConfig.getAllSegments() is empty?
We should not send this request to the ODP server.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check and a unit test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if odpConfig is not ready (apiKey and apiHost null)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check and a unit test.

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 core-api/src/main/java/com/optimizely/ab/odp/ODPSegmentOption.java
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 core-api/src/main/java/com/optimizely/ab/odp/ODPUserKey.java
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;
}
}
Loading