|
| 1 | +// Copyright (c) 2019 Baidu.com, Inc. All Rights Reserved |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Authors: Pengfei Zheng (zhengpengfei@baidu.com) |
| 16 | + |
| 17 | +#include <gflags/gflags.h> |
| 18 | +#include <brpc/reloadable_flags.h> |
| 19 | +#include "braft/lease.h" |
| 20 | + |
| 21 | +namespace braft { |
| 22 | + |
| 23 | +DEFINE_bool(raft_enable_leader_lease, false, |
| 24 | + "Enable or disable leader lease. only when all peers in a raft group " |
| 25 | + "set this configuration to true, leader lease check and vote are safe."); |
| 26 | +BRPC_VALIDATE_GFLAG(raft_enable_leader_lease, ::brpc::PassValidate); |
| 27 | + |
| 28 | +void LeaderLease::init(int64_t election_timeout_ms) { |
| 29 | + _election_timeout_ms = election_timeout_ms; |
| 30 | +} |
| 31 | + |
| 32 | +void LeaderLease::on_leader_start(int64_t term) { |
| 33 | + BAIDU_SCOPED_LOCK(_mutex); |
| 34 | + ++_lease_epoch; |
| 35 | + _term = term; |
| 36 | + _last_active_timestamp = 0; |
| 37 | +} |
| 38 | + |
| 39 | +void LeaderLease::on_leader_stop() { |
| 40 | + BAIDU_SCOPED_LOCK(_mutex); |
| 41 | + _last_active_timestamp = 0; |
| 42 | + _term = 0; |
| 43 | +} |
| 44 | + |
| 45 | +void LeaderLease::on_lease_start(int64_t expect_lease_epoch, int64_t last_active_timestamp) { |
| 46 | + BAIDU_SCOPED_LOCK(_mutex); |
| 47 | + if (_term == 0 || expect_lease_epoch != _lease_epoch) { |
| 48 | + return; |
| 49 | + } |
| 50 | + _last_active_timestamp = last_active_timestamp; |
| 51 | +} |
| 52 | + |
| 53 | +void LeaderLease::renew(int64_t last_active_timestamp) { |
| 54 | + BAIDU_SCOPED_LOCK(_mutex); |
| 55 | + _last_active_timestamp = last_active_timestamp; |
| 56 | +} |
| 57 | + |
| 58 | +void LeaderLease::get_lease_info(LeaseInfo* lease_info) { |
| 59 | + lease_info->term = 0; |
| 60 | + lease_info->lease_epoch = 0; |
| 61 | + if (!FLAGS_raft_enable_leader_lease) { |
| 62 | + lease_info->state = LeaderLease::DISABLED; |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + BAIDU_SCOPED_LOCK(_mutex); |
| 67 | + if (_term == 0) { |
| 68 | + lease_info->state = LeaderLease::EXPIRED; |
| 69 | + return; |
| 70 | + } |
| 71 | + if (_last_active_timestamp == 0) { |
| 72 | + lease_info->state = LeaderLease::NOT_READY; |
| 73 | + return; |
| 74 | + } |
| 75 | + if (butil::monotonic_time_ms() < _last_active_timestamp + _election_timeout_ms) { |
| 76 | + lease_info->term = _term; |
| 77 | + lease_info->lease_epoch = _lease_epoch; |
| 78 | + lease_info->state = LeaderLease::VALID; |
| 79 | + } else { |
| 80 | + lease_info->state = LeaderLease::SUSPECT; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +int64_t LeaderLease::lease_epoch() { |
| 85 | + BAIDU_SCOPED_LOCK(_mutex); |
| 86 | + return _lease_epoch; |
| 87 | +} |
| 88 | + |
| 89 | +void LeaderLease::reset_election_timeout_ms(int64_t election_timeout_ms) { |
| 90 | + BAIDU_SCOPED_LOCK(_mutex); |
| 91 | + _election_timeout_ms = election_timeout_ms; |
| 92 | +} |
| 93 | + |
| 94 | +void FollowerLease::init(int64_t election_timeout_ms, int64_t max_clock_drift_ms) { |
| 95 | + _election_timeout_ms = election_timeout_ms; |
| 96 | + _max_clock_drift_ms = max_clock_drift_ms; |
| 97 | + // When the node restart, we are not sure when the lease will be expired actually, |
| 98 | + // so just be conservative. |
| 99 | + _last_leader_timestamp = butil::monotonic_time_ms(); |
| 100 | +} |
| 101 | + |
| 102 | +void FollowerLease::renew(const PeerId& leader_id) { |
| 103 | + _last_leader = leader_id; |
| 104 | + _last_leader_timestamp = butil::monotonic_time_ms(); |
| 105 | +} |
| 106 | + |
| 107 | +int64_t FollowerLease::votable_time_from_now() { |
| 108 | + if (!FLAGS_raft_enable_leader_lease) { |
| 109 | + return 0; |
| 110 | + } |
| 111 | + |
| 112 | + int64_t now = butil::monotonic_time_ms(); |
| 113 | + int64_t votable_timestamp = _last_leader_timestamp + _election_timeout_ms + |
| 114 | + _max_clock_drift_ms; |
| 115 | + if (now >= votable_timestamp) { |
| 116 | + return 0; |
| 117 | + } |
| 118 | + return votable_timestamp - now; |
| 119 | +} |
| 120 | + |
| 121 | +const PeerId& FollowerLease::last_leader() { |
| 122 | + return _last_leader; |
| 123 | +} |
| 124 | + |
| 125 | +bool FollowerLease::expired() { |
| 126 | + return butil::monotonic_time_ms() - _last_leader_timestamp |
| 127 | + >= _election_timeout_ms + _max_clock_drift_ms; |
| 128 | +} |
| 129 | + |
| 130 | +void FollowerLease::reset() { |
| 131 | + _last_leader = PeerId(); |
| 132 | + _last_leader_timestamp = 0; |
| 133 | +} |
| 134 | + |
| 135 | +void FollowerLease::reset_election_timeout_ms(int64_t election_timeout_ms, |
| 136 | + int64_t max_clock_drift_ms) { |
| 137 | + _election_timeout_ms = election_timeout_ms; |
| 138 | + _max_clock_drift_ms = max_clock_drift_ms; |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace braft |
0 commit comments