-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement PreparedStmt on the Server side #16764
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
base: force_ci/prepstm
Are you sure you want to change the base?
Changes from all commits
b90965f
d64a3e2
85d3aa9
c7efa02
2256e49
9a783ed
9490f9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,12 +22,17 @@ | |||||
| import org.apache.iotdb.service.rpc.thrift.TSConnectionType; | ||||||
|
|
||||||
| import java.util.Collections; | ||||||
| import java.util.Map; | ||||||
| import java.util.Set; | ||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||
|
|
||||||
| public class MqttClientSession extends IClientSession { | ||||||
|
|
||||||
| private final String clientID; | ||||||
|
|
||||||
| // Map from statement name to PreparedStatementInfo | ||||||
| private final Map<String, PreparedStatementInfo> preparedStatements = new ConcurrentHashMap<>(); | ||||||
|
|
||||||
|
Comment on lines
+33
to
+35
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.
Suggested change
|
||||||
| public MqttClientSession(String clientID) { | ||||||
| this.clientID = clientID; | ||||||
| } | ||||||
|
|
@@ -76,4 +81,24 @@ public void addQueryId(Long statementId, long queryId) { | |||||
| public void removeQueryId(Long statementId, Long queryId) { | ||||||
| throw new UnsupportedOperationException(); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void addPreparedStatement(String statementName, PreparedStatementInfo info) { | ||||||
| preparedStatements.put(statementName, info); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public PreparedStatementInfo removePreparedStatement(String statementName) { | ||||||
| return preparedStatements.remove(statementName); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public PreparedStatementInfo getPreparedStatement(String statementName) { | ||||||
| return preparedStatements.get(statementName); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public Set<String> getPreparedStatementNames() { | ||||||
| return preparedStatements.keySet(); | ||||||
| } | ||||||
|
Comment on lines
+85
to
+103
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. throw execption here, mqtt is for write not for query. |
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * 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.db.protocol.session; | ||
|
|
||
| import org.apache.iotdb.commons.memory.IMemoryBlock; | ||
| import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Statement; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * Information about a prepared statement stored in a session. The AST is cached here to avoid | ||
| * re-parsing on EXECUTE. | ||
| */ | ||
| public class PreparedStatementInfo { | ||
|
|
||
| private final String statementName; | ||
| private final Statement sql; // Cached AST (contains Parameter nodes) | ||
| private final long createTime; | ||
| private final IMemoryBlock memoryBlock; // Memory block allocated for this PreparedStatement | ||
|
|
||
| public PreparedStatementInfo(String statementName, Statement sql, IMemoryBlock memoryBlock) { | ||
| this.statementName = requireNonNull(statementName, "statementName is null"); | ||
| this.sql = requireNonNull(sql, "sql is null"); | ||
| this.createTime = System.currentTimeMillis(); | ||
| this.memoryBlock = memoryBlock; | ||
| } | ||
|
|
||
| public PreparedStatementInfo( | ||
| String statementName, Statement sql, long createTime, IMemoryBlock memoryBlock) { | ||
| this.statementName = requireNonNull(statementName, "statementName is null"); | ||
| this.sql = requireNonNull(sql, "sql is null"); | ||
| this.createTime = createTime; | ||
| this.memoryBlock = memoryBlock; | ||
| } | ||
|
|
||
| public String getStatementName() { | ||
| return statementName; | ||
| } | ||
|
|
||
| public Statement getSql() { | ||
| return sql; | ||
| } | ||
|
|
||
| public long getCreateTime() { | ||
| return createTime; | ||
| } | ||
|
|
||
| public IMemoryBlock getMemoryBlock() { | ||
| return memoryBlock; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| PreparedStatementInfo that = (PreparedStatementInfo) o; | ||
| return Objects.equals(statementName, that.statementName) && Objects.equals(sql, that.sql); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(statementName, sql); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "PreparedStatementInfo{" | ||
| + "statementName='" | ||
| + statementName | ||
| + '\'' | ||
| + ", sql=" | ||
| + sql | ||
| + ", createTime=" | ||
| + createTime | ||
| + '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,9 @@ | |
| package org.apache.iotdb.db.protocol.thrift.handler; | ||
|
|
||
| import org.apache.iotdb.db.protocol.session.ClientSession; | ||
| import org.apache.iotdb.db.protocol.session.IClientSession; | ||
| import org.apache.iotdb.db.protocol.session.SessionManager; | ||
| import org.apache.iotdb.db.queryengine.plan.Coordinator; | ||
| import org.apache.iotdb.external.api.thrift.JudgableServerContext; | ||
| import org.apache.iotdb.external.api.thrift.ServerContextFactory; | ||
| import org.apache.iotdb.rpc.TElasticFramedTransport; | ||
|
|
@@ -70,7 +72,22 @@ public ServerContext createContext(TProtocol in, TProtocol out) { | |
| } | ||
|
|
||
| public void deleteContext(ServerContext context, TProtocol in, TProtocol out) { | ||
| IClientSession session = getSessionManager().getCurrSession(); | ||
|
|
||
| // Release session resources (including PreparedStatement memory) | ||
| // This handles TCP connection loss scenarios | ||
| if (session != null) { | ||
| try { | ||
| getSessionManager().closeSession(session, Coordinator.getInstance()::cleanupQueryExecution); | ||
| } catch (Exception e) { | ||
| logger.warn( | ||
| "Failed to close session during TCP connection disconnect: {}", e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| // Remove the session from the current thread | ||
| getSessionManager().removeCurrSession(); | ||
|
|
||
|
Comment on lines
+75
to
+90
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. why did you do this change? ClientRPCServiceImpl.handleClientExit has already called closeSession(req). |
||
| if (context != null && factory != null) { | ||
| ((JudgableServerContext) context).whenDisconnect(); | ||
| } | ||
|
|
||
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.
throw exception here, InternalClientSession should never call these methods, they're used for internal query and write which will never use prepare and execute