Repository navigation
fix(bigquery-jdbc): gate per-connection log writers on active connection lifecycle #15166
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
Changes from all commits
3b6153a
56be06c
37dc98c
ee09ec6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -72,9 +72,10 @@ class PerConnectionFileHandler extends Handler { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final Level level; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final long maxFileBytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final Formatter logFormatter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final ConcurrentHashMap<String, BoundedLogFileWriter> writers = new ConcurrentHashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final Set<String> registeredConnections = ConcurrentHashMap.newKeySet(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final ConcurrentHashMap<String, ConnectionFileSlot> slots = new ConcurrentHashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final BoundedLogFileWriter defaultWriter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private volatile boolean handlerClosed = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| volatile boolean handlerClosed = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PerConnectionFileHandler(String baseLogPath, Level level) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this(baseLogPath, level, resolveMaxLogFileBytes()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -309,7 +310,7 @@ private static SeekableByteChannel openExclusiveNewChannel(Path file) throws IOE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isPosix); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private BoundedLogFileWriter createLogWriter( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BoundedLogFileWriter createLogWriter( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String id, Path logDir, Level writerLevel, long limitBytes) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (logDir == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -388,6 +389,19 @@ static Path deriveBackupPath(Path activePath) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return activePath.resolveSibling(backup); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void registerConnection(String connectionId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (connectionId != null && !connectionId.isEmpty() && !handlerClosed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registeredConnections.add(connectionId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private boolean canOpenSlotFor(String connectionId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return !handlerClosed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && baseLogPath != null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && (registeredConnections.contains(connectionId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| || BigQueryJdbcRootLogger.isConnectionActive(connectionId)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void publish(LogRecord record) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (handlerClosed || !isLoggable(record)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -403,23 +417,24 @@ public void publish(LogRecord record) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (connectionId != null && !connectionId.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BoundedLogFileWriter writer = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writers.computeIfAbsent( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConnectionFileSlot slot = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slots.compute( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connectionId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handlerClosed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : createLogWriter(id, baseLogPath, this.level, this.maxFileBytes)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (writer != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (handlerClosed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writers.remove(connectionId, writer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer.close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (id, existing) -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (existing != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return existing.isClosed() ? null : existing; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return canOpenSlotFor(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? new ConnectionFileSlot(baseLogPath, level, maxFileBytes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+420
to
+430
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In To prevent silent loss of log records, we should check if the connection is still active when the existing slot is closed, and if so, instantiate and return a new
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (slot != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (slot.publishIfOpen(record, connectionId, this)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (writer.publish(record)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (slot.isClosed()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slots.remove(connectionId, slot); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writers.remove(connectionId, writer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -437,10 +452,11 @@ public void flush() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void close() throws SecurityException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handlerClosed = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (String id : writers.keySet()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BoundedLogFileWriter removed = writers.remove(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registeredConnections.clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (String id : slots.keySet()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConnectionFileSlot removed = slots.remove(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (removed != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| removed.close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| removed.closeSlot(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (defaultWriter != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -450,9 +466,10 @@ public void close() throws SecurityException { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void closeHandler(String connectionId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (connectionId != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BoundedLogFileWriter writer = writers.remove(connectionId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (writer != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer.close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registeredConnections.remove(connectionId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConnectionFileSlot slot = slots.remove(connectionId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (slot != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slot.closeSlot(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -633,4 +650,63 @@ synchronized void close() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final class ConnectionFileSlot { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final Path logPath; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final Level level; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final long maxFileBytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @GuardedBy("this") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private BoundedLogFileWriter writer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private volatile boolean closed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConnectionFileSlot(Path logPath, Level level, long maxFileBytes) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.logPath = logPath; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.level = level; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.maxFileBytes = maxFileBytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean isClosed() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return closed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| synchronized boolean publishIfOpen( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LogRecord record, String connectionId, PerConnectionFileHandler owner) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (closed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| || owner.handlerClosed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| || (!owner.registeredConnections.contains(connectionId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && !BigQueryJdbcRootLogger.isConnectionActive(connectionId))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| closeSlot(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (record.getLevel().intValue() < level.intValue()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (writer == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer = owner.createLogWriter(connectionId, logPath, level, maxFileBytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (writer == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!writer.publish(record)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| closeSlot(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| synchronized void closeSlot() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!closed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| closed = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (writer != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer.close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writer = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a connection is inactive or has been closed,
BigQueryJdbcRootLogger.findActiveContext(connectionId)returnsnull. Currently, a newConnectionLogContextis created withConnectionLogContext.ALL_VALUE. This causes any subsequent logging calls (e.g., from post-closeResultSetorStatementoperations) to proceed with formatting and publishing the log records, only for them to be discarded later by the handler'scanOpenSlotForcheck.By defaulting to
ConnectionLogContext.OFF_VALUEinstead, we can completely avoid the overhead of the logging pipeline for inactive or closed connections.