forked from lxyfirst/queue_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_processor_manager.cpp
More file actions
executable file
·184 lines (133 loc) · 3.85 KB
/
Copy pathasync_processor_manager.cpp
File metadata and controls
executable file
·184 lines (133 loc) · 3.85 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* async_processor_manager.cpp
* Author: lixingyi
*/
#include "async_processor_manager.h"
//#include "user_async_processor.h"
#include "queue_server.h"
using namespace framework ;
AsyncProcessorManager::AsyncProcessorManager()
{
// TODO Auto-generated constructor stub
}
AsyncProcessorManager::~AsyncProcessorManager()
{
clear() ;
}
base_fsm* AsyncProcessorManager::alloc_fsm(int fsm_type)
{
switch(fsm_type)
{
case StartVoteProcessor::TYPE:
return new StartVoteProcessor ;
default:
;
}
return NULL ;
}
void AsyncProcessorManager::free_fsm(base_fsm* object)
{
delete object ;
}
MajorityProcessor::MajorityProcessor():m_status(STATUS_INIT),m_wait_count(0),m_success_count(0)
{
m_timer.set_callback(this,&MajorityProcessor::on_timeout) ;
}
MajorityProcessor::~MajorityProcessor()
{
}
int MajorityProcessor::enter(fsm_manager* fm,int event_type,void* arg)
{
if(m_status == STATUS_INIT && init(arg)== PROCESSOR_CONTINUE )
{
m_status = STATUS_WAIT_DATA ;
arg = NULL ;
get_app().add_timer_after(&m_timer,5000) ;
if( m_wait_count >0 || m_success_count >0) return 0 ;
}
if(m_status == STATUS_WAIT_DATA && process(arg) == PROCESSOR_CONTINUE )
{
if( m_wait_count >0 && m_success_count > 0) return 0 ;
m_status = STATUS_WAIT_RESULT ;
arg = NULL ;
}
if(m_status == STATUS_WAIT_RESULT)
{
on_result(m_success_count == 0) ;
}
fm->destroy_fsm(this) ;
return 0 ;
}
int MajorityProcessor::batch_request(framework::packet* p)
{
int send_count = get_app().broadcast(p) ;
if(send_count >0)
{
set_wait_count(send_count) ;
set_success_count(get_app().majority_count() ) ;
return PROCESSOR_CONTINUE ;
}
return PROCESSOR_DONE ;
}
void MajorityProcessor::on_timeout(timer_manager* manager)
{
info_log_format(get_app().logger(),"fsm timeout fsm_id:%d",this->get_id());
//process(NULL) ;
on_timeout() ;
get_app().async_manager().destroy_fsm(this->get_id()) ;
}
int StartVoteProcessor::init(void* data)
{
VoteData* body = (VoteData*)data ;
trace_log_format(get_app().logger(),"broadcast vote vote_id:%d node_id:%d",body->vote_id(),body->node_id() ) ;
request.head.seq = this->get_id() ;
request.head.src_key = request.head.dst_key = body->node_id() ;
request.body.CopyFrom(*body) ;
return batch_request(&request) ;
}
int StartVoteProcessor::process(void* data)
{
packet_info* pi = (packet_info*)data ;
if(response.decode(pi->data,pi->size)!=pi->size) return PROCESSOR_DONE ;
trace_log_format(get_app().logger(),"vote response node_id:%d result:%d",
response.head.src_key ,response.body.error_code()) ;
dec_wait_count() ;
if(response.body.error_code() == EC_SUCCESS )
{
dec_success_count() ;
}
else if(response.body.error_code() == EC_VOTE_LEADER_EXIST)
{
get_app().set_leader(response.body.data()) ;
return PROCESSOR_DONE;
}
else
{
return PROCESSOR_DONE ;
}
return PROCESSOR_CONTINUE ;
}
void StartVoteProcessor::on_result(bool success)
{
if(success)
{
info_log_format(get_app().logger(),"vote success") ;
SSVoteNotify notify;
notify.head.seq = this->get_id() ;
notify.body.CopyFrom(request.body) ;
get_app().broadcast(¬ify) ;
get_app().set_leader(request.body) ;
}
else
{
info_log_format(get_app().logger(),"vote failed") ;
}
}
void StartVoteProcessor::on_timeout()
{
info_log_format(get_app().logger(),"vote timeout") ;
}
StartVoteProcessor::~StartVoteProcessor()
{
get_app().stop_vote() ;
}