use of io.mycat.proxy.session.MySQLClientSession in project Mycat2 by MyCATApache.
the class RowSetQuery method runTextQuery.
public static PromiseInternal<SqlResult<Void>> runTextQuery(String curSql, MySQLClientSession mySQLClientSession, StreamMysqlCollector collectorArg) {
if (mySQLClientSession.getCurNIOHandler() != null) {
throw new IllegalArgumentException();
}
PromiseInternal<SqlResult<Void>> promise = VertxUtil.newPromise();
if (mySQLClientSession.getIOThread() == Thread.currentThread()) {
VertxMycatTextCollector<Object, Object> resultSetHandler = new VertxMycatTextCollector<Object, Object>((Collector) collectorArg);
if (LOGGER.isDebugEnabled()) {
if (curSql.startsWith("XA ROLLBACK")) {
LOGGER.debug("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, new Throwable());
}
LOGGER.debug("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql);
}
resultSetHandler.request(mySQLClientSession, MySQLCommandType.COM_QUERY, curSql.getBytes(), new ResultSetCallBack<MySQLClientSession>() {
@Override
public void onFinishedSendException(Exception exception, Object sender, Object attr) {
MycatException mycatException = new MycatException(MySQLErrorCode.ER_UNKNOWN_ERROR, exception.getMessage());
LOGGER.error("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, mycatException);
promise.tryFail(mycatException);
}
@Override
public void onFinishedException(Exception exception, Object sender, Object attr) {
MycatException mycatException = new MycatException(MySQLErrorCode.ER_UNKNOWN_ERROR, exception.getMessage());
LOGGER.error("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, mycatException);
promise.tryFail(mycatException);
}
@Override
public void onFinished(boolean monopolize, MySQLClientSession mysql, Object sender, Object attr) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("onFinished session id:{} sql:{}", mySQLClientSession.sessionId(), curSql);
}
MySqlResult<Void> mySqlResult = new MySqlResult<>(resultSetHandler.getRowCount(), resultSetHandler.getAffectedRows(), resultSetHandler.getLastInsertId(), null, Optional.ofNullable(resultSetHandler.getRowResultDecoder()).map(i -> i.rowDesc).map(i -> i.columnDescriptor()).orElse(Collections.emptyList()));
promise.complete(mySqlResult);
}
@Override
public void onErrorPacket(ErrorPacketImpl errorPacket, boolean monopolize, MySQLClientSession mysql, Object sender, Object attr) {
MycatException mycatException = new MycatException(errorPacket.getErrorCode(), errorPacket.getErrorMessageString());
LOGGER.error("onErrorPacket session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, mycatException);
promise.tryFail(mycatException);
}
});
} else {
mySQLClientSession.getIOThread().addNIOJob(new NIOJob() {
@Override
public void run(ReactorEnvThread reactor) throws Exception {
if (LOGGER.isDebugEnabled()) {
LOGGER.error("nio job session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, new Throwable());
}
runTextQuery(curSql, mySQLClientSession, collectorArg).onComplete(promise);
}
@Override
public void stop(ReactorEnvThread reactor, Exception reason) {
promise.tryFail(reason);
}
@Override
public String message() {
return "proxy query text result set";
}
});
}
return promise;
}
use of io.mycat.proxy.session.MySQLClientSession in project Mycat2 by MyCATApache.
the class MySQLAPIImpl method query.
@Override
public void query(String sql, ResultSetCollector collector, MySQLAPIExceptionCallback exceptionCollector) {
TextResultSetTransforCollector transfor = new TextResultSetTransforCollector(collector);
TextResultSetHandler queryResultSetTask = new TextResultSetHandler(transfor);
queryResultSetTask.request(mySQLClientSession, MySQLCommandType.COM_QUERY, sql.getBytes(), new ResultSetCallBack<MySQLClientSession>() {
@Override
public void onFinishedSendException(Exception exception, Object sender, Object attr) {
exceptionCollector.onException(exception, MySQLAPIImpl.this);
}
@Override
public void onFinishedException(Exception exception, Object sender, Object attr) {
exceptionCollector.onException(exception, MySQLAPIImpl.this);
}
@Override
public void onFinished(boolean monopolize, MySQLClientSession mysql, Object sender, Object attr) {
exceptionCollector.onFinished(monopolize, MySQLAPIImpl.this);
}
@Override
public void onErrorPacket(ErrorPacketImpl errorPacket, boolean monopolize, MySQLClientSession mysql, Object sender, Object attr) {
exceptionCollector.onErrorPacket(errorPacket, monopolize, MySQLAPIImpl.this);
}
});
}
use of io.mycat.proxy.session.MySQLClientSession in project Mycat2 by MyCATApache.
the class MySQLDatasourcePool method idleConnectCheck.
private Future<Void> idleConnectCheck() {
List<MySQLClientSession> mySQLClientSessions = new ArrayList<>(idleSessions);
List<MySQLClientSession> needCloseConnections;
int minCon = datasourceConfig.getMinCon();
if (mySQLClientSessions.size() > minCon) {
needCloseConnections = mySQLClientSessions.subList(minCon, mySQLClientSessions.size());
} else {
needCloseConnections = Collections.emptyList();
}
ArrayList<Future> futures = new ArrayList<>(mySQLClientSessions.size());
for (MySQLClientSession needCloseConnection : needCloseConnections) {
idleSessions.remove(needCloseConnection);
needCloseConnection.setIdle(false);
needCloseConnection.switchNioHandler(null);
futures.add(removeSession(needCloseConnection, true, "idle check"));
}
for (MySQLClientSession mySQLClientSession : mySQLClientSessions) {
idleSessions.remove(mySQLClientSession);
mySQLClientSession.setIdle(false);
mySQLClientSession.switchNioHandler(null);
futures.add(checkIfNeedHeartBeat(mySQLClientSession));
}
return (Future) CompositeFuture.join(futures);
}
use of io.mycat.proxy.session.MySQLClientSession in project Mycat2 by MyCATApache.
the class MySQLDatasourcePool method createSession.
public Future<MySQLClientSession> createSession() {
if (!(Thread.currentThread() instanceof ReactorEnvThread)) {
return waitForIdleSession(false);
}
int limitCount = this.getSessionLimitCount();
synchronized (this) {
if (allSessions.size() >= limitCount && idleSessions.isEmpty()) {
return waitForIdleSession(true);
}
}
if (idleSessions.isEmpty()) {
Future<MySQLClientSession> future = innerCreateCon();
return future.recover(new Function<Throwable, Future<MySQLClientSession>>() {
final AtomicInteger retryCount = new AtomicInteger(0);
final long startTime = System.currentTimeMillis();
@Override
public Future<MySQLClientSession> apply(Throwable t) {
PromiseInternal<MySQLClientSession> promise = VertxUtil.newPromise();
long now = System.currentTimeMillis();
long maxConnectTimeout = MySQLDatasourcePool.this.getMaxConnectTimeout();
if (retryCount.get() >= limitCount) {
promise.tryFail(new MycatException("retry get connection fail:" + getName()));
} else if (startTime + maxConnectTimeout > now) {
promise.tryFail(new MycatException("retry get connection timeout:" + getName()));
} else {
retryCount.incrementAndGet();
waitForIdleSession(true).recover(this).onComplete(promise);
}
return promise.future();
}
});
} else {
MySQLClientSession session = idleSessions.removeFirst();
if (session.checkOpen()) {
session.setIdle(false);
session.switchNioHandler(null);
return Future.succeededFuture(session);
} else {
return session.close(false, "has closed").flatMap(unused -> createSession());
}
}
}
use of io.mycat.proxy.session.MySQLClientSession in project Mycat2 by MyCATApache.
the class MySQLDatasourcePool method innerCreateCon.
private Future<MySQLClientSession> innerCreateCon() {
PromiseInternal<MySQLClientSession> promise = VertxUtil.newPromise();
new BackendConCreateHandler(this, promise);
return promise.flatMap(session -> {
synchronized (allSessions) {
allSessions.add(session);
}
MySQLDatasource datasource = session.getDatasource();
String sql = datasource.getInitSqlForProxy();
if (!StringUtil.isEmpty(sql)) {
PromiseInternal<MySQLClientSession> promiseInternal = VertxUtil.newPromise();
ResultSetHandler.DEFAULT.request(session, COM_QUERY, sql.getBytes(StandardCharsets.UTF_8), promiseInternal);
return promiseInternal.onFailure(event -> {
session.close(false, "initSql fail");
});
} else {
return Future.succeededFuture(session);
}
});
}
Aggregations