淘寶TFS(三)

 安裝淘寶TFS記錄mysql

1.須要確保安裝了automake autoconfig 和 libtool,使用auotmake --version查看,通常狀況下已安裝
( 1.4以上版本須要安裝libuuid-devel,zlib-devel,mysql-devel三個開發包)
yum –y install automake autoconfig libtool zlib-devel mysql-devel uuid*
其中(autoconfig需在網站http://www.gnu.org/software/autoconf/下載)http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
 
2.TFS依賴於底層開發包tbnet,須要下載tbsys和tbnet源代碼(svn checkout http://code.taobao.org/svn/tb-common-utils/trunk/ tb-common-utils)
yum -y install libtool mysql-devel readline readline-devel zlib*
 
[root@localhost home]# cd tb-common-utils
[root@localhost tb-common-utils]# vi /etc/profile
# /etc/profile
export TBLIB_ROOT="/usr/local/tb/lib"    //定於tbsys和tbnet的安裝目錄
 
(ps:建議將這行命令直接寫入~/.bash_profile,而後執行「. ~/.bash_profile」)。 進入tb-common-utils文件夾, 執行build.sh進行安裝.
 
[root@localhost tb-common-utils]# chmod a+x build.sh
[root@localhost tb-common-utils]# ./build.sh
安裝在 /usr/local/tfs_lib 目錄下了
 
 
3.得到tfs源代碼:( svn checkout http://code.taobao.org/svn/tfs/trunk/ tfs)
 
4.安裝gtest庫:  http://code.google.com/p/googletest/downloads/list
 
a、下載,解壓,進入該目錄,按REAME說明 
 
b、g++ -I./include -I./ -c ./src/gtest-all.cc (注意,-I後沒有空格,直接加./)
 
c、ar -rv libgtest.a gtest-all.o
 
這步以後會生成兩個文件,libgtest.a和gtest-all.o做用不是很清楚,總之是庫。。。
 
d、g++ -I./include mytest.cpp libgtest.a -o mytest -lpthread (注意mytest爲本身寫的簡單測試代碼,編譯時注意加-lpthread,否則編譯會報錯:undefined reference to ...)
 
 
若是搭建單臺ds,請在ns.conf中將
#Block 最大備份數, default: 2
max_replication = 2
#Block 最小備份數, default: 2
min_replication = 2 改成1,不然集羣將沒法正常運行。
ds.conf中關於Nameserver的三個配置項必須和ns.conf中的一致,
[dataserver]
#!NameServer vip地址
ip_addr = 192.168.0.1
#!nameserver IP地址列表(master, salve的ip地址,只能以'|'分隔)
ip_addr_list = 192.168.0.1|192.168.0.2
#!NameServer 監聽的端口, 1024 ~ 55535
port = 9999 不然ds將沒法和ns通訊。
ns.conf中block_max_size通常設爲和mainblock_size相同或略大於mainblock_size,這樣能夠儘可能少使用擴展塊。
 
 
 
 
 
gcc編譯tfs c++客戶端
1.指定相關路徑(詳見tfs_demo.cpp 跟官方網例子同樣)
gcc tfs_demo.cpp -o tfs_demo.o -I/usr/local/tfs-2.0.3/include -I/usr/local/tb/lib/include/tbsys -I/usr/local/tb/lib/include/tbnet/ -L/usr/lib -L/usr/bin -L/usr/local/tb/lib  -L/usr/local/tb/lib/lib -ltfsclient  -ltbsys  -ltbnet -lz
 
gcc tfs_data_fetcher.cpp -o tfs_data_fetcher.o -I/usr/local/tfs-2.0.3/include -I/usr/local/tb/lib/include/tbsys -I/usr/local/tb/lib/include/tbnet/
 
tfs C++編譯經過:
gcc -c tfs_data_fetcher.cpp -o tfs_data_fetcher.o -I/usr/local/tfs-2.0.3/include -I/usr/local/tb/lib/include/tbnet/ -I/usr/local/tb/lib/include/tbsys
 
2.在/etc/ld.so.conf.d/
/usr/local/tfs-2.0.3/lib
/usr/local/tb/lib/lib
 
#tfs_data_fetcher.cpp
 
  
  
  
  
  1. #include <stdio.h> 
  2. #include <string> 
  3. #include <func.h> 
  4. #include <tfs_client_api.h> 
  5. #include "p_w_picpath_data_block.h" 
  6. #include "tfs_data_fetcher.h" 
  7.  
  8. using namespace std; 
  9. using namespace tfs::client; 
  10. using namespace tfs::common; 
  11.  
  12. const char* nsip = "10.168.100.240:8108"
  13.  
  14. TfsDataFetcher::TfsDataFetcher() 
  15. {} 
  16.  
  17. TfsDataFetcher::~TfsDataFetcher() 
  18. {} 
  19.  
  20. static TfsClient* tfsclient = TfsClient::Instance(); 
  21.  
  22. p_w_picpath_data_t TfsDataFetcher::fetch_p_w_picpath_data(const char* tfs_file_name) { 
  23.   p_w_picpath_data_t rtn; 
  24.   int ret = 0; 
  25.   int fd = -1; 
  26.    
  27.   tfsclient->initialize(nsip); 
  28.   // 打開待讀寫的文件 
  29.   fd = tfsclient->open(tfs_file_name, NULL, T_READ); 
  30.   if (ret != TFS_SUCCESS) 
  31.   { 
  32.     printf("open remote file %s error", tfs_file_name); 
  33.     rtn.data = NULL; 
  34.     rtn.len = 0; 
  35.     return rtn; 
  36.   } 
  37.  
  38.   // 得到文件屬性 
  39.   TfsFileStat fstat; 
  40.   ret = tfsclient->fstat(fd, &fstat); 
  41.   if (ret != TFS_SUCCESS || fstat.size_ <= 0) 
  42.   { 
  43.     printf("get remote file info error"); 
  44.     rtn.data = NULL; 
  45.     rtn.len = 0; 
  46.     return rtn; 
  47.   } 
  48.        
  49.   char* buffer = new char[fstat.size_]; 
  50.   int read = 0; 
  51.   uint32_t crc = 0; 
  52.      
  53.   // 讀取文件 
  54.   while (read < fstat.size_) 
  55.   { 
  56.     ret = tfsclient->read(fd, buffer + read, fstat.size_ - read); 
  57.     if (ret < 0) 
  58.     { 
  59.       break
  60.     } 
  61.     else 
  62.     { 
  63.       crc = Func::crc(crc, buffer + read, ret); // 對讀取的文件計算crc值 
  64.       read += ret; 
  65.     } 
  66.   } 
  67.  
  68.   if (ret < 0 || crc != fstat.crc_) 
  69.   { 
  70.     printf("read remote file error!\n"); 
  71.     delete []buffer; 
  72.     rtn.data = NULL; 
  73.     rtn.len = 0; 
  74.     return rtn; 
  75.   } 
  76.  
  77.   ret = tfsclient->close(fd); 
  78.   if (ret < 0) 
  79.   { 
  80.     printf("close remote file error!"); 
  81.     delete []buffer; 
  82.     rtn.data = NULL; 
  83.     rtn.len = 0;     
  84.     return rtn; 
  85.   } 
  86.    
  87.   rtn.data = (u_char *)buffer; 
  88.   rtn.len = (int)fstat.size_; 
  89.   //delete []buffer; 
  90.   return rtn; 
  91.  
  92. extern "C" 
  93. p_w_picpath_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name) 
  94.     TfsDataFetcher * fetcher = (TfsDataFetcher *)p; 
  95.     return fetcher->fetch_p_w_picpath_data(tfs_file_name); 

#main.cc++

        
        
        
        
    1. //main.c 
    2. #include <stdio.h> 
    3. #include "p_w_picpath_data_block.h" 
    4. #include <stdio.h> 
    5. #include "fcgi_stdio.h" 
    6. #include <unistd.h> 
    7.  
    8.  
    9. extern p_w_picpath_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name); 
    10. extern p_w_picpath_data_t invoke_ImageMagick_fetchImageData(empty_struct * p, p_w_picpath_data_t  config); 
    11.    
    12. empty_struct p;   
    13. empty_struct m; 
    14.  
    15. int main(int argc, char**argv)   
    16.   while (FCGI_Accept() >= 0) { 
    17.        
    18.          
    19.         const char* tfs_file_name = "T1rEbTB_b_1RCvBVdK";    
    20.         p_w_picpath_data_t config = invoke_tfsFetcher_fetchImageData(&p,  tfs_file_name); 
    21.         if(config.len == 0 || config.data == NULL){ 
    22.             return -1; 
    23.         }    
    24.          
    25.        fwrite(config.data,config.len,1,stdout); 
    26.        fflush(stdout); 
    27.          
    28.     } 
    29.     return 0; 
    30. }   
相關文章
相關標籤/搜索