-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaftStateMachine.h
75 lines (55 loc) · 1.74 KB
/
RaftStateMachine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by projector on 7/9/22.
//
#ifndef RAFTFUSEFS_RAFTSTATEMACHINE_H
#define RAFTFUSEFS_RAFTSTATEMACHINE_H
#include <braft/raft.h>
#include "Utils.h"
#include "raft_log.pb.h"
DECLARE_int32(port);
class RaftStateMachine : public braft::StateMachine {
public:
RaftStateMachine(){}
void setPFuseFs(void *pFuseFs) {
pFuseFS = pFuseFs;
}
int start();
int apply(RaftLog::LogData &logData, struct fuse_file_info *fi);
virtual void on_apply(braft::Iterator &iter);
virtual void on_leader_start(int64_t term);
virtual void on_leader_stop(const butil::Status &status);
int64_t getHasAppliedIndex() const {
return hasAppliedIndex;
}
//设置并持久化hasAppliedIndex
void setHasAppliedIndex(int64_t hasAppliedIndex);
int loadHasAppliedIndex();
private:
braft::Node* volatile _node;
butil::atomic<int64_t> _leader_term;
int64_t hasAppliedIndex = 0; //已经回放的位置,用于跳过已经回放的日志
void *pFuseFS;
};
class RaftClosure : public braft::Closure {
public:
RaftClosure(RaftStateMachine *raftStateMachine, braft::SynchronizedClosure *synchronizedClosure, int *nRetCode,
struct fuse_file_info *fi)
: raftStateMachine(raftStateMachine)
,synchronizedClosure(synchronizedClosure)
,nRetCode(nRetCode)
,fi(fi)
{}
void Run();
fuse_file_info *getFi() const {
return fi;
}
void setNRetCode(int nRetCode) {
*(this->nRetCode) = nRetCode;
}
private:
RaftStateMachine* raftStateMachine;
braft::SynchronizedClosure *synchronizedClosure; //同步等待的closure,如果不为NULL,说明是同步等结果
int *nRetCode;
struct fuse_file_info *fi;
};
#endif //RAFTFUSEFS_RAFTSTATEMACHINE_H