You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by "MarcosZyk (via GitHub)" <gi...@apache.org> on 2023/04/18 02:09:03 UTC
[GitHub] [iotdb] MarcosZyk opened a new pull request, #9637: [IOTDB-4837] Refactor Set Schema Template
MarcosZyk opened a new pull request, #9637:
URL: https://github.com/apache/iotdb/pull/9637
## Description
### Content1 ...
### Content2 ...
### Content3 ...
<!--
In each section, please describe design decisions made, including:
- Choice of algorithms
- Behavioral aspects. What configuration values are acceptable? How are corner cases and error
conditions handled, such as when there are insufficient resources?
- Class organization and design (how the logic is split between classes, inheritance, composition,
design patterns)
- Method organization and design (how the logic is split between methods, parameters and return types)
- Naming (class, method, API, configuration, HTTP endpoint, names of emitted metrics)
-->
<!-- It's good to describe an alternative design (or mention an alternative name) for every design
(or naming) decision point and compare the alternatives with the designs that you've implemented
(or the names you've chosen) to highlight the advantages of the chosen designs and names. -->
<!-- If there was a discussion of the design of the feature implemented in this PR elsewhere
(e. g. a "Proposal" issue, any other issue, or a thread in the development mailing list),
link to that discussion from this PR description and explain what have changed in your final design
compared to your original proposal or the consensus version in the end of the discussion.
If something hasn't changed since the original discussion, you can omit a detailed discussion of
those aspects of the design here, perhaps apart from brief mentioning for the sake of readability
of this PR description. -->
<!-- Some of the aspects mentioned above may be omitted for simple and small changes. -->
<hr>
This PR has:
- [ ] been self-reviewed.
- [ ] concurrent read
- [ ] concurrent write
- [ ] concurrent read and write
- [ ] added documentation for new or modified features or behaviors.
- [ ] added Javadocs for most classes and all non-trivial methods.
- [ ] added or updated version, __license__, or notice information
- [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious
for an unfamiliar reader.
- [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold
for code coverage.
- [ ] added integration tests.
- [ ] been tested in a test IoTDB cluster.
<!-- Check the items by putting "x" in the brackets for the done things. Not all of these items
apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items
from the checklist above are strictly necessary, but it would be very helpful if you at least
self-review the PR. -->
<hr>
##### Key changed/added classes (or packages if there are too many classes) in this PR
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] MarcosZyk commented on a diff in pull request #9637: [IOTDB-4837] Refactor Set Schema Template
Posted by "MarcosZyk (via GitHub)" <gi...@apache.org>.
MarcosZyk commented on code in PR #9637:
URL: https://github.com/apache/iotdb/pull/9637#discussion_r1171330584
##########
thrift/src/main/thrift/datanode.thrift:
##########
@@ -355,6 +355,16 @@ struct TCountPathsUsingTemplateResp{
2: optional i64 count
}
+struct TCheckTimeSeriesExistenceReq{
+ 1: required binary patternTree
+ 2: required list<common.TConsensusGroupId> schemaRegionIdList
+}
+
+struct TCheckTimeSeriesExistenceResp{
+ 1: required common.TSStatus status
+ 2: optional i64 count
Review Comment:
Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] MarcosZyk commented on a diff in pull request #9637: [IOTDB-4837] Refactor Set Schema Template
Posted by "MarcosZyk (via GitHub)" <gi...@apache.org>.
MarcosZyk commented on code in PR #9637:
URL: https://github.com/apache/iotdb/pull/9637#discussion_r1171294707
##########
confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SetTemplateProcedure.java:
##########
@@ -0,0 +1,528 @@
+/*
+ * 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.confignode.procedure.impl.schema;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.exception.IllegalPathException;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.path.PathPatternTree;
+import org.apache.iotdb.confignode.client.DataNodeRequestType;
+import org.apache.iotdb.confignode.client.async.AsyncDataNodeClientPool;
+import org.apache.iotdb.confignode.client.async.handlers.AsyncClientHandler;
+import org.apache.iotdb.confignode.consensus.request.read.template.CheckTemplateSettablePlan;
+import org.apache.iotdb.confignode.consensus.request.read.template.GetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.CommitSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.PreSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.response.template.TemplateInfoResp;
+import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException;
+import org.apache.iotdb.confignode.procedure.impl.statemachine.StateMachineProcedure;
+import org.apache.iotdb.confignode.procedure.state.schema.SetTemplateState;
+import org.apache.iotdb.confignode.procedure.store.ProcedureType;
+import org.apache.iotdb.db.exception.metadata.template.TemplateImcompatibeException;
+import org.apache.iotdb.db.exception.metadata.template.UndefinedTemplateException;
+import org.apache.iotdb.db.metadata.template.Template;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUpdateType;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUtil;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceReq;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceResp;
+import org.apache.iotdb.mpp.rpc.thrift.TUpdateTemplateReq;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD;
+
+public class SetTemplateProcedure
+ extends StateMachineProcedure<ConfigNodeProcedureEnv, SetTemplateState> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SetTemplateProcedure.class);
+
+ private String queryId;
+ private String templateName;
+ private String templateSetPath;
+
+ public SetTemplateProcedure() {
+ super();
+ }
+
+ public SetTemplateProcedure(String queryId, String templateName, String templateSetPath) {
+ super();
+ this.queryId = queryId;
+ this.templateName = templateName;
+ this.templateSetPath = templateSetPath;
+ }
+
+ @Override
+ protected Flow executeFromState(ConfigNodeProcedureEnv env, SetTemplateState state)
+ throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
+ long startTime = System.currentTimeMillis();
+ try {
+ switch (state) {
+ case VALIDATE_TEMPLATE_EXISTENCE:
+ LOGGER.info(
+ "Check template existence set on path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTemplateExistence(env);
+ break;
+ case PRE_SET:
+ LOGGER.info("Pre set schema template {} on path {}", templateName, templateSetPath);
+ preSetTemplate(env);
+ break;
+ case PRE_RELEASE:
+ LOGGER.info(
+ "Pre release schema template {} set on path {}", templateName, templateSetPath);
+ preReleaseTemplate(env);
+ break;
+ case VALIDATE_TIMESERIES_EXISTENCE:
+ LOGGER.info(
+ "Check timeseries existence under path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTimeSeriesExistence(env);
+ break;
+ case COMMIT_SET:
+ LOGGER.info("Commit set schema template {} on path {}", templateName, templateSetPath);
+ commitSetTemplate(env);
+ setNextState(SetTemplateState.COMMIT_RELEASE);
+ break;
+ case COMMIT_RELEASE:
+ LOGGER.info(
+ "Commit release schema template {} set on path {}", templateName, templateSetPath);
+ commitReleaseTemplate(env);
+ return Flow.NO_MORE_STATE;
+ default:
+ setFailure(new ProcedureException("Unrecognized SetTemplateState " + state.toString()));
+ return Flow.NO_MORE_STATE;
+ }
+ return Flow.HAS_MORE_STATE;
+ } finally {
+ LOGGER.info(
+ String.format(
+ "SetSchemaTemplate-[%s] costs %sms",
+ state.toString(), (System.currentTimeMillis() - startTime)));
+ }
+ }
+
+ private void validateTemplateExistence(ConfigNodeProcedureEnv env) {
+ // check whether the template can be set on given path
+ CheckTemplateSettablePlan checkTemplateSettablePlan =
+ new CheckTemplateSettablePlan(templateName, templateSetPath);
+ TemplateInfoResp resp =
+ (TemplateInfoResp)
+ env.getConfigManager()
+ .getConsensusManager()
+ .read(checkTemplateSettablePlan)
+ .getDataset();
+ if (resp.getStatus().getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_SET);
+ } else {
+ setFailure(
+ new ProcedureException(
+ new IoTDBException(resp.getStatus().getMessage(), resp.getStatus().getCode())));
+ }
+ }
+
+ private void preSetTemplate(ConfigNodeProcedureEnv env) {
+ PreSetSchemaTemplatePlan preSetSchemaTemplatePlan =
+ new PreSetSchemaTemplatePlan(templateName, templateSetPath);
+ TSStatus status =
+ env.getConfigManager().getConsensusManager().write(preSetSchemaTemplatePlan).getStatus();
+ if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_RELEASE);
+ } else {
+ LOGGER.warn(
+ "Failed to pre set template {} on path {} due to {}",
+ templateName,
+ templateSetPath,
+ status.getMessage());
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ }
+ }
+
+ private void preReleaseTemplate(ConfigNodeProcedureEnv env) {
+ Template template = getTemplate(env);
+ if (template == null) {
+ // already setFailure
+ return;
+ }
+
+ TUpdateTemplateReq req = new TUpdateTemplateReq();
+ req.setType(TemplateInternalRPCUpdateType.ADD_TEMPLATE_PRE_SET_INFO.toByte());
+ req.setTemplateInfo(
+ TemplateInternalRPCUtil.generateAddTemplateSetInfoBytes(template, templateSetPath));
+
+ Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
+ AsyncClientHandler<TUpdateTemplateReq, TSStatus> clientHandler =
+ new AsyncClientHandler<>(DataNodeRequestType.UPDATE_TEMPLATE, req, dataNodeLocationMap);
+ AsyncDataNodeClientPool.getInstance().sendAsyncRequestToDataNodeWithRetry(clientHandler);
+ Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
+ for (Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
+ if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ LOGGER.warn(
+ "Failed to sync template {} pre-set info on path {} to DataNode {}",
+ templateName,
+ templateSetPath,
+ dataNodeLocationMap.get(entry.getKey()));
+ setFailure(new ProcedureException(new MetadataException("Pre set template failed")));
+ return;
+ }
+ }
+ setNextState(SetTemplateState.VALIDATE_TIMESERIES_EXISTENCE);
+ }
+
+ private Template getTemplate(ConfigNodeProcedureEnv env) {
+ GetSchemaTemplatePlan getSchemaTemplatePlan = new GetSchemaTemplatePlan(templateName);
+ TemplateInfoResp templateResp =
+ (TemplateInfoResp)
+ env.getConfigManager().getConsensusManager().read(getSchemaTemplatePlan).getDataset();
+ if (templateResp.getStatus().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setFailure(
+ new ProcedureException(
+ new IoTDBException(
+ templateResp.getStatus().getMessage(), templateResp.getStatus().getCode())));
+ return null;
+ }
+ if (templateResp.getTemplateList() == null || templateResp.getTemplateList().isEmpty()) {
+ setFailure(new ProcedureException(new UndefinedTemplateException(templateName)));
+ return null;
+ }
+ return templateResp.getTemplateList().get(0);
+ }
+
+ private void validateTimeSeriesExistence(ConfigNodeProcedureEnv env) {
+ PathPatternTree patternTree = new PathPatternTree();
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+ PartialPath path = null;
+ try {
+ path = new PartialPath(templateSetPath);
+ patternTree.appendPathPattern(path);
+ patternTree.appendPathPattern(path.concatNode(MULTI_LEVEL_PATH_WILDCARD));
+ patternTree.serialize(dataOutputStream);
+ } catch (IllegalPathException | IOException ignored) {
+ }
+ ByteBuffer patternTreeBytes = ByteBuffer.wrap(byteArrayOutputStream.toByteArray());
+
+ Map<TConsensusGroupId, TRegionReplicaSet> relatedSchemaRegionGroup =
+ env.getConfigManager().getRelatedSchemaRegionGroup(patternTree);
+
+ List<TCheckTimeSeriesExistenceResp> respList = new ArrayList<>();
+ DataNodeRegionTaskExecutor<TCheckTimeSeriesExistenceReq, TCheckTimeSeriesExistenceResp>
+ regionTask =
+ new DataNodeRegionTaskExecutor<
+ TCheckTimeSeriesExistenceReq, TCheckTimeSeriesExistenceResp>(
+ env,
+ relatedSchemaRegionGroup,
+ false,
+ DataNodeRequestType.CHECK_TIMESERIES_EXISTENCE,
+ ((dataNodeLocation, consensusGroupIdList) ->
+ new TCheckTimeSeriesExistenceReq(patternTreeBytes, consensusGroupIdList))) {
+
+ @Override
+ protected List<TConsensusGroupId> processResponseOfOneDataNode(
+ TDataNodeLocation dataNodeLocation,
+ List<TConsensusGroupId> consensusGroupIdList,
+ TCheckTimeSeriesExistenceResp response) {
+ respList.add(response);
+ List<TConsensusGroupId> failedRegionList = new ArrayList<>();
+ if (response.getStatus().getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ return failedRegionList;
+ }
+
+ if (response.getStatus().getCode() == TSStatusCode.MULTIPLE_ERROR.getStatusCode()) {
+ List<TSStatus> subStatus = response.getStatus().getSubStatus();
+ for (int i = 0; i < subStatus.size(); i++) {
+ if (subStatus.get(i).getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ failedRegionList.add(consensusGroupIdList.get(i));
+ }
+ }
+ } else {
+ failedRegionList.addAll(consensusGroupIdList);
+ }
+ return failedRegionList;
+ }
+
+ @Override
+ protected void onAllReplicasetFailure(
+ TConsensusGroupId consensusGroupId, Set<TDataNodeLocation> dataNodeLocationSet) {
+ setFailure(
+ new ProcedureException(
+ new MetadataException(
+ String.format(
+ "Set template %s to %s failed when [check timeseries existence on DataNode] because all replicaset of schemaRegion %s failed. %s",
+ templateName,
+ templateSetPath,
+ consensusGroupId.id,
+ dataNodeLocationSet))));
+ interruptTask();
+ }
+ };
+ regionTask.execute();
+ if (isFailed()) {
+ return;
+ }
+
+ long result = 0;
+ for (TCheckTimeSeriesExistenceResp resp : respList) {
+ result += resp.getCount();
+ }
+
+ if (result == 0) {
+ setNextState(SetTemplateState.COMMIT_SET);
+ } else {
+ setFailure(new ProcedureException(new TemplateImcompatibeException(templateName, path)));
+ }
+ }
+
+ private void commitSetTemplate(ConfigNodeProcedureEnv env) {
+ CommitSetSchemaTemplatePlan commitSetSchemaTemplatePlan =
+ new CommitSetSchemaTemplatePlan(templateName, templateSetPath);
+ TSStatus status =
+ env.getConfigManager().getConsensusManager().write(commitSetSchemaTemplatePlan).getStatus();
+ if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.COMMIT_RELEASE);
+ } else {
+ LOGGER.warn(
+ "Failed to commit set template {} on path {} due to {}",
+ templateName,
+ templateSetPath,
+ status.getMessage());
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ }
+ }
+
+ private void commitReleaseTemplate(ConfigNodeProcedureEnv env) {
+ Template template = getTemplate(env);
+ if (template == null) {
+ // already setFailure
+ return;
+ }
+
+ TUpdateTemplateReq req = new TUpdateTemplateReq();
+ req.setType(TemplateInternalRPCUpdateType.COMMIT_TEMPLATE_SET_INFO.toByte());
+ req.setTemplateInfo(
+ TemplateInternalRPCUtil.generateAddTemplateSetInfoBytes(template, templateSetPath));
+
+ Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
+ AsyncClientHandler<TUpdateTemplateReq, TSStatus> clientHandler =
+ new AsyncClientHandler<>(DataNodeRequestType.UPDATE_TEMPLATE, req, dataNodeLocationMap);
+ AsyncDataNodeClientPool.getInstance().sendAsyncRequestToDataNodeWithRetry(clientHandler);
+ Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
+ for (Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
+ if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
Review Comment:
I think it's ok to keep current implementation since we can report precise failure info on specific DataNode.
##########
confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SetTemplateProcedure.java:
##########
@@ -0,0 +1,528 @@
+/*
+ * 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.confignode.procedure.impl.schema;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.exception.IllegalPathException;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.path.PathPatternTree;
+import org.apache.iotdb.confignode.client.DataNodeRequestType;
+import org.apache.iotdb.confignode.client.async.AsyncDataNodeClientPool;
+import org.apache.iotdb.confignode.client.async.handlers.AsyncClientHandler;
+import org.apache.iotdb.confignode.consensus.request.read.template.CheckTemplateSettablePlan;
+import org.apache.iotdb.confignode.consensus.request.read.template.GetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.CommitSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.PreSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.response.template.TemplateInfoResp;
+import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException;
+import org.apache.iotdb.confignode.procedure.impl.statemachine.StateMachineProcedure;
+import org.apache.iotdb.confignode.procedure.state.schema.SetTemplateState;
+import org.apache.iotdb.confignode.procedure.store.ProcedureType;
+import org.apache.iotdb.db.exception.metadata.template.TemplateImcompatibeException;
+import org.apache.iotdb.db.exception.metadata.template.UndefinedTemplateException;
+import org.apache.iotdb.db.metadata.template.Template;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUpdateType;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUtil;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceReq;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceResp;
+import org.apache.iotdb.mpp.rpc.thrift.TUpdateTemplateReq;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD;
+
+public class SetTemplateProcedure
+ extends StateMachineProcedure<ConfigNodeProcedureEnv, SetTemplateState> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SetTemplateProcedure.class);
+
+ private String queryId;
+ private String templateName;
+ private String templateSetPath;
+
+ public SetTemplateProcedure() {
+ super();
+ }
+
+ public SetTemplateProcedure(String queryId, String templateName, String templateSetPath) {
+ super();
+ this.queryId = queryId;
+ this.templateName = templateName;
+ this.templateSetPath = templateSetPath;
+ }
+
+ @Override
+ protected Flow executeFromState(ConfigNodeProcedureEnv env, SetTemplateState state)
+ throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
+ long startTime = System.currentTimeMillis();
+ try {
+ switch (state) {
+ case VALIDATE_TEMPLATE_EXISTENCE:
+ LOGGER.info(
+ "Check template existence set on path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTemplateExistence(env);
+ break;
+ case PRE_SET:
+ LOGGER.info("Pre set schema template {} on path {}", templateName, templateSetPath);
+ preSetTemplate(env);
+ break;
+ case PRE_RELEASE:
+ LOGGER.info(
+ "Pre release schema template {} set on path {}", templateName, templateSetPath);
+ preReleaseTemplate(env);
+ break;
+ case VALIDATE_TIMESERIES_EXISTENCE:
+ LOGGER.info(
+ "Check timeseries existence under path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTimeSeriesExistence(env);
+ break;
+ case COMMIT_SET:
+ LOGGER.info("Commit set schema template {} on path {}", templateName, templateSetPath);
+ commitSetTemplate(env);
+ setNextState(SetTemplateState.COMMIT_RELEASE);
+ break;
+ case COMMIT_RELEASE:
+ LOGGER.info(
+ "Commit release schema template {} set on path {}", templateName, templateSetPath);
+ commitReleaseTemplate(env);
+ return Flow.NO_MORE_STATE;
+ default:
+ setFailure(new ProcedureException("Unrecognized SetTemplateState " + state.toString()));
+ return Flow.NO_MORE_STATE;
+ }
+ return Flow.HAS_MORE_STATE;
+ } finally {
+ LOGGER.info(
+ String.format(
+ "SetSchemaTemplate-[%s] costs %sms",
+ state.toString(), (System.currentTimeMillis() - startTime)));
+ }
+ }
+
+ private void validateTemplateExistence(ConfigNodeProcedureEnv env) {
+ // check whether the template can be set on given path
+ CheckTemplateSettablePlan checkTemplateSettablePlan =
+ new CheckTemplateSettablePlan(templateName, templateSetPath);
+ TemplateInfoResp resp =
+ (TemplateInfoResp)
+ env.getConfigManager()
+ .getConsensusManager()
+ .read(checkTemplateSettablePlan)
+ .getDataset();
+ if (resp.getStatus().getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_SET);
+ } else {
+ setFailure(
+ new ProcedureException(
+ new IoTDBException(resp.getStatus().getMessage(), resp.getStatus().getCode())));
+ }
+ }
+
+ private void preSetTemplate(ConfigNodeProcedureEnv env) {
+ PreSetSchemaTemplatePlan preSetSchemaTemplatePlan =
+ new PreSetSchemaTemplatePlan(templateName, templateSetPath);
+ TSStatus status =
+ env.getConfigManager().getConsensusManager().write(preSetSchemaTemplatePlan).getStatus();
+ if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_RELEASE);
+ } else {
+ LOGGER.warn(
+ "Failed to pre set template {} on path {} due to {}",
+ templateName,
+ templateSetPath,
+ status.getMessage());
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ }
+ }
+
+ private void preReleaseTemplate(ConfigNodeProcedureEnv env) {
+ Template template = getTemplate(env);
+ if (template == null) {
+ // already setFailure
+ return;
+ }
+
+ TUpdateTemplateReq req = new TUpdateTemplateReq();
+ req.setType(TemplateInternalRPCUpdateType.ADD_TEMPLATE_PRE_SET_INFO.toByte());
+ req.setTemplateInfo(
+ TemplateInternalRPCUtil.generateAddTemplateSetInfoBytes(template, templateSetPath));
+
+ Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
+ AsyncClientHandler<TUpdateTemplateReq, TSStatus> clientHandler =
+ new AsyncClientHandler<>(DataNodeRequestType.UPDATE_TEMPLATE, req, dataNodeLocationMap);
+ AsyncDataNodeClientPool.getInstance().sendAsyncRequestToDataNodeWithRetry(clientHandler);
+ Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
+ for (Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
+ if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
Review Comment:
I think it's ok to keep current implementation since we can report precise failure info on specific DataNode.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] MarcosZyk merged pull request #9637: [IOTDB-4837] Refactor Set Schema Template
Posted by "MarcosZyk (via GitHub)" <gi...@apache.org>.
MarcosZyk merged PR #9637:
URL: https://github.com/apache/iotdb/pull/9637
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] MarcosZyk commented on a diff in pull request #9637: [IOTDB-4837] Refactor Set Schema Template
Posted by "MarcosZyk (via GitHub)" <gi...@apache.org>.
MarcosZyk commented on code in PR #9637:
URL: https://github.com/apache/iotdb/pull/9637#discussion_r1171276146
##########
server/src/main/java/org/apache/iotdb/db/service/thrift/impl/DataNodeInternalRPCServiceImpl.java:
##########
@@ -719,7 +725,51 @@ public TCountPathsUsingTemplateResp countPathsUsingTemplate(TCountPathsUsingTemp
return resp;
}
- private TSStatus executeSchemaDeletionTask(
+ @Override
+ public TCheckTimeSeriesExistenceResp checkTimeSeriesExistence(TCheckTimeSeriesExistenceReq req)
+ throws TException {
+ PathPatternTree patternTree = PathPatternTree.deserialize(req.patternTree);
+ TCheckTimeSeriesExistenceResp resp = new TCheckTimeSeriesExistenceResp();
+ AtomicLong result = new AtomicLong(0);
+ resp.setStatus(
+ executeInternalSchemaTask(
+ req.getSchemaRegionIdList(),
+ consensusGroupId -> {
+ ReadWriteLock readWriteLock =
+ regionManager.getRegionLock(new SchemaRegionId(consensusGroupId.getId()));
+ // check timeseries existence for set template shall block all timeseries creation
+ readWriteLock.writeLock().lock();
+ try {
+ ISchemaRegion schemaRegion =
+ schemaEngine.getSchemaRegion(new SchemaRegionId(consensusGroupId.getId()));
+ PathPatternTree filteredPatternTree =
+ filterPathPatternTree(patternTree, schemaRegion.getStorageGroupFullPath());
Review Comment:
Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] MarcosZyk commented on a diff in pull request #9637: [IOTDB-4837] Refactor Set Schema Template
Posted by "MarcosZyk (via GitHub)" <gi...@apache.org>.
MarcosZyk commented on code in PR #9637:
URL: https://github.com/apache/iotdb/pull/9637#discussion_r1171384618
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java:
##########
@@ -1723,52 +1723,25 @@ public Analysis visitCreateTimeseries(
}
private void checkIsTemplateCompatible(PartialPath timeseriesPath, String alias) {
- Pair<Template, PartialPath> templateInfo = schemaFetcher.checkTemplateSetInfo(timeseriesPath);
+ Pair<Template, PartialPath> templateInfo =
+ schemaFetcher.checkTemplateSetAndPreSetInfo(timeseriesPath);
Review Comment:
Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] MarcosZyk commented on a diff in pull request #9637: [IOTDB-4837] Refactor Set Schema Template
Posted by "MarcosZyk (via GitHub)" <gi...@apache.org>.
MarcosZyk commented on code in PR #9637:
URL: https://github.com/apache/iotdb/pull/9637#discussion_r1171276657
##########
server/src/main/java/org/apache/iotdb/db/exception/metadata/template/TemplateImcompatibeException.java:
##########
@@ -38,4 +39,23 @@ public TemplateImcompatibeException(String path, String templateName, String ove
TSStatusCode.TEMPLATE_INCOMPATIBLE.getStatusCode());
this.isUserException = true;
}
+
+ public TemplateImcompatibeException(
+ String path, String templateName, PartialPath templateSetPath) {
+ super(
+ String.format(
+ "Cannot create timeseries [%s] since schema template [%s] already set on path [%s], ",
+ path, templateName, templateSetPath),
+ TSStatusCode.TEMPLATE_INCOMPATIBLE.getStatusCode());
+ this.isUserException = true;
+ }
Review Comment:
Removed meaningless check and usage.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] Cpaulyz commented on a diff in pull request #9637: [IOTDB-4837] Refactor Set Schema Template
Posted by "Cpaulyz (via GitHub)" <gi...@apache.org>.
Cpaulyz commented on code in PR #9637:
URL: https://github.com/apache/iotdb/pull/9637#discussion_r1171033871
##########
thrift/src/main/thrift/datanode.thrift:
##########
@@ -355,6 +355,16 @@ struct TCountPathsUsingTemplateResp{
2: optional i64 count
}
+struct TCheckTimeSeriesExistenceReq{
+ 1: required binary patternTree
+ 2: required list<common.TConsensusGroupId> schemaRegionIdList
+}
+
+struct TCheckTimeSeriesExistenceResp{
+ 1: required common.TSStatus status
+ 2: optional i64 count
Review Comment:
It seems to be used only to determine whether or not it exists. Why not use a bool?
##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/AnalyzeVisitor.java:
##########
@@ -1723,52 +1723,25 @@ public Analysis visitCreateTimeseries(
}
private void checkIsTemplateCompatible(PartialPath timeseriesPath, String alias) {
- Pair<Template, PartialPath> templateInfo = schemaFetcher.checkTemplateSetInfo(timeseriesPath);
+ Pair<Template, PartialPath> templateInfo =
+ schemaFetcher.checkTemplateSetAndPreSetInfo(timeseriesPath);
Review Comment:
Should alias need to be considered? For example, root.sg.d1.speed(s1), template set on root.sg.d1.s1
##########
confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SetTemplateProcedure.java:
##########
@@ -0,0 +1,528 @@
+/*
+ * 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.confignode.procedure.impl.schema;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.exception.IllegalPathException;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.path.PathPatternTree;
+import org.apache.iotdb.confignode.client.DataNodeRequestType;
+import org.apache.iotdb.confignode.client.async.AsyncDataNodeClientPool;
+import org.apache.iotdb.confignode.client.async.handlers.AsyncClientHandler;
+import org.apache.iotdb.confignode.consensus.request.read.template.CheckTemplateSettablePlan;
+import org.apache.iotdb.confignode.consensus.request.read.template.GetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.CommitSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.PreSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.response.template.TemplateInfoResp;
+import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException;
+import org.apache.iotdb.confignode.procedure.impl.statemachine.StateMachineProcedure;
+import org.apache.iotdb.confignode.procedure.state.schema.SetTemplateState;
+import org.apache.iotdb.confignode.procedure.store.ProcedureType;
+import org.apache.iotdb.db.exception.metadata.template.TemplateImcompatibeException;
+import org.apache.iotdb.db.exception.metadata.template.UndefinedTemplateException;
+import org.apache.iotdb.db.metadata.template.Template;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUpdateType;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUtil;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceReq;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceResp;
+import org.apache.iotdb.mpp.rpc.thrift.TUpdateTemplateReq;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD;
+
+public class SetTemplateProcedure
+ extends StateMachineProcedure<ConfigNodeProcedureEnv, SetTemplateState> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SetTemplateProcedure.class);
+
+ private String queryId;
+ private String templateName;
+ private String templateSetPath;
+
+ public SetTemplateProcedure() {
+ super();
+ }
+
+ public SetTemplateProcedure(String queryId, String templateName, String templateSetPath) {
+ super();
+ this.queryId = queryId;
+ this.templateName = templateName;
+ this.templateSetPath = templateSetPath;
+ }
+
+ @Override
+ protected Flow executeFromState(ConfigNodeProcedureEnv env, SetTemplateState state)
+ throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
+ long startTime = System.currentTimeMillis();
+ try {
+ switch (state) {
+ case VALIDATE_TEMPLATE_EXISTENCE:
+ LOGGER.info(
+ "Check template existence set on path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTemplateExistence(env);
+ break;
+ case PRE_SET:
+ LOGGER.info("Pre set schema template {} on path {}", templateName, templateSetPath);
+ preSetTemplate(env);
+ break;
+ case PRE_RELEASE:
+ LOGGER.info(
+ "Pre release schema template {} set on path {}", templateName, templateSetPath);
+ preReleaseTemplate(env);
+ break;
+ case VALIDATE_TIMESERIES_EXISTENCE:
+ LOGGER.info(
+ "Check timeseries existence under path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTimeSeriesExistence(env);
+ break;
+ case COMMIT_SET:
+ LOGGER.info("Commit set schema template {} on path {}", templateName, templateSetPath);
+ commitSetTemplate(env);
+ setNextState(SetTemplateState.COMMIT_RELEASE);
+ break;
+ case COMMIT_RELEASE:
+ LOGGER.info(
+ "Commit release schema template {} set on path {}", templateName, templateSetPath);
+ commitReleaseTemplate(env);
+ return Flow.NO_MORE_STATE;
+ default:
+ setFailure(new ProcedureException("Unrecognized SetTemplateState " + state.toString()));
+ return Flow.NO_MORE_STATE;
+ }
+ return Flow.HAS_MORE_STATE;
+ } finally {
+ LOGGER.info(
+ String.format(
+ "SetSchemaTemplate-[%s] costs %sms",
+ state.toString(), (System.currentTimeMillis() - startTime)));
+ }
+ }
+
+ private void validateTemplateExistence(ConfigNodeProcedureEnv env) {
+ // check whether the template can be set on given path
+ CheckTemplateSettablePlan checkTemplateSettablePlan =
+ new CheckTemplateSettablePlan(templateName, templateSetPath);
+ TemplateInfoResp resp =
+ (TemplateInfoResp)
+ env.getConfigManager()
+ .getConsensusManager()
+ .read(checkTemplateSettablePlan)
+ .getDataset();
+ if (resp.getStatus().getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_SET);
+ } else {
+ setFailure(
+ new ProcedureException(
+ new IoTDBException(resp.getStatus().getMessage(), resp.getStatus().getCode())));
+ }
+ }
+
+ private void preSetTemplate(ConfigNodeProcedureEnv env) {
+ PreSetSchemaTemplatePlan preSetSchemaTemplatePlan =
+ new PreSetSchemaTemplatePlan(templateName, templateSetPath);
+ TSStatus status =
+ env.getConfigManager().getConsensusManager().write(preSetSchemaTemplatePlan).getStatus();
+ if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_RELEASE);
+ } else {
+ LOGGER.warn(
+ "Failed to pre set template {} on path {} due to {}",
+ templateName,
+ templateSetPath,
+ status.getMessage());
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ }
+ }
+
+ private void preReleaseTemplate(ConfigNodeProcedureEnv env) {
+ Template template = getTemplate(env);
+ if (template == null) {
+ // already setFailure
+ return;
+ }
+
+ TUpdateTemplateReq req = new TUpdateTemplateReq();
+ req.setType(TemplateInternalRPCUpdateType.ADD_TEMPLATE_PRE_SET_INFO.toByte());
+ req.setTemplateInfo(
+ TemplateInternalRPCUtil.generateAddTemplateSetInfoBytes(template, templateSetPath));
+
+ Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
+ AsyncClientHandler<TUpdateTemplateReq, TSStatus> clientHandler =
+ new AsyncClientHandler<>(DataNodeRequestType.UPDATE_TEMPLATE, req, dataNodeLocationMap);
+ AsyncDataNodeClientPool.getInstance().sendAsyncRequestToDataNodeWithRetry(clientHandler);
+ Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
+ for (Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
+ if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ LOGGER.warn(
+ "Failed to sync template {} pre-set info on path {} to DataNode {}",
+ templateName,
+ templateSetPath,
+ dataNodeLocationMap.get(entry.getKey()));
+ setFailure(new ProcedureException(new MetadataException("Pre set template failed")));
+ return;
+ }
+ }
+ setNextState(SetTemplateState.VALIDATE_TIMESERIES_EXISTENCE);
+ }
+
+ private Template getTemplate(ConfigNodeProcedureEnv env) {
+ GetSchemaTemplatePlan getSchemaTemplatePlan = new GetSchemaTemplatePlan(templateName);
+ TemplateInfoResp templateResp =
+ (TemplateInfoResp)
+ env.getConfigManager().getConsensusManager().read(getSchemaTemplatePlan).getDataset();
+ if (templateResp.getStatus().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setFailure(
+ new ProcedureException(
+ new IoTDBException(
+ templateResp.getStatus().getMessage(), templateResp.getStatus().getCode())));
+ return null;
+ }
+ if (templateResp.getTemplateList() == null || templateResp.getTemplateList().isEmpty()) {
+ setFailure(new ProcedureException(new UndefinedTemplateException(templateName)));
+ return null;
+ }
+ return templateResp.getTemplateList().get(0);
+ }
+
+ private void validateTimeSeriesExistence(ConfigNodeProcedureEnv env) {
+ PathPatternTree patternTree = new PathPatternTree();
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
+ PartialPath path = null;
+ try {
+ path = new PartialPath(templateSetPath);
+ patternTree.appendPathPattern(path);
+ patternTree.appendPathPattern(path.concatNode(MULTI_LEVEL_PATH_WILDCARD));
+ patternTree.serialize(dataOutputStream);
+ } catch (IllegalPathException | IOException ignored) {
+ }
+ ByteBuffer patternTreeBytes = ByteBuffer.wrap(byteArrayOutputStream.toByteArray());
+
+ Map<TConsensusGroupId, TRegionReplicaSet> relatedSchemaRegionGroup =
+ env.getConfigManager().getRelatedSchemaRegionGroup(patternTree);
+
+ List<TCheckTimeSeriesExistenceResp> respList = new ArrayList<>();
+ DataNodeRegionTaskExecutor<TCheckTimeSeriesExistenceReq, TCheckTimeSeriesExistenceResp>
+ regionTask =
+ new DataNodeRegionTaskExecutor<
+ TCheckTimeSeriesExistenceReq, TCheckTimeSeriesExistenceResp>(
+ env,
+ relatedSchemaRegionGroup,
+ false,
+ DataNodeRequestType.CHECK_TIMESERIES_EXISTENCE,
+ ((dataNodeLocation, consensusGroupIdList) ->
+ new TCheckTimeSeriesExistenceReq(patternTreeBytes, consensusGroupIdList))) {
+
+ @Override
+ protected List<TConsensusGroupId> processResponseOfOneDataNode(
+ TDataNodeLocation dataNodeLocation,
+ List<TConsensusGroupId> consensusGroupIdList,
+ TCheckTimeSeriesExistenceResp response) {
+ respList.add(response);
+ List<TConsensusGroupId> failedRegionList = new ArrayList<>();
+ if (response.getStatus().getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ return failedRegionList;
+ }
+
+ if (response.getStatus().getCode() == TSStatusCode.MULTIPLE_ERROR.getStatusCode()) {
+ List<TSStatus> subStatus = response.getStatus().getSubStatus();
+ for (int i = 0; i < subStatus.size(); i++) {
+ if (subStatus.get(i).getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ failedRegionList.add(consensusGroupIdList.get(i));
+ }
+ }
+ } else {
+ failedRegionList.addAll(consensusGroupIdList);
+ }
+ return failedRegionList;
+ }
+
+ @Override
+ protected void onAllReplicasetFailure(
+ TConsensusGroupId consensusGroupId, Set<TDataNodeLocation> dataNodeLocationSet) {
+ setFailure(
+ new ProcedureException(
+ new MetadataException(
+ String.format(
+ "Set template %s to %s failed when [check timeseries existence on DataNode] because all replicaset of schemaRegion %s failed. %s",
+ templateName,
+ templateSetPath,
+ consensusGroupId.id,
+ dataNodeLocationSet))));
+ interruptTask();
+ }
+ };
+ regionTask.execute();
+ if (isFailed()) {
+ return;
+ }
+
+ long result = 0;
+ for (TCheckTimeSeriesExistenceResp resp : respList) {
+ result += resp.getCount();
+ }
+
+ if (result == 0) {
+ setNextState(SetTemplateState.COMMIT_SET);
+ } else {
+ setFailure(new ProcedureException(new TemplateImcompatibeException(templateName, path)));
+ }
+ }
+
+ private void commitSetTemplate(ConfigNodeProcedureEnv env) {
+ CommitSetSchemaTemplatePlan commitSetSchemaTemplatePlan =
+ new CommitSetSchemaTemplatePlan(templateName, templateSetPath);
+ TSStatus status =
+ env.getConfigManager().getConsensusManager().write(commitSetSchemaTemplatePlan).getStatus();
+ if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.COMMIT_RELEASE);
+ } else {
+ LOGGER.warn(
+ "Failed to commit set template {} on path {} due to {}",
+ templateName,
+ templateSetPath,
+ status.getMessage());
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ }
+ }
+
+ private void commitReleaseTemplate(ConfigNodeProcedureEnv env) {
+ Template template = getTemplate(env);
+ if (template == null) {
+ // already setFailure
+ return;
+ }
+
+ TUpdateTemplateReq req = new TUpdateTemplateReq();
+ req.setType(TemplateInternalRPCUpdateType.COMMIT_TEMPLATE_SET_INFO.toByte());
+ req.setTemplateInfo(
+ TemplateInternalRPCUtil.generateAddTemplateSetInfoBytes(template, templateSetPath));
+
+ Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
+ AsyncClientHandler<TUpdateTemplateReq, TSStatus> clientHandler =
+ new AsyncClientHandler<>(DataNodeRequestType.UPDATE_TEMPLATE, req, dataNodeLocationMap);
+ AsyncDataNodeClientPool.getInstance().sendAsyncRequestToDataNodeWithRetry(clientHandler);
+ Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
+ for (Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
+ if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
Review Comment:
```suggestion
if(RpcUtils.squashResponseStatusList(clientHandler.getResponseList()).getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode())
```
##########
confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/SetTemplateProcedure.java:
##########
@@ -0,0 +1,528 @@
+/*
+ * 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.confignode.procedure.impl.schema;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TRegionReplicaSet;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.exception.IllegalPathException;
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.commons.path.PathPatternTree;
+import org.apache.iotdb.confignode.client.DataNodeRequestType;
+import org.apache.iotdb.confignode.client.async.AsyncDataNodeClientPool;
+import org.apache.iotdb.confignode.client.async.handlers.AsyncClientHandler;
+import org.apache.iotdb.confignode.consensus.request.read.template.CheckTemplateSettablePlan;
+import org.apache.iotdb.confignode.consensus.request.read.template.GetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.CommitSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.request.write.template.PreSetSchemaTemplatePlan;
+import org.apache.iotdb.confignode.consensus.response.template.TemplateInfoResp;
+import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureSuspendedException;
+import org.apache.iotdb.confignode.procedure.exception.ProcedureYieldException;
+import org.apache.iotdb.confignode.procedure.impl.statemachine.StateMachineProcedure;
+import org.apache.iotdb.confignode.procedure.state.schema.SetTemplateState;
+import org.apache.iotdb.confignode.procedure.store.ProcedureType;
+import org.apache.iotdb.db.exception.metadata.template.TemplateImcompatibeException;
+import org.apache.iotdb.db.exception.metadata.template.UndefinedTemplateException;
+import org.apache.iotdb.db.metadata.template.Template;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUpdateType;
+import org.apache.iotdb.db.metadata.template.TemplateInternalRPCUtil;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceReq;
+import org.apache.iotdb.mpp.rpc.thrift.TCheckTimeSeriesExistenceResp;
+import org.apache.iotdb.mpp.rpc.thrift.TUpdateTemplateReq;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD;
+
+public class SetTemplateProcedure
+ extends StateMachineProcedure<ConfigNodeProcedureEnv, SetTemplateState> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SetTemplateProcedure.class);
+
+ private String queryId;
+ private String templateName;
+ private String templateSetPath;
+
+ public SetTemplateProcedure() {
+ super();
+ }
+
+ public SetTemplateProcedure(String queryId, String templateName, String templateSetPath) {
+ super();
+ this.queryId = queryId;
+ this.templateName = templateName;
+ this.templateSetPath = templateSetPath;
+ }
+
+ @Override
+ protected Flow executeFromState(ConfigNodeProcedureEnv env, SetTemplateState state)
+ throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
+ long startTime = System.currentTimeMillis();
+ try {
+ switch (state) {
+ case VALIDATE_TEMPLATE_EXISTENCE:
+ LOGGER.info(
+ "Check template existence set on path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTemplateExistence(env);
+ break;
+ case PRE_SET:
+ LOGGER.info("Pre set schema template {} on path {}", templateName, templateSetPath);
+ preSetTemplate(env);
+ break;
+ case PRE_RELEASE:
+ LOGGER.info(
+ "Pre release schema template {} set on path {}", templateName, templateSetPath);
+ preReleaseTemplate(env);
+ break;
+ case VALIDATE_TIMESERIES_EXISTENCE:
+ LOGGER.info(
+ "Check timeseries existence under path {} when try setting template {}",
+ templateSetPath,
+ templateName);
+ validateTimeSeriesExistence(env);
+ break;
+ case COMMIT_SET:
+ LOGGER.info("Commit set schema template {} on path {}", templateName, templateSetPath);
+ commitSetTemplate(env);
+ setNextState(SetTemplateState.COMMIT_RELEASE);
+ break;
+ case COMMIT_RELEASE:
+ LOGGER.info(
+ "Commit release schema template {} set on path {}", templateName, templateSetPath);
+ commitReleaseTemplate(env);
+ return Flow.NO_MORE_STATE;
+ default:
+ setFailure(new ProcedureException("Unrecognized SetTemplateState " + state.toString()));
+ return Flow.NO_MORE_STATE;
+ }
+ return Flow.HAS_MORE_STATE;
+ } finally {
+ LOGGER.info(
+ String.format(
+ "SetSchemaTemplate-[%s] costs %sms",
+ state.toString(), (System.currentTimeMillis() - startTime)));
+ }
+ }
+
+ private void validateTemplateExistence(ConfigNodeProcedureEnv env) {
+ // check whether the template can be set on given path
+ CheckTemplateSettablePlan checkTemplateSettablePlan =
+ new CheckTemplateSettablePlan(templateName, templateSetPath);
+ TemplateInfoResp resp =
+ (TemplateInfoResp)
+ env.getConfigManager()
+ .getConsensusManager()
+ .read(checkTemplateSettablePlan)
+ .getDataset();
+ if (resp.getStatus().getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_SET);
+ } else {
+ setFailure(
+ new ProcedureException(
+ new IoTDBException(resp.getStatus().getMessage(), resp.getStatus().getCode())));
+ }
+ }
+
+ private void preSetTemplate(ConfigNodeProcedureEnv env) {
+ PreSetSchemaTemplatePlan preSetSchemaTemplatePlan =
+ new PreSetSchemaTemplatePlan(templateName, templateSetPath);
+ TSStatus status =
+ env.getConfigManager().getConsensusManager().write(preSetSchemaTemplatePlan).getStatus();
+ if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ setNextState(SetTemplateState.PRE_RELEASE);
+ } else {
+ LOGGER.warn(
+ "Failed to pre set template {} on path {} due to {}",
+ templateName,
+ templateSetPath,
+ status.getMessage());
+ setFailure(new ProcedureException(new IoTDBException(status.getMessage(), status.getCode())));
+ }
+ }
+
+ private void preReleaseTemplate(ConfigNodeProcedureEnv env) {
+ Template template = getTemplate(env);
+ if (template == null) {
+ // already setFailure
+ return;
+ }
+
+ TUpdateTemplateReq req = new TUpdateTemplateReq();
+ req.setType(TemplateInternalRPCUpdateType.ADD_TEMPLATE_PRE_SET_INFO.toByte());
+ req.setTemplateInfo(
+ TemplateInternalRPCUtil.generateAddTemplateSetInfoBytes(template, templateSetPath));
+
+ Map<Integer, TDataNodeLocation> dataNodeLocationMap =
+ env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations();
+ AsyncClientHandler<TUpdateTemplateReq, TSStatus> clientHandler =
+ new AsyncClientHandler<>(DataNodeRequestType.UPDATE_TEMPLATE, req, dataNodeLocationMap);
+ AsyncDataNodeClientPool.getInstance().sendAsyncRequestToDataNodeWithRetry(clientHandler);
+ Map<Integer, TSStatus> statusMap = clientHandler.getResponseMap();
+ for (Map.Entry<Integer, TSStatus> entry : statusMap.entrySet()) {
+ if (entry.getValue().getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
Review Comment:
the same
##########
server/src/main/java/org/apache/iotdb/db/exception/metadata/template/TemplateImcompatibeException.java:
##########
@@ -38,4 +39,23 @@ public TemplateImcompatibeException(String path, String templateName, String ove
TSStatusCode.TEMPLATE_INCOMPATIBLE.getStatusCode());
this.isUserException = true;
}
+
+ public TemplateImcompatibeException(
+ String path, String templateName, PartialPath templateSetPath) {
+ super(
+ String.format(
+ "Cannot create timeseries [%s] since schema template [%s] already set on path [%s], ",
+ path, templateName, templateSetPath),
+ TSStatusCode.TEMPLATE_INCOMPATIBLE.getStatusCode());
+ this.isUserException = true;
+ }
Review Comment:
I see that this Exception has two other constructors, one deprecated and one called when the SchemaRegion is creating timeseries.
Now the TemplateImcompatibeException should no longer be thrown when creating a sequence, and the other two constructors should be removed.
##########
server/src/main/java/org/apache/iotdb/db/service/thrift/impl/DataNodeInternalRPCServiceImpl.java:
##########
@@ -719,7 +725,51 @@ public TCountPathsUsingTemplateResp countPathsUsingTemplate(TCountPathsUsingTemp
return resp;
}
- private TSStatus executeSchemaDeletionTask(
+ @Override
+ public TCheckTimeSeriesExistenceResp checkTimeSeriesExistence(TCheckTimeSeriesExistenceReq req)
+ throws TException {
+ PathPatternTree patternTree = PathPatternTree.deserialize(req.patternTree);
+ TCheckTimeSeriesExistenceResp resp = new TCheckTimeSeriesExistenceResp();
+ AtomicLong result = new AtomicLong(0);
+ resp.setStatus(
+ executeInternalSchemaTask(
+ req.getSchemaRegionIdList(),
+ consensusGroupId -> {
+ ReadWriteLock readWriteLock =
+ regionManager.getRegionLock(new SchemaRegionId(consensusGroupId.getId()));
+ // check timeseries existence for set template shall block all timeseries creation
+ readWriteLock.writeLock().lock();
+ try {
+ ISchemaRegion schemaRegion =
+ schemaEngine.getSchemaRegion(new SchemaRegionId(consensusGroupId.getId()));
+ PathPatternTree filteredPatternTree =
+ filterPathPatternTree(patternTree, schemaRegion.getStorageGroupFullPath());
Review Comment:
```suggestion
filterPathPatternTree(patternTree, schemaRegion.getDatabaseFullPath());
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org