def __init__(self, task_id, processor_instance, worker): super().__init__(task_id, processor_instance, worker) self.running = True self.stopped = False self.read_timeout_millis = \ int(worker.config.get(Config.READ_TIMEOUT_MS, Config.DEFAULT_READ_TIMEOUT_MS)) self.python_serializer = PythonSerializer() self.cross_lang_serializer = CrossLangSerializer()
class InputStreamTask(StreamTask): """Base class for stream tasks that execute a :class:`runtime.processor.OneInputProcessor` or :class:`runtime.processor.TwoInputProcessor` """ def __init__(self, task_id, processor_instance, worker): super().__init__(task_id, processor_instance, worker) self.running = True self.stopped = False self.read_timeout_millis = \ int(worker.config.get(Config.READ_TIMEOUT_MS, Config.DEFAULT_READ_TIMEOUT_MS)) self.python_serializer = PythonSerializer() self.cross_lang_serializer = CrossLangSerializer() def init(self): pass def run(self): while self.running: item = self.reader.read(self.read_timeout_millis) if item is not None: msg_data = item.body() type_id = msg_data[:1] if (type_id == serialization._PYTHON_TYPE_ID): msg = self.python_serializer.deserialize(msg_data[1:]) else: msg = self.cross_lang_serializer.deserialize(msg_data[1:]) self.processor.process(msg) self.stopped = True def cancel_task(self): self.running = False while not self.stopped: pass
class InputStreamTask(StreamTask): """Base class for stream tasks that execute a :class:`runtime.processor.OneInputProcessor` or :class:`runtime.processor.TwoInputProcessor` """ def commit_trigger(self, barrier): raise RuntimeError( "commit_trigger is only supported in SourceStreamTask.") def __init__(self, task_id, processor_instance, worker, last_checkpoint_id): super().__init__(task_id, processor_instance, worker, last_checkpoint_id) self.running = True self.stopped = False self.read_timeout_millis = \ int(worker.config.get(Config.READ_TIMEOUT_MS, Config.DEFAULT_READ_TIMEOUT_MS)) self.python_serializer = PythonSerializer() self.cross_lang_serializer = CrossLangSerializer() def run(self): logger.info("Input task thread start.") try: while self.running: self.worker.initial_state_lock.acquire() try: item = self.reader.read(self.read_timeout_millis) self.is_initial_state = False finally: self.worker.initial_state_lock.release() if item is None: continue if isinstance(item, DataMessage): msg_data = item.body type_id = msg_data[0] if type_id == serialization.PYTHON_TYPE_ID: msg = self.python_serializer.deserialize(msg_data[1:]) else: msg = self.cross_lang_serializer.deserialize( msg_data[1:]) self.processor.process(msg) elif isinstance(item, CheckpointBarrier): logger.info("Got barrier:{}".format(item)) logger.info("Start to do checkpoint {}.".format( item.checkpoint_id)) input_points = item.get_input_checkpoints() self.do_checkpoint(item.checkpoint_id, input_points) logger.info("Do checkpoint {} success.".format( item.checkpoint_id)) else: raise RuntimeError( "Unknown item type! item={}".format(item)) except ChannelInterruptException: logger.info("queue has stopped.") except BaseException as e: logger.exception( "Last success checkpointId={}, now occur error.".format( self.last_checkpoint_id)) self.request_rollback(str(e)) logger.info("Source fetcher thread exit.") self.stopped = True def cancel_task(self): self.running = False while not self.stopped: time.sleep(0.5) pass