Caideyipi commented on code in PR #19242: URL: https://github.com/apache/iotdb/pull/19242#discussion_r4559627325
########## integration-test/src/test/java/org/apache/iotdb/pipe/it/dual/tablemodel/manual/AbstractPipeTableModelSharedClusterIT.java: ########## @@ -0,0 +1,208 @@ +/* + * 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.pipe.it.dual.tablemodel.manual; + +import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient; +import org.apache.iotdb.commons.pipe.agent.task.meta.PipeStaticMeta; +import org.apache.iotdb.confignode.rpc.thrift.TDropPipeReq; +import org.apache.iotdb.confignode.rpc.thrift.TShowPipeInfo; +import org.apache.iotdb.confignode.rpc.thrift.TShowPipeReq; +import org.apache.iotdb.confignode.rpc.thrift.TShowPipeResp; +import org.apache.iotdb.db.it.utils.TestUtils; +import org.apache.iotdb.isession.SessionConfig; +import org.apache.iotdb.it.env.MultiEnvFactory; +import org.apache.iotdb.itbase.env.BaseEnv; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.rules.ExternalResource; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import static org.junit.Assert.assertEquals; + +/** For pipe tests whose cluster configuration stays constant across all methods in a class. */ +public abstract class AbstractPipeTableModelSharedClusterIT + extends AbstractPipeTableModelDualManualIT { + + private static BaseEnv sharedSender; + private static BaseEnv sharedReceiver; + private static CompletableFuture<Void> startup; + + @Override + protected void setupConfig() { + super.setupConfig(); + // These small functional workloads verify models/patterns, not automatic region expansion. + // CPU-based defaults create many tiny TsFiles and concurrent loads can exhaust the IT heap. + // Keep a region per database; separate databases and the configured replicas remain covered. + for (final BaseEnv env : new BaseEnv[] {senderEnv, receiverEnv}) { + env.getConfig() + .getCommonConfig() + .setDataRegionGroupExtensionPolicy("CUSTOM") + .setDefaultDataRegionGroupNumPerDatabase(1); + } + } + + @ClassRule + public static final ExternalResource CLUSTERS = + new ExternalResource() { + @Override + protected void after() { + // Always release both environments, including when initialization or a test fails. + try { + if (sharedSender != null) { + sharedSender.cleanClusterEnvironment(); + } + } finally { + try { + if (sharedReceiver != null) { + sharedReceiver.cleanClusterEnvironment(); + } + } finally { + sharedSender = null; + sharedReceiver = null; + startup = null; + } + } + } + }; + + @Override + @Before + public void setUp() { + if (sharedSender == null) { + MultiEnvFactory.createEnv(2); + sharedSender = MultiEnvFactory.getEnv(0); + sharedReceiver = MultiEnvFactory.getEnv(1); + senderEnv = sharedSender; + receiverEnv = sharedReceiver; + setupConfig(); + // Join both starts before any test or cleanup runs; retain failures for subsequent methods. + startup = initializeClusters(); + } + senderEnv = sharedSender; + receiverEnv = sharedReceiver; + startup.join(); + } + + protected CompletableFuture<Void> initializeClusters() { + return CompletableFuture.allOf( + CompletableFuture.runAsync(senderEnv::initClusterEnvironment), + CompletableFuture.runAsync(receiverEnv::initClusterEnvironment)); + } + + @Override + protected void cleanEnvironment() { + if (startup == null || startup.isCompletedExceptionally()) { + // A failed start is cleaned by the class rule; do not open connections to unavailable nodes. + return; + } + try { + // Drop all user pipes in both SQL dialects before deleting any database. Otherwise an active + // pipe can recreate the receiver's data during cleanup and contaminate the next method. + dropUserPipes(senderEnv); + dropUserPipes(receiverEnv); + resetCatalog(senderEnv); + resetCatalog(receiverEnv); + } catch (Exception e) { + throw new AssertionError("Failed to reset shared pipe test clusters", e); + } + } + + private static void dropUserPipes(final BaseEnv env) throws Exception { + try (final SyncConfigNodeIServiceClient client = + (SyncConfigNodeIServiceClient) env.getLeaderConfigNodeConnection()) { + for (final boolean tableModel : new boolean[] {false, true}) { + final TShowPipeResp response = + client.showPipe( + new TShowPipeReq() + .setIsTableModel(tableModel) + .setUserName(SessionConfig.DEFAULT_USER)); + assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(), response.getStatus().getCode()); + for (final TShowPipeInfo pipe : response.getPipeInfoList()) { + if (!pipe.getId().startsWith(PipeStaticMeta.SYSTEM_PIPE_PREFIX)) { + assertEquals( + TSStatusCode.SUCCESS_STATUS.getStatusCode(), + client + .dropPipeExtended(new TDropPipeReq(pipe.getId()).setIsTableModel(tableModel)) + .getCode()); Review Comment: [P2] Tolerate pipes removed after the snapshot. `showPipe` is a snapshot; while this loop processes it, the automatic-drop procedure can remove a listed pipe. A subsequent `dropPipeExtended` then returns `PIPE_NOT_EXIST_ERROR` for that entry, turning successful cleanup into a flaky test failure. Please ignore only that status here and in `IoTDBPipeAutoDropIT.dropTablePipes`, as the tree shared-cluster cleanup already does. -- 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]
