Skip to content

Commit c51d456

Browse files
authored
Isolate all grpc calls from parent context (#102)
2 parents 6530d05 + bfba27f commit c51d456

File tree

6 files changed

+28
-24
lines changed

6 files changed

+28
-24
lines changed

jdbc/src/main/java/tech/ydb/jdbc/context/BaseYdbExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public BaseYdbExecutor(YdbContext ctx) {
5353
}
5454

5555
protected Session createNewTableSession(YdbValidator validator) throws SQLException {
56-
return validator.call("Get session", () -> tableClient.createSession(sessionTimeout));
56+
return validator.call("Get session", null, () -> tableClient.createSession(sessionTimeout));
5757
}
5858

5959
protected void closeCurrentResult() throws SQLException {
@@ -163,7 +163,7 @@ public YdbQueryResult executeScanQuery(YdbStatement statement, YdbQuery query, S
163163
}
164164
}
165165

166-
StreamQueryResult lazy = validator.call(msg, () -> {
166+
StreamQueryResult lazy = validator.call(msg, null, () -> {
167167
final CompletableFuture<Result<StreamQueryResult>> future = new CompletableFuture<>();
168168
final GrpcReadStream<ResultSetReader> stream = session.executeScanQuery(yql, params, settings);
169169
final StreamQueryResult result = new StreamQueryResult(msg, statement, query, stream::cancel);

jdbc/src/main/java/tech/ydb/jdbc/context/QueryServiceExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public QueryServiceExecutor(YdbContext ctx, int transactionLevel, boolean autoCo
6868
}
6969

7070
protected QuerySession createNewQuerySession(YdbValidator validator) throws SQLException {
71-
return validator.call("Get query session", () -> queryClient.createSession(sessionTimeout));
71+
return validator.call("Get query session", null, () -> queryClient.createSession(sessionTimeout));
7272
}
7373

7474
@Override
@@ -255,7 +255,7 @@ public YdbQueryResult executeDataQuery(
255255
tracer.query(yql);
256256
String msg = "STREAM_QUERY >>\n" + yql;
257257

258-
StreamQueryResult lazy = validator.call(msg, () -> {
258+
StreamQueryResult lazy = validator.call(msg, tracer, () -> {
259259
final CompletableFuture<Result<StreamQueryResult>> future = new CompletableFuture<>();
260260
final QueryStream stream = localTx.createQuery(yql, isAutoCommit, params, settings);
261261
final StreamQueryResult result = new StreamQueryResult(msg, statement, query, stream::cancel);

jdbc/src/main/java/tech/ydb/jdbc/context/TableServiceExecutor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ public boolean isValid(YdbValidator validator, int timeout) throws SQLException
248248
try {
249249
KeepAliveSessionSettings settings = new KeepAliveSessionSettings().setTimeout(Duration.ofSeconds(timeout));
250250
Session.State keepAlive = validator.call(
251-
"Keep alive: " + tx.txID(),
252-
() -> session.keepAlive(settings)
251+
"Keep alive: " + tx.txID(), null, () -> session.keepAlive(settings)
253252
);
254253
return keepAlive == Session.State.READY;
255254
} finally {

jdbc/src/main/java/tech/ydb/jdbc/context/YdbValidator.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.logging.Level;
1111
import java.util.logging.Logger;
1212

13+
import io.grpc.Context;
14+
1315
import tech.ydb.core.Issue;
1416
import tech.ydb.core.Result;
1517
import tech.ydb.core.Status;
@@ -54,8 +56,18 @@ public void clearWarnings() {
5456
this.issues.clear();
5557
}
5658

59+
private <T> T joinFuture(Supplier<CompletableFuture<T>> supplier) {
60+
Context ctx = Context.current().fork();
61+
Context previous = ctx.attach();
62+
try {
63+
return supplier.get().join();
64+
} finally {
65+
ctx.detach(previous);
66+
}
67+
}
68+
5769
public void execute(String msg, YdbTracer tracer, Supplier<CompletableFuture<Status>> fn) throws SQLException {
58-
Status status = fn.get().join();
70+
Status status = joinFuture(fn);
5971
addStatusIssues(status);
6072

6173
tracer.trace("<-- " + status.toString());
@@ -67,25 +79,18 @@ public void execute(String msg, YdbTracer tracer, Supplier<CompletableFuture<Sta
6779
}
6880
}
6981

70-
public <R> R call(String msg, Supplier<CompletableFuture<Result<R>>> fn) throws SQLException {
71-
try {
72-
Result<R> result = fn.get().join();
73-
addStatusIssues(result.getStatus());
74-
return result.getValue();
75-
} catch (UnexpectedResultException ex) {
76-
LOGGER.log(Level.FINE, "call problem {0}", ex.getStatus());
77-
throw ExceptionFactory.createException("Cannot call '" + msg + "' with " + ex.getStatus(), ex);
78-
}
79-
}
80-
8182
public <R> R call(String msg, YdbTracer tracer, Supplier<CompletableFuture<Result<R>>> fn) throws SQLException {
8283
try {
83-
Result<R> result = fn.get().join();
84+
Result<R> result = joinFuture(fn);
8485
addStatusIssues(result.getStatus());
85-
tracer.trace("<-- " + result.getStatus().toString());
86+
if (tracer != null) {
87+
tracer.trace("<-- " + result.getStatus().toString());
88+
}
8689
return result.getValue();
8790
} catch (UnexpectedResultException ex) {
88-
tracer.close();
91+
if (tracer != null) {
92+
tracer.close();
93+
}
8994
LOGGER.log(Level.FINE, "call problem {0}", ex.getStatus());
9095
throw ExceptionFactory.createException("Cannot call '" + msg + "' with " + ex.getStatus(), ex);
9196
}

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbDatabaseMetaDataImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ private List<String> listTables(Predicate<String> filter) throws SQLException {
13431343

13441344
private List<String> tables(String databasePrefix, String path, Predicate<String> filter) throws SQLException {
13451345
ListDirectoryResult result = validator.call(
1346-
"List tables from " + path, () -> executor.listDirectory(path)
1346+
"List tables from " + path, null, () -> executor.listDirectory(path)
13471347
);
13481348

13491349
List<String> tables = new ArrayList<>();
@@ -1376,7 +1376,7 @@ private TableDescription describeTable(String table) throws SQLException {
13761376

13771377
String databaseWithSuffix = withSuffix(connection.getCtx().getPrefixPath());
13781378

1379-
return validator.call("Describe table " + table, () -> executor
1379+
return validator.call("Describe table " + table, null, () -> executor
13801380
.describeTable(databaseWithSuffix + table, settings)
13811381
.thenApply(result -> {
13821382
// ignore scheme errors like path not found

jdbc/src/test/resources/logging.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ handlers= java.util.logging.ConsoleHandler
22
.level=INFO
33
java.util.logging.ConsoleHandler.level = ALL
44
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
5-
java.util.logging.SimpleFormatter.format= %1$tF %1$tT [%4$s] %3$s %5$s %n
5+
java.util.logging.SimpleFormatter.format= %1$tF %1$tT [%4$s]\t%3$s %5$s %n
66

77
tech.ydb.jdbc.level=ALL
88
#tech.ydb.level=FINEST

0 commit comments

Comments
 (0)