alpass163 commented on code in PR #18874: URL: https://github.com/apache/iotdb/pull/18874#discussion_r4311742249
########## 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: the high-available propagator has to wait -- 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]
