Skip to content

Add trace logging #1624

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

Open
wants to merge 1 commit into
base: 4.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public NetworkSession(
this.mode = mode;
this.retryLogic = retryLogic;
this.logging = logging;
this.log = new PrefixedLogger("[" + hashCode() + "]", logging.getLog(getClass()));
this.log = new PrefixedLogger(
"[" + Thread.currentThread().getName() + "][" + hashCode() + "]", logging.getLog(getClass()));
this.bookmarkHolder = bookmarkHolder;
CompletableFuture<DatabaseName> databaseNameFuture = databaseName
.databaseName()
Expand Down Expand Up @@ -267,7 +268,11 @@ private CompletionStage<Connection> acquireConnection(AccessMode mode) {
// there somehow is an existing open connection, this should not happen, just a precondition
throw new IllegalStateException("Existing open connection detected");
}
return connectionProvider.acquireConnection(connectionContext.contextWithMode(mode));
log.trace("connectionProvider.acquireConnection");
return connectionProvider
.acquireConnection(connectionContext.contextWithMode(mode))
.whenComplete(
(connection, error) -> log.trace("connectionProvider.acquireConnection finished"));
});

connectionStage = newConnectionStage.exceptionally(error -> null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;
import org.neo4j.driver.Query;
import org.neo4j.driver.Session;
Expand All @@ -46,6 +47,7 @@
import org.neo4j.driver.internal.BookmarkHolder;
import org.neo4j.driver.internal.cursor.AsyncResultCursor;
import org.neo4j.driver.internal.cursor.RxResultCursor;
import org.neo4j.driver.internal.logging.PrefixedLogger;
import org.neo4j.driver.internal.messaging.BoltProtocol;
import org.neo4j.driver.internal.spi.Connection;

Expand Down Expand Up @@ -93,6 +95,7 @@ private enum State {
private CompletableFuture<Void> rollbackFuture;
private Throwable causeOfTermination;
private final Logging logging;
private final Logger log;

public UnmanagedTransaction(Connection connection, BookmarkHolder bookmarkHolder, long fetchSize, Logging logging) {
this(connection, bookmarkHolder, fetchSize, new ResultCursorsHolder(), logging);
Expand All @@ -110,11 +113,16 @@ protected UnmanagedTransaction(
this.resultCursors = resultCursors;
this.fetchSize = fetchSize;
this.logging = logging;
this.log = new PrefixedLogger(
"[" + Thread.currentThread().getName() + "][" + hashCode() + "]", logging.getLog(getClass()));
;
}

public CompletionStage<UnmanagedTransaction> beginAsync(Bookmark initialBookmark, TransactionConfig config) {
log.trace("beginAsync");
return protocol.beginTransaction(connection, initialBookmark, config, logging)
.handle((ignore, beginError) -> {
log.trace("beginAsync protocol.beginTransaction finished");
if (beginError != null) {
if (beginError instanceof AuthorizationExpiredException) {
connection.terminateAndRelease(AuthorizationExpiredException.DESCRIPTION);
Expand Down Expand Up @@ -242,56 +250,76 @@ private static BiFunction<Void, Throwable, Void> handleCommitOrRollback(Throwabl
}

private void handleTransactionCompletion(boolean commitAttempt, Throwable throwable) {
log.trace(
"handleTransactionCompletion(commitAttempt=%b, throwable is null=%b)",
commitAttempt, throwable == null);
executeWithLock(lock, () -> {
log.trace("handleTransactionCompletion lock acquired");
if (commitAttempt && throwable == null) {
state = State.COMMITTED;
} else {
state = State.ROLLED_BACK;
}
});
log.trace("handleTransactionCompletion lock released");
if (throwable instanceof AuthorizationExpiredException) {
connection.terminateAndRelease(AuthorizationExpiredException.DESCRIPTION);
} else if (throwable instanceof ConnectionReadTimeoutException) {
connection.terminateAndRelease(throwable.getMessage());
} else {
connection.release(); // release in background
}
log.trace("handleTransactionCompletion finished");
}

private CompletionStage<Void> closeAsync(boolean commit, boolean completeWithNullIfNotOpen) {
log.trace("closeAsync(commit=%b, completeWithNullIfNotOpen=%b) before lock", commit, completeWithNullIfNotOpen);
CompletionStage<Void> stage = executeWithLock(lock, () -> {
log.trace("closeAsync lock acquired");
CompletionStage<Void> resultStage = null;
if (completeWithNullIfNotOpen && !isOpen()) {
log.trace("closeAsync will complete with null");
resultStage = completedWithNull();
} else if (state == State.COMMITTED) {
log.trace("closeAsync state=%s", state);
resultStage = failedFuture(
new ClientException(commit ? CANT_COMMIT_COMMITTED_MSG : CANT_ROLLBACK_COMMITTED_MSG));
} else if (state == State.ROLLED_BACK) {
log.trace("closeAsync state=%s", state);
resultStage = failedFuture(
new ClientException(commit ? CANT_COMMIT_ROLLED_BACK_MSG : CANT_ROLLBACK_ROLLED_BACK_MSG));
} else {
log.trace("closeAsync state=%s", state);
if (commit) {
if (rollbackFuture != null) {
log.trace("closeAsync rollbackFuture not null");
resultStage = failedFuture(new ClientException(CANT_COMMIT_ROLLING_BACK_MSG));
} else if (commitFuture != null) {
log.trace("closeAsync commitFuture not null");
resultStage = commitFuture;
} else {
log.trace("closeAsync initializing commitFuture");
commitFuture = new CompletableFuture<>();
}
} else {
if (commitFuture != null) {
log.trace("closeAsync commitFuture not null");
resultStage = failedFuture(new ClientException(CANT_ROLLBACK_COMMITTING_MSG));
} else if (rollbackFuture != null) {
log.trace("closeAsync rollbackFuture not null");
resultStage = rollbackFuture;
} else {
log.trace("closeAsync initializing rollbackFuture");
rollbackFuture = new CompletableFuture<>();
}
}
}
return resultStage;
});
log.trace("closeAsync lock released");

if (stage == null) {
log.trace("closeAsync stage is null");
CompletableFuture<Void> targetFuture;
Function<Throwable, CompletionStage<Void>> targetAction;
if (commit) {
Expand All @@ -309,6 +337,7 @@ private CompletionStage<Void> closeAsync(boolean commit, boolean completeWithNul
stage = targetFuture;
}

log.trace("closeAsync finished");
return stage;
}
}