Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ public void testTabletInsertRedirectsToInternalLeaderEndpoint() throws Exception
session.insert(createTablet(2L));

final Session underlyingSession = InternalSessionITUtils.getUnderlyingSession(session);
final Map<?, TEndPoint> tableModelDeviceIdToEndpoint =
final Map<?, Map<?, TEndPoint>> tableModelDeviceIdToEndpointByDatabase =
InternalSessionITUtils.getSessionField(underlyingSession, "tableModelDeviceIdToEndpoint");
assertEquals(1, tableModelDeviceIdToEndpointByDatabase.size());
final Map<?, TEndPoint> tableModelDeviceIdToEndpoint =
tableModelDeviceIdToEndpointByDatabase.values().iterator().next();
assertEquals(1, tableModelDeviceIdToEndpoint.size());
assertTrue(leaderEndPoints.contains(tableModelDeviceIdToEndpoint.values().iterator().next()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,69 @@ public static void main(String[] args)
}
}

@Test
public void testInsertTabletIntoSpecifiedDatabase()
throws IoTDBConnectionException, StatementExecutionException {
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
session.executeNonQueryStatement(
"CREATE TABLE db1.tablet_database (tag1 STRING TAG, m1 DOUBLE FIELD)");
session.executeNonQueryStatement(
"CREATE TABLE db2.tablet_database (tag1 STRING TAG, m1 DOUBLE FIELD)");

List<String> columnNames = Arrays.asList("tag1", "m1");
List<TSDataType> dataTypes = Arrays.asList(TSDataType.STRING, TSDataType.DOUBLE);
List<ColumnCategory> columnCategories =
Arrays.asList(ColumnCategory.TAG, ColumnCategory.FIELD);
Tablet tablet = new Tablet("tablet_database", columnNames, dataTypes, columnCategories, 2);
for (int row = 0; row < 2; row++) {
tablet.addTimestamp(row, row);
tablet.addValue("tag1", row, "tag:" + row);
tablet.addValue("m1", row, row * 1.0);
}

// The distinct tag values exercise the grouped multi-device write path.
session.insert(tablet, "db1");
session.executeNonQueryStatement("USE db1");
session.insert(tablet, "db2");
Tablet currentDatabaseTablet =
new Tablet("tablet_database", columnNames, dataTypes, columnCategories, 1);
currentDatabaseTablet.addTimestamp(0, 2);
currentDatabaseTablet.addValue("tag1", 0, "tag:2");
currentDatabaseTablet.addValue("m1", 0, 2.0);
session.insert(currentDatabaseTablet, null);

try (SessionDataSet dataSet =
session.executeQueryStatement("SELECT * FROM db1.tablet_database")) {
assertTrue(dataSet.hasNext());
dataSet.next();
assertTrue(dataSet.hasNext());
dataSet.next();
assertTrue(dataSet.hasNext());
dataSet.next();
assertFalse(dataSet.hasNext());
}
try (SessionDataSet dataSet =
session.executeQueryStatement("SELECT * FROM db2.tablet_database")) {
assertTrue(dataSet.hasNext());
dataSet.next();
assertTrue(dataSet.hasNext());
dataSet.next();
assertFalse(dataSet.hasNext());
}
try (SessionDataSet dataSet = session.executeQueryStatement("SHOW CURRENT_DATABASE")) {
assertTrue(dataSet.hasNext());
assertEquals("db1", dataSet.next().getField(0).getStringValue());
}

for (String invalidDatabase : Arrays.asList("", " ", "a.b", "root.treedb", "ROOT.treedb")) {
StatementExecutionException exception =
assertThrows(
StatementExecutionException.class, () -> session.insert(tablet, invalidDatabase));
assertEquals(TSStatusCode.SEMANTIC_ERROR.getStatusCode(), exception.getStatusCode());
}
}
}

