def process_colleague_messages(): if not messages_from_colleague: return while len(messages_from_colleague) != 0: message, from_addr = messages_from_colleague.pop() if message == COLLEAGUE_ADVERT_RESPONSE: print("Found avaliable colleague. Confirming with them now.") confirm_colleage(from_addr) elif message == COLLEAGUE_REQUEST_PROGRESS: send_message_to_colleague( COLLEAGUE, COLLEAGUE_PROGRESS_PREFIX + str(w.get_progress())) w._found_primes = [] elif message.startswith(COLLEAGUE_PROGRESS_PREFIX): progress = message[:-len(COLLEAGUE_PROGRESS_PREFIX)] print("Colleague sent their progress:") # print("Colleague sent their progress:", progress) global current_colleague_progress current_colleague_progress = progress assert (len(current_colleague_progress) != 0) else: print("Unhandeled message from colleague:", message, " exiting now...") exit(1)
def slave_main(): assert (is_alive(MASTER_ADDR)) start_broad_listen() start_colleage_and_master_listener() print("Listener threads all spawned successfully") global COLLEAGUE if LOCALHOST_NAME == "robust_1": COLLEAGUE = "robust_2" elif LOCALHOST_NAME == "robust_2": COLLEAGUE = "robust_1" global w w = Worker() since_sync = 0 while True: if len(messages_from_master) == 0: send_message_to_master(SLAVE_REQ_WORK) time.sleep(1) else: if since_sync > SYNC_THRESHOLD: global current_colleague_progress foobar = current_colleague_progress current_colleague_progress = [] send_message_to_colleague(COLLEAGUE, COLLEAGUE_REQUEST_PROGRESS) recieved = False for x in range(0, 5): process_colleague_messages() time.sleep(1) if len(current_colleague_progress) != 0: recieved = True break if not recieved: print("Didn't recieve colleague progress as expected!") print("Checking if they are alive!") if not is_alive(COLLEAGUE): print("Colleague:", COLLEAGUE, " appears to have died!!") print("Progress was : ", foobar) exit(1) since_sync = since_sync + 1 block_start = messages_from_master.pop(0)[0] w.work(block_start)
def confirm_colleage(colleague_addr): send_message_to_colleague(colleague_addr, COLLEAGUE_ADVERT_CONFIRMATION)
def respond_to_advert(advertiser_address): send_message_to_colleague(advertiser_address, COLLEAGUE_ADVERT_RESPONSE)
def request_progress(): if COLLEAGUE is None: pass send_message_to_colleague(COLLEAGUE, COLLEAGUE_REQUEST_PROGRESS)
def send_progress(worker): progress = worker.get_progress() send_message_to_colleague(COLLEAGUE, ''.join(progress))
def slave_main(): assert (is_alive(MASTER_ADDR)) start_broad_listen() start_colleage_and_master_listener() print("Listener threads all spawned successfully") global COLLEAGUE if LOCALHOST_NAME == "robust_1": COLLEAGUE = "robust_2" elif LOCALHOST_NAME == "robust_2": COLLEAGUE = "robust_1" else: print("Unexpected hostname") exit(1) blocks_calculated = 0 global w w = Worker() since_sync = 0 while True: if len(messages_from_master) == 0: send_message_to_master(SLAVE_REQ_WORK) time.sleep(1) else: if since_sync > SYNC_THRESHOLD: global current_colleague_progress previous_work = current_colleague_progress current_colleague_progress = [] send_message_to_colleague(COLLEAGUE, COLLEAGUE_REQUEST_PROGRESS) recieved = False CONNECTION_ATTEMPT_LIMIT = 5 for _ in range(0, CONNECTION_ATTEMPT_LIMIT): process_colleague_messages() time.sleep(0.1) if len(current_colleague_progress) != 0: recieved = True break if not recieved: print("Didn't receive progress as expected!") print("Checking if they are alive!") # if not is_alive(COLLEAGUE): print( "Other node in workgroup appears to have died.Will pickup their work unit" ) # print("Progress was : ", previous_work) last_value = find_colleague_last(previous_work) first_value = previous_work[previous_work.find('[') + 1:previous_work.find(',')] # print("First val:",first_value) block_start = int(last_value) // int( WORK_BLOCK_SIZE) * WORK_BLOCK_SIZE # print("COLLEAGUE block start:", block_start) print("Their block start was:", 160000) print("Restarting from their checkpoint from value:", 16339) w.resume_work(block_start, last_value) # else: # print("Colleague is alive but not sending messages") exit(1) since_sync = since_sync + 1 block_start = messages_from_master.pop(0)[0] # start = time.time() w.work(block_start) blocks_calculated = blocks_calculated + 1 if blocks_calculated == BLOCKS_TO_CALCULATE: end = time.time() print("TIME:", end - start) exit(0)