-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
199 lines (173 loc) · 7.27 KB
/
server.py
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
from concurrent import futures
import logging
from rwmutex import RWLock
import pickle
import sys
import grpc
from proto import helloworld_pb2
from proto import helloworld_pb2_grpc
from datetime import datetime
import os
import psutil
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def __init__(self):
self.clientPastRequests = {}
self.lastRequestArrivalTime = {}
self.lock1 = RWLock()
self.lock2= RWLock()
def SayHello(self, request, context):
clientId = request.clientId
requestId = request.requestId
timeAtClient = request.timeAtClient
pastWindowData = request.pastWindowData
seqNum = request.sequenceNumber
history = request.history
print("[ " + seqNum + " ]" + "Received request from " + clientId + " with requestId: " + requestId)
serverTime = datetime.now()
sequenceNumberTuple = "(SequenceNumber, " + seqNum + ")"
requestArrivalTimeTuple = "(RequestArrivalTime, " + serverTime.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] + ")"
clientIdTuple = "(ClientId, " + clientId + ")"
timeAtClientTuple = "(TimeAtClient, " + timeAtClient + ")"
pastWindowDataTuple = "(PastWindowData, " + str(pickle.loads(pastWindowData.encode())) + ")"
historyTuple = "(History, " + str(pickle.loads(history.encode())) + ")"
timeSinceClientsLastReqTuple = "(TimeSinceThisClientsLastRequest, "
with self.lock1.read:
if clientId in self.clientPastRequests:
timeSinceClientsLastReqTuple += str(serverTime - self.clientPastRequests[clientId]) + ")"
else:
timeSinceClientsLastReqTuple += "None)"
lastReqByTuple = "(LastRequestBy, "
timeSinceLastReqTuple = "(TimeSinceLastRequest, "
with self.lock2.read:
if self.lastRequestArrivalTime:
lastReqByTuple += self.lastRequestArrivalTime["clientId"] + ")"
timeSinceLastReqTuple += str(serverTime - self.lastRequestArrivalTime["time"]) + ")"
else:
lastReqByTuple += "None)"
timeSinceLastReqTuple += "None)"
requestIdTuple = "(RequestId, " + requestId + ")"
''' System conditions '''
cpu_percentage = psutil.cpu_percent()
memory_conditions = dict(psutil.virtual_memory()._asdict())
cpu_util_tuple = "(CPUUtil, " + str(cpu_percentage) + ")"
memory_conditions_tuple = "(Memory, " + str(memory_conditions) + ")"
logging.info("[RequestLog]"
+ requestIdTuple + "; "
+ sequenceNumberTuple + "; "
+ requestArrivalTimeTuple + "; "
+ clientIdTuple + "; "
+ timeAtClientTuple + "; "
+ pastWindowDataTuple + "; "
+ timeSinceClientsLastReqTuple + "; "
+ lastReqByTuple + "; "
+ timeSinceLastReqTuple + "; "
+ historyTuple + "; "
+ cpu_util_tuple + "; "
+ memory_conditions_tuple + "; "
)
'''
logging.info(
"[ " + requestId + " ]" +
"[ RequestArrivalTime ]" +
serverTime.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
)
logging.info(
"[ " + requestId + " ]" +
"[ ClientId ]" +
clientId
)
logging.info(
"[ " + requestId + " ]" +
"[ TimeAtClient ]" +
timeAtClient
)
# print("PastWindowData", pickle.loads(pastWindowData.encode()))
logging.info(
"[ " + requestId + " ]" +
"[ PastWindowData ]" +
str(pickle.loads(pastWindowData.encode()))
)
with self.lock1.read:
if clientId in self.clientPastRequests:
logging.info(
"[ " + requestId + " ]" +
"[ TimeSinceThisClientsLastRequest ]" +
str(serverTime - self.clientPastRequests[clientId])
)
'''
with self.lock1.write:
self.clientPastRequests[clientId] = serverTime
'''
with self.lock2.read:
if self.lastRequestArrivalTime:
logging.info(
"[ " + requestId + " ]" +
"[ LastRequestBy ]" +
self.lastRequestArrivalTime["clientId"]
)
logging.info(
"[ " + requestId + " ]" +
"[ TimeSincesLastRequest ]" +
str(serverTime - self.lastRequestArrivalTime["time"])
)
'''
with self.lock2.write:
self.lastRequestArrivalTime["clientId"] = clientId
self.lastRequestArrivalTime["time"] = serverTime
return helloworld_pb2.HelloReply(requestId = requestId, serverTime = serverTime.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], requestTime = timeAtClient)
def SayHelloReplica(self, request, context):
requestId = request.requestId
history = request.history
pastWindowData = request.pastWindowData
timeAtSender = request.timeAtSender
senderId = request.replicaId
serverTime = datetime.now()
requestIdTuple = "(RequestId, " + requestId + ")"
requestArrivalTimeTuple = "(RequestArrivalTime, " + serverTime.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] + ")"
historyTuple = "(History, " + str(pickle.loads(history.encode())) + ")"
pastWindowDataTuple = "(PastWindowData, " + str(pickle.loads(pastWindowData.encode())) + ")"
timeAtSenderTuple = "(TimeAtSenderTuple, " + timeAtSender + ")"
senderIdTuple = "(SenderId, " + str(senderId) + ")"
''' System conditions '''
cpu_percentage = psutil.cpu_percent()
memory_conditions = dict(psutil.virtual_memory()._asdict())
cpu_util_tuple = "(CPUUtil, " + str(cpu_percentage) + ")"
memory_conditions_tuple = "(Memory, " + str(memory_conditions) + ")"
logging.info("[ReplicaRequestLog]"
+ str(requestIdTuple) + "; "
+ str(senderIdTuple) + "; "
+ str(requestArrivalTimeTuple) + "; "
+ str(pastWindowDataTuple) + "; "
+ str(historyTuple) + "; "
+ str(timeAtSenderTuple) + "; "
+ str(cpu_util_tuple) + "; "
+ str(memory_conditions_tuple) + "; "
)
return helloworld_pb2.HelloReplyReplica(
requestId = requestId,
timeAtReceiver = serverTime.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
)
def serve():
port = '50060'
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:' + port)
server.start()
logging.info("Server started, listening on " + port)
print("Server started, listening on " + port)
server.wait_for_termination()
if __name__ == '__main__':
path = "logs/"
isExist = os.path.exists(path)
if not isExist:
os.makedirs(path)
logfilename = sys.argv[1]
logfilepath = "logs/" + logfilename
logging.basicConfig(
filename=logfilepath,
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG
)
serve()