You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by "luoluoyuyu (via GitHub)" <gi...@apache.org> on 2026/01/23 02:37:12 UTC

[PR] Optimize TsTable with Copy-on-Write pattern for thread-safe concurrent access [iotdb]

luoluoyuyu opened a new pull request, #17069:
URL: https://github.com/apache/iotdb/pull/17069

   ## Description
   - Change columnSchemaMap, tagColumnIndexMap, and idColumnIndexMap from final to volatile
   - Implement Copy-on-Write in executeWrite method:
     1. Create local copies of maps before modification
     2. Execute write operations on local copies
     3. Atomically update class fields after write completes
   - Add WriteOperation functional interface to pass map copies to write operations
   - This ensures readers see either complete old data or complete new data, avoiding ConcurrentModificationException
   
   
   <!--
   In each section, please describe design decisions made, including:
    - Choice of algorithms
    - Behavioral aspects. What configuration values are acceptable? How are corner cases and error 
       conditions handled, such as when there are insufficient resources?
    - Class organization and design (how the logic is split between classes, inheritance, composition, 
       design patterns)
    - Method organization and design (how the logic is split between methods, parameters and return types)
    - Naming (class, method, API, configuration, HTTP endpoint, names of emitted metrics)
   -->
   
   
   <!-- It's good to describe an alternative design (or mention an alternative name) for every design 
   (or naming) decision point and compare the alternatives with the designs that you've implemented 
   (or the names you've chosen) to highlight the advantages of the chosen designs and names. -->
   
   <!-- If there was a discussion of the design of the feature implemented in this PR elsewhere 
   (e. g. a "Proposal" issue, any other issue, or a thread in the development mailing list), 
   link to that discussion from this PR description and explain what have changed in your final design 
   compared to your original proposal or the consensus version in the end of the discussion. 
   If something hasn't changed since the original discussion, you can omit a detailed discussion of 
   those aspects of the design here, perhaps apart from brief mentioning for the sake of readability 
   of this PR description. -->
   
   <!-- Some of the aspects mentioned above may be omitted for simple and small changes. -->
   
   <hr>
   
   This PR has:
   - [ ] been self-reviewed.
       - [ ] concurrent read
       - [ ] concurrent write
       - [ ] concurrent read and write 
   - [ ] added documentation for new or modified features or behaviors.
   - [ ] added Javadocs for most classes and all non-trivial methods. 
   - [ ] added or updated version, __license__, or notice information
   - [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious 
     for an unfamiliar reader.
   - [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold 
     for code coverage.
   - [ ] added integration tests.
   - [ ] been tested in a test IoTDB cluster.
   
   <!-- 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 above are strictly necessary, but it would be very helpful if you at least 
   self-review the PR. -->
   
   <hr>
   
   ##### Key changed/added classes (or packages if there are too many classes) in this PR
   


-- 
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: reviews-unsubscribe@iotdb.apache.org

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


Re: [PR] Optimize TsTable with Copy-on-Write pattern for thread-safe concurrent access [iotdb]

Posted by "luoluoyuyu (via GitHub)" <gi...@apache.org>.
luoluoyuyu commented on code in PR #17069:
URL: https://github.com/apache/iotdb/pull/17069#discussion_r2740818565


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java:
##########
@@ -149,23 +150,78 @@ public TsTableColumnSchema getColumnSchema(final String columnName) {
   }
 
   /**
-   * Execute a write operation with optimistic lock support. This method handles the write flag and
-   * version increment automatically.
+   * Execute a write operation with Copy-on-Write support. This method creates new copies of the
+   * maps before modification to ensure thread-safe reads without locks.
    *
-   * @param writeOperation the write operation to execute
+   * @param writeOperation the write operation to execute on the new map copies
    */
-  private void executeWrite(Runnable writeOperation) {
+  private void executeWrite(WriteOperation writeOperation) {
     readWriteLock.writeLock().lock();
     isNotWrite.set(false);
     try {
-      writeOperation.run();
+      // Copy-on-Write: create local copies first
+      Map<String, TsTableColumnSchema> newColumnSchemaMap = new LinkedHashMap<>(columnSchemaMap);
+      Map<String, Integer> newTagColumnIndexMap = new HashMap<>(tagColumnIndexMap);
+      Map<String, Integer> newIdColumnIndexMap = new HashMap<>(idColumnIndexMap);
+
+      // Execute write operation on local copies
+      writeOperation.execute(newColumnSchemaMap, newTagColumnIndexMap, newIdColumnIndexMap);
+
+      // After write completes, atomically update the class fields
+      columnSchemaMap = newColumnSchemaMap;
+      tagColumnIndexMap = newTagColumnIndexMap;
+      idColumnIndexMap = newIdColumnIndexMap;

Review Comment:
   Fixed the issue of Tag column updates during rename.
   



-- 
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: reviews-unsubscribe@iotdb.apache.org

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


Re: [PR] Optimize TsTable with Copy-on-Write pattern for thread-safe concurrent access [iotdb]

Posted by "jt2594838 (via GitHub)" <gi...@apache.org>.
jt2594838 commented on code in PR #17069:
URL: https://github.com/apache/iotdb/pull/17069#discussion_r2726213678


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java:
##########
@@ -149,23 +150,78 @@ public TsTableColumnSchema getColumnSchema(final String columnName) {
   }
 
   /**
-   * Execute a write operation with optimistic lock support. This method handles the write flag and
-   * version increment automatically.
+   * Execute a write operation with Copy-on-Write support. This method creates new copies of the
+   * maps before modification to ensure thread-safe reads without locks.
    *
-   * @param writeOperation the write operation to execute
+   * @param writeOperation the write operation to execute on the new map copies
    */
-  private void executeWrite(Runnable writeOperation) {
+  private void executeWrite(WriteOperation writeOperation) {
     readWriteLock.writeLock().lock();
     isNotWrite.set(false);
     try {
-      writeOperation.run();
+      // Copy-on-Write: create local copies first
+      Map<String, TsTableColumnSchema> newColumnSchemaMap = new LinkedHashMap<>(columnSchemaMap);
+      Map<String, Integer> newTagColumnIndexMap = new HashMap<>(tagColumnIndexMap);
+      Map<String, Integer> newIdColumnIndexMap = new HashMap<>(idColumnIndexMap);
+
+      // Execute write operation on local copies
+      writeOperation.execute(newColumnSchemaMap, newTagColumnIndexMap, newIdColumnIndexMap);
+
+      // After write completes, atomically update the class fields
+      columnSchemaMap = newColumnSchemaMap;
+      tagColumnIndexMap = newTagColumnIndexMap;
+      idColumnIndexMap = newIdColumnIndexMap;

Review Comment:
   And, may only retain tagColumnIndexMap since `id` is a deprecated name



-- 
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: reviews-unsubscribe@iotdb.apache.org

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


Re: [PR] Optimize TsTable with Copy-on-Write pattern for thread-safe concurrent access [iotdb]

Posted by "jt2594838 (via GitHub)" <gi...@apache.org>.
jt2594838 commented on code in PR #17069:
URL: https://github.com/apache/iotdb/pull/17069#discussion_r2726211523


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java:
##########
@@ -149,23 +150,78 @@ public TsTableColumnSchema getColumnSchema(final String columnName) {
   }
 
   /**
-   * Execute a write operation with optimistic lock support. This method handles the write flag and
-   * version increment automatically.
+   * Execute a write operation with Copy-on-Write support. This method creates new copies of the
+   * maps before modification to ensure thread-safe reads without locks.
    *
-   * @param writeOperation the write operation to execute
+   * @param writeOperation the write operation to execute on the new map copies
    */
-  private void executeWrite(Runnable writeOperation) {
+  private void executeWrite(WriteOperation writeOperation) {
     readWriteLock.writeLock().lock();
     isNotWrite.set(false);
     try {
-      writeOperation.run();
+      // Copy-on-Write: create local copies first
+      Map<String, TsTableColumnSchema> newColumnSchemaMap = new LinkedHashMap<>(columnSchemaMap);
+      Map<String, Integer> newTagColumnIndexMap = new HashMap<>(tagColumnIndexMap);
+      Map<String, Integer> newIdColumnIndexMap = new HashMap<>(idColumnIndexMap);
+
+      // Execute write operation on local copies
+      writeOperation.execute(newColumnSchemaMap, newTagColumnIndexMap, newIdColumnIndexMap);
+
+      // After write completes, atomically update the class fields
+      columnSchemaMap = newColumnSchemaMap;
+      tagColumnIndexMap = newTagColumnIndexMap;
+      idColumnIndexMap = newIdColumnIndexMap;

Review Comment:
   There may still introduce inconsistency



-- 
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: reviews-unsubscribe@iotdb.apache.org

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


Re: [PR] Optimize TsTable with Copy-on-Write pattern for thread-safe concurrent access [iotdb]

Posted by "luoluoyuyu (via GitHub)" <gi...@apache.org>.
luoluoyuyu commented on code in PR #17069:
URL: https://github.com/apache/iotdb/pull/17069#discussion_r2740954182


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java:
##########
@@ -149,23 +150,78 @@ public TsTableColumnSchema getColumnSchema(final String columnName) {
   }
 
   /**
-   * Execute a write operation with optimistic lock support. This method handles the write flag and
-   * version increment automatically.
+   * Execute a write operation with Copy-on-Write support. This method creates new copies of the
+   * maps before modification to ensure thread-safe reads without locks.
    *
-   * @param writeOperation the write operation to execute
+   * @param writeOperation the write operation to execute on the new map copies
    */
-  private void executeWrite(Runnable writeOperation) {
+  private void executeWrite(WriteOperation writeOperation) {
     readWriteLock.writeLock().lock();
     isNotWrite.set(false);
     try {
-      writeOperation.run();
+      // Copy-on-Write: create local copies first
+      Map<String, TsTableColumnSchema> newColumnSchemaMap = new LinkedHashMap<>(columnSchemaMap);
+      Map<String, Integer> newTagColumnIndexMap = new HashMap<>(tagColumnIndexMap);
+      Map<String, Integer> newIdColumnIndexMap = new HashMap<>(idColumnIndexMap);
+
+      // Execute write operation on local copies
+      writeOperation.execute(newColumnSchemaMap, newTagColumnIndexMap, newIdColumnIndexMap);
+
+      // After write completes, atomically update the class fields
+      columnSchemaMap = newColumnSchemaMap;
+      tagColumnIndexMap = newTagColumnIndexMap;
+      idColumnIndexMap = newIdColumnIndexMap;

Review Comment:
   This PR primarily aims to prevent the HashMap from being modified during the read process, ensuring that unpredictable behavior does not occur during reads.



-- 
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: reviews-unsubscribe@iotdb.apache.org

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


Re: [PR] Optimize TsTable with Copy-on-Write pattern for thread-safe concurrent access [iotdb]

Posted by "luoluoyuyu (via GitHub)" <gi...@apache.org>.
luoluoyuyu commented on code in PR #17069:
URL: https://github.com/apache/iotdb/pull/17069#discussion_r2740944067


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java:
##########
@@ -149,23 +150,78 @@ public TsTableColumnSchema getColumnSchema(final String columnName) {
   }
 
   /**
-   * Execute a write operation with optimistic lock support. This method handles the write flag and
-   * version increment automatically.
+   * Execute a write operation with Copy-on-Write support. This method creates new copies of the
+   * maps before modification to ensure thread-safe reads without locks.
    *
-   * @param writeOperation the write operation to execute
+   * @param writeOperation the write operation to execute on the new map copies
    */
-  private void executeWrite(Runnable writeOperation) {
+  private void executeWrite(WriteOperation writeOperation) {
     readWriteLock.writeLock().lock();
     isNotWrite.set(false);
     try {
-      writeOperation.run();
+      // Copy-on-Write: create local copies first
+      Map<String, TsTableColumnSchema> newColumnSchemaMap = new LinkedHashMap<>(columnSchemaMap);
+      Map<String, Integer> newTagColumnIndexMap = new HashMap<>(tagColumnIndexMap);
+      Map<String, Integer> newIdColumnIndexMap = new HashMap<>(idColumnIndexMap);
+
+      // Execute write operation on local copies
+      writeOperation.execute(newColumnSchemaMap, newTagColumnIndexMap, newIdColumnIndexMap);
+
+      // After write completes, atomically update the class fields
+      columnSchemaMap = newColumnSchemaMap;
+      tagColumnIndexMap = newTagColumnIndexMap;
+      idColumnIndexMap = newIdColumnIndexMap;

Review Comment:
   The  ```private final transient AtomicLong instanceVersion = new AtomicLong(0L);``` and ```private final transient AtomicBoolean isNotWrite = new AtomicBoolean(true);``` define two variables to ensure optimistic read operation; data being written will be downgraded to acquiring data through a read lock.



-- 
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: reviews-unsubscribe@iotdb.apache.org

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


Re: [PR] Optimize TsTable with Copy-on-Write pattern for thread-safe concurrent access [iotdb]

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


-- 
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: reviews-unsubscribe@iotdb.apache.org

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