def try_to_get_rows(self, socket, message):
        new_port = mt.get_available_random_port(self.addr[0])
        if not os.path.exists(message.payload["status_db_url"]):
            socket.send_string(
                str(Message().set_message(
                    'ERROR', {
                        'info': 'unable to open database file',
                        'type_error': 'FileNotFoundError'
                    })))
        new_addr = (self.addr[0], new_port)
        try:
            database = sql.connect(message.payload["status_db_url"])
        except sql.OperationalError:
            socket.send_string(
                str(Message().set_message(
                    'ERROR', {
                        'info': 'unable to open database file',
                        'type_error': "OperationalError"
                    })))
            return -1

        socket.send_string(
            str(Message().set_message("OK", {
                "ip": self.addr[0],
                "port": new_port
            })))
        p = Process(target=self.new_get_rows, args=(
            new_addr,
            message,
        ))
        p.daemon = True
        p.start()
Example #2
0
    def new_get_data(self, file_url, is_byte_data=True):
        context = zmqa.Context()
        filesystem_socket = context.socket(zmq.REQ)
        # print("Vamos a buscar los resultados")
        print(
            "Client:filesystem addr:",
            "tcp://" + self.filesystem_addr[0] + ":" + self.filesystem_addr[1])
        filesystem_socket.connect("tcp://{}:{}".format(*self.filesystem_addr))
        filesystem_socket.send_string(
            str(Message().set_message("GETDATA", {
                "byte": is_byte_data,
                "file_url": file_url
            })))

        data_sock_message = mt.loop_tool(mt.try_to_recv,
                                         filesystem_socket.recv_string)
        if data_sock_message == -1:
            print("No me respondio el filesystem")
            return -1

        if data_sock_message.message_name != "OK":
            print(
                "Client:Some error happened getting the message with the ip and port to get the result of "
                "the operation")
            print("instead, data sock message was:", str(data_sock_message))
            return 1

        data_socket = context.socket(zmq.REQ)
        data_socket.connect("tcp://{}:{}".format(
            data_sock_message.payload["ip"],
            data_sock_message.payload["port"]))
        data_socket.send_string(str(Message().set_message("OK")))
        # print("Nos preparamos para recibir linea por linea")
        eof_cha = Client._get_end_character(is_byte_data)

        result_byte = eof_cha

        temp_line = mt.loop_tool(mt.raw_try_to_recv, data_socket.recv)
        if temp_line == -1:
            print("No me respondio el filesystem")
            return -1

        while temp_line != eof_cha:
            # print("Recibimos la linea")
            data_socket.send_string(str(Message().set_message("OK")))
            result_byte += temp_line
            temp_line = mt.loop_tool(mt.raw_try_to_recv, data_socket.recv)
            if temp_line == -1:
                print("No me respondio el filesystem")
                return -1

        import pickle
        result_data = pickle.loads(result_byte)
        # print("Tenemos los resultados: ", result_data)
        return result_data
    def try_to_put_data(self, socket, message):
        print("FS: Enter PutData")
        new_port = mt.get_available_random_port(self.addr[0])
        is_a_byte_message = message.payload["byte"]
        filename = message.payload["file_url"]

        lock, err = self._check_file_lock(filename)
        if lock is None:
            print("FS: Error 403, en el putdata")
            socket.send_string(
                str(Message().set_message(
                    "Error", {
                        "info": "Someone is using the file. Cannot access.",
                        'type_error': err
                    })))
            return -1

        print("FS: Creado el lock haremos put_data")
        try:
            if is_a_byte_message:
                # data_message = data_socket.recv()
                file = open(filename, "wb")
            else:
                # data_message = Message().get_message(data_socket.recv_string())
                file = open(filename, "w")
            print("FS:File correctly open")
            m = Message().set_message("OK", {
                "ip": self.addr[0],
                "port": new_port
            })
            socket.send_string(str(m))
            print("FS:OK message sent")
        except zmq.error.ZMQError:
            socket.send_string(
                str(Message().set_message(
                    "ERROR",
                    {"info": "Something went wrong creating the file."})))
            os.remove(lock)
            print("FS: Removemos el lock: ", lock)
            return -1

        new_addr = (self.addr[0], new_port)
        p = Process(target=self.new_put_data,
                    args=(
                        new_addr,
                        file,
                        is_a_byte_message,
                        filename,
                        lock,
                    ))
        p.daemon = True
        p.start()
