免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4629 | 回复: 3

[FastDFS] 请教fish:fastdfs v5.03 client api不能多线程并发调用的问题 [复制链接]

论坛徽章:
0
发表于 2014-09-05 10:52 |显示全部楼层
本帖最后由 liujian0616 于 2014-09-05 14:39 编辑

之前有个关于多线程的帖子我看了,时间过去太久,没什么参考价值,而且我在测试代码里也做到了   每个线程使用自己的tracker server ,还是报错

目前我需要在项目中用到fastdfs,但是项目总体架构用的是多线程模型,我在测试的时候发现client的api在多线程下是不安全的,会报段错误,加锁运行就没问题,但是加锁后这效率就太低了,不能支持大并发了

下面是我的测试代码,是根据fish您的代码改的:
  1. /**
  2. * Copyright (C) 2008 Happy Fish / YuQing
  3. *
  4. * FastDFS may be copied only under the terms of the GNU General
  5. * Public License V3, which may be found in the FastDFS source kit.
  6. * Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
  7. **/

  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include "fdfs_client.h"
  16. #include "logger.h"
  17. #include <getopt.h>
  18. #include <pthread.h>


  19. typedef struct test_fileinfo_s test_fileinfo_t;
  20. struct test_fileinfo_s{
  21.     char *filename;
  22.     char storage_ip[16];
  23.     char fileid[128];
  24.     int threadid;
  25.     long long time_used;
  26. };

  27. static test_fileinfo_t files[] = {
  28.     {"/usr/local/include/fastcommon/common_define.h",   "", "", 0, 0},
  29.     {"/usr/local/include/fastcommon/connection_pool.h", "", "", 0, 0},
  30.     {"/usr/local/include/fastcommon/fast_task_queue.h", "", "", 0, 0},
  31.     {"/usr/local/include/fastcommon/base64.h",          "", "", 0, 0},
  32.     {"/usr/local/include/fastcommon/hash.h",            "", "", 0, 0}
  33. };

  34. pthread_mutex_t g_mutex;

  35. void usage(){
  36.     printf("-t  thread num \n"
  37.            "-h  print this message");
  38.     return;
  39. }

  40. void* routine(void *param)
  41. {
  42.     //pthread_mutex_lock(&g_mutex);   //不加锁会有段错误
  43.         test_fileinfo_t *file = (test_fileinfo_t *)param;
  44.         char *conf_filename = "/etc/fdfs/client.conf";
  45.     char *local_filename = NULL;
  46.        
  47.         char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
  48.         ConnectionInfo *pTrackerServer;
  49.         int result;
  50.         int store_path_index;
  51.         ConnectionInfo storageServer;
  52.         char file_id[128];
  53.        

  54.         log_init();
  55.         g_log_context.log_level = LOG_ERR;
  56.         ignore_signal_pipe();

  57.         if ((result=fdfs_client_init(conf_filename)) != 0)
  58.         {
  59.                 return result;
  60.         }

  61.         pTrackerServer = tracker_get_connection();
  62.         if (pTrackerServer == NULL)
  63.         {
  64.                 fdfs_client_destroy();
  65.                 return errno != 0 ? errno : ECONNREFUSED;
  66.         }

  67.         local_filename = file->filename;
  68.         *group_name = '\0';
  69.        
  70.         if ((result=tracker_query_storage_store(pTrackerServer, \
  71.                         &storageServer, group_name, &store_path_index)) != 0)
  72.         {
  73.                 fdfs_client_destroy();
  74.                 fprintf(stderr, "tracker_query_storage fail, " \
  75.                         "error no: %d, error info: %s\n", \
  76.                         result, STRERROR(result));
  77.                 return result;
  78.         }

  79.         result = storage_upload_by_filename1(pTrackerServer, \
  80.                         &storageServer, store_path_index, \
  81.                         local_filename, NULL, \
  82.                         NULL, 0, group_name, file_id);
  83.         if (result == 0)
  84.         {
  85.                 printf("%s\n", file_id);
  86.         }
  87.         else
  88.         {
  89.                 fprintf(stderr, "upload file fail, " \
  90.                         "error no: %d, error info: %s\n", \
  91.                         result, STRERROR(result));
  92.         }

  93.         tracker_disconnect_server_ex(pTrackerServer, true);
  94.         fdfs_client_destroy();

  95.     //pthread_mutex_unlock(&g_mutex); //不加锁会有段错误
  96.         return result;
  97. }


  98. int create_thread(test_fileinfo_t *file, void*(*routine)(void*param)){
  99.     int result = 0;     
  100.     pthread_t tid;
  101.     printf("create a thread\n");
  102.     pthread_create(&tid, NULL, routine, file);
  103.    
  104.     return result;
  105. }

  106. int main(int argc, char** argv){
  107.     if (argc < 2){
  108.         usage();
  109.     }
  110.     char ch;
  111.     int thread_num = 0;
  112.     int i = 0;

  113.     while ((ch=getopt(argc, argv, "t:h")) != EOF) {
  114.         switch (ch){
  115.         case 't':
  116.             thread_num = atoi(optarg);
  117.             break;
  118.         case 'h':
  119.         default:
  120.             usage();
  121.             break;
  122.         }
  123.     }


  124.     pthread_mutex_init(&g_mutex, NULL);
  125.     for (i=0; i<thread_num; i++){
  126.         files[i].threadid = i;
  127.         create_thread(files+i, routine);
  128.     }
  129.     while (1){
  130.         sleep(2);
  131.     }
  132. }
