Skip to content

Commit 9e6ae26

Browse files
committed
Introduce connection context to allow correlation of log statements to the actual connection
[resolves #339]
1 parent 1b64fe8 commit 9e6ae26

18 files changed

+257
-61
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,52 @@ Logging facilities:
456456
* Transport Logging (`io.r2dbc.postgresql.client`)
457457
* `DEBUG` enables `Message` exchange logging
458458
* `TRACE` enables traffic logging
459+
460+
Logging that is associated with a connection reports the logical connection id (`cid`) which is a driver-local connection counter and the Postgres Process Id (`pid`) once the connection handshake finishes.
461+
462+
## Getting Help
463+
464+
Having trouble with R2DBC? We'd love to help!
465+
466+
* Check the [spec documentation](https://r2dbc.io/spec/0.8.0.RELEASE/spec/html/), and [Javadoc](https://r2dbc.io/spec/0.8.0.RELEASE/api/).
467+
* If you are upgrading, check out the [changelog](https://r2dbc.io/spec/0.8.0.RELEASE/CHANGELOG.txt) for "new and noteworthy" features.
468+
* Ask a question - we monitor [stackoverflow.com](https://stackoverflow.com) for questions
469+
tagged with [`r2dbc`](https://stackoverflow.com/tags/r2dbc).
470+
You can also chat with the community on [Gitter](https://gitter.im/r2dbc/r2dbc).
471+
* Report bugs with R2DBC PostgreSQL at [github.com/pgjdbc/r2dbc-postgresql/issues](https://github.com/pgjdbc/r2dbc-postgresql/issues).
472+
473+
## Reporting Issues
474+
475+
R2DBC uses GitHub as issue tracking system to record bugs and feature requests.
476+
If you want to raise an issue, please follow the recommendations below:
477+
478+
* Before you log a bug, please search the [issue tracker](https://github.com/pgjdbc/r2dbc-postgresql/issues) to see if someone has already reported the problem.
479+
* If the issue doesn't already exist, [create a new issue](https://github.com/pgjdbc/r2dbc-postgresql/issues/new).
480+
* Please provide as much information as possible with the issue report, we like to know the version of R2DBC PostgreSQL that you are using and JVM version.
481+
* If you need to paste code, or include a stack trace use Markdown ``` escapes before and after your text.
482+
* If possible try to create a test-case or project that replicates the issue.
483+
Attach a link to your code or a compressed file containing your code.
484+
485+
## Building from Source
486+
487+
You don't need to build from source to use R2DBC PostgreSQL (binaries in Maven Central), but if you want to try out the latest and greatest, R2DBC PostgreSQL can be easily built with the
488+
[maven wrapper](https://github.com/takari/maven-wrapper). You also need JDK 1.8 and Docker to run integration tests.
489+
490+
```bash
491+
$ ./mvnw clean install
492+
```
493+
494+
If you want to build with the regular `mvn` command, you will need [Maven v3.5.0 or above](https://maven.apache.org/run-maven/index.html).
495+
496+
_Also see [CONTRIBUTING.adoc](.github/CONTRIBUTING.adoc) if you wish to submit pull requests._
497+
498+
### Running JMH Benchmarks
499+
500+
Running the JMH benchmarks builds and runs the benchmarks without running tests.
501+
502+
```bash
503+
$ ./mvnw clean install -Pjmh
504+
```
459505

460506
## License
461507
This project is released under version 2.0 of the [Apache License][l].

src/main/java/io/r2dbc/postgresql/ConnectionContext.java renamed to src/main/java/io/r2dbc/postgresql/ConnectionResources.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/**
2626
* @author Mark Paluch
2727
*/
28-
final class ConnectionContext {
28+
final class ConnectionResources {
2929

3030
private final Client client;
3131

@@ -39,8 +39,8 @@ final class ConnectionContext {
3939

4040
private final PortalNameSupplier portalNameSupplier;
4141

42-
ConnectionContext(Client client, Codecs codecs, PostgresqlConnection connection, PostgresqlConnectionConfiguration configuration, PortalNameSupplier portalNameSupplier,
43-
StatementCache statementCache) {
42+
ConnectionResources(Client client, Codecs codecs, PostgresqlConnection connection, PostgresqlConnectionConfiguration configuration, PortalNameSupplier portalNameSupplier,
43+
StatementCache statementCache) {
4444
this.client = client;
4545
this.codecs = codecs;
4646
this.connection = connection;

src/main/java/io/r2dbc/postgresql/ExtendedQueryPostgresqlStatement.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ final class ExtendedQueryPostgresqlStatement implements PostgresqlStatement {
4848

4949
private final Bindings bindings;
5050

51-
private final ConnectionContext context;
51+
private final ConnectionResources context;
5252

5353
private final String sql;
5454

5555
private int fetchSize;
5656

5757
private String[] generatedColumns;
5858

59-
ExtendedQueryPostgresqlStatement(ConnectionContext context, String sql) {
59+
ExtendedQueryPostgresqlStatement(ConnectionResources context, String sql) {
6060
this.context = Assert.requireNonNull(context, "context must not be null");
6161
this.sql = Assert.requireNonNull(sql, "sql must not be null");
6262
this.bindings = new Bindings(expectedSize(sql));
@@ -181,7 +181,7 @@ private Flux<io.r2dbc.postgresql.api.PostgresqlResult> execute(String sql) {
181181
.cast(io.r2dbc.postgresql.api.PostgresqlResult.class);
182182
}
183183

184-
static PostgresqlResult createPostgresqlResult(String sql, ExceptionFactory factory, String statementName, Binding binding, ConnectionContext context, int fetchSize) {
184+
static PostgresqlResult createPostgresqlResult(String sql, ExceptionFactory factory, String statementName, Binding binding, ConnectionResources context, int fetchSize) {
185185
Flux<BackendMessage> messages = ExtendedQueryMessageFlow
186186
.execute(binding, context.getClient(), context.getPortalNameSupplier(), statementName, sql, context.getConfiguration().isForceBinary(), fetchSize)
187187
.filter(RESULT_FRAME_FILTER);

src/main/java/io/r2dbc/postgresql/PostgresqlBatch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
*/
2929
final class PostgresqlBatch implements io.r2dbc.postgresql.api.PostgresqlBatch {
3030

31-
private final ConnectionContext context;
31+
private final ConnectionResources context;
3232

3333
private final List<String> statements = new ArrayList<>();
3434

35-
PostgresqlBatch(ConnectionContext context) {
35+
PostgresqlBatch(ConnectionResources context) {
3636
this.context = Assert.requireNonNull(context, "context must not be null");
3737
}
3838

src/main/java/io/r2dbc/postgresql/PostgresqlConnection.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.r2dbc.postgresql.api.PostgresqlResult;
2121
import io.r2dbc.postgresql.api.PostgresqlStatement;
2222
import io.r2dbc.postgresql.client.Client;
23+
import io.r2dbc.postgresql.client.ConnectionContext;
2324
import io.r2dbc.postgresql.client.PortalNameSupplier;
2425
import io.r2dbc.postgresql.client.SimpleQueryMessageFlow;
2526
import io.r2dbc.postgresql.client.TransactionStatus;
@@ -56,10 +57,12 @@ final class PostgresqlConnection implements io.r2dbc.postgresql.api.PostgresqlCo
5657

5758
private final Logger logger = Loggers.getLogger(this.getClass());
5859

59-
private final ConnectionContext context;
60-
6160
private final Client client;
6261

62+
private final ConnectionResources resources;
63+
64+
private final ConnectionContext connectionContext;
65+
6366
private final Codecs codecs;
6467

6568
private final Flux<Integer> validationQuery;
@@ -70,11 +73,12 @@ final class PostgresqlConnection implements io.r2dbc.postgresql.api.PostgresqlCo
7073

7174
PostgresqlConnection(Client client, Codecs codecs, PortalNameSupplier portalNameSupplier, StatementCache statementCache, IsolationLevel isolationLevel,
7275
PostgresqlConnectionConfiguration configuration) {
73-
this.context = new ConnectionContext(client, codecs, this, configuration, portalNameSupplier, statementCache);
7476
this.client = Assert.requireNonNull(client, "client must not be null");
77+
this.resources = new ConnectionResources(client, codecs, this, configuration, portalNameSupplier, statementCache);
78+
this.connectionContext = client.getContext();
7579
this.codecs = Assert.requireNonNull(codecs, "codecs must not be null");
7680
this.isolationLevel = Assert.requireNonNull(isolationLevel, "isolationLevel must not be null");
77-
this.validationQuery = new SimpleQueryPostgresqlStatement(this.context, "SELECT 1").fetchSize(0).execute().flatMap(PostgresqlResult::getRowsUpdated);
81+
this.validationQuery = new SimpleQueryPostgresqlStatement(this.resources, "SELECT 1").fetchSize(0).execute().flatMap(PostgresqlResult::getRowsUpdated);
7882
}
7983

8084
Client getClient() {
@@ -87,7 +91,7 @@ public Mono<Void> beginTransaction() {
8791
if (IDLE == transactionStatus) {
8892
return exchange("BEGIN");
8993
} else {
90-
this.logger.debug("Skipping begin transaction because status is {}", transactionStatus);
94+
this.logger.debug(this.connectionContext.getMessage("Skipping begin transaction because status is {}"), transactionStatus);
9195
return Mono.empty();
9296
}
9397
});
@@ -111,15 +115,15 @@ public Mono<Void> commitTransaction() {
111115
if (IDLE != transactionStatus) {
112116
return exchange("COMMIT");
113117
} else {
114-
this.logger.debug("Skipping commit transaction because status is {}", transactionStatus);
118+
this.logger.debug(this.connectionContext.getMessage("Skipping commit transaction because status is {}"), transactionStatus);
115119
return Mono.empty();
116120
}
117121
});
118122
}
119123

120124
@Override
121125
public PostgresqlBatch createBatch() {
122-
return new PostgresqlBatch(this.context);
126+
return new PostgresqlBatch(this.resources);
123127
}
124128

125129
@Override
@@ -131,7 +135,7 @@ public Mono<Void> createSavepoint(String name) {
131135
if (OPEN == transactionStatus) {
132136
return exchange(String.format("SAVEPOINT %s", name));
133137
} else {
134-
this.logger.debug("Skipping create savepoint because status is {}", transactionStatus);
138+
this.logger.debug(this.connectionContext.getMessage("Skipping create savepoint because status is {}"), transactionStatus);
135139
return Mono.empty();
136140
}
137141
}));
@@ -142,9 +146,9 @@ public PostgresqlStatement createStatement(String sql) {
142146
Assert.requireNonNull(sql, "sql must not be null");
143147

144148
if (SimpleQueryPostgresqlStatement.supports(sql)) {
145-
return new SimpleQueryPostgresqlStatement(this.context, sql);
149+
return new SimpleQueryPostgresqlStatement(this.resources, sql);
146150
} else if (ExtendedQueryPostgresqlStatement.supports(sql)) {
147-
return new ExtendedQueryPostgresqlStatement(this.context, sql);
151+
return new ExtendedQueryPostgresqlStatement(this.resources, sql);
148152
} else {
149153
throw new IllegalArgumentException(String.format("Statement '%s' cannot be created. This is often due to the presence of both multiple statements and parameters at the same time.", sql));
150154
}
@@ -203,7 +207,7 @@ public Mono<Void> releaseSavepoint(String name) {
203207
if (OPEN == transactionStatus) {
204208
return exchange(String.format("RELEASE SAVEPOINT %s", name));
205209
} else {
206-
this.logger.debug("Skipping release savepoint because status is {}", transactionStatus);
210+
this.logger.debug(this.connectionContext.getMessage("Skipping release savepoint because status is {}"), transactionStatus);
207211
return Mono.empty();
208212
}
209213
});
@@ -215,7 +219,7 @@ public Mono<Void> rollbackTransaction() {
215219
if (IDLE != transactionStatus) {
216220
return exchange("ROLLBACK");
217221
} else {
218-
this.logger.debug("Skipping rollback transaction because status is {}", transactionStatus);
222+
this.logger.debug(this.connectionContext.getMessage("Skipping rollback transaction because status is {}"), transactionStatus);
219223
return Mono.empty();
220224
}
221225
});
@@ -229,7 +233,7 @@ public Mono<Void> rollbackTransactionToSavepoint(String name) {
229233
if (IDLE != transactionStatus) {
230234
return exchange(String.format("ROLLBACK TO SAVEPOINT %s", name));
231235
} else {
232-
this.logger.debug("Skipping rollback transaction to savepoint because status is {}", transactionStatus);
236+
this.logger.debug(this.connectionContext.getMessage("Skipping rollback transaction to savepoint because status is {}"), transactionStatus);
233237
return Mono.empty();
234238
}
235239
});
@@ -240,17 +244,17 @@ public Mono<Void> setAutoCommit(boolean autoCommit) {
240244

241245
return useTransactionStatus(transactionStatus -> {
242246

243-
this.logger.debug(String.format("Setting auto-commit mode to [%s]", autoCommit));
247+
this.logger.debug(this.connectionContext.getMessage(String.format("Setting auto-commit mode to [%s]", autoCommit)));
244248

245249
if (isAutoCommit()) {
246250
if (!autoCommit) {
247-
this.logger.debug("Beginning transaction");
251+
this.logger.debug(this.connectionContext.getMessage("Beginning transaction"));
248252
return beginTransaction();
249253
}
250254
} else {
251255

252256
if (autoCommit) {
253-
this.logger.debug("Committing pending transactions");
257+
this.logger.debug(this.connectionContext.getMessage("Committing pending transactions"));
254258
return commitTransaction();
255259
}
256260
}
@@ -305,7 +309,7 @@ public void onNext(Integer integer) {
305309

306310
@Override
307311
public void onError(Throwable t) {
308-
PostgresqlConnection.this.logger.debug("Validation failed", t);
312+
PostgresqlConnection.this.logger.debug(PostgresqlConnection.this.connectionContext.getMessage("Validation failed"), t);
309313
sink.success(false);
310314
}
311315

src/main/java/io/r2dbc/postgresql/PostgresqlResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class PostgresqlResult extends AbstractReferenceCounted implements io.r2db
4545

4646
private static final Predicate<BackendMessage> TAKE_UNTIL = or(CommandComplete.class::isInstance, EmptyQueryResponse.class::isInstance);
4747

48-
private final ConnectionContext context;
48+
private final ConnectionResources context;
4949

5050
private final Flux<BackendMessage> messages;
5151

@@ -55,7 +55,7 @@ final class PostgresqlResult extends AbstractReferenceCounted implements io.r2db
5555

5656
private volatile RowDescription rowDescription;
5757

58-
PostgresqlResult(ConnectionContext context, Flux<BackendMessage> messages, ExceptionFactory factory) {
58+
PostgresqlResult(ConnectionResources context, Flux<BackendMessage> messages, ExceptionFactory factory) {
5959
this.context = Assert.requireNonNull(context, "context must not be null");
6060
this.messages = Assert.requireNonNull(messages, "messages must not be null");
6161
this.factory = Assert.requireNonNull(factory, "factory must not be null");
@@ -139,7 +139,7 @@ public String toString() {
139139
'}';
140140
}
141141

142-
static PostgresqlResult toResult(ConnectionContext context, Flux<BackendMessage> messages, ExceptionFactory factory) {
142+
static PostgresqlResult toResult(ConnectionResources context, Flux<BackendMessage> messages, ExceptionFactory factory) {
143143
return new PostgresqlResult(context, messages, factory);
144144
}
145145

src/main/java/io/r2dbc/postgresql/PostgresqlRow.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
*/
3333
final class PostgresqlRow implements io.r2dbc.postgresql.api.PostgresqlRow {
3434

35-
private final ConnectionContext context;
35+
private final ConnectionResources context;
3636

3737
private final List<RowDescription.Field> fields;
3838

3939
private final ByteBuf[] data;
4040

4141
private volatile boolean isReleased = false;
4242

43-
PostgresqlRow(ConnectionContext context, List<RowDescription.Field> fields, ByteBuf[] data) {
43+
PostgresqlRow(ConnectionResources context, List<RowDescription.Field> fields, ByteBuf[] data) {
4444
this.context = Assert.requireNonNull(context, "context must not be null");
4545
this.fields = Assert.requireNonNull(fields, "fields must not be null");
4646
this.data = Assert.requireNonNull(data, "data must not be null");
@@ -107,7 +107,7 @@ public String toString() {
107107
'}';
108108
}
109109

110-
static PostgresqlRow toRow(ConnectionContext context, DataRow dataRow, RowDescription rowDescription) {
110+
static PostgresqlRow toRow(ConnectionResources context, DataRow dataRow, RowDescription rowDescription) {
111111
Assert.requireNonNull(dataRow, "dataRow must not be null");
112112
Assert.requireNonNull(rowDescription, "rowDescription must not be null");
113113

src/main/java/io/r2dbc/postgresql/SimpleQueryPostgresqlStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ final class SimpleQueryPostgresqlStatement implements PostgresqlStatement {
4545

4646
private static final Predicate<BackendMessage> WINDOW_UNTIL = or(CommandComplete.class::isInstance, EmptyQueryResponse.class::isInstance, ErrorResponse.class::isInstance);
4747

48-
private final ConnectionContext context;
48+
private final ConnectionResources context;
4949

5050
private final String sql;
5151

5252
private String[] generatedColumns;
5353

5454
private int fetchSize;
5555

56-
SimpleQueryPostgresqlStatement(ConnectionContext context, String sql) {
56+
SimpleQueryPostgresqlStatement(ConnectionResources context, String sql) {
5757
this.context = Assert.requireNonNull(context, "context must not be null");
5858
this.sql = Assert.requireNonNull(sql, "sql must not be null");
5959
fetchSize(isBatch() ? NO_LIMIT : this.context.getConfiguration().getFetchSize(sql));

src/main/java/io/r2dbc/postgresql/client/Client.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ default Flux<BackendMessage> exchange(Publisher<FrontendMessage> requests) {
104104
*/
105105
ByteBufAllocator getByteBufAllocator();
106106

107+
/**
108+
* Returns the {@link ConnectionContext}.
109+
*
110+
* @return the {@link ConnectionContext}
111+
* @since 0.8.6
112+
*/
113+
ConnectionContext getContext();
114+
107115
/**
108116
* Returns the connected process id if it has been communicated.
109117
*

0 commit comments

Comments
 (0)