博客
关于我
Swift中的Account Server代码解读
阅读量:184 次
发布时间:2019-02-28

本文共 3931 字,大约阅读时间需要 13 分钟。

Swift的Account Server、Container Server和Object Server通过Paste Deploy加载WSGI Application,分别由AccountController、ContainerController和ObjectController处理用户的HTTP请求。这些服务与Proxy Server类似,均通过run_wsgi()函数启动。Account Server的配置文件位于/etc/swift/account-server/目录下。

Paste Deploy的配置文件中,[pipeline:main]管道定义了健康检查和重建功能,接着调用account-server模块。setup.cfg文件定义了entry_points,指定了各个服务的app_factory函数,包括swift.account.server、swift.container.server等。

结合配置文件和setup.cfg,Paste Deploy最终使用swift.account.server模块的app_factory函数加载Account Server的WSGI Application,即swift.account.server.AccountController。该Controller与swift/proxy/controllers目录下的Controller不同,后者负责将HTTP请求转发给Account Server,而AccountController则是对请求的最终处理。

以PUT操作为例,Account PUT请求有两种语义:一种是创建Account,另一种是创建Account中的Container。两者的区别在于路径参数是否包含Container信息。

AccountController的实现如下:

class AccountController(object):    """WSGI controller for the account server."""    def PUT(self, req):        """Handle HTTP PUT request."""        # 从请求参数中获取drive, part, account, container信息        drive, part, account, container = split_and_validate_path(req, 3, 4)        if self.mount_check and not check_mount(self.root, drive):            return HTTPInsufficientStorage(drive=drive, request=req)                if container:            # PUT account container            if 'x-timestamp' not in req.headers:                timestamp = Timestamp(time.time())            else:                timestamp = valid_timestamp(req)                        pending_timeout = None            container_policy_index = req.headers.get('X-Backend-Storage-Policy-Index', 0)            if 'x-trans-id' in req.headers:                pending_timeout = 3                        broker = self._get_account_broker(drive, part, account, pending_timeout=pending_timeout)            if account.startswith(self.auto_create_account_prefix) and not os.path.exists(broker.db_file):                try:                    broker.initialize(timestamp.internal)                except DatabaseAlreadyExists:                    pass            if req.headers.get('x-account-override-deleted', 'no').lower() != 'yes' and broker.is_deleted():                return HTTPNotFound(request=req)                        broker.put_container(container, req.headers['x-put-timestamp'],                                  req.headers['x-delete-timestamp'],                                  req.headers['x-object-count'],                                  req.headers['x-bytes-used'],                                  container_policy_index)            if req.headers['x-delete-timestamp'] > req.headers['x-put-timestamp']:                return HTTPNoContent(request=req)            else:                return HTTPCreated(request=req)        else:            # PUT account            timestamp = valid_timestamp(req)            broker = self._get_account_broker(drive, part, account)            if not os.path.exists(broker.db_file):                try:                    broker.initialize(timestamp.internal)                    created = True                except DatabaseAlreadyExists:                    created = False            elif broker.is_status_deleted():                return self._deleted_response(broker, req, HTTPForbidden, body='Recently deleted')            else:                created = broker.is_deleted()                broker.update_put_timestamp(timestamp.internal)                if broker.is_deleted():                    return HTTPConflict(request=req)                        metadata = {}            metadata.update((key, (value, timestamp.internal)) for key, value in req.headers.iteritems() if is_sys_or_user_meta('account', key))            if metadata:                broker.update_metadata(metadata, validate_metadata=True)                        if created:                return HTTPCreated(request=req)            else:                return HTTPAccepted(request=req)

该Controller通过路径参数分割和验证请求,检查存储位置并调用相应的Broker进行操作。Broker负责处理具体的账户操作,包括创建账户和账户中的容器。

转载地址:http://flej.baihongyu.com/

你可能感兴趣的文章
Node.js 实现类似于.php,.jsp的服务器页面技术,自动路由
查看>>
Node.js 异步模式浅析
查看>>
node.js 怎么新建一个站点端口
查看>>
Node.js 文件系统的各种用法和常见场景
查看>>
Node.js 模块系统的原理、使用方式和一些常见的应用场景
查看>>
Node.js 的事件循环(Event Loop)详解
查看>>
node.js 简易聊天室
查看>>
Node.js 线程你理解的可能是错的
查看>>
Node.js 调用微信公众号 API 添加自定义菜单报错的解决方法
查看>>
node.js 配置首页打开页面
查看>>
node.js+react写的一个登录注册 demo测试
查看>>
Node.js中环境变量process.env详解
查看>>
Node.js之async_hooks
查看>>
Node.js初体验
查看>>
Node.js升级工具n
查看>>
Node.js卸载超详细步骤(附图文讲解)
查看>>
Node.js卸载超详细步骤(附图文讲解)
查看>>
Node.js基于Express框架搭建一个简单的注册登录Web功能
查看>>
node.js学习之npm 入门 —8.《怎样创建,发布,升级你的npm,node模块》
查看>>
Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
查看>>