def getNodeSucc(self): if not self.node_list: s = SystemException() s.message = "Finger table does not exist" raise s node = self.node_list[0] return node
def setFingertable(self, node_list): try: self.node_list = node_list except: s = SystemException() s.message = "Cannot set finger tables" raise s
def writeFile(self, rFile): print('writeFile') print('Content: {}'.format(rFile.content)) if not rFile.meta.contentHash: hash_input = rFile.meta.owner + ':' + rFile.meta.filename hash_input = hash_input.encode('utf-8') rFile.meta.contentHash = hashlib.sha256(hash_input).hexdigest() owner_node = self.findSucc(rFile.meta.contentHash) if owner_node.id != self.node_id.id: exception = SystemException() exception.message = 'Node {}:{} does not own file {}:{}'.format(self.node_id.ip, \ self.node_id.port, \ rFile.meta.owner, \ rFile.meta.filename) raise exception if rFile.meta.contentHash in self.files: self.files[rFile.meta.contentHash].version += 1 else: self.files[rFile.meta.contentHash] = rFile.meta # next line is in case a version was sent from the client self.files[rFile.meta.contentHash].version = 0 print('Meta data:') print(self.files[rFile.meta.contentHash]) file_name = rFile.meta.owner + '_' + rFile.meta.filename with open(file_name, 'w') as server_file: server_file.write(rFile.content)
def readFile(self, filename, owner): """ Parameters: - filename - owner """ print("\n\nreadFile({},{})".format(filename, owner)) fileToRead = None fileSHA = decode_sha256(owner + ":" + filename) fileServer = self.findSucc(key=fileSHA) # print ("fileServer = {} ".format(fileServer.port)) if self.nodeId == fileServer: if filename in self.rFiles: self.rFilesLock.acquire() existingFile = self.rFiles[filename] existingSHA = decode_sha256(existingFile.meta.owner + ":" + existingFile.meta.filename) if fileSHA == existingSHA: fileToRead = existingFile self.rFilesLock.release() else: self.rFilesLock.release() raise SystemException( message='Read Failed: Permission denied.....') else: # create raise SystemException(message='Read Failed: File not found') else: raise SystemException( message='Read Failed : Server does not own the file') # print("Completed readFile({},{})".format(filename, owner)) return fileToRead
def writeFile(self, rFile): self.rFile = rFile if (self.rFile.meta.filename == None or self.rFile.meta.owner == None): x = SystemException("\nFilename and owner cannot be 'None'.") raise x # Calculate Hash Value of owner and filename fileid = self.calculate_hash(self.rFile.meta.owner, self.rFile.meta.filename) # Find Successor of the File id node = self.findSucc(fileid) # If Successor node id matches current node id, write file if self.node.id == node.id: if self.dictionary.has_key(fileid): self.dictionary[fileid].meta.version = int( self.dictionary[fileid].meta.version) + 1 self.dictionary[fileid].content = self.rFile.content self.dictionary[fileid].meta.contentHash = self.calculate_hash( None, None, self.rFile.content) else: self.rFile.meta.version = 0 self.rFile.meta.contentHash = self.calculate_hash( None, None, self.rFile.content) self.dictionary[fileid] = self.rFile print "\nFile %s written on node %s:%d" % ( self.rFile.meta.filename, self.ip_address, self.port_no) else: x = SystemException( "\nCurrent node does not own the file id. %s not written. The file id is owned by node %s:%d" % (self.rFile.meta.filename, node.ip, node.port)) raise x
def writeFile(self, rFile): """ Parameters: - rFile """ if DEBUG: print("Inside writeFile ...") file_key = calculate_hash(rFile.meta.filename) file_node = self.findSucc(file_key) if DEBUG: print("Succ: {0}:{1}".format(file_node.ip, file_node.port)) if file_node != self.current_node: self.is_file_owned = False elif self.is_same_node and file_node == self.current_node: self.is_file_owned = True if self.is_file_owned: self.createFile(rFile.meta.filename, rFile.content) self.is_file_owned = False else: exception = SystemException() exception.message = "This Server Node is not Finger Table Empty" raise exception return 0
def writeFile(self, rFile): self.curr_node = getCurrNode() node = self.findSucc(rFile.meta.contentHash) if node.port == self.node.port and node.ip == self.node.ip: if not rFile in rfile_list: temp = RFile() tempMeta = RFileMetadata() tempMeta.filename = rFile.meta.filename tempMeta.version = 0 tempMeta.contentHash = rFile.meta.contentHash temp.content = rFile.content temp.meta = tempMeta rfile_list.append(temp) else: for i in rfile_list: if i.meta.filename == rFile.meta.filename: ver = i.meta.version rfile_list.remove(i) break temp = RFile() tempMeta = RFileMetadata() tempMeta.filename = rFile.meta.filename tempMeta.version = ver + 1 tempMeta.contentHash = rFile.meta.contentHash temp.content = rFile.content temp.meta = tempMeta rfile_list.append(temp) else: s = SystemException() s.message = "Key not associated" raise s
def findSucc(self, key): """ Parameters: - key """ if DEBUG: print("Inside findSucc ...") if len(self.node_list) > 0: pred_node = self.findPred(key) if pred_node.id == key: return pred_node elif pred_node == self.current_node: if self.is_same_node: return self.current_node else: succ_node = self.node_list[0] return succ_node else: succ_node = self.get_server_node(pred_node.ip, pred_node.port, key, False) if succ_node == self.current_node: self.is_file_owned = True else: self.is_file_owned = False return succ_node else: exception = SystemException() exception.message = "Finger Table is Empty" raise exception return 0
def getNodeSucc(self): print('getNodeSucc') if self.finger_table: return self.finger_table[0] else: exception = SystemException() exception.message = 'Node {}:{} does not have a fingertable!'.format(self.node_id.ip, \ self.node_id.port) raise exception
def checkFingerTable(self): check_fingertable = True # if length is o - raise exception if (len(self.finger_table) == 0): systemException = SystemException() systemException.message = "Finger table not set properly at server side" raise systemException return False return check_fingertable
def findPred(self, key_identifier): # Raise exception in case of empty finger table, otherwise return predecessor node pred_node_id = self.current_node_id if len(self.finger_table_node_list) != 0: # Logic to identify predecessor node for given key identifier power_num = math.pow(2, 256) current_node_hash_num = int(self.current_node_id.id, 16) key_num = int(key_identifier, 16) finger_table_hash_num = int(self.finger_table_node_list[0].id, 16) if current_node_hash_num > finger_table_hash_num: finger_table_hash_num += power_num if key_num < current_node_hash_num: key_num += power_num # Check if Key falls in the range of first entry of Finger Table if current_node_hash_num < key_num <= finger_table_hash_num: pred_node_id = self.current_node_id else: # Find closest preceding finger table entry for i in range(len(self.finger_table_node_list) - 1, -1, -1): current_node_hash_num = int(self.current_node_id.id, 16) key_num = int(key_identifier, 16) finger_table_hash_num = int( self.finger_table_node_list[i].id, 16) if current_node_hash_num > finger_table_hash_num: finger_table_hash_num += power_num if key_num < current_node_hash_num: key_num += power_num if current_node_hash_num < finger_table_hash_num < key_num and \ self.current_node_id.id != self.finger_table_node_list[i].id: # Create new client to get predecessor node new_client_filestore = self._createFileStoreClient( self.finger_table_node_list[i].ip, self.finger_table_node_list[i].port) return new_client_filestore.findPred(key_identifier) if DEBUG_FLAG: print( "Inside FindPred ==> Key is not referenced by Finger Table." ) else: findPred_system_ex = SystemException() findPred_system_ex.message = ERROR_MISSING_NODE_FINGERTABLE + \ self.current_node_id.ip + COLON_STRING + \ str(self.current_node_id.port) raise findPred_system_ex if DEBUG_FLAG: print("Inside FindPred ==> Return Pred Node Id = %s" % pred_node_id.id) return pred_node_id
def getNodeSucc(self): # Raise exception in case of empty finger table, otherwise return first entry of list if len(self.finger_table_node_list) != 0: return self.finger_table_node_list[0] else: getNodeSucc_system_ex = SystemException() getNodeSucc_system_ex.message = ERROR_MISSING_NODE_FINGERTABLE + \ self.current_node_id.ip + COLON_STRING + \ str(self.current_node_id.port) raise getNodeSucc_system_ex
def readFile(self, filename, file_owner): hash = hashlib.sha256(filename +\ ":" + file_owner).hexdigest() succ = self.findSucc(hash) if(succ.id != self.myNode.id): raise SystemException("Cannot Read: Server does not own this file!") for obj in self.fileData: if obj.meta.contentHash == hash: return obj raise SystemException("Hash not found: Check owner or file name!")
def readFile(self, filename): self.curr_node = getCurrNode() node = self.findSucc(sha256(filename).hexdigest()) if node.port == self.node.port and node.ip == self.node.ip: for i in rfile_list: if i.meta.filename == filename: return i else: s = SystemException() s.message = "No file exist with the name " + filename raise s
def getNodeSucc(self): if DEBUG: print("Inside getNodeSucc ...") # it should be node before this node not 1st node server_node = None if len(self.node_list) > 0: server_node = self.node_list[0] else: exception = SystemException() exception.message = "Finger Table is Empty" raise exception return server_node
def getNodeSucc(self): if self.finger_table == None: x = SystemException( "\nFinger table for current node does not exist.") raise x return self.finger_table[0]
def findPred(self, key): # we have to check if the fingertable is properly set if len(self.fingertable) == 0: raise SystemException( "Fingertable is not properly set for this node, {}".format( self.nodeid_obj)) nodehash = int(self.nodeid_obj.id.decode('utf-8'), 16) tempkey = key key = int(key, 16) list_len = len(self.fingertable) for i in range(list_len - 1, 0, -1): curr_node_key = int(self.fingertable[i].id.decode('utf-8'), 16) cond1 = curr_node_key > nodehash and curr_node_key < key cond2 = curr_node_key < key and key < nodehash cond3 = curr_node_key > nodehash and key < nodehash if cond1 or cond2 or cond3: client, transport = get_client_and_transport_objs( self.fingertable[i]) transport.open() true_pred = client.findPred(tempkey) transport.close() return true_pred return self.nodeid_obj
def getNodeSucc(self): # Returns the closest node to this node (this is stored at the first entry of fingertable)" # Raise a SystemException if the table has not been initialized if len(self.fingerTable) == 0: raise SystemException( "The fingertable has not been properly initialized") else: return self.fingerTable[0]
def findSucc(self, key): if not self.node_list: s = SystemException() s.message = "Finger Table does not exist" raise s self.curr_node = getCurrNode() self.curr_node = self.findPred(key) if self.node.ip == self.curr_node.ip and self.node.port == self.curr_node.port: return self.getNodeSucc() transport = TSocket.TSocket(self.curr_node.ip, self.curr_node.port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) tNode = FileStore.Client(protocol) transport.open() targetNode = tNode.getNodeSucc() transport.close() return targetNode
def readFile(self, filename, owner): fileKey = sha256(owner + ':' + filename).hexdigest() # Look for the file in my files, serve if found, raise exception if not found if fileKey in self.myFiles: return self.myFiles[fileKey] else: raise SystemException( "Cannot find file with name {}:{}. This node may not own this file" .format(owner, filename))
def findPred(self, key): if not len(self.finger_table): raise SystemException("Find Pred Error - Finger table is missing") if self.NodeID >= self.finger_table[0].id: if not (key <= self.NodeID and key > self.finger_table[0].id): n = NodeID() n.id = self.NodeID n.ip = self.ip n.port = int(self.port_num) return n else: if key > self.NodeID and key <= self.finger_table[0].id: n = NodeID() n.id = self.NodeID n.ip = self.ip n.port = int(self.port_num) return n if self.NodeID >= key: for i in reversed(self.finger_table): if not (i.id <= self.NodeID and i.id > key): t = TSocket.TSocket(i.ip, i.port) t = TTransport.TBufferedTransport(t) p = TBinaryProtocol.TBinaryProtocol(t) c = FileStore.Client(p) t.open() k = c.findPred(key) t.close() return k raise SystemException("Find Pred Error - Cannot find pred 1") else: for i in reversed(self.finger_table): if i.id > self.NodeID and i.id <= key: t = TSocket.TSocket(i.ip, i.port) t = TTransport.TBufferedTransport(t) p = TBinaryProtocol.TBinaryProtocol(t) c = FileStore.Client(p) t.open() k = c.findPred(key) t.close() return k raise SystemException("Find Pred Error - Cannot find pred 2")
def readFile(self, filename, owner): key = hashlib.sha256(owner + ":" + filename).hexdigest() node = self.findSucc(key) if not node.id == self.NodeID: raise SystemException("Write File Error - Server does not own fil`s id") found = False for my_file in self.files: if filename == my_file.meta.filename and owner == my_file.meta.owner: found = True f = RFile() m = RFileMetadata() m.filename = filename m.version = my_file.meta.version m.owner = owner m.contentHash = my_file.meta.contentHash f.meta = m f.content = my_file.content return f if not found: raise SystemException("Read File Error - File not found")
def findPred(self, key): if self.finger_table == None: x = SystemException( "\nFinger table for current node does not exist.") raise x # Get preceding node if (int(self.node.id, 16) < int(key, 16)): n_prime = self.closest_preceding_finger(key, True) else: n_prime = self.closest_preceding_finger(key, False) return n_prime
def setFingertable(self, node_list_input): # Raise exception in case of empty input node list, otherwise set finger table node list if len(node_list_input) != 0: self.finger_table_node_list = node_list_input else: setFingertable_system_ex = SystemException() setFingertable_system_ex.message = ERROR_FINGERTABLE_SYSTEM_EXCP_MSG + \ self.current_node_id.ip + COLON_STRING + \ str(self.current_node_id.port) raise setFingertable_system_ex print(SUCCESS_SET_FINGER_TABLE_MSG) if DEBUG_FLAG: for node_id in self.finger_table_node_list: print("Node Info --> " + node_id.id + " : " + node_id.ip + " : " + str(node_id.port)) print( "------------------------------------------------------------------------------------------\n" )
def findSucc(self, key): if len(self.fingerTable) == 0: raise SystemException("no finger table") n = self.findPred(key) if not n.id == self.currentNode.id: transport, client = createClient(n.ip, n.port) s = client.getNodeSucc() transport.close() return s else: return self.getNodeSucc()
def readFile(self, filename, owner): print('readFile') hash_input = owner + ':' + filename hash_input = hash_input.encode('utf-8') file_hash = hashlib.sha256(hash_input).hexdigest() owner_node = self.findSucc(file_hash) if owner_node.id != self.node_id.id: exception = SystemException() exception.message = 'Node {}:{} does not own file {}:{}'.format(self.node_id.ip, \ self.node_id.port, \ owner, \ filename) raise exception if file_hash in self.files: file_name = owner + '_' + filename rFile = RFile() with open(file_name, 'r') as server_file: rFile.content = server_file.read() rFile.meta = self.files[file_hash] print('Returning file:') print(rFile) return rFile else: exception = SystemException() exception.message = 'File {} owned by {} does not exist!'.format( filename, owner) raise exception
def findPred(self, key): print('findPred') next_node = None key_num = int(key, 16) if self.finger_table: print('Checking if current node is predeccesor') if is_between(int(self.node_id.id, 16), int(self.finger_table[0].id, 16), key_num): # Means current node is predecessor # Base case return self.node_id found_predecessor = False print('Checking if key is between nodes in fingertable') for node in self.finger_table: if next_node is not None and is_between(int(next_node.id, 16), \ int(node.id, 16), key_num): found_predecessor = True break next_node = node if found_predecessor is False: # Node was not in between any two nodes in finger table. Need to check last node then print('Using last node in finger table') next_node = self.finger_table[-1] assert next_node.id != self.node_id.id print('Next node {}:{}'.format(next_node.ip, next_node.port)) transport = TSocket.TSocket(next_node.ip, next_node.port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = FileStore.Client(protocol) transport.open() predecessor_node = client.findPred(key) print('Node returned fron next node:') print(predecessor_node) transport.close() return predecessor_node else: exception = SystemException() exception.message = 'Node {}:{} does not have a fingertable!'.format(self.node_id.ip, \ self.node_id.port) raise exception
def writeFile(self, rFile): """ Parameters: - rFile """ # print ("\n\nwriteFile({})".format(rFile)) fileSHA = decode_sha256(rFile.meta.owner + ":" + rFile.meta.filename) fileServer = self.findSucc(key=fileSHA) # print ("fileServer = {} ".format(fileServer.port)) if self.nodeId == fileServer: self.rFilesLock.acquire() print("Writing to this server....") if rFile.meta.filename in self.rFiles: existingFile = self.rFiles[rFile.meta.filename] existingSHA = decode_sha256(existingFile.meta.owner + ":" + existingFile.meta.filename) if fileSHA == existingSHA: # Edit existingFile.content = rFile.content existingFile.meta.version = existingFile.meta.version + 1 existingFile.meta.contentHash = decode_sha256( rFile.content) else: self.rFilesLock.release() raise SystemException( message='Write Failed: File name already exists ') else: # create meta = RFileMetadata(filename=rFile.meta.filename, version=0, owner=rFile.meta.owner, contentHash=decode_sha256(rFile.content)) self.rFiles[rFile.meta.filename] = RFile(meta=meta, content=rFile.content) self.rFilesLock.release() else: raise SystemException( message='Write Failed: Server does not own the file')
def sendToCoord(self, command: str): logger.debug(f'Receiver command "{command}"') """ get 1 one put 1 10 quorum """ tokens = command.split(' ') key = int(tokens[1]) constLevel = tokens[-1].upper() if not 0 <= key <= 255: raise SystemException('Keys are out of range [0, 255]') if constLevel != 'ONE' and constLevel != 'QUORUM': raise SystemException(f'ConstLevel should be either "ONE" or "QUORUM"') if tokens[0] == 'put' and len(tokens) == 4: return self.coordinator.clientPut(ClientRequest(constLevel, KV(key, tokens[2]))) if tokens[0] == 'get' and len(tokens) == 3: return self.coordinator.clientGet(ClientRequest(constLevel, KV(key))) else: raise SystemException('Bad request format')
def writeFile(self, rFileNodeIn): hash_object = hashlib.sha256( (rFileNodeIn.meta.filename).encode()).hexdigest() # print("\nWrite - " + rFileNodeIn.meta.filename + ", hash code = " + hash_object) fileServerNodeSucc = self.findSucc(hash_object) # print("\nWrite - fileServerNodeSucc = " + fileServerNodeSucc.id) if (fileServerNodeSucc.id != self.serverNode.id): systemException = SystemException() systemException.message = ("File not owned by Server " + self.serverNode.ip + ":" + str(self.serverNode.port) + "\n") raise systemException if hash_object not in self.file_info: self.file_info[hash_object] = ( rFileNodeIn.meta.filename, 0, rFileNodeIn.content, hashlib.sha256((rFileNodeIn.content).encode()).hexdigest()) else: version = self.file_info[hash_object][1] self.file_info[hash_object] = ( rFileNodeIn.meta.filename, version + 1, rFileNodeIn.content, hashlib.sha256((rFileNodeIn.content).encode()).hexdigest())