Example #4
0
    def try_to_connect_master(self):

        print("Client: ", 'Esperando respuesta por: ', self.listener_addr)
        while True:
            print("Client: Into the while trying to get some master")
            sock = zmqa.Context().socket(zmq.REQ)
            self._get_new_master()
            print("Client: ", "Trying to cannect to: ", self.current_master)
            sock.connect("tcp://{}:{}".format(*self.current_master))
            sock.send_string(
                str(Message().set_message(
                    "JOB", {
                        "job_url": self.job_url,
                        "job_id": self.jhash,
                        "data_type": self.data_type,
                        "client_addr": self.listener_addr,
                        "functions_url": self.files_urls["functions_url"],
                        "map_data_url": self.files_urls["data_url"],
                        "status_db_url": self.files_urls["db_url"]
                    })))

            print("Enviado el mensaje de job")
            answer = mt.loop_tool(mt.try_to_recv, sock.recv_string)
            if answer == -1:
                print("Client: ", self.current_master, " did not responded")
                self._get_new_master()
                continue

            print("Client:JOB answer:", answer)

            if answer.message_name == "OK":
                self.master_ping_addr = (answer.payload["ping_tracker_ip"],
                                         answer.payload["ping_tracker_port"])
                print("Client: ", "Sended job")
                return 0
    def new_insert_blockresult(self, new_addr, message):
        database = sql.connect(message.payload["status_db_url"])
        context = zmq.Context()
        block_result_socket = context.socket(zmq.REP)
        block_result_socket.bind("tcp://{}:{}".format(*new_addr))

        try:
            block_result_message = mt.recv_message(
                block_result_socket.recv_string, 1)
        except TimeoutError:
            print('Timeout!! en el insertblockresult')
            database.close()
            block_result_socket.close()
            return 0
        cursor = database.cursor()
        # cursor.execute('''DELETE FROM block_result WHERE result_url = "{}"'''
        #                .format(block_result_message.payload["block_id"]))
        # database.commit()

        for url in block_result_message.payload["result_url"]:
            try:
                cursor.execute(
                    '''INSERT INTO block_result VALUES ("{}", "{}")'''.format(
                        block_result_message.payload["block_id"], str(url)))
            except sql.IntegrityError:
                print("Ya existe la tupla: ", ())
        database.commit()
        block_result_socket.send_string(str(Message().set_message("OK")))
        cursor.close()
        database.close()
        block_result_socket.close()
        return 0
    def start_server(self):
        """
        Escucha lo que se manda por addr y en dependencia del mensaje, se ejecuta la función necesaria

        :param addr:
            Dirección que escuchará el worker
        :return:
            Nothing
        """
        context = zmqa.Context()
        socket = context.socket(zmq.REP)
        socket.bind("tcp://{}:{}".format(self.addr[0], self.addr[1]))

        print("FS:Running in {}".format(self.addr))
        attend_ping = Thread(target=self._attend_ping)
        attend_ping.daemon = True
        attend_ping.start()

        while True:
            print("FS: Into the WHILE")
            text = loop.run_until_complete(socket.recv_string())
            print("FS: Server received:", text)
            message = Message().get_message(text)
            function_to_execute = self.names[message.message_name]
            function_to_execute(socket, message)
    def new_update_row(self, new_addr, message):

        context = zmq.Context()
        update_socket = context.socket(zmq.REP)
        try:
            update_socket.bind('tcp://{}:{}'.format(*new_addr))
        except zmq.error.ZMQError:
            print("FS ZMQERROR")
            return -1

        try:
            update_message = mt.recv_message(update_socket.recv_string, 1)
        except TimeoutError:
            print('Timeout!! en el updaterow')
            return 0
        try:
            status_db_url = message.payload["status_db_url"]
            fields_values = update_message.payload["fields_values"]
            key = update_message.payload["key"]
            table = update_message.payload["table"]
        except KeyError:
            print("FS: Hubo KeyError en el update row")

        self._update_status_fields(fields_values, key, status_db_url, table)

        update_socket.send_string(str(Message().set_message("OK")))
        update_socket.close()
        print("FS: Hicimos el update")
        return 0
    def try_to_put_job(self, socket, message):
        print("Intentemos hacer putjob")
        job_id = message.payload["job_id"]
        # files = message.payload["files"]
        db_url = str(job_id) + "/database.sqlite3"
        # print("files to save:", files)
        try:
            os.mkdir(job_id)
        except FileExistsError:
            print(
                "FS:Ya esa tarea existe, escribiré sobre la carpeta ya creada")

        new_port = mt.get_available_random_port(self.addr[0])
        message_to_send = Message().set_message("OK", {
            "ip": self.addr[0],
            "port": new_port
        })
        socket.send_string(str(message_to_send))
        new_addr = (self.addr[0], new_port)
        p = Process(target=self.new_put_job, args=(
            new_addr,
            job_id,
            db_url,
        ))
        p.daemon = True
        p.start()
