Skip to content
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

Async cache setting #1736

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class AsyncExecutorService {
private static AsyncExecutorService asyncExecutorService = null;
private ResultStorageEngine resultStorageEngine;
private ThreadLocal<AsyncAPIResultFuture> asyncResultFutureThreadLocal = new ThreadLocal<>();

private boolean bypassCache;
/**
* A Future with Synchronous Execution Complete Flag.
*/
Expand All @@ -62,9 +62,10 @@ private class AsyncAPIResultFuture {

@Inject
private AsyncExecutorService(Elide elide, Integer threadPoolSize, AsyncAPIDAO asyncAPIDao,
ResultStorageEngine resultStorageEngine) {
ResultStorageEngine resultStorageEngine, boolean bypassCache) {
this.elide = elide;
runners = new HashMap();
this.bypassCache = bypassCache;

for (String apiVersion : elide.getElideSettings().getDictionary().getApiVersions()) {
runners.put(apiVersion, new QueryRunner(elide, apiVersion));
Expand All @@ -84,10 +85,10 @@ private AsyncExecutorService(Elide elide, Integer threadPoolSize, AsyncAPIDAO as
* @param asyncAPIDao DAO Object
*/
public static void init(Elide elide, Integer threadPoolSize, AsyncAPIDAO asyncAPIDao,
ResultStorageEngine resultStorageEngine) {
ResultStorageEngine resultStorageEngine, boolean bypassCache) {
if (asyncExecutorService == null) {
asyncExecutorService = new AsyncExecutorService(elide, threadPoolSize, asyncAPIDao,
resultStorageEngine);
resultStorageEngine, bypassCache);
} else {
log.debug("asyncExecutorService is already initialized.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import lombok.extern.slf4j.Slf4j;

import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
Expand All @@ -37,6 +40,7 @@ public class AsyncQueryThread implements Callable<AsyncAPIResult> {
private AsyncExecutorService service;
private String apiVersion;


public AsyncQueryThread(AsyncAPI queryObj, User user, AsyncExecutorService service, String apiVersion) {
this.queryObj = queryObj;
this.user = user;
Expand All @@ -49,10 +53,13 @@ public AsyncAPIResult call() throws URISyntaxException, NoHttpResponseException
ElideResponse response = null;
log.debug("AsyncQuery Object from request: {}", this);
UUID requestUUID = UUID.fromString(queryObj.getRequestId());
Map<String, List<String>> bypassCacheMap = new HashMap<String, List<String>>();
bypassCacheMap.put("bypasscache", Arrays.asList(String.valueOf(service.isBypassCache())));
Copy link
Member

Choose a reason for hiding this comment

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

Won't this always override the cache? I don't think this is the behavior that we want.


if (queryObj.getQueryType().equals(QueryType.JSONAPI_V1_0)) {
response = executeJsonApiRequest(service.getElide(), user, apiVersion, requestUUID);
response = executeJsonApiRequest(service.getElide(), user, apiVersion, requestUUID, bypassCacheMap);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of naming the variable bypassCacheMap, call it headers. It is a better reflection of what the map represents.

} else if (queryObj.getQueryType().equals(QueryType.GRAPHQL_V1_0)) {
response = executeGraphqlRequest(service.getRunners(), user, apiVersion, requestUUID);
response = executeGraphqlRequest(service.getRunners(), user, apiVersion, requestUUID, bypassCacheMap);
}
nullResponseCheck(response);

Expand Down Expand Up @@ -90,24 +97,25 @@ private String getPath(URIBuilder uri) {
return uri.getPath();
}

private ElideResponse executeJsonApiRequest(Elide elide, User user, String apiVersion, UUID requestUUID)
throws URISyntaxException {
private ElideResponse executeJsonApiRequest(Elide elide, User user, String apiVersion, UUID requestUUID,
Map<String, List<String>> bypassCacheMap) throws URISyntaxException {
URIBuilder uri = new URIBuilder(queryObj.getQuery());
MultivaluedMap<String, String> queryParams = getQueryParams(uri);
log.debug("Extracted QueryParams from AsyncQuery Object: {}", queryParams);

//TODO - we need to add the baseUrlEndpoint to the queryObject.
ElideResponse response = elide.get("", getPath(uri), queryParams, user, apiVersion, requestUUID);
ElideResponse response = elide.get("", getPath(uri), queryParams, bypassCacheMap, user, apiVersion,
requestUUID);
log.debug("JSONAPI_V1_0 getResponseCode: {}, JSONAPI_V1_0 getBody: {}",
response.getResponseCode(), response.getBody());
return response;
}

private ElideResponse executeGraphqlRequest(Map<String, QueryRunner> runners, User user, String apiVersion,
UUID requestUUID) throws URISyntaxException {
UUID requestUUID, Map<String, List<String>> bypassCacheMap) throws URISyntaxException {
QueryRunner runner = runners.get(apiVersion);
//TODO - we need to add the baseUrlEndpoint to the queryObject.
ElideResponse response = runner.run("", queryObj.getQuery(), user, requestUUID);
ElideResponse response = runner.run("", queryObj.getQuery(), user, requestUUID, bypassCacheMap);
log.debug("GRAPHQL_V1_0 getResponseCode: {}, GRAPHQL_V1_0 getBody: {}",
response.getResponseCode(), response.getBody());
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setupMockElide() {
asyncAPIDao = mock(DefaultAsyncAPIDAO.class);
testUser = mock(User.class);
resultStorageEngine = mock(FileResultStorageEngine.class);
AsyncExecutorService.init(elide, 5, asyncAPIDao, resultStorageEngine);
AsyncExecutorService.init(elide, 5, asyncAPIDao, resultStorageEngine, true);
service = AsyncExecutorService.getInstance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testProcessQueryJsonApi() throws NoHttpResponseException, URISyntaxE
queryObj.setQuery(query);
queryObj.setQueryType(QueryType.JSONAPI_V1_0);

when(elide.get(anyString(), anyString(), any(), any(), anyString(), any())).thenReturn(response);
when(elide.get(anyString(), anyString(), any(), any(), any(), anyString(), any())).thenReturn(response);
AsyncQueryThread queryThread = new AsyncQueryThread(queryObj, user, asyncExecutorService, "v1");
AsyncQueryResult queryResultObj = (AsyncQueryResult) queryThread.call();
assertEquals(queryResultObj.getResponseBody(), "ResponseBody");
Expand All @@ -72,7 +72,7 @@ public void testProcessQueryGraphQl() throws NoHttpResponseException, URISyntaxE
queryObj.setQuery(query);
queryObj.setQueryType(QueryType.GRAPHQL_V1_0);

when(runner.run(anyString(), eq(query), eq(user), any())).thenReturn(response);
when(runner.run(anyString(), eq(query), eq(user), any(), any())).thenReturn(response);
AsyncQueryThread queryThread = new AsyncQueryThread(queryObj, user, asyncExecutorService, "v1");
AsyncQueryResult queryResultObj = (AsyncQueryResult) queryThread.call();
assertEquals(queryResultObj.getResponseBody(), "ResponseBody");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected void configure() {

ResultStorageEngine resultStorageEngine =
new FileResultStorageEngine(System.getProperty("java.io.tmpDir"));
AsyncExecutorService.init(elide, 5, asyncAPIDao, resultStorageEngine);
AsyncExecutorService.init(elide, 5, asyncAPIDao, resultStorageEngine, true);
bind(AsyncExecutorService.getInstance()).to(AsyncExecutorService.class);

BillingService billingService = new BillingService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public class AsyncProperties {
* Whether or not the async feature is enabled.
*/
private boolean enabled = false;

/**
* Bypass Query result caching.
*/
private boolean bypassCache = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public AsyncExecutorService buildAsyncExecutorService(Elide elide, ElideConfigPr
AsyncAPIDAO asyncQueryDao, EntityDictionary dictionary,
@Autowired(required = false) ResultStorageEngine resultStorageEngine) {
AsyncExecutorService.init(elide, settings.getAsync().getThreadPoolSize(),
asyncQueryDao, resultStorageEngine);
asyncQueryDao, resultStorageEngine, settings.getAsync().isBypassCache());
AsyncExecutorService asyncExecutorService = AsyncExecutorService.getInstance();

// Binding AsyncQuery LifeCycleHook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ elide:
cleanupEnabled: true
queryCleanupDays: 7
defaultAsyncAPIDAO: true
bypassCache: true
dynamic-config:
path: src/test/resources/configs
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected void configure() {
// TODO: If null, initialize with FileResultStorageEngine
ResultStorageEngine resultStorageEngine = asyncProperties.getResultStorageEngine();
AsyncExecutorService.init(elide, asyncProperties.getThreadSize(), asyncAPIDao,
resultStorageEngine);
resultStorageEngine, asyncProperties.bypassCache());
bind(AsyncExecutorService.getInstance()).to(AsyncExecutorService.class);

// Binding AsyncQuery LifeCycleHook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@ default AsyncAPIDAO getAPIDAO() {
default ResultStorageEngine getResultStorageEngine() {
return null;
}

/**
* Bypass Query result caching.
*
* @return Default: true
*/
default boolean bypassCache() {
return true;
AvaniMakwana marked this conversation as resolved.
Show resolved Hide resolved
}
}