Example #1
0
    def get_chunk_ds_id(self, chunk_id):
        ds_ip = ''
        ds_port = 0
        logging.info('get chunk: %s', chunk_id)

        packet = GetChkPacket(chunk_id)
        msg = packet.get_message()
        sock = self._get_sockfd_to_mds()
        send_command(sock, msg)

        recv = recv_command(sock)
        state = recv['state']
        info = recv['info']
        sock.close()
        if state == RET_FAILURE:
            logging.error('get chunk recv from mds: %s', recv)
            return (ds_ip, ds_port)

        # get chunk ds_id
        chunk_idx = int(chunk_id.rsplit('_chk')[1])
        chunks_info = info['chunks']
        chunk_info = chunks_info[chunk_idx]
        ds_id = chunk_info['ds_id']
        ds_ip = ds_id.split(':')[0]
        ds_port = int(ds_id.split(':')[1])

        return (ds_ip, ds_port)
Example #2
0
    def _handle_add_ds(self, filed, args):
        """handle ds -> mds add ds request, and response"""
        logging.info('handle add ds request')

        # get the ds info
        rack_id = args['rack_id']
        ds_ip = args['ds_ip']
        ds_port = args['ds_port']
        ds_info = {
            'ip': ds_ip,
            'port': ds_port,
            'rack': rack_id,
            'status': DS_CONNECTED,
            'update_time': time.asctime(),
        }

        state = RET_SUCCESS
        info = 'ok'

        # write to meta db
        state = self.mds.addds(ds_ip, ds_port, ds_info)
        if state == RET_FAILURE:
            info = 'mds addds error'

        # reply to client
        reply = AddDSReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("add ds return: %s", msg)
        send_command(filed, msg)
Example #3
0
    def _handle_list_dir(self, filed, args):
        """handle client -> mds list dir request, and response"""
        logging.info('handle list dir request')

        # get the dirname
        dirname = args['dirname']

        state = RET_SUCCESS
        info = ''

        # check dirname valid
        state, info = self._isvalid_dirname(dirname)

        # check dir exists
        if state == RET_SUCCESS:
            if not self.mds.hasdir(dirname):
                state = RET_FAILURE
                info = 'directory not exists: ' + dirname

        # get meta from db
        if state == RET_SUCCESS:
            info = self.mds.lsdir(dirname)
            if not info:  # None
                info = []

        # reply to client
        reply = ListDirReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("list dir return: %s", msg)
        send_command(filed, msg)
Example #4
0
    def _add_ds(self, mds_ip='127.0.0.1', mds_port=8000):
        """register ds to mds"""
        logging.info('add ds to mds')
        rack_id = self._config.getint('dataserver', 'rack_id')
        ds_ip = self._config.get('dataserver', 'ds_ip')
        ds_port = self._config.getint('dataserver', 'ds_port')

        packet = AddDSPacket(rack_id, ds_ip, ds_port)
        msg = packet.get_message()

        try:
            sock = eventlet.connect((mds_ip, mds_port))
        except socket.error:
            logging.error('can not connect to mds %s:%d', mds_ip, mds_port)
            sys.exit(
                'can not connect to mds, start mds and set the conf file!')
        sock_fd = sock.makefile('rw')

        logging.info('add ds msg: %s', msg)
        send_command(sock_fd, msg)

        recv = recv_command(sock_fd)
        state = recv['state']
        if state == RET_FAILURE:
            logging.error('add ds error, return :%s', recv)
            sys.exit('add ds error, mds return ' + recv)

        sock_fd.close()
        return state
Example #5
0
    def _handle_repair_chunk_commit(self, filed, args):
        """handle client -> mds repair chunk commit request, and response"""
        logging.info('handle repair chunk commit request')

        state = RET_SUCCESS
        info = 'ok'

        # get the chunk and ds id
        chunk_id = args['chunk']
        ds_id = args['ds_id']

        # check chunk exists or not
        if not self.mds.haschk(chunk_id):
            state = RET_FAILURE
            info = 'no such chunk'

        # update chunk info
        chunk_info = self.mds.getchk(chunk_id)
        chunk_info['ds_id'] = ds_id
        self.mds.addchk(chunk_id, chunk_info)

        # reply to client
        reply = RepairChkCommitReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("repair chunk return: %s", msg)
        send_command(filed, msg)
Example #6
0
    def _handle_status_dir(self, filed, args):
        """handle client -> mds status dir request, and response"""
        logging.info('handle status dir request')

        # get the dirname
        dirname = args['dirname']

        state = RET_SUCCESS
        info = ''

        # check dirname valid
        state, info = self._isvalid_dirname(dirname)

        # write to meta db
        if state == RET_SUCCESS:
            dirinfo = self.mds.statdir(dirname)
            if not dirinfo:
                state = RET_FAILURE
                info = 'no such directory'
            else:
                info = dirinfo

        # reply to client
        reply = StatusDirReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("status dir return: %s", msg)
        send_command(filed, msg)
