This is an automated email from the ASF dual-hosted git repository.
JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 585d88d72ed Fix concurrent table schema snapshots
585d88d72ed is described below
commit 585d88d72ed217846eee813986ca76a772d53a82
Author: Caideyipi <[email protected]>
AuthorDate: Tue Dec 1 20:22:15 2026 +0800
Fix concurrent table schema snapshots
---
.../consensus/response/table/DescTableResp.java | 9 +-
.../response/ConvertToThriftRespTest.java | 127 +++++++++++++++++++++
.../apache/iotdb/commons/schema/table/TsTable.java | 19 +--
3 files changed, 145 insertions(+), 10 deletions(-)
diff --git
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/response/table/DescTableResp.java
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/response/table/DescTableResp.java
index 7808c068899..b97150bdace 100644
---
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/response/table/DescTableResp.java
+++
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/response/table/DescTableResp.java
@@ -28,6 +28,7 @@ import org.apache.iotdb.consensus.common.DataSet;
import org.apache.tsfile.enums.TSDataType;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -44,9 +45,11 @@ public class DescTableResp implements DataSet {
final Set<String> preDeletedColumns,
final Map<String, TSDataType> preAlteredColumns) {
this.status = status;
- this.table = table;
- this.preDeletedColumns = preDeletedColumns;
- this.preAlteredColumns = preAlteredColumns;
+ this.table = Objects.isNull(table) ? null : new TsTable(table);
+ this.preDeletedColumns =
+ Objects.isNull(preDeletedColumns) ? null : new
HashSet<>(preDeletedColumns);
+ this.preAlteredColumns =
+ Objects.isNull(preAlteredColumns) ? null : new
HashMap<>(preAlteredColumns);
}
public TDescTableResp convertToTDescTableResp() {
diff --git
a/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/response/ConvertToThriftRespTest.java
b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/response/ConvertToThriftRespTest.java
index 37f22ed7d68..b52a893b2bf 100644
---
a/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/response/ConvertToThriftRespTest.java
+++
b/iotdb-core/confignode/src/test/java/org/apache/iotdb/confignode/consensus/response/ConvertToThriftRespTest.java
@@ -26,13 +26,19 @@ import org.apache.iotdb.common.rpc.thrift.TEndPoint;
import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.commons.exception.IllegalPathException;
import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.commons.schema.table.TsTableInternalRPCUtil;
+import org.apache.iotdb.commons.schema.table.column.FieldColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
import org.apache.iotdb.commons.trigger.TriggerInformation;
import org.apache.iotdb.commons.udf.UDFInformation;
import org.apache.iotdb.commons.udf.UDFType;
import
org.apache.iotdb.confignode.consensus.response.function.FunctionTableResp;
+import org.apache.iotdb.confignode.consensus.response.table.DescTableResp;
import
org.apache.iotdb.confignode.consensus.response.trigger.TransferringTriggersResp;
import
org.apache.iotdb.confignode.consensus.response.trigger.TriggerLocationResp;
import org.apache.iotdb.confignode.consensus.response.trigger.TriggerTableResp;
+import org.apache.iotdb.confignode.rpc.thrift.TDescTableResp;
import org.apache.iotdb.confignode.rpc.thrift.TGetLocationForTriggerResp;
import org.apache.iotdb.confignode.rpc.thrift.TGetTriggerTableResp;
import org.apache.iotdb.confignode.rpc.thrift.TGetUDFTableResp;
@@ -42,10 +48,22 @@ import org.apache.iotdb.trigger.api.enums.FailureStrategy;
import org.apache.iotdb.trigger.api.enums.TriggerEvent;
import org.apache.ratis.thirdparty.com.google.common.collect.ImmutableList;
+import org.apache.tsfile.enums.TSDataType;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
public class ConvertToThriftRespTest {
@Test
@@ -154,4 +172,113 @@ public class ConvertToThriftRespTest {
functionTableResp.getUdfInformation().get(1),
UDFInformation.deserialize(tGetUDFTableResp.allUDFInformation.get(1)));
}
+
+ @Test
+ public void convertDescTableRespSnapshotsMutableSchema() {
+ final TsTable table = new TsTable("table");
+ table.addColumnSchema(new FieldColumnSchema("field", TSDataType.INT32));
+ final Set<String> preDeletedColumns = new HashSet<>();
+ preDeletedColumns.add("field");
+ final Map<String, TSDataType> preAlteredColumns = new HashMap<>();
+ preAlteredColumns.put("field", TSDataType.DOUBLE);
+
+ final DescTableResp descTableResp =
+ new DescTableResp(
+ new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()),
+ table,
+ preDeletedColumns,
+ preAlteredColumns);
+
+ table.removeColumnSchema("field");
+ preDeletedColumns.clear();
+ preAlteredColumns.clear();
+
+ final TDescTableResp thriftResp = descTableResp.convertToTDescTableResp();
+ final TsTable tableSnapshot =
+
TsTableInternalRPCUtil.deserializeSingleTsTable(thriftResp.getTableInfo());
+ Assert.assertEquals(TSDataType.INT32,
tableSnapshot.getColumnSchema("field").getDataType());
+ Assert.assertEquals(Collections.singleton("field"),
thriftResp.getPreDeletedColumns());
+ Assert.assertEquals(
+ Byte.valueOf(TSDataType.DOUBLE.serialize()),
+ thriftResp.getPreAlteredColumns().get("field"));
+ }
+
+ @Test
+ public void convertDescTableRespWaitsForConcurrentSchemaMutation() throws
Exception {
+ final CountDownLatch copyStarted = new CountDownLatch(1);
+ final CountDownLatch continueCopy = new CountDownLatch(1);
+ final CountDownLatch mutationStarted = new CountDownLatch(1);
+ final AtomicReference<Thread> mutationThread = new AtomicReference<>();
+ final TsTable table = new TsTable("table");
+ table.addColumnSchema(new BlockingFieldColumnSchema(copyStarted,
continueCopy));
+ final ExecutorService executor = Executors.newFixedThreadPool(2);
+
+ try {
+ final Future<DescTableResp> responseFuture =
+ executor.submit(
+ () ->
+ new DescTableResp(
+ new
TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()),
+ table,
+ null,
+ null));
+ Assert.assertTrue(copyStarted.await(5, TimeUnit.SECONDS));
+
+ final Future<?> mutationFuture =
+ executor.submit(
+ () -> {
+ mutationThread.set(Thread.currentThread());
+ mutationStarted.countDown();
+ table.removeColumnSchema("field");
+ });
+ Assert.assertTrue(mutationStarted.await(5, TimeUnit.SECONDS));
+ final long mutationWaitDeadline = System.nanoTime() +
TimeUnit.SECONDS.toNanos(5);
+ while (!mutationFuture.isDone()
+ && mutationThread.get().getState() != Thread.State.WAITING
+ && System.nanoTime() < mutationWaitDeadline) {
+ Thread.yield();
+ }
+ Assert.assertFalse(mutationFuture.isDone());
+ Assert.assertEquals(Thread.State.WAITING,
mutationThread.get().getState());
+
+ continueCopy.countDown();
+ final TDescTableResp thriftResp =
+ responseFuture.get(5, TimeUnit.SECONDS).convertToTDescTableResp();
+ mutationFuture.get(5, TimeUnit.SECONDS);
+
+ final TsTable tableSnapshot =
+
TsTableInternalRPCUtil.deserializeSingleTsTable(thriftResp.getTableInfo());
+ Assert.assertNotNull(tableSnapshot.getColumnSchema("field"));
+ Assert.assertNull(table.getColumnSchema("field"));
+ } finally {
+ continueCopy.countDown();
+ executor.shutdownNow();
+ }
+ }
+
+ private static class BlockingFieldColumnSchema extends FieldColumnSchema {
+ private final CountDownLatch copyStarted;
+ private final CountDownLatch continueCopy;
+
+ private BlockingFieldColumnSchema(
+ final CountDownLatch copyStarted, final CountDownLatch continueCopy) {
+ super("field", TSDataType.INT32);
+ this.copyStarted = copyStarted;
+ this.continueCopy = continueCopy;
+ }
+
+ @Override
+ public TsTableColumnSchema copy() {
+ copyStarted.countDown();
+ try {
+ if (!continueCopy.await(5, TimeUnit.SECONDS)) {
+ throw new IllegalStateException("Timed out waiting to finish the
schema copy");
+ }
+ } catch (final InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new IllegalStateException(e);
+ }
+ return super.copy();
+ }
+ }
}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
index 3b9dc16e570..4a65de177fd 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
@@ -140,13 +140,18 @@ public class TsTable {
}
public TsTable(TsTable origin) {
- this.tableName = origin.tableName;
- origin.columnSchemaMap.forEach((col, schema) ->
this.columnSchemaMap.put(col, schema.copy()));
- this.tagColumnIndexMap.putAll(origin.tagColumnIndexMap);
- this.props = origin.props == null ? null : new HashMap<>(origin.props);
- this.ttlValue = origin.ttlValue;
- this.tagNums = origin.tagNums;
- this.fieldNum = origin.fieldNum;
+ origin.readWriteLock.readLock().lock();
+ try {
+ this.tableName = origin.tableName;
+ origin.columnSchemaMap.forEach((col, schema) ->
this.columnSchemaMap.put(col, schema.copy()));
+ this.tagColumnIndexMap.putAll(origin.tagColumnIndexMap);
+ this.props = origin.props == null ? null : new HashMap<>(origin.props);
+ this.ttlValue = origin.ttlValue;
+ this.tagNums = origin.tagNums;
+ this.fieldNum = origin.fieldNum;
+ } finally {
+ origin.readWriteLock.readLock().unlock();
+ }
}
public String getTableName() {