-
Notifications
You must be signed in to change notification settings - Fork 228
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
Lattice Cache - Caching Just Data Implementation #2619
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e017b05
Data cache implementation
0b47a24
Data cache
bfeca3b
Separate thread for event changes. Hasmap computeifabsent
276f3b1
Remove lazy caching. Prepopulate in the beginning, return null if dat…
7355b82
Addressed comments
d1f01df
Population of cache
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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 |
---|---|---|
|
@@ -21,31 +21,35 @@ | |
|
||
import org.apache.helix.metaclient.api.ChildChangeListener; | ||
import org.apache.helix.metaclient.api.MetaClientCacheInterface; | ||
import org.apache.helix.metaclient.datamodel.DataRecord; | ||
import org.apache.helix.metaclient.exception.MetaClientException; | ||
import org.apache.helix.metaclient.factories.MetaClientCacheConfig; | ||
import org.apache.helix.metaclient.factories.MetaClientConfig; | ||
import org.apache.helix.metaclient.impl.zk.adapter.ChildListenerAdapter; | ||
import org.apache.helix.metaclient.impl.zk.factory.ZkMetaClientConfig; | ||
import org.apache.helix.metaclient.impl.zk.factory.ZkMetaClientFactory; | ||
import org.apache.helix.metaclient.recipes.lock.LockInfoSerializer; | ||
import org.apache.helix.zookeeper.zkclient.ZkClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class ZkMetaClientCache<T> extends ZkMetaClient<T> implements MetaClientCacheInterface<T> { | ||
|
||
private Map<String, DataRecord> _dataCacheMap; | ||
private ConcurrentHashMap<String, T> _dataCacheMap; | ||
private final String _rootEntry; | ||
private TrieNode _childrenCacheTree; | ||
private ChildChangeListener _eventListener; | ||
private boolean _cacheData; | ||
private boolean _cacheChildren; | ||
private boolean _lazyCaching; | ||
private static final Logger LOG = LoggerFactory.getLogger(ZkMetaClientCache.class); | ||
private ZkClient _cacheClient; | ||
private ExecutorService executor; | ||
|
||
// TODO: Look into using conditional variable instead of latch. | ||
private final CountDownLatch _initializedCache = new CountDownLatch(1); | ||
|
||
/** | ||
* Constructor for ZkMetaClientCache. | ||
|
@@ -56,19 +60,43 @@ public ZkMetaClientCache(ZkMetaClientConfig config, MetaClientCacheConfig cacheC | |
super(config); | ||
_cacheClient = getZkClient(); | ||
_rootEntry = cacheConfig.getRootEntry(); | ||
_lazyCaching = cacheConfig.getLazyCaching(); | ||
_cacheData = cacheConfig.getCacheData(); | ||
_cacheChildren = cacheConfig.getCacheChildren(); | ||
|
||
if (_cacheData) { | ||
_dataCacheMap = new ConcurrentHashMap<>(); | ||
} | ||
if (_cacheChildren) { | ||
_childrenCacheTree = new TrieNode(_rootEntry, null); | ||
} | ||
} | ||
|
||
/** | ||
* Get data for a given key. | ||
* If datacache is enabled, will fetch for cache. If it doesn't exist | ||
* returns null (for when initial populating cache is in progress). | ||
* @param key key to identify the entry | ||
* @return data for the key | ||
*/ | ||
@Override | ||
public Stat exists(String key) { | ||
throw new MetaClientException("Not implemented yet."); | ||
public T get(final String key) { | ||
if (_cacheData) { | ||
T data = getDataCacheMap().get(key); | ||
if (data == null) { | ||
LOG.debug("Data not found in cache for key: {}. This could be because the cache is still being populated.", key); | ||
} | ||
return data; | ||
} | ||
return super.get(key); | ||
} | ||
|
||
@Override | ||
public T get(final String key) { | ||
throw new MetaClientException("Not implemented yet."); | ||
public List<T> get(List<String> keys) { | ||
List<T> dataList = new ArrayList<>(); | ||
for (String key : keys) { | ||
dataList.add(get(key)); | ||
} | ||
return dataList; | ||
} | ||
|
||
@Override | ||
|
@@ -81,14 +109,94 @@ public int countDirectChildren(final String key) { | |
throw new MetaClientException("Not implemented yet."); | ||
} | ||
|
||
@Override | ||
public List<T> get(List<String> keys) { | ||
throw new MetaClientException("Not implemented yet."); | ||
private void populateAllCache() { | ||
// TODO: Concurrently populate children and data cache. | ||
if (_cacheData) { | ||
Marcosrico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
List<String> children = _cacheClient.getChildren(_rootEntry); | ||
for (String child : children) { | ||
String childPath = _rootEntry + "/" + child; | ||
T dataRecord = _cacheClient.readData(childPath, true); | ||
xyuanlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getDataCacheMap().put(childPath, dataRecord); | ||
} | ||
} catch (Exception e) { | ||
// Ignore | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<Stat> exists(List<String> keys) { | ||
throw new MetaClientException("Not implemented yet."); | ||
private class CacheUpdateRunnable implements Runnable { | ||
private final String path; | ||
private final ChildChangeListener.ChangeType changeType; | ||
|
||
public CacheUpdateRunnable(String path, ChildChangeListener.ChangeType changeType) { | ||
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. nit: Add todo for dedup. 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. Added todo a little lower :) |
||
this.path = path; | ||
this.changeType = changeType; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
waitForPopulateAllCache(); | ||
// TODO: HANDLE DEDUP EVENT CHANGES | ||
switch (changeType) { | ||
case ENTRY_CREATED: | ||
// Not implemented yet. | ||
modifyDataInCache(path, false); | ||
break; | ||
case ENTRY_DELETED: | ||
// Not implemented yet. | ||
modifyDataInCache(path, true); | ||
break; | ||
case ENTRY_DATA_CHANGE: | ||
modifyDataInCache(path, false); | ||
break; | ||
default: | ||
LOG.error("Unknown change type: " + changeType); | ||
} | ||
} | ||
} | ||
|
||
private void waitForPopulateAllCache() { | ||
try { | ||
_initializedCache.await(); | ||
} catch (InterruptedException e) { | ||
throw new MetaClientException("Interrupted while waiting for cache to populate.", e); | ||
} | ||
} | ||
|
||
private void modifyDataInCache(String path, Boolean isDelete) { | ||
if (_cacheData) { | ||
if (isDelete) { | ||
getDataCacheMap().remove(path); | ||
} else { | ||
T dataRecord = _cacheClient.readData(path, true); | ||
getDataCacheMap().put(path, dataRecord); | ||
} | ||
} | ||
} | ||
|
||
public ConcurrentHashMap<String, T> getDataCacheMap() { | ||
return _dataCacheMap; | ||
} | ||
|
||
public TrieNode getChildrenCacheTree() { | ||
return _childrenCacheTree; | ||
} | ||
|
||
/** | ||
* Connect to the underlying ZkClient. | ||
*/ | ||
@Override | ||
public void connect() { | ||
super.connect(); | ||
_eventListener = (path, changeType) -> { | ||
Runnable cacheUpdateRunnable = new CacheUpdateRunnable(path, changeType); | ||
executor.execute(cacheUpdateRunnable); | ||
}; | ||
executor = Executors.newSingleThreadExecutor(); | ||
_cacheClient.subscribePersistRecursiveListener(_rootEntry, new ChildListenerAdapter(_eventListener)); | ||
populateAllCache(); | ||
// Notify the latch that cache is populated. | ||
_initializedCache.countDown(); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
same as above.