Example #7
0
    def _handle_status_dir(self, filed, args):
        """handle client -> mds status dir request, and response"""
        logging.info('handle status dir request')

        # get the dirname
        dirname = args['dirname']

        state = RET_SUCCESS
        info = ''

        # check dirname valid
        state, info = self._isvalid_dirname(dirname)

        # write to meta db
        if state == RET_SUCCESS:
            dirinfo = self.mds.statdir(dirname)
            if not dirinfo:
                state = RET_FAILURE
                info = 'no such directory'
            else:
                info = dirinfo

        # reply to client
        reply = StatusDirReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("status dir return: %s", msg)
        send_command(filed, msg)
Example #8
0
    def _handle_list_dir(self, filed, args):
        """handle client -> mds list dir request, and response"""
        logging.info('handle list dir request')

        # get the dirname
        dirname = args['dirname']

        state = RET_SUCCESS
        info = ''

        # check dirname valid
        state, info = self._isvalid_dirname(dirname)

        # check dir exists
        if state == RET_SUCCESS:
            if not self.mds.hasdir(dirname):
                state = RET_FAILURE
                info = 'directory not exists: ' + dirname

        # get meta from db
        if state == RET_SUCCESS:
            info = self.mds.lsdir(dirname)
            if not info:  # None
                info = []

        # reply to client
        reply = ListDirReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("list dir return: %s", msg)
        send_command(filed, msg)
Example #9
0
    def _handle_report_ds(self, filed, args):
        """handle ds -> mds report ds request, and response"""
        logging.info('handle report ds request')

        # get the ds info
        ds_ip = args['ds_ip']
        ds_port = args['ds_port']
        ds_info = args['info']

        state = RET_SUCCESS
        info = 'ok'

        # write to meta db
        value = self.mds.getds(ds_ip, ds_port)
        for key in ds_info.keys():
            value[key] = ds_info[key]

        value['update_time'] = time.asctime()
        state = self.mds.updateds(ds_ip, ds_port, value)
        if state == RET_FAILURE:
            info = 'report ds error'

        # reply to client
        reply = ReportDSReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("report ds return: %s", msg)
        send_command(filed, msg)
Example #10
0
def get_blocks_from_ds(ds_id, chunk_id, blist, block_num, need_data, index):
    """get @blist form a chunk, chunk contain block_num blocks
    return a block list store in @need_data
    """
    data_list = []

    packet = GetChunkPacket(chunk_id, block_num, blist)
    msg = packet.get_message()
    sock = eventlet.connect((ds_id.split(':')[0],
                             int(ds_id.split(':')[1])))
    sock = sock.makefile('rw')
    send_command(sock, msg)
    recv = recv_command(sock)
    if recv['state'] != RET_SUCCESS:
        logging.error('get chunk from ds error: %s', recv['info'])
    else:
        data = recv_data(sock)
    sock.close()

    size = len(data)/len(blist)
    data_list = [data[i*size:(i+1)*size] for i in range(len(blist))]
    for i in range(len(blist)):
        need_data[index+i] = data_list[i]

    return data_list
Example #11
0
    def statdir(self, dirname):
        """stat directory"""
        dirname = dirname.strip()
        if not dirname.endswith('/'):
            dirname += '/'

        # change dirname to absolute path
        absolute_path = self._change_to_absolute_path(dirname)
        if not absolute_path.endswith('/'):
            absolute_path += '/'

        # make request packet
        packet = StatusDirPacket(absolute_path)
        msg = packet.get_message()

        # get socket to mds
        sock = self._get_sockfd_to_mds()

        # send request
        logging.info('stat dir send msg: %s', msg)
        send_command(sock, msg)

        # recv response
        recv = recv_command(sock)
        logging.info('stat dir recv msg: %s', recv)
        sock.close()

        # check response and return
        state = recv['state']
        info = recv['info']
        if state == RET_FAILURE:
            logging.info('stat dir response error: %s', info)
        return (state, info)
Example #12
0
    def _handle_add_file_commit(self, filed, args):
        """handle client -> mds add file commit request, and response"""
        logging.info('handle add file commit request')

        state = RET_SUCCESS
        info = 'ok'

        # get the filename
        filename = args['name']

        # check if filename in tmp table
        if self.mds.hasfile(filename):
            state = RET_FAILURE
            info = 'already have such file, can not commit'

        if not self.mds.hastmp(filename):
            state = RET_FAILURE
            info = 'time out!! no such file, can not commit'

        # move tmp filename information to real table
        if state == RET_SUCCESS:
            tmpinfo = self.mds.gettmp(filename)
            state = self._store_file_info(filename, tmpinfo)

        # delete tmp filename information
        if state == RET_SUCCESS:
            self.mds.deltmp(filename)

        # reply to client
        reply = AddFileCommitReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("add file commit return: %s", msg)
        send_command(filed, msg)
Example #13
0
    def _handle_report_ds(self, filed, args):
        """handle ds -> mds report ds request, and response"""
        logging.info('handle report ds request')

        # get the ds info
        ds_ip = args['ds_ip']
        ds_port = args['ds_port']
        ds_info = args['info']

        state = RET_SUCCESS
        info = 'ok'

        # write to meta db
        value = self.mds.getds(ds_ip, ds_port)
        for key in ds_info.keys():
            value[key] = ds_info[key]

        value['update_time'] = time.asctime()
        state = self.mds.updateds(ds_ip, ds_port, value)
        if state == RET_FAILURE:
            info = 'report ds error'

        # reply to client
        reply = ReportDSReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("report ds return: %s", msg)
        send_command(filed, msg)
