-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
198 lines (155 loc) · 8.26 KB
/
Copy pathworker.py
File metadata and controls
198 lines (155 loc) · 8.26 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
"""The Python implementation of the GRPC mapreduce worker."""
from __future__ import print_function
import random
import argparse
import logging
import time
import re
import os
import sys
from collections import Counter
from typing import List
import grpc
import mapreduce_pb2
import mapreduce_pb2_grpc
from pathlib import Path
abs_path = Path(__file__).resolve().parent
node_name = 'Worker ' + str(os.getpid())
TASK_TYPES = {0: 'map', 1: 'reduce'}
class Worker:
def __init__(self):
self.channel = grpc.insecure_channel("localhost:50051")
self.stub = mapreduce_pb2_grpc.DriverStub(self.channel)
logging.info("Worker up", extra={'node': node_name})
# Wait for the server to be ready (adjust the timeout as needed)
grpc.channel_ready_future(self.channel).result(timeout=None)
def notify_task_completion(self, taskId: int, taskType: int) -> None:
"""Sends a message to the Driver to notify the completion of a task
so that the Driver can keep track of completed tasks.
Args:
taskId (int): completed task ID
taskType (int): completed task type
"""
try:
logging.info("Notifying task completion...", extra={'node': node_name})
ack = self.stub.AcknowledgeTaskCompletion(mapreduce_pb2.TaskCompletion(taskId=taskId, taskType=taskType), metadata = [('sender_name', node_name)])
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.DEADLINE_EXCEEDED:
# Handle timeout exception here
logging.error("RPC Timeout: Deadline exceeded", extra={'node': node_name})
else:
# Handle other gRPC errors
logging.error(f" gRPC Error: {e}", extra={'node': node_name})
except Exception as ex:
# Handle other non-gRPC exceptions
logging.error(f"Unexpected error: {ex}", extra={'node': node_name})
def map(self, taskId: int, input_files: List[str], M: int) -> None:
"""Executes map task with the given taskId on the given target files.
Args:
taskId (int): task ID
input_files (List[str]): target files of the map operation
M (int): number of reduce tasks specified by the user
"""
logging.info(f"Executing map operation with id {taskId} on files {[os.path.basename(path) for path in input_files]}", extra={'node': node_name})
for input_file in input_files:
f = open(Path(abs_path / input_file).resolve(), 'r')
text = f.read()
# words = text.split()
words = re.findall(r'\b[a-zA-Z0-9]+\b', text)
for word in words:
bucket_id = ord(word[0]) % M
bucket_name = "mr-" + str(taskId) + "-" + str(bucket_id)
bucket_path = (abs_path / ("../data/map/" + bucket_name)).resolve()
mode = 'w' if not os.path.exists(bucket_path) else 'a'
with open(bucket_path, mode) as bucket_file:
bucket_file.write(word + '\n')
self.notify_task_completion(taskId=taskId, taskType=0)
def reduce(self, taskId: int, input_files: List[str]) -> None:
"""Executes reduce task with the given taskId on the given target files.
The target files could be inferred by looking for those with bucketId == taskId but
they can be inferred beforehand by the driver aswell since they are predefined by the taskId.
Args:
taskId (int): task ID
input_files (List[str]): target files of the reduce operation
"""
logging.info(f"Executing reduce operation with id {taskId} on files {[os.path.basename(path) for path in input_files]}", extra={'node': node_name})
words = []
for input_file in input_files:
f = open(Path(abs_path / input_file).resolve(), 'r')
words_partial = f.read().splitlines()
words.extend(words_partial)
c = Counter(words)
out_path = (abs_path / ("../data/reduce/mr-" + str(taskId))).resolve()
mode = 'w' if not os.path.exists(out_path) else 'a'
with open(out_path, mode) as out_file:
for word, count in c.most_common():
out_file.write('{} {}\n'.format(word, count))
self.notify_task_completion(taskId=taskId, taskType=1)
def run(self) -> None:
"""
Worker's loop.
Basic workflow:
1. Ping the server to check that it is up
2. Request a new task assignment
3. Complete task assignment
4. Notify about task completion
"""
while True:
try:
# first ping the server to see if it is up
pong = self.stub.PingPong(mapreduce_pb2.Ping(ping=random.randint(0, 10000)), wait_for_ready=True, timeout=10, metadata = [('sender_name', node_name)])
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.DEADLINE_EXCEEDED:
# Handle timeout exception here
logging.info("PING Timeout: Driver is not up anymore. Shutting down...", extra={'node': node_name})
break
elif e.code() == grpc.StatusCode.CANCELLED or e.code() == grpc.StatusCode.UNAVAILABLE:
# Handle cancelled connection
logging.info("Cannot reach Driver. Checking if it is still up...", extra={'node': node_name})
continue
else:
logging.error(f"gRPC Error: {e}", extra={'node': node_name})
break
except Exception as ex:
# Handle other non-gRPC exceptions
logging.error(f"Unexpected error: {ex}", extra={'node': node_name})
break
try:
# server is up so we request a new task
logging.info("Server is up. Requesting new task...", extra={'node': node_name})
taskAssignment = self.stub.AssignTask(mapreduce_pb2.TaskRequest(request=0), metadata = [('sender_name', node_name)])
# handle driver assignment
if taskAssignment.assignmentStatus == 0: # new task
logging.info(f"Received {TASK_TYPES[taskAssignment.taskType]} task assignment", extra={'node': node_name})
if taskAssignment.taskType == 0: # map task
self.map(taskAssignment.taskId, taskAssignment.input_files, taskAssignment.M)
elif taskAssignment.taskType == 1: # reduce task
self.reduce(taskAssignment.taskId, taskAssignment.input_files)
else:
logging.warning(f"Received task assignment of unknown type", extra={'node': node_name})
elif taskAssignment.assignmentStatus == 1: # no tasks available at the moment
logging.info(f"No tasks to complete right now...", extra={'node': node_name})
time.sleep(5)
else:
logging.warning(f"Received task assignment with unknown status", extra={'node': node_name})
except grpc.RpcError as e:
logging.error(f"gRPC Error: {e}", extra={'node': node_name})
break
except Exception as ex:
# Handle other non-gRPC exceptions
logging.error(f"Unexpected error: {ex}", extra={'node': node_name})
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Invoke worker')
parser.add_argument('--verbose', '-v', action="store_true", help='log system messages')
args = parser.parse_args()
logging_level = logging.DEBUG
if not args.verbose:
logging_level = logging.CRITICAL
logging.basicConfig(
format='%(asctime)s - %(node)s - %(levelname)s - %(message)s',
datefmt='%d/%m/%Y %I:%M:%S %p',
level=logging_level
)
worker = Worker()
worker.run()