Example #9
0
    def send_job(self, data_file, function_file):
        """
        A function to put a job's data on the service's filesystem

        :param data_file:
            The url of the data file to send
        :param function_file:
            The url of the function file to send
        :param data_type:
            The type of data stored on the data file
        :return:
            0 if worked OK
        """
        print("Client: ", 'Tratamos de hacer jobreg a: ', self.current_master)
        client_context = zmqa.Context()
        register_socket = client_context.socket(zmq.REQ)
        register_socket.connect("tcp://" + self.current_master[0] + ":" +
                                self.current_master[1])

        register_message = Message().set_message(
            "JOBREG", {
                "files": [function_file, data_file],
                "extra_info": self.job_info_for_register
            })
        register_socket.send_string(str(register_message))
        answer = mt.loop_tool(mt.try_to_recv, register_socket.recv_string)
        if answer == -1:
            print("Client: ", 'No me respondio: ', self.current_master)
            register_socket.close()
            return -1
        print("Client: ", "Recibi del jobreg: ", answer)
        # print("Client:answer del JOBREG:", answer)
        if answer.message_name == "OK":
            self.jhash = answer.payload["job_id"]
            self.masters_list = answer.payload["trackers_addr"]
            # print(answer.payload["filesystem"])
            self.filesystem_addrs = answer.payload["filesystem"]
            self.filesystem_addr = self._get_new_filesystem_node()

            if "info" in answer.payload.keys():
                print("Info:", answer.payload["info"])
        else:
            print("Client:Job not correctly sent")
            return -1

        # First sends the files to the filesystem

        while True:
            r = self.send_job_data_to_fs(function_file, data_file)
            if r == -1:
                print("Client: Problem with the FS")
                #     todo: tengo que escoger otro nodo de filesystem
                self.filesystem_addr = self._get_new_filesystem_node()
            else:
                break
        register_socket.close()
        assert self.try_to_connect_master() != -1, "No hay workers disponibles"

        return 0
