Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support backup request policy #2734

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/brpc/backup_request_policy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.


#ifndef BRPC_BACKUP_REQUEST_POLICY_H
#define BRPC_BACKUP_REQUEST_POLICY_H

#include "brpc/controller.h"

namespace brpc {

class BackupRequestPolicy {
public:
virtual ~BackupRequestPolicy() = default;

// Return the time in milliseconds in which another request
// will be sent if RPC does not finish.
virtual int32_t GetBackupRequestMs() const = 0;
chenBright marked this conversation as resolved.
Show resolved Hide resolved

// Return true if the backup request should be sent.
virtual bool DoBackup(const Controller* controller) const = 0;
};
chenBright marked this conversation as resolved.
Show resolved Hide resolved

}

#endif // BRPC_BACKUP_REQUEST_POLICY_H
6 changes: 5 additions & 1 deletion src/brpc/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ChannelOptions::ChannelOptions()
, log_succeed_without_server(true)
, use_rdma(false)
, auth(NULL)
, backup_request_policy(NULL)
, retry_policy(NULL)
, ns_filter(NULL)
{}
Expand Down Expand Up @@ -495,8 +496,10 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
// overriding connect_timeout_ms does not make sense, just use the
// one in ChannelOptions
cntl->_connect_timeout_ms = _options.connect_timeout_ms;
if (cntl->backup_request_ms() == UNSET_MAGIC_NUM) {
if (cntl->backup_request_ms() == UNSET_MAGIC_NUM &&
NULL == cntl->_backup_request_policy) {
cntl->set_backup_request_ms(_options.backup_request_ms);
cntl->_backup_request_policy = _options.backup_request_policy;
}
chenBright marked this conversation as resolved.
Show resolved Hide resolved
if (cntl->connection_type() == CONNECTION_TYPE_UNKNOWN) {
cntl->set_connection_type(_options.connection_type);
Expand Down Expand Up @@ -536,6 +539,7 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
// Currently we cannot handle retry and backup request correctly
cntl->set_max_retry(0);
cntl->set_backup_request_ms(-1);
cntl->_backup_request_policy = NULL;
}

if (cntl->backup_request_ms() >= 0 &&
Expand Down
14 changes: 12 additions & 2 deletions src/brpc/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "brpc/controller.h" // brpc::Controller
#include "brpc/details/profiler_linker.h"
#include "brpc/retry_policy.h"
#include "brpc/backup_request_policy.h"
#include "brpc/naming_service_filter.h"

namespace brpc {
Expand All @@ -55,11 +56,12 @@ struct ChannelOptions {
int32_t timeout_ms;

// Send another request if RPC does not finish after so many milliseconds.
// Overridable by Controller.set_backup_request_ms().
// Overridable by Controller.set_backup_request_ms() or
// Controller.set_backup_request_policy().
// The request will be sent to a different server by best effort.
// If timeout_ms is set and backup_request_ms >= timeout_ms, backup request
// will never be sent.
// backup request does NOT imply server-side cancelation.
// backup request does NOT imply server-side cancellation.
// Default: -1 (disabled)
// Maximum: 0x7fffffff (roughly 30 days)
int32_t backup_request_ms;
Expand Down Expand Up @@ -112,6 +114,14 @@ struct ChannelOptions {
// Default: NULL
const Authenticator* auth;

// Customize the backup request time and whether to send backup request.
// Priority: `backup_request_policy' > `backup_request_ms'.
// Overridable by Controller.set_backup_request_ms() or
// Controller.set_backup_request_policy().
// This object is NOT owned by channel and should remain valid when channel is used.
// Default: NULL
const BackupRequestPolicy* backup_request_policy;

// Customize the error code that should be retried. The interface is
// defined in src/brpc/retry_policy.h
// This object is NOT owned by channel and should remain valid when
Expand Down
18 changes: 18 additions & 0 deletions src/brpc/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ void Controller::ResetPods() {
_connection_type = CONNECTION_TYPE_UNKNOWN;
_timeout_ms = UNSET_MAGIC_NUM;
_backup_request_ms = UNSET_MAGIC_NUM;
_backup_request_policy = NULL;
_connect_timeout_ms = UNSET_MAGIC_NUM;
_real_timeout_ms = UNSET_MAGIC_NUM;
_deadline_us = -1;
Expand Down Expand Up @@ -344,6 +345,16 @@ void Controller::set_backup_request_ms(int64_t timeout_ms) {
}
}

int64_t Controller::backup_request_ms() const {
int timeout_ms = NULL != _backup_request_policy ?
_backup_request_policy->GetBackupRequestMs() : _backup_request_ms;
if (timeout_ms > 0x7fffffff) {
timeout_ms = 0x7fffffff;
LOG(WARNING) << "backup_request_ms is limited to 0x7fffffff (roughly 24 days)";
}
return timeout_ms;
}
chenBright marked this conversation as resolved.
Show resolved Hide resolved

void Controller::set_max_retry(int max_retry) {
if (max_retry > MAX_RETRY_COUNT) {
LOG(WARNING) << "Retry count can't be larger than "
Expand Down Expand Up @@ -1259,6 +1270,11 @@ int Controller::HandleSocketFailed(bthread_id_t id, void* data, int error_code,
cntl->timeout_ms(),
butil::endpoint2str(cntl->remote_side()).c_str());
} else if (error_code == EBACKUPREQUEST) {
const BackupRequestPolicy* policy = cntl->_backup_request_policy;
if (NULL != policy && !policy->DoBackup(cntl)) {
// No need to do backup request.
return bthread_id_unlock(id);
}
cntl->SetFailed(error_code, "Reached backup timeout=%" PRId64 "ms @%s",
cntl->backup_request_ms(),
butil::endpoint2str(cntl->remote_side()).c_str());
Expand Down Expand Up @@ -1313,6 +1329,7 @@ CallId Controller::call_id() {
void Controller::SaveClientSettings(ClientSettings* s) const {
s->timeout_ms = _timeout_ms;
s->backup_request_ms = _backup_request_ms;
s->backup_request_policy = _backup_request_policy;
s->max_retry = _max_retry;
s->tos = _tos;
s->connection_type = _connection_type;
Expand All @@ -1325,6 +1342,7 @@ void Controller::SaveClientSettings(ClientSettings* s) const {
void Controller::ApplyClientSettings(const ClientSettings& s) {
set_timeout_ms(s.timeout_ms);
set_backup_request_ms(s.backup_request_ms);
set_backup_request_policy(s.backup_request_policy);
set_max_retry(s.max_retry);
set_type_of_service(s.tos);
set_connection_type(s.connection_type);
Expand Down
11 changes: 9 additions & 2 deletions src/brpc/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "brpc/grpc.h"
#include "brpc/kvmap.h"
#include "brpc/rpc_dump.h"
#include "brpc/backup_request_policy.h"

// EAUTH is defined in MAC
#ifndef EAUTH
Expand Down Expand Up @@ -180,7 +181,10 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
// Set/get the delay to send backup request in milliseconds. Use
// ChannelOptions.backup_request_ms on unset.
void set_backup_request_ms(int64_t timeout_ms);
int64_t backup_request_ms() const { return _backup_request_ms; }
void set_backup_request_policy(const BackupRequestPolicy* policy) {
_backup_request_policy = policy;
}
int64_t backup_request_ms() const;

// Set/get maximum times of retrying. Use ChannelOptions.max_retry on unset.
// <=0 means no retry.
Expand Down Expand Up @@ -670,7 +674,8 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
struct ClientSettings {
int32_t timeout_ms;
int32_t backup_request_ms;
int max_retry;
const BackupRequestPolicy* backup_request_policy;
int max_retry;
int32_t tos;
ConnectionType connection_type;
CompressType request_compress_type;
Expand Down Expand Up @@ -800,6 +805,8 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
int32_t _timeout_ms;
int32_t _connect_timeout_ms;
int32_t _backup_request_ms;
// Priority: `_backup_request_policy' > `_backup_request_ms'.
const BackupRequestPolicy* _backup_request_policy;
// If this rpc call has retry/backup request,this var save the real timeout for current call
int64_t _real_timeout_ms;
// Deadline of this RPC (since the Epoch in microseconds).
Expand Down
126 changes: 125 additions & 1 deletion test/brpc_channel_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,18 @@ class ChannelTest : public ::testing::Test{
bool single_server,
bool short_connection,
const brpc::Authenticator* auth = NULL,
std::string connection_group = std::string()) {
std::string connection_group = std::string(),
bool use_backup_request_policy = false) {
brpc::ChannelOptions opt;
if (short_connection) {
opt.connection_type = brpc::CONNECTION_TYPE_SHORT;
}
opt.auth = auth;
opt.max_retry = 0;
opt.connection_group = connection_group;
if (use_backup_request_policy) {
opt.backup_request_policy = &_backup_request_policy;
}
if (single_server) {
EXPECT_EQ(0, channel->Init(_ep, &opt));
} else {
Expand Down Expand Up @@ -1918,6 +1922,105 @@ class ChannelTest : public ::testing::Test{
StopAndJoin();
}

void TestBackupRequest(bool single_server, bool async,
bool short_connection) {
std::cout << " *** single=" << single_server
<< " async=" << async
<< " short=" << short_connection << std::endl;

ASSERT_EQ(0, StartAccept(_ep));
brpc::Channel channel;
SetUpChannel(&channel, single_server, short_connection);

const int RETRY_NUM = 1;
test::EchoRequest req;
test::EchoResponse res;
brpc::Controller cntl;
req.set_message(__FUNCTION__);

cntl.set_max_retry(RETRY_NUM);
cntl.set_backup_request_ms(10); // 10ms
cntl.set_timeout_ms(100); // 10ms
req.set_sleep_us(50000); // 100ms
CallMethod(&channel, &cntl, &req, &res, async);
ASSERT_EQ(0, cntl.ErrorCode()) << cntl.ErrorText();
ASSERT_TRUE(cntl.has_backup_request());
ASSERT_EQ(RETRY_NUM, cntl.retried_count());
bthread_usleep(70000); // wait for the sleep task to finish

if (short_connection) {
// Sleep to let `_messenger' detect `Socket' being `SetFailed'
const int64_t start_time = butil::gettimeofday_us();
while (_messenger.ConnectionCount() != 0) {
EXPECT_LT(butil::gettimeofday_us(), start_time + 100000L/*100ms*/);
bthread_usleep(1000);
}
} else {
EXPECT_GE(1ul, _messenger.ConnectionCount());
}
StopAndJoin();
}

class BackupRequestPolicyImpl : public brpc::BackupRequestPolicy {
public:
int32_t GetBackupRequestMs() const override {
return 10;
}

// Return true if the backup request should be sent.
bool DoBackup(const brpc::Controller*) const override {
return backup;
}

bool backup{true};

};

void TestBackupRequestPolicy(bool single_server, bool async,
bool short_connection) {
ASSERT_EQ(0, StartAccept(_ep));
for (int i = 0; i < 2; ++i) {
bool backup = i == 0;
std::cout << " *** single=" << single_server
<< " async=" << async
<< " short=" << short_connection
<< " backup=" << backup
<< std::endl;

brpc::Channel channel;
SetUpChannel(&channel, single_server, short_connection, NULL, "", true);

const int RETRY_NUM = 1;
test::EchoRequest req;
test::EchoResponse res;
brpc::Controller cntl;
req.set_message(__FUNCTION__);

_backup_request_policy.backup = backup;
cntl.set_max_retry(RETRY_NUM);
cntl.set_timeout_ms(100); // 100ms
req.set_sleep_us(50000); // 50ms
CallMethod(&channel, &cntl, &req, &res, async);
ASSERT_EQ(0, cntl.ErrorCode()) << cntl.ErrorText();
ASSERT_EQ(backup, cntl.has_backup_request());
ASSERT_EQ(backup ? RETRY_NUM : 0, cntl.retried_count());
bthread_usleep(70000); // wait for the sleep task to finish

if (short_connection) {
// Sleep to let `_messenger' detect `Socket' being `SetFailed'
const int64_t start_time = butil::gettimeofday_us();
while (_messenger.ConnectionCount() != 0) {
ASSERT_LT(butil::gettimeofday_us(), start_time + 100000L/*100ms*/);
bthread_usleep(1000);
}
} else {
ASSERT_GE(1ul, _messenger.ConnectionCount());
}
}

StopAndJoin();
}

butil::EndPoint _ep;
butil::TempFile _server_list;
std::string _naming_url;
Expand All @@ -1930,6 +2033,7 @@ class ChannelTest : public ::testing::Test{
bool _close_fd_once;

MyEchoService _svc;
BackupRequestPolicyImpl _backup_request_policy;
};

class MyShared : public brpc::SharedObject {
Expand Down Expand Up @@ -2597,6 +2701,26 @@ TEST_F(ChannelTest, retry_backoff) {
}
}

TEST_F(ChannelTest, backup_request) {
for (int i = 0; i <= 1; ++i) { // Flag SingleServer
for (int j = 0; j <= 1; ++j) { // Flag Asynchronous
for (int k = 0; k <= 1; ++k) { // Flag ShortConnection
TestBackupRequest(i, j, k);
}
}
}
}

TEST_F(ChannelTest, backup_request_policy) {
for (int i = 0; i <= 1; ++i) { // Flag SingleServer
for (int j = 0; j <= 1; ++j) { // Flag Asynchronous
for (int k = 0; k <= 1; ++k) { // Flag ShortConnection
TestBackupRequestPolicy(i, j, k);
}
}
}
}

TEST_F(ChannelTest, multiple_threads_single_channel) {
srand(time(NULL));
ASSERT_EQ(0, StartAccept(_ep));
Expand Down
Loading