def __init__(self, adapter: RollSiteAdapter, options: dict = None): if options is None: options = {} self.adapter = adapter self.roll_site_header: ErRollSiteHeader = adapter.roll_site_header self.namespace = adapter.namespace self.name = create_store_name(self.roll_site_header) self.tagged_key = '' self.obj_type = adapter.obj_type self.proxy_endpoint = adapter.proxy_endpoint channel = self.grpc_channel_factory.create_channel(self.proxy_endpoint) self.stub = proxy_pb2_grpc.DataTransferServiceStub(channel) static_er_conf = get_static_er_conf() self.__bin_packet_len = int(options.get( RollSiteConfKeys.EGGROLL_ROLLSITE_ADAPTER_SENDBUF_SIZE.key, static_er_conf.get(RollSiteConfKeys.EGGROLL_ROLLSITE_ADAPTER_SENDBUF_SIZE.key, RollSiteConfKeys.EGGROLL_ROLLSITE_ADAPTER_SENDBUF_SIZE.default_value))) self.total_written = 0 self.ba = bytearray(self.__bin_packet_len) self.buffer = ArrayByteBuffer(self.ba) self.writer = PairBinWriter(pair_buffer=self.buffer) self.push_batch_cnt = 0 self.push_pair_cnt = 0 self.topic_src = proxy_pb2.Topic(name=self.name, partyId=self.roll_site_header._src_party_id, role=self.roll_site_header._src_role, callback=None) self.topic_dst = proxy_pb2.Topic(name=self.name, partyId=self.roll_site_header._dst_party_id, role=self.roll_site_header._dst_role, callback=None)
class FileWriteBatch(PairWriteBatch): def __init__(self, file): self.writer = PairBinWriter(FileByteBuffer(file)) def put(self, k, v): self.writer.write(k, v) def write(self): pass def close(self): pass
class MmapWriteBatch(PairWriteBatch): def __init__(self, file): self._file = open(file.name, "wb") self.writer = PairBinWriter(FileByteBuffer(self._file)) def put(self, k, v): self.writer.write(k, v) def write(self): pass def close(self): self._file.close()
def put(self, k, v): if self.obj_type == 'object': L.debug(f"set tagged_key: {k}") self.tagged_key = _serdes.deserialize(k) try: self.writer.write(k, v) except IndexError as e: bin_batch = bytes(self.ba[0:self.buffer.get_offset()]) self.push(bin_batch) # TODO:0: replace 1024 with constant self.ba = bytearray( max(self.__bin_packet_len, len(k) + len(v) + 1024)) self.buffer = ArrayByteBuffer(self.ba) self.writer = PairBinWriter(pair_buffer=self.buffer) self.writer.write(k, v) except Exception as e: L.error(f"Unexpected error: {sys.exc_info()[0]}") raise e
def commit(bs=sendbuf_size): nonlocal ba nonlocal buffer nonlocal writer bin_batch = None if ba: bin_batch = bytes(ba[0:buffer.get_offset()]) ba = bytearray(bs) buffer = ArrayByteBuffer(ba) writer = PairBinWriter(pair_buffer=buffer) return bin_batch
def commit(bs=sendbuf_size): L.debug(f'generate_bin_batch commit: {done_cnt}') nonlocal ba nonlocal buffer nonlocal writer bin_batch = None if ba: bin_batch = bytes(ba[0:buffer.get_offset()]) ba = bytearray(bs) buffer = ArrayByteBuffer(ba) writer = PairBinWriter(pair_buffer=buffer) return bin_batch
def test_pair_bin(self): bs = bytearray(32) buf = ArrayByteBuffer(bs) writer = PairBinWriter(buf) for i in range(10): try: writer.write(str(i).encode(), str(i).encode()) except IndexError as e: print(buf.read_bytes(buf.get_offset(), 0)) buf.set_offset(0) writer = PairBinWriter(buf) writer.write(str(i).encode(), str(i).encode()) buf.set_offset(0) reader = PairBinReader(buf) print("last") print(list(reader.read_all()))
class RollSiteWriteBatch(PairWriteBatch): grpc_channel_factory = GrpcChannelFactory() # TODO:0: check if secure channel needed def __init__(self, adapter: RollSiteAdapter, options: dict = None): if options is None: options = {} self.adapter = adapter self.roll_site_header: ErRollSiteHeader = adapter.roll_site_header self.namespace = adapter.namespace self.name = create_store_name(self.roll_site_header) self.tagged_key = '' self.obj_type = adapter.obj_type self.proxy_endpoint = adapter.proxy_endpoint channel = self.grpc_channel_factory.create_channel(self.proxy_endpoint) self.stub = proxy_pb2_grpc.DataTransferServiceStub(channel) static_er_conf = get_static_er_conf() self.__bin_packet_len = int( options.get( RollSiteConfKeys.EGGROLL_ROLLSITE_ADAPTER_SENDBUF_SIZE.key, static_er_conf.get( RollSiteConfKeys.EGGROLL_ROLLSITE_ADAPTER_SENDBUF_SIZE.key, RollSiteConfKeys.EGGROLL_ROLLSITE_ADAPTER_SENDBUF_SIZE. default_value))) self.total_written = 0 self.ba = bytearray(self.__bin_packet_len) self.buffer = ArrayByteBuffer(self.ba) self.writer = PairBinWriter(pair_buffer=self.buffer) self.push_cnt = 0 self.topic_src = proxy_pb2.Topic( name=self.name, partyId=self.roll_site_header._src_party_id, role=self.roll_site_header._src_role, callback=None) self.topic_dst = proxy_pb2.Topic( name=self.name, partyId=self.roll_site_header._dst_party_id, role=self.roll_site_header._dst_role, callback=None) def __repr__(self): return f'<ErRollSiteWriteBatch(' \ f'adapter={self.adapter}, ' \ f'roll_site_header={self.roll_site_header}' \ f'namespace={self.namespace}, ' \ f'name={self.name}, ' \ f'obj_type={self.obj_type}, ' \ f'proxy_endpoint={self.proxy_endpoint}) ' \ f'at {hex(id(self))}>' def generate_message(self, obj, metadata): data = proxy_pb2.Data(value=obj) metadata.seq += 1 packet = proxy_pb2.Packet(header=metadata, body=data) yield packet # TODO:0: configurable def push(self, obj): L.debug( f'pushing for task: {self.name}, partition id: {self.adapter.partition_id}, push cnt: {self.get_push_count()}' ) task_info = proxy_pb2.Task( taskId=self.name, model=proxy_pb2.Model(name=self.adapter.roll_site_header_string, dataKey=self.namespace)) command_test = proxy_pb2.Command() # TODO: conf test as config and use it conf_test = proxy_pb2.Conf(overallTimeout=200000, completionWaitTimeout=200000, packetIntervalTimeout=200000, maxRetries=10) metadata = proxy_pb2.Metadata(task=task_info, src=self.topic_src, dst=self.topic_dst, command=command_test, seq=0, ack=0) max_retry_cnt = 100 exception = None for i in range(1, max_retry_cnt + 1): try: self.stub.push(self.generate_message(obj, metadata)) exception = None self.increase_push_count() break except Exception as e: exception = e L.info( f'caught exception in pushing {self.name}, partition_id: {self.adapter.partition_id}: {e}. retrying. current retry count: {i}, max_retry_cnt: {max_retry_cnt}' ) time.sleep(min(0.1 * i, 30)) if exception: raise GrpcCallError("error in push", self.proxy_endpoint, exception) def write(self): bin_data = bytes(self.ba[0:self.buffer.get_offset()]) self.push(bin_data) self.buffer = ArrayByteBuffer(self.ba) def send_end(self): L.info(f"send_end tagged_key:{self.tagged_key}") task_info = proxy_pb2.Task( taskId=self.name, model=proxy_pb2.Model(name=self.adapter.roll_site_header_string, dataKey=self.namespace)) command_test = proxy_pb2.Command(name="set_status") conf_test = proxy_pb2.Conf(overallTimeout=20000, completionWaitTimeout=20000, packetIntervalTimeout=20000, maxRetries=10) metadata = proxy_pb2.Metadata(task=task_info, src=self.topic_src, dst=self.topic_dst, command=command_test, operator="markEnd", seq=self.get_push_count(), ack=0) packet = proxy_pb2.Packet(header=metadata) try: # TODO:0: retry and sleep for all grpc call in RollSite self.stub.unaryCall(packet) except Exception as e: raise GrpcCallError('send_end', self.proxy_endpoint, e) def close(self): bin_batch = bytes(self.ba[0:self.buffer.get_offset()]) self.push(bin_batch) self.send_end() L.info(f'closing RollSiteWriteBatch for name: {self.name}, ' f'total push count: {self.push_cnt}') def put(self, k, v): if self.obj_type == 'object': L.debug(f"set tagged_key: {k}") self.tagged_key = _serdes.deserialize(k) try: self.writer.write(k, v) except IndexError as e: bin_batch = bytes(self.ba[0:self.buffer.get_offset()]) self.push(bin_batch) # TODO:0: replace 1024 with constant self.ba = bytearray( max(self.__bin_packet_len, len(k) + len(v) + 1024)) self.buffer = ArrayByteBuffer(self.ba) self.writer = PairBinWriter(pair_buffer=self.buffer) self.writer.write(k, v) except Exception as e: L.error(f"Unexpected error: {sys.exc_info()[0]}") raise e def increase_push_count(self): self.push_cnt += 1 def get_push_count(self): return self.push_cnt
def __init__(self, file): self._file = open(file.name, "wb") self.writer = PairBinWriter(FileByteBuffer(self._file))
def __init__(self, file): self.writer = PairBinWriter(FileByteBuffer(file))
def test_pair_bin_no_abb(self): bs = bytearray(32) buf = ArrayByteBuffer(bs) writer = PairBinWriter(pair_buffer=buf, data=bs) for i in range(10): try: writer.write(str(i).encode(), str(i).encode()) except IndexError as e: writer.set_offset(0) writer = PairBinWriter(pair_buffer=buf, data=bs) writer.write(str(i).encode(), str(i).encode()) pbr = PairBinReader(pair_buffer=buf, data=writer.get_data()) print(pbr.read_bytes(writer.get_offset(), 0)) writer.set_offset(0) reader = PairBinReader(pair_buffer=buf, data=bs) print("last") print(list(reader.read_all()))