Example #10
0
    def new_put_data(self, current_file_url, file_url, is_byte):
        context = zmqa.Context()
        # print("Client: FS addr:", self.filesystem_addr)
        filesystem_socket = context.socket(zmq.REQ)
        filesystem_socket.connect("tcp://" + self.filesystem_addr[0] + ":" +
                                  self.filesystem_addr[1])
        # print("Client: connected to filesystem")
        message = Message().set_message("PUTDATA", {
            'file_url': file_url,
            'byte': is_byte
        })
        filesystem_socket.send_string(str(message))
        answer = mt.loop_tool(mt.try_to_recv, filesystem_socket.recv_string)
        if answer == -1:
            print("Client: Putdata answer timed out!")
            filesystem_socket.close()
            return -1
        if answer.message_name != 'OK':
            print("Client: No me devolvieron OK en el put data")
            filesystem_socket.close()
            return 1
        filesystem_socket.close()
        data_addr = answer.payload["ip"] + ":" + answer.payload["port"]
        putdata_context = zmqa.Context()
        putdata_socket = putdata_context.socket(zmq.REQ)
        # print("Client: data addr:", data_addr)
        putdata_socket.connect("tcp://" + data_addr)
        # print("Client:putdata socket connected to " + data_addr)
        # en este momento mandamos el data
        try:
            with open(current_file_url, 'r') as file:
                lines, eof = self._read_some_lines(file, is_byte)
                while True:
                    putdata_socket.send_string(lines)
                    # print("Client: Mandamos el texto: ",lines)
                    if eof:
                        break

                    # print("about to receive the data answer")
                    answer = mt.loop_tool(mt.try_to_recv,
                                          putdata_socket.recv_string)
                    if answer.message_name != "OK":
                        print(
                            "Client: Data socket answer timed out. Something wrong sending the functions file"
                        )
                        return 1
                    if answer == -1:
                        print("Client: Data Message timed out")
                        putdata_socket.close()
                        return -1
                    lines, eof = self._read_some_lines(file, is_byte)
            # data_socket.recv_string()
            putdata_socket.close()
            print("Data sendend: ", current_file_url)
        except FileNotFoundError:
            print('No existe el file: ', current_file_url)
            return 1
Example #11
0
    def new_put_job(self):
        context = zmqa.Context()
        # new_addr = (self.client_ip,mt.get_available_random_port(self.client_ip))
        filesystem_socket = context.socket(zmq.REQ)
        print("Client: ", 'Tratemos de hacer putjob a: ', self.filesystem_addr)
        filesystem_socket.connect("tcp://{}:{}".format(*self.filesystem_addr))
        message = Message().set_message("PUTJOB", {"job_id": self.jhash})
        filesystem_socket.send_string(str(message))
        answer = mt.loop_tool(mt.try_to_recv, filesystem_socket.recv_string)
        if answer == -1:
            print("PUTJOB answer timed out!")
            filesystem_socket.close()
            return -1
        if answer.message_name != 'OK':
            print("No me devolvieron OK")
            filesystem_socket.close()
            return -1
        filesystem_socket.close()
        data_addr = answer.payload["ip"] + ":" + answer.payload["port"]
        putjob_context = zmqa.Context()
        putjob_socket = putjob_context.socket(zmq.REQ)
        putjob_socket.connect("tcp://" + data_addr)
        putjob_socket.send_string(str(Message().set_message("OK")))

        answer = mt.loop_tool(mt.try_to_recv, putjob_socket.recv_string)
        if answer == -1:
            print("PUTJOB answer timed out!")
            putjob_socket.close()
            return -1
        if answer.message_name != "OK":
            print("Error")
            putjob_socket.close()
            return -1
        if 'job_url' in answer.payload and 'database_url' in answer.payload:
            job_url = answer.payload['job_url']
            database_url = answer.payload['database_url']
            putjob_socket.close()
            return job_url, database_url
        putjob_socket.close()
        return -1
 def try_to_insert_blockresult(self, socket, message):
     new_port = mt.get_available_random_port(self.addr[0])
     if not os.path.exists(message.payload["status_db_url"]):
         socket.send_string(
             str(Message().set_message(
                 'ERROR', {
                     'info': 'unable to open database file',
                     'type_error': 'FileNotFoundError'
                 })))
     socket.send_string(
         str(Message().set_message("OK", {
             "ip": self.addr[0],
             "port": new_port
         })))
     new_addr = (self.addr[0], new_port)
     p = Process(target=self.new_insert_blockresult,
                 args=(
                     new_addr,
                     message,
                 ))
     p.daemon = True
     p.start()
