You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "ashley-taylor (via GitHub)" <gi...@apache.org> on 2024/05/08 02:05:44 UTC

[PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

ashley-taylor opened a new pull request, #2900:
URL: https://github.com/apache/avro/pull/2900

   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/main/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, commits follow the guidelines from [How to write a good git commit message](https://chris.beams.io/posts/git-commit/)
       1. Subject is separated from body by a blank line
       1. Subject is limited to 50 characters (not including Jira issue reference)
       1. Subject does not end with a period
       1. Subject uses the imperative mood ("add", not "adding")
       1. Body wraps at 72 characters
       1. Body explains "what" and "why", not "how"
   
   -->
   
   ## What is the purpose of the change
   
   Improve the performance by removing synchronisation calls.
   
   Running the `ReflectRecordTest` with 8 threads on a M2 Macbook pro
   
   before change
   
   ```
       Benchmark                  Mode  Cnt        Score        Error  Units
       ReflectRecordTest.decode  thrpt    3  4,157,551.230 ± 831123.819  ops/s
       ReflectRecordTest.encode  thrpt    3  4,926,092.511 ± 455798.533  ops/s
   ```
   
   
   after
   ```
       Benchmark                  Mode  Cnt         Score         Error  Units
       ReflectRecordTest.decode  thrpt    3  12,221,568.529 ± 1308392.259  ops/s
       ReflectRecordTest.encode  thrpt    3  12,569,201.802 ± 1898124.234  ops/s
   ```
   
   
   ## Verifying this change
   
   
   This change is already covered by existing tests, such as *(please describe tests)*.
   
   `org.apache.avo.reflect` already tests this code
   
   ## Documentation
   
   - Does this pull request introduce a new feature? no
   - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   


-- 
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: dev-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk merged PR #2900:
URL: https://github.com/apache/avro/pull/2900


-- 
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: dev-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #2900:
URL: https://github.com/apache/avro/pull/2900#discussion_r1595066467


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -379,12 +379,14 @@ private ClassAccessorData(Class<?> c) {
      * Return the field accessors as an array, indexed by the field index of the
      * given schema.
      */
-    private synchronized FieldAccessor[] getAccessorsFor(Schema schema) {
-      // if synchronized is removed from this method, adjust bySchema appropriately
+    private FieldAccessor[] getAccessorsFor(Schema schema) {
+      // to avoid synchronization, we replace the map for each modification
       FieldAccessor[] result = bySchema.get(schema);
       if (result == null) {
         result = createAccessorsFor(schema);
+        Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>(this.bySchema);
         bySchema.put(schema, result);
+        this.bySchema = bySchema;

Review Comment:
   Without synchronization `this.bySchema` could be modified several times by now. Would that be a problem ?



-- 
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: issues-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "ashley-taylor (via GitHub)" <gi...@apache.org>.
ashley-taylor commented on PR #2900:
URL: https://github.com/apache/avro/pull/2900#issuecomment-2136130880

   @martin-g anything you need to get this merged? 


-- 
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: issues-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2900:
URL: https://github.com/apache/avro/pull/2900#discussion_r1622618815


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -379,12 +379,14 @@ private ClassAccessorData(Class<?> c) {
      * Return the field accessors as an array, indexed by the field index of the
      * given schema.
      */
-    private synchronized FieldAccessor[] getAccessorsFor(Schema schema) {
-      // if synchronized is removed from this method, adjust bySchema appropriately
+    private FieldAccessor[] getAccessorsFor(Schema schema) {
+      // to avoid synchronization, we replace the map for each modification
       FieldAccessor[] result = bySchema.get(schema);
       if (result == null) {
         result = createAccessorsFor(schema);
+        Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>(this.bySchema);
         bySchema.put(schema, result);
+        this.bySchema = bySchema;

Review Comment:
   The remark about `volatile` is valid: that's exactly what it does.
   
   Also a few notes about `WeakHashMap`:
   
   1. it has weak keys, so it'll only cleanup entries that can no longer be looked up
   2. its internal hash table is a singly-linked list, and the update to remove an entry is an atomic operation (that never yields an invalid data structure)
   3. the update happens without a memory barrier, which means that the worst case scenario is that the update is not yet available to the other thread, which means that the entry to be removed is read & skipped over in the concurrent call to `get`
   
   In practical terms, this means that because the we update a *copy* of the `WeakHashMap` before it becomes available, and it's only modified to expunge stale entries after it becomes available, the result is effectively thread safe.



-- 
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: issues-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "ashley-taylor (via GitHub)" <gi...@apache.org>.
ashley-taylor commented on code in PR #2900:
URL: https://github.com/apache/avro/pull/2900#discussion_r1597435136


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -379,12 +379,14 @@ private ClassAccessorData(Class<?> c) {
      * Return the field accessors as an array, indexed by the field index of the
      * given schema.
      */
-    private synchronized FieldAccessor[] getAccessorsFor(Schema schema) {
-      // if synchronized is removed from this method, adjust bySchema appropriately
+    private FieldAccessor[] getAccessorsFor(Schema schema) {
+      // to avoid synchronization, we replace the map for each modification
       FieldAccessor[] result = bySchema.get(schema);
       if (result == null) {
         result = createAccessorsFor(schema);
+        Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>(this.bySchema);
         bySchema.put(schema, result);
+        this.bySchema = bySchema;

Review Comment:
   Since the object is derived It won't cause an issue. There could be duplicated generation across threads. Causing some wasted compute at the start of processing. But since `this.bySchema` is always replaced with a new map with no future modifications concurrent read are safe. 



-- 
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: issues-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "ashley-taylor (via GitHub)" <gi...@apache.org>.
ashley-taylor commented on PR #2900:
URL: https://github.com/apache/avro/pull/2900#issuecomment-2101488844

   to recreate the benchmark, you need to change the `threads` value here to be greater than 1
   
   https://github.com/apache/avro/blob/33668bfdf87bab87082ebad355758719ef2739ff/lang/java/perf/src/main/java/org/apache/avro/perf/Perf.java#L75


-- 
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: issues-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #2900:
URL: https://github.com/apache/avro/pull/2900#issuecomment-2136679307

   > @martin-g anything you need to get this merged?
   
   I'll leave it to a Java developer because I am not that familiar with the Java SDK.


-- 
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: issues-unsubscribe@avro.apache.org

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


Re: [PR] AVRO-3987 [Java] Concurrency improvement for ReflectData FieldAccessors [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2900:
URL: https://github.com/apache/avro/pull/2900#discussion_r1618425234


##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java:
##########
@@ -379,12 +379,14 @@ private ClassAccessorData(Class<?> c) {
      * Return the field accessors as an array, indexed by the field index of the
      * given schema.
      */
-    private synchronized FieldAccessor[] getAccessorsFor(Schema schema) {
-      // if synchronized is removed from this method, adjust bySchema appropriately
+    private FieldAccessor[] getAccessorsFor(Schema schema) {
+      // to avoid synchronization, we replace the map for each modification
       FieldAccessor[] result = bySchema.get(schema);
       if (result == null) {
         result = createAccessorsFor(schema);
+        Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>(this.bySchema);
         bySchema.put(schema, result);
+        this.bySchema = bySchema;

Review Comment:
   I'm not really a Java programmer but the `this.bySchema = bySchema` assignment seems OK, assuming that the `volatile` causes a memory barrier that ensures the effects of `bySchema.put(schema, result)` will be seen by other threads that read `this.bySchema` after the assignment.
   
   However, I wonder about `bySchema.get(schema)`.  Is WeakHashMap.get formally documented to be safe for concurrent calls?  Each time WeakHashMap.get is called, WeakHashMap.expungeStaleEntries can delete some entries, and although WeakHashMap.expungeStaleEntries synchronizes those deletions against each other, it doesn't seem to synchronize them against any reads that WeakHashMap.get may be doing on other threads.



-- 
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: issues-unsubscribe@avro.apache.org

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