Example #14
0
    def getfile(self,
                des_path,
                local_path,
                repair_flag=False,
                test_flag=False):
        """get file from @des_path to @local_path,
        if repair_flag is True, repair missing chunks
        """
        logging.info('get file: %s to %s', des_path, local_path)
        state = RET_SUCCESS
        info = 'ok'

        filename = self._change_to_absolute_path(des_path)
        packet = GetFilePacket(filename)
        msg = packet.get_message()

        sock = self._get_sockfd_to_mds()
        logging.info('get file send to mds: %s', msg)
        send_command(sock, msg)

        recv = recv_command(sock)
        sock.close()
        state = recv['state']
        info = recv['info']

        # check the file info
        if state == RET_FAILURE:
            logging.error('get file recv from mds: %s', recv)
            return (state, info)

        # init the code driver
        driver = self._get_code_driver(info['code'])

        # get each object and write to file
        data_need_len = info['filesize']
        object_num = info['object_num']
        fd = open(local_path, 'w')
        for obj_idx in range(object_num):
            object_id = '%s_obj%d' % (filename, obj_idx)
            chunks_info = info['objects'][obj_idx]
            (state, data) = self._get_object(chunks_info, object_id, driver)
            if state == RET_FAILURE:
                logging.error('get object %s error', object_id)
                info = 'get object error'
                break
            data_len = len(data)
            if data_len > data_need_len:
                data_len = data_need_len
                data = data[:data_need_len]
            data_need_len -= data_len

            if not test_flag:
                fd.write(data)

        # write file
        fd.close()

        if state == RET_SUCCESS:
            info = 'ok'
        return (state, info)
Example #15
0
    def _handle_add_file_commit(self, filed, args):
        """handle client -> mds add file commit request, and response"""
        logging.info('handle add file commit request')

        state = RET_SUCCESS
        info = 'ok'

        # get the filename
        filename = args['name']

        # check if filename in tmp table
        if self.mds.hasfile(filename):
            state = RET_FAILURE
            info = 'already have such file, can not commit'

        if not self.mds.hastmp(filename):
            state = RET_FAILURE
            info = 'time out!! no such file, can not commit'

        # move tmp filename information to real table
        if state == RET_SUCCESS:
            tmpinfo = self.mds.gettmp(filename)
            state = self._store_file_info(filename, tmpinfo)

        # delete tmp filename information
        if state == RET_SUCCESS:
            self.mds.deltmp(filename)

        # reply to client
        reply = AddFileCommitReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("add file commit return: %s", msg)
        send_command(filed, msg)
Example #16
0
    def _handle_repair_chunk_commit(self, filed, args):
        """handle client -> mds repair chunk commit request, and response"""
        logging.info('handle repair chunk commit request')

        state = RET_SUCCESS
        info = 'ok'

        # get the chunk and ds id
        chunk_id = args['chunk']
        ds_id = args['ds_id']

        # check chunk exists or not
        if not self.mds.haschk(chunk_id):
            state = RET_FAILURE
            info = 'no such chunk'

        # update chunk info
        chunk_info = self.mds.getchk(chunk_id)
        chunk_info['ds_id'] = ds_id
        self.mds.addchk(chunk_id, chunk_info)

        # reply to client
        reply = RepairChkCommitReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("repair chunk return: %s", msg)
        send_command(filed, msg)
Example #17
0
    def _add_ds(self, mds_ip='127.0.0.1', mds_port=8000):
        """register ds to mds"""
        logging.info('add ds to mds')
        rack_id = self._config.getint('dataserver', 'rack_id')
        ds_ip = self._config.get('dataserver', 'ds_ip')
        ds_port = self._config.getint('dataserver', 'ds_port')

        packet = AddDSPacket(rack_id, ds_ip, ds_port)
        msg = packet.get_message()

        try:
            sock = eventlet.connect((mds_ip, mds_port))
        except socket.error:
            logging.error('can not connect to mds %s:%d', mds_ip, mds_port)
            sys.exit('can not connect to mds, start mds and set the conf file!')
        sock_fd = sock.makefile('rw')

        logging.info('add ds msg: %s', msg)
        send_command(sock_fd, msg)

        recv = recv_command(sock_fd)
        state = recv['state']
        if state == RET_FAILURE:
            logging.error('add ds error, return :%s', recv)
            sys.exit('add ds error, mds return ' + recv)

        sock_fd.close()
        return state
