本题需要模拟一个有限状态机(Finite State Machine, FSM)。我们将每个 RmAppAttempt
视为一个有状态对象,依据“当前状态 +(事件源, 事件)→ 下一状态”的确定性转移来更新。
核心做法:
RmAppAttempt
的当前状态。事件源|对象|事件
(空格分隔多个事件)。Apache Hadoop YARN 是一种新的 Hadoop 资源管理器,主要部件为 resource manager 和 node manager 。 resource manager 使用有限状态机维护有状态对象的生命周期。RmAppAttempt 是 resource manager 中用于维护一个 Application 运行尝试的生命周期的数据结构,当某个事件发生时 RmAppAttempt 会根据当前状芯进行状态迁移,同时触发一个其他行为。RmAppAttemp 相关状态和事件简述如下:
1.RmApp 发送 start 事件创建 RmAppAttempt ,初始化后,状态迁移为 submitted。
2.RmAppAttempt 在 submitted 状态下收到资源调度器 (ResouceScheduler) 发来的 app_accepted 事件,状态迁移为 scheduled 。
3.RmAppAttempt 在 scheduled 状态下收到 ReContainer 发来的 container_allocated 事件,状态迁移为 allocated 。
4.RmAppAttempt 在 scheduled 状态下收到 ApplicationMasterLauncher 发来的 launched 事件,状态迁移为 running 。
5.RmAppAttempt 在 running 状态下收到 ResouceScheduler 发来的 finished 事件,状态迁移为 finished 。
6.在 RmAppAttempt 运行过程中(不含 finished 状态),当收到来自客户端的杀死应用程序的命令时,RmApp 向 RmAppAttempt 发送 kill 事件,RmAppattemp 收到后,状态迁移到 killed 状态。
请编写一段程序模拟实现 RmAppAttempt 状态机,接收一段连续事件输入,输出每个事件处理后 yam 中对应 RmAppAttempt 对象的状态。
模拟 RmAppAttempt 状态机,接收一段连续事件输入
输入接口:(事件间用空格分隔)
事件源 ∣RmAppAttempt 对象∣ 事件
事件源:$RmApp、ResouceScheduler、ApplicationMasterLauncher、RmContainer$
事件:start、app_accepted、container _allocated、launched、finished、kill
输出接口:(输出结果间用分号;分隔)
RmAppAttempt 对象 |状态
状态:$submitted、scheduled、allocated、running、finished、killed$
无效输入通过状态机过滤,不需要任何输出
输入
RmApp|RmAppAttempt_001|start RmApp|RmAppAttempt_002|start RmApp|RmAppAttempt_001|kill
输出
RmAppAttempt_001|submitted;RmAppAttempt_002|submitted;RmAppAttempt_001|killed;