From da1877c4164e26932907fec24707a8b070dc6158 Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Fri, 24 Jan 2025 16:41:02 +0800 Subject: [PATCH] refactor --- src/server/pegasus_write_service_impl.h | 5 +++-- src/server/rocksdb_wrapper.cpp | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/server/pegasus_write_service_impl.h b/src/server/pegasus_write_service_impl.h index 8a8d6d2f82..167243b7e5 100644 --- a/src/server/pegasus_write_service_impl.h +++ b/src/server/pegasus_write_service_impl.h @@ -179,7 +179,7 @@ class pegasus_write_service::impl : public dsn::replication::replica_base { // Get old value from the RocksDB instance according to the provided key. db_get_context get_ctx; - int err = _rocksdb_wrapper->get(req.key.to_string_view(), &get_ctx); + const int err = _rocksdb_wrapper->get(req.key.to_string_view(), &get_ctx); if (dsn_unlikely(err != rocksdb::Status::kOk)) { return make_error_response(err, err_resp); } @@ -254,8 +254,9 @@ class pegasus_write_service::impl : public dsn::replication::replica_base return resp.error; } + // Shouldn't fail to parse since the value must be a valid int64. CHECK(dsn::buf2int64(update.value.to_string_view(), resp.new_value), - "invalid int64 value for put incr: key={}, value={}", + "invalid int64 value for put idempotent incr: key={}, value={}", update.key, update.value); diff --git a/src/server/rocksdb_wrapper.cpp b/src/server/rocksdb_wrapper.cpp index 6f5ba8bc34..f5fb2cc527 100644 --- a/src/server/rocksdb_wrapper.cpp +++ b/src/server/rocksdb_wrapper.cpp @@ -79,20 +79,25 @@ int rocksdb_wrapper::get(std::string_view raw_key, /*out*/ db_get_context *ctx) { FAIL_POINT_INJECT_F("db_get", [](std::string_view) -> int { return FAIL_DB_GET; }); - rocksdb::Status s = + const rocksdb::Status s = _db->Get(_rd_opts, _data_cf, utils::to_rocksdb_slice(raw_key), &ctx->raw_value); if (dsn_likely(s.ok())) { - // success + // The key is found and read successfully. ctx->found = true; ctx->expire_ts = pegasus_extract_expire_ts(_pegasus_data_version, ctx->raw_value); if (check_if_ts_expired(utils::epoch_now(), ctx->expire_ts)) { ctx->expired = true; METRIC_VAR_INCREMENT(read_expired_values); + } else { + ctx->expired = false; } return rocksdb::Status::kOk; - } else if (s.IsNotFound()) { - // NotFound is an acceptable error + } + + if (s.IsNotFound()) { + // NotFound is considered normal since the key may not be present in DB now. ctx->found = false; + ctx->expired = false; return rocksdb::Status::kOk; }