Example #18
0
    def getchunk(self,
                 chunk_id,
                 local_path,
                 repair_flag=False,
                 test_flag=False):
        """get chunk from @des_path to @local_path,
        if repair_flag is True, repair missing chunks
        """
        logging.info('get chunk: %s to %s', chunk_id, local_path)
        state = RET_SUCCESS
        info = 'ok'

        packet = GetChkPacket(chunk_id)
        msg = packet.get_message()
        sock = self._get_sockfd_to_mds()
        send_command(sock, msg)

        recv = recv_command(sock)
        state = recv['state']
        info = recv['info']
        sock.close()
        if state == RET_FAILURE:
            logging.error('get chunk recv from mds: %s', recv)
            return (state, info)

        # check chunk status
        chunk_idx = int(chunk_id.rsplit('_chk')[1])
        chunks_info = info['chunks']
        chunk_info = chunks_info[chunk_idx]
        chunk_state = chunk_info['status']
        ds_id = chunk_info['ds_id']

        if chunk_info['ds_info']['status'] != DS_CONNECTED:
            chunk_state = CHUNK_MISSING

        if chunk_state != CHUNK_OK:
            # degrade chunk get
            data = self._degrade_get_chunk(recv['info'], chunk_id)
        else:
            # get data from chunk
            data = self._get_one_chunk_from_ds(ds_id, chunk_id)

        if not data:
            info = 'get chunk from ds error'
            state == RET_FAILURE
        else:
            if not test_flag:
                fd = open(local_path, 'w')
                fd.write(data)
                fd.close()

        if state == RET_SUCCESS:
            info = 'ok'
        return (state, info)
Example #19
0
    def _send_chunk_to_ds(self, chunk_id, chunk_data, ds_id):
        packet = AddChunkPacket(chunk_id, len(chunk_data))
        msg = packet.get_message()
        sock = self._get_sockfd_to_ds(
            ds_id.split(':')[0], int(ds_id.split(':')[1]))
        send_command(sock, msg)

        # sending data
        send_data(sock, chunk_data)
        recv = recv_command(sock)
        logging.info('send chunk to ds recv: %s', recv)
        sock.close()
        return recv['state']
Example #20
0
    def _handle_get_chunk(self, filed, args):
        """handle client -> mds get chunk request, and response"""
        logging.info('handle get chunk request')

        state = RET_SUCCESS
        info = {}

        # get the chunk_id
        chunk_id = args['chunk']

        # check chunk exists or not
        if not self.mds.haschk(chunk_id):
            state = RET_FAILURE
            info = 'no such chunk'

        # get the chunks stripe information
        one_stripe = []
        if state == RET_SUCCESS:
            # get the objects information
            object_id = self._get_obj_from_chunk_id(chunk_id)
            object_info = self.mds.getobj(object_id)
            chunk_num = object_info['chunk_num']

            # get the chunks information
            for chk_index in range(0, chunk_num):
                chunk_id = '%s_chk%d' % (object_id, chk_index)
                # get the chunks information
                chunk_info = self.mds.getchk(chunk_id)

                # get the ds information
                ds_id = chunk_info['ds_id']
                ds_info = self._get_ds_info(ds_id)
                chunk_info['ds_info'] = ds_info
                one_stripe.append(chunk_info)

        if state == RET_SUCCESS:
            info['chunks'] = one_stripe

        # get code information from file
        if state == RET_SUCCESS:
            filename = self._get_file_from_chunk_id(chunk_id)
            file_info = self.mds.getfile(filename)
            code_info = file_info['code']
            info['code'] = code_info

        # reply to client
        reply = GetChkReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("get chunk return: %s", msg)
        send_command(filed, msg)
Example #21
0
    def _handle_get_chunk(self, filed, args):
        """handle client -> mds get chunk request, and response"""
        logging.info('handle get chunk request')

        state = RET_SUCCESS
        info = {}

        # get the chunk_id
        chunk_id = args['chunk']

        # check chunk exists or not
        if not self.mds.haschk(chunk_id):
            state = RET_FAILURE
            info = 'no such chunk'

        # get the chunks stripe information
        one_stripe = []
        if state == RET_SUCCESS:
            # get the objects information
            object_id = self._get_obj_from_chunk_id(chunk_id)
            object_info = self.mds.getobj(object_id)
            chunk_num = object_info['chunk_num']

            # get the chunks information
            for chk_index in range(0, chunk_num):
                chunk_id = '%s_chk%d' % (object_id, chk_index)
                # get the chunks information
                chunk_info = self.mds.getchk(chunk_id)

                # get the ds information
                ds_id = chunk_info['ds_id']
                ds_info = self._get_ds_info(ds_id)
                chunk_info['ds_info'] = ds_info
                one_stripe.append(chunk_info)

        if state == RET_SUCCESS:
            info['chunks'] = one_stripe

        # get code information from file
        if state == RET_SUCCESS:
            filename = self._get_file_from_chunk_id(chunk_id)
            file_info = self.mds.getfile(filename)
            code_info = file_info['code']
            info['code'] = code_info

        # reply to client
        reply = GetChkReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("get chunk return: %s", msg)
        send_command(filed, msg)
Example #22
0
    def _handle_delete_chunk(self, filed, args):
        """handle client -> ds delete chunk request, and response"""
        logging.info('handle delete chunk request')

        chunk_id = args['chunk_id']

        # delete local filesystem chunk
        state = self._ds.remove_chunk(chunk_id)
        logging.info('add chunk: %s', chunk_id)

        # reply to client
        reply = DeleteChunkReplyPacket(state)
        msg = reply.get_message()
        logging.info("delete chunk return: %s", msg)
        send_command(filed, msg)
Example #23
0
    def _handle_delete_chunk(self, filed, args):
        """handle client -> ds delete chunk request, and response"""
        logging.info('handle delete chunk request')

        chunk_id = args['chunk_id']

        # delete local filesystem chunk
        state = self._ds.remove_chunk(chunk_id)
        logging.info('add chunk: %s', chunk_id)

        # reply to client
        reply = DeleteChunkReplyPacket(state)
        msg = reply.get_message()
        logging.info("delete chunk return: %s", msg)
        send_command(filed, msg)
