Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe: support replace and modify mode for alter pipe sql #12018

Merged
merged 13 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
import org.apache.iotdb.confignode.rpc.thrift.TShowPipeInfo;
import org.apache.iotdb.confignode.rpc.thrift.TShowPipeReq;
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.MultiClusterIT2;
Expand All @@ -34,7 +35,10 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.fail;

Expand All @@ -43,13 +47,13 @@
public class IoTDBPipeAlterIT extends AbstractPipeDualIT {

@Test
public void testBasicPipeAlter() throws Exception {
public void testBasicAlterPipe() throws Exception {
DataNodeWrapper receiverDataNode = receiverEnv.getDataNodeWrapper(0);

// create pipe
String sql =
String.format(
"create pipe a2b with sink ('node-urls'='%s', 'batch.enable'='false')",
"create pipe a2b with processor ('processor'='do-nothing-processor') with sink ('node-urls'='%s')",
receiverDataNode.getIpAndPortString());
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
Expand All @@ -58,15 +62,24 @@ public void testBasicPipeAlter() throws Exception {
fail(e.getMessage());
}

// show pipe and record first creation time
long firstCreationTime;
// show pipe
long lastCreationTime;
try (SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
List<TShowPipeInfo> showPipeResult = client.showPipe(new TShowPipeReq()).pipeInfoList;
Assert.assertEquals(1, showPipeResult.size());
Assert.assertTrue(showPipeResult.get(0).pipeConnector.contains("batch.enable=false"));
// check status
Assert.assertEquals("RUNNING", showPipeResult.get(0).state);
firstCreationTime = showPipeResult.get(0).creationTime;
// check configurations
Assert.assertTrue(
showPipeResult.get(0).pipeProcessor.contains("processor=do-nothing-processor"));
Assert.assertTrue(
showPipeResult
.get(0)
.pipeConnector
.contains(String.format("node-urls=%s", receiverDataNode.getIpAndPortString())));
// record last creation time
lastCreationTime = showPipeResult.get(0).creationTime;
}

// stop pipe
Expand All @@ -82,17 +95,14 @@ public void testBasicPipeAlter() throws Exception {
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
List<TShowPipeInfo> showPipeResult = client.showPipe(new TShowPipeReq()).pipeInfoList;
Assert.assertEquals(1, showPipeResult.size());
// check status
Assert.assertEquals("STOPPED", showPipeResult.get(0).state);
}

// alter pipe
sql =
String.format(
"alter pipe a2b modify sink ('node-urls'='%s', 'batch.enable'='true')",
receiverDataNode.getIpAndPortString());
// alter pipe (modify)
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute(sql);
statement.execute("alter pipe a2b modify sink ('sink.batch.enable'='false')");
} catch (SQLException e) {
fail(e.getMessage());
}
Expand All @@ -102,17 +112,159 @@ public void testBasicPipeAlter() throws Exception {
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
List<TShowPipeInfo> showPipeResult = client.showPipe(new TShowPipeReq()).pipeInfoList;
Assert.assertEquals(1, showPipeResult.size());
Assert.assertTrue(showPipeResult.get(0).pipeConnector.contains("batch.enable=true"));
// check status
Assert.assertEquals("STOPPED", showPipeResult.get(0).state);
// alter pipe will reset pipe creation time
Assert.assertTrue(showPipeResult.get(0).creationTime > firstCreationTime);
// alter pipe will clear exception messages
// check configurations
Assert.assertTrue(showPipeResult.get(0).pipeConnector.contains("batch.enable=false"));
Assert.assertTrue(
showPipeResult.get(0).pipeProcessor.contains("processor=do-nothing-processor"));
Assert.assertTrue(
showPipeResult
.get(0)
.pipeConnector
.contains(String.format("node-urls=%s", receiverDataNode.getIpAndPortString())));
// check creation time and record last creation time
Assert.assertTrue(showPipeResult.get(0).creationTime > lastCreationTime);
lastCreationTime = showPipeResult.get(0).creationTime;
// check exception message
Assert.assertEquals("", showPipeResult.get(0).exceptionMessage);
}

// start pipe
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("start pipe a2b");
} catch (SQLException e) {
fail(e.getMessage());
}

// alter pipe (replace)
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("alter pipe a2b replace processor ('processor'='down-sampling-processor')");
} catch (SQLException e) {
fail(e.getMessage());
}

