-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn.py
144 lines (110 loc) · 5.53 KB
/
spawn.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
"""
This file spawns n processes as dynamo nodes. A client can contact any of these nodes and save/get a database.
"""
import attr
from typing import List, Tuple, Dict
import grpc
from concurrent import futures
from dynamo_pb2_grpc import add_DynamoInterfaceServicer_to_server
from threading import Thread
from structures import Params, Process, NetworkParams
from dynamo_node import DynamoNode
from partitioning import init_membership_list
import time
import multiprocessing
import datetime
import logging
_ONE_DAY = datetime.timedelta(days=1)
def _wait_forever(server):
try:
while True:
time.sleep(_ONE_DAY.total_seconds())
except KeyboardInterrupt:
server.stop(None)
def start_process_multiprocess(n_id, port, view, membership_information, params, network_params, logger):
logger.info(f"Starting process {n_id} at port {port} with params: {params}")
SERVER_ADDRESS = f"localhost:{port}"
server = grpc.server(futures.ThreadPoolExecutor())
add_DynamoInterfaceServicer_to_server(DynamoNode(
n_id=n_id,
view=view,
membership_information=membership_information,
params=params,
network_params=network_params,
logger=logger),
server)
server.add_insecure_port(SERVER_ADDRESS)
logger.info(f"Python GRPC server running at {SERVER_ADDRESS}")
server.start()
_wait_forever(server)
def start_process(n_id, port, view, membership_information, params, network_params, logger):
logger.info(f"Starting process {n_id} at port {port} with params: {params}")
SERVER_ADDRESS = f"localhost:{port}"
server = grpc.server(futures.ThreadPoolExecutor())
add_DynamoInterfaceServicer_to_server(DynamoNode(
n_id=n_id,
view=view,
membership_information=membership_information,
params=params,
network_params=network_params,
logger=logger),
server)
server.add_insecure_port(SERVER_ADDRESS)
logger.info(f"Python GRPC server running at {SERVER_ADDRESS}")
server.start()
return Process(ip="localhost", port=port, server=server)
def create_view(start_port, num_proc) -> Dict[int, int]:
'''
Get key value pair to each process in the dynamo ring.
Returns address to each instance.
'''
view = {}
for i in range(num_proc):
view[i] = start_port
start_port += 1
return view
def start_db(params: Params, membership_information: Dict[int, List[int]], network_params: NetworkParams = None, wait=False, start_port: int = 2333, logger: logging.Logger = None):
"""
Spawns n servers in different threads and these servers act as dynamo instances
TODO: convert to processes.
"""
processes : List[Process] = []
view = create_view(start_port=start_port, num_proc=params.num_proc)
logger.info(f"Membership Info {membership_information} Number of processes {params.num_proc}")
for i in range(params.num_proc):
process = start_process(i, view[i], view, membership_information, params, network_params, logger)
processes.append(process)
# ending condition
if wait:
processes[-1].server.wait_for_termination()
return processes
def start_db_multiprocess(params: Params, membership_information: Dict[int, List[int]], network_params: NetworkParams = None, wait=False, start_port: int = 2333, logger = None):
"""
Spawns n servers in different threads and these servers act as dynamo instances
TODO: convert to processes.
"""
processes : List[Process] = []
view = create_view(start_port=start_port, num_proc=params.num_proc)
logger.info(f"Membership Info {membership_information} Number of processes {params.num_proc}")
for i in range(params.num_proc):
process = multiprocessing.Process(target=start_process_multiprocess, args=(i, view[i], view, membership_information, params, network_params, logger))
process.start()
processes.append(process)
for p in processes:
p.join()
def start_db_background(params: Params, membership_information: Dict[int, List[int]], network_params: NetworkParams, logger: logging.Logger, num_tasks:int = 2, wait: bool = False, start_port: int = 2333):
executor = futures.ThreadPoolExecutor(max_workers=num_tasks)
server = executor.submit(start_db, params, membership_information, network_params, wait, start_port, logger)
return server
def start_db_background_multiprocess(params: Params, membership_information: Dict[int, List[int]], network_params: NetworkParams, logger, num_tasks:int = 2, wait: bool = False, start_port: int = 2333):
executor = futures.ThreadPoolExecutor(max_workers=num_tasks)
server = executor.submit(start_db_multiprocess, params, membership_information, network_params, wait, start_port, logger)
return server
def init_server(params, network_params):
membership_information = init_membership_list(params)
logging.basicConfig(filename='dynamo_node.log', level=logging.DEBUG)
logger = logging.getLogger('dynamo_node')
logger.setLevel(logging.DEBUG)
logger.debug("Testing...")
start_db_background_multiprocess(params, membership_information, network_params, logger)
# start_db_background(params, membership_information, network_params, wait=True, logger=logger)