处理故障中断与恢复中断
发送机(transmitter)和接收机(receiver)模块会自动处理其父节点的故障与恢复。但是,其中运行有进程的处理器(processor)模块或队列(queue)模块并不具备处理故障中断和恢复中断的内置能力。下面介绍三个为处理故障中断与恢复中断而设计的进程的典型示例。
路由拓扑示例
如果一个节点要维护一张路由拓扑,并在节点和链路状态变化时动态更新,典型做法是在节点模型中加入一个专门负责该任务的模块。你应当把该模块的进程模型的 failure intrpts 和 recovery intrpts 属性设为 “network wide”。当系统中任何节点或链路的状态发生变化时,该模块内的根进程会:
- 收到一个故障中断或恢复中断;
- 调用内核过程
op_intrpt_type()判断发生的是哪种类型的中断; - 更新路由拓扑以反映这一变化。
下面的图示展示了一段进程模型的示例代码片段,它根据整个网络中对象的故障与恢复来更新路由表。
图 200:根据故障/恢复事件更新拓扑的 Proto-C 示例
/** This state is entered when a link connected to this node or**/
/** a directly connected router fails/recovers. In both cases **/
/** the link status is changed. **/
/** Link failure leads to setting the metric for the entries in**/
/** the RIP routing table for which the next hop was reachable **/
/** through the failed interface to infinite. **/
/** Link recovery inserts the route to the directly connected **/
/** neighbor on the recovered link into the routing table. **/
/** Both cases trigger sending of RIP route update messages. **/
if (intrpt_type == OPC_INTRPT_FAIL)
{
/** Link has failed. **/
/* Handle the failure of the connected link. */
status_changed = rip_link_fail (table_index);
}
else
{
/** Link has recovered. **/
/* Handle the link recovery. */
status_changed = rip_link_recover (table_index);
}
/* Routing table has changed. Schedule generation of*/
/* route update messages. */
•
•
•
节点故障与恢复示例
节点故障通常被视为节点内各模块的灾难性事件。发送机会自动中止正在传输的任何数据包并清空其发送队列;接收机在节点被禁用期间不会接收数据包。此外,进程可能需要获知节点故障的通知,以便使自己保持在干净的状态。例如,队列模块内的进程可能需要清空其数据包缓冲区,或者进程可能销毁其子进程。如果一个模块需要获知其父节点故障的通知,可以把该模块的 failure intrpt 属性设为 “local only”。设置为接收该中断的进程将在节点真正故障之前被调用,从而可以采取处理节点故障所需的任何动作。通常,进程调用内核过程 op_intrpt_type() 来判断该中断是一个故障中断。下面的图示展示了一段进程模型的示例代码片段,它在父节点故障之前清空自己的数据包缓冲区。
图 201:处理包括故障中断在内的多种中断类型的 Proto-C 示例
/* This process just received an interrupt. Based on the type of */
/* interrupt, take the appropriate actions. */
/* Determine which interrupt type just occurred. */
intrpt_type = op_intrpt_type ();
if (intrpt_type == OPC_INTRPT_STREAM)
{
/* Received stream intrpt. Call procedure to handle the packet. */
packet_receive_and_enqueue ();
}
else if (intrpt_type == OPC_INTRPT_SELF)
{
/* Received self interrupt. Call procedure to send a packet. */
packet_dequeue_and_send ();
}
else if (intrpt_type == OPC_INTRPT_FAIL)
{
/* This process just received a failure interrupt for its */
/* parent node and must flush the subqueues and cancel self */
/* interrupts. */
/* Flush the subqueues. */
op_q_flush ();
/* Cancel all self interrupts. */
op_intrpt_clear_self ();
}
类似地,当节点恢复时,该节点内的进程往往需要重新初始化自身。例如,进程可以复位自己的计数器、将数组清零,并转移到等待接收数据包的状态。如果一个进程只需要在其父节点恢复时得到通知,那么就把它所在模块的 recovery intrpts 属性设为 “local only”,这样当节点恢复时,一个恢复中断就会投递到该模块。进程调用内核过程 op_intrpt_type() 把这种中断与其他中断(如流中断或自身中断)区分开来。下面的代码清单展示了一段进程模型的示例代码片段,它在父节点恢复时重新初始化自己的状态变量。
图 202:处理包括恢复中断在内的多种中断类型的 Proto-C 示例
/* This process just received an interrupt. Based on the type of */
/* interrupt, take the appropriate actions. */
/* Determine which interrupt type just occurred. */
intrpt_type = op_intrpt_type ();
if (intrpt_type == OPC_INTRPT_SELF)
{
/* Received self interrupt. Re-transmit packet. */
packet_retransmit ();
}
else if (intrpt_type == OPC_INTRPT_STREAM)
{
/* Received stream interrupt. Handle the packet. */
packet_receive ();
}
else if (intrpt_type == OPC_INTRPT_RECOVER)
{
/* This process just received a recovery interrupt for */
/* its parent node and must re-initialize its state variables. */
/* Reset counters to 0. */
num_packets = 0;
num_errors = 0;
/* Set current time. */
start_time = op_sim_time ();
/* Reset array values. */
for (i = 0; i = NUM_ENTRIES; i++)
entry_array [i] = NULL_ENTRY;
}