Example #13
0
    def _ping_master(self):
        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.connect("tcp://{}:{}".format(*self.master_ping_addr))
        message = Message().set_message("PING", {"info": "Are you there?"})

        answer = mt.loop_tool(
            mt.try_to_send_message, mt.send_message, message,
            self.master_ping_addr,
            lambda: print("Client: ", "No me respondio el master"), 2)
        if answer is None:
            return False
        return True
    def new_get_rows(self, new_addr, message):
        database = sql.connect(message.payload["status_db_url"])

        context = zmq.Context()
        get_rows_socket = context.socket(zmq.REP)
        try:
            get_rows_socket.bind('tcp://{}:{}'.format(*new_addr))
        except zmq.error.ZMQError:
            print("FS ZMQERROR")
            return -1

        try:
            get_rows_message = mt.recv_message(get_rows_socket.recv_string, 1)
        except TimeoutError:
            print('Timeout!! en el getrows')
            database.close()
            get_rows_socket.close()
            return 0
        cursor = database.cursor()
        where_line = ""
        print(get_rows_message)
        for condition in get_rows_message.payload["filters"]:
            if len(where_line) > 0:
                where_line += " AND "
            where_line += "{} = '{}'".format(*condition)
        if len(where_line) > 0:
            select_line = '''SELECT * FROM {} WHERE {}''' \
                .format(get_rows_message.payload["table"], where_line)
            print('Mandamos a ejecutar el comando de sql: ', select_line)
            cursor.execute(select_line)
        else:
            select_line = '''SELECT * FROM {}''' \
                .format(get_rows_message.payload["table"])
            print('Mandamos a ejecutar el comando de sql: ', select_line)
            cursor.execute(select_line)
        print('Preparamos a respuesta al query')
        headers = [elem[0] for elem in cursor.description]
        result = cursor.fetchall()
        to_send = []
        # To_send es una lista de diccionarios donde cada elemento de la lista
        # es un diccionario con los valores de la fila correspondiente
        for item in result:
            to_send.append({headers[i]: item[i] for i in range(len(headers))})
        print('Le mandamos como respuesta del query: ', to_send)
        database.commit()
        get_rows_socket.send_string(
            str(Message().set_message("OK", {"rows": to_send})))
        cursor.close()
        database.close()
        get_rows_socket.close()
        return 0
 def try_to_insert_job(self, socket, message):
     new_port = mt.get_available_random_port(self.addr[0])
     socket.send_string(
         str(Message().set_message("OK", {
             "ip": self.addr[0],
             "port": new_port
         })))
     new_addr = (self.addr[0], new_port)
     p = Process(target=self.new_insert_job, args=(
         new_addr,
         message,
     ))
     p.daemon = True
     p.start()
