-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorch.py
442 lines (400 loc) · 16.6 KB
/
orch.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# from kafka.admin import KafkaAdminClient, NewTopic
# from kafka import KafkaProducer, KafkaConsumer
# from kafka.errors import *
# import random
# import sys
# import json
# import mysql.connector
# from mysql.connector import Error
# from prettytable import PrettyTable
# import threading
# import time
# def maketopics():
# Admin = KafkaAdminClient(bootstrap_servers="localhost:9092", client_id="Orchestrar")
# print("Creating Topics")
# topic_names = ['test_config', 'heartbeat', 'register', 'metrics', 'trigger']
# for topic in topic_names:
# NewTopic(name=topic, num_partitions=3, replication_factor=3)
# print("Topics Created")
# return Admin
# def exitcode(Admin, connection):
# topic_names = ['test_config', 'heartbeat', 'register', 'metrics', 'trigger']
# Admin.delete_topics(topics=topic_names)
# cursor = connection.cursor()
# if connection.is_connected():
# cursor.close()
# connection.close()
# print("MySQL connection is closed")
# exit()
# def make_config():
# test_config_mesg = {
# "test_id": 0,
# "test_type": "",
# "test_message_delay": 0.00,
# "throughput_per_driver": 0
# }
# test_config_mesg["test_id"] = str(random.randint(100000, 999999))
# tp = int(input("Enter the Desired Throughput Per Driver : "))
# test_config_mesg["throughput_per_driver"] = str(tp)
# TOT = input("Select Type of Test: (A) Avalanche / (T) Tsunami : ")
# if TOT == "A":
# test_config_mesg["test_type"] = "AVALANCHE"
# elif TOT == "T":
# test_config_mesg["test_type"] = "TSUNAMI"
# delay = input("Enter delay : ")
# test_config_mesg["test_message_delay"] = delay
# return test_config_mesg
# def make_trigger():
# trigger_req = {
# "test_id": "",
# "trigger": "YES"
# }
# trigger_req["test_id"] = str(random.randint(100000, 999999))
# return trigger_req
# def kafka_setup():
# print("Setting Up Kafka Infrastructure")
# Producer = KafkaProducer(bootstrap_servers="localhost:9092")
# Consumer = KafkaConsumer()
# Consumer.subscribe(["register", "heartbeat", "metrics"])
# return Consumer, Producer
# def Registering(Consumer, n, connection):
# print("Registering Nodes ...")
# nodeslist = []
# i = n
# print("Start your Driver Nodes ...")
# for message in Consumer:
# message = message.value.decode('utf8').replace("'", '"')
# node = json.loads(message) # Adjusted JSON decoding without [1:-1]
# nodeslist.append(node)
# print("Registered Node : " + node["node_id"])
# i -= 1
# if i == 0:
# break
# print(f'{n} nodes registered ..')
# return
# def setup_db():
# connection_config_dict = {
# 'user': 'root',
# 'password': 'Naren@123',
# 'host': 'localhost',
# 'database': 'bd_proj'
# }
# connection = mysql.connector.connect(**connection_config_dict)
# return connection
# def insert_metric(data, connection):
# try:
# if connection.is_connected():
# cursor = connection.cursor()
# create_table_query = """
# CREATE TABLE IF NOT EXISTS metrics(
# id INT AUTO_INCREMENT PRIMARY KEY,
# node_id VARCHAR(255) NOT NULL,
# test_id VARCHAR(255) NOT NULL,
# report_id VARCHAR(255) NOT NULL,
# mean_latency DOUBLE,
# median_latency DOUBLE,
# min_latency DOUBLE,
# max_latency DOUBLE,
# timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
# )
# """
# cursor.execute(create_table_query)
# connection.commit()
# insert_metric_query = """
# INSERT INTO metrics (node_id, test_id, report_id, mean_latency, median_latency, min_latency, max_latency)
# VALUES (%s, %s, %s, %s, %s, %s, %s)
# """
# metric_data = (data["node_id"], data["test_id"], data["report_id"],
# data["metrics"]["mean_latency"], data["metrics"]["median_latency"],
# data["metrics"]["min_latency"], data["metrics"]["max_latency"])
# cursor.execute(insert_metric_query, metric_data)
# connection.commit()
# print(f"Inserted metrics data for node {data['node_id']} into the database.") # Debugging insertion
# except Error as e:
# print("Error while connecting to MySQL", e)
# def init_orch():
# print("Starting ... ")
# print("Importing Libraries ... ")
# Admin = maketopics()
# Consumer, Producer = kafka_setup()
# connection = setup_db()
# return Consumer, Producer, Admin, connection
# def showstats(test_id, connection):
# if connection.is_connected():
# cursor = connection.cursor()
# print(f"Executing query for test_id {test_id}") # Debugging query execution
# cursor.execute("SELECT * FROM bd_proj.metrics WHERE test_id = %s ", (str(test_id),))
# result = cursor.fetchall()
# print(f"test_id : {test_id}")
# table = PrettyTable()
# table.field_names = ["node_id", "report_id", "mean_latency", "median_latency", "min_latency", "max_latency"]
# for i in result:
# table.add_row([i[1], i[3], i[4], i[5], i[6], i[7]])
# print(table)
# def start_test_with_duration(Consumer, Producer, connection, n, duration=180):
# stop_test = threading.Event() # Flag to signal when to stop the test
# def send_heartbeats_and_metrics():
# start_time = time.time()
# i = 0
# while not stop_test.is_set() and time.time() - start_time < duration:
# messages = Consumer.poll(timeout_ms=1000)
# for tp, message_batch in messages.items():
# for message in message_batch:
# msg = message.value.decode('utf8').replace("'", '"')
# data = json.loads(msg)
# if message.topic == "heartbeat":
# print(f'heartbeat from {data["node_id"]}')
# elif message.topic == "metrics":
# print("Received metrics data:", msg)
# insert_metric(data, connection)
# print(f"Metrics for node {data['node_id']} inserted.")
# table = PrettyTable()
# table.field_names = ["node_id", "test_id", "report_id", "mean_latency", "median_latency", "min_latency", "max_latency"]
# table.add_row([
# data["node_id"],
# data["test_id"],
# data["report_id"],
# data["metrics"]["mean_latency"],
# data["metrics"]["median_latency"],
# data["metrics"]["min_latency"],
# data["metrics"]["max_latency"]
# ])
# print(table)
# i += 1
# if i == n:
# stop_test.set()
# break
# if time.time() - start_time >= duration:
# stop_test.set()
# time.sleep(1)
# test_thread = threading.Thread(target=send_heartbeats_and_metrics)
# test_thread.start()
# test_thread.join(duration)
# stop_test.set()
# test_thread.join()
# print("\nTest completed. You may now select other options.\n")
# def orchestrar(Consumer, Producer, Admin, connection, n):
# while True:
# print("Choose Option -")
# print("[1] : Start Test")
# print("[2] : Print Metrics")
# print("[3] : Change Number of Driver Nodes")
# print("[-1] : Exit")
# option = input("")
# if option == "1":
# print("Prepping test_config")
# test_config_mesg = make_config()
# test_config_mesg_str = json.dumps(test_config_mesg)
# Producer.send("test_config", test_config_mesg_str.encode("utf-8"))
# start = input("Trigger Test ? (y/n) : ")
# if start.lower() == 'y':
# print("Triggering Test")
# trigger_req = make_trigger()
# trigger_req_str = json.dumps(trigger_req)
# Producer.send("trigger", trigger_req_str.encode("utf-8"))
# # Start test with a fixed duration and wait until completion
# start_test_with_duration(Consumer, Producer, connection, n, duration=180)
# else:
# print("Test aborted by user.")
# continue
# elif option == "2":
# test_id = input("Enter test_id : => ")
# showstats(test_id, connection)
# continue
# elif option == "-1":
# print("Exiting Application .... ")
# exitcode(Admin, connection)
# return "exit"
# elif option == "3":
# return "done"
# else:
# print("Invalid option selected. Please choose a valid option.")
from kafka.admin import KafkaAdminClient, NewTopic
from kafka import KafkaProducer, KafkaConsumer
from kafka.errors import *
import random
import sys
import json
import mysql.connector
from mysql.connector import Error
from prettytable import PrettyTable
import threading
import time
def maketopics():
Admin = KafkaAdminClient(bootstrap_servers="localhost:9092", client_id="Orchestrar")
print("Creating Topics")
topic_names = ['test_config', 'heartbeat', 'register', 'metrics', 'trigger']
for topic in topic_names:
NewTopic(name=topic, num_partitions=3, replication_factor=3)
print("Topics Created")
return Admin
def exitcode(Admin, connection):
topic_names = ['test_config', 'heartbeat', 'register', 'metrics', 'trigger']
Admin.delete_topics(topics=topic_names)
cursor = connection.cursor()
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
exit()
def make_config():
test_config_mesg = {
"test_id": 0,
"test_type": "",
"test_message_delay": 0.00,
"throughput_per_driver": 0
}
test_config_mesg["test_id"] = str(random.randint(100000, 999999))
tp = int(input("Enter the Desired Throughput Per Driver : "))
test_config_mesg["throughput_per_driver"] = str(tp)
TOT = input("Select Type of Test: (A) Avalanche / (T) Tsunami : ")
if TOT == "A":
test_config_mesg["test_type"] = "AVALANCHE"
elif TOT == "T":
test_config_mesg["test_type"] = "TSUNAMI"
delay = input("Enter delay : ")
test_config_mesg["test_message_delay"] = delay
return test_config_mesg
def make_trigger():
trigger_req = {
"test_id": "",
"trigger": "YES"
}
trigger_req["test_id"] = str(random.randint(100000, 999999))
return trigger_req
def kafka_setup():
print("Setting Up Kafka Infrastructure")
Producer = KafkaProducer(bootstrap_servers="localhost:9092")
Consumer = KafkaConsumer()
Consumer.subscribe(["register", "heartbeat", "metrics"])
return Consumer, Producer
def Registering(Consumer, n, connection):
print("Registering Nodes ...")
nodeslist = []
i = n
print("Start your Driver Nodes ...")
for message in Consumer:
message = message.value.decode('utf8').replace("'", '"')
node = json.loads(message) # Adjusted JSON decoding without [1:-1]
nodeslist.append(node)
print("Registered Node : " + node["node_id"])
i -= 1
if i == 0:
break
print(f'{n} nodes registered ..')
return
def setup_db():
connection_config_dict = {
'user': 'root',
'password': 'Naren@123',
'host': 'localhost',
'database': 'bd_proj'
}
connection = mysql.connector.connect(**connection_config_dict)
return connection
def insert_metric(data, connection):
try:
if connection.is_connected():
cursor = connection.cursor()
create_table_query = """
CREATE TABLE IF NOT EXISTS metrics(
id INT AUTO_INCREMENT PRIMARY KEY,
node_id VARCHAR(255) NOT NULL,
test_id VARCHAR(255) NOT NULL,
report_id VARCHAR(255) NOT NULL,
mean_latency DOUBLE,
median_latency DOUBLE,
min_latency DOUBLE,
max_latency DOUBLE,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
cursor.execute(create_table_query)
connection.commit()
insert_metric_query = """
INSERT INTO metrics (node_id, test_id, report_id, mean_latency, median_latency, min_latency, max_latency)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
metric_data = (data["node_id"], data["test_id"], data["report_id"],
data["metrics"]["mean_latency"], data["metrics"]["median_latency"],
data["metrics"]["min_latency"], data["metrics"]["max_latency"])
cursor.execute(insert_metric_query, metric_data)
connection.commit()
print(f"Inserted metrics data for node {data['node_id']} into the database.") # Debugging insertion
except Error as e:
print("Error while connecting to MySQL", e)
def init_orch():
print("Starting ... ")
print("Importing Libraries ... ")
Admin = maketopics()
Consumer, Producer = kafka_setup()
connection = setup_db()
return Consumer, Producer, Admin, connection
def showstats(test_id, connection):
if connection.is_connected():
cursor = connection.cursor()
print(f"Executing query for test_id {test_id}") # Debugging query execution
cursor.execute("SELECT * FROM bd_proj.metrics WHERE test_id = %s ", (str(test_id),))
result = cursor.fetchall()
print(f"test_id : {test_id}")
table = PrettyTable()
table.field_names = ["node_id", "report_id", "mean_latency", "median_latency", "min_latency", "max_latency"]
for i in result:
table.add_row([i[1], i[3], i[4], i[5], i[6], i[7]])
print(table)
def orchestrar(Consumer, Producer, Admin, connection, n):
while True:
print("Choose Option -")
print("[1] : Start Test")
print("[2] : Print Metrics")
print("[3] : Change Number of Driver Nodes")
print("[-1] : Exit")
option = input("")
if option == "1":
print("Prepping test_config")
test_config_mesg = make_config()
test_config_mesg_str = json.dumps(test_config_mesg)
Producer.send("test_config", test_config_mesg_str.encode("utf-8"))
start = input("Trigger Test ? (y/n) : ")
if start.lower() == 'y':
print("Triggering Test")
trigger_req = make_trigger()
trigger_req_str = json.dumps(trigger_req)
Producer.send("trigger", trigger_req_str.encode("utf-8"))
print("Receiving heartbeats and metrics...")
for message in Consumer:
msg = message.value.decode('utf8').replace("'", '"')
data = json.loads(msg)
if message.topic == "heartbeat":
print(f'heartbeat from {data["node_id"]}')
elif message.topic == "metrics":
print("Received metrics data:", msg)
insert_metric(data, connection)
print(f"Metrics for node {data['node_id']} inserted.")
table = PrettyTable()
table.field_names = ["node_id", "test_id", "report_id", "mean_latency", "median_latency", "min_latency", "max_latency"]
table.add_row([
data["node_id"],
data["test_id"],
data["report_id"],
data["metrics"]["mean_latency"],
data["metrics"]["median_latency"],
data["metrics"]["min_latency"],
data["metrics"]["max_latency"]
])
print(table)
else:
print("Test aborted by user.")
continue
elif option == "2":
test_id = input("Enter test_id : => ")
showstats(test_id, connection)
continue
elif option == "-1":
print("Exiting Application .... ")
exitcode(Admin, connection)
return "exit"
elif option == "3":
return "done"
else:
print("Invalid option selected. Please choose a valid option.")