Java: the misses in rocksdbjni are very expensive
Expected behavior
This is only for rocksdbjni.
In rocksdbjni, I would expect the misses to be less expensive than the hits, or at least as expensive.
I have tested v9.5.2, but the last v9.6.1 has no change in java/rocksjni/rocksjni.cc so the issue remains.
Actual behavior
The misses are far more expensive than the hits due to a C++ exception which is thrown then caught a few lines after.
For instance in java/rocksjni/rocksjni.cc in Java_org_rocksdb_RocksDB_get__J_3BII():
// throws a rocksdb::KVException...
ROCKSDB_NAMESPACE::KVException::ThrowOnError(
env,
db->Get(ROCKSDB_NAMESPACE::ReadOptions(), db->DefaultColumnFamily(),
key.slice(), &value.pinnable_slice()));
return value.NewByteArray();
} catch (ROCKSDB_NAMESPACE::KVException&) {
// ... which is caught a few lines after
return nullptr;
}Just changing the above code to the following speeds up a lot the misses:
const auto status =
db->Get(ROCKSDB_NAMESPACE::ReadOptions(), db->DefaultColumnFamily(),
key.slice(), &value.pinnable_slice()));
if (status.ok()) {
return value.NewByteArray();
} else if (!status.IsNotFound()) {
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
}
} catch (ROCKSDB_NAMESPACE::KVException&) {
}
return nullptr;Steps to reproduce the behavior
I have created a small reproducer in trazfr/rocksdbjni-miss-perfs which runs the two following rocksdbjni jar and send the results to the Datadog profiler:
- the vanilla rocksdb
9.5.2 - a patched version with only the change above
# needs docker-compose
./run.sh --api-key <DATADOG_API_KEY> --site <DATADOG_SITE>In this reproducer, we create a DB with 1M entries, then we perform:
- 1M hits with all the keys in the DB (method
App.readDatabaseHits()) - 1M misses (method
App.readDatabaseMisses()) - loop
I don't compare vanilla vs patched as the compilation parameters are different, but only the CPU time spent on hits vs misses in each case. The following tests are done on an AWS m5d.4xlarge machine (linux/x84_64), but I have also been able to reproduce an a M1 Mac (MacOS/aarch64 and qemu+linux/aarch64).
On the vanilla version, in case of miss, we can see that a lot of CPU time is spent in rocksdb::KVException::ThrowOnError(). This makes the misses slower than the hits by far:
With the small patch above, as we don't throw exceptions, the misses are a bit less expensive than the hits:
Source: facebook/rocksdb