Example #16
0
 def _remove_job(self):
     sock = zmqa.Context().socket(zmq.REQ)
     sock.connect("tcp://{}:{}".format(*self.filesystem_addr))
     sock.send_string(
         str(Message().set_message("REMOVEJOB", {'job_url': self.job_url})))
     answer = mt.loop_tool(mt.try_to_recv, sock.recv_string)
     if answer == -1:
         print("Client: No me respondio el FileSystem")
         return -1
     if answer.message_name == "OK":
         print("Client: Job removed")
     else:
         print("Client: error al remover el job:", answer.payload['info'])
     return 0
    def remove_job(socket, message):
        """
        Elimina del filesystem los archivos correspondientes a una tarea

        :param socket:
            El socket en el que se manda el mensaje "REMOVEJOB"
        :param message:
            El mensaje "REMOVEJOB" aún por ser interpretado
        :return:
            Nothing
        """
        try:
            for file in os.listdir("./" + message.payload["job_url"] + "/"):
                os.remove("./" + message.payload["job_url"] + "/" + file)
            os.rmdir("./" + message.payload["job_url"] + "/")
            socket.send_string(
                str(Message().set_message(
                    "OK", {"info": "Operation successfully done."})))
            return 0
        except FileNotFoundError:
            socket.send_string(
                str(Message().set_message("ERROR",
                                          {"info": "404: Job not found"})))
            return -1
    def new_insert_block(self, new_addr, message):
        database = sql.connect(message.payload["status_db_url"])
        insert_context = zmq.Context()
        insert_socket = insert_context.socket(zmq.REP)
        try:
            insert_socket.bind('tcp://{}:{}'.format(*new_addr))
        except zmq.error.ZMQError:
            print("FS ZMQERROR")
            return -1
        try:
            insert_message = mt.recv_message(insert_socket.recv_string, 1)
        except TimeoutError:
            print('Timeout!! en el insertblock')
            database.close()
            insert_socket.close()
            return 0

        cursor = database.cursor()
        try:
            cursor.execute(
                "INSERT INTO block VALUES ('{}','{}','{}','','')".format(
                    insert_message.payload["block_id"],
                    insert_message.payload["state"],
                    insert_message.payload["phase"]))
            print("Insertamos el bloque: ", insert_message.payload['block_id'])
        except sql.IntegrityError:
            print("Ya existe ese bloque")
        for slice_url in insert_message.payload["slices_id"]:
            try:
                print("Insertamos este slice: ", slice_url)
                insert_command = "INSERT INTO slices_url VALUES ('{}','{}')" \
                    .format(slice_url, insert_message.payload["block_id"])
                cursor.execute(insert_command)
                print('Ejecutamos el comando de insercion de slices: ',
                      insert_command)
            except sql.IntegrityError:
                print("Ya existe esta tupla: ", slice_url)
        database.commit()
        print('Hacemos commit a la insercion')
        insert_socket.send_string(str(Message().set_message("OK")))

        cursor.close()
        database.close()
        insert_socket.close()
        return 0
    def new_insert_job(self, new_addr, message):
        database = sql.connect(message.payload["status_db_url"])
        context = zmq.Context()
        insert_job_socket = context.socket(zmq.REP)
        try:
            insert_job_socket.bind('tcp://{}:{}'.format(*new_addr))
        except zmq.error.ZMQError:
            print("FS ZMQERROR")
            return -1

        try:
            insert_job_message = mt.recv_message(insert_job_socket.recv_string,
                                                 1)
        except TimeoutError:
            print('Timeout!! en el insertjob')
            database.close()
            insert_job_socket.close()
            return 0
        if insert_job_message.message_name != "INSERTJOB":
            print("Something went wrong on INSERTJOB!!!")
        d = insert_job_message.payload
        insert_string = "\"" + str(d["job_id"]) + "\", \"" + str(d["tracker_ip_ping"]) + "\", \"" \
                        + str(d["tracker_port_ping"]) + "\", \"" + str(d["answer_ip"]) + "\", \"" \
                        + str(d["answer_port"]) + "\", \"" + str(d["status_phase"]) + "\", \"" \
                        + str(d["map_data_url"]) + "\", \"" + str(d["result_url"]) + "\", \"" \
                        + str(d["job_state"]) + "\", \"" + str(d["data_type"]) + "\""
        cursor = database.cursor()
        # print(insert_string)
        try:
            cursor.execute(
                '''INSERT INTO job VALUES ({})'''.format(insert_string))
            database.commit()
        except sql.IntegrityError:
            print('Ya existe ese job: ', d["job_id"])
        message_to_send = Message().set_message("OK", {})
        insert_job_socket.send_string(str(message_to_send))
        # print("Sent OK")
        cursor.close()
        database.close()
        insert_job_socket.close()
        return 0
 def new_put_data(self, new_addr, file, is_a_byte_message, filename, lock):
     print("FS: Vamos a ejecutar el putdata de: ", filename)
     data_context = zmq.Context()
     data_socket = data_context.socket(zmq.REP)
     try:
         data_socket.bind("tcp://{}:{}".format(*new_addr))
     except zmq.error.ZMQError:
         print("FS ZMQERROR")
         os.remove(lock)
         return -1
     try:
         while True:
             print("FS: Preparado para recibir data")
             if is_a_byte_message:
                 data_to_write = mt.raw_recv(data_socket.recv, 1)
             else:
                 data_to_write = mt.raw_recv(data_socket.recv_string, 1)
             data_socket.send_string(str(Message().set_message("OK")))
             print("FS: Recibido el data")
             if data_to_write != '' and data_to_write != b'':
                 file.write(data_to_write)
                 print("FS: Escribimos en el arhivo: ", data_to_write)
             else:
                 file.close()
                 print("FS: Cerramos el archivo")
                 os.remove(lock)
                 print("FS: Removemos el lock: ", lock)
                 data_socket.close()
                 return filename
     except TimeoutError:
         print("FS: Hubo timeout en el putdata")
         file.close()
         os.remove(lock)
         data_socket.close()
         print("FS: Removemos el lock: ", lock)
     assert False, "FS: Por alguna razon estamos fuera del metodo en put data"
    def try_to_get_data(self, socket, message):
        print("FS: Tratamos de hacer getdata")
        new_port = mt.get_available_random_port(self.addr[0])
        file_url = message.payload["file_url"]

        if not os.path.exists(file_url):
            print("FS: No existe: ", file_url)
            message_to_send = Message().set_message(
                "ERROR", {
                    "info":
                    "File does not exist: {}. "
                    "I can't access this file right now.".format(file_url),
                    'type_error':
                    'FileNotFound'
                })
            socket.send_string(str(message_to_send))
            # assert False,"este fue el mensaje que me habian mandado: {}".format(str(message))
            return -1

        print("FS: Revisemos que nadie mas lo esta usando")
        lock, err = self._check_file_lock(message.payload["file_url"])
        if lock is None:
            print(
                "FS: Error 403.1: Temporarily Forbidden. I can't access this file right now."
            )
            message_to_send = Message().set_message(
                "ERROR", {
                    "info": "Temporarily Forbidden. "
                    "I can't access this file right now.",
                    'type_error': err
                })
            socket.send_string(str(message_to_send))
            return -1
        try:
            if message.payload["byte"]:
                data = open("./" + message.payload["file_url"], "rb")
            else:
                data = open("./" + message.payload["file_url"], "r")
            message_to_send = Message().set_message("OK", {
                "ip": str(self.addr[0]),
                "port": new_port
            })
            socket.send_string(str(message_to_send))
        except FileNotFoundError:
            message_to_send = Message().set_message(
                "ERROR", {
                    "info": "File is not in the filesystem}",
                    "type_error": 'FileNotFound'
                })
            socket.send_string(str(message_to_send))
            # data.close()
            os.remove(lock)
            print("FS: Removemos el lock: ", lock)
            # assert False, "este fue el mensaje que me habian mandado: {}".format(str(message))
            return -1

        new_addr = (self.addr[0], new_port)
        p = Process(target=self.new_get_data,
                    args=(
                        new_addr,
                        data,
                        lock,
                        message,
                    ))
        p.daemon = True
        p.start()
    def new_put_job(self, new_addr, job_id, db_url):
        print("Estamos dentro del putjob")
        data_context = zmqa.Context()
        data_socket = data_context.socket(zmq.REP)
        try:
            data_socket.bind('tcp://{}:{}'.format(*new_addr))
        except zmq.error.ZMQError:
            print("FS ZMQERROR")
            return -1

        answer = mt.loop_tool(mt.try_to_recv, data_socket.recv_string, 1)
        if answer == -1:
            data_socket.close()
            print("No me respondieron en el put job")
            return -1
        if answer.message_name != "OK":
            data_socket.close()
            print("Hubo algun error en el putjob")
            return -1

        try:
            init_file = open("./" + str(job_id) + "/__init__.py", "x")
            init_file.close()
            db_file = open(db_url, "x")
            db_file.close()

            # Now I'm gonna configure the database
            connection = sql.connect(db_url)
            cursor = connection.cursor()

            cursor.execute('''CREATE TABLE block 
            (block_id text PRIMARY KEY NOT NULL, state text, phase text,worker_ip text,worker_port text)'''
                           )

            cursor.execute('''CREATE TABLE slices_url 
            (slice_url text PRIMARY KEY NOT NULL , block_id text,
            FOREIGN KEY (block_id) REFERENCES block (block_id)) ''')

            cursor.execute('''CREATE TABLE result_url 
            (result_url text PRIMARY KEY NOT NULL )''')

            cursor.execute('''CREATE TABLE block_result 
            (block_id text NOT NULL, result_url text NOT NULL, 
            PRIMARY KEY (block_id,result_url) , 
            FOREIGN KEY (block_id) REFERENCES block (block_id) ON DELETE CASCADE ON UPDATE NO ACTION,
            FOREIGN KEY (result_url) REFERENCES result_url (result_url) ON DELETE CASCADE ON UPDATE NO ACTION
            )''')

            cursor.execute('''CREATE TABLE job 
            (job_id text PRIMARY KEY NOT NULL, tracker_ip_ping text, tracker_port_ping text, answer_ip text, answer_port text,
            status_phase text, map_data_url text, result_url text, job_state text, data_type text)'''
                           )
            connection.commit()
            cursor.close()
            connection.close()

        except FileExistsError:
            print("FS:Los archivos de inicialización ya existen... no sé cómo")
        data_socket.send_string(
            str(Message().set_message("OK", {
                "job_url": "./" + job_id,
                "database_url": db_url
            })))
        data_socket.close()
        print("Se hizo putjob")
        return 0
