分享

OpenStack Swift源码导读之可插拔的后端设备实现

xioaxu790 发表于 2014-9-29 14:37:37 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 7289
问题导读
1、是什么让Swift存储的构建更加灵活了呢?
2、/swift/swift/obj/mem_diskfile.py用于什么?
3、如何理解REST API?






Swift作为一个存储的具体实现出现在OpenStack中,与Cinder的定位有差别,这导致Swift的兼容并包性不够强。必须基于XFS文件系统来存储数据?显然Swift也希望能将数据存储到更多的后端设备中,这样Swift可以与具体的XFS文件系统解耦,作为独立的存储软件存在。这能使得Swift存储的构建更加灵活,同时也能吸引更多的存储厂商投入到其怀抱中。

Swift提供了一种简单机制来实现后端存储设备的pluggable——可插拔的后端。这篇文章想探讨一下该机制。在亚特兰大峰会上面,这一特性是Swift的热门话题之一,对于亚特兰大OpenStack峰会涉及Swift的话题这里有汇总:链接。

看起来很有意思的创举,细看起代码来,其实挺简单的。有句话说,代码之外,了无秘密。Swift对于存储介质的要求其实挺简单的,提供读取,写入接口,涉及两种类型的数据:对象数据和元数据。对于面向对象而言,这个接口的实现就很容易了。

/swift/swift/obj/server.py文件定义了REST API,在各个API中有访问DiskFile的流程,其实也就是对DiskFile需要提供的接口的要求。那么只要新的DiskFile实现各个API中需要的接口即可。这样接口其实是固定的。OpenStack的官方文档给出了接口的详细描述:Back-end API for Object Server REST APIs。新的设备如何接入?怎样被业务访问到呢?是不是整个obj目录要整体替换掉,那样显得很笨拙,有很多代码是可以共用,就像刚才提到的REST API这一部分是固定的,完全可以保留。但是从下面的代码来看:
  1. class ObjectController(object):
  2. def setup(self, conf):
  3.     """
  4.     Implementation specific setup. This method is called at the very end
  5.     by the constructor to allow a specific implementation to modify
  6.     existing attributes or add its own attributes.
  7.     :param conf: WSGI configuration parameter
  8.     """
  9.     # Common on-disk hierarchy shared across account, container and object
  10.     # servers.
  11.     self._diskfile_mgr = DiskFileManager(conf, self.logger)
  12.     # This is populated by global_conf_callback way below as the semaphore
  13.     # is shared by all workers.
  14.     if 'replication_semaphore' in conf:
  15.         # The value was put in a list so it could get past paste
  16.         self.replication_semaphore = conf['replication_semaphore'][0]
  17.     else:
  18.         self.replication_semaphore = None
  19.     self.replication_failure_threshold = int(
  20.         conf.get('replication_failure_threshold') or 100)
  21.     self.replication_failure_ratio = float(
  22.         conf.get('replication_failure_ratio') or 1.0)
  23. def get_diskfile(self, device, partition, account, container, obj,
  24.                  **kwargs):
  25.     """
  26.     Utility method for instantiating a DiskFile object supporting a given
  27.     REST API.
  28.     An implementation of the object server that wants to use a different
  29.     DiskFile class would simply over-ride this method to provide that
  30.     behavior.
  31.     """
  32.     return self._diskfile_mgr.get_diskfile(
  33.         device, partition, account, container, obj, **kwargs)
复制代码


从上面加粗的diskfile_mgr成员来看,似乎server类与diskfile耦合了,绑定在一起了。但是再看,也只有这两处涉及到了具体的Diskfile相关类的对象的生成。只要能够向server的ObjectController类中“注入”自定义的Diskfile Manager等类的对象即可。很自然的就想到了开放出setup接口,提供一个类似于setdiskfile(SpecialDiskfileMgr diskfilemgr)的接口,这样其实破坏了封装性,不是面向对象的实现。并且,ObjectController是框架生成的,不易于获取到其运行时的实例。考虑另外一种思路,就是继承server中的ObjectController,实现一个新的ObjectController,这个ObjectController只需要重写setup方法即可。如果有必要,将get_diskfile也重写一下,如果接口定义得足够优雅,那么只需要前者。运行时,将新的ObjectController注册到WSGI框架中即可。正好WSGI框架是通过配置来指定具体的server类的。因此只需要修改配置即可同时支持多种不同的后端。

Swift提供一个简单的样例,一个内存文件系统的后端接口:

/swift/swift/obj/mem_diskfile.py定义了一整套的上述文档中规定的接口的实现。/swift/swift/obj/mem_server.py中定义了新的ObjectController,供客户配置使用。只需要修改/etc/swift/object-server.conf中的pipline中的最后的server指向新的ObjectController即可。

看具体ObjectController的代码:
  1. class ObjectController(server.ObjectController):
  2.     """
  3.     Implements the WSGI application for the Swift In-Memory Object Server.
  4.     """
  5.     def setup(self, conf):
  6.         """
  7.         Nothing specific to do for the in-memory version.
  8.         :param conf: WSGI configuration parameter
  9.         """
  10.         self._filesystem = InMemoryFileSystem()
  11.     def get_diskfile(self, device, partition, account, container, obj,
  12.                      **kwargs):
  13.         """
  14.         Utility method for instantiating a DiskFile object supporting a given
  15.         REST API.
  16.         An implementation of the object server that wants to use a different
  17.         DiskFile class would simply over-ride this method to provide that
  18.         behavior.
  19.         """
  20.         return self._filesystem.get_diskfile(account, container, obj, **kwargs)
复制代码



相信不久的将来,Swift会有越来越多的后端存储设备可以选用,这其实相当于Swift变成一个存储的管理软件,集成各种存储设备的适配“驱动”即可实现将数据存储到其上。

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条