2014年11月25日火曜日

MySQL Fabricつらい(FABRIC_OPT_MODE = roでスレーブだけを見に行かせる方法が見つかったと思ったのに失敗した)

FABRIC_OPT_MODE = roはスレーブだけを見に行くわけじゃない の時にはweightにしたがってコネクションがラウンドロビンされるから、スレーブのweightを大きくして誤魔化す的なことをしていたけど、なんかそれっぽいものを見つけた。


$ mysqlfabric group lookup_servers my_first_fabric
Password for admin:
Fabric UUID:  5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1

                         server_uuid         address    status       mode weight
------------------------------------ --------------- --------- ---------- ------
47cf54df-63fc-11e4-942e-fa163e020fd0 127.0.0.1:20886   PRIMARY READ_WRITE    1.0
4b7036a9-63fc-11e4-942e-fa163e020fd0 127.0.0.1:20887 SECONDARY  READ_ONLY    1.0



このmodeの値、"READ_WRITE", "READ_ONLY"が入ってるところの取りうるバリエーションを知りたい。
マニュアルにはread_only, read_write, offlineっぽいのが書いてはあるものの、 正直信用ならないので


$ grep -r READ_WRITE /usr/lib/python2.6/site-packages/mysql/fabric/
/usr/lib/python2.6/site-packages/mysql/fabric/server.py:    :type mode: OFFLINE, READ_ONLY, READ_WRITE.
/usr/lib/python2.6/site-packages/mysql/fabric/server.py:    READ_WRITE = "READ_WRITE"
/usr/lib/python2.6/site-packages/mysql/fabric/server.py:    SERVER_MODE = [OFFLINE, READ_ONLY, WRITE_ONLY, READ_WRITE]
Binary file /usr/lib/python2.6/site-packages/mysql/fabric/server.pyo matches
/usr/lib/python2.6/site-packages/mysql/fabric/services/highavailability.py:    master.mode = _server.MySQLServer.READ_WRITE
Binary file /usr/lib/python2.6/site-packages/mysql/fabric/services/highavailability.pyc matches
/usr/lib/python2.6/site-packages/mysql/fabric/services/server.py:        (_server.MySQLServer.WRITE_ONLY, _server.MySQLServer.READ_WRITE)
Binary file /usr/lib/python2.6/site-packages/mysql/fabric/services/highavailability.pyo matches
Binary file /usr/lib/python2.6/site-packages/mysql/fabric/services/server.pyo matches
Binary file /usr/lib/python2.6/site-packages/mysql/fabric/services/server.pyc matches
Binary file /usr/lib/python2.6/site-packages/mysql/fabric/server.pyc matches

ああ、あるね、マニュアルに載ってないのあるね。Fabricクオリティだもんね。


$ mysqlfabric server set_mode 47cf54df-63fc-11e4-942e-fa163e020fd0 WRITE_ONLY
Password for admin:
Fabric UUID:  5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1

                                uuid finished success result
------------------------------------ -------- ------- ------
0389370b-8f9e-4b68-ab2a-75e2940701cb        1       1      1

state success          when                                                   description
----- ------- ------------- -------------------------------------------------------------
    3       2   1.41689e+09 Triggered by <mysql .fabric.events.event="" 0x217fc90="" at="" object="">.
    4       2   1.41689e+09                          Executing action (_set_server_mode).
    5       2   1.41689e+09                           Executed action (_set_server_mode).


$ mysqlfabric group lookup_servers my_first_fabric
Password for admin:
Fabric UUID:  5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1

                         server_uuid         address    status       mode weight
------------------------------------ --------------- --------- ---------- ------
47cf54df-63fc-11e4-942e-fa163e020fd0 127.0.0.1:20886   PRIMARY WRITE_ONLY    1.0
4b7036a9-63fc-11e4-942e-fa163e020fd0 127.0.0.1:20887 SECONDARY  READ_ONLY    1.0



セットできた。。


$ less test.cc
    mysql_options(&mysql, FABRIC_OPT_GROUP, "my_first_fabric");
    mysql_options(&mysql, FABRIC_OPT_DEFAULT_MODE, "ro");
    mysql_options4(&mysql, FABRIC_OPT_GROUP_CREDENTIALS, "msandbox", "msandbox");

    for (n= 0; n < 1000; n++)
    {
      //mysql_options(&mysql, FABRIC_OPT_MODE, "ro");
      mysql_query(&mysql, "SELECT @@port, @@read_only");
      res= mysql_store_result(&mysql);
      row = mysql_fetch_row(res);
      std::cout << "port: " << row[0] << ", read_only: " << row[1] << std::endl;
      mysql_free_result(res);
      mysql_query(&mysql, "COMMIT");
    }

$ gcc -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqlclient -g test.cc

$ ./a.out | sort | uniq -c
    506 port: 20886, read_only: 0
    494 port: 20887, read_only: 1
      1 Using Fabric for MYSQL connection

だが有効にならないFabricクオリティ!
とはいえ。


$ gdb a.out
gdb> b Fabric_context::pick_connection
gdb> r
gdb> p *m_grp
..
  m_iter = {
    m_grp = 0x6221a0,
    m_srv_list = std::vector of length 2, capacity 2 = {{
        m_host = "127.0.0.1",
        m_port = 20886,
        m_mode = fabric::Server::WRITE_ONLY,
        m_status = fabric::Server::PRIMARY,
        m_weight = 1,
        m_uuid = {
          uuid = "G\317T\337c\374\021\344\224.\372\026>\002\017", 
        },
        m_parent_group = 0x6221a0,
        m_tainted = false
      }, {
        m_host = "127.0.0.1",
        m_port = 20887,
        m_mode = fabric::Server::READ_ONLY,
        m_status = fabric::Server::SECONDARY,
        m_weight = 1,
        m_uuid = {
          uuid = "Kp6\251c\374\021\344\224.\372\026>\002\017", 
        },
        m_parent_group = 0x6221a0,
        m_tainted = false
      }},
..

クライアント側にWRITE_ONLYは渡されてるから、Connector/Cがまだ対応してないってことなのかしら。labsとはいえ同じ会社が作ってるConnector/Cでこれだというのに、PHPのMySQL Fabric対応実装の mysqlnd とか本当に大丈夫かしら。。愛想尽かされない程度にやってくださいね。。

0 件のコメント :

コメントを投稿