Example #24
0
    def _handle_get_file(self, filed, args):
        """handle client -> mds get file request, and response"""
        logging.info('handle get file request')

        state = RET_SUCCESS
        info = 'ok'

        # get the filename
        filename = args['name']

        # check file exists or not
        if not self.mds.hasfile(filename):
            state = RET_FAILURE
            info = 'no such file'

        # get the file information
        if state == RET_SUCCESS:
            file_info = self.mds.getfile(filename)
            object_num = file_info['object_num']
            chunk_num = file_info['chunk_num']

            objects = []
            for obj_index in range(0, object_num):
                one_stripe = []
                for chk_index in range(0, chunk_num):
                    chunk_id = self._get_chkkey_from_index(filename,
                                                           obj_index,
                                                           chk_index)
                    # get the chunks information
                    chunk_info = self.mds.getchk(chunk_id)

                    # get the ds information
                    ds_id = chunk_info['ds_id']
                    ds_info = self._get_ds_info(ds_id)
                    chunk_info['ds_info'] = ds_info
                    one_stripe.append(chunk_info)
                objects.append(one_stripe)

            file_info['objects'] = objects

        if state == RET_SUCCESS:
            info = file_info

        # reply to client
        reply = GetFileReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("get file return: %s", msg)
        send_command(filed, msg)
Example #25
0
    def report_ds(self, ds_ip, ds_port, status=DS_CONNECTED):
        info = {
            'status': status,
        }
        packet = ReportDSPacket(ds_ip, ds_port, info)
        msg = packet.get_message()
        sock = self._get_sockfd_to_mds()

        logging.info('report ds :%s', msg)
        send_command(sock, msg)

        recv = recv_command(sock)
        logging.info('reprot ds recv: %s', recv)
        sock.close()

        return recv['state']
Example #26
0
    def statfile(self, path):
        """stat a file"""
        filename = self._change_to_absolute_path(path)
        packet = StatFilePacket(filename)
        msg = packet.get_message()

        sock = self._get_sockfd_to_mds()
        logging.info('stat file send to mds: %s', msg)
        send_command(sock, msg)

        recv = recv_command(sock)
        logging.info('stat file recv %s', recv)
        sock.close()
        state = recv['state']
        info = recv['info']
        return (state, info)
Example #27
0
def test_add_ds(rack_id=0, ds_ip='127.0.0.1', ds_port=7000):
    """test function: add_ds(rack_id, ds_ip, ds_port)
    """
    print 'add ds, rack_id:%d ip:%s port:%d' % (rack_id, ds_ip, ds_port)
    packet = AddDSPacket(rack_id, ds_ip, ds_port)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #28
0
    def _handle_add_chunk(self, filed, args):
        """handle client -> ds add chunk request, and response"""
        logging.info('handle add chunk request')

        # receive data from client
        chunk_id = args['chunk_id']
        data = recv_data(filed)

        # write data to local filesystem
        state = self._ds.write_chunk(chunk_id, data)
        logging.info('add chunk: %s', chunk_id)

        # reply to client
        reply = AddChunkReplyPacket(state)
        msg = reply.get_message()
        logging.info("add chunk return: %s", msg)
        send_command(filed, msg)
Example #29
0
def add_file(filename='/testfile', fileinfo={}):
    """
    test function: add_file(filename, fileinfo)
    filename should be absolute path,
    finleinfo contain all the info in dict format:
    fileinfo = {
        "filename": filename,
        "filesize": 1048576,
        "block_size": 512,
        "code": {
            "type": "rs",  # "rs/crs/zcode/etc.",
            "k": 2,
            "m": 2,
            "w": 8,
        },
    }
    """
    fileinfo = {
        "filesize": 20480,
        "code": {
            "type": CODE_RS,  # "rs/crs/zcode/etc.",
            "k": 2,
            "m": 2,
            "w": 8,
            "packet_size": 512,
            "block_size": 1024,
        },
    }

    print 'add file %s' % filename
    print 'file info:'
    print fileinfo
    packet = AddFilePacket(filename, fileinfo)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
    return recv
Example #30
0
    def _handle_add_chunk(self, filed, args):
        """handle client -> ds add chunk request, and response"""
        logging.info('handle add chunk request')

        # receive data from client
        chunk_id = args['chunk_id']
        data = recv_data(filed)

        # write data to local filesystem
        state = self._ds.write_chunk(chunk_id, data)
        logging.info('add chunk: %s', chunk_id)

        # reply to client
        reply = AddChunkReplyPacket(state)
        msg = reply.get_message()
        logging.info("add chunk return: %s", msg)
        send_command(filed, msg)
Example #31
0
def test_add_ds(rack_id=0, ds_ip='127.0.0.1', ds_port=7000):
    """test function: add_ds(rack_id, ds_ip, ds_port)
    """
    print 'add ds, rack_id:%d ip:%s port:%d' % (rack_id, ds_ip, ds_port)
    packet = AddDSPacket(rack_id, ds_ip, ds_port)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #32
