diff --git a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/UnsetTemplateProcedure.java b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/UnsetTemplateProcedure.java index eb57e57c4c0da..5883db04dc11f 100644 --- a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/UnsetTemplateProcedure.java +++ b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/impl/schema/UnsetTemplateProcedure.java @@ -144,9 +144,6 @@ private void constructBlackList(final ConfigNodeProcedureEnv env) { private void invalidateCache(final ConfigNodeProcedureEnv env) { try { - // Cannot roll back after cache invalidation - // Because we do not know whether there are time series successfully created - alreadyRollback = true; executeInvalidateCache(env); setNextState(UnsetTemplateState.CHECK_DATANODE_TEMPLATE_ACTIVATION); } catch (final ProcedureException e) { @@ -217,21 +214,26 @@ protected void rollbackState( } alreadyRollback = true; ProcedureException rollbackException; - final TSStatus status = - env.getConfigManager() - .getClusterSchemaManager() - .rollbackPreUnsetSchemaTemplate(template.getId(), path); - if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) { - return; - } else { - LOGGER.error( - "Failed to rollback pre unset template operation of template {} set on {}", - template.getName(), - path); - rollbackException = - new ProcedureException( - new MetadataException( - "Rollback template pre unset failed because of" + status.getMessage())); + try { + executeRollbackInvalidateCache(env); + final TSStatus status = + env.getConfigManager() + .getClusterSchemaManager() + .rollbackPreUnsetSchemaTemplate(template.getId(), path); + if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) { + return; + } else { + LOGGER.error( + "Failed to rollback pre unset template operation of template {} set on {}", + template.getName(), + path); + rollbackException = + new ProcedureException( + new MetadataException( + "Rollback template pre unset failed because of" + status.getMessage())); + } + } catch (final ProcedureException e) { + rollbackException = e; } try { executeInvalidateCache(env); @@ -244,6 +246,42 @@ protected void rollbackState( } } + private void executeRollbackInvalidateCache(ConfigNodeProcedureEnv env) + throws ProcedureException { + Map dataNodeLocationMap = + env.getConfigManager().getNodeManager().getRegisteredDataNodeLocations(); + TUpdateTemplateReq rollbackTemplateSetInfoReq = new TUpdateTemplateReq(); + rollbackTemplateSetInfoReq.setType( + TemplateInternalRPCUpdateType.ROLLBACK_INVALIDATE_TEMPLATE_SET_INFO.toByte()); + rollbackTemplateSetInfoReq.setTemplateInfo(getAddTemplateSetInfo()); + DataNodeAsyncRequestContext clientHandler = + new DataNodeAsyncRequestContext<>( + CnToDnAsyncRequestType.UPDATE_TEMPLATE, + rollbackTemplateSetInfoReq, + dataNodeLocationMap); + CnToDnInternalServiceAsyncRequestManager.getInstance().sendAsyncRequestWithRetry(clientHandler); + Map statusMap = clientHandler.getResponseMap(); + for (TSStatus status : statusMap.values()) { + // all dataNodes must clear the related template cache + if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) { + LOGGER.error( + "Failed to rollback template cache of template {} set on {}", template.getName(), path); + throw new ProcedureException(new MetadataException("Rollback template cache failed")); + } + } + } + + private ByteBuffer getAddTemplateSetInfo() { + if (this.addTemplateSetInfo == null) { + this.addTemplateSetInfo = + ByteBuffer.wrap( + TemplateInternalRPCUtil.generateAddTemplateSetInfoBytes( + template, path.getFullPath())); + } + + return addTemplateSetInfo; + } + @Override protected boolean isRollbackSupported(final UnsetTemplateState unsetTemplateState) { return true; diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/DataNodeInternalRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/DataNodeInternalRPCServiceImpl.java index 31a098b9897b5..0d79820e80ae0 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/DataNodeInternalRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/DataNodeInternalRPCServiceImpl.java @@ -2265,7 +2265,6 @@ public TSStatus setTTL(TSetTTLReq req) throws TException { @Override public TSStatus updateTemplate(final TUpdateTemplateReq req) { switch (TemplateInternalRPCUpdateType.getType(req.type)) { - // Reserved for rolling upgrade case ROLLBACK_INVALIDATE_TEMPLATE_SET_INFO: ClusterTemplateManager.getInstance().addTemplateSetInfo(req.getTemplateInfo()); break; diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/TemplateInternalRPCUpdateType.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/TemplateInternalRPCUpdateType.java index ebe730114e2c7..7719391304493 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/TemplateInternalRPCUpdateType.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/template/TemplateInternalRPCUpdateType.java @@ -26,7 +26,6 @@ import java.nio.ByteBuffer; public enum TemplateInternalRPCUpdateType { - // Deprecated ROLLBACK_INVALIDATE_TEMPLATE_SET_INFO((byte) 0), INVALIDATE_TEMPLATE_SET_INFO((byte) 1), ADD_TEMPLATE_PRE_SET_INFO((byte) 2),