目录
Orchestrator中有很多hooks函数,用于调用外部服务做一些配置或者准备等工作。
hooks分类
Orchestrator中主要有以下这些hooks,按照其执行顺序依次为:
PreGracefulTakeoverProcesses
(只对graceful执行)OnFailureDetectionProcesses
PreFailoverProcesses
PostMasterFailoverProcesses
PostUnsuccessfulFailoverProcesses
(失败时执行)PostFailoverProcesses
(成功时执行)PostGracefulTakeoverProcesses
(只对graceful执行)
这些hooks分布在Orchestrator recovery 过程中的不同位置,用于调用外部服务,做一些补充工作。
hooks的执行
说到hooks,就要看下hooks是如何执行的,这里就涉及到executeProcesses
函数:
// executeProcesses executes a list of processes
func executeProcesses(processes []string, description string, topologyRecovery *TopologyRecovery, failOnError bool) (err error) {
if len(processes) == 0 {
AuditTopologyRecovery(topologyRecovery, fmt.Sprintf("No %s hooks to run", description))
return nil
}
AuditTopologyRecovery(topologyRecovery, fmt.Sprintf("Running %d %s hooks", len(processes), description))
for i, command := range processes {
command, async := prepareCommand(command, topologyRecovery)
env := applyEnvironmentVariables(topologyRecovery)
fullDescription := fmt.Sprintf("%s hook %d of %d", description, i+1, len(processes))
if async {
fullDescription = fmt.Sprintf("%s (async)", fullDescription)
}
if async {
// Ignore errors
go executeProcess(command, env, topologyRecovery, fullDescription)
} else {
if cmdErr := executeProcess(command, env, topologyRecovery, fullDescription); cmdErr != nil {
if failOnError {
AuditTopologyRecovery(topologyRecovery, fmt.Sprintf("Not running further %s hooks", description))
return cmdErr
}
if err == nil {
// Keep first error encountered
err = cmdErr
}
}
}
}
AuditTopologyRecovery(topologyRecovery, fmt.Sprintf("done running %s hooks", description))
return err
}
位于go/logic/topology_recovery.go
文件中。
所有hook执行都通过调用该函数来实现,这个函数最后一个参数,表示,如果hook执行失败时,是否会导致recovery异常退出。
通过分析这个函数的调用关系,整理如下结论:
下面参数指定的hook脚本执行失败会导致recovery过程异常退出:
PreFailoverProcesses
OnFailureDetectionProcesses
PreGracefulTakeoverProcesses
其余的hook执行失败都不会导致recovery异常退出。
在executeProcesses
中,通过环境环境变量传递参数,调用shell脚本。