Repository navigation
Add coreSize and maxIdleTime conf for SessionPool and TableSessionPool #19424
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ | |
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.apache.iotdb.rpc.RpcUtils.isSetSqlDialect; | ||
| import static org.apache.iotdb.rpc.RpcUtils.isUseDatabase; | ||
|
|
@@ -99,11 +100,13 @@ public class SessionPool implements ISessionPool { | |
| private static final int RETRY = 3; | ||
| private static final int FINAL_RETRY = RETRY - 1; | ||
|
|
||
| private final ConcurrentLinkedDeque<ISession> queue = new ConcurrentLinkedDeque<>(); | ||
| private final ConcurrentLinkedDeque<IdleSession> queue = new ConcurrentLinkedDeque<>(); | ||
| // sessions currently borrowed for a result set or a table-session wrapper. | ||
| private final ConcurrentMap<ISession, ISession> occupied = new ConcurrentHashMap<>(); | ||
| private int size = 0; | ||
| private int maxSize = 0; | ||
| private int coreSize = SessionConfig.DEFAULT_SESSION_POOL_CORE_SIZE; | ||
| private long maxIdleTimeInMs = SessionConfig.DEFAULT_SESSION_POOL_MAX_IDLE_TIME_IN_MS; | ||
| private final long waitToGetSessionTimeoutInMs; | ||
|
|
||
| // parameters for Session constructor | ||
|
|
@@ -526,8 +529,12 @@ public SessionPool( | |
| initAvailableNodes(SessionUtils.parseSeedNodeUrls(this.nodeUrls)); | ||
| } | ||
|
|
||
| @SuppressWarnings("unsafeThreadSchedule") | ||
| public SessionPool(AbstractSessionPoolBuilder builder) { | ||
| this.maxSize = builder.maxSize; | ||
| this.coreSize = builder.coreSize; | ||
| this.maxIdleTimeInMs = builder.maxIdleTimeInMs; | ||
| validateIdleConfig(coreSize, maxSize, maxIdleTimeInMs); | ||
| this.user = builder.username; | ||
| this.password = builder.pw; | ||
| this.fetchSize = builder.fetchSize; | ||
|
|
@@ -589,6 +596,15 @@ public SessionPool(AbstractSessionPoolBuilder builder) { | |
| } else { | ||
| this.availableNodes = new DummyNodesSupplier(endPointList); | ||
| } | ||
|
|
||
| if (maxIdleTimeInMs > 0) { | ||
| if (executorService == null) { | ||
| initThreadPool(); | ||
| } | ||
| long checkIntervalInMs = Math.max(1_000L, Math.min(maxIdleTimeInMs, 60_000L)); | ||
| executorService.scheduleWithFixedDelay( | ||
| this::evictIdleSessions, checkIntervalInMs, checkIntervalInMs, TimeUnit.MILLISECONDS); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Several issues with scheduling idle eviction here:
|
||
| } | ||
|
|
||
| private Session constructNewSession() { | ||
|
|
@@ -712,7 +728,8 @@ private ISession getSession() throws IoTDBConnectionException { | |
| if (closed) { | ||
| throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); | ||
| } | ||
| session = queue.poll(); | ||
| IdleSession idleSession = queue.poll(); | ||
| session = idleSession == null ? null : idleSession.session; | ||
| } | ||
| if (session != null) { | ||
| return session; | ||
|
|
@@ -763,14 +780,11 @@ private ISession getSession() throws IoTDBConnectionException { | |
| // wake up from this.wait(1000) by this.notify() | ||
| } | ||
|
|
||
| session = queue.poll(); | ||
| IdleSession idleSession = queue.poll(); | ||
| session = idleSession == null ? null : idleSession.session; | ||
|
|
||
| // for putBack or size-- | ||
| this.notify(); | ||
|
|
||
| if (closed) { | ||
| throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); | ||
| } | ||
|
Comment on lines
-771
to
-773
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -835,7 +849,7 @@ public int currentOccupiedSize() { | |
| protected void putBack(ISession session) { | ||
| synchronized (this) { | ||
| if (!closed) { | ||
| queue.push(session); | ||
| queue.push(new IdleSession(session, System.currentTimeMillis())); | ||
| // 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. | ||
|
|
@@ -903,7 +917,10 @@ public void close() { | |
| return; | ||
| } | ||
| closed = true; | ||
| idleSessions = new ArrayList<>(queue); | ||
| idleSessions = new ArrayList<>(queue.size()); | ||
| for (IdleSession idleSession : queue) { | ||
| idleSessions.add(idleSession.session); | ||
| } | ||
| borrowedSessions = new ArrayList<>(occupied.keySet()); | ||
| queue.clear(); | ||
| occupied.clear(); | ||
|
|
@@ -974,7 +991,7 @@ private void tryConstructNewSession() { | |
| session.close(); | ||
| throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); | ||
| } | ||
| queue.push(session); | ||
| queue.push(new IdleSession(session, System.currentTimeMillis())); | ||
| this.notify(); | ||
| } | ||
| } catch (IoTDBConnectionException e) { | ||
|
|
@@ -986,6 +1003,54 @@ private void tryConstructNewSession() { | |
| } | ||
| } | ||
|
|
||
| private static void validateIdleConfig(int coreSize, int maxSize, long maxIdleTimeInMs) { | ||
| if (coreSize < 0 || coreSize > maxSize) { | ||
| throw new IllegalArgumentException( | ||
| String.format(SessionMessages.SESSION_POOL_CORE_SIZE_ARG_ILLEGAL, coreSize, maxSize)); | ||
| } | ||
| if (maxIdleTimeInMs < 0) { | ||
| throw new IllegalArgumentException( | ||
| String.format(SessionMessages.SESSION_POOL_MAX_IDLE_TIME_ARG_ILLEGAL, maxIdleTimeInMs)); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor validation edge case: if a caller passes |
||
|
|
||
| /** | ||
| * Closes idle sessions which have been idle for more than {@link #maxIdleTimeInMs}, until the | ||
| * total number of sessions drops to {@link #coreSize}. Evicted sessions are closed outside the | ||
| * lock to avoid holding it during I/O. | ||
| */ | ||
| private void evictIdleSessions() { | ||
| List<ISession> evicted = new ArrayList<>(); | ||
| int sizeAfterEviction; | ||
| synchronized (this) { | ||
| if (closed) { | ||
| return; | ||
| } | ||
| long now = System.currentTimeMillis(); | ||
| while (size > coreSize) { | ||
| IdleSession idleSession = queue.peekLast(); | ||
| if (idleSession == null || now - idleSession.idleSince <= maxIdleTimeInMs) { | ||
| break; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use monotonic |
||
| queue.pollLast(); | ||
| size--; | ||
| evicted.add(idleSession.session); | ||
| } | ||
| sizeAfterEviction = size; | ||
| if (!evicted.isEmpty()) { | ||
| // wake up all waiters so that they can create new sessions if size < maxSize. | ||
| this.notifyAll(); | ||
| } | ||
| } | ||
| if (!evicted.isEmpty()) { | ||
| LOGGER.debug( | ||
| SessionMessages.EVICT_IDLE_SESSIONS, evicted.size(), sizeAfterEviction, coreSize); | ||
| } | ||
| for (ISession session : evicted) { | ||
| closeSession(session); | ||
| } | ||
| } | ||
|
|
||
| private void closeSession(ISession session) { | ||
| if (session != null) { | ||
| try { | ||
|
|
@@ -3583,8 +3648,8 @@ public String getPassword() { | |
| @Override | ||
| public void setFetchSize(int fetchSize) { | ||
| this.fetchSize = fetchSize; | ||
| for (ISession session : queue) { | ||
| session.setFetchSize(fetchSize); | ||
| for (IdleSession idleSession : queue) { | ||
| idleSession.session.setFetchSize(fetchSize); | ||
| } | ||
| for (ISession session : occupied.keySet()) { | ||
| session.setFetchSize(fetchSize); | ||
|
|
@@ -3614,8 +3679,8 @@ public void setTimeZone(String zoneId) | |
| } | ||
| } | ||
| this.zoneId = ZoneId.of(zoneId); | ||
| for (ISession session : queue) { | ||
| session.setTimeZoneOfSession(zoneId); | ||
| for (IdleSession idleSession : queue) { | ||
| idleSession.session.setTimeZoneOfSession(zoneId); | ||
| } | ||
| for (ISession session : occupied.keySet()) { | ||
| session.setTimeZoneOfSession(zoneId); | ||
|
|
@@ -3644,8 +3709,8 @@ public void setEnableRedirection(boolean enableRedirection) { | |
| deviceIdToEndpoint = new ConcurrentHashMap<>(); | ||
| tableModelDeviceIdToEndpoint = new ConcurrentHashMap<>(); | ||
| } | ||
| for (ISession session : queue) { | ||
| session.setEnableRedirection(enableRedirection); | ||
| for (IdleSession idleSession : queue) { | ||
| idleSession.session.setEnableRedirection(enableRedirection); | ||
| } | ||
| for (ISession session : occupied.keySet()) { | ||
| session.setEnableRedirection(enableRedirection); | ||
|
|
@@ -3660,8 +3725,8 @@ public boolean isEnableRedirection() { | |
| @Override | ||
| public void setEnableQueryRedirection(boolean enableQueryRedirection) { | ||
| this.enableQueryRedirection = enableQueryRedirection; | ||
| for (ISession session : queue) { | ||
| session.setEnableQueryRedirection(enableQueryRedirection); | ||
| for (IdleSession idleSession : queue) { | ||
| idleSession.session.setEnableQueryRedirection(enableQueryRedirection); | ||
| } | ||
| for (ISession session : occupied.keySet()) { | ||
| session.setEnableQueryRedirection(enableQueryRedirection); | ||
|
|
@@ -3722,8 +3787,8 @@ public TSConnectionInfoResp fetchAllConnections() throws IoTDBConnectionExceptio | |
| @Override | ||
| public void setVersion(Version version) { | ||
| this.version = version; | ||
| for (ISession session : queue) { | ||
| session.setVersion(version); | ||
| for (IdleSession idleSession : queue) { | ||
| idleSession.session.setVersion(version); | ||
| } | ||
| for (ISession session : occupied.keySet()) { | ||
| session.setVersion(version); | ||
|
|
@@ -3738,8 +3803,8 @@ public Version getVersion() { | |
| @Override | ||
| public void setQueryTimeout(long timeoutInMs) { | ||
| this.queryTimeoutInMs = timeoutInMs; | ||
| for (ISession session : queue) { | ||
| session.setQueryTimeout(timeoutInMs); | ||
| for (IdleSession idleSession : queue) { | ||
| idleSession.session.setQueryTimeout(timeoutInMs); | ||
| } | ||
| for (ISession session : occupied.keySet()) { | ||
| session.setQueryTimeout(timeoutInMs); | ||
|
|
@@ -3751,6 +3816,16 @@ public long getQueryTimeout() { | |
| return queryTimeoutInMs; | ||
| } | ||
|
|
||
| private static final class IdleSession { | ||
| private final ISession session; | ||
| private final long idleSince; | ||
|
|
||
| private IdleSession(ISession session, long idleSince) { | ||
| this.session = session; | ||
| this.idleSince = idleSince; | ||
| } | ||
| } | ||
|
|
||
| public static class Builder extends AbstractSessionPoolBuilder { | ||
|
|
||
| public Builder useSSL(boolean useSSL) { | ||
|
|
@@ -3807,6 +3882,31 @@ public Builder maxSize(int maxSize) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the minimum number of sessions kept in the pool. Idle sessions will not be evicted when | ||
| * the total number of sessions reaches this value. | ||
| * | ||
| * @param coreSize the minimum number of sessions, must be in [0, maxSize] | ||
| * @return this builder | ||
| */ | ||
| public Builder coreSize(int coreSize) { | ||
| this.coreSize = coreSize; | ||
| return this; | ||
| } | ||
|
Comment on lines
+3949
to
+3964
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest clarifying in both
|
||
|
|
||
| /** | ||
| * Sets the maximum time in milliseconds that an idle session can stay in the pool. Idle | ||
| * sessions are evicted until the total number of sessions drops to the core size. 0 means idle | ||
| * sessions are never evicted. | ||
| * | ||
| * @param maxIdleTimeInMs the maximum idle time in milliseconds, must not be negative | ||
| * @return this builder | ||
| */ | ||
| public Builder maxIdleTimeInMs(long maxIdleTimeInMs) { | ||
| this.maxIdleTimeInMs = maxIdleTimeInMs; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder user(String user) { | ||
| this.username = user; | ||
| return this; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SessionPool/ISessionPoolprovide getters for the other pool settings (getMaxSize(),getWaitToGetSessionTimeoutInMs(),getConnectionTimeoutInMs(), etc.). Consider addinggetCoreSize()andgetMaxIdleTimeInMs()(asdefaultmethods onISessionPoolif exposed on the interface to keep backward compatibility), which would also eliminate theWhitebox.getInternalStatereflection inSessionPoolTest. Both fields can also befinalif assigned defaults in the non-builder constructors.