Пример #1
0
void* execute(void *data)
{
    struct thread_info *tinfo = (struct thread_info*) data;
    tinfo->t_total = 0;
    int client_len;
    int client_sockfd;

    struct sockaddr_in clientaddr;

    //static sigset_t  signal_mask;
    //sigemptyset (&signal_mask);
    //sigaddset (&signal_mask, SIGALRM);
    //sigaddset (&signal_mask, SIGPIPE);
    //pthread_sigmask(SIG_UNBLOCK, &signal_mask, NULL);

    client_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_addr.s_addr = inet_addr(ip);
    clientaddr.sin_port = htons(port);

    client_len = sizeof(clientaddr);

    if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) < 0)
    {
        printf("Connect error.\n");
        exit(0);
    }

    struct msg *rmsg = NULL;

    uint64_t st, et;

    int i;
    for(i = 0; i < tinfo->count; i++)
    {

        st = utime_time();
        write_msg_ping(client_sockfd);

        rmsg = read_msg2(client_sockfd);
        msg_free(rmsg);
        et = utime_time();
        tinfo->t_total += et - st;
        if(printable) printf("Ping cmd Time %"PRIu64"uc\n", et - st);

        st = utime_time();
        write_msg_get_stats(client_sockfd);

        rmsg = read_msg2(client_sockfd);
        msg_free(rmsg);
        et = utime_time();
        tinfo->t_total += et - st;
        if(printable) printf("GetStat cmd Time %"PRIu64"uc\n", et - st);


        st = utime_time();
        write_msg_get_node(client_sockfd);

        rmsg = read_msg2(client_sockfd);
        msg_free(rmsg);
        et = utime_time();
        tinfo->t_total += et - st;
        if(printable) printf("GetNode cmd Time %"PRIu64"uc\n", et - st);
        tinfo->ec++;
    }

    //printf("Close socket.\n");
    close(client_sockfd);
    pthread_mutex_lock(&lock);
    finished--;
    pthread_mutex_unlock(&lock);

//    printf("### threads:%d finished:%d\n", threads, finished);

    pthread_exit(NULL);
    return NULL;
}
Пример #2
0
int redis_health_check()
{
    int res;
    char buf[1024];
    fd_set rfdset;
    struct timeval tval;

    utime_t stime, etime;
    if(!zoodis.redis_sock)
    {
        res = socket(PF_INET,SOCK_STREAM, 0);
        if(res < 0)
        {
            log_warn("Redis: cannot open socket for check redis server. %s", strerror(errno));
            return 0;
        }

        zoodis.redis_sock = res;

        res = connect(zoodis.redis_sock, (struct sockaddr*)&zoodis.redis_addr, sizeof(struct sockaddr_in));
        if(res < 0)
        {
            log_warn("Redis: cannot connect redis server %s:%d, %s", DEFAULT_REDIS_IP, zoodis.redis_port, strerror(errno));
            close(zoodis.redis_sock);
            zoodis.redis_sock = 0;
            return 0;
        }
    }

    stime = utime_time();
    res = write(zoodis.redis_sock, DEFAULT_REDIS_PING, strlen(DEFAULT_REDIS_PING));
    if(res < 0)
    {
        log_warn("Redis: error while write(send) ping to redis server. %s", strerror(errno));
        close(zoodis.redis_sock);
        zoodis.redis_sock = 0;
        return 0;
    }

    memset(buf, 0x00, 1024);

    FD_ZERO(&rfdset);
    FD_SET(zoodis.redis_sock, &rfdset);

    tval.tv_sec = zoodis.redis_pong_timeout_sec;
    tval.tv_usec = zoodis.redis_pong_timeout_usec;;

    res = select(zoodis.redis_sock+1, &rfdset, NULL, NULL, &tval);
    if(res < 0)
    {
        log_warn("Redis: redis could not response in time (timeout:%d.%d).", tval.tv_sec, tval.tv_usec);
        close(zoodis.redis_sock);
        zoodis.redis_sock = 0;
        return 0;
    }else
    {
        res = read(zoodis.redis_sock, buf, 1024);
        etime = utime_time();
        if(res < 0)
        {
            log_warn("Redis: test failed, %s", strerror(errno));
            close(zoodis.redis_sock);
            zoodis.redis_sock = 0;
            return 0;
        }else if(res == 0)
        {
            log_warn("Redis: test failed, connection closed.");
            close(zoodis.redis_sock);
            zoodis.redis_sock = 0;
            return 0;
        }else
        {
            if(strncmp(buf, DEFAULT_REDIS_PONG, 5) != 0)
            {
                log_warn("Redis: test failed, responsed not PONG, %.*s", res, buf);
                return 0;
            }else
            {
                utime_t elapsedTime = etime - stime;
                log_info("Redis: test successed. Elapsed %"PRIu64" usec", elapsedTime);
                return 1;
            }
        }
    }
}