JackieTien97 commented on code in PR #18874:
URL: https://github.com/apache/iotdb/pull/18874#discussion_r4305472610
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/consensus/statemachine/schemaregion/SchemaExecutionVisitor.java:
##########
@@ -411,6 +428,23 @@ private void executeInternalCreateAlignedTimeSeries(
}
}
+ /**
+ * @param context the templates the node needs, which this visitor has
already resolved for its
+ * own use; null for the nodes which involve no template
+ */
+ private TSStatus triggerAddViewColumn(
Review Comment:
**[P1] The trigger runs in the schema-region state machine, so it fires per
replica and on replay: a column the user dropped comes back**
`triggerAddViewColumn` is called from `SchemaExecutionVisitor`, i.e. from
the apply path of the schema-region state machine. So it calls the ConfigNode
every time a replica applies an entry (on the leader, on every follower, and
again whenever an entry is re-applied), not once per statement. An entry
applied after the user has changed the view adds the column back:
- 1C3D, `schema_replication_factor=3`: follower DN3 stopped gracefully
(snapshot taken at stop and loaded at restart). While it was down: `create
timeseries root.c3.dlate.late1 INT32`, then `alter view v drop column late1`.
When DN3 rejoined and applied the entry, it ran an `AddViewColumnProcedure` and
`late1` was back in the view.
- 1C1D: `alter view v8 drop column zz`, then the DataNode is killed (crash,
OOM kill, `kill -9`; `sbin/windows/stop-datanode.bat` also uses `taskkill /f`)
and restarted. Replaying the entries after the last snapshot brings `zz` back;
that restart also produced 45 rolled-back procedures and 73 ERROR lines. In
1C3D, force-restarting one follower ran 21 procedures (20 rolled back) and
brought back the dropped `m5`.
A graceful stop + restart alone does not reproduce the second case, since
Ratis takes a snapshot at stop; the first case does not depend on how the node
stopped.
A state-machine apply shouldn't have external side effects. Suggest
triggering once per statement on the coordinator, after the schema-region write
has succeeded. That also covers the ordering issue in the thread at line 118
and removes the per-replica RPCs.
##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/view/CreateTableViewProcedure.java:
##########
@@ -116,13 +121,91 @@ protected void checkTableExistence(final
ConfigNodeProcedureEnv env) {
}
}
final TSStatus status =
- new TreeDeviceViewFieldDetector(env.getConfigManager(), table, null)
- .detectMissingFieldTypes();
+ new TreeDeviceViewFieldDetector(env.getConfigManager(),
table).detectFieldsForCreation();
if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
setFailure(new ProcedureException(new IoTDBException(status)));
}
}
+ @Override
+ protected TUpdateTableReq buildCommitReleaseTableReq() {
+ final boolean newAutoSync =
+ table
+ .getPropValue(TreeViewSchema.AUTO_SYNC_SCHEMA)
+ .map(Boolean::parseBoolean)
+ .orElse(false);
+ if (newAutoSync) {
+ // Whether the view is a new one, or replace the old table, just add view
+ final TUpdateTree2TableView viewInfo =
+ new
TUpdateTree2TableView(TreeToTableViewOperationType.ADD.getOperationType());
+
table.getPropValue(TreeViewSchema.TREE_PATH_PATTERN).ifPresent(viewInfo::setTreePath);
+ viewInfo.setIsRestrict(TreeViewSchema.isRestrict(table));
+ viewInfo.setTagNum(table.getTagNum());
+ return SchemaUtils.buildCommitUpdateTableReq(database,
table.getTableName(), null, viewInfo);
+ }
+ // Creating, or replacing with the auto detection turned off: the view the
old definition had
+ // registered has to go. drop a non-existing view is allowed.
+ final boolean oldAutoSync =
+ Objects.nonNull(oldView)
+ && oldView
+ .getPropValue(TreeViewSchema.AUTO_SYNC_SCHEMA)
+ .map(Boolean::parseBoolean)
+ .orElse(false);
+ if (oldAutoSync) {
+ return SchemaUtils.buildCommitUpdateTableReq(
+ database,
+ table.getTableName(),
+ null,
+ new
TUpdateTree2TableView(TreeToTableViewOperationType.REMOVE.getOperationType()));
+ }
+ return super.buildCommitReleaseTableReq();
+ }
+
+ /**
+ * A rolled-back replace restores the view the old definition had. A
DataNode which restarted
+ * while the replace was running does not hold it: the table such a DataNode
pulls is only
+ * pre-created, and a pre-created table does not enter the index.
Registering the old view again
+ * is idempotent, so a DataNode which never lost it is not affected.
+ */
+ @Override
+ protected TUpdateTableReq buildRollbackTableReq() {
+ final boolean oldAutoSync =
+ Objects.nonNull(oldView)
+ && oldView
+ .getPropValue(TreeViewSchema.AUTO_SYNC_SCHEMA)
+ .map(Boolean::parseBoolean)
+ .orElse(false);
+ if (!oldAutoSync) {
+ return super.buildRollbackTableReq();
+ }
+ final TUpdateTree2TableView viewInfo =
+ new
TUpdateTree2TableView(TreeToTableViewOperationType.ADD.getOperationType());
+
oldView.getPropValue(TreeViewSchema.TREE_PATH_PATTERN).ifPresent(viewInfo::setTreePath);
+ viewInfo.setIsRestrict(TreeViewSchema.isRestrict(oldView));
+ viewInfo.setTagNum(oldView.getTagNum());
+ return SchemaUtils.rollbackUpdateTableReq(database, table.getTableName(),
null, viewInfo);
+ }
+
+ @Override
+ protected void reconcileTree2TableViewColumns(final ConfigNodeProcedureEnv
env) {
+ // The series which already existed under the path pattern were created
before the DataNodes
+ // mounted the view, so they never triggered the addition of their column.
+ final List<TsTableColumnSchema> missingColumns =
+ TreeDeviceViewFieldDetector.detectMissingColumnsAfterCreation(
+ env.getConfigManager(), database, table);
+ if (missingColumns.isEmpty()) {
+ return;
+ }
+
+ addChildProcedure(
Review Comment:
**[P1] A failing catch-up child rolls back a view that is already committed
on the DataNodes**
This complements the thread at `TreeDeviceViewFieldDetector.java:140`, which
is what makes the child fail today. The catch-up is added as a child in
`COMMIT_RELEASE`, after the view has been committed on the ConfigNode and
released to the DataNodes (table cache and index). When the child fails, the
parent is rolled back on the ConfigNode only: `rollbackCreate` deletes the view
there, but on the DataNodes `rollbackUpdateTable` only clears the pre-update
entry, which the commit has already turned into a committed table, and for a
new view the rollback request carries no view info, so the index registration
made at commit-release stays.
Reproduced with only `root.sb.d.s1` (value 42): `create view vsb (device
TAG, renamed INT32 FIELD FROM s1) with (auto_sync_schema=true) as root.sb.**`
returns `552: Column 's1' already exist`. Afterwards `SHOW TABLES`, `DESC vsb`
and `information_schema` say the view does not exist, yet `select * from vsb`
still returns `d, 42`. The index keeps such a view too: after `vf_only` was
rolled back the same way, new series under its path kept calling the ConfigNode
(`Table 's5db.vf_only' does not exist`).
`SetViewPropertiesProcedure` (line 93) has the same shape: `alter view
vf_only2 set properties auto_sync_schema=true` fails with 552 and is rolled
back on the ConfigNode, yet the DataNode keeps syncing the view (`probe` was
added) until it restarts (`probe2` was not).
Suggestion: don't attach a child that can fail after the parent has been
released. Either detect the missing columns before `PRE_CREATE` and create the
view with them, run the catch-up as an independent procedure whose failure
doesn't undo the view, or make the rollback after release revert the DataNodes
as well.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/DataNodeInternalRPCServiceImpl.java:
##########
@@ -2898,6 +2928,62 @@ public TSStatus updateTemplate(final TUpdateTemplateReq
req) {
return RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS);
}
+ /**
+ * The devices which activated the template, each with the measurements the
extension added to it:
+ * the tree-to-table views covering these devices need them as columns.
+ *
+ * <p>The measurements come from comparing the new template with the old
one. The devices come
+ * from the schema regions, where every device which uses a template records
its id, see {@code
+ * EntityTraverser}; the template manager only knows where a template is
mounted, so it cannot
+ * tell which devices use it.
+ */
+ private Map<PartialPath, List<Pair<String, TSDataType>>>
collectDevicesUsingExtendedTemplate(
+ final Template oldTemplate, final Template newTemplate) throws Exception
{
+ final List<Pair<String, TSDataType>> extendedMeasurements =
+ newTemplate.getSchemaMap().values().stream()
+ .filter(
+ schema ->
+ Objects.isNull(oldTemplate)
+ ||
!oldTemplate.getSchemaMap().containsKey(schema.getMeasurementName()))
+ .map(schema -> new Pair<>(schema.getMeasurementName(),
schema.getType()))
+ .toList();
+ final Pair<Template, List<PartialPath>> templateSetInfo =
+
ClusterTemplateManager.getInstance().getAllPathsSetTemplate(newTemplate.getName());
+ final List<PartialPath> setPaths =
+ Objects.isNull(templateSetInfo) ? null : templateSetInfo.getRight();
+ if (extendedMeasurements.isEmpty() || Objects.isNull(setPaths)) {
+ return Collections.emptyMap();
+ }
+
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
= new HashMap<>();
+ Collection<ISchemaRegion> allSchemaRegions =
SchemaEngine.getInstance().getAllSchemaRegions();
+ for (final PartialPath setPath : setPaths) {
+ final String setPathString = setPath.getFullPath();
+ for (final ISchemaRegion schemaRegion : allSchemaRegions) {
+ // A device under this path can only live in the region of the
database the path belongs to.
+ if (!setPathString.startsWith(schemaRegion.getDatabaseFullPath())) {
+ continue;
+ }
+ try (final ISchemaReader<IDeviceSchemaInfo> deviceReader =
+ schemaRegion.getDeviceReader(
+ SchemaRegionReadPlanFactory.getShowDevicesPlan(
+
setPath.concatNode(IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD),
Review Comment:
**[P1] Template extension misses devices that activated the template at the
set path itself**
`setPath.**` only matches paths below `setPath`, so a device that activated
the template exactly at the set path is never collected, and the extended
measurements never reach the views covering it.
Reproduced: `create device template tF (f1 INT32)`, `set device template tF
to root.s6.gf.devx`, `create timeseries using device template on
root.s6.gf.devx`, all under an auto-sync view. `alter device template tF add
(f2 DOUBLE)` returns in 5 ms and `f2` is never added; a plain view created
afterwards over the same path has `f2`.
Please match `setPath` itself as well. This also applies to whatever
replaces this callback when the lock cycle in the thread at line 2912 is fixed.
##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TreeDeviceViewFieldDetector.java:
##########
@@ -166,59 +260,37 @@ private Map<TConsensusGroupId, TRegionReplicaSet>
getLatestSchemaRegionMap() {
private class TreeDeviceViewFieldDetectionTaskExecutor
extends DataNodeRegionTaskExecutor<TDeviceViewReq, TDeviceViewResp> {
- protected TreeDeviceViewFieldDetectionTaskExecutor(
- final ConfigManager configManager,
- final Map<TConsensusGroupId, TRegionReplicaSet> targetRegionGroup,
- final int tagNumber,
- final boolean restrict) {
- super(
- configManager,
- targetRegionGroup,
- false,
- CnToDnAsyncRequestType.DETECT_TREE_DEVICE_VIEW_FIELD_TYPE,
- ((dataNodeLocation, consensusGroupIdList) ->
- new TDeviceViewReq(
- consensusGroupIdList, Arrays.asList(path.getNodes()),
tagNumber, restrict)));
- configManager
- .getClusterSchemaManager()
- .getTemplateSetInfo(Collections.singletonList(path))
Review Comment:
**[P1] The detection change is intended, but the existing IT still asserts
the old behavior, and the description doesn't mention it**
Without the ConfigNode-side merge of set templates, a template that is set
but not activated no longer contributes fields, which matches "a view only
contains created series". It applies to every tree-to-table view though, not
only auto-sync ones, and `IoTDBTableIT#testTreeViewTable` (lines 963–980) still
expects `614: Multiple types encountered ... 'S1'` from the unactivated
template `t1`'s `S1 BOOLEAN`. On this head the test fails at
`IoTDBTableIT.java:965`, while master passes on the same machine; the PR's CI
hasn't run yet (all workflows are awaiting approval). With only those lines
changed to the new semantics, the whole test passes on this head:
```java
// Template t1 is set but not activated, so its S1 BOOLEAN is not a
created series and does
// not take part in the detection
statement.execute("create view tree_table (tag1 tag, tag2 tag) as
root.\"重庆\".\"1\".**");
statement.execute("drop view tree_table");
statement.execute(
"create view tree_table (tag1 tag, tag2 tag, S1 field) as
root.\"重庆\".\"1\".**");
statement.execute("drop view tree_table");
```
Please also state the change in the PR description: plain views no longer
get the fields of unactivated templates; a typeless `FIELD` that exists only in
such a template now fails with `528: Measurements not found`; type conflicts
between such a template and existing series are no longer reported at CREATE
VIEW.
##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TreeViewSchema.java:
##########
@@ -35,6 +35,7 @@ public class TreeViewSchema {
public static final String RESTRICT = "__restrict";
public static final String UNSUPPORTED_NEED_LAST_CACHE_PROPERTY =
"The tree view does not support need_last_cache property.";
+ public static final String AUTO_SYNC_SCHEMA = "auto_sync_schema";
Review Comment:
**[P3] `auto_sync_schema` is not visible anywhere**
SHOW CREATE VIEW doesn't include it in `WITH (...)` the way it includes
`ttl`, so recreating a view from its SHOW CREATE output silently drops it; SHOW
TABLES DETAILS and `information_schema.tables` don't show it either, so users
can't tell which views auto-sync.
##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/schema/TreeDeviceViewFieldDetector.java:
##########
@@ -64,98 +65,191 @@ public class TreeDeviceViewFieldDetector {
private final ConfigManager configManager;
private final PartialPath path;
private final TsTable table;
- private final Map<String, Set<FieldColumnSchema>> fields;
private TDeviceViewResp result = new TDeviceViewResp(StatusUtils.OK, new
ConcurrentHashMap<>());
private final Map<String, String> lowerCase2OriginalMap = new HashMap<>();
- public TreeDeviceViewFieldDetector(
- final ConfigManager configManager,
- final TsTable table,
- final Map<String, Set<FieldColumnSchema>> fields) {
+ public TreeDeviceViewFieldDetector(final ConfigManager configManager, final
TsTable table) {
this.configManager = configManager;
this.path = TreeViewSchema.getPrefixPattern(table);
this.table = table;
- this.fields = fields;
}
- public TSStatus detectMissingFieldTypes() {
- if (table.getFieldNum() == 0 && Objects.isNull(fields)) {
+ /**
+ * Fills the fields of the view which is being created: a view may declare
no field at all, in
+ * which case every field under its tree path pattern becomes a column, or
declare fields whose
+ * type is left to the detection.
+ */
+ public TSStatus detectFieldsForCreation() {
+ if (table.getFieldNum() == 0) {
+ // for the first time creation of the table, user do not assign any
field;
+ // just detect all the fields of prefix path
new TreeDeviceViewFieldDetectionTaskExecutor(
configManager,
getLatestSchemaRegionMap(),
table.getTagNum(),
- TreeViewSchema.isRestrict(table))
+ TreeViewSchema.isRestrict(table),
+ null,
+ false)
.execute();
if (result.getStatus().getCode() !=
TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
return result.getStatus();
}
result
.getDeviewViewFieldTypeMap()
.forEach(
- (field, type) -> {
- final FieldColumnSchema columnSchema =
- new FieldColumnSchema(field,
TSDataType.getTsDataType(type));
- if (!field.equals(lowerCase2OriginalMap.get(field))) {
- TreeViewSchema.setOriginalName(columnSchema,
lowerCase2OriginalMap.get(field));
- }
- table.addColumnSchema(columnSchema);
- });
- } else {
- final Map<String, Set<FieldColumnSchema>> unknownFields;
- if (Objects.isNull(fields)) {
- unknownFields = new HashMap<>();
- for (final TsTableColumnSchema schema : table.getColumnList()) {
- if (!(schema instanceof FieldColumnSchema)
- || schema.getDataType() != TSDataType.UNKNOWN) {
- continue;
- }
- final String key = TreeViewSchema.getSourceName(schema);
- if (!unknownFields.containsKey(key)) {
- unknownFields.put(key, new HashSet<>());
- }
- unknownFields.get(key).add((FieldColumnSchema) schema);
- }
- } else {
- unknownFields = fields;
- }
+ (field, type) ->
+
table.addColumnSchema(toFieldColumnSchemaWithOriginName(field, type)));
+ return StatusUtils.OK;
+ }
+
+ return detectFieldTypes(collectUnknownFieldColumns());
+ }
+
+ /**
+ * Resolves the type of the field columns which are being added to an
existing view. The columns
+ * to resolve are given by the caller, as only it knows which ones it is
adding.
+ */
+ public TSStatus detectFieldTypesForColumnAddition(final Map<String,
FieldColumnSchema> fields) {
+ return detectFieldTypes(fields);
+ }
+
+ /**
+ * The fields which exist under the view's tree path pattern but are not
columns of the view yet,
+ * as the columns to add. Used after a view is created, to pick up the
series which were created
+ * before the view was mounted on the DataNodes, and which the DataNodes
therefore never reported.
+ */
+ public Pair<TSStatus, List<TsTableColumnSchema>>
detectNewFieldColumnsAfterCreation() {
+ new TreeDeviceViewFieldDetectionTaskExecutor(
+ configManager,
+ getLatestSchemaRegionMap(),
+ table.getTagNum(),
+ TreeViewSchema.isRestrict(table),
+ null,
+ true)
Review Comment:
**[P3] The catch-up silently accepts type conflicts that a plain CREATE VIEW
rejects**
The catch-up runs the detection with `tolerateTypeMismatch = true`, so when
a measurement has different types on different devices, it takes whichever type
is reported first. A plain CREATE VIEW over the same data fails with `614:
Multiple types encountered ... 's1'`.
Reproduced with the same tree data in two databases: the catch-up chose
INT64 in one and TEXT in the other; the other device's values are then
unreadable through the view, or read as strings.
Suggest handling it like the plain detection (skip the column and report
it), or at least choosing deterministically and logging the conflict.
##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java:
##########
@@ -2268,6 +2281,159 @@ public TSStatus alterTableAddColumn(final
TAlterOrDropTableReq req) {
false));
}
+ public TUpdateTree2TableViewsResp
addColumnForTree2TableViews(TUpdateTree2TableViewsReq req) {
+ if (req == null || !req.isSetViewInfos()) {
+ return new
TUpdateTree2TableViewsResp(RpcUtils.getStatus(TSStatusCode.ILLEGAL_PARAMETER));
+ }
+
+ final List<TUpdateTree2TableViewFailure> failedViews = new ArrayList<>();
+ final List<Pair<TAlterOrDropTableReq, Procedure<ConfigNodeProcedureEnv>>>
procedures =
+ new ArrayList<>();
+ // The requests which overlapped with another task on the same table,
together with the status
+ // of the last submission attempt, see processOverlappingTask.
+ final List<Pair<TAlterOrDropTableReq, TSStatus>> overlappingReqs = new
ArrayList<>();
+
+ // Submit every view before waiting, so procedures for different views can
run concurrently.
+ for (final TAlterOrDropTableReq addColumnReq : req.getViewInfos()) {
+ if (!isAddViewColumnReq(addColumnReq)) {
+ failedViews.add(illegalAddViewColumnFailure(addColumnReq));
+ continue;
+ }
+
+ final TSStatus submissionStatus = submitAddViewColumn(addColumnReq,
procedures, failedViews);
+ if (submissionStatus.getCode() ==
TSStatusCode.OVERLAP_WITH_EXISTING_TASK.getStatusCode()) {
+ overlappingReqs.add(new Pair<>(addColumnReq, submissionStatus));
+ }
+ }
+
+ processOverlappingTask(overlappingReqs, procedures, failedViews);
+
+ final long waitDeadline = System.currentTimeMillis() +
ADD_VIEW_COLUMN_WAIT_TIME_OUT_MS;
+ for (final Pair<TAlterOrDropTableReq, Procedure<ConfigNodeProcedureEnv>>
procedure :
+ procedures) {
+ final long remainingWaitTime = Math.max(0, waitDeadline -
System.currentTimeMillis());
+ final TSStatus status = waitingProcedureFinished(procedure.getRight(),
remainingWaitTime);
+ if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ failedViews.add(
+ new TUpdateTree2TableViewFailure(
+ procedure.getLeft().database, procedure.getLeft().tableName,
status));
+ }
+ }
+
+ final TSStatus status = failedViews.isEmpty() ? StatusUtils.OK :
StatusUtils.INTERNAL_ERROR;
+ final TUpdateTree2TableViewsResp response = new
TUpdateTree2TableViewsResp(status);
+ if (!failedViews.isEmpty()) {
+ response.setFailedViews(failedViews);
+ }
+ return response;
+ }
+
+ /** Whether the request carries everything an add-column operation on a view
needs. */
+ private static boolean isAddViewColumnReq(final @Nullable
TAlterOrDropTableReq addColumnReq) {
+ return Objects.nonNull(addColumnReq)
+ && addColumnReq.isSetDatabase()
+ && addColumnReq.isSetTableName()
+ && addColumnReq.isSetQueryId()
+ && addColumnReq.isSetOperationType()
+ && addColumnReq.getOperationType()
+ == AlterOrDropTableOperationType.ADD_COLUMN.getTypeValue()
+ && addColumnReq.isSetUpdateInfo()
+ && addColumnReq.isSetIsView()
+ && addColumnReq.isIsView();
+ }
+
+ private static TUpdateTree2TableViewFailure illegalAddViewColumnFailure(
+ final TAlterOrDropTableReq addColumnReq) {
+ return new TUpdateTree2TableViewFailure(
+ Objects.isNull(addColumnReq) ? "" :
Objects.toString(addColumnReq.getDatabase(), ""),
+ Objects.isNull(addColumnReq) ? "" :
Objects.toString(addColumnReq.getTableName(), ""),
+ RpcUtils.getStatus(TSStatusCode.ILLEGAL_PARAMETER));
+ }
+
+ /**
+ * Submits the add-column procedure of one view and settles everything but
an overlap with another
+ * task on the same table.
+ */
+ private TSStatus submitAddViewColumn(
+ final TAlterOrDropTableReq addColumnReq,
+ final List<Pair<TAlterOrDropTableReq,
Procedure<ConfigNodeProcedureEnv>>> procedures,
+ final List<TUpdateTree2TableViewFailure> failedViews) {
+ final AddViewColumnProcedure procedure;
+ procedure =
+ new AddViewColumnProcedure(
+ addColumnReq.database,
+ addColumnReq.tableName,
+ addColumnReq.queryId,
+
TsTableColumnSchemaUtil.deserializeColumnSchemaList(addColumnReq.updateInfo),
+ false);
+
+ final Pair<TSStatus, Long> submissionResult =
+ executeWithoutDuplicateWithoutWaiting(
+ addColumnReq.database,
+ null,
+ addColumnReq.tableName,
+ null,
+ addColumnReq.queryId,
+ ProcedureType.ADD_VIEW_COLUMN_PROCEDURE,
+ procedure);
+ final TSStatus status = submissionResult.getLeft();
+ if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ procedures.add(new Pair<>(addColumnReq, procedure));
+ } else if (status.getCode() ==
TSStatusCode.REPEATED_WITH_EXISTING_TASK.getStatusCode()) {
+ final Procedure<ConfigNodeProcedureEnv> repeatedProcedure =
+ executor.getProcedures().get(submissionResult.getRight());
+ // null means the procedure could have finished at this moment
+ if (Objects.isNull(repeatedProcedure)) {
+ failedViews.add(
+ new TUpdateTree2TableViewFailure(
+ addColumnReq.database,
+ addColumnReq.tableName,
+ RpcUtils.getStatus(TSStatusCode.INTERNAL_SERVER_ERROR)));
+ } else {
+ procedures.add(new Pair<>(addColumnReq, repeatedProcedure));
+ }
+ } else if (status.getCode() !=
TSStatusCode.OVERLAP_WITH_EXISTING_TASK.getStatusCode()) {
+ failedViews.add(
+ new TUpdateTree2TableViewFailure(addColumnReq.database,
addColumnReq.tableName, status));
+ }
+ return status;
+ }
+
+ /**
+ * Retries the requests which overlapped with another task on the same
table, A request which
+ * still overlaps after the last round is reported as a failure, with the
status of its last
+ * attempt.
+ */
+ private void processOverlappingTask(
+ final List<Pair<TAlterOrDropTableReq, TSStatus>> overlappingReqs,
+ final List<Pair<TAlterOrDropTableReq,
Procedure<ConfigNodeProcedureEnv>>> procedures,
+ final List<TUpdateTree2TableViewFailure> failedViews) {
+ List<Pair<TAlterOrDropTableReq, TSStatus>> pending = overlappingReqs;
+ for (int round = 0; round < OVERLAP_RETRY_ROUNDS && !pending.isEmpty();
round++) {
+ try {
+ Thread.sleep(OVERLAP_RETRY_WAIT_MS);
Review Comment:
**[P2] The overlap retry sleeps in the RPC thread while the schema-region
state machine waits**
An overlapping request sleeps 2 s per round, up to 4 rounds, and then waits
up to 30 s for the procedure. Meanwhile the calling DataNode is blocked inside
the state-machine apply, so every later schema write of that region queues
behind it.
Measured with 4 schema regions, 8 threads × 5 new measurements: master total
0.64–0.96 s, p90 0.17–0.47 s; this PR total 13–15.7 s, p50 0.4–3.0 s, p90
3.4–6.2 s, max 9.3 s (including the retry failure at line 2367).
Suggest not blocking here at all, e.g. merge the columns into the pending
procedure of the same view, or queue them on the ConfigNode and return; this
works best with the trigger moved out of the state machine (see
`SchemaExecutionVisitor.java:435`).
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/consensus/statemachine/schemaregion/SchemaExecutionVisitor.java:
##########
@@ -485,17 +521,20 @@ public TSStatus visitActivateTemplate(
@Override
public TSStatus visitBatchActivateTemplate(
final BatchActivateTemplateNode node, final ISchemaRegion schemaRegion) {
+ final Map<Integer, Template> templateMap = new HashMap<>();
+ final Set<Integer> missingTemplateIds = new HashSet<>();
+ collectTemplates(node.getTemplateActivationMap(), templateMap,
missingTemplateIds);
+
+ triggerAddViewColumn(node, new
TriggerAddViewColumnVisitor.Context(templateMap));
+
final List<TSStatus> statusList = new ArrayList<>();
final List<PartialPath> alreadyActivatedDeviceList = new ArrayList<>();
for (final Map.Entry<PartialPath, Pair<Integer, Integer>> entry :
node.getTemplateActivationMap().entrySet()) {
- final Template template =
-
ClusterTemplateManager.getInstance().getTemplate(entry.getValue().left);
+ final Template template = templateMap.get(entry.getValue().left);
if (Objects.isNull(template)) {
- return new TSStatus(TSStatusCode.UNDEFINED_TEMPLATE.getStatusCode())
- .setMessage(
- DataNodeMiscMessages
-
.MESSAGE_TEMPLATE_NULL_TRYING_ACTIVATE_TEMPLATE_MAY_TEMPLATE_BEING_UNSET_1CE92779);
+ // its id is in missingTemplateIds, which is reported at the end
+ continue;
Review Comment:
**[P3] Two behavior changes beyond the feature, not mentioned in the
description** (from reading the code)
1. Batch template activation, here and in
`visitInternalBatchActivateTemplate`: master returns `UNDEFINED_TEMPLATE` at
the first device whose template is missing; this PR skips those devices,
activates the others and reports the missing ids at the end.
2. The commit-release of every table DDL (create table, add / rename / alter
column, drop table, set properties) now goes through
`SchemaUtils.commitReleaseTableWithHA` (`SchemaUtils.java:285`), i.e. waits
until each unreachable DataNode is proven fenced, where master broadcast and
only logged failures. Its javadoc ties this to the view index, but it changes
the behavior of all table DDL.
They may well be fine, but please call them out in the description, or move
them to a separate PR.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/table/TriggerAddViewColumnVisitor.java:
##########
@@ -0,0 +1,389 @@
+/*
+ * 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 org.apache.iotdb.db.schemaengine.table;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.client.AbstractConfigNodeInfo;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.consensus.ConfigRegionId;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.schema.table.AlterOrDropTableOperationType;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchemaUtil;
+import org.apache.iotdb.commons.schema.template.Template;
+import org.apache.iotdb.confignode.rpc.thrift.TAlterOrDropTableReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewFailure;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsResp;
+import org.apache.iotdb.db.i18n.DataNodeSchemaMessages;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClient;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClientManager;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanVisitor;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.ActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.BatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateAlignedTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalBatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.MeasurementGroup;
+import
org.apache.iotdb.db.queryengine.plan.relational.metadata.fetcher.TableHeaderSchemaValidator;
+import
org.apache.iotdb.db.schemaengine.schemaregion.view.TreeToTableViewReference;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HexFormat;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Adds the columns which the tree-to-table views covering the created
timeseries need.
+ *
+ * <p>The trigger is a best-effort side effect of the schema creation, see
addViewColumnsIfNeeded.
+ */
+public class TriggerAddViewColumnVisitor
+ implements PlanVisitor<TSStatus, TriggerAddViewColumnVisitor.Context> {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(TriggerAddViewColumnVisitor.class);
+ private static final IClientManager<ConfigRegionId, ConfigNodeClient>
CONFIG_NODE_CLIENT_MANAGER =
+ ConfigNodeClientManager.getInstance();
+
+ public static final String QUERY_ID_PREFIX = "tree_to_table_add_column";
+ public static final String ALTER_DEVICE_TEMPLATE_ID_PREFIX = "template_";
+
+ public static final class Context {
+
+ private final Map<Integer, Template> templates;
+
+ public Context(Map<Integer, Template> templates) {
+ this.templates = Objects.isNull(templates) ? Collections.emptyMap() :
templates;
+ }
+
+ public Map<Integer, Template> getTemplates() {
+ return templates;
+ }
+ }
+
+ /**
+ * Adds the columns the views covering the given devices need.
+ *
+ * <p>It is a best-effort side effect of the write which triggers it: every
failure is logged here
+ * and none of them is propagated, so that the write operation is never
affected. The columns
+ * which are not added this time are added by a later write of the same
measurements, as every
+ * write plan carries its own planNodeId, hence its own queryId.
+ *
+ * @param device2Measurements eg. device01 -> {s1, s2, s3}; device02 -> {s3,
s4}
+ * @param operationId the planNodeId of the plan which triggers this
operation
+ */
+ public static void addViewColumnsIfNeeded(
+ Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements,
String operationId) {
+ if (device2Measurements == null || device2Measurements.isEmpty()) {
+ return;
+ }
+ final Map<TreeToTableViewReference, Set<Pair<String, TSDataType>>>
view2Measurements =
+ collectViewMeasurements(device2Measurements);
+ if (view2Measurements.isEmpty()) {
+ return;
+ }
+
+ try (ConfigNodeClient configNodeClient =
+
CONFIG_NODE_CLIENT_MANAGER.borrowClient(AbstractConfigNodeInfo.CONFIG_REGION_ID))
{
+ List<TAlterOrDropTableReq> updateViewReqs =
+ view2Measurements.entrySet().stream()
+ .map(entry -> TriggerAddViewColumnVisitor.toAlterTableReq(entry,
operationId))
+ .toList();
+
+ TUpdateTree2TableViewsResp resp =
+ configNodeClient.updateTree2TableViews(new
TUpdateTree2TableViewsReq(updateViewReqs));
+ if (resp == null || resp.getStatus() == null) {
+
LOGGER.error(DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_EMPTY_RESPONSE,
operationId);
+ return;
+ }
+ if (resp.getStatus().getCode() !=
TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ LOGGER.error(
+ DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_CN_FAILED, operationId,
resp.getStatus());
+ }
+ // the views are updated one by one in ConfigNode, so the failed ones
are reported one by one
+ if (resp.getFailedViews() != null) {
+ for (TUpdateTree2TableViewFailure failedView : resp.getFailedViews()) {
+ LOGGER.error(
+ DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_FAILED_FOR_VIEW,
+ failedView.getDatabase(),
+ failedView.getTableName(),
+ operationId,
+ failedView.getStatus());
+ }
+ }
+ } catch (ClientManagerException | TException e) {
+ LOGGER.warn(DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_CN_UNREACHABLE,
operationId, e);
+ }
+ }
+
+ /**
+ * @return the measurements each view covering the given devices needs; eg:
view01 -> {s1, s2,
+ * s3}, view02 -> {s1, s2, s3, s4}
+ */
+ private static Map<TreeToTableViewReference, Set<Pair<String, TSDataType>>>
+ collectViewMeasurements(
+ Map<PartialPath, List<Pair<String, TSDataType>>>
device2Measurements) {
+
+ // deduplicate the measurement by set, the measurement with same name and
dataType
+ // from different device would count once, eg: dev01.s1(int32) and
dev02.s1(int32)
+ // other deduplication would leave to ConfigMTree in CN
Review Comment:
**[P2] Every series creation under a covered path calls the ConfigNode and
persists a procedure, even when no column is missing**
`collectViewMeasurements` only checks which views cover the device; it never
looks at the columns the view already has in the DataNode table cache (as this
comment says, deduplication is left to the ConfigNode). `submitAddViewColumn`
then submits an `AddViewColumnProcedure` without a pre-check; it is rejected in
`COLUMN_CHECK` and rolled back.
Measured: creating 100 new devices that only use the existing measurements
`s1, s2` takes 496 / 539 ms on master and 2015 / 2191 ms with this PR. Over
three such runs (300 devices) the ConfigNode rolled back exactly 300
procedures, all `Column 's1, s2' already exist`, and each device left 2 ERROR
lines on the DataNode. With 3 replicas every replica sends the RPC (10 devices
→ 10 rolled-back procedures, 60 ERROR lines).
Suggest filtering on the DataNode against the cached view by source
measurement (a `FROM` column counts by its original name) and skipping the RPC
when nothing is missing; and on the ConfigNode, returning success without a
procedure when every requested column already exists.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/table/DataNodeTableCache.java:
##########
@@ -370,6 +419,60 @@ public void commitUpdateTable(
}
}
+ /**
+ * Applies the effect of a committed change on the tree-to-table view index.
Every attribute the
+ * operation needs comes with the request: the table this DataNode caches
may be a placeholder, so
+ * the entry cannot be computed from it.
+ */
+ private void operateOnTree2TableViewCache(
+ String database,
+ String tableName,
+ @Nullable String oldTableName,
+ TUpdateTree2TableView updateTree2TableViewInfo) {
+ if (Objects.isNull(updateTree2TableViewInfo)) {
+ return;
+ }
+ final TreeToTableViewOperationType type =
+
TreeToTableViewOperationType.getType(updateTree2TableViewInfo.getTree2TableOperationType());
+ try {
+ switch (type) {
+ case REMOVE:
+ // Unregistering a view needs nothing but its name.
+ treeToTableViewCache.removeView(database, tableName);
Review Comment:
**[P1] Dropping a table-model database leaves its auto-sync views in the
index**
Views only leave the tree-to-table view index through this `REMOVE`.
Dropping the database goes through `invalid(database)` (line 510, unchanged),
which clears `databaseTableMap` and `specialStatusMap` but not
`treeToTableViewCache`, so the views of a dropped database stay registered
until the DataNode restarts.
Reproduced: in database `s4x`, `create view vstale (dev TAG) with
(auto_sync_schema=true) as root.s4.**`, then `drop database s4x`. New series
under `root.s4` still call the ConfigNode for the dropped view (`Database is
not set ... root.s4x`). After `create database s4x` and `create view vstale
(dev TAG) as root.s4.**`, this time without auto-sync, `create timeseries
root.s4.d8.leak INT32` still added `leak` to the new view.
Please unregister the views of a database when it is dropped.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/table/TriggerAddViewColumnVisitor.java:
##########
@@ -0,0 +1,389 @@
+/*
+ * 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 org.apache.iotdb.db.schemaengine.table;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.client.AbstractConfigNodeInfo;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.consensus.ConfigRegionId;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.schema.table.AlterOrDropTableOperationType;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchemaUtil;
+import org.apache.iotdb.commons.schema.template.Template;
+import org.apache.iotdb.confignode.rpc.thrift.TAlterOrDropTableReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewFailure;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsResp;
+import org.apache.iotdb.db.i18n.DataNodeSchemaMessages;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClient;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClientManager;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanVisitor;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.ActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.BatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateAlignedTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalBatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.MeasurementGroup;
+import
org.apache.iotdb.db.queryengine.plan.relational.metadata.fetcher.TableHeaderSchemaValidator;
+import
org.apache.iotdb.db.schemaengine.schemaregion.view.TreeToTableViewReference;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HexFormat;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Adds the columns which the tree-to-table views covering the created
timeseries need.
+ *
+ * <p>The trigger is a best-effort side effect of the schema creation, see
addViewColumnsIfNeeded.
+ */
+public class TriggerAddViewColumnVisitor
+ implements PlanVisitor<TSStatus, TriggerAddViewColumnVisitor.Context> {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(TriggerAddViewColumnVisitor.class);
+ private static final IClientManager<ConfigRegionId, ConfigNodeClient>
CONFIG_NODE_CLIENT_MANAGER =
+ ConfigNodeClientManager.getInstance();
+
+ public static final String QUERY_ID_PREFIX = "tree_to_table_add_column";
+ public static final String ALTER_DEVICE_TEMPLATE_ID_PREFIX = "template_";
+
+ public static final class Context {
+
+ private final Map<Integer, Template> templates;
+
+ public Context(Map<Integer, Template> templates) {
+ this.templates = Objects.isNull(templates) ? Collections.emptyMap() :
templates;
+ }
+
+ public Map<Integer, Template> getTemplates() {
+ return templates;
+ }
+ }
+
+ /**
+ * Adds the columns the views covering the given devices need.
+ *
+ * <p>It is a best-effort side effect of the write which triggers it: every
failure is logged here
+ * and none of them is propagated, so that the write operation is never
affected. The columns
+ * which are not added this time are added by a later write of the same
measurements, as every
+ * write plan carries its own planNodeId, hence its own queryId.
+ *
+ * @param device2Measurements eg. device01 -> {s1, s2, s3}; device02 -> {s3,
s4}
+ * @param operationId the planNodeId of the plan which triggers this
operation
+ */
+ public static void addViewColumnsIfNeeded(
+ Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements,
String operationId) {
+ if (device2Measurements == null || device2Measurements.isEmpty()) {
+ return;
+ }
+ final Map<TreeToTableViewReference, Set<Pair<String, TSDataType>>>
view2Measurements =
+ collectViewMeasurements(device2Measurements);
+ if (view2Measurements.isEmpty()) {
+ return;
+ }
+
+ try (ConfigNodeClient configNodeClient =
+
CONFIG_NODE_CLIENT_MANAGER.borrowClient(AbstractConfigNodeInfo.CONFIG_REGION_ID))
{
+ List<TAlterOrDropTableReq> updateViewReqs =
+ view2Measurements.entrySet().stream()
+ .map(entry -> TriggerAddViewColumnVisitor.toAlterTableReq(entry,
operationId))
+ .toList();
+
+ TUpdateTree2TableViewsResp resp =
+ configNodeClient.updateTree2TableViews(new
TUpdateTree2TableViewsReq(updateViewReqs));
+ if (resp == null || resp.getStatus() == null) {
+
LOGGER.error(DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_EMPTY_RESPONSE,
operationId);
+ return;
+ }
+ if (resp.getStatus().getCode() !=
TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ LOGGER.error(
+ DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_CN_FAILED, operationId,
resp.getStatus());
+ }
+ // the views are updated one by one in ConfigNode, so the failed ones
are reported one by one
+ if (resp.getFailedViews() != null) {
+ for (TUpdateTree2TableViewFailure failedView : resp.getFailedViews()) {
+ LOGGER.error(
+ DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_FAILED_FOR_VIEW,
+ failedView.getDatabase(),
+ failedView.getTableName(),
+ operationId,
+ failedView.getStatus());
+ }
+ }
+ } catch (ClientManagerException | TException e) {
+ LOGGER.warn(DataNodeSchemaMessages.LOG_ADD_VIEW_COLUMN_CN_UNREACHABLE,
operationId, e);
+ }
+ }
+
+ /**
+ * @return the measurements each view covering the given devices needs; eg:
view01 -> {s1, s2,
+ * s3}, view02 -> {s1, s2, s3, s4}
+ */
+ private static Map<TreeToTableViewReference, Set<Pair<String, TSDataType>>>
+ collectViewMeasurements(
+ Map<PartialPath, List<Pair<String, TSDataType>>>
device2Measurements) {
+
+ // deduplicate the measurement by set, the measurement with same name and
dataType
+ // from different device would count once, eg: dev01.s1(int32) and
dev02.s1(int32)
+ // other deduplication would leave to ConfigMTree in CN
+ final Map<TreeToTableViewReference, Set<Pair<String, TSDataType>>>
view2Measurements =
+ new HashMap<>();
+
+ for (Map.Entry<PartialPath, List<Pair<String, TSDataType>>> entry :
+ device2Measurements.entrySet()) {
+ PartialPath path = entry.getKey();
+ Set<TreeToTableViewReference> treeToTableViews =
+ TreeToTableViewCache.getInstance().getTreeToTableViews(path);
+
+ treeToTableViews.forEach(
+ view ->
+ view2Measurements
+ .computeIfAbsent(view, key -> new LinkedHashSet<>())
+ .addAll(entry.getValue()));
+ }
+ return view2Measurements;
+ }
+
+ private static TAlterOrDropTableReq toAlterTableReq(
+ Map.Entry<TreeToTableViewReference, Set<Pair<String, TSDataType>>>
view2Measurements,
+ String operationId) {
+ final TreeToTableViewReference view = view2Measurements.getKey();
+ final List<TsTableColumnSchema> columnSchemas =
+ view2Measurements.getValue().stream()
+ .map(TriggerAddViewColumnVisitor::toColumnSchema)
+ .toList();
+ TAlterOrDropTableReq tAlterOrDropTableReq =
+ new TAlterOrDropTableReq(
+ view.getDatabaseName(),
+ view.getTableName(),
+ buildQueryId(
+ operationId + "_" + columnFingerprint(columnSchemas),
+ view.getDatabaseName(),
+ view.getTableName()),
+ AlterOrDropTableOperationType.ADD_COLUMN.getTypeValue(),
+ ByteBuffer.wrap(TsTableColumnSchemaUtil.serialize(columnSchemas)));
+ tAlterOrDropTableReq.setIsView(true);
+ return tAlterOrDropTableReq;
+ }
+
+ // transform the upperCase to the from-attribute
+ private static TsTableColumnSchema toColumnSchema(final Pair<String,
TSDataType> measurement) {
+ final String measurementName = measurement.getLeft();
+ final String from =
+ measurementName.chars().anyMatch(Character::isUpperCase) ?
measurementName : null;
+ return TableHeaderSchemaValidator.generateColumnSchema(
+ TsTableColumnCategory.FIELD,
+ measurementName.toLowerCase(Locale.ROOT),
+ measurement.getRight(),
+ null,
+ from);
+ }
+
+ @Override
+ public TSStatus visitCreateTimeSeries(
+ CreateTimeSeriesNode node, TriggerAddViewColumnVisitor.Context context) {
+ final MeasurementPath path = node.getPath();
+ final Pair<String, TSDataType> measurement =
+ new Pair<>(path.getMeasurement(), node.getDataType());
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ Collections.singletonMap(path.getDevicePath(),
Collections.singletonList(measurement));
+ addViewColumnsIfNeeded(device2Measurements, node.getPlanNodeId().getId());
+ return null;
+ }
+
+ @Override
+ public TSStatus visitCreateAlignedTimeSeries(
+ CreateAlignedTimeSeriesNode node, TriggerAddViewColumnVisitor.Context
context) {
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ Collections.singletonMap(
+ node.getDevicePath(), toMeasurements(node.getMeasurements(),
node.getDataTypes()));
+ addViewColumnsIfNeeded(device2Measurements, node.getPlanNodeId().getId());
+ return null;
+ }
+
+ @Override
+ public TSStatus visitCreateMultiTimeSeries(
+ CreateMultiTimeSeriesNode node, TriggerAddViewColumnVisitor.Context
context) {
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ toDevice2Measurements(node.getMeasurementGroupMap());
+ addViewColumnsIfNeeded(device2Measurements, node.getPlanNodeId().getId());
+ return null;
+ }
+
+ @Override
+ public TSStatus visitInternalCreateTimeSeries(
+ InternalCreateTimeSeriesNode node, TriggerAddViewColumnVisitor.Context
context) {
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ Collections.singletonMap(node.getDevicePath(),
toMeasurements(node.getMeasurementGroup()));
+ addViewColumnsIfNeeded(device2Measurements, node.getPlanNodeId().getId());
+ return null;
+ }
+
+ @Override
+ public TSStatus visitInternalCreateMultiTimeSeries(
+ InternalCreateMultiTimeSeriesNode node,
TriggerAddViewColumnVisitor.Context context) {
+ // the alignment flag is of no use to a view, only the measurements of the
devices matter
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ new HashMap<>(node.getDeviceMap().size());
+ for (final Map.Entry<PartialPath, Pair<Boolean, MeasurementGroup>> entry :
+ node.getDeviceMap().entrySet()) {
+ device2Measurements.put(entry.getKey(),
toMeasurements(entry.getValue().getRight()));
+ }
+ addViewColumnsIfNeeded(device2Measurements, node.getPlanNodeId().getId());
+ return null;
+ }
+
+ @Override
+ public TSStatus visitActivateTemplate(
+ ActivateTemplateNode node, TriggerAddViewColumnVisitor.Context context) {
+ final Template template = context.getTemplates().get(node.getTemplateId());
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ Collections.singletonMap(node.getActivatePath(),
toMeasurements(template));
+ addViewColumnsIfNeeded(device2Measurements, node.getPlanNodeId().getId());
+ return null;
+ }
+
+ @Override
+ public TSStatus visitBatchActivateTemplate(
+ BatchActivateTemplateNode node, TriggerAddViewColumnVisitor.Context
context) {
+ addViewColumnsIfNeeded(
+ toActivationDevice2Measurements(node.getTemplateActivationMap(),
context),
+ node.getPlanNodeId().getId());
+ return null;
+ }
+
+ @Override
+ public TSStatus visitInternalBatchActivateTemplate(
+ InternalBatchActivateTemplateNode node,
TriggerAddViewColumnVisitor.Context context) {
+ addViewColumnsIfNeeded(
+ toActivationDevice2Measurements(node.getTemplateActivationMap(),
context),
+ node.getPlanNodeId().getId());
+ return null;
+ }
+
+ private static Map<PartialPath, List<Pair<String, TSDataType>>>
toActivationDevice2Measurements(
+ final Map<PartialPath, Pair<Integer, Integer>> templateActivationMap,
+ final TriggerAddViewColumnVisitor.Context context) {
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ new HashMap<>(templateActivationMap.size());
+ for (final Map.Entry<PartialPath, Pair<Integer, Integer>> entry :
+ templateActivationMap.entrySet()) {
+ final Template template =
context.getTemplates().get(entry.getValue().getLeft());
+ if (Objects.isNull(template)) {
+ continue;
+ }
+ device2Measurements.put(entry.getKey(), toMeasurements(template));
+ }
+ return device2Measurements;
+ }
+
+ private static Map<PartialPath, List<Pair<String, TSDataType>>>
toDevice2Measurements(
+ final Map<PartialPath, MeasurementGroup> measurementGroupMap) {
+ final Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements
=
+ new HashMap<>(measurementGroupMap.size());
+ for (final Map.Entry<PartialPath, MeasurementGroup> entry :
measurementGroupMap.entrySet()) {
+ device2Measurements.put(entry.getKey(),
toMeasurements(entry.getValue()));
+ }
+ return device2Measurements;
+ }
+
+ private static List<Pair<String, TSDataType>> toMeasurements(final Template
template) {
+ return template.getSchemaMap().values().stream()
+ .map(schema -> new Pair<>(schema.getMeasurementName(),
schema.getType()))
+ .toList();
+ }
+
+ private static List<Pair<String, TSDataType>>
toMeasurements(MeasurementGroup measurementGroup) {
+ return toMeasurements(measurementGroup.getMeasurements(),
measurementGroup.getDataTypes());
+ }
+
+ private static List<Pair<String, TSDataType>> toMeasurements(
+ final List<String> measurements, final List<TSDataType> dataTypes) {
+ final List<Pair<String, TSDataType>> result = new
ArrayList<>(measurements.size());
+ for (int i = 0; i < measurements.size(); i++) {
+ result.add(new Pair<>(measurements.get(i), dataTypes.get(i)));
+ }
+ return result;
+ }
+
+ @Override
+ public TSStatus visitPlan(PlanNode node, TriggerAddViewColumnVisitor.Context
context) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * The queryId identifies one column-adding operation in ConfigNode, so as
to deduplicate the
+ * requests sent by the leader and the followers while applying the same
schema region log entry.
+ *
+ * <p>The {@code operationId} is the planNodeId of the plan which triggers
the operation, and it
+ * is identical on every replica of one log entry. The database, the table
and the fingerprint of
+ * the columns are appended because ConfigNode only compares the queryId,
instead of the table,
+ * while deduplicating: without them the operation of another view, or of
another column set,
+ * would be treated as a duplicate and silently skipped.
+ */
+ private static String buildQueryId(String operationId, String database,
String table) {
Review Comment:
**[P1] The dedup queryId is ambiguous: views `a_b.c` and `a.b_c` collide,
and one of them silently never gets columns**
The queryId joins database and table with `_`, so `a_b.c` and `a.b_c` get
the same id for the same plan node and columns. `checkDuplicateTableTask`
matches `ADD_VIEW_COLUMN_PROCEDURE` by type and queryId only, so the request
for the second view is treated as a repeat of the first view's procedure, waits
for it, and reports success.
Reproduced with `a_b.c`, `a.b_c` and `x1.y1` all auto-syncing over
`root.qc.**`: `s_new` and `s_new2` were added to `a.b_c` and `x1.y1` but never
to `a_b.c`, with nothing logged; after `drop view a.b_c`, the next new
measurement reached `a_b.c` immediately.
Please use an unambiguous encoding (e.g. length-prefixed parts) or check
database and table as well.
##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/table/view/DropViewProcedure.java:
##########
@@ -38,6 +42,17 @@ public DropViewProcedure(
super(database, tableName, queryId, isGeneratedByPipe);
}
+ /** A dropped view has to leave the tree-to-table view index of every
DataNode. */
+ @Override
+ protected TUpdateTableReq buildCommitReleaseTableReq(final String oldName) {
+ // Unregistering a view needs nothing but its name, which the request
already carries.
+ return SchemaUtils.buildCommitUpdateTableReq(
+ database,
+ table.getTableName(),
+ null,
+ new
TUpdateTree2TableView(TreeToTableViewOperationType.REMOVE.getOperationType()));
Review Comment:
**[P3] `REMOVE` is sent for every dropped view, so each DataNode warns for
plain views**
Dropping a view without `auto_sync_schema` makes every DataNode log `WARN
Tree to table view %s.%s is not registered`; this already happens with the
property gated. Send `REMOVE` only for auto-sync views, or make removing an
unregistered view a silent no-op.
##########
iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeSchemaMessages.java:
##########
@@ -740,5 +741,12 @@ private DataNodeSchemaMessages() {}
public static final String EXCEPTION_COMMA_50AD1C01 = ", ";
public static final String
MESSAGE_SKIP_ROLLBACK_RENAMING_OLD_TABLE_ARG_ARG_BECAUSE_IT_HAS_BEEN_HANDLED_664F2456
= "Skip rollback renaming old table {}.{} because it has been handled.";
public static final String
MESSAGE_SKIP_COMMIT_UPDATE_TABLE_ARG_ARG_BECAUSE_IT_HAS_BEEN_HANDLED_31362A1C =
"Skip commit-update table {}.{} because it has been handled.";
+ public static final String LOG_ADD_VIEW_COLUMN_CN_FAILED = "Failed to add
columns to the tree to table views for operation %s, ConfigNode returned %s.";
+ public static final String LOG_ADD_VIEW_COLUMN_FAILED_FOR_VIEW = "Failed to
add a column to the view %s.%s for operation %s, ConfigNode returned %s.";
Review Comment:
**[P3] The new log templates use `%s`, which SLF4J doesn't substitute, so
the logs lose every argument**
The DataNode logs literally `Failed to add columns to the tree to table
views for operation %s, ConfigNode returned %s`. This applies to all 8 new
`LOG_` constants in `DataNodeSchemaMessages`, to
`DataNodeMiscMessages.LOG_FAILED_TO_COLLECT_THE_DEVICES_USING_THE_EXTENDED_TEMPLATE_ARG_95856581`
and to
`ProcedureMessages.LOG_FAILED_TO_DETECT_THE_TREE_SERIES_UNDER_THE_NEW_VIEW_ARG_ARG_ARG_9544E1D9`;
please use `{}`. Also, per the naming convention in CLAUDE.md:
- 5 constants have no `_<md5>` suffix: `LOG_ADD_VIEW_COLUMN_CN_FAILED`,
`LOG_ADD_VIEW_COLUMN_FAILED_FOR_VIEW`, `LOG_ADD_VIEW_COLUMN_EMPTY_RESPONSE`,
`LOG_ADD_VIEW_COLUMN_CN_UNREACHABLE`, `LOG_TRIGGER_ADD_VIEW_COLUMN_FAILED`;
- the suffix of `..._E86EAEDE` doesn't match its text (`6DFCC941`); all
suffixes change anyway once the text uses `{}`;
- the zh `LOG_ADD_VIEW_COLUMN_FAILED_FOR_VIEW` puts the operation first
(`为操作 %s 的视图 %s.%s ...`) while the arguments are passed as database, table,
operation, so the zh log mislabels them.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/table/TriggerAddViewColumnVisitor.java:
##########
@@ -0,0 +1,389 @@
+/*
+ * 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 org.apache.iotdb.db.schemaengine.table;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.client.AbstractConfigNodeInfo;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.consensus.ConfigRegionId;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.schema.table.AlterOrDropTableOperationType;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchemaUtil;
+import org.apache.iotdb.commons.schema.template.Template;
+import org.apache.iotdb.confignode.rpc.thrift.TAlterOrDropTableReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewFailure;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsResp;
+import org.apache.iotdb.db.i18n.DataNodeSchemaMessages;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClient;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClientManager;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanVisitor;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.ActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.BatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateAlignedTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalBatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.MeasurementGroup;
+import
org.apache.iotdb.db.queryengine.plan.relational.metadata.fetcher.TableHeaderSchemaValidator;
+import
org.apache.iotdb.db.schemaengine.schemaregion.view.TreeToTableViewReference;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HexFormat;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Adds the columns which the tree-to-table views covering the created
timeseries need.
+ *
+ * <p>The trigger is a best-effort side effect of the schema creation, see
addViewColumnsIfNeeded.
+ */
+public class TriggerAddViewColumnVisitor
+ implements PlanVisitor<TSStatus, TriggerAddViewColumnVisitor.Context> {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(TriggerAddViewColumnVisitor.class);
+ private static final IClientManager<ConfigRegionId, ConfigNodeClient>
CONFIG_NODE_CLIENT_MANAGER =
+ ConfigNodeClientManager.getInstance();
+
+ public static final String QUERY_ID_PREFIX = "tree_to_table_add_column";
+ public static final String ALTER_DEVICE_TEMPLATE_ID_PREFIX = "template_";
+
+ public static final class Context {
+
+ private final Map<Integer, Template> templates;
+
+ public Context(Map<Integer, Template> templates) {
+ this.templates = Objects.isNull(templates) ? Collections.emptyMap() :
templates;
+ }
+
+ public Map<Integer, Template> getTemplates() {
+ return templates;
+ }
+ }
+
+ /**
+ * Adds the columns the views covering the given devices need.
+ *
+ * <p>It is a best-effort side effect of the write which triggers it: every
failure is logged here
+ * and none of them is propagated, so that the write operation is never
affected. The columns
+ * which are not added this time are added by a later write of the same
measurements, as every
+ * write plan carries its own planNodeId, hence its own queryId.
+ *
+ * @param device2Measurements eg. device01 -> {s1, s2, s3}; device02 -> {s3,
s4}
+ * @param operationId the planNodeId of the plan which triggers this
operation
+ */
+ public static void addViewColumnsIfNeeded(
+ Map<PartialPath, List<Pair<String, TSDataType>>> device2Measurements,
String operationId) {
+ if (device2Measurements == null || device2Measurements.isEmpty()) {
+ return;
+ }
+ final Map<TreeToTableViewReference, Set<Pair<String, TSDataType>>>
view2Measurements =
+ collectViewMeasurements(device2Measurements);
+ if (view2Measurements.isEmpty()) {
+ return;
+ }
+
+ try (ConfigNodeClient configNodeClient =
+
CONFIG_NODE_CLIENT_MANAGER.borrowClient(AbstractConfigNodeInfo.CONFIG_REGION_ID))
{
+ List<TAlterOrDropTableReq> updateViewReqs =
+ view2Measurements.entrySet().stream()
+ .map(entry -> TriggerAddViewColumnVisitor.toAlterTableReq(entry,
operationId))
+ .toList();
+
+ TUpdateTree2TableViewsResp resp =
+ configNodeClient.updateTree2TableViews(new
TUpdateTree2TableViewsReq(updateViewReqs));
Review Comment:
**[P2] While any DataNode is down, every new column under an auto-sync view
blocks the schema write for ~22 s**
This call waits for the `AddViewColumnProcedure`, whose `PRE_RELEASE` waits
until every DataNode has answered or is proven fenced.
Reproduced in 1C3D with DN3 stopped: `create timeseries root.c3.dlate.late1
INT32` took 22,549 ms, and the ConfigNode log shows
`AddTableColumn-c3db.v-PRE_RELEASE costs 22314ms`. Without auto-sync, `create
timeseries` runs no table procedure at all.
The schema write shouldn't wait for the view update; suggest making the
trigger asynchronous, with a reconciliation path (see line 110) as the fallback.
##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/table/TriggerAddViewColumnVisitor.java:
##########
@@ -0,0 +1,389 @@
+/*
+ * 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 org.apache.iotdb.db.schemaengine.table;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.client.AbstractConfigNodeInfo;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.consensus.ConfigRegionId;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.schema.table.AlterOrDropTableOperationType;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchemaUtil;
+import org.apache.iotdb.commons.schema.template.Template;
+import org.apache.iotdb.confignode.rpc.thrift.TAlterOrDropTableReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewFailure;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsReq;
+import org.apache.iotdb.confignode.rpc.thrift.TUpdateTree2TableViewsResp;
+import org.apache.iotdb.db.i18n.DataNodeSchemaMessages;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClient;
+import org.apache.iotdb.db.protocol.client.ConfigNodeClientManager;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanVisitor;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.ActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.BatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateAlignedTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.CreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalBatchActivateTemplateNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateMultiTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.InternalCreateTimeSeriesNode;
+import
org.apache.iotdb.db.queryengine.plan.planner.plan.node.metadata.write.MeasurementGroup;
+import
org.apache.iotdb.db.queryengine.plan.relational.metadata.fetcher.TableHeaderSchemaValidator;
+import
org.apache.iotdb.db.schemaengine.schemaregion.view.TreeToTableViewReference;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.TException;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.utils.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HexFormat;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Adds the columns which the tree-to-table views covering the created
timeseries need.
+ *
+ * <p>The trigger is a best-effort side effect of the schema creation, see
addViewColumnsIfNeeded.
+ */
+public class TriggerAddViewColumnVisitor
+ implements PlanVisitor<TSStatus, TriggerAddViewColumnVisitor.Context> {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(TriggerAddViewColumnVisitor.class);
+ private static final IClientManager<ConfigRegionId, ConfigNodeClient>
CONFIG_NODE_CLIENT_MANAGER =
+ ConfigNodeClientManager.getInstance();
+
+ public static final String QUERY_ID_PREFIX = "tree_to_table_add_column";
+ public static final String ALTER_DEVICE_TEMPLATE_ID_PREFIX = "template_";
+
+ public static final class Context {
+
+ private final Map<Integer, Template> templates;
+
+ public Context(Map<Integer, Template> templates) {
+ this.templates = Objects.isNull(templates) ? Collections.emptyMap() :
templates;
+ }
+
+ public Map<Integer, Template> getTemplates() {
+ return templates;
+ }
+ }
+
+ /**
+ * Adds the columns the views covering the given devices need.
+ *
+ * <p>It is a best-effort side effect of the write which triggers it: every
failure is logged here
+ * and none of them is propagated, so that the write operation is never
affected. The columns
+ * which are not added this time are added by a later write of the same
measurements, as every
Review Comment:
**[P2] Columns missed while the ConfigNode is unavailable are never added,
and covered schema writes stall meanwhile**
The javadoc says the columns not added this time are added by a later write
of the same measurements, but writing to an existing series creates nothing, so
the trigger never fires for it again.
Reproduced with the ConfigNode stopped (localhost): `create timeseries`
under a covered path took 2063 ms instead of 12 ms, and an insert that
auto-creates a series 1032 ms instead of 21 ms; a network partition would cost
the full client timeouts. After the ConfigNode came back, `x2` and `x3`,
created while it was down, were never added.
Missed columns need a reconciliation path, e.g. the ConfigNode-side catch-up
already used at creation, run on recovery or periodically for auto-sync views;
and the javadoc should be corrected.
##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/ProcedureManager.java:
##########
@@ -2268,6 +2281,159 @@ public TSStatus alterTableAddColumn(final
TAlterOrDropTableReq req) {
false));
}
+ public TUpdateTree2TableViewsResp
addColumnForTree2TableViews(TUpdateTree2TableViewsReq req) {
+ if (req == null || !req.isSetViewInfos()) {
+ return new
TUpdateTree2TableViewsResp(RpcUtils.getStatus(TSStatusCode.ILLEGAL_PARAMETER));
+ }
+
+ final List<TUpdateTree2TableViewFailure> failedViews = new ArrayList<>();
+ final List<Pair<TAlterOrDropTableReq, Procedure<ConfigNodeProcedureEnv>>>
procedures =
+ new ArrayList<>();
+ // The requests which overlapped with another task on the same table,
together with the status
+ // of the last submission attempt, see processOverlappingTask.
+ final List<Pair<TAlterOrDropTableReq, TSStatus>> overlappingReqs = new
ArrayList<>();
+
+ // Submit every view before waiting, so procedures for different views can
run concurrently.
+ for (final TAlterOrDropTableReq addColumnReq : req.getViewInfos()) {
+ if (!isAddViewColumnReq(addColumnReq)) {
+ failedViews.add(illegalAddViewColumnFailure(addColumnReq));
+ continue;
+ }
+
+ final TSStatus submissionStatus = submitAddViewColumn(addColumnReq,
procedures, failedViews);
+ if (submissionStatus.getCode() ==
TSStatusCode.OVERLAP_WITH_EXISTING_TASK.getStatusCode()) {
+ overlappingReqs.add(new Pair<>(addColumnReq, submissionStatus));
+ }
+ }
+
+ processOverlappingTask(overlappingReqs, procedures, failedViews);
+
+ final long waitDeadline = System.currentTimeMillis() +
ADD_VIEW_COLUMN_WAIT_TIME_OUT_MS;
+ for (final Pair<TAlterOrDropTableReq, Procedure<ConfigNodeProcedureEnv>>
procedure :
+ procedures) {
+ final long remainingWaitTime = Math.max(0, waitDeadline -
System.currentTimeMillis());
+ final TSStatus status = waitingProcedureFinished(procedure.getRight(),
remainingWaitTime);
+ if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ failedViews.add(
+ new TUpdateTree2TableViewFailure(
+ procedure.getLeft().database, procedure.getLeft().tableName,
status));
+ }
+ }
+
+ final TSStatus status = failedViews.isEmpty() ? StatusUtils.OK :
StatusUtils.INTERNAL_ERROR;
+ final TUpdateTree2TableViewsResp response = new
TUpdateTree2TableViewsResp(status);
+ if (!failedViews.isEmpty()) {
+ response.setFailedViews(failedViews);
+ }
+ return response;
+ }
+
+ /** Whether the request carries everything an add-column operation on a view
needs. */
+ private static boolean isAddViewColumnReq(final @Nullable
TAlterOrDropTableReq addColumnReq) {
+ return Objects.nonNull(addColumnReq)
+ && addColumnReq.isSetDatabase()
+ && addColumnReq.isSetTableName()
+ && addColumnReq.isSetQueryId()
+ && addColumnReq.isSetOperationType()
+ && addColumnReq.getOperationType()
+ == AlterOrDropTableOperationType.ADD_COLUMN.getTypeValue()
+ && addColumnReq.isSetUpdateInfo()
+ && addColumnReq.isSetIsView()
+ && addColumnReq.isIsView();
+ }
+
+ private static TUpdateTree2TableViewFailure illegalAddViewColumnFailure(
+ final TAlterOrDropTableReq addColumnReq) {
+ return new TUpdateTree2TableViewFailure(
+ Objects.isNull(addColumnReq) ? "" :
Objects.toString(addColumnReq.getDatabase(), ""),
+ Objects.isNull(addColumnReq) ? "" :
Objects.toString(addColumnReq.getTableName(), ""),
+ RpcUtils.getStatus(TSStatusCode.ILLEGAL_PARAMETER));
+ }
+
+ /**
+ * Submits the add-column procedure of one view and settles everything but
an overlap with another
+ * task on the same table.
+ */
+ private TSStatus submitAddViewColumn(
+ final TAlterOrDropTableReq addColumnReq,
+ final List<Pair<TAlterOrDropTableReq,
Procedure<ConfigNodeProcedureEnv>>> procedures,
+ final List<TUpdateTree2TableViewFailure> failedViews) {
+ final AddViewColumnProcedure procedure;
+ procedure =
+ new AddViewColumnProcedure(
+ addColumnReq.database,
+ addColumnReq.tableName,
+ addColumnReq.queryId,
+
TsTableColumnSchemaUtil.deserializeColumnSchemaList(addColumnReq.updateInfo),
Review Comment:
**[P1] The overlap retry can never succeed: the first attempt consumes
`updateInfo`**
`deserializeColumnSchemaList(addColumnReq.updateInfo)` reads the request's
`ByteBuffer` in place. When the first attempt overlaps with another task on the
table, `processOverlappingTask` (line 2422) calls `submitAddViewColumn` again
with the same request after the sleep; the buffer is exhausted by then, so the
second deserialization throws `BufferUnderflowException`, which escapes the RPC
handler (the DataNode gets `TApplicationException: Internal error processing
updateTree2TableViews`). The column only lands because the DataNode's client
retries the whole RPC.
Reproduced with 4 schema regions and 8 threads creating new measurements
concurrently: 37 `BufferUnderflowException`s at `submitAddViewColumn:2367` via
`processOverlappingTask:2422`.
Deserialize once (or read from `updateInfo.duplicate()`) and keep the column
list with the pending request.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]