Example #23
0
    def execute(self):
        context = zmqa.Context()
        socket = context.socket(zmq.REP)
        print("Client:Binded to the listener port:",
              "tcp://" + self.listener_addr[0] + ":" + self.listener_addr[1])
        result = self._try_execute_task()
        socket.bind("tcp://" + self.listener_addr[0] + ":" +
                    self.listener_addr[1])
        assert result != -1, "No se pudo ejecutar el el job"
        # print("client: result:", result)
        p = Process(target=self.show_progress_job)
        p.daemon = True
        p.start()
        print("Client: Cargando....")
        while True:  # Está en un while True para que se pueda mandar información por el socket ademas del resultado

            response = mt.loop_tool(mt.try_to_recv, socket.recv_string, 5)
            if response == -1:
                # print("Client: Result response timed out!")

                if self._ping_master():
                    # print("Client: ",'Aun esta corriendo el master')
                    continue
                else:
                    print("Client: Se cayo el master anterior,busquemos otro")
                    self.try_to_connect_master()
                continue

            # Le respondo y después reviso lo que me mandó, si de todas formas le voy a responder lo mismo
            socket.send_string(str(Message().set_message("OK")))
            if response.message_name == "DONE" or response.message_name == "ERROR":
                if response.message_name == "ERROR":
                    print("Client:Error occurred during the operation: ",
                          response.payload["info"])
                    return -1
                print("Listo el resultado")
                break
            if response.message_name == "RELOCATE":
                print("Client: ", "Me Mandaron a hacer Relocate")
                self.master_ping_addr = (response.payload["ping_tracker_ip"],
                                         response.payload["ping_tracker_port"])
                self.listener_addr = (response.payload["answer_ip"],
                                      response.payload["answer_port"])
                socket.close()
                socket = context.socket(zmq.REP)
                socket.bind("tcp://" + self.listener_addr[0] + ":" +
                            self.listener_addr[1])
                continue
            else:
                print("Client:Operation info:", response.payload["info"])
        socket.close()
        result_url = response.payload["result_url"]
        print("Client:The result is in:", result_url,
              ", inside the filesystem")
        current_tries = 0
        result_data = None
        # Tratamos de recoger los resultados
        while current_tries < self.tries:
            result_data = self.new_get_data(result_url)
            if result_data == -1:
                print("Hubo bateo al buscar los resultados")
                self.filesystem_addr = self._get_new_filesystem_node()
                if self.filesystem_addr == -1:
                    print("Client: No pudimos resolver los resultados")
                    return -1
            else:
                break

        # self.remove_job()
        return result_data