Coverage Report

Created: 2024-09-03 06:23

/src/brpc/src/brpc/load_balancer.cpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
19
#include <gflags/gflags.h>
20
#include "brpc/reloadable_flags.h"
21
#include "brpc/load_balancer.h"
22
23
24
namespace brpc {
25
26
DEFINE_bool(show_lb_in_vars, false, "Describe LoadBalancers in vars");
27
DEFINE_int32(default_weight_of_wlb, 0, "Default weight value of Weighted LoadBalancer(wlb). "
28
             "wlb policy degradation is enabled when default_weight_of_wlb > 0 to avoid some "
29
             "problems when user is using wlb but forgot to set the weights of some of their "
30
             "downstream instances. Then these instances will be set default_weight_of_wlb as "
31
             "their weights. wlb policy degradation is not enabled by default.");
32
BRPC_VALIDATE_GFLAG(show_lb_in_vars, PassValidate);
33
34
// For assigning unique names for lb.
35
static butil::static_atomic<int> g_lb_counter = BUTIL_STATIC_ATOMIC_INIT(0);
36
37
0
void SharedLoadBalancer::DescribeLB(std::ostream& os, void* arg) {
38
0
    (static_cast<SharedLoadBalancer*>(arg))->Describe(os, DescribeOptions());
39
0
}
40
41
0
void SharedLoadBalancer::ExposeLB() {
42
0
    bool changed = false;
43
0
    _st_mutex.lock();
44
0
    if (!_exposed) {
45
0
        _exposed = true;
46
0
        changed = true;
47
0
    }
48
0
    _st_mutex.unlock();
49
0
    if (changed) {
50
0
        char name[32];
51
0
        snprintf(name, sizeof(name), "_load_balancer_%d", g_lb_counter.fetch_add(
52
0
                     1, butil::memory_order_relaxed));
53
0
        _st.expose(name);
54
0
    }
55
0
}
56
57
SharedLoadBalancer::SharedLoadBalancer()
58
    : _lb(NULL)
59
    , _weight_sum(0)
60
    , _exposed(false)
61
0
    , _st(DescribeLB, this) {
62
0
}
63
64
0
SharedLoadBalancer::~SharedLoadBalancer() {
65
0
    _st.hide();
66
0
    if (_lb) {
67
0
        _lb->Destroy();
68
0
        _lb = NULL;
69
0
    }
70
0
}
71
72
0
int SharedLoadBalancer::Init(const char* lb_protocol) {
73
0
    std::string lb_name;
74
0
    butil::StringPiece lb_params;
75
0
    if (!ParseParameters(lb_protocol, &lb_name, &lb_params)) {
76
0
        LOG(FATAL) << "Fail to parse this load balancer protocol '" << lb_protocol << '\'';
77
0
        return -1;
78
0
    }
79
0
    const LoadBalancer* lb = LoadBalancerExtension()->Find(lb_name.c_str());
80
0
    if (lb == NULL) {
81
0
        LOG(FATAL) << "Fail to find LoadBalancer by `" << lb_name << "'";
82
0
        return -1;
83
0
    }
84
0
    _lb = lb->New(lb_params);
85
0
    if (_lb == NULL) {
86
0
        LOG(FATAL) << "Fail to new LoadBalancer";
87
0
        return -1;
88
0
    }
89
0
    if (FLAGS_show_lb_in_vars && !_exposed) {
90
0
        ExposeLB();
91
0
    }
92
0
    return 0;
93
0
}
94
95
void SharedLoadBalancer::Describe(std::ostream& os,
96
0
                                  const DescribeOptions& options) {
97
0
    if (_lb == NULL) {
98
0
        os << "lb=NULL";
99
0
    } else {
100
0
        _lb->Describe(os, options);
101
0
    }
102
0
}
103
104
bool SharedLoadBalancer::ParseParameters(const butil::StringPiece& lb_protocol,
105
                                         std::string* lb_name,
106
0
                                         butil::StringPiece* lb_params) {
107
0
    lb_name->clear();
108
0
    lb_params->clear();
109
0
    if (lb_protocol.empty()) {
110
0
        return false;
111
0
    }
112
0
    const char separator = ':';
113
0
    size_t pos = lb_protocol.find(separator);
114
0
    if (pos == std::string::npos) {
115
0
        lb_name->append(lb_protocol.data(), lb_protocol.size());
116
0
    } else {
117
0
        lb_name->append(lb_protocol.data(), pos);
118
0
        if (pos < lb_protocol.size() - sizeof(separator)) {
119
0
            *lb_params = lb_protocol.substr(pos + sizeof(separator));
120
0
        }
121
0
    }
122
123
0
    return true;
124
0
}
125
                                         
126
} // namespace brpc