private static void insertRelationalTabletPerformanceTest()
throws IoTDBConnectionException, StatementExecutionException {
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ public void testInternalPullConsumerPollsAcrossDataNodes() throws Exception {
session.executeNonQueryStatement("flush");

final Session underlyingSession = InternalSessionITUtils.getUnderlyingSession(session);
final Map<?, TEndPoint> deviceToEndpoint =
final Map<?, Map<?, TEndPoint>> deviceToEndpointByDatabase =
InternalSessionITUtils.getSessionField(
underlyingSession, "tableModelDeviceIdToEndpoint");
leaders.addAll(deviceToEndpoint.values());
deviceToEndpointByDatabase
.values()
.forEach(deviceToEndpoint -> leaders.addAll(deviceToEndpoint.values()));
}

Assert.assertTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ void open(
boolean enableRPCCompression,
int connectionTimeoutInMs,
Map<String, TEndPoint> deviceIdToEndpoint,
Map<IDeviceID, TEndPoint> tabletModelDeviceIdToEndpoint,
Map<IDeviceID, TEndPoint> tableModelDeviceIdToEndpoint,
INodeSupplier nodeSupplier)
throws IoTDBConnectionException;

void openWithTableModelDatabaseCache(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds an internal cache-sharing hook to ISession, a public interface that users program against, and its nested-map parameter exposes the cache layout. The only callers are in SessionPool. The method has to be on the interface only because getSession() declares ISession session = queue.poll();. Opening the new connection through a local Session would avoid that:

Session newSession = constructNewSession();
newSession.openWithTableModelDatabaseCache(...);
session = newSession;

tryConstructNewSession() already uses a Session.

Separately, the new abstract methods here and the new ITableSession.insert(Tablet, String) break every third-party implementation of these interfaces at compile time. ISession already uses default methods for this kind of evolution (e.g. isEnableCacheLeader). insert(Tablet, String) could be a default method that delegates to insert(tablet) when database == null and throws UnsupportedOperationException otherwise.

boolean enableRPCCompression,
int connectionTimeoutInMs,
Map<String, TEndPoint> deviceIdToEndpoint,
Map<String, Map<IDeviceID, TEndPoint>> tableModelDeviceIdToEndpoint,
INodeSupplier nodeSupplier)
throws IoTDBConnectionException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ public interface ITableSession extends AutoCloseable {
*/
void insert(Tablet tablet) throws StatementExecutionException, IoTDBConnectionException;

/**
* Inserts a {@link Tablet} into the specified database for this invocation only.
*
* <p>The database does not become the current database of this session and subsequent operations
* are unaffected.
*
* @param tablet the tablet containing time-series data to be inserted.
* @param database the target database for this insertion; if null, the current database of this
* session is used.
* @throws StatementExecutionException if an error occurs while executing the statement.
* @throws IoTDBConnectionException if there is an issue with the IoTDB connection.
*/
void insert(Tablet tablet, String database)
throws StatementExecutionException, IoTDBConnectionException;

/**
* Executes a non-query SQL statement, such as a DDL or DML command.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ public class SessionConfig {

public static final String DEFAULT_SSL_PROTOCOL = "TLS";

public static final String SUPPORTS_DATABASE_SCOPED_TABLET = "supports_database_scoped_tablet";

private SessionConfig() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.isession.util;

import java.util.regex.Pattern;

/** Utilities for table-model database names. */
public final class TableModelDatabaseUtils {

private static final Pattern DATABASE_NAME_PATTERN =
Pattern.compile("([a-zA-Z0-9`_\\-\\u2E80-\\u9FFF]+)");

private TableModelDatabaseUtils() {}

public static boolean isValidDatabaseName(String databaseName, int maxLength) {
return databaseName != null
&& databaseName.length() <= maxLength
&& databaseName.indexOf('.') < 0
&& DATABASE_NAME_PATTERN.matcher(databaseName).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ private SessionMessages() {}
public static final String EXCEPTION_SESSION_CAN_NOT_CONNECT_TO_ARG_226FBA66 =
"Session can not connect to {}";

public static final String
EXCEPTION_SERVER_DOES_NOT_SUPPORT_DATABASE_SCOPED_TABLET_INSERTION_01CFFAF8 =
"The connected server does not support database-scoped Tablet insertion. Upgrade the"
+ " server to use insert(Tablet, String database).";

public static final String
EXCEPTION_ALL_VALUES_ARE_NULL_AND_THIS_SUBMISSION_IS_IGNORED_DEVICEID_IS_ARG_TIME_IS_ARG_MEASUREMENTS_IS_ARG_5524221C =
"All values are null and this submission is ignored,deviceId is [{}],time is"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ private SessionMessages() {}

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

public static final String
EXCEPTION_SERVER_DOES_NOT_SUPPORT_DATABASE_SCOPED_TABLET_INSERTION_01CFFAF8 =
"连接的服务器不支持按 database 写入 Tablet。请升级服务器后使用 insert(Tablet, String database)。";

public static final String
EXCEPTION_ALL_VALUES_ARE_NULL_AND_THIS_SUBMISSION_IS_IGNORED_DEVICEID_IS_ARG_TIME_IS_ARG_MEASUREMENTS_IS_ARG_5524221C =
"所有值均为空,本次提交被忽略,deviceId 为 [{}],time 为 [{}],measurements 为 [{}]";
Expand Down
Loading
Loading