Skip to content

Instantly share code, notes, and snippets.

@ymmt2005
Created February 3, 2016 08:00
Show Gist options
  • Select an option

  • Save ymmt2005/a32c0e712b7f16ff05e2 to your computer and use it in GitHub Desktop.

Select an option

Save ymmt2005/a32c0e712b7f16ff05e2 to your computer and use it in GitHub Desktop.

Revisions

  1. ymmt2005 revised this gist Feb 3, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bittorrent.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    BitTorrent 勉強会
    =================
    BitTorrent 調査まとめ
    =====================

    概要
    ----
  2. ymmt2005 created this gist Feb 3, 2016.
    283 changes: 283 additions & 0 deletions bittorrent.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,283 @@
    BitTorrent 勉強会
    =================

    概要
    ----

    [BitTorrentの仕組み](http://jp.bitcomet.com/doc/principles.htm)を読むのが早い。

    完全な P2P というわけではなく、コンテンツとピアの情報を管理する**トラッカー**というサーバーがいる。トラッカー不在でも動作する**DHTモード**という仕組みもある。

    多数のピアから分散ダウンロードする仕組みなので、以下の場合に有効。

    * 巨大なファイルを複数サーバーでダウンロードする
    * そこそこのファイルを非常に多数のサーバーでダウンロードする

    逆に言えば、帯域が問題にならないケースで利用する意味は薄い。

    Winny と違い、ダウンロードしようとしていないファイルを勝手にダウンロード
    することはないので、その点は安心。

    準備
    ----

    Ubuntu 14.04 に以下のパッケージをインストールしておく。

    * `transmission-cli`
    * `transmission-daemon`

    transmission-daemon はデフォルトで DHT 有効になっているので、
    インストール後はひとまず停止しておくのが無難。

    `transmission-daemon``transmission-remote` コマンドで管理する。
    `transmission-daemon` の RPC の初期パスワードは[明記されていないが][Bug#747588]`transmission`

    例えば新たなファイルをシードする方法は以下に詳しい。

    * [How To Create and Seed a Torrent](https://evpo.wordpress.com/2015/01/11/how-to-create-seed-torrent-ubuntu-transmission/)

    用語
    ----

    * [BitTorrent Glossary](http://help.bittorrent.com/customer/portal/articles/179175-glossary)
    * [シード、ピアなどの用語について教えてください](http://jp.bitcomet.com/faq/words.htm)

    ### torrent (torrent file)

    トレントファイルには、ファイルの情報とトラッカーの情報が記録されている。
    クライアントはトラッカーにアクセスしてファイルを持つピアの情報を入手し、
    分散ダウンロードする仕組みとなっている。

    torrent ファイルの作成には `transmission-create` コマンドを使う。

    ### DHT Network

    * [DHTネットワークって何ですか?](http://jp.bitcomet.com/faq/dht.htm)

    トラッカーが落ちたときでもダウンロードができるよう、ピア同士で相手を
    探してネットワークを構築する仕組み。UDP でピアを探すのでネットワーク
    に負荷がかかるとのこと。

    データセンター内であれば、ここは最適化できそうではある。

    ### Seed/Seeder

    torrent の指すファイルを完全に保持しており、他のピアに配信している
    ピアのこと。torrent 単位であって、ある torrent のシードでも、完全に
    保持していない torrent についてはピアのまま。

    ### Web Seed

    HTTP/FTP サーバーをシードサーバーにする機能。torrent ファイルに
    ダウンロード用の URL を埋め込む拡張で、対応しているクライアントなら
    HTTP/FTP サーバーをシードとして利用する。

    * [BEP19: WebSeed - HTTP/FTP Seeding (GetRight style)][BEP19]

    ### Magnet URI

    * [What are magnet links, and how do I use them to download torrents?](http://lifehacker.com/5875899/what-are-magnet-links-and-how-do-i-use-them-to-download-torrents)
    * [Wikipedia](https://en.wikipedia.org/wiki/Magnet_URI_scheme)

    torrent ファイルにかわり、DHT を前提にファイルのハッシュを URI にしたもの。
    トラッカーの情報は含まれない。torrent ファイルを magnet に変換するには、
    以下のようにする。

    ```
    transmission-show -m *.torrent
    ```

    Magnet URI には HTTP の URL もフォールバック用に `as` パラメータとして
    含めることができる。しかしながら、クライアントが対応していないことがある。

    * https://en.wikipedia.org/wiki/Magnet_URI_scheme#Clients_table

    対応しているクライアントであれば、通常の HTTP サーバーに初期シードさせる
    ことができる、気がする(要検証)。

    Wikipedia にはないが、`ws` という Web Seed 用のパラメータがあり、これは
    transmission も対応している。[ここ](https://wiki.theory.org/BitTorrent_Magnet-URI_Webseeding#Retrieving_metadata_-_.22ws.3D.22_parameter)によると、`ws`
    に指定した URL + ".torrent" として torrent ファイルをメタデータとして
    置いておくべし、とある。

    * [BitTorrent Magnet-URI Webseeding][magnet_ws]

    活用例
    ------

    ### Twitter

    * [Murder: Fast datacenter code deploys using BitTorrent](https://blog.twitter.com/2010/murder-fast-datacenter-code-deploys-using-bittorrent)

    Murdur という OSS を公開している。Ruby のデプロイツールである
    Capistrano に組込んで使うようになっている。40分かかっていたデプロイ
    作業が 12 秒に短縮されたとのこと。

    ### Facebook

    * [behind-the-scenes look at Facebook release engineering](http://arstechnica.com/business/2012/04/exclusive-a-behind-the-scenes-look-at-facebook-release-engineering/)

    HipHop でコンパイルした 1.5 GB のバイナリを配布するのに利用。
    Tracker は独自作成のもので、同じラックのサーバーを優先的に利用するようにしている。

    * [Facebook uses BitTorrent, and they love it](https://torrentfreak.com/facebook-uses-bittorrent-and-they-love-it-100625/)

    ### eBay

    * [BitTorrent for Package Distribution in the Enterprise](http://www.ebaytechblog.com/2012/01/31/bittorrent-for-package-distribution-in-the-enterprise/)

    データセンター内で BitTorrent 利用するなら、という解説。
    WebSeed 使うとかパラメータ調整するとか。eBay での利用の解説はなし。


    各種実装
    --------

    ### Transmission

    `transmission-gtk` は Ubuntu Desktop の標準 BitTorrent クライアントに
    なっている。`transmission-daemon` としてデーモンとしても動作し、サーバーで
    常時走らせることも可能。

    * [wiki][transmission]

    ### Deluge

    後述する [libtorrent][] の Python バインディングで作成された
    高機能クライアント。とあるが、ドキュメント不足でインストールしても
    まともに使えない。。

    ### Murdur

    Twitter は BitTorrent の配布の仕組みを Murdur という OSS として公開している。
    Capistrano 連携を除くと、Python で書かれた [BitTornado][] をもとにした非常に
    シンプルな仕組みとなっている。BitTornado の開発は 2006 年から停滞しており、
    Python3 対応も見込めないことから、参考に留めるのが良さそう。

    * [GitHub][Murdur]

    ### Go

    * [Taipei-Torrent][]

    一通りクライアント機能はあるようだが、トラッカーの実装は初期にあたえた
    torrent のアナウンスしかないようだ。
    torrent 増やすには再起動? しかしそうすると、ピアの情報が失われる。。

    * [anacrolix/torrent][]

    ライブラリとしての利用を前提とある。かなり巨大な実装。
    一方でトラッカーはクライアントからトラッカーにつなぐ部分しか実装がない。

    短くいうと、トラッカーは別で調達する必要はあるが、クライアントは作れる or ある。

    ### C/C++

    C++ で実装された [libtorrent][] を使うものが多い。
    Python binding もあり、頻繁にリリースもされている。

    うまくいかない実験
    ------------------

    magnet URI に `ws` パラメータを付加して `transmission-daemon`
    ダウンロードさせようとしたが、ダウンロードが開始されなくて困ったの図。

    他にピアがいないからなのか、やはり Web サーバーに `.torrent` ファイルを
    置いてないからなのか、まだ調査できていない。

    ```
    $ transmission-remote -n transmission:transmission -a 'magnet:?xt=urn:btih:d9aeb6e7bfbd65886dc2394d19a473c01d53c69e&dn=block-modules-2.6.32-21-generic-di%5F2.6.32-21.32%5Famd64.udeb&ws=http%3a%2f%2farchive%2eubuntu%2ecom%2fubuntu%2fpool%2fmain%2fl%2flinux%2fblock%2dmodules%2d2%2e6%2e32%2d21%2dgeneric%2ddi_2%2e6%2e32%2d21%2e32_amd64%2eudeb'
    localhost:9091/transmission/rpc/ responded: "success"
    $ transmission-remote -n transmission:transmission -l
    ID Done Have ETA Up Down Ratio Status Name
    1 n/a None Unknown 0.0 0.0 None Idle memtest86+.bin
    2 n/a None Unknown 0.0 0.0 None Idle block-modules-2.6.32-21-generic-di_2.6.32-21.32_amd64.udeb
    Sum: None 0.0 0.0
    $ transmission-remote -n transmission:transmission -t 2 -i
    NAME
    Id: 2
    Name: block-modules-2.6.32-21-generic-di_2.6.32-21.32_amd64.udeb
    Hash: d9aeb6e7bfbd65886dc2394d19a473c01d53c69e
    Magnet: magnet:?xt=urn:btih:d9aeb6e7bfbd65886dc2394d19a473c01d53c69e&dn=block-modules-2.6.32-21-generic-di%5F2.6.32-21.32%5Famd64.udeb&ws=http%3A%2F%2Farchive.ubuntu.com%2Fubuntu%2Fpool%2Fmain%2Fl%2Flinux%2Fblock-modules-2.6.32-21-generic-di%5F2.6.32-21.32%5Famd64.udeb
    TRANSFER
    State: Idle
    Location: /var/lib/transmission-daemon/downloads
    Percent Done: nan%
    ETA: 0 seconds (0 seconds)
    Download Speed: 0 kB/s
    Upload Speed: 0 kB/s
    Have: None (None verified)
    Availability: None
    Availability: nan%
    Total size: None (None wanted)
    Downloaded: None
    Uploaded: None
    Ratio: None
    Corrupt DL: None
    Peers: connected to 0, uploading to 0, downloading from 0
    Web Seeds: downloading from 0 of 1 web seeds
    HISTORY
    Date added: Wed Feb 3 09:53:23 2016
    Date started: Wed Feb 3 09:53:23 2016
    Downloading Time: 3 minutes, 17 seconds (197 seconds)
    ORIGINS
    Public torrent: Yes
    Piece Count: 0
    Piece Size: None
    LIMITS & BANDWIDTH
    Download Limit: Unlimited
    Upload Limit: Unlimited
    Ratio Limit: Default
    Honors Session Limits: Yes
    Peer limit: 50
    Bandwidth Priority: Normal
    $ sudo transmission-show /var/lib/transmission-daemon/info/torrents/block-modules-2.6.32-21-generic-di_2.6.32-21.32_amd64.udeb.d9aeb6e7bfbd6588.torrent
    Name: block-modules-2.6.32-21-generic-di_2.6.32-21.32_amd64.udeb
    File: torrents/block-modules-2.6.32-21-generic-di_2.6.32-21.32_amd64.udeb.d9aeb6e7bfbd6588.torrent
    GENERAL
    Name: block-modules-2.6.32-21-generic-di_2.6.32-21.32_amd64.udeb
    Hash: d9aeb6e7bfbd65886dc2394d19a473c01d53c69e
    Created by:
    Created on: Unknown
    Piece Count: 0
    Piece Size: 0.00 KiB
    Total Size: 0.00 kB
    Privacy: Public torrent
    TRACKERS
    WEBSEEDS
    http://archive.ubuntu.com/ubuntu/pool/main/l/linux/block-modules-2.6.32-21-generic-di_2.6.32-21.32_amd64.udeb
    FILES
    ```

    所感
    ----

    トラッカーどうするかがちょっと悩ましい。
    Seeding はやはり Web Seeding できるほうが楽。

    magnet URI 対応しているなら、BitTorrent プロトコルにこだわる必要は
    必ずしもないかもしれない。と思ってみたが、Gnutella はじめ、BitTorrent ほど
    クライアントや実装の選択肢が広いものはない。

    [Murdur]: https://github.com/lg/murder
    [BitTornado]: http://www.bittornado.com/
    [Taipei-Torrent]: https://github.com/jackpal/Taipei-Torrent
    [anacrolix/torrent]: https://github.com/anacrolix/torrent
    [libtorrent]: http://www.libtorrent.org/
    [transmission]: https://trac.transmissionbt.com/wiki
    [Bug#747588]: https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/747588
    [magnet_ws]: https://wiki.theory.org/BitTorrent_Magnet-URI_Webseeding
    [BEP19]: http://www.bittorrent.org/beps/bep_0019.html