淘宝TFS(三)

 安装淘宝TFS记录

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 "image_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. image_data_t TfsDataFetcher::fetch_image_data(const char* tfs_file_name) { 
  23.   image_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. image_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name) 
  94.     TfsDataFetcher * fetcher = (TfsDataFetcher *)p; 
  95.     return fetcher->fetch_image_data(tfs_file_name); 

#main.c

        
        
        
        
    1. //main.c 
    2. #include <stdio.h> 
    3. #include "image_data_block.h" 
    4. #include <stdio.h> 
    5. #include "fcgi_stdio.h" 
    6. #include <unistd.h> 
    7.  
    8.  
    9. extern image_data_t invoke_tfsFetcher_fetchImageData(empty_struct * p, const char * tfs_file_name); 
    10. extern image_data_t invoke_ImageMagick_fetchImageData(empty_struct * p, image_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.         image_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. }   

  1.  

你可能感兴趣的:(TFS)