0
    def _get_one_chunk_from_ds(self, ds_id, chunk_id):
        """get one chunk"""
        data = ''

        packet = GetChunkPacket(chunk_id, 1, [0])  # get all blocks in one chunk
        msg = packet.get_message()
        sock = self._get_sockfd_to_ds(ds_id.split(':')[0],
                                      int(ds_id.split(':')[1]))
        send_command(sock, msg)
        recv = recv_command(sock)
        if recv['state'] != RET_SUCCESS:
            logging.error('get chunk from ds error: %s', recv['info'])
        else:
            data = recv_data(sock)
        sock.close()

        return data
Example #33
0
    def _handle_delete_file(self, filed, args):
        """handle client -> mds delete file request, and response"""
        logging.info('handle delete file request')

        state = RET_SUCCESS
        info = 'ok'

        # get the file name
        filename = args['name']

        # check file exists or not
        if not self.mds.hasfile(filename):
            state = RET_FAILURE
            info = 'no such file, can not delete'

        if state == RET_SUCCESS:
            # get file information
            fileinfo = self.mds.getfile(filename)
            object_num = fileinfo['object_num']
            chunk_num = fileinfo['chunk_num']

            # remove file information
            self.mds.delfile(filename)
            chunks = []
            for obj_index in range(0, object_num):
                # remove objects information
                object_id = self._get_objkey_from_index(filename, obj_index)
                self.mds.delobj(object_id)

                for chk_index in range(0, chunk_num):
                    chunk_id = self._get_chkkey_from_index(filename,
                                                           obj_index,
                                                           chk_index)
                    # remvoe chunks information
                    chunk = self.mds.getchk(chunk_id)
                    chunks.append((chunk_id, chunk['ds_id']))
                    self.mds.delchk(chunk_id)

        # reply to client
        if state == RET_SUCCESS:
            info = chunks
        reply = DeleteFileReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("delete file return: %s", msg)
        send_command(filed, msg)
Example #34
0
def test_repair_chk(chk_id='/testfile_obj0_chk0'):
    """
    test function: repair_chk(chk_id)
    """
    print 'repair chk %s' % chk_id
    packet = RepairChkPacket(chk_id)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #35
0
def test_get_obj(obj_id='/testfile_obj0'):
    """
    test function: get_obj(obj_id)
    """
    print 'get obj %s' % obj_id
    packet = GetObjPacket(obj_id)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #36
0
def test_repair_chk(chk_id='/testfile_obj0_chk0'):
    """
    test function: repair_chk(chk_id)
    """
    print 'repair chk %s' % chk_id
    packet = RepairChkPacket(chk_id)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #37
0
def test_get_obj(obj_id='/testfile_obj0'):
    """
    test function: get_obj(obj_id)
    """
    print 'get obj %s' % obj_id
    packet = GetObjPacket(obj_id)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #38
0
def test_add_file_commit(filename='/testfile'):
    """
    test function: add_file_commit(filename)
    filename should be absolute path,
    """
    print 'add file commit %s' % filename
    packet = AddFileCommitPacket(filename)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #39
0
def test_add_file_commit(filename='/testfile'):
    """
    test function: add_file_commit(filename)
    filename should be absolute path,
    """
    print 'add file commit %s' % filename
    packet = AddFileCommitPacket(filename)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #40
0
def test_delete_file(filename='/testfile'):
    """
    test function: delete_file(filename)
    filename should be absolute path,
    """
    print 'delete file %s' % filename
    packet = DeleteFilePacket(filename)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #41
0
def test_delete_file(filename='/testfile'):
    """
    test function: delete_file(filename)
    filename should be absolute path,
    """
    print 'delete file %s' % filename
    packet = DeleteFilePacket(filename)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #42
0
    def _handle_add_ds(self, filed, args):
        """handle ds -> mds add ds request, and response"""
        logging.info('handle add ds request')

        # get the ds info
        rack_id = args['rack_id']
        ds_ip = args['ds_ip']
        ds_port = args['ds_port']
        ds_info = {
            'ip': ds_ip,
            'port': ds_port,
            'rack': rack_id,
            'status': DS_CONNECTED,
            'update_time': time.asctime(),
        }

        #print self.mds.get_alive_ds(),'qqweqweqwe'

        qwe = 0
        for num in range(0, len(self.mds.get_alive_ds())):
            #self._check_ds(slef.mds.get_alive_ds()[num](':')[1])
            rep = self._check_ds(
                self.mds.get_alive_ds()[int(num - qwe)].split(':')[0],
                int(self.mds.get_alive_ds()[num - qwe].split(':')[1]))

            if rep == 1:
                qwe = qwe + 1

        #self.mds.del_alive_ds("127.0.0.1",7006)
        state = RET_SUCCESS
        info = 'ok'

        # write to meta db
        state = self.mds.addds(ds_ip, ds_port, ds_info)
        if state == RET_FAILURE:
            info = 'mds addds error'

        # reply to client
        reply = AddDSReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("add ds return: %s", msg)
        send_command(filed, msg)
        print self.mds.get_alive_ds()
Example #43
0
def test_get_file(filepath='/testfile'):
    """
    test function: get_file(filepath)
    filepath should be absolute path,
    """
    print 'get file %s' % filepath
    packet = GetFilePacket(filepath)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #44
