示例#1
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
示例#2
0
文件: api.py 项目: charley-ye/SCFS-v1
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
示例#3
0
文件: api.py 项目: charley-ye/SCFS-v1
    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)
示例#4
0
文件: api.py 项目: charley-ye/SCFS-v1
    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)
示例#5
0
文件: api.py 项目: charley-ye/SCFS-v1
    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)
示例#6
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
示例#7
0
    def _handle_conncetion(self, filed):
        """handle connected socket as a file"""
        logging.info('connection start')

        command = recv_command(filed)
        self._handlers[command['method']](filed, command)

        filed.close()
        logging.info('disconnected')
示例#8
0
    def _handle_conncetion(self, filed):
        """handle connected socket as a file"""
        logging.info('connection start')

        command = recv_command(filed)
        self._handlers[command['method']](filed, command)

        filed.close()
        logging.info('disconnected')
示例#9
0
文件: api.py 项目: charley-ye/SCFS-v1
    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)
示例#10
0
    def _handle_conncetion(self, filed):
        """handle connected socket as a file"""
        logging.info('connection start')

        command = recv_command(filed)
        try:
            self._handlers[command['method']](filed, command)
        except KeyError:
            logging.exception("no such command handler: %s", command)

        filed.close()
        logging.info('disconnected')
示例#11
0
    def _handle_conncetion(self, filed):
        """handle connected socket as a file"""
        logging.info('connection start')

        command = recv_command(filed)
        try:
            self._handlers[command['method']](filed, command)
        except KeyError:
            logging.exception("no such command handler: %s", command)

        filed.close()
        logging.info('disconnected')
示例#12
0
文件: api.py 项目: charley-ye/SCFS-v1
    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']
示例#13
0
文件: api.py 项目: charley-ye/SCFS-v1
    def delfile(self, path):
        """delete a file"""
        filename = self._change_to_absolute_path(path)

        # delete meta data in mds
        packet = DeleteFilePacket(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']

        # delete data chunk in ds
        for item in info:
            chunk_id = item[0]
            ds_id = item[1]

            packet = DeleteChunkPacket(chunk_id)
            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)
            state = recv['state']
            if state == RET_FAILURE:
                logging.error('delete chunk in ds: %s %s', chunk_id, ds_id)
                info = 'delete chunk ds error'

        if state == RET_SUCCESS:
            info = 'ok'
        return (state, info)
示例#14
0
文件: api.py 项目: charley-ye/SCFS-v1
    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']
示例#15
0
文件: api.py 项目: charley-ye/SCFS-v1
    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)
示例#16
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()
示例#17
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
示例#18
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()
示例#19
0
文件: api.py 项目: charley-ye/SCFS-v1
    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
