def PUT(self, request): """ Create path if needed and write data to it """ logging.debug("storage.put") #request.showVariables() response = {"headers": [], "status": "201 OK", "body": ""} if not request.file_name: response['status'] = http_code['304'] return response replication = request.metadata.get("HTTP_REPLICATION", None) if replication == "1": logging.debug("REPLICATION") fm = FileManager(self.data_path + "/" + request.file_name,\ request.file_name) if not fm.write_file(request.body): response['status'] = http_code['500'] response['body'] = "Could not replicate" logging.debug("Could not replicate") return response client_id = request.metadata.get("HTTP_CLIENT_ID", "test:test") logging.debug("HTTP_CLIENT_ID: %s" % client_id) replicas_count = request.metadata.get("HTTP_REPLICAS_COUNT", 3) fm = FileManager(self.data_path + "/" + request.file_name\ , request.file_name) if not fm.write_file(request.body): response['status'] = http_code['500'] logging.debug("Starting to save data in disk") storage_replicas = make_tuple(request.metadata["HTTP_STORAGE_LIST"]) for replica in storage_replicas: if replica[0] == self.port_ip[0] and\ replica[1] == self.port_ip[1]: continue headers = {} headers['file_name'] = request.file_name headers['data_length'] = request.content_length headers['etag'] = request.file_name headers['replicas_count'] = replicas_count headers['client_id'] = "%s:%s" % (request.remote_addr, request.remote_port) headers["replication"] = "1" response = self._put_request(request.file_name, replica, data=request.body, headers=headers) logging.debug("response: %s" % response) if not "201" in response["status"]: response['status'] = http_code['500'] response['body'] = "Could reach proper number of replicas" return response logging.debug("Starting to send metadata confirmation for proxy") proxy_list = make_tuple(request.metadata["HTTP_PROXY_LIST"]) logging.debug(proxy_list) for proxy in proxy_list: logging.debug(proxy) headers = {} headers['data_length'] = request.content_length headers['etag'] = request.file_name headers['replicas_count'] = 3 headers['client_id'] = client_id response = self._post_request(request.file_name, proxy, headers=headers) logging.debug("response: %s" % response) if "201" in response["status"]: break return response
def PUT(self, request): logging.debug("proxy.put") proxy_nodes = [] storage_nodes = [] self.get_nodes(proxy_nodes, storage_nodes) print("storage_node: %s" % storage_nodes) print("proxy_node: %s" % proxy_nodes) #request.showVariables() print("Am I leader? %s" % self.group.is_leader) response = {"headers": [], "status": "200 OK", "body": ""} replication = request.metadata.get("HTTP_REPLICATION", None) if replication == "1": logging.debug("REPLICATION") fm = FileManager(self.data_path + "/" + request.file_name,\ request.file_name) if not fm.write_file(request.body): response['status'] = http_code['500'] response['body'] = "Could not replicate" logging.debug("Could not replicate") return response if len(proxy_nodes) < 3: response['status'] = http_code['304'] response['body'] = "Deny of service due server is down" print("Deny of service due server is down") logging.debug("Deny of service due server is down") return response if not self.group.is_leader: response['status'] = http_code['304'] response['body'] = "No leader. Try to find the leader" logging.debug("I'm no leader. Try to find the leader") return response if not request.file_name: response['status'] = http_code['400'] response['body'] = "No valid file name found" logging.debug("No valid file name found") return response client_id = request.metadata.get("HTTP_CLIENT_ID", "test:test") #logging.debug("HTTP_CLIENT_ID: %s" % client_id) metadata = MetadataManager(self.data_path + "/" + request.file_name, request.file_name) file_exist = metadata.restore() if len(metadata.lock_list) > 0: response['status'] = http_code['403'] response['body'] = "File is locked" logging.debug("file is locked") return response metadata.size = request.metadata["HTTP_DATA_LENGTH"] metadata.etag = request.metadata["HTTP_ETAG"] # Create File in any nodes if not file_exist: write_balancer = WriteLoadBalancer(self.data_path, storage_nodes) balanced_storage_list = write_balancer.balance() if balanced_storage_list: storage_list = balanced_storage_list else: storage_list = storage_nodes logging.debug("storage_list: %s" % storage_list) replicas_count = request.metadata.get("HTTP_REPLICAS_COUNT", 3) replica_list = storage_list[:int(replicas_count)] logging.debug("replicas_list: %s" % replica_list) metadata.storage_list = replica_list metadata.replicas_count = replicas_count # Update File in the same nodes else: replica_list = metadata.storage_list available_replicas = [] for replica in metadata.storage_list: for node in storage_nodes: if replica[0] == node[0] and replica[1] == node[1]: available_replicas.append((replica[0], replica[1])) if len(available_replicas) != len(metadata.storage_list): response['status'] = http_code['304'] response['body'] = "Deny of service due server is down" print("Deny of service due server is down") logging.debug("Deny of service due server is down") return response #if len(replica_list) < 3: #response['status'] = http_code['304'] #response['body'] = "Deny of service due server is down" #return response #fl = FileLock(self.data_path + "/" + request.file_name) #while True: #if not fl.is_locked(): #break #else: #logging.debug("file is locked. Someone is updating") #time.sleep(1) #response['status'] = http_code['403'] #response['body'] = "File is locked. Someone is updating." #return response # Cuts off not needed replicas metadata.add_lock_item(client_id, "LOCK_WRITE") metadata.save() #fl.lock() body = {} body["STORAGE_LIST"] = replica_list response['body'] = json.dumps(body) response['status'] = http_code['201'] logging.debug("response: %s" % response) return response