0
def test_get_file(filepath='/testfile'):
    """
    test function: get_file(filepath)
    filepath should be absolute path,
    """
    print 'get file %s' % filepath
    packet = GetFilePacket(filepath)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #45
0
def repair_chk_commit(chk_id='/testfile_obj0_chk0',
                      ds_id='127.0.0.1:7000'):
    """
    test function: repair_chk_commit(chk_id, ds_id)
    """
    print 'repair chk commit %s %s' % (chk_id, ds_id)
    packet = RepairChkCommitPacket(chk_id, ds_id)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
    return recv
Example #46
0
    def _handle_make_dir(self, filed, args):
        """handle client -> mds make dir request, and response"""
        logging.info('handle make dir request')

        # get the dirname
        dirname = args['dirname']

        state = RET_SUCCESS
        info = ''

        # check dirname valid
        state, info = self._isvalid_dirname(dirname)

        # check parent exists
        parent = ''.join(dirname[:-1].rpartition('/')[0:2])
        if state == RET_SUCCESS:
            if not self.mds.hasdir(parent):
                state = RET_FAILURE
                info = 'mkdir parent no exists: ' + parent

        # check dir exists
        if state == RET_SUCCESS:
            if self.mds.hasdir(dirname):
                state = RET_FAILURE
                info = 'directory already exists: ' + dirname

        # write to meta db
        if state == RET_SUCCESS:
            dirinfo = {}
            dirinfo['parent_dir'] = parent
            dirinfo['create_time'] = time.asctime()

            state = self.mds.mkdir(dirname, dirinfo)
            if state == RET_FAILURE:
                info = 'mds mkdir error'

        # reply to client
        reply = MakeDirReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("make dir return: %s", msg)
        send_command(filed, msg)
Example #47
0
def test_remove_dir(dirname='/testdir/'):
    """test function: remove_dir(dirname)
    dirname should be absolute path and end with '/'
    """

    dirname = dirname.strip()
    if not dirname.endswith('/'):
        dirname += '/'
    print 'remove dirname %s' % dirname
    packet = RemoveDirPacket(dirname)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #48
0
def test_report_ds(ds_ip='127.0.0.1', ds_port=7000, status=DS_CONNECTED):
    """test function: report_ds(info)
    report ds state info to mds
    """
    info = {
        'space': 102400,
        'chunk_num': 898,
        'status': status,
    }
    packet = ReportDSPacket(ds_ip, ds_port, info)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
Example #49
0
    def _handle_get_chunk(self, filed, args):
        """handle client -> ds get chunk requst, and response"""
        logging.info('handle get chunk request')

        chunk_id = args['chunk_id']
        total = args['total']
        lists = args['list']
        logging.info('get chunk: %s', chunk_id)

        # get data from local filesystem
        state, data = self._ds.read_chunk(chunk_id, total, lists)
        logging.info('read chunk return: %d', state)

        # reply state
        reply = GetChunkReplyPacket(state)
        msg = reply.get_message()
        logging.info("get chunk return: %s", msg)
        send_command(filed, msg)

        # reply data
        if isinstance(data, list):
            data = b''.join(data)
        send_data(filed, data)
Example #50
0
    def _handle_repair_chunk(self, filed, args):
        """handle client -> mds repair chunk request, and response"""
        logging.info('handle repair chunk request')

        state = RET_SUCCESS
        info = 'ok'

        # get the chunk_id
        chunk_id = args['chunk']

        # check chunk exists or not
        if not self.mds.haschk(chunk_id):
            state = RET_FAILURE
            info = 'no such chunk'

        # choose alive ds for the chunk
        new_ds = ''
        if state == RET_SUCCESS:
            ds_list = self._get_ds_list_by_chunk_id(chunk_id)
            alive_ds = self.mds.get_alive_ds()
            for item in alive_ds:
                if item not in ds_list:
                    new_ds = item
                    break

        if not new_ds:
            state = RET_FAILURE
            info = 'no enough alive ds for chunk'

        if state == RET_SUCCESS:
            info = new_ds

        # reply to client
        reply = RepairChkReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("repair chunk return: %s", msg)
        send_command(filed, msg)
Example #51
0
    def _handle_remove_dir(self, filed, args):
        """handle client -> mds remove dir request, and response"""
        logging.info('handle remove dir request')

        # get the dirname
        dirname = args['dirname']

        state = RET_SUCCESS
        info = 'ok'

        # check dirname valid
        state, info = self._isvalid_dirname(dirname)

        # check subfiles empty
        if state == RET_SUCCESS:
            if self.mds.hassub(dirname):
                state = RET_FAILURE
                info = 'has subfiles in %s ' + dirname

        # check dir exists
        if state == RET_SUCCESS:
            if not self.mds.hasdir(dirname):
                state = RET_FAILURE
                info = 'directory not exists: ' + dirname

        # write to meta db
        if state == RET_SUCCESS:
            state = self.mds.deldir(dirname)
            if state == RET_FAILURE:
                info = 'rmdir error'

        # reply to client
        reply = RemoveDirReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("remove dir return: %s", msg)
        send_command(filed, msg)
