コード例 #1
0
ファイル: client_put.py プロジェクト: makcuk/fabnet
    def process(self, packet):
        """In this method should be implemented logic of processing
        reuqest packet from sender node

        @param packet - object of FabnetPacketRequest class
        @return object of FabnetPacketResponse
                or None for disabling packet response to sender
        """
        data = packet.binary_data
        key = packet.parameters.get('key', None)
        if key is not None:
            self._validate_key(key)

        checksum = packet.parameters.get('checksum', None)
        replica_count = int(packet.parameters.get('replica_count', MIN_REPLICA_COUNT))
        wait_writes_count = int(packet.parameters.get('wait_writes_count', 1))
        if checksum is None:
            return FabnetPacketResponse(ret_code=RC_ERROR,
                    ret_message='Checksum does not found in request packet!')

        if wait_writes_count > replica_count:
            return FabnetPacketResponse(ret_code=RC_ERROR, ret_message='Cant waiting more replicas than saving!')
        if replica_count < MIN_REPLICA_COUNT:
            return FabnetPacketResponse(ret_code=RC_ERROR, ret_message='Minimum replica count is equal to %s!'%MIN_REPLICA_COUNT)

        data_block = DataBlock(data, checksum)
        data_block.validate()
        succ_count = 0
        is_replica = False
        keys = KeyUtils.generate_new_keys(self.operator.node_name, replica_count, prime_key=key)
        data, checksum = data_block.pack(keys[0], replica_count)
        for key in keys:
            range_obj = self.operator.ranges_table.find(long(key, 16))
            if not range_obj:
                logger.debug('[ClientPutOperation] Internal error: No hash range found for key=%s!'%key)
            else:
                params = {'key': key, 'checksum': checksum, 'is_replica': is_replica}
                if succ_count >= wait_writes_count:
                    self._init_operation(range_obj.node_address, 'PutDataBlock', params, binary_data=data)
                else:
                    resp = self._init_operation(range_obj.node_address, 'PutDataBlock', params, sync=True, binary_data=data)
                    if resp.ret_code:
                        logger.debug('[ClientPutOperation] PutDataBlock error from %s: %s'%(range_obj.node_address, resp.ret_message))
                    else:
                        succ_count += 1

            is_replica = True

        if wait_writes_count < succ_count:
            return FabnetPacketResponse(ret_code=RC_ERROR, ret_message='Writing data error!')

        return FabnetPacketResponse(ret_parameters={'key': keys[0]})
コード例 #2
0
ファイル: put_data_block.py プロジェクト: makcuk/fabnet
    def process(self, packet):
        """In this method should be implemented logic of processing
        reuqest packet from sender node

        @param packet - object of FabnetPacketRequest class
        @return object of FabnetPacketResponse
                or None for disabling packet response to sender
        """
        primary_key = packet.parameters.get('primary_key', None)
        replica_count =  packet.parameters.get('replica_count', None)
        key = packet.parameters.get('key', None)
        checksum = packet.parameters.get('checksum', None)
        is_replica = packet.parameters.get('is_replica', False)
        data = packet.binary_data

        if key is None:
            return FabnetPacketResponse(ret_code=RC_ERROR, ret_message='key does not found!')
        if checksum is None:
            return FabnetPacketResponse(ret_code=RC_ERROR, ret_message='data checksum does not found!')

        if primary_key:
            if replica_count is None:
                return FabnetPacketResponse(ret_code=RC_ERROR, ret_message='replica_count does not found!')

            data_block = DataBlock(data, checksum)
            data_block.validate()
            data, checksum = data_block.pack(primary_key, replica_count)
        else:
            #DataBlock.read_header(data) #FIXME
            pass

        dht_range = self.operator.get_dht_range()
        if not is_replica:
            dht_range.put(key, data)
        else:
            dht_range.put_replica(key, data)

        return FabnetPacketResponse()