本文整理汇总了Python中apache.thermos.common.ckpt.CheckpointDispatcher.dispatch方法的典型用法代码示例。如果您正苦于以下问题:Python CheckpointDispatcher.dispatch方法的具体用法?Python CheckpointDispatcher.dispatch怎么用?Python CheckpointDispatcher.dispatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apache.thermos.common.ckpt.CheckpointDispatcher
的用法示例。
在下文中一共展示了CheckpointDispatcher.dispatch方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read
# 需要导入模块: from apache.thermos.common.ckpt import CheckpointDispatcher [as 别名]
# 或者: from apache.thermos.common.ckpt.CheckpointDispatcher import dispatch [as 别名]
def read(args, options):
"""Replay a thermos checkpoint.
Usage: thermos read [options] checkpoint_filename
Options:
--simple Do not replay the full task state machine. Only print out the contents of
each checkpoint log message.
"""
if len(args) != 1:
app.error('Expected one checkpoint file, got %s' % len(args))
if not os.path.exists(args[0]):
app.error('Could not find %s' % args[0])
dispatcher = CheckpointDispatcher()
state = RunnerState(processes={})
with open(args[0], 'r') as fp:
try:
for record in ThriftRecordReader(fp, RunnerCkpt):
if not options.simple:
dispatcher.dispatch(state, record)
else:
print('CKPT: %s' % record)
except RecordIO.Error as err:
print("Failed to recover from %s: %s" % (fp.name, err))
return
if not options.simple:
if state is None or state.header is None:
print('Checkpoint stream CORRUPT or outdated format')
return
print('Recovered Task Header:')
print(' id: %s' % state.header.task_id)
print(' user: %s' % state.header.user)
print(' host: %s' % state.header.hostname)
print(' sandbox: %s' % state.header.sandbox)
if state.header.ports:
print(' ports: %s' % ' '.join(
'%s->%s' % (name, port) for (name, port) in state.header.ports.items()))
print('Recovered Task States:')
for task_status in state.statuses:
print(' %s [pid: %d] => %s' % (
time.asctime(time.localtime(task_status.timestamp_ms / 1000.0)),
task_status.runner_pid,
TaskState._VALUES_TO_NAMES[task_status.state]))
print('Recovered Processes:')
for process, process_history in state.processes.items():
print(' %s runs: %s' % (process, len(process_history)))
for k in reversed(range(len(process_history))):
run = process_history[k]
print(' %2d: pid=%d, rc=%s, finish:%s, state:%s' % (
k,
run.pid,
run.return_code if run.return_code is not None else '',
time.asctime(time.localtime(run.stop_time)) if run.stop_time else 'None',
ProcessState._VALUES_TO_NAMES.get(run.state, 'Unknown')))
示例2: main
# 需要导入模块: from apache.thermos.common.ckpt import CheckpointDispatcher [as 别名]
# 或者: from apache.thermos.common.ckpt.CheckpointDispatcher import dispatch [as 别名]
def main(args):
values = app.get_options()
if len(args) > 0:
print("ERROR: unrecognized arguments: %s\n" % (" ".join(args)), file=sys.stderr)
app.help()
sys.exit(1)
if not values.ckpt:
print("ERROR: must supply --checkpoint", file=sys.stderr)
app.help()
sys.exit(1)
fp = file(values.ckpt, "r")
rr = ThriftRecordReader(fp, RunnerCkpt)
wrs = RunnerState(processes={})
dispatcher = CheckpointDispatcher()
try:
for wts in rr:
print('Recovering: %s' % wts)
if values.assemble is True:
dispatcher.dispatch(wrs, wts)
except RecordIO.Error as err:
print('Error recovering checkpoint stream: %s' % err, file=sys.stderr)
return
print('\n\n\n')
if values.assemble:
print('Recovered Task Header')
pprint.pprint(wrs.header, indent=4)
print('\nRecovered Task States')
for task_status in wrs.statuses:
print(' %s [pid: %d] => %s' % (
time.asctime(time.localtime(task_status.timestamp_ms / 1000.0)),
task_status.runner_pid,
TaskState._VALUES_TO_NAMES[task_status.state]))
print('\nRecovered Processes')
pprint.pprint(wrs.processes, indent=4)
示例3: inspect
# 需要导入模块: from apache.thermos.common.ckpt import CheckpointDispatcher [as 别名]
# 或者: from apache.thermos.common.ckpt.CheckpointDispatcher import dispatch [as 别名]
def inspect(self, task_id):
"""
Reconstructs the checkpoint stream and returns a CheckpointInspection.
"""
dispatcher = CheckpointDispatcher()
state = RunnerState(processes = {})
muxer = ProcessMuxer(self._path.given(task_id=task_id))
runner_processes = []
coordinator_processes = set()
processes = set()
def consume_process_record(record):
if not record.process_status:
return
try:
user_uid = pwd.getpwnam(state.header.user).pw_uid
except KeyError:
log.error('Could not find user: %s' % state.header.user)
return
if record.process_status.state == ProcessState.FORKED:
coordinator_processes.add((record.process_status.coordinator_pid, user_uid,
record.process_status.fork_time))
elif record.process_status.state == ProcessState.RUNNING:
processes.add((record.process_status.pid, user_uid,
record.process_status.start_time))
# replay runner checkpoint
runner_pid = None
runner_latest_update = 0
try:
with open(self._path.given(task_id=task_id).getpath('runner_checkpoint')) as fp:
with closing(ThriftRecordReader(fp, RunnerCkpt)) as ckpt:
for record in ckpt:
dispatcher.dispatch(state, record)
runner_latest_update = max(runner_latest_update,
self.get_timestamp(record.process_status))
# collect all bound runners
if record.task_status:
if record.task_status.runner_pid != runner_pid:
runner_processes.append((record.task_status.runner_pid,
record.task_status.runner_uid or 0,
record.task_status.timestamp_ms))
runner_pid = record.task_status.runner_pid
elif record.process_status:
consume_process_record(record)
except (IOError, OSError, RecordIO.Error) as err:
log.debug('Error inspecting task runner checkpoint: %s' % err)
return
# register existing processes in muxer
for process_name in state.processes:
muxer.register(process_name)
# read process checkpoints
process_latest_update = runner_latest_update
for record in muxer.select():
process_latest_update = max(process_latest_update, self.get_timestamp(record.process_status))
consume_process_record(record)
return CheckpointInspection(
runner_latest_update=runner_latest_update,
process_latest_update=process_latest_update,
runner_processes=runner_processes,
coordinator_processes=coordinator_processes,
processes=processes)
示例4: TaskRunner
# 需要导入模块: from apache.thermos.common.ckpt import CheckpointDispatcher [as 别名]
# 或者: from apache.thermos.common.ckpt.CheckpointDispatcher import dispatch [as 别名]
#.........这里部分代码省略.........
task_id=self._task_id,
ports=self._portmap,
user=self._user)
self._task, uninterp = (task % Environment(thermos=context)).interpolate()
if len(uninterp) > 0:
raise self.InvalidTask('Failed to interpolate task, missing: %s' %
', '.join(str(ref) for ref in uninterp))
try:
ThermosTaskValidator.assert_same_task(self._pathspec, self._task)
except ThermosTaskValidator.InvalidTaskError as e:
raise self.InvalidTask('Invalid task: %s' % e)
self._plan = None # plan currently being executed (updated by Handlers)
self._regular_plan = planner_class(self._task, clock=clock,
process_filter=lambda proc: proc.final().get() is False)
self._finalizing_plan = planner_class(self._task, clock=clock,
process_filter=lambda proc: proc.final().get() is True)
self._chroot = chroot
self._sandbox = sandbox
self._container_sandbox = container_sandbox
self._terminal_state = None
self._ckpt = None
self._process_map = dict((p.name().get(), p) for p in self._task.processes())
self._task_processes = {}
self._stages = dict((state, stage(self)) for state, stage in self.STAGES.items())
self._finalization_start = None
self._preemption_deadline = None
self._watcher = ProcessMuxer(self._pathspec)
self._state = RunnerState(processes={})
self._preserve_env = preserve_env
self._mesos_containerizer_path = mesos_containerizer_path
# create runner state
universal_handler = universal_handler or TaskRunnerUniversalHandler
self._dispatcher = CheckpointDispatcher()
self._dispatcher.register_handler(universal_handler(self))
self._dispatcher.register_handler(TaskRunnerProcessHandler(self))
self._dispatcher.register_handler(TaskRunnerTaskHandler(self))
# recover checkpointed runner state and update plan
self._recovery = True
self._replay_runner_ckpt()
@property
def task(self):
return self._task
@property
def task_id(self):
return self._task_id
@property
def state(self):
return self._state
@property
def processes(self):
return self._task_processes
def task_state(self):
return self._state.statuses[-1].state if self._state.statuses else TaskState.ACTIVE
def close_ckpt(self):
"""Force close the checkpoint stream. This is necessary for runners terminated through
exception propagation."""
log.debug('Closing the checkpoint stream.')
self._ckpt.close()
示例5: TaskMonitor
# 需要导入模块: from apache.thermos.common.ckpt import CheckpointDispatcher [as 别名]
# 或者: from apache.thermos.common.ckpt.CheckpointDispatcher import dispatch [as 别名]
class TaskMonitor(object):
"""
Class responsible for reconstructing and monitoring the state of an individual Thermos task via
its runner checkpoint. Also exports information on active processes in the task.
"""
def __init__(self, root, task_id):
"""Construct a TaskMonitor.
:param root: The checkpoint root of the task.
:param task_id: The task id of the task.
"""
pathspec = TaskPath(root=root, task_id=task_id)
self._dispatcher = CheckpointDispatcher()
self._runnerstate = RunnerState(processes={})
self._runner_ckpt = pathspec.getpath("runner_checkpoint")
self._active_file, self._finished_file = (
pathspec.given(state=state).getpath("task_path") for state in ("active", "finished")
)
self._ckpt_head = 0
self._apply_states()
self._lock = threading.Lock()
def _apply_states(self):
"""
os.stat() the corresponding checkpoint stream of this task and determine if there are new ckpt
records. Attempt to read those records and update the high watermark for that stream.
Returns True if new states were applied, False otherwise.
"""
ckpt_offset = None
try:
ckpt_offset = os.stat(self._runner_ckpt).st_size
updated = False
if self._ckpt_head < ckpt_offset:
with open(self._runner_ckpt, "r") as fp:
fp.seek(self._ckpt_head)
rr = ThriftRecordReader(fp, RunnerCkpt)
while True:
runner_update = rr.try_read()
if not runner_update:
break
try:
self._dispatcher.dispatch(self._runnerstate, runner_update)
except CheckpointDispatcher.InvalidSequenceNumber as e:
log.error("Checkpoint stream is corrupt: %s" % e)
break
new_ckpt_head = fp.tell()
updated = self._ckpt_head != new_ckpt_head
self._ckpt_head = new_ckpt_head
return updated
except OSError as e:
if e.errno == errno.ENOENT:
# The log doesn't yet exist, will retry later.
log.warning("Could not read from checkpoint %s" % self._runner_ckpt)
return False
else:
raise
def refresh(self):
"""
Check to see if there are new updates and apply them. Return true if
updates were applied, false otherwise.
"""
with self._lock:
return self._apply_states()
def get_sandbox(self):
"""Get the sandbox of this task, or None if it has not yet been discovered."""
state = self.get_state()
if state.header:
return state.header.sandbox
def get_state(self):
"""Get the latest state of this Task."""
with self._lock:
self._apply_states()
return copy.deepcopy(self._runnerstate)
def task_state(self):
state = self.get_state()
return state.statuses[-1].state if state.statuses else TaskState.ACTIVE
@property
def active(self):
return os.path.exists(self._active_file)
@property
def finished(self):
return os.path.exists(self._finished_file)
def get_active_processes(self):
"""
Get active processes. Returned is a list of tuples of the form:
(ProcessStatus object of running object, its run number)
"""
active_processes = []
with self._lock:
self._apply_states()
state = self._runnerstate
#.........这里部分代码省略.........