從hiredis使用出core談談redis多線程的使用

      在實際工做中,我須要使用redis的客戶端去鏈接redis,因而選擇了hiredis客戶端(公司強推)。  hiRedis 是 Redis 官方指定的 C 語言客戶端開發包,支持 Redis 完整的命令集、管線以及事件驅動編程。 redis

一、情景描述

1.1 使用場景

      一個epool模型的服務器不斷接受外界請求,這個服務器框架給用戶預留一個回調函數(多線程),回調函數爲用戶本身去實現的業務邏輯,其中redis的使用就須要在這個回調函數內部實現。 shell

1.2 初步實現方案

      在程序啓動的時候,我就初始化redis的鏈接,得到hiredis句柄。而後把hiredis句柄傳入到線程函數裏面。讓其作相應的業務邏輯。 編程

1.3 結果

      很不幸,一次請求都沒問題,作壓力測試,同時開20個線程訪問,程序當即出core。 vim

      線上出core以下: 服務器

(gdb) bt
#0  0x000000302af2e2ed in raise () from /lib64/tls/libc.so.6
#1  0x000000302af2fa3e in abort () from /lib64/tls/libc.so.6
#2  0x000000302af62db1 in __libc_message () from /lib64/tls/libc.so.6
#3  0x000000302af6888e in _int_free () from /lib64/tls/libc.so.6
#4  0x000000302af6a12d in _int_realloc () from /lib64/tls/libc.so.6
#5  0x000000302af6b39c in realloc () from /lib64/tls/libc.so.6
#6  0x0000000000dc2269 in sdscatlen (s=Variable "s" is not available.
) at sds.c:97
#7  0x0000000000dc1d40 in __redisAppendCommand (c=0x16fa1d0, cmd=Variable "cmd" is not available.
) at hiredis.c:1186
#8  0x0000000000dc1d97 in redisvAppendCommand (c=0x16fa1d0, format=Variable "format" is not available.
) at hiredis.c:1206
#9  0x0000000000dc1eed in redisvCommand (c=0x16fa1d0, format=Variable "format" is not available.
) at hiredis.c:1267
#10 0x0000000000dc1fb6 in redisCommand (c=Variable "c" is not available.
) at hiredis.c:1276
#11 0x0000002b1a8e6310 in Default_Handler::get_batch_redis (this=0x1ff41f0, redis_ins=0x175a7d0, dataid=6202, buf_num=12, res_num=6, key_sign=0x2bd67cb3c8, 
    res_lens=0x2bd5f54208, res_buf=0x2bd5f54398 "") at default_handler.cpp:659
#12 0x0000002b1a9134df in Default_Ms_Handler::get_digest (this=0x1ff41f0) at default_ms_handler.cpp:646
#13 0x000000000092910c in do_proc () at gss_work.cpp:1107
#14 0x000000000091c91f in thread_main () at gss_net.cpp:188
#15 0x0000000000bc10e9 in default_native () at ubserver_app.cpp:283
#16 0x0000000000bbc676 in eppool_consume (pool=0x2230b90, data=0x22188f0) at eppool.cpp:649
#17 0x0000000000bbc4d1 in _eppool_workers (param=0x22188f0) at eppool.cpp:604
#18 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#19 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#20 0x0000000000000000 in ?? ()
      當時通過屢次嘗試。把鏈接放入到了每一個線程中。那麼就不會出core了。


二、線下復現

      由於不方便公開公司代碼,因此我寫一個相似的代碼來複現這個case。 網絡

2.1 代碼

      代碼主要有testredis.cpp和Makefile(本身指定hiredis目錄)。用法是 ./redis -n [num] -h [host] -p [port], n爲host數目,多個host用"-"進行分割。 多線程

testredis.cpp app

/***************************************************************************
 * 
 * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
 
 
/**
 * @file redistest.cpp
 * @author liujun05(com@baidu.com)
 * @date 2014/02/25 10:28:44
 * @brief 
 *  
 **/

#include<unistd.h>
#include <stdio.h>
#include <hiredis.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>


#ifndef uint32
#define uint32 unsigned int 
#endif

#define MAX_REDIS_SERVER_CNT 10
#define MAX_REDIS_IPS 1024

typedef struct _redis_conf_t
{
    uint32 redis_num;
    char redis_ips[MAX_REDIS_IPS];
    char redis_ip_array[MAX_REDIS_SERVER_CNT][MAX_REDIS_IPS];
    uint32 redis_port;

} redis_conf;