// show pipe
try (SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
List<TShowPipeInfo> showPipeResult = client.showPipe(new TShowPipeReq()).pipeInfoList;
Assert.assertEquals(1, showPipeResult.size());
// check status
Assert.assertEquals("RUNNING", showPipeResult.get(0).state);
// check configurations
Assert.assertTrue(
showPipeResult.get(0).pipeProcessor.contains("processor=down-sampling-processor"));
Assert.assertTrue(showPipeResult.get(0).pipeConnector.contains("batch.enable=false"));
Assert.assertTrue(
showPipeResult
.get(0)
.pipeConnector
.contains(String.format("node-urls=%s", receiverDataNode.getIpAndPortString())));
// check creation time and record last creation time
Assert.assertTrue(showPipeResult.get(0).creationTime > lastCreationTime);
lastCreationTime = showPipeResult.get(0).creationTime;
// check exception message
Assert.assertEquals("", showPipeResult.get(0).exceptionMessage);
}

// alter pipe (modify)
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("alter pipe a2b modify sink ('connector.batch.enable'='true')");
} catch (SQLException e) {
fail(e.getMessage());
}

// show pipe
try (SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
List<TShowPipeInfo> showPipeResult = client.showPipe(new TShowPipeReq()).pipeInfoList;
Assert.assertEquals(1, showPipeResult.size());
// check status
Assert.assertEquals("RUNNING", showPipeResult.get(0).state);
// check configurations
Assert.assertTrue(showPipeResult.get(0).pipeConnector.contains("batch.enable=true"));
Assert.assertTrue(
showPipeResult.get(0).pipeProcessor.contains("processor=down-sampling-processor"));
Assert.assertTrue(
showPipeResult
.get(0)
.pipeConnector
.contains(String.format("node-urls=%s", receiverDataNode.getIpAndPortString())));
// check creation time and record last creation time
Assert.assertTrue(showPipeResult.get(0).creationTime > lastCreationTime);
lastCreationTime = showPipeResult.get(0).creationTime;
// check exception message
Assert.assertEquals("", showPipeResult.get(0).exceptionMessage);
}

// alter pipe (replace empty)
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("alter pipe a2b replace processor ()");
} catch (SQLException e) {
fail(e.getMessage());
}

// show pipe
try (SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
List<TShowPipeInfo> showPipeResult = client.showPipe(new TShowPipeReq()).pipeInfoList;
Assert.assertEquals(1, showPipeResult.size());
// check status
Assert.assertEquals("RUNNING", showPipeResult.get(0).state);
// check configurations
Assert.assertTrue(showPipeResult.get(0).pipeConnector.contains("batch.enable=true"));
Assert.assertFalse(
showPipeResult.get(0).pipeProcessor.contains("processor=down-sampling-processor"));
Assert.assertTrue(
showPipeResult
.get(0)
.pipeConnector
.contains(String.format("node-urls=%s", receiverDataNode.getIpAndPortString())));
// check creation time and record last creation time
Assert.assertTrue(showPipeResult.get(0).creationTime > lastCreationTime);
lastCreationTime = showPipeResult.get(0).creationTime;
// check exception message
Assert.assertEquals("", showPipeResult.get(0).exceptionMessage);
}

// alter pipe (modify empty)
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("alter pipe a2b modify sink ()");
} catch (SQLException e) {
fail(e.getMessage());
}

