org.apache.iotdb.session.pool.SessionPool Maven / Gradle / Ivy
/*
* 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.session.pool;
import org.apache.iotdb.isession.Config;
import org.apache.iotdb.isession.ISession;
import org.apache.iotdb.isession.ISessionDataSet;
import org.apache.iotdb.isession.pool.ISessionDataSetWrapper;
import org.apache.iotdb.isession.pool.ISessionPool;
import org.apache.iotdb.isession.template.Template;
import org.apache.iotdb.isession.util.SystemStatus;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.service.rpc.thrift.TSBackupConfigurationResp;
import org.apache.iotdb.service.rpc.thrift.TSConnectionInfoResp;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
import org.apache.iotdb.tsfile.write.record.Tablet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentMap;
/**
* SessionPool is a wrapper of a Session Set. Using SessionPool, the user do not need to consider
* how to reuse a session connection. Even if the session is disconnected, the session pool can
* recognize it and remove the broken session connection and create a new one.
*
* If there is no available connections and the pool reaches its max size, the all methods will
* hang until there is a available connection.
*
*
If a user has waited for a session for more than 60 seconds, a warn log will be printed.
*
*
The only thing you have to remember is that:
*
*
For a query, if you have get all data, i.e., SessionDataSetWrapper.hasNext() == false, it is
* ok. Otherwise, i.e., you want to stop the query before you get all data
* (SessionDataSetWrapper.hasNext() == true), then you have to call
* closeResultSet(SessionDataSetWrapper wrapper) manually. Otherwise the connection is occupied by
* the query.
*
*
Another case that you have to manually call closeResultSet() is that when there is exception
* when you call SessionDataSetWrapper.hasNext() or next()
*/
public class SessionPool implements ISessionPool {
private static final Logger logger = LoggerFactory.getLogger(SessionPool.class);
public static final String SESSION_POOL_IS_CLOSED = "Session pool is closed";
public static final String CLOSE_THE_SESSION_FAILED = "close the session failed.";
private static final int RETRY = 3;
private static final int FINAL_RETRY = RETRY - 1;
private final ConcurrentLinkedDeque queue = new ConcurrentLinkedDeque<>();
// for session whose resultSet is not released.
private final ConcurrentMap occupied = new ConcurrentHashMap<>();
private int size = 0;
private int maxSize = 0;
private final long waitToGetSessionTimeoutInMs;
// parameters for Session constructor
private final String host;
private final int port;
private final String user;
private final String password;
private final int fetchSize;
private final ZoneId zoneId;
private final boolean enableCacheLeader;
// parameters for Session#open()
private final int connectionTimeoutInMs;
private final boolean enableCompression;
// whether the queue is closed.
private boolean closed;
// Redirect-able SessionPool
private final List nodeUrls;
public SessionPool(String host, int port, String user, String password, int maxSize) {
this(
host,
port,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
false,
null,
Config.DEFAULT_CACHE_LEADER_MODE,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
public SessionPool(List nodeUrls, String user, String password, int maxSize) {
this(
nodeUrls,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
false,
null,
Config.DEFAULT_CACHE_LEADER_MODE,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
public SessionPool(
String host, int port, String user, String password, int maxSize, boolean enableCompression) {
this(
host,
port,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
enableCompression,
null,
Config.DEFAULT_CACHE_LEADER_MODE,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
public SessionPool(
List nodeUrls, String user, String password, int maxSize, boolean enableCompression) {
this(
nodeUrls,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
enableCompression,
null,
Config.DEFAULT_CACHE_LEADER_MODE,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
public SessionPool(
String host,
int port,
String user,
String password,
int maxSize,
boolean enableCompression,
boolean enableCacheLeader) {
this(
host,
port,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
enableCompression,
null,
enableCacheLeader,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
public SessionPool(
List nodeUrls,
String user,
String password,
int maxSize,
boolean enableCompression,
boolean enableCacheLeader) {
this(
nodeUrls,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
enableCompression,
null,
enableCacheLeader,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
public SessionPool(
String host, int port, String user, String password, int maxSize, ZoneId zoneId) {
this(
host,
port,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
false,
zoneId,
Config.DEFAULT_CACHE_LEADER_MODE,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
public SessionPool(
List nodeUrls, String user, String password, int maxSize, ZoneId zoneId) {
this(
nodeUrls,
user,
password,
maxSize,
Config.DEFAULT_FETCH_SIZE,
60_000,
false,
zoneId,
Config.DEFAULT_CACHE_LEADER_MODE,
Config.DEFAULT_CONNECTION_TIMEOUT_MS);
}
@SuppressWarnings("squid:S107")
public SessionPool(
String host,
int port,
String user,
String password,
int maxSize,
int fetchSize,
long waitToGetSessionTimeoutInMs,
boolean enableCompression,
ZoneId zoneId,
boolean enableCacheLeader,
int connectionTimeoutInMs) {
this.maxSize = maxSize;
this.host = host;
this.port = port;
this.nodeUrls = null;
this.user = user;
this.password = password;
this.fetchSize = fetchSize;
this.waitToGetSessionTimeoutInMs = waitToGetSessionTimeoutInMs;
this.enableCompression = enableCompression;
this.zoneId = zoneId;
this.enableCacheLeader = enableCacheLeader;
this.connectionTimeoutInMs = connectionTimeoutInMs;
}
public SessionPool(
List nodeUrls,
String user,
String password,
int maxSize,
int fetchSize,
long waitToGetSessionTimeoutInMs,
boolean enableCompression,
ZoneId zoneId,
boolean enableCacheLeader,
int connectionTimeoutInMs) {
this.maxSize = maxSize;
this.host = null;
this.port = -1;
this.nodeUrls = nodeUrls;
this.user = user;
this.password = password;
this.fetchSize = fetchSize;
this.waitToGetSessionTimeoutInMs = waitToGetSessionTimeoutInMs;
this.enableCompression = enableCompression;
this.zoneId = zoneId;
this.enableCacheLeader = enableCacheLeader;
this.connectionTimeoutInMs = connectionTimeoutInMs;
}
private Session constructNewSession() {
Session session;
if (nodeUrls == null) {
// Construct custom Session
session =
new Session.Builder()
.host(host)
.port(port)
.username(user)
.password(password)
.fetchSize(fetchSize)
.zoneId(zoneId)
.enableCacheLeader(enableCacheLeader)
.build();
} else {
// Construct redirect-able Session
session =
new Session.Builder()
.nodeUrls(nodeUrls)
.username(user)
.password(password)
.fetchSize(fetchSize)
.zoneId(zoneId)
.enableCacheLeader(enableCacheLeader)
.build();
}
return session;
}
// if this method throws an exception, either the server is broken, or the ip/port/user/password
// is incorrect.
@SuppressWarnings({"squid:S3776", "squid:S2446"}) // Suppress high Cognitive Complexity warning
private ISession getSession() throws IoTDBConnectionException {
ISession session = queue.poll();
if (closed) {
throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
}
if (session != null) {
return session;
}
boolean shouldCreate = false;
long start = System.currentTimeMillis();
while (session == null) {
synchronized (this) {
if (size < maxSize) {
// we can create more session
size++;
shouldCreate = true;
// but we do it after skip synchronized block because connection a session is time
// consuming.
break;
}
// we have to wait for someone returns a session.
try {
if (logger.isDebugEnabled()) {
logger.debug("no more sessions can be created, wait... queue.size={}", queue.size());
}
this.wait(1000);
long timeOut = Math.min(waitToGetSessionTimeoutInMs, 60_000);
if (System.currentTimeMillis() - start > timeOut) {
logger.warn(
"the SessionPool has wait for {} seconds to get a new connection: {}:{} with {}, {}",
(System.currentTimeMillis() - start) / 1000,
host,
port,
user,
password);
logger.warn(
"current occupied size {}, queue size {}, considered size {} ",
occupied.size(),
queue.size(),
size);
if (System.currentTimeMillis() - start > waitToGetSessionTimeoutInMs) {
throw new IoTDBConnectionException(
String.format("timeout to get a connection from %s:%s", host, port));
}
}
} catch (InterruptedException e) {
// wake up from this.wait(1000) by this.notify()
}
session = queue.poll();
if (closed) {
throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
}
}
}
if (shouldCreate) {
// create a new one.
if (logger.isDebugEnabled()) {
if (nodeUrls == null) {
logger.debug("Create a new Session {}, {}, {}, {}", host, port, user, password);
} else {
logger.debug("Create a new redirect Session {}, {}, {}", nodeUrls, user, password);
}
}
session = constructNewSession();
try {
session.open(enableCompression, connectionTimeoutInMs);
// avoid someone has called close() the session pool
synchronized (this) {
if (closed) {
// have to release the connection...
session.close();
throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
}
}
} catch (IoTDBConnectionException e) {
// if exception, we will throw the exception.
// Meanwhile, we have to set size--
synchronized (this) {
size--;
// we do not need to notifyAll as any waited thread can continue to work after waked up.
this.notify();
if (logger.isDebugEnabled()) {
logger.debug("open session failed, reduce the count and notify others...");
}
}
throw e;
}
}
return session;
}
@Override
public int currentAvailableSize() {
return queue.size();
}
@Override
public int currentOccupiedSize() {
return occupied.size();
}
@SuppressWarnings({"squid:S2446"})
private void putBack(ISession session) {
queue.push(session);
synchronized (this) {
// we do not need to notifyAll as any waited thread can continue to work after waked up.
this.notify();
// comment the following codes as putBack is too frequently called.
// if (logger.isTraceEnabled()) {
// logger.trace("put a session back and notify others..., queue.size = {}",
// queue.size());
// }
}
}
private void occupy(ISession session) {
occupied.put(session, session);
}
@Override
/** close all connections in the pool */
public synchronized void close() {
for (ISession session : queue) {
try {
session.close();
} catch (IoTDBConnectionException e) {
// do nothing
logger.warn(CLOSE_THE_SESSION_FAILED, e);
}
}
for (ISession session : occupied.keySet()) {
try {
session.close();
} catch (IoTDBConnectionException e) {
// do nothing
logger.warn(CLOSE_THE_SESSION_FAILED, e);
}
}
logger.info("closing the session pool, cleaning queues...");
this.closed = true;
queue.clear();
occupied.clear();
}
@Override
public void closeResultSet(ISessionDataSetWrapper wrapper) {
boolean putback = true;
try {
wrapper.getSessionDataSet().closeOperationHandle();
} catch (IoTDBConnectionException | StatementExecutionException e) {
tryConstructNewSession();
putback = false;
} finally {
ISession session = occupied.remove(wrapper.getSession());
if (putback && session != null) {
putBack(wrapper.getSession());
}
}
}
@SuppressWarnings({"squid:S2446"})
private void tryConstructNewSession() {
Session session = constructNewSession();
try {
session.open(enableCompression, connectionTimeoutInMs);
// avoid someone has called close() the session pool
synchronized (this) {
if (closed) {
// have to release the connection...
session.close();
throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
}
queue.push(session);
this.notify();
}
} catch (IoTDBConnectionException e) {
synchronized (this) {
size--;
// we do not need to notifyAll as any waited thread can continue to work after waked up.
this.notify();
if (logger.isDebugEnabled()) {
logger.debug("open session failed, reduce the count and notify others...");
}
}
}
}
private void closeSession(ISession session) {
if (session != null) {
try {
session.close();
} catch (Exception e2) {
// do nothing. We just want to guarantee the session is closed.
logger.warn(CLOSE_THE_SESSION_FAILED, e2);
}
}
}
private void cleanSessionAndMayThrowConnectionException(
ISession session, int times, IoTDBConnectionException e) throws IoTDBConnectionException {
closeSession(session);
tryConstructNewSession();
if (times == FINAL_RETRY) {
throw new IoTDBConnectionException(
String.format(
"retry to execute statement on %s:%s failed %d times: %s",
host, port, RETRY, e.getMessage()),
e);
}
}
/**
* insert the data of a device. For each timestamp, the number of measurements is the same.
*
* @param tablet data batch
*/
@Override
public void insertTablet(Tablet tablet)
throws IoTDBConnectionException, StatementExecutionException {
/*
* A Tablet example:
* device1
* time s1, s2, s3
* 1, 1, 1, 1
* 2, 2, 2, 2
* 3, 3, 3, 3
*
* times in Tablet may be not in ascending orde
*/
insertTablet(tablet, false);
}
@Override
/**
* insert the data of a device. For each timestamp, the number of measurements is the same.
*
* Users need to control the count of Tablet and write a batch when it reaches the maxBatchSize
*
* @param tablet a tablet data of one device
* @param sorted whether times in Tablet are in ascending order
*/
public void insertTablet(Tablet tablet, boolean sorted)
throws IoTDBConnectionException, StatementExecutionException {
/*
* A Tablet example:
* device1
* time s1, s2, s3
* 1, 1, 1, 1
* 2, 2, 2, 2
* 3, 3, 3, 3
*/
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertTablet(tablet, sorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertTablet failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
@Override
/**
* insert the data of a device. For each timestamp, the number of measurements is the same.
*
*
Users need to control the count of Tablet and write a batch when it reaches the maxBatchSize
*
* @param tablet a tablet data of one device
*/
public void insertAlignedTablet(Tablet tablet)
throws IoTDBConnectionException, StatementExecutionException {
insertAlignedTablet(tablet, false);
}
/**
* insert the data of a device. For each timestamp, the number of measurements is the same.
*
*
Users need to control the count of Tablet and write a batch when it reaches the maxBatchSize
*
* @param tablet a tablet data of one device
* @param sorted whether times in Tablet are in ascending order
*/
@Override
public void insertAlignedTablet(Tablet tablet, boolean sorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedTablet(tablet, sorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedTablet failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* use batch interface to insert data
*
* @param tablets multiple batch
*/
@Override
public void insertTablets(Map tablets)
throws IoTDBConnectionException, StatementExecutionException {
insertTablets(tablets, false);
}
/**
* use batch interface to insert data
*
* @param tablets multiple batch
*/
@Override
public void insertAlignedTablets(Map tablets)
throws IoTDBConnectionException, StatementExecutionException {
insertAlignedTablets(tablets, false);
}
/**
* use batch interface to insert aligned data
*
* @param tablets multiple batch
*/
@Override
public void insertTablets(Map tablets, boolean sorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertTablets(tablets, sorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertTablets failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* use batch interface to insert aligned data
*
* @param tablets multiple batch
*/
@Override
public void insertAlignedTablets(Map tablets, boolean sorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedTablets(tablets, sorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedTablets failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert data in batch format, which can reduce the overhead of network. This method is just like
* jdbc batch insert, we pack some insert request in batch and send them to server If you want
* improve your performance, please see insertTablet method
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertRecords(
List deviceIds,
List times,
List> measurementsList,
List> typesList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertRecords failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert aligned data in batch format, which can reduce the overhead of network. This method is
* just like jdbc batch insert, we pack some insert request in batch and send them to server If
* you want improve your performance, please see insertTablet method.
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertAlignedRecords(
List multiSeriesIds,
List times,
List> multiMeasurementComponentsList,
List> typesList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedRecords(
multiSeriesIds, times, multiMeasurementComponentsList, typesList, valuesList);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedRecords failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert data that belong to the same device in batch format, which can reduce the overhead of
* network. This method is just like jdbc batch insert, we pack some insert request in batch and
* send them to server If you want improve your performance, please see insertTablet method
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> typesList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertRecordsOfOneDevice(
deviceId, times, measurementsList, typesList, valuesList, false);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert data that belong to the same device in batch format, which can reduce the overhead of
* network. This method is just like jdbc batch insert, we pack some insert request in batch and
* send them to server If you want improve your performance, please see insertTablet method
*
* @see Session#insertTablet(Tablet)
*/
@Deprecated
@Override
public void insertOneDeviceRecords(
String deviceId,
List times,
List> measurementsList,
List> typesList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertRecordsOfOneDevice(
deviceId, times, measurementsList, typesList, valuesList, false);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert String format data that belong to the same device in batch format, which can reduce the
* overhead of network. This method is just like jdbc batch insert, we pack some insert request in
* batch and send them to server If you want improve your performance, please see insertTablet
* method
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertStringRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertStringRecordsOfOneDevice(
deviceId, times, measurementsList, valuesList, false);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertStringRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert data that belong to the same device in batch format, which can reduce the overhead of
* network. This method is just like jdbc batch insert, we pack some insert request in batch and
* send them to server If you want improve your performance, please see insertTablet method
*
* @param haveSorted whether the times list has been ordered.
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> typesList,
List> valuesList,
boolean haveSorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertRecordsOfOneDevice(
deviceId, times, measurementsList, typesList, valuesList, haveSorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert data that belong to the same device in batch format, which can reduce the overhead of
* network. This method is just like jdbc batch insert, we pack some insert request in batch and
* send them to server If you want improve your performance, please see insertTablet method
*
* @param haveSorted whether the times list has been ordered.
* @see Session#insertTablet(Tablet)
*/
@Deprecated
@Override
public void insertOneDeviceRecords(
String deviceId,
List times,
List> measurementsList,
List> typesList,
List> valuesList,
boolean haveSorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertRecordsOfOneDevice(
deviceId, times, measurementsList, typesList, valuesList, haveSorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert String format data that belong to the same device in batch format, which can reduce the
* overhead of network. This method is just like jdbc batch insert, we pack some insert request in
* batch and send them to server If you want improve your performance, please see insertTablet
* method
*
* @param haveSorted whether the times list has been ordered.
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertStringRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> valuesList,
boolean haveSorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertStringRecordsOfOneDevice(
deviceId, times, measurementsList, valuesList, haveSorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertStringRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert aligned data that belong to the same device in batch format, which can reduce the
* overhead of network. This method is just like jdbc batch insert, we pack some insert request in
* batch and send them to server If you want improve your performance, please see insertTablet
* method.
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertAlignedRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> typesList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedRecordsOfOneDevice(
deviceId, times, measurementsList, typesList, valuesList, false);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert aligned data as String format that belong to the same device in batch format, which can
* reduce the overhead of network. This method is just like jdbc batch insert, we pack some insert
* request in batch and send them to server If you want improve your performance, please see
* insertTablet method.
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertAlignedStringRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedStringRecordsOfOneDevice(
deviceId, times, measurementsList, valuesList);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedStringRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert aligned data that belong to the same device in batch format, which can reduce the
* overhead of network. This method is just like jdbc batch insert, we pack some insert request in
* batch and send them to server If you want improve your performance, please see insertTablet
* method.
*
* @param haveSorted whether the times list has been ordered.
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertAlignedRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> typesList,
List> valuesList,
boolean haveSorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedRecordsOfOneDevice(
deviceId, times, measurementsList, typesList, valuesList, haveSorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert aligned data as String format that belong to the same device in batch format, which can
* reduce the overhead of network. This method is just like jdbc batch insert, we pack some insert
* request in batch and send them to server If you want improve your performance, please see
* insertTablet method.
*
* @param haveSorted whether the times list has been ordered.
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertAlignedStringRecordsOfOneDevice(
String deviceId,
List times,
List> measurementsList,
List> valuesList,
boolean haveSorted)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedStringRecordsOfOneDevice(
deviceId, times, measurementsList, valuesList, haveSorted);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedStringRecordsOfOneDevice failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert data in batch format, which can reduce the overhead of network. This method is just like
* jdbc batch insert, we pack some insert request in batch and send them to server If you want
* improve your performance, please see insertTablet method
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertRecords(
List deviceIds,
List times,
List> measurementsList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertRecords(deviceIds, times, measurementsList, valuesList);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertRecords failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* Insert aligned data in batch format, which can reduce the overhead of network. This method is
* just like jdbc batch insert, we pack some insert request in batch and send them to server If
* you want improve your performance, please see insertTablet method.
*
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertAlignedRecords(
List multiSeriesIds,
List times,
List> multiMeasurementComponentsList,
List> valuesList)
throws IoTDBConnectionException, StatementExecutionException {
for (int i = 0; i < RETRY; i++) {
ISession session = getSession();
try {
session.insertAlignedRecords(
multiSeriesIds, times, multiMeasurementComponentsList, valuesList);
putBack(session);
return;
} catch (IoTDBConnectionException e) {
// TException means the connection is broken, remove it and get a new one.
logger.warn("insertAlignedRecords failed", e);
cleanSessionAndMayThrowConnectionException(session, i, e);
} catch (StatementExecutionException | RuntimeException e) {
putBack(session);
throw e;
}
}
}
/**
* insert data in one row, if you want improve your performance, please use insertRecords method
* or insertTablet method
*
* @see Session#insertRecords(List, List, List, List, List)
* @see Session#insertTablet(Tablet)
*/
@Override
public void insertRecord(
String deviceId,
long time,
List measurements,
List types,
List