先介绍下 swift 的代码结构:
- bin/: Executable scripts that are the processes run by the deployer
- doc/: Documentation
- etc/: Sample config files
- examples/: Config snippets used in the docs
- swift/: Core code
- account/: account server
- cli/: code that backs some of the CLI tools in bin/
- common/: code shared by different modules
- middleware/: “standard”, officially-supported middleware
- ring/: code implementing Swift’s ring
- container/: container server
- locale/: internationalization (translation) data
- obj/: object server
- proxy/: proxy server
- test/: Unit, functional, and probe tests
部署完成 swift 以后,就可以运行类似 swift-init proxy start
, swift-init account-server start
, swift-init container-server start
, swift-init object-server start
, swift-init main start
, swift-init all start
等命令来启动相应的服务。
swift 的服务分别有:account-auditor, account-server, container-auditor, container-replicator, container-reconciler, container-server, container-sharder, container-sync, container-updater, object-auditor, object-server, object-expirer, object-replicator, object-reconstructor, object-updater, proxy-server, account-replicator, account-reaper
类似 start 的命令有: status, start, force-reload, stop, no-wait, no-daemon, reload, shutdown, restart, once
同时,server 和 start 的位置可以互换,还支持正则匹配符号 *, 如: swift-init proxy start
也可以用 swift-init start proxy
来代替,还可以用 swift-init pro* start
。
下面以在 proxy node 上启动 proxy server 为例来介绍,启动其他服务也是类似。
swift-init proxy start
命令后面是可以带参数的,可以用来重写配置文件中的部分配置,相关的参数可以在 bin/swift-init 中的 main 函数看到。
当运行 swift-init proxy start
命令后,首先运行的是 bin/swift-init 中的 main 函数,如下
1 | def main(): |
进一步看 Manager 类的构造函数,看看是怎么实例化的。其存在于 swift/swift/common/manager.py 中
1 | class Manager(object): |
实例化完 manager 对象后,其 servers 属性中保存了已经实例化了的需要进行操作的服务。然后调用 manager 的 run_command() 函数来对相应的服务进行操作。
1 | class Manager(object): |
调用 run_command() 后会根据在命令行输入的命令类型(如:start,restart,stop,…)来决定调用 manager 的哪个函数来对 server 进行相应的操作。
如果命令行输入的是 swift-init proxy start
,那么则会在 run_command() 中调用 start() 函数。
1 | class Manager(object): |
我们进一步看 Server 类的 launch() 函数是如何启动一个服务的。
1 | class Server(object): |
swift 中各个 server 的进程 pid 都是写入到文件中,默认保存在路径 /var/run/swift 下,我们看到上面 launch() 先查看指定路径下是否存在相应的文件,从而判断相应的服务是否已经启动,如果没有启动再调用 spawn() 启动。下面进一步看 spawn() 函数。
1 | class Server(object): |
至此大致流程已经分析完毕
Reference
https://blog.csdn.net/gaoxingnengjisuan/article/details/9285031
https://blog.csdn.net/gaoxingnengjisuan/article/details/9305219
https://blog.csdn.net/gaoxingnengjisuan/article/details/9315627