You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2021/12/31 03:00:06 UTC

[GitHub] [druid] jasonk000 opened a new pull request #12109: perf: indexing: improve DimensionDictionary index locking on add path

jasonk000 opened a new pull request #12109:
URL: https://github.com/apache/druid/pull/12109


   ### Description
   
   Following from #12105 with 1-20% CPU in `DimensionDictionary`, there are some improvements to be made to the `add()` path on the `StringDimensionIndexer`.
   
   There are three sub-changes:
   
   #### Introduce a new benchmark
   
   `StringDimensionIndexerProcessBenchmark` in cff7a6caaab2d28218ab01655c44d24e99c40e0c covers the `StringDimensionIndexer` `processRowValsToUnsortedEncodedKeyComponent` for throughput.
   
   #### Optimise `StringDimensionIndexer` -> `DimensionDictionary` boundary
   
   in 400f0a5137264d9c4f0a88dc346b6730458b1b09 introduce an `AddResult` class to combine the result of both (a) what is the index of the result, and (b) was the result a new addition or refers to a previous element.
   
   This results in a ~30% improvement on this codepath single threaded, and ~70%  by bypassing multiple `size()` calls to determine whether the dictionary has changed.
   
   #### Switch lock implementation in `DimensionDictionary` to `StampedLock`
   
   In aea89f5c502317b69957fbf592d84e4e9ee3f9a5 introduce improved concurrency under multiple-reader scenarios, and scenarios where `add()` of existing elements. Results in 3x improvement in contended situation.
   
   #### Refactor
   
   Slight refactor in 3e5c410047a74b3cae8044070260384803a2e1e5 to simplify flow in `StringDimensionIndexer`.
   
   <!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
   
   This PR has:
   - [x] been self-reviewed.
      - [x] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
   - [ ] added documentation for new or modified features or behaviors.
   - [x] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
   - [x] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [x] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
   - [ ] added integration tests.
   - [ ] been tested in a test Druid cluster. -> nb: partly, have not tested `StampedLock` in prod
   


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] samarthjain commented on a change in pull request #12109: perf: indexing: improve DimensionDictionary index locking on add path

Posted by GitBox <gi...@apache.org>.
samarthjain commented on a change in pull request #12109:
URL: https://github.com/apache/druid/pull/12109#discussion_r821035172



##########
File path: processing/src/main/java/org/apache/druid/segment/DimensionDictionary.java
##########
@@ -47,102 +47,146 @@
   private final Object2IntMap<T> valueToId = new Object2IntOpenHashMap<>();
 
   private final List<T> idToValue = new ArrayList<>();
-  private final ReentrantReadWriteLock lock;
+  private final StampedLock lock;
 
   public DimensionDictionary()
   {
-    this.lock = new ReentrantReadWriteLock();
+    this.lock = new StampedLock();

Review comment:
       I think it is worthwhile comparing benchmark runs using synchronized vs stamped locks vs read write locks. The modern JVMs have significantly improved performance for locks afforded by synchronized construct. Looking at this code, it doesn't look like there is a lot of long running activity going around when holding the write lock. So I suspect just using basic synchronized based locking should suffice. One other behavior change with StampedLock is that it is not reentrant unlike the `ReadWriteReentrantLock` and synchronized. That makes the code slightly more difficult to maintain, but I personally am OK with it if perf turns out to be better. 




-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] samarthjain commented on pull request #12109: perf: indexing: improve DimensionDictionary index locking on add path

Posted by GitBox <gi...@apache.org>.
samarthjain commented on pull request #12109:
URL: https://github.com/apache/druid/pull/12109#issuecomment-1061057132


   @jasonk000 - To make it easier to compare performance of reentrant locks vs other locking mechanisms, it might make sense to separate out locking related changes from the add path changes. 


-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org