|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.confignode.procedure.impl.schema.table.view; |
| 21 | + |
| 22 | +import org.apache.iotdb.common.rpc.thrift.TSStatus; |
| 23 | +import org.apache.iotdb.commons.exception.IoTDBException; |
| 24 | +import org.apache.iotdb.commons.exception.MetadataException; |
| 25 | +import org.apache.iotdb.commons.schema.table.TableNodeStatus; |
| 26 | +import org.apache.iotdb.commons.schema.table.TreeViewSchema; |
| 27 | +import org.apache.iotdb.commons.schema.table.TsTable; |
| 28 | +import org.apache.iotdb.confignode.consensus.request.write.table.view.PreCreateTableViewPlan; |
| 29 | +import org.apache.iotdb.confignode.exception.DatabaseNotExistsException; |
| 30 | +import org.apache.iotdb.confignode.persistence.schema.TreeDeviceViewFieldDetector; |
| 31 | +import org.apache.iotdb.confignode.procedure.env.ConfigNodeProcedureEnv; |
| 32 | +import org.apache.iotdb.confignode.procedure.exception.ProcedureException; |
| 33 | +import org.apache.iotdb.confignode.procedure.impl.schema.SchemaUtils; |
| 34 | +import org.apache.iotdb.confignode.procedure.impl.schema.table.CreateTableProcedure; |
| 35 | +import org.apache.iotdb.confignode.procedure.state.schema.CreateTableState; |
| 36 | +import org.apache.iotdb.confignode.procedure.store.ProcedureType; |
| 37 | +import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; |
| 38 | +import org.apache.iotdb.rpc.TSStatusCode; |
| 39 | + |
| 40 | +import org.apache.tsfile.utils.Pair; |
| 41 | +import org.apache.tsfile.utils.ReadWriteIOUtils; |
| 42 | +import org.slf4j.Logger; |
| 43 | +import org.slf4j.LoggerFactory; |
| 44 | + |
| 45 | +import java.io.DataOutputStream; |
| 46 | +import java.io.IOException; |
| 47 | +import java.nio.ByteBuffer; |
| 48 | +import java.util.Objects; |
| 49 | +import java.util.Optional; |
| 50 | + |
| 51 | +import static org.apache.iotdb.rpc.TSStatusCode.TABLE_ALREADY_EXISTS; |
| 52 | + |
| 53 | +public class CreateTableViewProcedure extends CreateTableProcedure { |
| 54 | + private static final Logger LOGGER = LoggerFactory.getLogger(CreateTableViewProcedure.class); |
| 55 | + private boolean replace; |
| 56 | + private TsTable oldView; |
| 57 | + private TableNodeStatus oldStatus; |
| 58 | + |
| 59 | + public CreateTableViewProcedure(final boolean isGeneratedByPipe) { |
| 60 | + super(isGeneratedByPipe); |
| 61 | + } |
| 62 | + |
| 63 | + public CreateTableViewProcedure( |
| 64 | + final String database, |
| 65 | + final TsTable table, |
| 66 | + final boolean replace, |
| 67 | + final boolean isGeneratedByPipe) { |
| 68 | + super(database, table, isGeneratedByPipe); |
| 69 | + this.replace = replace; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected void checkTableExistence(final ConfigNodeProcedureEnv env) { |
| 74 | + if (!replace) { |
| 75 | + super.checkTableExistence(env); |
| 76 | + } else { |
| 77 | + try { |
| 78 | + final Optional<Pair<TsTable, TableNodeStatus>> oldTableAndStatus = |
| 79 | + env.getConfigManager() |
| 80 | + .getClusterSchemaManager() |
| 81 | + .getTableAndStatusIfExists(database, table.getTableName()); |
| 82 | + if (oldTableAndStatus.isPresent()) { |
| 83 | + if (!TreeViewSchema.isTreeViewTable(oldTableAndStatus.get().getLeft())) { |
| 84 | + setFailure( |
| 85 | + new ProcedureException( |
| 86 | + new IoTDBException( |
| 87 | + String.format( |
| 88 | + "Table '%s.%s' already exists.", database, table.getTableName()), |
| 89 | + TABLE_ALREADY_EXISTS.getStatusCode()))); |
| 90 | + return; |
| 91 | + } else { |
| 92 | + oldView = oldTableAndStatus.get().getLeft(); |
| 93 | + oldStatus = oldTableAndStatus.get().getRight(); |
| 94 | + } |
| 95 | + } |
| 96 | + final TDatabaseSchema schema = |
| 97 | + env.getConfigManager().getClusterSchemaManager().getDatabaseSchemaByName(database); |
| 98 | + if (!table.getPropValue(TsTable.TTL_PROPERTY).isPresent() |
| 99 | + && schema.isSetTTL() |
| 100 | + && schema.getTTL() != Long.MAX_VALUE) { |
| 101 | + table.addProp(TsTable.TTL_PROPERTY, String.valueOf(schema.getTTL())); |
| 102 | + } |
| 103 | + setNextState(CreateTableState.PRE_CREATE); |
| 104 | + } catch (final MetadataException | DatabaseNotExistsException e) { |
| 105 | + setFailure(new ProcedureException(e)); |
| 106 | + } |
| 107 | + } |
| 108 | + final TSStatus status = |
| 109 | + new TreeDeviceViewFieldDetector(env.getConfigManager(), table, null) |
| 110 | + .detectMissingFieldTypes(); |
| 111 | + if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) { |
| 112 | + setFailure(new ProcedureException(new IoTDBException(status))); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void preCreateTable(final ConfigNodeProcedureEnv env) { |
| 118 | + final TSStatus status = |
| 119 | + SchemaUtils.executeInConsensusLayer( |
| 120 | + new PreCreateTableViewPlan(database, table, TableNodeStatus.PRE_CREATE), env, LOGGER); |
| 121 | + if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) { |
| 122 | + setNextState(CreateTableState.PRE_RELEASE); |
| 123 | + } else { |
| 124 | + setFailure(new ProcedureException(new IoTDBException(status))); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected void rollbackCreate(final ConfigNodeProcedureEnv env) { |
| 130 | + if (Objects.isNull(oldView)) { |
| 131 | + super.rollbackCreate(env); |
| 132 | + return; |
| 133 | + } |
| 134 | + final TSStatus status = |
| 135 | + SchemaUtils.executeInConsensusLayer( |
| 136 | + new PreCreateTableViewPlan(database, oldView, oldStatus), env, LOGGER); |
| 137 | + if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) { |
| 138 | + LOGGER.warn("Failed to rollback table creation {}.{}", database, table.getTableName()); |
| 139 | + setFailure(new ProcedureException(new IoTDBException(status))); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void serialize(final DataOutputStream stream) throws IOException { |
| 145 | + stream.writeShort( |
| 146 | + isGeneratedByPipe |
| 147 | + ? ProcedureType.PIPE_ENRICHED_CREATE_TABLE_VIEW_PROCEDURE.getTypeCode() |
| 148 | + : ProcedureType.CREATE_TABLE_VIEW_PROCEDURE.getTypeCode()); |
| 149 | + innerSerialize(stream); |
| 150 | + ReadWriteIOUtils.write(replace, stream); |
| 151 | + |
| 152 | + ReadWriteIOUtils.write(Objects.nonNull(oldView), stream); |
| 153 | + if (Objects.nonNull(oldView)) { |
| 154 | + oldView.serialize(stream); |
| 155 | + } |
| 156 | + |
| 157 | + ReadWriteIOUtils.write(Objects.nonNull(oldStatus), stream); |
| 158 | + if (Objects.nonNull(oldStatus)) { |
| 159 | + oldStatus.serialize(stream); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void deserialize(final ByteBuffer byteBuffer) { |
| 165 | + super.deserialize(byteBuffer); |
| 166 | + replace = ReadWriteIOUtils.readBool(byteBuffer); |
| 167 | + |
| 168 | + if (ReadWriteIOUtils.readBool(byteBuffer)) { |
| 169 | + this.oldView = TsTable.deserialize(byteBuffer); |
| 170 | + } |
| 171 | + |
| 172 | + if (ReadWriteIOUtils.readBool(byteBuffer)) { |
| 173 | + this.oldStatus = TableNodeStatus.deserialize(byteBuffer); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public boolean equals(final Object o) { |
| 179 | + return super.equals(o) |
| 180 | + && replace == ((CreateTableViewProcedure) o).replace |
| 181 | + && Objects.equals(oldView, ((CreateTableViewProcedure) o).oldView) |
| 182 | + && Objects.equals(oldStatus, ((CreateTableViewProcedure) o).oldStatus); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public int hashCode() { |
| 187 | + return Objects.hash(super.hashCode(), replace, oldView, oldStatus); |
| 188 | + } |
| 189 | +} |
0 commit comments