示例#20
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()
示例#21
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()
示例#22
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()
示例#23
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()
示例#24
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()
示例#25
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()
示例#26
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()
示例#27
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()
示例#28
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()
示例#29
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()
示例#30
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
示例#31
0
文件: api.py 项目: charley-ye/SCFS-v1
    def rmdir(self, dirname):
        """remove empty 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 += '/'

        # check current directory in dirname
        if self._cwd.startswith(absolute_path):
            logging.info('can not remove directory contain cwd')
            state = RET_FAILURE
            info = 'can not remvoe directory contain cwd'
            return (state, info)

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

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

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

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

        # check response and return
        state = recv['state']
        info = recv['info']
        if state == RET_FAILURE:
            logging.info('rmdir response error: %s', info)
        return (state, info)
示例#32
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()
示例#33
0
文件: api.py 项目: charley-ye/SCFS-v1
    def _add_file_commit(self, filename):
        """call after add file"""
        state = RET_SUCCESS
        info = 'ok'

        packet = AddFileCommitPacket(filename)
        msg = packet.get_message()

        sock = self._get_sockfd_to_mds()
        logging.info('add file commit :%s', msg)
        send_command(sock, msg)

        recv = recv_command(sock)
        logging.info('add file commit recv: %s', recv)
        sock.close()
        state = recv['state']
        if state == RET_FAILURE:
            info = 'add file commit error'

        if state == RET_SUCCESS:
            info = 'ok'
        return (state, info)
示例#34
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()
示例#35
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()
示例#36
0
文件: api.py 项目: charley-ye/SCFS-v1
    def _get_blocks_from_ds(self, ds_id, chunk_id, blist, block_num):
        """get @blist form a chunk, chunk contain block_num blocks
        return a block list
        """
        data_list = []

        packet = GetChunkPacket(chunk_id, block_num, blist)
        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()

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

        return data_list
示例#37
0
文件: api.py 项目: charley-ye/SCFS-v1
    def getobject(self, object_id, local_path, repair_flag=False):
        """get object from @des_path to @local_path,
        if repair_flag is True, repair missing objects
        """
        logging.info('get object: %s to %s', object_id, local_path)
        state = RET_SUCCESS
        info = 'ok'

        packet = GetObjPacket(object_id)
        msg = packet.get_message()
        sock = self._get_sockfd_to_mds()
        send_command(sock, msg)

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

        # init the code driver
        driver = self._get_code_driver(info['code'])
        chunks_info = info['chunks']
        (state, data) = self._get_object(chunks_info, object_id, driver)
        if state == RET_FAILURE:
            logging.error('decode error')
            info = 'decode error'
            return (RET_FAILURE, info)

        # write to disk
        fd = open(local_path, 'w')
        fd.write(data)
        fd.close()

        if state == RET_SUCCESS:
            info = 'ok'

        return (state, info)
示例#38
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()
示例#39
0
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)
示例#40
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)
示例#41
0
文件: api.py 项目: charley-ye/SCFS-v1
    def putfile(self, src_path, des_path, code_info={}):  # NOQA
        """put local @src_path file to remote @des_path with @code_info"""
        state = RET_SUCCESS
        info = 'ok'

        # get the local src_path information(filesize)
        try:
            filesize = getsize(src_path)
        except OSError:
            logging.error('no such file in local: %s', src_path)
            state = RET_FAILURE
            info = 'no such file in local'
            return (state, info)

        # set the fileinfo
        fileinfo = {}
        fileinfo['filesize'] = filesize

        code = {  # default code info
            'type': CODE_RS,
            'k': 2,
            'm': 2,
            'w': 8,
            'packet_size': self._packet_size,
            'block_size': self._block_size,
        }
        for (key, value) in code_info.items():
            code[key] = value
        fileinfo['code'] = code

        # call add file to mds with des_path and fileinfo
        filename = self._change_to_absolute_path(des_path)
        packet = AddFilePacket(filename, fileinfo)
        msg = packet.get_message()

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

        # recv the mds response
        recv = recv_command(sock)
        sock.close()
        logging.info('put file recv from mds: %s', recv)

        state = recv['state']
        info = recv['info']
        if state == RET_FAILURE:
            logging.error('put file recv from mds error')
            return (state, info)

        # get the objects and chunks ds information
        object_size = info['object_size']
        object_num = info['object_num']
        chunk_size = info['chunk_size']
        ds_list = info['objects']
        driver = self._get_code_driver(code)
        fd = open(src_path, 'r')

        for obj_idx in range(object_num):
            # split file to object
            data = fd.read(object_size)
            if len(data) < object_size:
                data += ' ' * (object_size - len(data))

            # encode object to chunks
            (state, chunks) = driver.encode(data)
            if state == RET_FAILURE:
                logging.error('driver encode error')
                info = 'driver encode error'
                return (state, info)

            data_chunk_num = driver.get_data_chunk_num()
            parity_chunk_num = driver.get_parity_chunk_num()
            # put chunks to ds
            for data_idx in range(data_chunk_num):
                chunk_id = self._get_chkkey_from_idx(filename, obj_idx,
                                                     data_idx)
                chunk_data = chunks[0][data_idx * chunk_size:(data_idx + 1) *
                                       chunk_size]
                ds_id = ds_list[obj_idx][data_idx]
                self.pool.spawn_n(self._send_chunk_to_ds, chunk_id, chunk_data,
                                  ds_id)

            for parity_idx in range(parity_chunk_num):
                chunk_id = self._get_chkkey_from_idx(
                    filename, obj_idx, parity_idx + data_chunk_num)
                chunk_data = chunks[1][parity_idx *
                                       chunk_size:(parity_idx + 1) *
                                       chunk_size]
                ds_id = ds_list[obj_idx][parity_idx + data_chunk_num]
                self.pool.spawn_n(self._send_chunk_to_ds, chunk_id, chunk_data,
                                  ds_id)
            # wait for write end
            self.pool.waitall()

        fd.close()

        # commit to mds
        if state == RET_SUCCESS:
            (state, info) = self._add_file_commit(filename)

        if state == RET_SUCCESS:
            info = 'ok'
        return (state, info)
示例#42
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)