Skip to content

fix(bigquery-jdbc): add cross-process global log lock fallback and interrupt recovery - #15253

Merged
Neenu1995 merged 2 commits into
mainfrom
fix/bq-jdbc-log-lock-and-interrupt
Nov 28, 2026
Merged

Neenu1995 merged 2 commits into
mainfrom
fix/bq-jdbc-log-lock-and-interrupt

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

Stack: PR 2 of 6 (base: fix/bq-jdbc-log-file-hardening)
Bug: b/584840147

Summary

Adds in-JVM and cross-process sentinel locking on BQ-JDBC-GLOBAL.log with collision-free fallback file allocation and ClosedByInterruptException channel recovery in PerConnectionFileHandler.

Design Decisions

  • In-JVM + cross-process global log coordination: Tracks active canonical global paths in ACTIVE_GLOBAL_PATHS and acquires a non-blocking 1-byte sentinel lock (FileChannel.tryLock(Long.MAX_VALUE - 1, 1L, false)), falling back to an exclusive BQ-JDBC-GLOBAL-<yyyyMMddHHmmss>-<4hex>.log file on contention.
  • Interrupt-safe channel writes & recovery: Preserves the calling thread's interrupt status in finally around NIO channel opens and writes and reopens validated channels after ClosedByInterruptException.

@Neenu1995
Neenu1995 requested review from a team as code owners November 26, 2026 20:44

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the robustness and security of log file handling in PerConnectionFileHandler by implementing a fallback mechanism for the global log file when it is locked or foreign-owned, enforcing strict POSIX file permissions, and robustly handling thread interrupts during write operations. The review feedback highlights a critical path resolution bug in openGlobalChannel where differing canonical paths before and after file creation on symlinked filesystems (like macOS) can leak keys in ACTIVE_GLOBAL_PATHS and permanently lock out future handlers. To resolve this, it is recommended to canonicalize the parent directory from the start and clean up the now-unused canonicalClaimKey helper method.

@Neenu1995
Neenu1995 added this pull request to stack #15256 November 26, 2026 20:52
@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-lock-and-interrupt branch 2 times, most recently from 5652b76 to a1fb46f Compare November 27, 2026 00:51
@Neenu1995 Neenu1995 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 27, 2026
PosixFilePermissions.asFileAttribute(FILE_PERMS_0600);
private static final int PROCESS_UID = resolveProcessUid();
private static final UserPrincipal PROCESS_PRINCIPAL = resolveProcessPrincipal();
static volatile Integer overrideUidForTesting;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. This is a mutable static test hook in production code. Could tests pass the UID in through a package-private parameter instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since both PerConnectionFileHandler and overrideUidForTesting are package-private, external callers outside com.google.cloud.bigquery.jdbc cannot access or mutate it, and effectiveProcessUid() is only read on cold directory/channel open validation (never on log writes). Threading an explicit UID parameter through the constructors, ChannelLogWriter, and the static NIO helpers (ensureSecureDirectory, openGlobalChannel, openAppendValidated, verifyPosixOwner) touches ~45 lines across PRs 2–6 solely for test plumbing, so we kept the single package-private hook to keep the stack diffs minimal.

@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-lock-and-interrupt branch 2 times, most recently from 8abca56 to 5d26bb9 Compare November 27, 2026 22:55
@Neenu1995
Neenu1995 enabled stacked auto-merge (squash) November 28, 2026 00:34
stacked auto-merge was automatically disabled November 28, 2026 00:56

Auto-merge was disabled for another pull request in the stack

Base automatically changed from fix/bq-jdbc-log-file-hardening to main November 28, 2026 00:56
@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-lock-and-interrupt branch from 5d26bb9 to da7426d Compare November 28, 2026 00:56
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 28, 2026
@Neenu1995
Neenu1995 merged commit 5ae9558 into main Nov 28, 2026
195 checks passed
@Neenu1995
Neenu1995 deleted the fix/bq-jdbc-log-lock-and-interrupt branch November 28, 2026 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants