Skip to content

Commit e3fbbef

Browse files
committed
Fix master lease time
1 parent 321dbdb commit e3fbbef

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

include/skywalker/checkpoint.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ class Checkpoint {
2929
virtual bool LoadCheckpoint(uint32_t group_id, uint64_t instance_id,
3030
uint32_t machine_id, const std::string& dir,
3131
const std::vector<std::string>& files);
32-
33-
private:
34-
// No copying allowed
35-
Checkpoint(const Checkpoint&);
36-
void operator=(const Checkpoint&);
3732
};
3833

3934
} // namespace skywalker

include/skywalker/state_machine.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ class StateMachine {
2424

2525
private:
2626
uint32_t id_;
27-
28-
// No copying allowed
29-
StateMachine(const StateMachine&);
30-
void operator=(const StateMachine&);
3127
};
3228

3329
} // namespace skywalker

machine/master_machine.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void MasterMachine::Recover() {
2727
}
2828

2929
bool MasterMachine::Execute(uint32_t group_id, uint64_t instance_id,
30-
const std::string& value, void* /* context */) {
30+
const std::string& value, void* context) {
3131
MasterState state;
3232
if (state.ParseFromString(value)) {
3333
if (instance_id < state_.version()) {
@@ -37,7 +37,12 @@ bool MasterMachine::Execute(uint32_t group_id, uint64_t instance_id,
3737
int ret = config_->GetDB()->SetMasterState(state);
3838
if (ret == 0) {
3939
bool b = (state.node_id() != state_.node_id());
40-
state.set_lease_time(NowMicros() + state.lease_time());
40+
uint64_t* now = reinterpret_cast<uint64_t*>(context);
41+
if (now) {
42+
state.set_lease_time(*now + state.lease_time());
43+
} else {
44+
state.set_lease_time(NowMicros() + state.lease_time());
45+
}
4146
SetMasterState(state);
4247
LOG_INFO(
4348
"Group %u - now the master's version=%llu, "

machine/master_machine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MasterMachine : public StateMachine {
3333
void SetString(const std::string& s);
3434

3535
virtual bool Execute(uint32_t group_id, uint64_t instance_id,
36-
const std::string& value, void* /* context */);
36+
const std::string& value, void* context);
3737

3838
private:
3939
Config* config_;

paxos/group.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ Group::Group(uint64_t node_id, uint32_t group_id, const GroupOptions& options,
1818
config_(node_id, group_id, options, network),
1919
instance_(&config_),
2020
use_master_(options.use_master),
21-
lease_timeout_(options.master_lease_time),
2221
retrie_master_(false),
22+
lease_timeout_(options.master_lease_time),
23+
now_(0),
2324
mutex_(),
2425
cond_(&mutex_),
2526
propose_end_(false),
@@ -126,11 +127,12 @@ void Group::TryBeMaster() {
126127

127128
void Group::TryBeMasterInLoop() {
128129
MasterState state(master_machine_->GetMasterState());
129-
if (state.lease_time() <= NowMicros() || state.node_id() == node_id_) {
130+
now_ = NowMicros();
131+
if (state.lease_time() <= now_ || state.node_id() == node_id_) {
130132
state.set_node_id(node_id_);
131133
state.set_lease_time(lease_timeout_);
132134
instance_.OnPropose(master_machine_->machine_id(),
133-
state.SerializeAsString());
135+
state.SerializeAsString(), &now_);
134136
} else {
135137
propose_cb_(instance_.GetInstanceId(),
136138
Status::Conflict("Already has master"), nullptr);

paxos/group.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ class Group {
7272
Instance instance_;
7373

7474
bool use_master_;
75-
uint64_t lease_timeout_;
7675
bool retrie_master_;
76+
uint64_t lease_timeout_;
77+
uint64_t now_;
7778
MembershipMachine* membership_machine_;
7879
MasterMachine* master_machine_;
7980
ProposeCompleteCallback propose_cb_;

0 commit comments

Comments
 (0)