diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java index f26751102db7..6fc7dd168d24 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java @@ -275,7 +275,11 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { .put(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY, this.connectionId) .build(); this.otelContext = Context.current().with(baggage); + boolean initialized = false; + ExecutorService createdQueryExecutor = null; + ExecutorService createdMetadataExecutor = null; try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(this.connectionId)) { + BigQueryJdbcRootLogger.registerConnection(this.connectionId); this.connectionUrl = url; if (LOG.isLoggable(Level.CONFIG)) { Properties connectionProps = ds.createProperties(); @@ -413,11 +417,27 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { this.bigQuery = getBigQueryConnection(); // Cached pool executes queries immediately without queueing and reclaims all idle threads // when inactive, minimizing resources. - this.queryExecutor = + createdQueryExecutor = BigQueryJdbcMdc.newCachedThreadPool(String.format("BQ-Query-%s", connectionId)); - this.metadataExecutor = + this.queryExecutor = createdQueryExecutor; + createdMetadataExecutor = BigQueryJdbcMdc.newFixedThreadPool( String.format("BQ-Metadata-%s", connectionId), this.metadataFetchThreadCount); + this.metadataExecutor = createdMetadataExecutor; + initialized = true; + } finally { + if (!initialized) { + if (createdQueryExecutor != null) { + createdQueryExecutor.shutdownNow(); + } + if (createdMetadataExecutor != null) { + createdMetadataExecutor.shutdownNow(); + } + BigQueryJdbcRootLogger.closeConnectionHandler(this.connectionId); + BigQueryJdbcOpenTelemetry.unregisterConnection(this.connectionId); + BigQueryJdbcOpenTelemetry.releaseSdk(this.openTelemetry); + this.openTelemetry = null; + } } } @@ -1329,6 +1349,7 @@ private OpenTelemetry getOpenTelemetryInstance() { effectiveProjectId, this.credentials, this.proxyProperties); + this.openTelemetry = openTelemetry; boolean hasExternalOtel = this.customOpenTelemetry != null || this.useGlobalOpenTelemetry; Logging localLoggingClient = null; diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java index 0acea77e5bf6..ef67a9cc9719 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java @@ -115,7 +115,7 @@ private static boolean isLoggerClass(String className) { return className.equals("com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger") || className.equals("com.google.cloud.bigquery.jdbc.BigQueryJdbcResultSetLogger") || className.startsWith("com.google.cloud.bigquery.jdbc.BigQueryJdbcRootLogger") - || className.equals("com.google.cloud.bigquery.jdbc.PerConnectionFileHandler") + || className.startsWith("com.google.cloud.bigquery.jdbc.PerConnectionFileHandler") || className.equals(BigQueryJdbcLogRecord.class.getName()); } } diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcResultSetLogger.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcResultSetLogger.java index 2e5753fa2c46..6ead87ad49a2 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcResultSetLogger.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcResultSetLogger.java @@ -31,10 +31,11 @@ * plus the connection ID of the ResultSet it serves. * *

Hot-path methods ({@code finestTrace} etc.) set the source class and method explicitly, so no - * stack-trace walking (caller inference) happens for them. The connection ID is stamped into {@code - * LogRecord.getParameters()[0]} on every record so {@code PerConnectionFileHandler} can route the - * record even when the thread-local MDC (mapped diagnostic context — the per-thread connection ID - * set by the JDBC proxy) is empty, which is the normal case for un-proxied ResultSet calls. + * stack-trace walking (caller inference) happens for them. A typed {@link ConnectionLogContext} is + * stamped into {@code LogRecord.getParameters()[0]} on every record so {@code + * PerConnectionFileHandler} can route the record even when the thread-local MDC (mapped diagnostic + * context — the per-thread connection ID set by the JDBC proxy) is empty, which is the normal case + * for un-proxied ResultSet calls. */ public final class BigQueryJdbcResultSetLogger { @@ -49,6 +50,7 @@ public final class BigQueryJdbcResultSetLogger { private final BigQueryJdbcCustomLogger delegate; private final String targetClassName; private final String connectionId; + private final ConnectionLogContext connectionContext; private volatile BigQueryJdbcResultSetLogger arrowStructLogger; private volatile BigQueryJdbcResultSetLogger jsonStructLogger; @@ -56,25 +58,41 @@ public final class BigQueryJdbcResultSetLogger { private volatile BigQueryJdbcResultSetLogger jsonArrayLogger; private BigQueryJdbcResultSetLogger( - BigQueryJdbcCustomLogger delegate, String targetClassName, String connectionId) { + BigQueryJdbcCustomLogger delegate, + String targetClassName, + ConnectionLogContext connectionContext) { this.delegate = delegate; this.targetClassName = targetClassName; - this.connectionId = connectionId; + this.connectionContext = connectionContext; + this.connectionId = connectionContext != null ? connectionContext.connectionId() : null; } public static BigQueryJdbcResultSetLogger getLogger(Class clazz) { - return getLogger(clazz, null); + return getLogger(clazz, (ConnectionLogContext) null); } public static BigQueryJdbcResultSetLogger getLogger(Class clazz, String connectionId) { + if (connectionId == null || connectionId.isEmpty()) { + return getLogger(clazz, (ConnectionLogContext) null); + } + ConnectionLogContext active = BigQueryJdbcRootLogger.findActiveContext(connectionId); + ConnectionLogContext context = + active != null + ? active + : new ConnectionLogContext(connectionId, ConnectionLogContext.ALL_VALUE); + return getLogger(clazz, context); + } + + static BigQueryJdbcResultSetLogger getLogger( + Class clazz, ConnectionLogContext connectionContext) { String className = clazz.getName(); BigQueryJdbcCustomLogger delegate = DELEGATES.computeIfAbsent(className, BigQueryJdbcCustomLogger::new); - if (connectionId == null) { + if (connectionContext == null) { return GLOBAL_FACADES.computeIfAbsent( className, k -> new BigQueryJdbcResultSetLogger(delegate, k, null)); } - return new BigQueryJdbcResultSetLogger(delegate, className, connectionId); + return new BigQueryJdbcResultSetLogger(delegate, className, connectionContext); } public String getConnectionId() { @@ -92,46 +110,46 @@ public boolean isLoggable(Level level) { public BigQueryJdbcResultSetLogger getArrowStructLogger() { if (arrowStructLogger == null) { - arrowStructLogger = getLogger(BigQueryArrowStruct.class, connectionId); + arrowStructLogger = getLogger(BigQueryArrowStruct.class, connectionContext); } return arrowStructLogger; } public BigQueryJdbcResultSetLogger getJsonStructLogger() { if (jsonStructLogger == null) { - jsonStructLogger = getLogger(BigQueryJsonStruct.class, connectionId); + jsonStructLogger = getLogger(BigQueryJsonStruct.class, connectionContext); } return jsonStructLogger; } public BigQueryJdbcResultSetLogger getArrowArrayLogger() { if (arrowArrayLogger == null) { - arrowArrayLogger = getLogger(BigQueryArrowArray.class, connectionId); + arrowArrayLogger = getLogger(BigQueryArrowArray.class, connectionContext); } return arrowArrayLogger; } public BigQueryJdbcResultSetLogger getJsonArrayLogger() { if (jsonArrayLogger == null) { - jsonArrayLogger = getLogger(BigQueryJsonArray.class, connectionId); + jsonArrayLogger = getLogger(BigQueryJsonArray.class, connectionContext); } return jsonArrayLogger; } /** - * Stamps the connection ID into {@code params[0]} and forwards to the shared logger. Does not - * touch the record's source class/method: hot-path callers set them explicitly, and cold-path - * callers ({@code severe}, {@code warning}) rely on {@link + * Stamps the {@link ConnectionLogContext} into {@code params[0]} and forwards to the shared + * logger. Does not touch the record's source class/method: hot-path callers set them explicitly, + * and cold-path callers ({@code severe}, {@code warning}) rely on {@link * BigQueryJdbcCustomLogger.BigQueryJdbcLogRecord} to infer them lazily. */ public void log(LogRecord record) { - if (connectionId != null) { + if (connectionContext != null) { Object[] existingParams = record.getParameters(); if (existingParams == null || existingParams.length == 0) { - record.setParameters(new Object[] {connectionId}); - } else { + record.setParameters(new Object[] {connectionContext}); + } else if (!(existingParams[0] instanceof ConnectionLogContext)) { Object[] newParams = new Object[existingParams.length + 1]; - newParams[0] = connectionId; + newParams[0] = connectionContext; System.arraycopy(existingParams, 0, newParams, 1, existingParams.length); record.setParameters(newParams); } diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcRootLogger.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcRootLogger.java index b6e1b13afc7e..14bb9886aba8 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcRootLogger.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcRootLogger.java @@ -25,6 +25,7 @@ import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.Handler; @@ -43,8 +44,10 @@ class BigQueryJdbcRootLogger { private static final Logger storageLogger = Logger.getLogger("com.google.cloud.bigquery.storage"); private static final boolean isTest = Boolean.getBoolean("JDBC_TESTS"); + private static final ConcurrentHashMap ACTIVE_CONNECTIONS = + new ConcurrentHashMap<>(); - private static Handler fileHandler = null; + private static volatile Handler fileHandler = null; static final String PROCESS_ID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; @@ -104,9 +107,9 @@ public String format(LogRecord record) { String date = DATE_FORMATTER.format(Instant.ofEpochMilli(record.getMillis())); String connectionId = BigQueryJdbcMdc.getConnectionId(); if (connectionId == null || connectionId.isEmpty()) { - Object[] params = record.getParameters(); - if (params != null && params.length > 0 && params[0] instanceof String) { - connectionId = (String) params[0]; + ConnectionLogContext context = ConnectionLogContext.extractFromParameters(record); + if (context != null) { + connectionId = context.connectionId(); } } String connStr = @@ -162,7 +165,7 @@ public static Logger getRootLogger() { return logger; } - public static void setLevel(Level level, String logPath) throws IOException { + public static synchronized void setLevel(Level level, String logPath) throws IOException { if (level != Level.OFF) { if (logPath != null) { setPath(logPath, level); @@ -177,7 +180,7 @@ public static void setLevel(Level level, String logPath) throws IOException { logger.setLevel(level); } - static void setPath(String logPath, Level level) { + static synchronized void setPath(String logPath, Level level) { try { if (logPath == null) { logPath = ""; @@ -201,9 +204,46 @@ static void setPath(String logPath, Level level) { } } + static ConnectionLogContext registerConnection(String connectionId) { + if (connectionId == null || connectionId.isEmpty()) { + return null; + } + ConnectionLogContext context = + ACTIVE_CONNECTIONS.computeIfAbsent( + connectionId, id -> new ConnectionLogContext(id, ConnectionLogContext.ALL_VALUE)); + Handler currentHandler = fileHandler; + if (currentHandler instanceof PerConnectionFileHandler) { + ((PerConnectionFileHandler) currentHandler).registerConnection(connectionId); + } + return context; + } + + static ConnectionLogContext findActiveContext(String connectionId) { + if (connectionId == null || connectionId.isEmpty()) { + return null; + } + return ACTIVE_CONNECTIONS.get(connectionId); + } + + static boolean isConnectionActive(String connectionId) { + return connectionId != null && ACTIVE_CONNECTIONS.containsKey(connectionId); + } + + static int activeConnectionCount() { + return ACTIVE_CONNECTIONS.size(); + } + public static void closeConnectionHandler(String connectionId) { - if (fileHandler instanceof PerConnectionFileHandler) { - ((PerConnectionFileHandler) fileHandler).closeHandler(connectionId); + if (connectionId == null) { + return; + } + ConnectionLogContext removed = ACTIVE_CONNECTIONS.remove(connectionId); + if (removed != null) { + removed.setLevelValue(ConnectionLogContext.OFF_VALUE); + } + Handler currentHandler = fileHandler; + if (currentHandler instanceof PerConnectionFileHandler) { + ((PerConnectionFileHandler) currentHandler).closeHandler(connectionId); } } } diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/ConnectionLogContext.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/ConnectionLogContext.java new file mode 100644 index 000000000000..073b2333a72b --- /dev/null +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/ConnectionLogContext.java @@ -0,0 +1,66 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.bigquery.jdbc; + +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +/** + * Carries per-connection logging identity and threshold across threads and unproxied {@code + * ResultSet} log records without colliding with user or library {@code String} log parameters. + */ +final class ConnectionLogContext { + static final int OFF_VALUE = Level.OFF.intValue(); + static final int ALL_VALUE = Level.ALL.intValue(); + + private final String connectionId; + private volatile int levelValue; + + ConnectionLogContext(String connectionId, int levelValue) { + this.connectionId = Objects.requireNonNull(connectionId, "connectionId cannot be null"); + this.levelValue = levelValue; + } + + String connectionId() { + return connectionId; + } + + int levelValue() { + return levelValue; + } + + void setLevelValue(int levelValue) { + this.levelValue = levelValue; + } + + static ConnectionLogContext extractFromParameters(LogRecord record) { + if (record == null) { + return null; + } + Object[] params = record.getParameters(); + if (params != null && params.length > 0 && params[0] instanceof ConnectionLogContext) { + return (ConnectionLogContext) params[0]; + } + return null; + } + + @Override + public String toString() { + return connectionId; + } +} diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandler.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandler.java index 79874f7d085a..fbd7a5c844be 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandler.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandler.java @@ -28,7 +28,9 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanContext; import io.opentelemetry.context.Context; +import java.text.MessageFormat; import java.time.Instant; +import java.util.Arrays; import java.util.Collections; import java.util.logging.ErrorManager; import java.util.logging.Handler; @@ -63,6 +65,13 @@ public void publish(LogRecord record) { connectionId = BigQueryJdbcMdc.getConnectionId(); } + if (connectionId == null || connectionId.isEmpty()) { + ConnectionLogContext context = ConnectionLogContext.extractFromParameters(record); + if (context != null) { + connectionId = context.connectionId(); + } + } + if (connectionId == null || connectionId.isEmpty()) { return; } @@ -97,8 +106,9 @@ private void publishToGcp(LogRecord record, String connectionId, Logging logging logId = UNSAFE_LOG_CHARACTERS.matcher(logId).replaceAll("_"); } + String formattedBody = formatMessage(record); LogEntry.Builder builder = - LogEntry.newBuilder(Payload.StringPayload.of(formatMessage(record))) + LogEntry.newBuilder(Payload.StringPayload.of(formattedBody != null ? formattedBody : "")) .setSeverity(mapGcpSeverity(record.getLevel())) .setTimestamp(Instant.ofEpochMilli(record.getMillis())) .setLogName(logId); @@ -135,13 +145,16 @@ private void publishToOTel(LogRecord record, String connectionId, OpenTelemetry ? loggerName : BigQueryJdbcOpenTelemetry.INSTRUMENTATION_SCOPE_NAME); + String formattedBody = formatMessage(record); LogRecordBuilder builder = logger .logRecordBuilder() - .setBody(formatMessage(record)) .setSeverity(mapSeverity(record.getLevel())) .setTimestamp(Instant.ofEpochMilli(record.getMillis())) .setContext(Context.current()); + if (formattedBody != null) { + builder.setBody(formattedBody); + } if (connectionId != null) { builder.setAttribute( @@ -165,15 +178,33 @@ private Severity mapSeverity(Level level) { private String formatMessage(LogRecord record) { String message = record.getMessage(); + if (message == null) { + return null; + } Object[] params = record.getParameters(); - if (params != null && params.length > 0) { - try { - return java.text.MessageFormat.format(message, params); - } catch (IllegalArgumentException e) { - return message; + if (params == null || params.length == 0) { + return message; + } + int startIndex = (params[0] instanceof ConnectionLogContext) ? 1 : 0; + if (params.length <= startIndex || !hasPositionalPlaceholder(message)) { + return message; + } + Object[] messageParams = + startIndex == 0 ? params : Arrays.copyOfRange(params, startIndex, params.length); + try { + return MessageFormat.format(message, messageParams); + } catch (IllegalArgumentException e) { + return message; + } + } + + private static boolean hasPositionalPlaceholder(String message) { + for (int i = 0; i <= 9; i++) { + if (message.indexOf("{" + i) >= 0) { + return true; } } - return message; + return false; } @Override diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java index 0b5279cf6bf0..f743e08d2814 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java @@ -16,130 +16,513 @@ package com.google.cloud.bigquery.jdbc; +import com.google.common.annotations.VisibleForTesting; import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.SeekableByteChannel; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.FileHandler; +import java.util.logging.ErrorManager; +import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; +import javax.annotation.concurrent.GuardedBy; /** * Custom logging handler that dynamically creates and routes log records to per-connection specific - * log files using the connection ID retrieved from BigQueryJdbcMdc. + * log files using the connection ID retrieved from {@link BigQueryJdbcMdc} or a typed {@link + * ConnectionLogContext}. + * + *

Uses NIO {@link SeekableByteChannel} directly rather than {@link + * java.util.logging.FileHandler} so paths are never parsed for {@code %} tokens, no {@code .lck} + * lock files or world-readable {@code .1} fallback files are created, and {@code 0600} POSIX file + * permissions are preserved across size-cap wraps. */ class PerConnectionFileHandler extends Handler { + static final int DEFAULT_MAX_LOG_FILE_BYTES = 100 * 1024 * 1024; + static final String MAX_LOG_FILE_BYTES_SYSTEM_PROPERTY = + "com.google.cloud.bigquery.jdbc.maxLogFileBytes"; + + private static final String DEFAULT_HANDLER_ID = "Jdbc-default"; + private static final int MAX_COLLISION_ATTEMPTS = 64; + private static final DateTimeFormatter TIMESTAMP_FORMATTER = + DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); + private static final Set OWNER_DIR_PERMS = + PosixFilePermissions.fromString("rwx------"); + private static final Set OWNER_FILE_PERMS = + PosixFilePermissions.fromString("rw-------"); + private final Path baseLogPath; private final Level level; - private final ConcurrentHashMap handlers = new ConcurrentHashMap<>(); - private FileHandler defaultHandler; + private final int maxFileBytes; + private final Formatter logFormatter; + private final Set registeredConnections = ConcurrentHashMap.newKeySet(); + private final ConcurrentHashMap slots = new ConcurrentHashMap<>(); + private final BoundedLogFileWriter defaultWriter; + private volatile boolean handlerClosed = false; PerConnectionFileHandler(String baseLogPath, Level level) { - this.baseLogPath = Paths.get(baseLogPath != null ? baseLogPath : "").toAbsolutePath(); + this(baseLogPath, level, resolveMaxLogFileBytes()); + } + + @VisibleForTesting + PerConnectionFileHandler(String baseLogPath, Level level, int maxFileBytes) { this.level = level; + this.maxFileBytes = maxFileBytes > 0 ? maxFileBytes : DEFAULT_MAX_LOG_FILE_BYTES; + this.logFormatter = BigQueryJdbcRootLogger.getFormatter(); + setLevel(level); + setFormatter(this.logFormatter); + Path requestedPath = Paths.get(baseLogPath != null ? baseLogPath : "").toAbsolutePath(); + Path verifiedDir = requestedPath; + BoundedLogFileWriter initializedDefault = null; try { - if (!Files.exists(this.baseLogPath)) { - Files.createDirectories(this.baseLogPath); - } - - this.defaultHandler = createFileHandler("Jdbc-default"); + verifiedDir = ensureSecureDirectory(requestedPath); + initializedDefault = + createLogWriter(DEFAULT_HANDLER_ID, verifiedDir, this.level, this.maxFileBytes); } catch (IOException e) { - reportError( - "Failed to initialize default log file", e, java.util.logging.ErrorManager.OPEN_FAILURE); + reportError("Failed to initialize default log file", e, ErrorManager.OPEN_FAILURE); } + this.baseLogPath = verifiedDir; + this.defaultWriter = initializedDefault; } - private String getLogFilePath(String id) { - if ("Jdbc-default".equals(id)) { - return baseLogPath.resolve("BQ-JDBC-GLOBAL.log").toString(); + static int resolveMaxLogFileBytes() { + Integer configured = Integer.getInteger(MAX_LOG_FILE_BYTES_SYSTEM_PROPERTY); + if (configured != null && configured > 0) { + return configured; } - String uuid = id; - if (id.startsWith("BQ-JDBC-")) { - uuid = id.substring("BQ-JDBC-".length()); + return DEFAULT_MAX_LOG_FILE_BYTES; + } + + private static boolean isPosixFileSystem(Path path) { + return path.getFileSystem().supportedFileAttributeViews().contains("posix"); + } + + /** + * Ensures {@code dir} exists as a real directory without modifying permissions on pre-existing + * shared directories, creating any missing subdirectories with {@code 0700} on POSIX systems, and + * rejecting symbolic links below top-level OS root links (such as macOS {@code /tmp} or {@code + * /var}). + */ + static Path ensureSecureDirectory(Path dir) throws IOException { + Path absPath = dir.toAbsolutePath(); + Path root = absPath.getRoot(); + if (root == null) { + throw new IOException("Log path has no filesystem root: " + dir); } - String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); - String shortUuid = uuid.length() >= 4 ? uuid.substring(0, 4) : uuid; - return baseLogPath.resolve("BQ-JDBC-" + timestamp + "-" + shortUuid + ".log").toString(); + boolean isPosix = isPosixFileSystem(absPath); + FileAttribute[] attrs = + isPosix + ? new FileAttribute[] {PosixFilePermissions.asFileAttribute(OWNER_DIR_PERMS)} + : new FileAttribute[0]; + + Path current = root; + int depth = 0; + for (Path segment : absPath) { + String part = segment.toString(); + if (".".equals(part) || part.isEmpty()) { + continue; + } + if ("..".equals(part)) { + Path parent = current.getParent(); + if (parent != null) { + current = parent; + depth = Math.max(0, depth - 1); + } + continue; + } + depth++; + current = current.resolve(segment); + if (Files.isSymbolicLink(current)) { + if (depth == 1 && Files.isDirectory(current)) { + current = current.toRealPath(); + depth = current.getNameCount(); + continue; + } + throw new IOException("Refusing to traverse symbolic link in log path: " + current); + } + if (Files.exists(current, LinkOption.NOFOLLOW_LINKS)) { + if (!Files.isDirectory(current, LinkOption.NOFOLLOW_LINKS)) { + throw new IOException("Log path component is not a directory: " + current); + } + } else { + try { + Files.createDirectory(current, attrs); + if (isPosix) { + Files.setPosixFilePermissions(current, OWNER_DIR_PERMS); + } + } catch (FileAlreadyExistsException e) { + if (Files.isSymbolicLink(current) + || !Files.isDirectory(current, LinkOption.NOFOLLOW_LINKS)) { + throw new IOException("Log path component is not a regular directory: " + current, e); + } + } + } + } + return current; + } + + /** + * Opens or creates {@code file} for appending with {@code 0600} permissions on POSIX systems, + * rejecting symbolic links, non-regular files, and files owned by a different OS user. + */ + static SeekableByteChannel openSecureAppendChannel(Path file) throws IOException { + if (Files.isSymbolicLink(file)) { + throw new IOException("Refusing to write logs to symbolic link: " + file); + } + boolean isPosix = isPosixFileSystem(file); + FileAttribute[] attrs = + isPosix + ? new FileAttribute[] {PosixFilePermissions.asFileAttribute(OWNER_FILE_PERMS)} + : new FileAttribute[0]; + + Set createNewOpts = new HashSet<>(); + createNewOpts.add(StandardOpenOption.CREATE_NEW); + createNewOpts.add(StandardOpenOption.WRITE); + try { + SeekableByteChannel channel = Files.newByteChannel(file, createNewOpts, attrs); + if (isPosix) { + Files.setPosixFilePermissions(file, OWNER_FILE_PERMS); + } + return channel; + } catch (FileAlreadyExistsException existsEx) { + if (Files.isSymbolicLink(file) || !Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) { + throw new IOException("Refusing to write logs to non-regular file: " + file, existsEx); + } + if (isPosix) { + String currentUser = System.getProperty("user.name"); + String ownerName = Files.getOwner(file, LinkOption.NOFOLLOW_LINKS).getName(); + if (currentUser != null && !currentUser.isEmpty() && !currentUser.equals(ownerName)) { + throw new FileAlreadyExistsException( + file.toString(), null, "Log file is owned by a different OS user: " + ownerName); + } + Files.setPosixFilePermissions(file, OWNER_FILE_PERMS); + } + Set writeExistingOpts = new HashSet<>(); + writeExistingOpts.add(StandardOpenOption.WRITE); + writeExistingOpts.add(LinkOption.NOFOLLOW_LINKS); + SeekableByteChannel channel = Files.newByteChannel(file, writeExistingOpts); + channel.position(channel.size()); + return channel; + } + } + + private static SeekableByteChannel openExclusiveNewChannel(Path file) throws IOException { + boolean isPosix = isPosixFileSystem(file); + FileAttribute[] attrs = + isPosix + ? new FileAttribute[] {PosixFilePermissions.asFileAttribute(OWNER_FILE_PERMS)} + : new FileAttribute[0]; + Set opts = new HashSet<>(); + opts.add(StandardOpenOption.CREATE_NEW); + opts.add(StandardOpenOption.WRITE); + SeekableByteChannel channel = Files.newByteChannel(file, opts, attrs); + if (isPosix) { + Files.setPosixFilePermissions(file, OWNER_FILE_PERMS); + } + return channel; } - private FileHandler createFileHandler(String id) { + private BoundedLogFileWriter createLogWriter( + String id, Path logDir, Level writerLevel, int limitBytes) { try { - String filePath = getLogFilePath(id); - FileHandler fh = new FileHandler(filePath, 0, 1, true); - fh.setLevel(level); - fh.setFormatter(BigQueryJdbcRootLogger.getFormatter()); - return fh; + Path verifiedDir = ensureSecureDirectory(logDir); + SeekableByteChannel channel = openChannelForId(verifiedDir, id); + return new BoundedLogFileWriter(channel, writerLevel, limitBytes, logFormatter, this); } catch (IOException e) { - reportError( - "Failed to create log file for connection " + id, - e, - java.util.logging.ErrorManager.OPEN_FAILURE); + reportError("Failed to create log file for connection " + id, e, ErrorManager.OPEN_FAILURE); return null; } } + private static SeekableByteChannel openChannelForId(Path logDir, String id) throws IOException { + if (DEFAULT_HANDLER_ID.equals(id)) { + Path globalPath = logDir.resolve("BQ-JDBC-GLOBAL.log"); + try { + return openSecureAppendChannel(globalPath); + } catch (FileAlreadyExistsException foreignOwnerEx) { + // Shared directory (e.g., /tmp) already contains another OS user's global log file; + // isolate this JVM's global log by process ID using exclusive creation. + for (int attempt = 0; attempt < MAX_COLLISION_ATTEMPTS; attempt++) { + String suffix = + attempt == 0 + ? BigQueryJdbcRootLogger.PROCESS_ID + : BigQueryJdbcRootLogger.PROCESS_ID + "-" + attempt; + Path pidGlobal = logDir.resolve("BQ-JDBC-GLOBAL-" + suffix + ".log"); + try { + return openSecureAppendChannel(pidGlobal); + } catch (FileAlreadyExistsException retryEx) { + // Try next suffix. + } + } + throw foreignOwnerEx; + } + } + + String uuid = id.startsWith("BQ-JDBC-") ? id.substring("BQ-JDBC-".length()) : id; + String timestamp = LocalDateTime.now().format(TIMESTAMP_FORMATTER); + String shortUuid = uuid.length() >= 4 ? uuid.substring(0, 4) : uuid; + for (int attempt = 0; attempt < MAX_COLLISION_ATTEMPTS; attempt++) { + String fileName = + attempt == 0 + ? "BQ-JDBC-" + timestamp + "-" + shortUuid + ".log" + : "BQ-JDBC-" + timestamp + "-" + attempt + "-" + shortUuid + ".log"; + try { + return openExclusiveNewChannel(logDir.resolve(fileName)); + } catch (FileAlreadyExistsException e) { + // Same-second prefix collision or pre-existing file; retry with collision counter. + } + } + throw new IOException("Unable to allocate unique log file for connection " + id); + } + + void registerConnection(String connectionId) { + if (connectionId == null || connectionId.isEmpty() || handlerClosed) { + return; + } + registeredConnections.add(connectionId); + } + + private boolean canOpenSlotFor(String connectionId) { + return !handlerClosed + && (registeredConnections.contains(connectionId) + || BigQueryJdbcRootLogger.isConnectionActive(connectionId)); + } + @Override public void publish(LogRecord record) { - if (!isLoggable(record)) { + if (handlerClosed || !isLoggable(record)) { return; } String connectionId = BigQueryJdbcMdc.getConnectionId(); if (connectionId == null || connectionId.isEmpty()) { - Object[] params = record.getParameters(); - if (params != null && params.length > 0 && params[0] instanceof String) { - connectionId = (String) params[0]; + ConnectionLogContext context = ConnectionLogContext.extractFromParameters(record); + if (context != null) { + connectionId = context.connectionId(); } } - FileHandler handler = defaultHandler; if (connectionId != null && !connectionId.isEmpty()) { - handler = handlers.computeIfAbsent(connectionId, this::createFileHandler); - if (handler == null) { - handler = defaultHandler; + ConnectionFileSlot slot = + slots.compute( + connectionId, + (id, existing) -> { + if (existing != null) { + return existing.isClosed() ? null : existing; + } + if (!canOpenSlotFor(id)) { + return null; + } + return new ConnectionFileSlot(baseLogPath, level, maxFileBytes); + }); + if (slot != null && slot.publishIfOpen(record, connectionId, this)) { + return; } } - if (handler != null) { - handler.publish(record); + if (!handlerClosed && defaultWriter != null) { + defaultWriter.publish(record); } } @Override public void flush() { - if (defaultHandler != null) { - defaultHandler.flush(); + if (defaultWriter != null) { + defaultWriter.flush(); } - for (FileHandler h : handlers.values()) { - h.flush(); + for (ConnectionFileSlot slot : slots.values()) { + slot.flushSlot(); } } @Override public void close() throws SecurityException { - for (FileHandler h : handlers.values()) { - try { - h.close(); - } catch (Exception e) { + handlerClosed = true; + registeredConnections.clear(); + for (String id : slots.keySet()) { + ConnectionFileSlot removed = slots.remove(id); + if (removed != null) { + try { + removed.closeSlot(); + } catch (Exception ignored) { + // Best-effort close across remaining connection slots. + } } } - try { - if (defaultHandler != null) defaultHandler.close(); - } finally { - handlers.clear(); + if (defaultWriter != null) { + defaultWriter.close(); } } public void closeHandler(String connectionId) { - if (connectionId != null) { - FileHandler handler = handlers.remove(connectionId); - if (handler != null) { - handler.close(); + if (connectionId == null) { + return; + } + registeredConnections.remove(connectionId); + ConnectionFileSlot slot = slots.remove(connectionId); + if (slot != null) { + slot.closeSlot(); + } + } + + private void reportHandlerError(String msg, Exception ex, int code) { + reportError(msg, ex, code); + } + + private static final class BoundedLogFileWriter { + private final SeekableByteChannel channel; + private final Level writerLevel; + private final int maxBytes; + private final Formatter formatter; + private final PerConnectionFileHandler owner; + + @GuardedBy("this") + private long writtenBytes; + + @GuardedBy("this") + private boolean closed; + + BoundedLogFileWriter( + SeekableByteChannel channel, + Level writerLevel, + int maxBytes, + Formatter formatter, + PerConnectionFileHandler owner) + throws IOException { + this.channel = channel; + this.writerLevel = writerLevel; + this.maxBytes = maxBytes; + this.formatter = formatter; + this.owner = owner; + this.writtenBytes = channel.size(); + } + + synchronized void publish(LogRecord record) { + if (closed || record.getLevel().intValue() < writerLevel.intValue()) { + return; + } + String formatted; + try { + formatted = formatter.format(record); + } catch (Exception ex) { + owner.reportHandlerError("Failed to format log record", ex, ErrorManager.FORMAT_FAILURE); + return; + } + byte[] bytes = formatted.getBytes(StandardCharsets.UTF_8); + try { + if (maxBytes > 0 && writtenBytes > 0 && writtenBytes + bytes.length > maxBytes) { + channel.truncate(0); + channel.position(0); + writtenBytes = 0; + } + ByteBuffer buffer = ByteBuffer.wrap(bytes); + while (buffer.hasRemaining()) { + writtenBytes += channel.write(buffer); + } + } catch (IOException ex) { + owner.reportHandlerError("Failed to write log record", ex, ErrorManager.WRITE_FAILURE); + } + } + + synchronized void flush() { + if (closed) { + return; + } + if (channel instanceof FileChannel) { + try { + ((FileChannel) channel).force(false); + } catch (IOException ex) { + owner.reportHandlerError("Failed to flush log file", ex, ErrorManager.FLUSH_FAILURE); + } + } + } + + synchronized void close() { + if (closed) { + return; + } + closed = true; + try { + channel.close(); + } catch (IOException ex) { + owner.reportHandlerError("Failed to close log file", ex, ErrorManager.CLOSE_FAILURE); + } + } + } + + private static final class ConnectionFileSlot { + private final Path logPath; + private final Level level; + private final int maxFileBytes; + + @GuardedBy("this") + private BoundedLogFileWriter writer; + + @GuardedBy("this") + private boolean closed; + + ConnectionFileSlot(Path logPath, Level level, int maxFileBytes) { + this.logPath = logPath; + this.level = level; + this.maxFileBytes = maxFileBytes; + } + + synchronized boolean isClosed() { + return closed; + } + + synchronized boolean publishIfOpen( + LogRecord record, String connectionId, PerConnectionFileHandler owner) { + if (closed || owner.handlerClosed || !owner.canOpenSlotFor(connectionId)) { + closeSlot(); + owner.slots.remove(connectionId, this); + return false; + } + if (writer == null) { + writer = owner.createLogWriter(connectionId, logPath, level, maxFileBytes); + if (writer == null) { + return false; + } + } + writer.publish(record); + return true; + } + + synchronized void flushSlot() { + if (!closed && writer != null) { + writer.flush(); + } + } + + synchronized void closeSlot() { + if (closed) { + return; + } + closed = true; + if (writer != null) { + try { + writer.close(); + } finally { + writer = null; + } } } } diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java index c4a6d576f91c..abc2eeb50591 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java @@ -335,7 +335,10 @@ public void testResultSetLoggerCachingBehavior() { BigQueryJdbcResultSetLogger.getLogger(BigQueryBaseResultSet.class); BigQueryJdbcResultSetLogger globalLogger2 = BigQueryJdbcResultSetLogger.getLogger(BigQueryBaseResultSet.class); + BigQueryJdbcResultSetLogger emptyIdLogger = + BigQueryJdbcResultSetLogger.getLogger(BigQueryBaseResultSet.class, ""); assertSame(globalLogger1, globalLogger2); + assertSame(globalLogger1, emptyIdLogger); // Connection-scoped facades are distinct lightweight objects, but they all forward to the // single registered logger for the class — that is what prevents the registration leak. BigQueryJdbcResultSetLogger connLogger1 = @@ -360,14 +363,17 @@ public void testResultSetLoggerParameterPreservation() { LogRecord record = new LogRecord(Level.INFO, "Processing job {0}"); record.setParameters(new Object[] {"job-999"}); rsLogger.log(record); + // Calling log() again with an already-stamped record must be idempotent. + rsLogger.log(record); List records = rsHandler.getRecords(); - assertEquals(1, records.size()); - LogRecord loggedRecord = records.get(0); + assertEquals(2, records.size()); + LogRecord loggedRecord = records.get(1); Object[] params = loggedRecord.getParameters(); assertEquals(2, params.length); - assertEquals("conn-123", params[0]); + assertTrue(params[0] instanceof ConnectionLogContext); + assertEquals("conn-123", ((ConnectionLogContext) params[0]).connectionId()); assertEquals("job-999", params[1]); } finally { rsLogger.delegate().removeHandler(rsHandler); @@ -394,7 +400,9 @@ private void assertConnectionIdPropagated( capturedLogs.clear(); action.run(); assertEquals(1, capturedLogs.size()); - assertEquals(connectionId, capturedLogs.get(0).getParameters()[0]); + Object firstParam = capturedLogs.get(0).getParameters()[0]; + assertTrue(firstParam instanceof ConnectionLogContext); + assertEquals(connectionId, ((ConnectionLogContext) firstParam).connectionId()); assertTrue(assertLogContains(expectedMessage)); } finally { BigQueryJdbcRootLogger.getRootLogger().setLevel(originalRootLevel); diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandlerTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandlerTest.java index e923dd011778..12101f13ab7f 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandlerTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/OpenTelemetryJulHandlerTest.java @@ -31,6 +31,8 @@ import io.opentelemetry.sdk.logs.data.LogRecordData; import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; import java.util.List; +import java.util.logging.Level; +import java.util.logging.LogRecord; import java.util.logging.Logger; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -131,4 +133,49 @@ public void testPublishToGcp() { assertEquals("Test message", ((Payload.StringPayload) entry.getPayload()).getData()); assertEquals(com.google.cloud.logging.Severity.INFO, entry.getSeverity()); } + + @Test + public void testResultSetLoggerRoutingAndMessageFormatting() { + BigQueryJdbcOpenTelemetry.registerConnection( + "test-uuid", otelTesting.getOpenTelemetry(), null, false); + BigQueryJdbcMdc.clear(); + + BigQueryJdbcResultSetLogger rsLogger = + BigQueryJdbcResultSetLogger.getLogger(BigQueryBaseResultSet.class, "test-uuid"); + rsLogger.fineTrace("getObject", "Column 'user_id' has unparsed {json}"); + + LogRecord parameterized = new LogRecord(Level.INFO, "Processing job {0}"); + parameterized.setParameters(new Object[] {"job-999"}); + rsLogger.log(parameterized); + + // Non-ResultSet record with plain String param and no positional placeholder keeps quotes + Baggage baggage = + Baggage.builder() + .put(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY, "test-uuid") + .build(); + try (Scope scope = baggage.makeCurrent()) { + LogRecord retryStyle = new LogRecord(Level.INFO, "Retrying 'getQueryResults'"); + retryStyle.setParameters(new Object[] {"attempt=1"}); + logger.log(retryStyle); + + LogRecord directPositional = new LogRecord(Level.INFO, "Direct {0} ok"); + directPositional.setParameters(new Object[] {"param0"}); + logger.log(directPositional); + + LogRecord malformedPattern = new LogRecord(Level.INFO, "Broken {0"); + malformedPattern.setParameters(new Object[] {"param0"}); + logger.log(malformedPattern); + + LogRecord nullMessage = new LogRecord(Level.INFO, null); + logger.log(nullMessage); + } + + List logs = otelTesting.getLogRecords(); + assertEquals(6, logs.size()); + assertEquals("Column 'user_id' has unparsed {json}", logs.get(0).getBodyValue().asString()); + assertEquals("Processing job job-999", logs.get(1).getBodyValue().asString()); + assertEquals("Retrying 'getQueryResults'", logs.get(2).getBodyValue().asString()); + assertEquals("Direct param0 ok", logs.get(3).getBodyValue().asString()); + assertEquals("Broken {0", logs.get(4).getBodyValue().asString()); + } } diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java index cbc4dec030c8..c0362daaa677 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java @@ -16,6 +16,8 @@ package com.google.cloud.bigquery.jdbc; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -24,19 +26,37 @@ import com.google.cloud.bigquery.Field; import com.google.cloud.bigquery.FieldList; +import com.google.cloud.bigquery.Schema; import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.jdbc.utils.TestUtilities; +import com.google.cloud.bigquery.jdbc.utils.URIBuilder; import java.io.IOException; +import java.nio.channels.SeekableByteChannel; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.PosixFilePermissions; import java.sql.SQLException; import java.sql.Statement; +import java.util.List; import java.util.Optional; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; +import java.util.logging.Logger; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; @@ -45,13 +65,11 @@ public class PerConnectionFileHandlerTest { @TempDir Path tempDir; private PerConnectionFileHandler handler; - private BigQueryConnection mockConnection; private Level originalLevel; @BeforeEach public void setUp() { handler = new PerConnectionFileHandler(tempDir.toString(), Level.INFO); - mockConnection = Mockito.mock(BigQueryConnection.class); BigQueryJdbcMdc.clear(); originalLevel = BigQueryJdbcRootLogger.getRootLogger().getLevel(); BigQueryJdbcRootLogger.getRootLogger().setLevel(Level.ALL); @@ -66,17 +84,23 @@ public void tearDown() { BigQueryJdbcRootLogger.getRootLogger().setLevel(originalLevel); } + private Optional findLogFile(Path dir, String suffix) throws IOException { + try (Stream files = Files.list(dir)) { + return files + .filter( + p -> + p.getFileName().toString().startsWith("BQ-JDBC-") + && p.getFileName().toString().endsWith(suffix)) + .findFirst(); + } + } + private Optional findLogFile(String suffix) throws IOException { - return Files.list(tempDir) - .filter( - p -> - p.getFileName().toString().startsWith("BQ-JDBC-") - && p.getFileName().toString().endsWith(suffix)) - .findFirst(); + return findLogFile(tempDir, suffix); } @Test - public void testInitialization() throws IOException { + public void testInitialization() { Path defaultLog = tempDir.resolve("BQ-JDBC-GLOBAL.log"); assertTrue(Files.exists(defaultLog)); } @@ -89,12 +113,13 @@ public void testPublishDefault() throws IOException { Path defaultLog = tempDir.resolve("BQ-JDBC-GLOBAL.log"); assertTrue(Files.exists(defaultLog)); - String content = new String(Files.readAllBytes(defaultLog)); + String content = new String(Files.readAllBytes(defaultLog), StandardCharsets.UTF_8); assertTrue(content.contains("Test message default")); } @Test public void testPublishConnectionSpecific() throws IOException { + handler.registerConnection("c123"); BigQueryJdbcMdc.registerInstance("c123"); LogRecord record = new LogRecord(Level.INFO, "Test message connection 123"); @@ -107,6 +132,7 @@ public void testPublishConnectionSpecific() throws IOException { @Test public void testCloseHandler() throws IOException { + handler.registerConnection("c123"); BigQueryJdbcMdc.registerInstance("c123"); LogRecord record = new LogRecord(Level.INFO, "Test message connection 123"); @@ -131,7 +157,8 @@ public void testCloseHandler() throws IOException { @Test public void testProxyExceptionLogRouting() throws Exception { // Register the temp file handler to the root logger so proxy logs are routed to it - java.util.logging.Logger rootLogger = BigQueryJdbcRootLogger.getRootLogger(); + Logger rootLogger = BigQueryJdbcRootLogger.getRootLogger(); + handler.registerConnection("c456"); rootLogger.addHandler(handler); try { @@ -161,7 +188,7 @@ public void testProxyExceptionLogRouting() throws Exception { Optional connLog = findLogFile("-c456.log"); assertTrue(connLog.isPresent()); - String content = new String(Files.readAllBytes(connLog.get())); + String content = new String(Files.readAllBytes(connLog.get()), StandardCharsets.UTF_8); assertTrue(content.contains("Database error")); assertTrue(content.contains("Exception occurred during executeQuery")); @@ -182,7 +209,8 @@ public void testProxyExceptionLogRouting() throws Exception { @Test public void testResultSetExceptionLogRouting() throws Exception { // Register the temp file handler to the root logger so ResultSet logs are captured - java.util.logging.Logger rootLogger = BigQueryJdbcRootLogger.getRootLogger(); + Logger rootLogger = BigQueryJdbcRootLogger.getRootLogger(); + handler.registerConnection("c789"); rootLogger.addHandler(handler); try { @@ -196,7 +224,7 @@ public void testResultSetExceptionLogRouting() throws Exception { // Create a mock FieldList and schema for the ResultSet FieldList fields = FieldList.of(Field.of("col", StandardSQLTypeName.STRING)); - com.google.cloud.bigquery.Schema schema = com.google.cloud.bigquery.Schema.of(fields); + Schema schema = Schema.of(fields); // Instantiate a real BigQueryJsonResultSet (which extends BigQueryBaseResultSet) // passing the mock statement carrying connectionId "c789" @@ -213,7 +241,7 @@ public void testResultSetExceptionLogRouting() throws Exception { Optional connLog = findLogFile("-c789.log"); assertTrue(connLog.isPresent()); - String content = new String(Files.readAllBytes(connLog.get())); + String content = new String(Files.readAllBytes(connLog.get()), StandardCharsets.UTF_8); assertTrue(content.contains("Column label cannot be null")); } finally { @@ -221,4 +249,320 @@ public void testResultSetExceptionLogRouting() throws Exception { rootLogger.removeHandler(handler); } } + + @Test + @EnabledOnOs({OS.LINUX, OS.MAC}) + public void testPosixOwnerOnlyPermissionsOnCreatedDirsAndFiles() throws IOException { + Path nestedDir = tempDir.resolve("parent_logs/child_logs"); + PerConnectionFileHandler posixHandler = + new PerConnectionFileHandler(nestedDir.toString(), Level.FINEST); + try { + posixHandler.registerConnection("p999"); + try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance("p999")) { + posixHandler.publish(new LogRecord(Level.INFO, "Secret query")); + } + posixHandler.flush(); + + assertEquals( + "rwx------", + PosixFilePermissions.toString(Files.getPosixFilePermissions(nestedDir.getParent()))); + assertEquals( + "rwx------", PosixFilePermissions.toString(Files.getPosixFilePermissions(nestedDir))); + assertEquals( + "rw-------", + PosixFilePermissions.toString( + Files.getPosixFilePermissions(nestedDir.resolve("BQ-JDBC-GLOBAL.log")))); + + Optional connLog = findLogFile(nestedDir, "-p999.log"); + assertTrue(connLog.isPresent()); + assertEquals( + "rw-------", PosixFilePermissions.toString(Files.getPosixFilePermissions(connLog.get()))); + } finally { + posixHandler.close(); + } + } + + @Test + @EnabledOnOs({OS.LINUX, OS.MAC}) + public void testExistingSharedDirectoryAndPermissiveFileHandling() throws IOException { + Path sharedDir = Files.createDirectory(tempDir.resolve("shared_dir")); + Files.setPosixFilePermissions(sharedDir, PosixFilePermissions.fromString("rwxr-xr-x")); + + // Pre-existing permissive (0644) regular global log file owned by current user gets tightened + Path existingGlobal = Files.createFile(sharedDir.resolve("BQ-JDBC-GLOBAL.log")); + Files.setPosixFilePermissions(existingGlobal, PosixFilePermissions.fromString("rw-r--r--")); + + PerConnectionFileHandler sharedHandler = + new PerConnectionFileHandler(sharedDir.toString(), Level.INFO); + try { + assertEquals( + "rwxr-xr-x", PosixFilePermissions.toString(Files.getPosixFilePermissions(sharedDir))); + assertEquals( + "rw-------", + PosixFilePermissions.toString(Files.getPosixFilePermissions(existingGlobal))); + } finally { + sharedHandler.close(); + } + + // System /tmp root path (symlink on macOS, real dir on Linux) is accepted without altering mode + assertNotNull(PerConnectionFileHandler.ensureSecureDirectory(Paths.get("/tmp"))); + } + + @Test + @EnabledOnOs({OS.LINUX, OS.MAC}) + public void testSymbolicLinksAndNonRegularPathsAreRejected() throws IOException { + Path targetFile = Files.write(tempDir.resolve("target.txt"), "keep".getBytes()); + Path fileSymlink = tempDir.resolve("symlink.log"); + Files.createSymbolicLink(fileSymlink, targetFile); + + assertThrows( + IOException.class, () -> PerConnectionFileHandler.openSecureAppendChannel(fileSymlink)); + assertEquals("keep", new String(Files.readAllBytes(targetFile), StandardCharsets.UTF_8)); + + // Directory passed where regular log file is expected must be rejected + Path subDir = Files.createDirectory(tempDir.resolve("not_a_regular_file")); + assertThrows(IOException.class, () -> PerConnectionFileHandler.openSecureAppendChannel(subDir)); + + // Regular file passed where log directory is expected must be rejected + assertThrows( + IOException.class, () -> PerConnectionFileHandler.ensureSecureDirectory(targetFile)); + + // Symlinked directory and nested path beneath a symlinked directory must both be rejected + Path realTargetDir = Files.createDirectory(tempDir.resolve("real_target_dir")); + Path dirSymlink = tempDir.resolve("dir_symlink"); + Files.createSymbolicLink(dirSymlink, realTargetDir); + assertThrows( + IOException.class, () -> PerConnectionFileHandler.ensureSecureDirectory(dirSymlink)); + assertThrows( + IOException.class, + () -> PerConnectionFileHandler.ensureSecureDirectory(dirSymlink.resolve("nested_child"))); + assertFalse(Files.exists(realTargetDir.resolve("nested_child"))); + + // Existing file Channel append works cleanly for current user + Path ownedFile = tempDir.resolve("owned.log"); + try (SeekableByteChannel ch = PerConnectionFileHandler.openSecureAppendChannel(ownedFile)) { + assertNotNull(ch); + } + } + + @Test + public void testPercentTokensAndSystemPropertyMaxBytes() throws IOException { + Path literalPercentDir = tempDir.resolve("dir_%t_%h_%g_%u"); + PerConnectionFileHandler percentHandler = + new PerConnectionFileHandler(literalPercentDir.toString(), Level.INFO); + try { + percentHandler.publish(new LogRecord(Level.INFO, "Literal percent dir log")); + percentHandler.flush(); + + Path globalLog = literalPercentDir.resolve("BQ-JDBC-GLOBAL.log"); + assertTrue(Files.exists(globalLog)); + String content = new String(Files.readAllBytes(globalLog), StandardCharsets.UTF_8); + assertTrue(content.contains("Literal percent dir log")); + } finally { + percentHandler.close(); + } + + String prop = PerConnectionFileHandler.MAX_LOG_FILE_BYTES_SYSTEM_PROPERTY; + String prev = System.getProperty(prop); + try { + System.setProperty(prop, "4096"); + assertEquals(4096, PerConnectionFileHandler.resolveMaxLogFileBytes()); + System.setProperty(prop, "-5"); + assertEquals( + PerConnectionFileHandler.DEFAULT_MAX_LOG_FILE_BYTES, + PerConnectionFileHandler.resolveMaxLogFileBytes()); + } finally { + if (prev == null) { + System.clearProperty(prop); + } else { + System.setProperty(prop, prev); + } + } + } + + @Test + public void testBoundedLogFileSizeAndSameSecondCollisionIsolation() throws IOException { + Path boundedDir = tempDir.resolve("bounded"); + int maxBytes = 1024; + PerConnectionFileHandler boundedHandler = + new PerConnectionFileHandler(boundedDir.toString(), Level.INFO, maxBytes); + try { + for (int i = 0; i < 40; i++) { + boundedHandler.publish( + new LogRecord(Level.INFO, "Padding log line to exceed 1 KiB limit #" + i)); + } + // Two connections sharing the same 4-char prefix in the same second get distinct files + boundedHandler.registerConnection("abcd-1111"); + boundedHandler.registerConnection("abcd-2222"); + try (BigQueryJdbcMdc.MdcCloseable m1 = BigQueryJdbcMdc.registerInstance("abcd-1111")) { + boundedHandler.publish(new LogRecord(Level.INFO, "First abcd connection")); + } + try (BigQueryJdbcMdc.MdcCloseable m2 = BigQueryJdbcMdc.registerInstance("abcd-2222")) { + boundedHandler.publish(new LogRecord(Level.INFO, "Second abcd connection")); + } + boundedHandler.flush(); + } finally { + boundedHandler.close(); + } + + // Post-close registerConnection and publish are safe no-ops + boundedHandler.registerConnection("after-close"); + try (BigQueryJdbcMdc.MdcCloseable m3 = BigQueryJdbcMdc.registerInstance("after-close")) { + boundedHandler.publish(new LogRecord(Level.INFO, "Ignored after handler close")); + } + + try (Stream stream = Files.list(boundedDir)) { + List files = stream.collect(Collectors.toList()); + assertTrue(files.stream().noneMatch(p -> p.getFileName().toString().endsWith(".lck"))); + assertEquals( + 2L, files.stream().filter(p -> p.getFileName().toString().endsWith("-abcd.log")).count()); + Path globalLog = boundedDir.resolve("BQ-JDBC-GLOBAL.log"); + assertTrue(Files.size(globalLog) <= maxBytes + 512); + } + } + + @Test + public void testConcurrentCloseAndPublishLeavesNoOpenConnectionFiles() throws Exception { + Path raceDir = tempDir.resolve("race"); + for (int round = 0; round < 15; round++) { + PerConnectionFileHandler raceHandler = + new PerConnectionFileHandler(raceDir.resolve("r" + round).toString(), Level.INFO); + String connId = "race-" + round; + raceHandler.registerConnection(connId); + CountDownLatch start = new CountDownLatch(1); + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future pubTask = + pool.submit( + () -> { + try (BigQueryJdbcMdc.MdcCloseable m = BigQueryJdbcMdc.registerInstance(connId)) { + start.await(); + for (int i = 0; i < 10; i++) { + raceHandler.publish(new LogRecord(Level.INFO, "race msg " + i)); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }); + Future closeTask = + pool.submit( + () -> { + try { + start.await(); + raceHandler.close(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }); + start.countDown(); + pubTask.get(5, TimeUnit.SECONDS); + closeTask.get(5, TimeUnit.SECONDS); + } finally { + pool.shutdownNow(); + } + // After close(), publishing again with the same connId must not create a new file + Path roundDir = raceDir.resolve("r" + round); + try (Stream before = Files.list(roundDir)) { + long countAfterClose = before.count(); + try (BigQueryJdbcMdc.MdcCloseable m = BigQueryJdbcMdc.registerInstance(connId)) { + raceHandler.publish(new LogRecord(Level.INFO, "post-close")); + } + try (Stream after = Files.list(roundDir)) { + assertEquals(countAfterClose, after.count()); + } + } + } + } + + @Test + public void testPostCloseResultSetAndStringParamsDoNotResurrectHandlers() throws Exception { + Path leakDir = tempDir.resolve("no_leak"); + PerConnectionFileHandler finestHandler = + new PerConnectionFileHandler(leakDir.toString(), Level.FINEST); + Logger rootLogger = BigQueryJdbcRootLogger.getRootLogger(); + rootLogger.addHandler(finestHandler); + + try { + String connId = "dead-conn-uuid"; + ConnectionLogContext activeCtx = BigQueryJdbcRootLogger.registerConnection(connId); + assertNotNull(activeCtx); + assertEquals(ConnectionLogContext.ALL_VALUE, activeCtx.levelValue()); + finestHandler.registerConnection(connId); + + // Construct ResultSet and child Struct logger WHILE connection is active (covers active != + // null) + BigQueryStatement mockStmt = mock(BigQueryStatement.class); + mockStmt.connectionId = connId; + Schema schema = Schema.of(FieldList.of(Field.of("col", StandardSQLTypeName.INT64))); + BigQueryJsonResultSet rs = + BigQueryJsonResultSet.of(schema, 0, null, mockStmt, new Future[0]); + BigQueryJdbcResultSetLogger activeStructLogger = rs.LOG.getArrowStructLogger(); + + try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(connId)) { + finestHandler.publish(new LogRecord(Level.INFO, "Before close")); + } + finestHandler.flush(); + Optional initialLog = findLogFile(leakDir, "-dead.log"); + assertTrue(initialLog.isPresent()); + + rs.close(); + BigQueryJdbcRootLogger.closeConnectionHandler(connId); + finestHandler.closeHandler(connId); + assertEquals(ConnectionLogContext.OFF_VALUE, activeCtx.levelValue()); + Files.delete(initialLog.get()); + + // 1. Post-close ResultSet getter and exception log on ResultSet created while active + assertThrows(SQLException.class, () -> rs.getShort(1)); + assertThrows(SQLException.class, () -> rs.findColumn(null)); + + // 2. Pre-existing detached Struct logger AND newly obtained post-close logger (active == + // null) + activeStructLogger.finestTrace("getAttributes"); + BigQueryJdbcResultSetLogger.getLogger(BigQueryArrowStruct.class, connId) + .finestTrace("getAttributes"); + + // 3. Third-party retry logger passing plain String in params[0] with empty MDC + BigQueryJdbcMdc.clear(); + LogRecord retryRecord = new LogRecord(Level.FINEST, "Retrying RPC"); + retryRecord.setParameters(new Object[] {"BigQuery retried method: getQueryResults"}); + finestHandler.publish(retryRecord); + finestHandler.flush(); + + assertFalse(findLogFile(leakDir, "-dead.log").isPresent()); + assertFalse(findLogFile(leakDir, "-BigQ.log").isPresent()); + + Formatter formatter = BigQueryJdbcRootLogger.getFormatter(); + String formattedRetry = formatter.format(retryRecord); + assertTrue(formattedRetry.contains("[NO_CONN]")); + assertFalse(formattedRetry.contains("[BigQuery retried method:")); + } finally { + rootLogger.removeHandler(finestHandler); + finestHandler.close(); + } + } + + @Test + public void testFailedConnectionConstructorCleansUpActiveRegistration() throws IOException { + Path failLogDir = tempDir.resolve("failed_conn_logs"); + int baselineActive = BigQueryJdbcRootLogger.activeConnectionCount(); + String invalidAuthUrl = + new URIBuilder(TestUtilities.getBaseConnectionUrl()) + .append("ProjectId", "test-proj") + .append("OAuthType", 99) + .toString(); + + BigQueryJdbcRootLogger.setLevel(Level.CONFIG, failLogDir.toString()); + try { + assertThrows(RuntimeException.class, () -> new BigQueryConnection(invalidAuthUrl)); + assertEquals(baselineActive, BigQueryJdbcRootLogger.activeConnectionCount()); + // Constructor logged CONFIG properties before failing, then closed/unregistered its slot + try (Stream files = Files.list(failLogDir)) { + List created = files.collect(Collectors.toList()); + assertTrue(created.stream().anyMatch(p -> !p.endsWith("BQ-JDBC-GLOBAL.log"))); + assertTrue(created.stream().noneMatch(p -> p.getFileName().toString().endsWith(".lck"))); + } + } finally { + BigQueryJdbcRootLogger.setLevel(Level.OFF, null); + } + } }