-
Notifications
You must be signed in to change notification settings - Fork 1
/
jooServer.py
117 lines (93 loc) · 3.23 KB
/
jooServer.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
import socket
import time
import traceback
import numpy as np
from datetime import datetime
import torch
from termcolor import colored
import statistics as stat
# Local import
from postgresqlJoins import PostgresqlJoins
from util import databases
from util import joinHelper as jh
from datetime import datetime
def printColor(s):
print("\033[0;31m" + s + "\033[0m\n")
database = databases.ErgastF1()
jh.setTableData(database.tables, database.relations)
current_time = datetime.now().strftime("%H:%M:%S")
print("Start Time =", current_time)
# Fix seed for reproducibility
seed = 42
np.random.seed(seed)
torch.manual_seed(seed)
class JooServer:
prefix = "template"
HOST = '127.0.0.1'
PORT = 17342
nextOrder = 0
def __init__(self) -> None:
timestr = time.strftime("%m-%d_%H-%M-%S")
self.joinLog = open("servers/" + self.prefix + "_" + timestr + ".log", 'w')
def choose(self, tables, cardinalities, logData):
orders = jh.generateJoinOrders(sorted(tables))
result = orders[self.nextOrder]
self.nextOrder += 1
if self.nextOrder == 105:
self.nextOrder = 0
print("Next join order:", self.nextOrder)
return result
def onAccept(self, conn):
logData = []
# read data
data = b'' + conn.recv(1024)
splitpoint = data.index(0) # seperator between tables and weights
while splitpoint < 0:
data = data + conn.recv(1024)
splitpoint = data.index(0)
# parse table names
namesStr = data[:splitpoint].decode('utf-8')
tables = namesStr.split(";")
# read cardinalities
data = data[splitpoint + 1:]
number_of_relations = len(tables)
expectedLen = 8 * ((1 << number_of_relations) - 1 - number_of_relations)
# read until all data arrived
while len(data) < expectedLen:
data = data + conn.recv(1024)
# parse cardinalities
cardinalities = np.frombuffer(data, dtype=np.int64).tolist()
print("Features:")
print(tables, cardinalities)
logData.append(tables)
logData.append(cardinalities)
order = self.choose(tables, cardinalities, logData)
logString = ";".join(str(x) for x in logData)
self.joinLog.write(logString)
self.joinLog.write("\n")
self.joinLog.flush()
print("Log:", colored(logString, 'yellow'))
print("Order choosen " + str(order))
idString = jh.toIntString(order, tables)
print(idString)
conn.send(np.array(idString, dtype=np.int16).tobytes())
def run(self):
# open server socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((self.HOST, self.PORT))
s.listen()
print("Server started")
try:
while True:
print("Listening for connection")
# wait for connection
conn, addr = s.accept()
self.onAccept(conn)
except Exception as e:
print(e)
traceback.print_exc()
s.close()
if __name__ == "__main__":
print("Running dummy server")
server = JooServer()
server.run()