rocksdb/java/rocksjni/stderr_logger.cc

86 lines
3.1 KiB
C++
Raw Normal View History

Add native logger support to RocksJava (#12213) Summary: ## Overview In this PR, we introduce support for setting the RocksDB native logger through Java. As mentioned in the discussion on the [Google Group discussion](https://groups.google.com/g/rocksdb/c/xYmbEs4sqRM/m/e73E4whJAQAJ), this work is primarily motivated by the JDK 17 [performance regression in JNI thread attach/detach calls](https://bugs.openjdk.org/browse/JDK-8314859): the only existing RocksJava logging configuration call, `setLogger`, invokes the provided logger over the JNI. ## Changes Specifically, these changes add support for the `devnull` and `stderr` native loggers. For the `stderr` logger, we add the ability to prefix every log with a `logPrefix`, so that it becomes possible know which database a particular log is coming from (if multiple databases are in use). The API looks like the following: ```java Options opts = new Options(); NativeLogger stderrNativeLogger = NativeLogger.newStderrLogger( InfoLogLevel.DEBUG_LEVEL, "[my prefix here]"); options.setLogger(stderrNativeLogger); try (final RocksDB db = RocksDB.open(options, ...)) {...} // Cleanup stderrNativeLogger.close() opts.close(); ``` Note that the API to set the logger is the same, via `Options::setLogger` (or `DBOptions::setLogger`). However, it will set the RocksDB logger to be native when the provided logger is an instance of `NativeLogger`. ## Testing Two tests have been added in `NativeLoggerTest.java`. The first test creates both the `devnull` and `stderr` loggers, and sets them on the associated `Options`. However, to avoid polluting the testing output with logs from `stderr`, only the `devnull` logger is actually used in the test. The second test does the same logic, but for `DBOptions`. It is possible to manually verify the `stderr` logger by modifying the tests slightly, and observing that the console indeed gets cluttered with logs from `stderr`. Pull Request resolved: https://github.com/facebook/rocksdb/pull/12213 Reviewed By: cbi42 Differential Revision: D52772306 Pulled By: ajkr fbshipit-source-id: 4026895f78f9cc250daf6bfa57427957e2d8b053
2024-01-18 01:51:36 +00:00
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
#include "util/stderr_logger.h"
#include <jni.h>
#include <memory>
#include "include/org_rocksdb_util_StdErrLogger.h"
#include "rocksjni/cplusplus_to_java_convert.h"
#include "rocksjni/portal.h"
/*
* Class: org_rocksdb_util_StdErrLogger
* Method: newStdErrLogger
* Signature: (BLjava/lang/String;)J
*/
jlong Java_org_rocksdb_util_StdErrLogger_newStdErrLogger(JNIEnv* env,
jclass /*jcls*/,
jbyte jlog_level,
jstring jlog_prefix) {
auto log_level = static_cast<ROCKSDB_NAMESPACE::InfoLogLevel>(jlog_level);
std::shared_ptr<ROCKSDB_NAMESPACE::StderrLogger>* sptr_logger = nullptr;
if (jlog_prefix == nullptr) {
sptr_logger = new std::shared_ptr<ROCKSDB_NAMESPACE::StderrLogger>(
new ROCKSDB_NAMESPACE::StderrLogger(log_level));
} else {
jboolean has_exception = JNI_FALSE;
auto log_prefix = ROCKSDB_NAMESPACE::JniUtil::copyStdString(
env, jlog_prefix, &has_exception); // also releases jlog_prefix
if (has_exception == JNI_TRUE) {
return 0;
}
sptr_logger = new std::shared_ptr<ROCKSDB_NAMESPACE::StderrLogger>(
new ROCKSDB_NAMESPACE::StderrLogger(log_level, log_prefix));
}
return GET_CPLUSPLUS_POINTER(sptr_logger);
}
/*
* Class: org_rocksdb_util_StdErrLogger
* Method: setInfoLogLevel
* Signature: (JB)V
*/
void Java_org_rocksdb_util_StdErrLogger_setInfoLogLevel(JNIEnv* /*env*/,
jclass /*jcls*/,
jlong jhandle,
jbyte jlog_level) {
auto* handle =
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::StderrLogger>*>(
jhandle);
handle->get()->SetInfoLogLevel(
static_cast<ROCKSDB_NAMESPACE::InfoLogLevel>(jlog_level));
}
/*
* Class: org_rocksdb_util_StdErrLogger
* Method: infoLogLevel
* Signature: (J)B
*/
jbyte Java_org_rocksdb_util_StdErrLogger_infoLogLevel(JNIEnv* /*env*/,
jclass /*jcls*/,
jlong jhandle) {
auto* handle =
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::StderrLogger>*>(
jhandle);
return static_cast<jbyte>(handle->get()->GetInfoLogLevel());
}
/*
* Class: org_rocksdb_util_StdErrLogger
* Method: disposeInternal
* Signature: (J)V
*/
void Java_org_rocksdb_util_StdErrLogger_disposeInternal(JNIEnv* /*env*/,
jobject /*jobj*/,
jlong jhandle) {
auto* handle =
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::StderrLogger>*>(
jhandle);
delete handle; // delete std::shared_ptr
}