swift的部分代碼概述和學習

來源:互聯網
上載者:User

swift是openstack的object storage service。最近結合著一些文章大致梳理了一下它的原始碼。致謝牛皮糖的部落格的深入講解。

關於代碼目錄

是它的代碼目錄

其中可以看到幾個重要的檔案夾:Accout、Ring、Container、obj、proxy。

在物理主機上安裝完成的目錄為:

/usr/lib/python2.7/dist-packages/swift# lsaccount  common  container  __init__.py  __init__.pyc  obj  proxy

關於使用到的第三方

swift的對象資訊使用是SQLite資料庫。SQLite,是一款輕型的資料庫,是遵守ACID的關聯式資料庫管理系統,它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它佔用資源非常的低,在嵌入式裝置中,可能只需要幾百K的記憶體就夠了。它能夠支援Windows/Linux/Unix等等主流的作業系統,同時能夠跟很多程式語言相結合,比如 Tcl、C#、PHP、Java等,還有ODBC介面,同樣比起Mysql、PostgreSQL這兩款開源世界著名的資料庫管理系統來講,它的處理速度比他們都快。

可以通過下面的方法來輸出資料庫包含的表

import sqlite3cx=sqlite3.connect("./test.db")cu=cx.cursor()cu.execute("select name from sqlite_master where type='table'")print cu.fetchall()

如果需要查看某個資料庫的某個表的內容,可以通過下列語句來實現,利於我需要查看test.db內container的內容:

import sqlite3cx=sqlite3.connect("./test.db")cu=cx.cursor()cu.execute("select * from container")print cu.fetchall()

可以通過開啟官網的連結詳細瞭解感興趣的每一個函數,其實可以當做手冊來用:http://docs.openstack.org/developer/swift/py-modindex.html但是其中代碼並不是全部,如果需要全部的代碼,還是需要下載swift的原始碼。

關於ring

對於ring的分析有一篇部落格分析的比較好:http://www.cnblogs.com/yuxc/archive/2012/06/22/2558312.html#2438073,它的原文在http://greg.brim.net/page/building_a_consistent_hashing_ring.html。

Ring的Rebalance機制
  當叢集中發生儲存節點宕機、新增(刪)儲存節點、新增(刪)zone等必須改變partition和node間的映射關係時,就需要對Ring檔案進行更新,也就是在swift文檔中見到的rebalance一詞。

在源碼中對rebalance的定義如下:swift/Common/Ring/Builder.py

def rebalance(self):        """        Rebalance the ring.        This is the main work function of the builder, as it will assign and        reassign partitions to devices in the ring based on weights, distinct        zones, recent reassignments, etc.        The process doesn't always perfectly assign partitions (that'd take a        lot more analysis and therefore a lot more time -- I had code that did        that before). Because of this, it keeps rebalancing until the device        skew (number of partitions a device wants compared to what it has) gets        below 1% or doesn't change by more than 1% (only happens with ring that        can't be balanced no matter what -- like with 3 zones of differing        weights with replicas set to 3).        :returns: (number_of_partitions_altered, resulting_balance)        """        self._ring = None        if self._last_part_moves_epoch is None:            self._initial_balance()            self.devs_changed = False            return self.parts, self.get_balance()        retval = 0        self._update_last_part_moves()        last_balance = 0        while True:            reassign_parts = self._gather_reassign_parts()            self._reassign_parts(reassign_parts)            retval += len(reassign_parts)            while self._remove_devs:                self.devs[self._remove_devs.pop()['id']] = None            balance = self.get_balance()            if balance < 1 or abs(last_balance - balance) < 1 or \                    retval == self.parts:                break            last_balance = balance        self.devs_changed = False        self.version += 1        return retval, balance

  在基於原有的Ring來構造新Ring時,swift-ring-builder首先要重新計算每個裝置所需的partition數量。然後,將需要重新分配的partition收集起來。取消分配給被移除裝置上的partition並把這些partition添加到收集列表。從擁有比當前所需的partition數多的裝置上隨機地取消分配多出的partition並添加到收集列表中。最後,將收集列表中的partition使用與初始化分配時類似的方法重新分配。
  在本地執行swift-ring-builder命令列來產生新的ring檔案,然後把這些檔案複製到叢集的每個節點的/etc/swift目錄中,所有需要使用ring的server進程會每15秒(預設值)檢查一遍ring檔案的修改時間mtime,如果發現和記憶體中的不一致,則重新載入ring檔案到記憶體中去。
  舉例說明
  現在再增加一台儲存節點node4並作為zone5,使用相同權重的devcie。那麼每個儲存節點上的partition數是52428.8。需要從每台儲存節點上隨機地移除13107.2個partition到收集列表,然後再重新分配這些parttion到node4上。當有partition的replica被重分配時,重分配的時間將被記錄。在RingBuilder類內使用min_part_hours來限制在規定時間內,同一個partition不會被移動兩次。
  由於收集用來重新分配的partition是基於隨機的,rebalacne進程並不能一次就可以完美地重平衡ring。為了達到一個較為平衡的ring,rebalacne進程被重複執行直到接近完美(小於1%)或者當rebalacne的提升達不到最小值1%。
  具體的操作如下,首先移除舊的ring檔案:
rm -f account.builder account.ring.gz backups/account.builder backups/account.ring.gz
.......
 
  然後,重新平衡ring檔案:
swift-ring-builder account.builder create 18 3 1
swift-ring-builder account.builder add z1-192.168.1.50:6002/sdc 100
swift-ring-builder account.builder add z2-192.168.1.51:6002/sdc 100
swift-ring-builder account.builder add z3-192.168.1.52:6002/sdc 100
swift-ring-builder account.builder add z4-192.168.1.54:6002/sdc 100
swift-ring-builder account.builder add z5-192.168.1.53:6002/sdc 100
swift-ring-builder account.builder rebalance
 
swift-ring-builder container.builder create 18 3 1
swift-ring-builder container.builder add z1-192.168.1.50:6001/sdc 100
swift-ring-builder container.builder add z2-192.168.1.51:6001/sdc 100
swift-ring-builder container.builder add z3-192.168.1.52:6001/sdc 100
swift-ring-builder container.builder add z4-192.168.1.54:6001/sdc 100
swift-ring-builder container.builder add z5-192.168.1.53:6001/sdc 100
swift-ring-builder container.builder rebalance
 
swift-ring-builder object.builder create 18 3 1
swift-ring-builder object.builder add z1-192.168.1.50:6000/sdc 100
swift-ring-builder object.builder add z2-192.168.1.51:6000/sdc 100
swift-ring-builder object.builder add z3-192.168.1.52:6000/sdc 100
swift-ring-builder object.builder add z4-192.168.1.54:6000/sdc 100
swift-ring-builder object.builder add z5-192.168.1.53:6000/sdc 100
swift-ring-builder object.builder rebalance
 
  最後,複製account.ring.gz、container.ring.gz、object.ring.gz到叢集的各節點的/etc/swift目錄下。這樣我們就完成了Ring的重平衡(rebalance)。

參考文獻:http://docs.openstack.org/developer/swift/index.htm

牛皮糖的:http://www.cnblogs.com/yuxc/archive/2012/06/28/2568584.html

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.