-
Notifications
You must be signed in to change notification settings - Fork 591
Implement a ThreadLocalCache to improve read perf #1155
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
149 changes: 149 additions & 0 deletions
149
hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/OptimisticCache.java
This file contains hidden or 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,149 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with this | ||
| * work for additional information regarding copyright ownership. The ASF | ||
| * licenses this file to You 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.baidu.hugegraph.backend.cache; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.Map; | ||
| import java.util.concurrent.locks.StampedLock; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import com.baidu.hugegraph.iterator.MapperIterator; | ||
| import com.baidu.hugegraph.util.E; | ||
|
|
||
| public class OptimisticCache<K, V> extends AbstractCache<K, V> { | ||
|
|
||
| private final StampedLock stampedLock; | ||
| private final Map<K, V> cache; | ||
|
|
||
| public OptimisticCache(long capacity) { | ||
| super(capacity); | ||
| this.stampedLock = new StampedLock(); | ||
| this.cache = new HashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| protected V access(K id) { | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| long stamp = stampedLock.tryOptimisticRead(); | ||
| V value = this.cache.get(id); | ||
| if (!stampedLock.validate(stamp)) { | ||
| stamp = stampedLock.readLock(); | ||
| try { | ||
| value = this.cache.get(id); | ||
| } finally { | ||
| stampedLock.unlockRead(stamp); | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean write(K id, V value) { | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| final long stamp = stampedLock.writeLock(); | ||
| try { | ||
| this.cache.put(id, value); | ||
| } finally { | ||
| stampedLock.unlockWrite(stamp); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected void remove(K id) { | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| final long stamp = stampedLock.writeLock(); | ||
| try { | ||
| this.cache.remove(id); | ||
| } finally { | ||
| stampedLock.unlockWrite(stamp); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected Iterator<CacheNode<K, V>> nodes() { | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| final long stamp = stampedLock.readLock(); | ||
| try { | ||
| Iterator<Map.Entry<K, V>> iter = this.cache.entrySet().iterator(); | ||
| return new MapperIterator<>(iter, kvEntry -> { | ||
| return new CacheNode<>(kvEntry.getKey(), kvEntry.getValue()); | ||
| }); | ||
| } finally { | ||
| stampedLock.unlockRead(stamp); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsKey(K id) { | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| long stamp = stampedLock.tryOptimisticRead(); | ||
| boolean existed = this.cache.containsKey(id); | ||
| if (!stampedLock.validate(stamp)) { | ||
| stamp = stampedLock.readLock(); | ||
| try { | ||
| existed = this.cache.containsKey(id); | ||
| } finally { | ||
| stampedLock.unlockRead(stamp); | ||
| } | ||
| } | ||
| return existed; | ||
| } | ||
|
|
||
| @Override | ||
| public void traverse(Consumer<V> consumer) { | ||
| E.checkNotNull(consumer, "consumer"); | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| final long stamp = stampedLock.readLock(); | ||
| try { | ||
| this.cache.values().forEach(consumer); | ||
| } finally { | ||
| stampedLock.unlockRead(stamp); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| final long stamp = stampedLock.writeLock(); | ||
| try { | ||
| this.cache.clear(); | ||
| } finally { | ||
| stampedLock.unlockWrite(stamp); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long size() { | ||
| final StampedLock stampedLock = this.stampedLock; | ||
| long stamp = stampedLock.tryOptimisticRead(); | ||
| int size = this.cache.size(); | ||
| if (!stampedLock.validate(stamp)) { | ||
| stamp = stampedLock.readLock(); | ||
| try { | ||
| size = this.cache.size(); | ||
| } finally { | ||
| stampedLock.unlockRead(stamp); | ||
| } | ||
| } | ||
| return size; | ||
| } | ||
| } |
103 changes: 103 additions & 0 deletions
103
hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/ThreadLocalCache.java
This file contains hidden or 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,103 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with this | ||
| * work for additional information regarding copyright ownership. The ASF | ||
| * licenses this file to You 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.baidu.hugegraph.backend.cache; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import com.baidu.hugegraph.iterator.MapperIterator; | ||
| import com.baidu.hugegraph.util.E; | ||
|
|
||
| public class ThreadLocalCache<K, V> extends AbstractCache<K, V> { | ||
|
|
||
| private final List<Cache<K, V>> allCaches; | ||
| private final ThreadLocal<Cache<K ,V>> localCache; | ||
|
|
||
| public ThreadLocalCache(long capacity) { | ||
| super(capacity); | ||
| this.allCaches = new CopyOnWriteArrayList<>(); | ||
| this.localCache = new ThreadLocal<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsKey(K id) { | ||
| return this.getOrNewCache().containsKey(id); | ||
| } | ||
|
|
||
| @Override | ||
| public void traverse(Consumer<V> consumer) { | ||
| E.checkNotNull(consumer, "consumer"); | ||
| this.getOrNewCache().values().forEach(consumer); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| this.allCaches.forEach(HashMap::clear); | ||
| } | ||
|
|
||
| @Override | ||
| protected V access(K id) { | ||
| return this.getOrNewCache().get(id); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean write(K id, V value) { | ||
| this.getOrNewCache(); | ||
| this.allCaches.forEach(cache -> cache.put(id, value)); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected void remove(K id) { | ||
| this.allCaches.forEach(cache -> cache.remove(id)); | ||
| } | ||
|
|
||
| @Override | ||
| protected Iterator<CacheNode<K, V>> nodes() { | ||
| Iterator<Map.Entry<K, V>> iter = this.getOrNewCache().entrySet().iterator(); | ||
| return new MapperIterator<>(iter, kvEntry -> { | ||
| return new CacheNode<>(kvEntry.getKey(), kvEntry.getValue()); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public long size() { | ||
| return this.getOrNewCache().size(); | ||
| } | ||
|
|
||
| private Cache<K, V> getOrNewCache() { | ||
| Cache<K, V> cache = this.localCache.get(); | ||
| if (cache == null) { | ||
| cache = new Cache<>(); | ||
| this.localCache.set(cache); | ||
| this.allCaches.add(cache); | ||
| } | ||
| return cache; | ||
| } | ||
|
|
||
| private static class Cache<K, V> extends HashMap<K, V> { | ||
|
|
||
| private static final long serialVersionUID = -3426955734239657957L; | ||
| } | ||
| } | ||
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.
check capacity and throw if ooc