Skip to content

Commit dc790cc

Browse files
authored
Support database-scoped tablet insertion in table sessions
1 parent 43b531d commit dc790cc

27 files changed

Lines changed: 673 additions & 64 deletions

File tree

‎integration-test/src/test/java/org/apache/iotdb/relational/it/session/InternalTableSessionRedirectIT.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ public void testTabletInsertRedirectsToInternalLeaderEndpoint() throws Exception
114114
session.insert(createTablet(2L));
115115

116116
final Session underlyingSession = InternalSessionITUtils.getUnderlyingSession(session);
117-
final Map<?, TEndPoint> tableModelDeviceIdToEndpoint =
117+
final Map<?, Map<?, TEndPoint>> tableModelDeviceIdToEndpointByDatabase =
118118
InternalSessionITUtils.getSessionField(underlyingSession, "tableModelDeviceIdToEndpoint");
119+
assertEquals(1, tableModelDeviceIdToEndpointByDatabase.size());
120+
final Map<?, TEndPoint> tableModelDeviceIdToEndpoint =
121+
tableModelDeviceIdToEndpointByDatabase.values().iterator().next();
119122
assertEquals(1, tableModelDeviceIdToEndpoint.size());
120123
assertTrue(leaderEndPoints.contains(tableModelDeviceIdToEndpoint.values().iterator().next()));
121124
}

‎integration-test/src/test/java/org/apache/iotdb/relational/it/session/IoTDBSessionRelationalIT.java‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,68 @@ public static void main(String[] args)
178178
}
179179
}
180180

