Skip to content

Commit

Permalink
add GetRequestFromReplica interface (#133)
Browse files Browse the repository at this point in the history
* add GetRequestFromReplica interface

* fix test case failed

---------

Co-authored-by: JunchaoChen <[email protected]>
  • Loading branch information
cjcchen and JunchaoChen authored Jan 29, 2024
1 parent 9c6a829 commit 7d47883
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
34 changes: 34 additions & 0 deletions interface/common/resdb_txn_accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,40 @@ ResDBTxnAccessor::GetTxn(uint64_t min_seq, uint64_t max_seq) {
return txn_resp;
}

absl::StatusOr<std::vector<Request>> ResDBTxnAccessor::GetRequestFromReplica(
uint64_t min_seq, uint64_t max_seq, const ReplicaInfo& replica) {
QueryRequest request;
request.set_min_seq(min_seq);
request.set_max_seq(max_seq);

std::unique_ptr<NetChannel> client =
GetNetChannel(replica.ip(), replica.port());

std::string response_str;
int ret = client->SendRequest(request, Request::TYPE_QUERY);
if (ret) {
return absl::InternalError("send data fail.");
}
client->SetRecvTimeout(1000);
ret = client->RecvRawMessageStr(&response_str);
if (ret) {
return absl::InternalError("recv data fail.");
}

QueryResponse resp;

if (!resp.ParseFromString(response_str)) {
LOG(ERROR) << "parse fail len:" << response_str.size();
return absl::InternalError("recv data fail.");
}

std::vector<Request> txn_resp;
for (auto& transaction : resp.transactions()) {
txn_resp.push_back(transaction);
}
return txn_resp;
}

absl::StatusOr<uint64_t> ResDBTxnAccessor::GetBlockNumbers() {
QueryRequest request;
request.set_min_seq(0);
Expand Down
2 changes: 2 additions & 0 deletions interface/common/resdb_txn_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ResDBTxnAccessor {
virtual absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> GetTxn(
uint64_t min_seq, uint64_t max_seq);

virtual absl::StatusOr<std::vector<Request>> GetRequestFromReplica(
uint64_t min_seq, uint64_t max_seq, const ReplicaInfo& replica);
virtual absl::StatusOr<uint64_t> GetBlockNumbers();

protected:
Expand Down
5 changes: 3 additions & 2 deletions interface/common/resdb_txn_accessor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace {

using ::resdb::testing::EqualsProto;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::ElementsAre;
using ::testing::Invoke;
using ::testing::Pointee;
Expand Down Expand Up @@ -61,9 +62,9 @@ TEST(ResDBTxnAccessorTest, GetTransactionsFail) {
auto client = std::make_unique<MockNetChannel>(ip, port);
EXPECT_CALL(*client,
SendRequest(EqualsProto(request), Request::TYPE_QUERY, _))
.WillOnce(Return(0));
.Times(AtLeast(1)).WillRepeatedly(Return(0));
EXPECT_CALL(*client, RecvRawMessageStr)
.WillOnce(Invoke([&](std::string* resp) { return -1; }));
.Times(AtLeast(1)).WillRepeatedly(Invoke([&](std::string* resp) { return -1; }));
return client;
}));
absl::StatusOr<std::vector<std::pair<uint64_t, std::string>>> resp =
Expand Down

0 comments on commit 7d47883

Please sign in to comment.