-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use rocksdb_wrapper::get to reimplement incr (#651)
- Loading branch information
Showing
9 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
#include "rocksdb_wrapper.h" | ||
|
||
#include <rocksdb/db.h> | ||
#include "pegasus_write_service_impl.h" | ||
|
||
namespace pegasus { | ||
namespace server { | ||
|
||
const int FAIL_DB_GET = -104; | ||
|
||
rocksdb_wrapper::rocksdb_wrapper(pegasus_server_impl *server) | ||
: replica_base(server), | ||
_db(server->_db), | ||
_rd_opts(server->_data_cf_rd_opts), | ||
_pegasus_data_version(server->_pegasus_data_version), | ||
_pfc_recent_expire_count(server->_pfc_recent_expire_count) | ||
{ | ||
} | ||
|
||
int rocksdb_wrapper::get(dsn::string_view raw_key, /*out*/ db_get_context *ctx) | ||
{ | ||
FAIL_POINT_INJECT_F("db_get", [](dsn::string_view) -> int { return FAIL_DB_GET; }); | ||
|
||
rocksdb::Status s = _db->Get(_rd_opts, utils::to_rocksdb_slice(raw_key), &(ctx->raw_value)); | ||
if (dsn_likely(s.ok())) { | ||
// success | ||
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; | ||
_pfc_recent_expire_count->increment(); | ||
} | ||
return rocksdb::Status::kOk; | ||
} else if (s.IsNotFound()) { | ||
// NotFound is an acceptable error | ||
ctx->found = false; | ||
return rocksdb::Status::kOk; | ||
} | ||
|
||
dsn::blob hash_key, sort_key; | ||
pegasus_restore_key(dsn::blob(raw_key.data(), 0, raw_key.size()), hash_key, sort_key); | ||
derror_rocksdb("Get", | ||
s.ToString(), | ||
"hash_key: {}, sort_key: {}", | ||
utils::c_escape_string(hash_key), | ||
utils::c_escape_string(sort_key)); | ||
return s.code(); | ||
} | ||
|
||
} // namespace server | ||
} // namespace pegasus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <dsn/dist/replication/replica_base.h> | ||
|
||
namespace rocksdb { | ||
class DB; | ||
class ReadOptions; | ||
} // namespace rocksdb | ||
|
||
namespace dsn { | ||
class perf_counter_wrapper; | ||
} // namespace dsn | ||
|
||
namespace pegasus { | ||
namespace server { | ||
struct db_get_context; | ||
class pegasus_server_impl; | ||
|
||
class rocksdb_wrapper : public dsn::replication::replica_base | ||
{ | ||
public: | ||
rocksdb_wrapper(pegasus_server_impl *server); | ||
|
||
/// Calls RocksDB Get and store the result into `db_get_context`. | ||
/// \returns 0 if Get succeeded. On failure, a non-zero rocksdb status code is returned. | ||
/// \result ctx.expired=true if record expired. Still 0 is returned. | ||
/// \result ctx.found=false if record is not found. Still 0 is returned. | ||
int get(dsn::string_view raw_key, /*out*/ db_get_context *ctx); | ||
|
||
private: | ||
rocksdb::DB *_db; | ||
rocksdb::ReadOptions &_rd_opts; | ||
const uint32_t _pegasus_data_version; | ||
dsn::perf_counter_wrapper &_pfc_recent_expire_count; | ||
}; | ||
} // namespace server | ||
} // namespace pegasus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
#include "server/pegasus_server_write.h" | ||
#include "server/pegasus_write_service_impl.h" | ||
#include "pegasus_server_test_base.h" | ||
|
||
namespace pegasus { | ||
namespace server { | ||
class rocksdb_wrapper_test : public pegasus_server_test_base | ||
{ | ||
protected: | ||
std::unique_ptr<pegasus_server_write> _server_write; | ||
pegasus_write_service::impl *_write_impl{nullptr}; | ||
rocksdb_wrapper *_rocksdb_wrapper{nullptr}; | ||
dsn::blob _raw_key; | ||
|
||
public: | ||
void SetUp() override | ||
{ | ||
start(); | ||
_server_write = dsn::make_unique<pegasus_server_write>(_server.get(), true); | ||
_write_impl = _server_write->_write_svc->_impl.get(); | ||
_rocksdb_wrapper = _write_impl->_rocksdb_wrapper.get(); | ||
|
||
pegasus::pegasus_generate_key( | ||
_raw_key, dsn::string_view("hash_key"), dsn::string_view("sort_key")); | ||
} | ||
|
||
void single_set(dsn::blob raw_key, dsn::blob user_value, int32_t expire_ts_seconds) | ||
{ | ||
dsn::apps::update_request put; | ||
put.key = raw_key; | ||
put.value = user_value; | ||
put.expire_ts_seconds = expire_ts_seconds; | ||
db_write_context write_ctx; | ||
dsn::apps::update_response put_resp; | ||
_write_impl->batch_put(write_ctx, put, put_resp); | ||
ASSERT_EQ(_write_impl->batch_commit(0), 0); | ||
} | ||
}; | ||
|
||
TEST_F(rocksdb_wrapper_test, get) | ||
{ | ||
// not found | ||
db_get_context get_ctx1; | ||
_rocksdb_wrapper->get(_raw_key, &get_ctx1); | ||
ASSERT_FALSE(get_ctx1.found); | ||
|
||
// expired | ||
int32_t expired_ts = utils::epoch_now(); | ||
db_get_context get_ctx2; | ||
single_set(_raw_key, dsn::blob::create_from_bytes("abc"), expired_ts); | ||
_rocksdb_wrapper->get(_raw_key, &get_ctx2); | ||
ASSERT_TRUE(get_ctx2.found); | ||
ASSERT_TRUE(get_ctx2.expired); | ||
ASSERT_EQ(get_ctx2.expire_ts, expired_ts); | ||
|
||
// found | ||
expired_ts = INT32_MAX; | ||
db_get_context get_ctx3; | ||
single_set(_raw_key, dsn::blob::create_from_bytes("abc"), expired_ts); | ||
_rocksdb_wrapper->get(_raw_key, &get_ctx3); | ||
ASSERT_TRUE(get_ctx2.found); | ||
ASSERT_FALSE(get_ctx3.expired); | ||
ASSERT_EQ(get_ctx3.expire_ts, expired_ts); | ||
} | ||
} // namespace server | ||
} // namespace pegasus |