Skip to content
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* 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.treemodel.auto.basic;

import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
import org.apache.iotdb.confignode.rpc.thrift.TCreatePipeReq;
import org.apache.iotdb.db.it.utils.TestUtils;
import org.apache.iotdb.it.env.cluster.node.DataNodeWrapper;
import org.apache.iotdb.it.framework.IoTDBTestRunner;
import org.apache.iotdb.itbase.category.MultiClusterIT2DualTreeAutoBasic;
import org.apache.iotdb.pipe.it.dual.treemodel.auto.AbstractPipeTreeModelSharedClusterIT;
import org.apache.iotdb.rpc.TSStatusCode;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;

@RunWith(IoTDBTestRunner.class)
@Category({MultiClusterIT2DualTreeAutoBasic.class})
public class IoTDBPipeHistoricalTsFileCloseIT extends AbstractPipeTreeModelSharedClusterIT {

private static final long TIME_PARTITION_INTERVAL_IN_MS = 1_000L;
private static final int PRE_PIPE_INSERT_COUNT = 10;
private static final int TOTAL_INSERT_COUNT = 50;

@Override
protected void setupConfig() {
super.setupConfig();
senderEnv
.getConfig()
.getCommonConfig()
.setTimestampPrecision("ms")
.setPartitionInterval(TIME_PARTITION_INTERVAL_IN_MS)
.setAutoCreateSchemaEnabled(true)
.setEnableSeqSpaceCompaction(false)
.setEnableUnseqSpaceCompaction(false)
.setEnableCrossSpaceCompaction(false)
.setPipeMemoryManagementEnabled(false)
.setIsPipeEnableMemoryCheck(false)
.setDnConnectionTimeoutMs(600000);
receiverEnv
.getConfig()
.getCommonConfig()
.setTimestampPrecision("ms")
.setAutoCreateSchemaEnabled(true)
.setPipeMemoryManagementEnabled(false)
.setIsPipeEnableMemoryCheck(false)
.setDnConnectionTimeoutMs(600000);
}

@Test
public void testHistoricalPipeClosesTsFilesCreatedDuringWriting() throws Exception {
final DataNodeWrapper receiverDataNode = receiverEnv.getDataNodeWrapper(0);
final String receiverIp = receiverDataNode.getIp();
final int receiverPort = receiverDataNode.getPort();

try (final Connection connection = senderEnv.getConnection();
final Statement statement = connection.createStatement()) {
statement.execute("create database root.db");
}

final CountDownLatch initialWritesCompleted = new CountDownLatch(1);
final CountDownLatch continueWriting = new CountDownLatch(1);
final ExecutorService writerExecutor = Executors.newSingleThreadExecutor();
final Future<?> writeFuture =
writerExecutor.submit(
() -> {
try (final Connection connection = senderEnv.getConnection();
final Statement statement = connection.createStatement()) {
for (int index = 0; index < TOTAL_INSERT_COUNT; ++index) {
final long timestamp = index * TIME_PARTITION_INTERVAL_IN_MS + 1;
statement.execute(
"insert into root.db.d1 (time, s1) values ("
+ timestamp
+ ", "
+ index
+ ")");

if (index + 1 == PRE_PIPE_INSERT_COUNT) {
initialWritesCompleted.countDown();
if (!continueWriting.await(1, TimeUnit.MINUTES)) {
throw new AssertionError("Timed out waiting to continue the writes");
}
} else if (index >= PRE_PIPE_INSERT_COUNT) {
TimeUnit.MILLISECONDS.sleep(40);
}
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
});

try {
Assert.assertTrue(initialWritesCompleted.await(1, TimeUnit.MINUTES));

final Map<String, String> sourceAttributes = new HashMap<>();
sourceAttributes.put("source.inclusion", "data.insert");
sourceAttributes.put("source.pattern", "root.db.**");
sourceAttributes.put("source.history.enable", "true");
sourceAttributes.put("source.realtime.enable", "true");
sourceAttributes.put("user", "root");

final Map<String, String> sinkAttributes = new HashMap<>();
sinkAttributes.put("sink", "iotdb-thrift-sink");
sinkAttributes.put("sink.batch.enable", "false");
sinkAttributes.put("sink.ip", receiverIp);
sinkAttributes.put("sink.port", Integer.toString(receiverPort));

try (final SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
continueWriting.countDown();
final TSStatus createStatus =
client.createPipe(
new TCreatePipeReq("historicalTsFileClose", sinkAttributes)
.setExtractorAttributes(sourceAttributes)
.setProcessorAttributes(Collections.emptyMap()));
Assert.assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(), createStatus.getCode());

final TSStatus startStatus = client.startPipe("historicalTsFileClose");
Assert.assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(), startStatus.getCode());
}

writeFuture.get(2, TimeUnit.MINUTES);

final Set<String> expectedRows = new HashSet<>();
for (int index = 0; index < TOTAL_INSERT_COUNT; ++index) {
expectedRows.add(index * TIME_PARTITION_INTERVAL_IN_MS + 1 + "," + index + ",");
}
await()
.pollInSameThread()
.pollDelay(0, TimeUnit.MILLISECONDS)
.pollInterval(1, TimeUnit.SECONDS)
.atMost(2, TimeUnit.MINUTES)
.untilAsserted(
() -> {
try (final Connection connection = receiverEnv.getConnection();
final Statement statement = connection.createStatement();
final ResultSet resultSet =
statement.executeQuery("select s1 from root.db.d1")) {
TestUtils.assertResultSetEqual(resultSet, "Time,root.db.d1.s1,", expectedRows);
}
});
} finally {
continueWriting.countDown();
writerExecutor.shutdownNow();
Assert.assertTrue(writerExecutor.awaitTermination(1, TimeUnit.MINUTES));
}
}
}
Loading
Loading