typedef struct _redis_data_t
{
    uint32 redis_num;
    redisContext *rc[MAX_REDIS_SERVER_CNT];
}redis_data;

redis_conf g_cfg;
redis_data g_data;

void show_usage()
{
	printf("usage: ./redis -n [num] -h [host] -p [port]\n");
}

/** 解析參數 */
int main_parse_option(int argc, char **argv)
{
    int c;
    //reset 獲取參數的位置,屢次調用時這個會出現問題
    while ((c = getopt(argc, argv, "h:p:n:")) != -1)
    {
        switch (c)
        {
        case 'h':
			sprintf(g_cfg.redis_ips, optarg);
            break;
        case 'p':
			g_cfg.redis_port = atoi(optarg);
            break;
        case 'n':
			g_cfg.redis_num = atoi(optarg);
            break;
        default:
			show_usage();
            fflush(stdout);
            return -1;
        }
    }
    return 0;
}

void* test_thread1(void* data)
{
    redis_data* redis_ins = (redis_data*)data;
    redisReply *reply;
    for(int i=0; i<redis_ins->redis_num; i++)
    {
        reply = (redisReply *)redisCommand( redis_ins->rc[i] ,"SET %s %s", "foo", "hello world");
        freeReplyObject(reply);
    }
}

int init_data()
{
    g_data.redis_num = 0;
    struct timeval timeout = { 1, 500000 }; // 1.5 seconds

    char *ptok = NULL;
    char *part = strtok_r(g_cfg.redis_ips, "-", &ptok);
    int num = 0;
    while(part)
    {
        strcpy(g_cfg.redis_ip_array[num++], part);
        part = strtok_r(NULL, "-", &ptok);
    }

    if(num != g_cfg.redis_num || num > MAX_REDIS_SERVER_CNT)
    {
        printf("ip num[%d] not equal redis_num[%d] or not vaild\n", num, g_cfg.redis_num);
    }

    g_data.redis_num = (num > MAX_REDIS_SERVER_CNT ) ? MAX_REDIS_SERVER_CNT : num;
    int i= 0;

    for(i=0; i<g_data.redis_num; i++)
    {
        g_data.rc[i] = redisConnectWithTimeout( g_cfg.redis_ip_array[i], g_cfg.redis_port , timeout);
        if( g_data.rc[i] == NULL || g_data.rc[i]->err)
        {
            printf("content to redis server[%s:%u], error[%s]\n",
                    g_cfg.redis_ip_array[i], g_cfg.redis_port, g_data.rc[i]->errstr
            );
            goto exit;
        }
    } 
    return 0;

exit:
   for(int j=0; j<i; j++)
   {
        if(g_data.rc[j] != NULL)
        {
            redisFree(g_data.rc[j]);
        }
   }
   return -1;
}


int destory_data()
{
   for(int j=0; j<g_data.redis_num; j++)
   {
        if(g_data.rc[j] != NULL)
        {
            redisFree(g_data.rc[j]);
        }
   }
}

int main(int argc, char** argv)
{
    g_cfg.redis_ips[0] = '\0';
    g_cfg.redis_port = 6379;
    g_cfg.redis_num = 0;
    if( 0 != main_parse_option(argc, argv) )
    {
		show_usage();
        return -1;
    }

    if(  0 == g_cfg.redis_num || g_cfg.redis_num >  MAX_REDIS_SERVER_CNT )
    {
        printf("the reids num[%u] is not vaild\n", g_cfg.redis_num);
		show_usage();
        return 0;
    }

    int ret = init_data();
    if( ret != 0)
    {
        printf("init num fail\n");
        return -1;
    }


    pthread_t t[100];
    for(int i=0; i<100; i++)
    {
        pthread_create(&t[i], NULL, test_thread1, &g_data);
    }

    for(int i=0; i<100; i++)
    {
        pthread_join(t[i], NULL);
    }

    destory_data();
    return 0;
}



/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
Makefile
redis: testredis.cpp
	g++ -g testredis.cpp -I./hiredis -L./hiredis -lhiredis -lpthread -o redis

clean:
	rm redis

2.2 編譯執行

liujun05@cq01-rdqa-dev012.cq01:~/test/hiredis$ ./redis -n2 -h10.48.46.26-10.46.175.102
*** glibc detected *** double free or corruption (!prev): 0x000000000050aa80 ***
Aborted (core dumped)
      能夠看到出core了