复制代码
附上core文件调试的堆栈信息:
  1. (gdb) where
  2. #0  0x00007f141e45279a in _int_free () from /lib64/libc.so.6
  3. #1  0x00007f141e996e29 in iniFreeContext (pContext=0x7f141d7d3c30) at ../common/ini_file_reader.c:506
  4. #2  0x00007f141ebb615c in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:408
  5. #3  0x0000000000401251 in routine (param=0x6023e8) at test3.c:66
  6. #4  0x00007f141e7759d1 in start_thread (arg=0x7f141d7d4700) at pthread_create.c:301
  7. #5  0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
  8. (gdb) thread apply all bt

  9. Thread 6 (Thread 0x7f141cdd3700 (LWP 1674)):
  10. #0  0x00007f141e453ded in _int_malloc () from /lib64/libc.so.6
  11. #1  0x00007f141e4549a1 in malloc () from /lib64/libc.so.6
  12. #2  0x00007f141e997446 in iniDoLoadItemsFromBuffer (content=<value optimized out>, pContext=0x7f141cdd2c30) at ../common/ini_file_reader.c:446
  13. #3  0x00007f141e996fce in iniDoLoadFromFile (szFilename=0x7f141cdd2b00 "/etc/fdfs/client.conf", pContext=0x7f141cdd2c30) at ../common/ini_file_reader.c:214
  14. #4  0x00007f141e997888 in iniLoadFromFile (szFilename=0x401ae8 "/etc/fdfs/client.conf", pContext=0x7f141cdd2c30) at ../common/ini_file_reader.c:161
  15. #5  0x00007f141ebb6121 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:398
  16. #6  0x0000000000401251 in routine (param=0x602490) at test3.c:66
  17. #7  0x00007f141e7759d1 in start_thread (arg=0x7f141cdd3700) at pthread_create.c:301
  18. #8  0x00007f141e4c2b5d in clone () from /lib64/libc.so.6

  19. Thread 5 (Thread 0x7f1417fff700 (LWP 1675)):
  20. #0  0x00007f141e453ded in _int_malloc () from /lib64/libc.so.6
  21. #1  0x00007f141e4549a1 in malloc () from /lib64/libc.so.6
  22. #2  0x00007f141e997446 in iniDoLoadItemsFromBuffer (content=<value optimized out>, pContext=0x7f1417ffec30) at ../common/ini_file_reader.c:446
  23. #3  0x00007f141e996fce in iniDoLoadFromFile (szFilename=0x7f1417ffeb00 "/etc/fdfs/client.conf", pContext=0x7f1417ffec30) at ../common/ini_file_reader.c:214
  24. #4  0x00007f141e997888 in iniLoadFromFile (szFilename=0x401ae8 "/etc/fdfs/client.conf", pContext=0x7f1417ffec30) at ../common/ini_file_reader.c:161
  25. #5  0x00007f141ebb6121 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:398
  26. #6  0x0000000000401251 in routine (param=0x602538) at test3.c:66
  27. #7  0x00007f141e7759d1 in start_thread (arg=0x7f1417fff700) at pthread_create.c:301
  28. #8  0x00007f141e4c2b5d in clone () from /lib64/libc.so.6

  29. Thread 4 (Thread 0x7f13fffff700 (LWP 1676)):
  30. #0  0x00007f141e996d0f in iniGetBoolValue (szSectionName=<value optimized out>, szItemName=<value optimized out>, pContext=<value optimized out>,
  31.     bDefaultValue=false) at ../common/ini_file_reader.c:624
  32. #1  0x00007f141ebb084f in fdfs_connection_pool_init (config_filename=0x401ae8 "/etc/fdfs/client.conf", pItemContext=0x7f13ffffec30)
  33.     at ../tracker/fdfs_shared_func.c:842
  34. #2  0x00007f141ebb6004 in fdfs_client_do_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf", iniContext=0x7f13ffffec30)
  35.     at client_func.c:319
  36. #3  0x00007f141ebb6152 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:406
  37. #4  0x0000000000401251 in routine (param=0x6025e0) at test3.c:66
  38. #5  0x00007f141e7759d1 in start_thread (arg=0x7f13fffff700) at pthread_create.c:301
  39. #6  0x00007f141e4c2b5d in clone () from /lib64/libc.so.6

  40. Thread 3 (Thread 0x7f141e1d5700 (LWP 1672)):
  41. #0  0x00007f141e996d0f in iniGetBoolValue (szSectionName=<value optimized out>, szItemName=<value optimized out>, pContext=<value optimized out>,
  42. ---Type <return> to continue, or q <return> to quit---
  43.     bDefaultValue=false) at ../common/ini_file_reader.c:624
  44. #1  0x00007f141ebb084f in fdfs_connection_pool_init (config_filename=0x401ae8 "/etc/fdfs/client.conf", pItemContext=0x7f141e1d4c30)
  45.     at ../tracker/fdfs_shared_func.c:842
  46. #2  0x00007f141ebb6004 in fdfs_client_do_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf", iniContext=0x7f141e1d4c30)
  47.     at client_func.c:319
  48. #3  0x00007f141ebb6152 in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:406
  49. #4  0x0000000000401251 in routine (param=0x602340) at test3.c:66
  50. #5  0x00007f141e7759d1 in start_thread (arg=0x7f141e1d5700) at pthread_create.c:301
  51. #6  0x00007f141e4c2b5d in clone () from /lib64/libc.so.6

  52. Thread 2 (Thread 0x7f141efd5700 (LWP 1671)):
  53. #0  0x00007f141e486ced in nanosleep () from /lib64/libc.so.6
  54. #1  0x00007f141e486b60 in sleep () from /lib64/libc.so.6
  55. #2  0x0000000000401491 in main (argc=<value optimized out>, argv=<value optimized out>) at test3.c:150

  56. Thread 1 (Thread 0x7f141d7d4700 (LWP 1673)):
  57. #0  0x00007f141e45279a in _int_free () from /lib64/libc.so.6
  58. #1  0x00007f141e996e29 in iniFreeContext (pContext=0x7f141d7d3c30) at ../common/ini_file_reader.c:506
  59. #2  0x00007f141ebb615c in fdfs_client_init_ex (pTrackerGroup=0x602860, conf_filename=0x401ae8 "/etc/fdfs/client.conf") at client_func.c:408
  60. #3  0x0000000000401251 in routine (param=0x6023e8) at test3.c:66
  61. #4  0x00007f141e7759d1 in start_thread (arg=0x7f141d7d4700) at pthread_create.c:301
  62. #5  0x00007f141e4c2b5d in clone () from /lib64/libc.so.6
复制代码

论坛徽章:
0
发表于 2014-09-05 18:16 |显示全部楼层
貌似已经解决了,上面报段错误 是因为 函数   fdfs_client_init(conf_filename)    和  tracker_get_connection() 调用了全局变量  g_tracker_group,每个线程自己弄一个局部的 tracker_group  就能解决问题了

论坛徽章:
0
发表于 2014-10-20 17:03 |显示全部楼层
请问怎么设置局部的 tracker_group呢?回复 2# liujian0616


   

论坛徽章:
2
IT运维版块每日发帖之星
日期:2016-05-09 06:20:00IT运维版块每日发帖之星
日期:2016-08-11 06:20:00
发表于 2016-08-08 11:22 |显示全部楼层
java client api 也有同样问题,大侠们有解决方案么,除了加锁。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP