def rez(self, nodeName, ringMember): print "Rezzing", nodeName, getHashString(nodeName) try: Peer(nodeName).join(ringMember) self.deadNodes.remove(nodeName) except Exception as e: print "Error rezzing", e
def __init__(self,contents,hashid=None): #assumes contents are objects not jsonDUMPs if hashid is None: self.hashid = getHashString(str(contents)) # do we want a long or string? Assuming string else: self.hashid = hashid self.contents = contents
def store(self,name,val): key = getHashString(name) target = self.findSuccessor(key,False) # if fails do wut? I don't think it will try: Peer(target).put(key,val) except Exception as e: print self.name, e, "the node I tried to store in literally just died" return True
def setupExperiment(self): targets = self.aliveNodes[:] for node in targets: self.kill(node) print "Wanton destruction complete." print "alive:", self.aliveNodes print len(targets), len(self.deadNodes) time.sleep(2) print "Creating new network." n = random.choice(self.deadNodes) Peer(n).create() self.deadNodes.remove(n) ## adding the rest while len(self.aliveNodes) < len(self.deadNodes): self.rezRandom() time.sleep(MAINT_INT) print "Done." self.startChurn() print "Storing." self.choosing = True tester = random.choice(self.aliveNodes) self.safe.append(tester) self.choosing = False try: Peer(tester).storeFile(TEST_FILE) except: print "failed." traceback.print_exc(file=sys.stdout) print "terminating." return print "Store done." self.safe.pop() self.choosing = True tester = random.choice(self.aliveNodes) outputAddress = getHashString( Peer(tester).name) print outputAddress self.safe.append(tester) self.choosing = False try: Peer(tester).stage(TEST_FILE, outputAddress) except: print "failed." traceback.print_exc(file=sys.stdout) print "terminating." return print "Stage done." #self.safe.pop() #print "Churning the network before we retrieve." #time.sleep(10) """
def kill(self,victim): newPort = random.choice(PORTS) print "Killing", victim, getHashString(victim) try: oldPort = int(victim[victim.rfind(":")+1:]) newName = victim[:victim.rfind(":")+1]+str(newPort) Peer(victim).kill(newPort) self.aliveNodes.remove(victim) self.deadNodes.append(newName) PORTS.remove(newPort) PORTS.append(oldPort) #do at end except Exception as e: print "He's already dead", e
def getKeyfile(self, filename): key = getHashString(filename) tries = 0 print self.name, "getting keyfile", key while tries < 20: try: target = self.findSuccessor(key, True) keyfile = Peer(target).get(key) # why is this a dict? rpc it turns out if keyfile == "FAIL": time.sleep(3) else: return keyfile except Exception as e: print self.name, "failed to retrieve keyfile from", target, " due to" , e time.sleep(1) print self.name, "failed to retrieve keyfile from", target tries = tries + 1 return False
def startMapReduce(self,filename): key = getHashString(filename) target = self.findSuccessor(key) keyfile = Peer(target).get(key) # why is this a dict? rpc it turns out keys = keyfile['keys'] return self.doMapReduce(keys)
def retrieve(self,name): key = getHashString(name) target = self.findSuccessor(key,True) # if fails do wut? return Peer(target).get(key), target, str(Peer(target).hashid)[:6]
def __init__(self, filename): self.hashid = getHashString(filename) self.keys = [] #list of identfiers