181+
@Test
182+
public void testInsertTabletIntoSpecifiedDatabase()
183+
throws IoTDBConnectionException, StatementExecutionException {
184+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
185+
session.executeNonQueryStatement(
186+
"CREATE TABLE db1.tablet_database (tag1 STRING TAG, m1 DOUBLE FIELD)");
187+
session.executeNonQueryStatement(
188+
"CREATE TABLE db2.tablet_database (tag1 STRING TAG, m1 DOUBLE FIELD)");
189+
190+
List<String> columnNames = Arrays.asList("tag1", "m1");
191+
List<TSDataType> dataTypes = Arrays.asList(TSDataType.STRING, TSDataType.DOUBLE);
192+
List<ColumnCategory> columnCategories =
193+
Arrays.asList(ColumnCategory.TAG, ColumnCategory.FIELD);
194+
Tablet tablet = new Tablet("tablet_database", columnNames, dataTypes, columnCategories, 2);
195+
for (int row = 0; row < 2; row++) {
196+
tablet.addTimestamp(row, row);
197+
tablet.addValue("tag1", row, "tag:" + row);
198+
tablet.addValue("m1", row, row * 1.0);
199+
}
200+
201+
session.insert(tablet, "db1");
202+
session.executeNonQueryStatement("USE db1");
203+
session.insert(tablet, "db2");
204+
Tablet currentDatabaseTablet =
205+
new Tablet("tablet_database", columnNames, dataTypes, columnCategories, 1);
206+
currentDatabaseTablet.addTimestamp(0, 2);
207+
currentDatabaseTablet.addValue("tag1", 0, "tag:2");
208+
currentDatabaseTablet.addValue("m1", 0, 2.0);
209+
session.insert(currentDatabaseTablet, null);
210+
211+
try (SessionDataSet dataSet =
212+
session.executeQueryStatement("SELECT * FROM db1.tablet_database")) {
213+
assertTrue(dataSet.hasNext());
214+
dataSet.next();
215+
assertTrue(dataSet.hasNext());
216+
dataSet.next();
217+
assertTrue(dataSet.hasNext());
218+
dataSet.next();
219+
assertFalse(dataSet.hasNext());
220+
}
221+
try (SessionDataSet dataSet =
222+
session.executeQueryStatement("SELECT * FROM db2.tablet_database")) {
223+
assertTrue(dataSet.hasNext());
224+
dataSet.next();
225+
assertTrue(dataSet.hasNext());
226+
dataSet.next();
227+
assertFalse(dataSet.hasNext());
228+
}
229+
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW CURRENT_DATABASE")) {
230+
assertTrue(dataSet.hasNext());
231+
assertEquals("db1", dataSet.next().getField(0).getStringValue());
232+
}
233+
234+
for (String invalidDatabase : Arrays.asList("", " ", "a.b", "root.treedb", "ROOT.treedb")) {
235+
StatementExecutionException exception =
236+
assertThrows(
237+
StatementExecutionException.class, () -> session.insert(tablet, invalidDatabase));
238+
assertEquals(TSStatusCode.ILLEGAL_PATH.getStatusCode(), exception.getStatusCode());
239+
}
240+
}
241+
}
242+
181243
private static void insertRelationalTabletPerformanceTest()
182244
throws IoTDBConnectionException, StatementExecutionException {
183245
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {

‎integration-test/src/test/java/org/apache/iotdb/subscription/it/consensus/local/tablemodel/InternalSubscriptionTableIT.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ public void testInternalPullConsumerPollsAcrossDataNodes() throws Exception {
163163
session.executeNonQueryStatement("flush");
164164

165165
final Session underlyingSession = InternalSessionITUtils.getUnderlyingSession(session);
166-
final Map<?, TEndPoint> deviceToEndpoint =
166+
final Map<?, Map<?, TEndPoint>> deviceToEndpointByDatabase =
167167
InternalSessionITUtils.getSessionField(
168168
underlyingSession, "tableModelDeviceIdToEndpoint");
169-
leaders.addAll(deviceToEndpoint.values());
169+
deviceToEndpointByDatabase
170+
.values()
171+
.forEach(deviceToEndpoint -> leaders.addAll(deviceToEndpoint.values()));
170172
}
171173

172174
Assert.assertTrue(

‎iotdb-client/isession/src/main/java/org/apache/iotdb/isession/ISession.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ void open(
7070
boolean enableRPCCompression,
7171
int connectionTimeoutInMs,
7272
Map<String, TEndPoint> deviceIdToEndpoint,
73-
Map<IDeviceID, TEndPoint> tabletModelDeviceIdToEndpoint,
73+
Map<IDeviceID, TEndPoint> tableModelDeviceIdToEndpoint,
74+
INodeSupplier nodeSupplier)
75+
throws IoTDBConnectionException;
76+
77+
void openWithTableModelDatabaseCache(
78+
boolean enableRPCCompression,
79+
int connectionTimeoutInMs,
80+
Map<String, TEndPoint> deviceIdToEndpoint,
81+
Map<String, Map<IDeviceID, TEndPoint>> tableModelDeviceIdToEndpoint,
7482
INodeSupplier nodeSupplier)
7583
throws IoTDBConnectionException;
7684

‎iotdb-client/isession/src/main/java/org/apache/iotdb/isession/ITableSession.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,34 @@ public interface ITableSession extends AutoCloseable {
4040
/**
4141
* Inserts a {@link Tablet} into the database.
4242
*
43+
* <p>A multi-device Tablet may be written to multiple DataNodes. The operation is not atomic;
44+
* some rows may be persisted even if an error is returned.
45+
*
4346
* @param tablet the tablet containing time-series data to be inserted.
4447
* @throws StatementExecutionException if an error occurs while executing the statement.
4548
* @throws IoTDBConnectionException if there is an issue with the IoTDB connection.
4649
*/
4750
void insert(Tablet tablet) throws StatementExecutionException, IoTDBConnectionException;
4851

52+
/**
53+
* Inserts a {@link Tablet} into the specified database for this invocation only.
54+
*
55+
* <p>The database does not become the current database of this session and subsequent operations
56+
* are unaffected.
57+
*
58+
* <p>A multi-device Tablet may be written to multiple DataNodes. The operation is not atomic;
59+
* some rows may be persisted even if an error is returned. Mixed-version clusters in which some
60+
* DataNodes do not support database-scoped Tablet insertion are not supported.
61+
*
62+
* @param tablet the tablet containing time-series data to be inserted.
63+
* @param database the target database for this insertion; if null, the current database of this
64+
* session is used.
65+
* @throws StatementExecutionException if an error occurs while executing the statement.
66+
* @throws IoTDBConnectionException if there is an issue with the IoTDB connection.
67+
*/
68+
void insert(Tablet tablet, String database)
69+
throws StatementExecutionException, IoTDBConnectionException;
70+
4971
/**
5072
* Executes a non-query SQL statement, such as a DDL or DML command.
5173
*

‎iotdb-client/isession/src/main/java/org/apache/iotdb/isession/SessionConfig.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ public class SessionConfig {
5858

5959
public static final String DEFAULT_SSL_PROTOCOL = "TLS";
6060

61+
public static final String SUPPORTS_DATABASE_SCOPED_TABLET = "supports_database_scoped_tablet";
62+
6163
private SessionConfig() {}
6264
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.isession.util;
21+
22+
import java.util.regex.Pattern;
23+
24+
/** Utilities for table-model database names. */
25+
public final class TableModelDatabaseUtils {
26+
27+
public static final int MAX_DATABASE_NAME_LENGTH = 64;
28+
29+
private static final Pattern DATABASE_NAME_PATTERN =
30+
Pattern.compile("([a-zA-Z0-9`_\\-\\u2E80-\\u9FFF]+)");
31+
32+
private TableModelDatabaseUtils() {
33+
// Utility class.
34+
}
35+
36+
public static boolean isValidDatabaseName(String databaseName, int maxLength) {
37+
return databaseName != null
38+
&& databaseName.length() <= maxLength
39+
&& DATABASE_NAME_PATTERN.matcher(databaseName).matches();
40+
}
41+
}

‎iotdb-client/session/src/main/i18n/en/org/apache/iotdb/session/i18n/SessionMessages.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@ private SessionMessages() {}
312312
public static final String EXCEPTION_SESSION_CAN_NOT_CONNECT_TO_ARG_226FBA66 =
313313
"Session can not connect to {}";
314314

315+
public static final String EXCEPTION_INVALID_TABLE_MODEL_DATABASE_NAME_ARG_ARG_BC5566E2 =
316+
"Invalid table-model database name \"%s\". Expected an identifier of at most %d characters"
317+
+ " using letters, numbers, Chinese characters, backticks, underscores, or hyphens.";
318+
319+
public static final String
320+
EXCEPTION_SERVER_DOES_NOT_SUPPORT_DATABASE_SCOPED_TABLET_INSERTION_01CFFAF8 =
321+
"The connected server does not support database-scoped Tablet insertion. Upgrade the"
322+
+ " server to use insert(Tablet, String database).";
323+
315324
public static final String
316325
EXCEPTION_ALL_VALUES_ARE_NULL_AND_THIS_SUBMISSION_IS_IGNORED_DEVICEID_IS_ARG_TIME_IS_ARG_MEASUREMENTS_IS_ARG_5524221C =
317326
"All values are null and this submission is ignored,deviceId is [{}],time is"

‎iotdb-client/session/src/main/i18n/zh/org/apache/iotdb/session/i18n/SessionMessages.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,13 @@ private SessionMessages() {}
309309

310310
public static final String EXCEPTION_SESSION_CAN_NOT_CONNECT_TO_ARG_226FBA66 = "Session 无法连接到 {}";
311311

312+
public static final String EXCEPTION_INVALID_TABLE_MODEL_DATABASE_NAME_ARG_ARG_BC5566E2 =
313+
"表模型数据库名称“%s”无效。名称长度不得超过 %d 个字符,且只能包含字母、数字、中文字符、反引号、下划线或连字符。";
314+
315+
public static final String
316+
EXCEPTION_SERVER_DOES_NOT_SUPPORT_DATABASE_SCOPED_TABLET_INSERTION_01CFFAF8 =
317+
"连接的服务器不支持按 database 写入 Tablet。请升级服务器后使用 insert(Tablet, String database)。";
318+
312319
public static final String
313320
EXCEPTION_ALL_VALUES_ARE_NULL_AND_THIS_SUBMISSION_IS_IGNORED_DEVICEID_IS_ARG_TIME_IS_ARG_MEASUREMENTS_IS_ARG_5524221C =
314321
"所有值均为空,本次提交被忽略,deviceId 为 [{}],time 为 [{}],measurements 为 [{}]";

0 commit comments

Comments
 (0)