(gdb) bt
#0  0x000000302af2e2ed in raise () from /lib64/tls/libc.so.6
#1  0x000000302af2fa3e in abort () from /lib64/tls/libc.so.6
#2  0x000000302af62db1 in __libc_message () from /lib64/tls/libc.so.6
#3  0x000000302af6888e in _int_free () from /lib64/tls/libc.so.6
#4  0x000000302af68bd6 in free () from /lib64/tls/libc.so.6
#5  0x0000000000403c75 in redisBufferWrite (c=0x50a010, done=0x571c008c) at hiredis.c:1162
#6  0x0000000000403d3e in redisGetReply (c=0x50a010, reply=0x571c00b8) at hiredis.c:1195
#7  0x0000000000403f62 in redisvCommand (c=0x50a010, format=Variable "format" is not available.
) at hiredis.c:1296
#8  0x0000000000404006 in redisCommand (c=Variable "c" is not available.
) at hiredis.c:1313
#9  0x00000000004013e7 in test_thread1 (data=0x509ba0) at testredis.cpp:88
#10 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#11 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#12 0x0000000000000000 in ?? ()
      雖然出core位置不一致,可是通過查看代碼,出core的緣由應該是一致的。

2.3 緣由分析

      從堆棧5能夠看到 hiredis.c的1162行出的core,打開hiredis.c 框架

1160         } else if (nwritten > 0) {
1161             if (nwritten == (signed)sdslen(c->obuf)) {
1162                 sdsfree(c->obuf);
1163                 c->obuf = sdsempty();
1164             } else {
1165                 c->obuf = sdsrange(c->obuf,nwritten,-1);
1166             }
      能夠看到的確在1152行對c->obuf進行了一次free致使出core。

      咱們分析下調用關係,首先調用redisCommand. 異步

1309 void *redisCommand(redisContext *c, const char *format, ...) {
1310     va_list ap;
1311     void *reply = NULL;
1312     va_start(ap,format);
1313     reply = redisvCommand(c,format,ap);
1314     va_end(ap);
1315     return reply;
1316 }
      而後調用redisvCommand
1303 void *redisvCommand(redisContext *c, const char *format, va_list ap) {
1304     if (redisvAppendCommand(c,format,ap) != REDIS_OK)
1305         return NULL;
1306     return __redisBlockForReply(c);
1307 }

      接着調用redisvAppendCommand

1233 int redisvAppendCommand(redisContext *c, const char *format, va_list ap) {
1234     char *cmd;
1235     int len;
1236 
1237     len = redisvFormatCommand(&cmd,format,ap);
1238     if (len == -1) {
1239         __redisSetError(c,REDIS_ERR_OOM,"Out of memory");
1240         return REDIS_ERR;
1241     }
1242 
1243     if (__redisAppendCommand(c,cmd,len) != REDIS_OK) {
1244         free(cmd);
1245         return REDIS_ERR;
1246     }
1247 
1248     free(cmd);
1249     return REDIS_OK;
1250 }

      這裏,咱們須要care調用__redisAppendCommand.

1220 int __redisAppendCommand(redisContext *c, char *cmd, size_t len) {
1221     sds newbuf;
1222 
1223     newbuf = sdscatlen(c->obuf,cmd,len);
1224     if (newbuf == NULL) {
1225         __redisSetError(c,REDIS_ERR_OOM,"Out of memory");
1226         return REDIS_ERR;
1227     }
1228 
1229     c->obuf = newbuf;
1230     return REDIS_OK;
1231 }

      問題出現了。

      對於任意一個多線程,他傳入的redisContext* c都是一個,那麼他們也公用同一個c->obuf,這裏很明顯,線程數據是耦合的。

      當一個線程調用sdsfree c->obuf,其餘任意一個線程使用c->obuf都會致使出core. 這也是我所謂的hiredis對多線程支持的很差的地方。

3. 終極解決方案

      那麼,若是我必定要在多線程中經過hiredis客戶端調用redis呢。有沒有方案了,答案確定是有,只不過性能稍差。

      原先的作法是先得到hiredis鏈接句柄,而後把句柄傳入到多線程中,讓多線程使用。如今改爲在線程裏面鏈接得到hiredis句柄,而後再進行使用。固然,代價是對於每一個請求,都須要去鏈接redis服務器,加大了網絡開銷的同時還加大了redis的請求。

      redis是單線程異步模型,hiredis這個客戶端看來也只支持單線程。但願後續有redis的相關程序猿來改進相應問題,在hiredis使用多線程須要慎重。

相關文章
相關標籤/搜索