-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
287 lines (227 loc) · 8.75 KB
/
client.py
File metadata and controls
287 lines (227 loc) · 8.75 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#! /usr/bin/env python3
import logging
import argparse
import yaml
import time
import json
import asyncio
import aiohttp
from aiohttp import web
from random import random
import hashlib
class View:
def __init__(self, view_number, num_nodes):
self._view_number = view_number
self._num_nodes = num_nodes
self._leader = view_number % num_nodes
# To encode to json
def get(self):
return self._view_number
# Recover from json data.
def set_view(self, view):
self._view_number = view
self._leader = view % self._num_nodes
class Status:
def __init__(self, f):
self.f = f
self.reply_msgs = {}
class SequenceElement:
def __init__(self, proposal):
self.proposal = proposal
self.from_nodes = set([])
def _update_sequence(self, view, proposal, from_node):
'''
Update the record in the status when receive reply messages.
input:
view: View object of self._follow_view
proposal: proposal in json_data
from_node: The node send given the message.
'''
# The key need to include hash(proposal) in case get different
# preposals from BFT nodes. Need sort key in json.dumps to make
# sure getting the same string. Use hashlib so that we got same
# hash everytime.
hash_object = hashlib.md5(json.dumps(proposal, sort_keys=True).encode())
key = (view.get(), hash_object.digest())
if key not in self.reply_msgs:
self.reply_msgs[key] = self.SequenceElement(proposal)
self.reply_msgs[key].from_nodes.add(from_node)
def _check_succeed(self):
'''
Check if receive more than f + 1 given type message in the same view.
input:
msg_type: self.PREPARE or self.COMMIT
'''
for key in self.reply_msgs:
if len(self.reply_msgs[key].from_nodes)>= self.f + 1:
return True
return False
def logging_config(log_level=logging.INFO, log_file=None):
root_logger = logging.getLogger()
if root_logger.hasHandlers():
return
root_logger.setLevel(log_level)
f = logging.Formatter("[%(levelname)s]%(module)s->%(funcName)s: \t %(message)s \t --- %(asctime)s")
h = logging.StreamHandler()
h.setFormatter(f)
h.setLevel(log_level)
root_logger.addHandler(h)
if log_file:
from logging.handlers import TimedRotatingFileHandler
h = TimedRotatingFileHandler(log_file, when='midnight', interval=1, backupCount=7)
h.setFormatter(f)
h.setLevel(log_level)
root_logger.addHandler(h)
def arg_parse():
# parse command line options
parser = argparse.ArgumentParser(description='PBFT Node')
parser.add_argument('-id', '--client_id', type=int, help='client id')
parser.add_argument('-nm', '--num_messages', default=10, type=int, help='number of message want to send for this client')
parser.add_argument('-c', '--config', default='pbft.yaml', type=argparse.FileType('r'), help='use configuration [%(default)s]')
args = parser.parse_args()
return args
def conf_parse(conf_file) -> dict:
'''
Sample config:
nodes:
- host:
port:
- host:
port:
loss%:
skip:
heartbeat:
ttl:
interval:
election_slice: 10
sync_interval: 10
misc:
network_timeout: 10
'''
conf = yaml.load(conf_file)
return conf
def make_url(node, command):
return "http://{}:{}/{}".format(node['host'], node['port'], command)
class Client:
REQUEST = "request"
REPLY = "reply"
VIEW_CHANGE_REQUEST = 'view_change_request'
def __init__(self, conf, args, log):
self._nodes = conf['nodes']
self._resend_interval = conf['misc']['network_timeout']
self._client_id = args.client_id
self._num_messages = args.num_messages
self._session = None
self._address = conf['clients'][self._client_id]
self._client_url = "http://{}:{}".format(self._address['host'],
self._address['port'])
self._log = log
self._retry_times = conf['retry_times_before_view_change']
# Number of faults tolerant.
self._f = (len(self._nodes) - 1) // 3
# Event for sending next request
self._is_request_succeed = None
# To record the status of current request
self._status = None
async def request_view_change(self):
json_data = {
"action" : "view change"
}
for i in range(len(self._nodes)):
try:
await self._session.post(make_url(
self._nodes[i], Client.VIEW_CHANGE_REQUEST), json=json_data)
except:
self._log.info("---> %d failed to send view change message to node %d.",
self._client_id, i)
else:
self._log.info("---> %d succeed in sending view change message to node %d.",
self._client_id, i)
async def get_reply(self, request):
'''
Count the number of valid reply messages and decide whether request succeed:
1. Process the request only if timestamp is still valid(not stall)
2. Count the number of reply message within same view,
if above f + 1, means success.
input:
request:
reply_msg = {
'index': self._index,
'view': json_data['view'],
'proposal': json_data['proposal'][slot],
'type': Status.REPLY
}
output:
Web.Response
'''
json_data = await request.json()
if time.time() - json_data['proposal']['timestamp'] >= self._resend_interval:
return web.Response()
view = View(json_data['view'], len(self._nodes))
self._status._update_sequence(view, json_data['proposal'], json_data['index'])
if self._status._check_succeed():
# self._log.info("Get reply from %d", json_data['index'])
self._is_request_succeed.set()
return web.Response()
async def request(self):
if not self._session:
timeout = aiohttp.ClientTimeout(self._resend_interval)
self._session = aiohttp.ClientSession(timeout = timeout)
for i in range(self._num_messages):
accumulate_failure = 0
is_sent = False
dest_ind = 0
self._is_request_succeed = asyncio.Event()
# Every time succeed in sending message, wait for 0 - 1 second.
await asyncio.sleep(random())
json_data = {
'id': (self._client_id, i),
'client_url': self._client_url + "/" + Client.REPLY,
'timestamp': time.time(),
'data': str(i)
}
while 1:
try:
self._status = Status(self._f)
await self._session.post(make_url(self._nodes[dest_ind], Client.REQUEST), json=json_data)
await asyncio.wait_for(self._is_request_succeed.wait(), self._resend_interval)
except:
json_data['timestamp'] = time.time()
self._status = Status(self._f)
self._is_request_succeed.clear()
self._log.info("---> %d message %d sent fail.", self._client_id, i)
accumulate_failure += 1
if accumulate_failure == self._retry_times:
await self.request_view_change()
# Sleep 0 - 1 second for view change
await asyncio.sleep(random())
accumulate_failure = 0
dest_ind = (dest_ind + 1) % len(self._nodes)
else:
self._log.info("---> %d message %d sent successfully.", self._client_id, i)
is_sent = True
if is_sent:
break
await self._session.close()
def main():
logging_config()
log = logging.getLogger()
args = arg_parse()
conf = conf_parse(args.config)
log.debug(conf)
addr = conf['clients'][args.client_id]
log.info("begin")
client = Client(conf, args, log)
addr = client._address
host = addr['host']
port = addr['port']
asyncio.ensure_future(client.request())
app = web.Application()
app.add_routes([
web.post('/' + Client.REPLY, client.get_reply),
])
web.run_app(app, host=host, port=port, access_log=None)
# loop = asyncio.get_event_loop()
# loop.run_until_complete(client.request())
if __name__ == "__main__":
main()