Skip to content

Commit

Permalink
Merge pull request #3864 from katzyn/loom
Browse files Browse the repository at this point in the history
Improve performance of TCP client driver when it is used from virtual threads
  • Loading branch information
katzyn committed Aug 7, 2023
2 parents a466abd + 3c74922 commit 752b246
Show file tree
Hide file tree
Showing 19 changed files with 398 additions and 125 deletions.
10 changes: 8 additions & 2 deletions h2/src/main/org/h2/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public ResultInterface executeQuery(long maxrows, boolean scrollable) {
Database database = getDatabase();
session.waitIfExclusiveModeEnabled();
boolean callStop = true;
synchronized (session) {
session.lock();
try {
session.startStatementWithinTransaction(this);
Session oldSession = session.setThreadLocalSession();
try {
Expand Down Expand Up @@ -230,14 +231,17 @@ public ResultInterface executeQuery(long maxrows, boolean scrollable) {
stop();
}
}
} finally {
session.unlock();
}
}

@Override
public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
long start = 0;
boolean callStop = true;
synchronized (session) {
session.lock();
try {
Database database = getDatabase();
session.waitIfExclusiveModeEnabled();
commitIfNonTransactional();
Expand Down Expand Up @@ -300,6 +304,8 @@ public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
}
}
}
} finally {
session.unlock();
}
}

Expand Down
26 changes: 21 additions & 5 deletions h2/src/main/org/h2/command/CommandRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ private void prepareIfRequired() {

@Override
public ResultInterface getMetaData() {
synchronized (session) {
final SessionRemote session = this.session;
session.lock();
try {
if (!isQuery) {
return null;
}
Expand All @@ -144,13 +146,17 @@ public ResultInterface getMetaData() {
}
session.autoCommitIfCluster();
return result;
} finally {
session.unlock();
}
}

@Override
public ResultInterface executeQuery(long maxRows, boolean scrollable) {
checkParameters();
synchronized (session) {
final SessionRemote session = this.session;
session.lock();
try {
int objectId = session.getNextId();
ResultRemote result = null;
for (int i = 0, count = 0; i < transferList.size(); i++) {
Expand Down Expand Up @@ -185,6 +191,8 @@ public ResultInterface executeQuery(long maxRows, boolean scrollable) {
session.autoCommitIfCluster();
session.readSessionState();
return result;
} finally {
session.unlock();
}
}

Expand All @@ -194,7 +202,9 @@ public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
int generatedKeysMode = GeneratedKeysMode.valueOf(generatedKeysRequest);
boolean readGeneratedKeys = generatedKeysMode != GeneratedKeysMode.NONE;
int objectId = readGeneratedKeys ? session.getNextId() : 0;
synchronized (session) {
final SessionRemote session = this.session;
session.lock();
try {
long updateCount = 0L;
ResultRemote generatedKeys = null;
boolean autoCommit = false;
Expand Down Expand Up @@ -246,6 +256,8 @@ public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
return new ResultWithGeneratedKeys.WithKeys(updateCount, generatedKeys);
}
return ResultWithGeneratedKeys.of(updateCount);
} finally {
session.unlock();
}
}

Expand Down Expand Up @@ -273,10 +285,12 @@ private void sendParameters(Transfer transfer) throws IOException {

@Override
public void close() {
final SessionRemote session = this.session;
if (session == null || session.isClosed()) {
return;
}
synchronized (session) {
session.lock();
try {
session.traceOperation("COMMAND_CLOSE", id);
for (Transfer transfer : transferList) {
try {
Expand All @@ -285,8 +299,10 @@ public void close() {
trace.error(e, "close");
}
}
} finally {
session.unlock();
}
session = null;
this.session = null;
try {
for (ParameterInterface p : parameters) {
Value v = p.getParamValue();
Expand Down
5 changes: 4 additions & 1 deletion h2/src/main/org/h2/command/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7088,7 +7088,8 @@ private TableView createCTEView(String cteViewName, String querySQL, ArrayList<P
// it twice - once without the flag set, and if we didn't see a recursive term,
// then we just compile it again.
TableView view;
synchronized (session) {
session.lock();
try {
view = new TableView(schema, id, cteViewName, querySQL,
queryParameters, columnTemplateArray, session,
true, false, true,
Expand All @@ -7110,6 +7111,8 @@ private TableView createCTEView(String cteViewName, String querySQL, ArrayList<P
}
// both removeSchemaObject and removeLocalTempTable hold meta locks
database.unlockMeta(session);
} finally {
session.unlock();
}
view.setTableExpression(true);
view.setTemporary(isTemporary);
Expand Down
6 changes: 5 additions & 1 deletion h2/src/main/org/h2/engine/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,9 @@ private void executeMeta() {
lastRecords.add(rec);
}
}
synchronized (systemSession) {
final SessionLocal systemSession = this.systemSession;
systemSession.lock();
try {
executeMeta(firstRecords);
// Domains may depend on other domains
int count = domainRecords.size();
Expand Down Expand Up @@ -652,6 +654,8 @@ private void executeMeta() {
}
}
executeMeta(lastRecords);
} finally {
systemSession.unlock();
}
}

Expand Down
5 changes: 4 additions & 1 deletion h2/src/main/org/h2/engine/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ private static SessionLocal openSession(ConnectionInfo ci) {
throw DbException.get(ErrorCode.DATABASE_CALLED_AT_SHUTDOWN);
}
}
synchronized (session) {
session.lock();
try {
session.setAllowLiterals(true);
DbSettings defaultSettings = DbSettings.DEFAULT;
for (String setting : ci.getKeys()) {
Expand Down Expand Up @@ -286,6 +287,8 @@ private static SessionLocal openSession(ConnectionInfo ci) {
}
session.setAllowLiterals(false);
session.commit(true);
} finally {
session.unlock();
}
return session;
}
Expand Down
29 changes: 29 additions & 0 deletions h2/src/main/org/h2/engine/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.h2.engine;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

import org.h2.command.CommandInterface;
import org.h2.jdbc.meta.DatabaseMeta;
Expand Down Expand Up @@ -90,6 +91,8 @@ public DynamicSettings(Mode mode, TimeZoneProvider timeZone) {

}

private final ReentrantLock lock = new ReentrantLock();

private ArrayList<String> sessionState;

boolean sessionStateChanged;
Expand All @@ -101,6 +104,32 @@ public DynamicSettings(Mode mode, TimeZoneProvider timeZone) {
Session() {
}

/**
* Locks this session with a reentrant lock.
*
* <pre>
* final Session session = ...;
* session.lock();
* try {
* ...
* } finally {
* session.unlock();
* }
* </pre>
*/
public final void lock() {
lock.lock();
}

/**
* Unlocks this session.
*
* @see #lock()
*/
public final void unlock() {
lock.unlock();
}

/**
* Get the list of the cluster servers for this session.
*
Expand Down
10 changes: 7 additions & 3 deletions h2/src/main/org/h2/engine/SessionLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,13 @@ public void setLockTimeout(int lockTimeout) {
}

@Override
public synchronized CommandInterface prepareCommand(String sql,
int fetchSize) {
return prepareLocal(sql);
public CommandInterface prepareCommand(String sql, int fetchSize) {
lock();
try {
return prepareLocal(sql);
} finally {
unlock();
}
}

/**
Expand Down

0 comments on commit 752b246

Please sign in to comment.