i3wangyi commented on a change in pull request #367: Add transient cache for CustomRestClient implementation URL: https://github.com/apache/helix/pull/367#discussion_r310786737
########## File path: helix-rest/src/main/java/org/apache/helix/rest/client/CustomRestClientImpl.java ########## @@ -97,7 +106,43 @@ public CustomRestClientImpl(HttpClient httpClient) { kv -> result.put(kv.getKey(), kv.getValue().get(IS_HEALTHY_FIELD).asBoolean())); return result; }; - return handleResponse(post(url, payLoads), jsonConverter); + String requestId = baseUrl + customPayloads.toString() + partitions.toString(); + try { + lock(requestId); + if (_cachedResults.containsKey(requestId)) { + return _cachedResults.get(requestId); + } + Map<String, Boolean> result = handleResponse(post(url, payLoads), jsonConverter); + _cachedResults.put(requestId, result); + return result; + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + unlock(requestId); + } + return Collections.emptyMap(); + } + + @VisibleForTesting + Map<String, Map<String, Boolean>> getCachedResults() { + return _cachedResults; + } + + private void lock(String key) throws InterruptedException { + synchronized (lockedKeys) { Review comment: It is locked on key level, my unit test just verifies it. See the below debug message, other thread is able to acquire a different key before others release the key ``` Thread: 7 acquired key http://localhost:1000{}[Thread: 7] Thread: 4 acquired key http://localhost:1000{}[Thread: 4] Thread: 6 acquired key http://localhost:1000{}[Thread: 6] Thread: 0 acquired key http://localhost:1000{}[Thread: 0] Thread: 9 acquired key http://localhost:1000{}[Thread: 9] Thread: 5 acquired key http://localhost:1000{}[Thread: 5] Thread: 2 acquired key http://localhost:1000{}[Thread: 2] Thread: 8 acquired key http://localhost:1000{}[Thread: 8] Thread: 3 acquired key http://localhost:1000{}[Thread: 3] Thread: 1 acquired key http://localhost:1000{}[Thread: 1] Thread: 4 release lock for http://localhost:1000{}[Thread: 4] Thread: 9 release lock for http://localhost:1000{}[Thread: 9] Thread: 2 release lock for http://localhost:1000{}[Thread: 2] Thread: 1 release lock for http://localhost:1000{}[Thread: 1] Thread: 6 release lock for http://localhost:1000{}[Thread: 6] Thread: 0 release lock for http://localhost:1000{}[Thread: 0] Thread: 3 release lock for http://localhost:1000{}[Thread: 3] Thread: 8 release lock for http://localhost:1000{}[Thread: 8] Thread: 7 release lock for http://localhost:1000{}[Thread: 7] Thread: 5 release lock for http://localhost:1000{}[Thread: 5] ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services