def test_command():
    """test send and recv command"""
    ip = '127.0.0.1'
    port = 8899
    command = 'command'

    # start server to receive command
    print 'start server'
    server = eventlet.listen((ip, port))
    POOL.spawn_n(start_accept, server)

    # connect to server
    print 'start connect'
    client = eventlet.connect((ip, port))
    client_fd = client.makefile('rw')

    # send command
    send_command(client_fd, command)

    # receive response(same with send command)
    response = recv_command(client_fd)

    # check response
    eq_(response, command)
Example #53
0
    def _handle_stat_file(self, filed, args):
        """handle client -> mds stat file request, and response"""
        logging.info('handle stat file request')

        state = RET_SUCCESS
        info = 'ok'

        # get the filename
        filename = args['name']

        # check file exists or not
        if not self.mds.hasfile(filename):
            logging.info('mds has no such file')
            state = RET_FAILURE
            info = 'no such file'

        if state == RET_SUCCESS:
            info = self.mds.getfile(filename)

        # reply to client
        reply = StatFileReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("stat file return: %s", msg)
        send_command(filed, msg)
Example #54
0
    def _handle_add_file(self, filed, args):
        """handle client -> mds add file request, store to tmp table,
        and response,
        """
        logging.info('handle add file request')

        state = RET_SUCCESS
        info = 'ok'

        # get the file name, file size
        filename = args['name']
        fileinfo = args['info']

        # check file exists or not
        if self.mds.hasfile(filename):
            state = RET_FAILURE
            info = 'already has such file'

        if self.mds.hastmp(filename):
            state = RET_FAILURE
            info = 'already add such file, not commit yet'

        # set the code driver by fileinfo
        try:
            code = self._get_code_driver(fileinfo['code'])
        except (KeyError, ValueError, AssertionError):
            logging.exception('set code driver error')
            state = RET_FAILURE
            info = 'code info error'

        file_info = {}
        if state == RET_SUCCESS:
            # count the object num according to object size and file size
            object_size = code.get_object_size()
            filesize = int(fileinfo['filesize'])
            object_num = int(filesize/object_size)
            if filesize % object_size:   # add puls one
                object_num += 1
            chunk_num = code.get_chunk_num()

            file_info['filename'] = filename
            file_info['filesize'] = filesize
            file_info['code'] = fileinfo['code']
            file_info['object_num'] = object_num
            file_info['object_size'] = object_size
            file_info['block_size'] = code.get_block_size()
            file_info['chunk_size'] = code.get_chunk_size()
            file_info['chunk_num'] = chunk_num

            # check the ds alive num, must > chunk num(k+m)
            alive_ds = self.mds.get_alive_ds()
            if len(alive_ds) < code.get_chunk_num():
                logging.error('alive ds num (%d) must not less than '
                              'chunk num (%d) in one stripe',
                              len(alive_ds), code.get_chunk_num())
                state = RET_FAILURE
                info = 'alive ds num less than chunk num'

            # assign the chunks to alive ds
            objects = self._assign_ds(alive_ds, object_num, chunk_num)
            file_info['objects'] = objects
            if not objects:
                logging.error('assign ds failed')
                state = RET_FAILURE

        if state == RET_SUCCESS:
            # store file meta data to temp table
            self.mds.addtmp(filename, file_info)

        if state == RET_SUCCESS:
            info = file_info

        # response to client
        reply = AddFileReplyPacket(state, info)
        msg = reply.get_message()
        logging.info("add file return: %s", msg)
        send_command(filed, msg)
Example #55
0
def test_ds():
    """test function: add_chunk(chunk_id, chunk_length, chunk_data)"""
    # start the sever to receive command
    # get config options
    config = configparser.ConfigParser()
    config.read(DS_CONFIG_FILE)
    config.set('storage', 'chunk_store_dir', './bin/storage/')

    # start server
    ds_ = DSServer(config, test=True)
    POOL.spawn_n(start_server, ds_)

    # test add chunk
    chunk_id = 'obj0_chk0'
    length = DATA_LENGTH
    data = os.urandom(length)  # generate test data of length size

    packet = AddChunkPacket(chunk_id, length)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    # sending data
    send_data(sock_fd, data)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    sock_fd.close()
    eq_(recv['state'], RET_SUCCESS)
    eq_(recv['method'], OP_ADD_CHUNK_REPLY)

    # test get chunk
    """test function: get_chunk(chunk_id, total_blocks, block_list)"""
    chunk_id = 'obj0_chk0'
    total_blocks = 10
    block_list = [0, 1, 2, 3, 4]

    packet = GetChunkPacket(chunk_id, total_blocks, block_list)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile('rw')

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    eq_(recv['state'], RET_SUCCESS)
    eq_(recv['method'], OP_GET_CHUNK_REPLY)

    # recieve data
    get_data = recv_data(sock_fd)
    eq_(get_data, data[:length/2])

    # test delete chunk
    """test function: delete_chunk(chunk_id)"""
    chunk_id = 'obj0_chk0'

    packet = DeleteChunkPacket(chunk_id)
    msg = packet.get_message()

    sock = get_new_connection()
    sock_fd = sock.makefile()

    logging.info('%s', msg)
    send_command(sock_fd, msg)

    recv = recv_command(sock_fd)
    print recv
    logging.info('recv: %s', recv)
    eq_(recv['state'], RET_SUCCESS)
    eq_(recv['method'], OP_DELETE_CHUNK_REPLY)