// show pipe
try (SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) senderEnv.getLeaderConfigNodeConnection()) {
List<TShowPipeInfo> showPipeResult = client.showPipe(new TShowPipeReq()).pipeInfoList;
Assert.assertEquals(1, showPipeResult.size());
// check status
Assert.assertEquals("RUNNING", showPipeResult.get(0).state);
// check configurations
Assert.assertTrue(showPipeResult.get(0).pipeConnector.contains("batch.enable=true"));
Assert.assertFalse(
showPipeResult.get(0).pipeProcessor.contains("processor=down-sampling-processor"));
Assert.assertTrue(
showPipeResult
.get(0)
.pipeConnector
.contains(String.format("node-urls=%s", receiverDataNode.getIpAndPortString())));
// check creation time and record last creation time
Assert.assertTrue(showPipeResult.get(0).creationTime > lastCreationTime);
lastCreationTime = showPipeResult.get(0).creationTime;
// check exception message
Assert.assertEquals("", showPipeResult.get(0).exceptionMessage);
VGalaxies marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Test
public void testPipeAlterFailure() {
public void testAlterPipeFailure() {
DataNodeWrapper receiverDataNode = receiverEnv.getDataNodeWrapper(0);

// alter non-existed pipe
Expand All @@ -138,17 +290,66 @@ public void testPipeAlterFailure() {
} catch (SQLException e) {
fail(e.getMessage());
}
}

// useless alter
sql =
@Test
public void testAlterPipeProcessor() {
DataNodeWrapper receiverDataNode = receiverEnv.getDataNodeWrapper(0);

// create pipe
String sql =
String.format(
"alter pipe a2b modify sink ('node-urls'='%s', 'batch.enable'='false')",
"create pipe a2b with processor ('processor'='down-sampling-processor', 'down-sampling.interval-seconds'='1', 'down-sampling.split-file'='true') with sink ('node-urls'='%s', 'batch.enable'='false')",
receiverDataNode.getIpAndPortString());
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute(sql);
} catch (SQLException e) {
fail(e.getMessage());
}

// insert data on sender
if (!TestUtils.tryExecuteNonQueriesWithRetry(
senderEnv,
Arrays.asList(
"insert into root.db.d1 (time, at1) values (1000, 1), (1500, 2), (2000, 3), (2500, 4), (3000, 5)",
"flush"))) {
fail();
}

// check data on receiver
Set<String> expectedResSet = new HashSet<>();
expectedResSet.add("1000,1.0,");
expectedResSet.add("2000,3.0,");
expectedResSet.add("3000,5.0,");
TestUtils.assertDataOnEnv(
receiverEnv, "select * from root.**", "Time,root.db.d1.at1,", expectedResSet);

// alter pipe (modify 'down-sampling.interval-seconds')
try (Connection connection = senderEnv.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("alter pipe a2b modify processor ('down-sampling.interval-seconds'='2')");
} catch (SQLException e) {
fail(e.getMessage());
}

// insert data on sender
if (!TestUtils.tryExecuteNonQueriesWithRetry(
senderEnv,
Arrays.asList(
"insert into root.db.d1 (time, at1) values (11000, 1), (11500, 2), (12000, 3), (12500, 4), (13000, 5)",
"flush"))) {
fail();
} catch (SQLException ignore) {
}

// check data on receiver
expectedResSet.clear();
expectedResSet.add("11000,1.0,");
expectedResSet.add("13000,5.0,");
TestUtils.assertDataOnEnv(
VGalaxies marked this conversation as resolved.
Show resolved Hide resolved
receiverEnv,
"select * from root.** where time > 10000",
"Time,root.db.d1.at1,",
expectedResSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -281,30 +280,25 @@ public String toString() {
}

/**
* This method uses {@link KeyReducer} to reduce keys and then sorts them to determine if two
* PipeParameters are equivalent.
* This method adds (non-existed) or replaces (existed) equivalent attributes in this
* PipeParameters with those from another PipeParameters.
*
* @param that provide the key that needs to be updated along with the value
* @return this pipe parameters
*/
public boolean isEquivalent(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
PipeParameters that = (PipeParameters) obj;
return Objects.equals(
public PipeParameters addOrReplaceEquivalentAttributes(PipeParameters that) {
Map<String, Entry<String, String>> thisMap =
this.attributes.entrySet().stream()
.collect(
Collectors.toMap(
entry -> KeyReducer.reduce(entry.getKey()), Entry::getValue,
(oldValue, newValue) -> oldValue, TreeMap::new))
.toString(),
.collect(Collectors.toMap(entry -> KeyReducer.reduce(entry.getKey()), entry -> entry));
Map<String, Entry<String, String>> thatMap =
that.attributes.entrySet().stream()
.collect(
Collectors.toMap(
entry -> KeyReducer.reduce(entry.getKey()), Entry::getValue,
(oldValue, newValue) -> oldValue, TreeMap::new))
.toString());
.collect(Collectors.toMap(entry -> KeyReducer.reduce(entry.getKey()), entry -> entry));
thatMap.forEach(
(key, entry) -> {
this.attributes.remove(thisMap.getOrDefault(key, entry).getKey());
this.attributes.put(entry.getKey(), entry.getValue());
});
return this;
}

private static class KeyReducer {
Expand Down Expand Up @@ -334,6 +328,7 @@ static String reduce(String key) {
}

public static class ValueHider {

private static final Set<String> KEYS = new HashSet<>();

private static final String PLACEHOLDER = "******";
Expand Down
Loading
Loading