Beispiel #1
0
int Grab::incrSendAmount()
{
	redisContext *c;
	c=redisConnPool::getInstance()->getConnection();
	if(c==NULL){
		UB_LOG_FATAL( "get redis connection failed, [%s:%d]", __FILE__, __LINE__ );
		return -5;
	}
	redisReply *reply;
	UB_LOG_DEBUG( "HINCRBY GRAB:%d send_amount 1", _id );
	reply = (redisReply*)redisCommand( c, "HINCRBY GRAB:%d send_amount 1", _id );
	redisConnPool::getInstance()->releaseConnection(c);
	if(reply!=NULL){
		if(reply->type!=REDIS_REPLY_ERROR){
			_info.send_amount=reply->integer;
		}else{
			UB_LOG_FATAL( "REDIS ERROR[%s]", reply->str );
			if(reply != NULL)
			{   
				freeReplyObject(reply);
			}
			return -5;
		}
		freeReplyObject(reply);
	}else{
		UB_LOG_FATAL( "get a NULL redis connection, [%s:%d]", __FILE__, __LINE__ );
		return -5;
	}
	return 0;
}
Beispiel #2
0
int Grab::setSendAmount(const uint32_t amount)
{
    redisContext *c;
    c=redisConnPool::getInstance()->getConnection();
    if(c==NULL){
        UB_LOG_FATAL( "get redis connection failed, [%s:%d]", __FILE__, __LINE__ );
        return -5;
    }
    redisReply *reply;
    UB_LOG_DEBUG( "HSET GRAB:%d send_amount %d", _id, amount );
    reply = (redisReply*)redisCommand( c, "HSET GRAB:%d send_amount %d", _id, amount );
    redisConnPool::getInstance()->releaseConnection(c);
    if(reply!=NULL){
        if(reply->type==REDIS_REPLY_ERROR){
			UB_LOG_FATAL( "REDIS ERROR[%s]", reply->str );
			if(reply != NULL)
			{   
				freeReplyObject(reply);
			}
			return -5;
		}
		freeReplyObject(reply);
	}else{
		UB_LOG_FATAL( "get a NULL redis connection, [%s:%d]", __FILE__, __LINE__ );
		return -5;
	}
	return 0;
}
Beispiel #3
0
int Grab::getGrabInfo(grab_t* grab_info)
{
    UB_LOG_TRACE("Grab::getGrabInfo start");
    string query_sql("SELECT `id`, `name`, `c_id`, `info`, `start_time`, `end_time`, `amount`, `probability` "
                     "FROM `MCP`.`mcp_content_grab` "
                     "WHERE `enable` = 1 AND `id` = " );
    query_sql.append(toString(_id));
    //DuokooMysql mysql;
    DuokooMysql mysql("grab_mysql");
    int count=mysql.query(query_sql);
    if(count<0){
        UB_LOG_FATAL( "sql[%s], [%s:%d]", query_sql.c_str(), __FILE__, __LINE__ );
        return -5;
    }else if(count==0){
        UB_LOG_FATAL( "Grab id[%d] has no record, [%s:%d]", _id, __FILE__, __LINE__ );
        return -5;
    }
    _info.clear();
    _info.id=atoi(mysql.getResult(0,0).c_str());
    _info.name=mysql.getResult(0,1);
    _info.c_id=mysql.getResult(0,2);
    _info.info=mysql.getResult(0,3);
    _info.start_time=mysql.getResult(0,4);
    _info.end_time=mysql.getResult(0,5);
    _info.amount=atoi(mysql.getResult(0,6).c_str());
    _info.probability=atof(mysql.getResult(0,7).c_str());
    _info.send_amount=this->getSendAmount();
    UB_LOG_NOTICE("name:[%s]",_info.name.c_str());
    if(grab_info!=NULL){
        *grab_info = _info;
    }

    UB_LOG_TRACE("Grab::getGrabInfo end");
    return 0;
}
/**
 *
 * @brief 处理srch命令的函数
 *
 * @param[in] cmd_no 命令号,一个函数可能处理多条命令,可以用命令号来区分
 * @param[in] req_head 请求数据的nshead_t*
 * @param[in] req_buf 请求数据的buffer,本buffer不包含nshead_t
 * @param[out] res_head 应答数据的nshead_t*
 * @param[out] res_buf 应答数据的buffer,本buffer不包含nshead_t
 * @return
 *       0  : 成功
 *       -1 : 失败,socket将直接关闭,不给client返回错误信息
 **/
STATIC int
srch_process(int cmd_no, nshead_t * req_head, ub_buff_t * req_buf,
         nshead_t * res_head, ub_buff_t * res_buf)
{
    /**
     *在打NOTICE日志时,请使用 ub_log_pushnotice 加入日志信息
     *如果返回值不为0,将导致socket被直接关闭而不给客户返回任何信息
     **/
    srch_thread_data_t *p_thread_data;

    if(NULL == req_head || NULL == req_buf || NULL == res_head || NULL == res_buf) {
        UB_LOG_FATAL("parameter error in srch process.");
        return -1;
    }

    srch_reset_res(req_head, res_head, res_buf);

    char* req = req_buf->buf;
    char* res = res_buf->buf;

    res_head->body_len = 0;
	int response_buffer_size = ub_server_get_write_size() - sizeof (nshead_t);

    p_thread_data = (srch_thread_data_t* )ub_server_get_user_data();
    if(NULL == p_thread_data) {
        UB_LOG_FATAL("thead_data null");
        return -1;
    }

	int post_count            = 0;
	int max_post_count        = p_thread_data->max_post_count;
	u_int64_t* pids           = p_thread_data->post_ids;
	delpost_record_t* records = p_thread_data->records;

	if (!unpack_data(req, req_head->body_len, post_count, pids, max_post_count))
	{
		UB_LOG_WARNING("unpack mcpack error");
		return -1;
	}

	int ret = query(post_count, pids, records, 
			g_conf.mask_path, g_conf.index_file, g_conf.mask_file);

	if (ret < 0)
	{
		UB_LOG_WARNING("query[ret=%d] error", ret);
		return -1;
	}
	
	ret = pack_data(res, response_buffer_size, post_count, records);
	if (ret < 0)
	{
		UB_LOG_WARNING("pack mcpack error");
		return -1;
	}

	res_head->body_len = ret;

    return 0;
}
Beispiel #5
0
int Grab::userGrabPre(const string& user_id, string& num)
{
	int res=this->getGrabInfo();
	if(res!=0){
		UB_LOG_FATAL( "getGrabInfo failed, [%s:%d]", __FILE__, __LINE__ );
		return -5;
	}
	time_t t;
	t=time(0);
    
	if(t>stringToTimestamp(_info.start_time)){
		UB_LOG_NOTICE( "_id[%d] has started, can not pre order [%s]", _id, _info.start_time.c_str() );
		return -1;
	}
    
	if(t>stringToTimestamp(_info.end_time)){
		UB_LOG_NOTICE( "_id[%d] has already end[%s]", _id, _info.end_time.c_str() );
		return -2;
	}
	if(_info.send_amount>=_info.amount){
		UB_LOG_NOTICE( "_id[%d] has already send end. amount[%d],send_amount[%d]", _id, _info.amount, _info.send_amount );
		return -3;
	}
	res=this->getUserGrab(user_id, num);
	if(res<0){
		UB_LOG_FATAL("getUserGrab failed, [%s:%d]", __FILE__, __LINE__ );
		return -5;
	}else if(res>0){
		UB_LOG_NOTICE( "user[%s] has already get a num[%s] in grab[%d]", user_id.c_str(), num.c_str(), _id );
		return -6;
	}
	//long random_num=getRandomNum(100);
    //if(random_num<_info.probability){
    res=this->getGrabNum(user_id, num, 2);
    if(res==-5){
        UB_LOG_FATAL("getGrabNum failed, user_id[%s], [%s:%d]", user_id.c_str(), __FILE__, __LINE__ );
        num.clear();
        return -5;
    }else if(res==-3){
        UB_LOG_FATAL("getGrabNum has already send over, id[%d], user_id[%s], [%s:%d]", 
                _id, user_id.c_str(), __FILE__, __LINE__ );
/*        res = this->setSendAmount(_info.amount);*/
        //if(res!=0){
            //UB_LOG_FATAL( "setSendAmount failed, id[%d], [%s:%d]", _id, __FILE__, __LINE__ );
        //}
        /*num.clear();*/
        return -5;
    }else{
        res= this->incrSendAmount();
        if(res!=0){
            UB_LOG_FATAL( "incrSendAmount failed, id[%d], [%s:%d]", _id, __FILE__, __LINE__ );
        }
    }
	//}else{
	//	return -4;
	//}
	return 0;
}
Beispiel #6
0
int McpServlet::get_grab_list_by_type(const idl::mcp_get_grab_list_by_type_params& in, idl::mcp_get_grab_list_by_type_response& out)
{
    UB_LOG_TRACE( "get_grab_list_by_type start" );
    string type_id(in.type_id());
    uint32_t page(in.page());
    uint32_t page_size(in.page_size());

    vector<grab_t> grab_list;
    int count=0;
    ContentType type(type_id);
    type.set_page_info(page, page_size);
    int res=type.getGrabList(count, grab_list);
    if(res!=0){
        UB_LOG_FATAL( "getGrabList failed, [%s:%d]", __FILE__, __LINE__ );
        count = res;
        goto end;
    }
end:
    out.m_result_params()->set_result(count);
    vector<grab_t>::const_iterator iter=grab_list.begin();
    for(int i=0; iter!=grab_list.end(); ++i, ++iter){
        setGrabInfoResult(out.m_result_params()->mutable_grab_list(i), *iter);
    }
    UB_LOG_TRACE( "get_grab_list_by_type end" );
    return 0;
}
Beispiel #7
0
/*word segment,scw_out memory init*/
int segment_memory_init(scw_out_t **pout)
{
    if(*pout){
        UB_LOG_FATAL("word segment memory pout init again");
        scw_destroy_out(*pout);
    }

	u_int scw_out_flag;
	scw_out_flag = SCW_OUT_WPCOMP | SCW_OUT_PROP;
	if((*pout=scw_create_out(10000, scw_out_flag))==NULL)
	{
		UB_LOG_FATAL("error: initializing the output buffer error");
		return -1;
	}
    return 0;
}
Beispiel #8
0
int McpServlet::user_grab_num(const idl::mcp_user_grab_num_params& in, idl::mcp_user_grab_num_response& out)
{
    UB_LOG_TRACE( "user_grab_num start" );
    string user_id(in.user_id());
	string push_channelid(in.push_channelid());
	string push_userid(in.push_userid());
    uint32_t grab_id(in.grab_id());
    Grab grab(grab_id);
    string num;
    int res=grab.userGrab(user_id, num);
    if(res!=0){
        UB_LOG_FATAL( "userGrab failed, [%s:%d]", __FILE__, __LINE__ );
    }

	/*2013.05.23 zhengxie 发送成功结果给消息系统 beg */
	grab_t grab_info;
	grab.getGrabInfo(&grab_info);
	string grab_name = grab_info.name;
	grab_info.clear();
	if( (res == 0) && IS_VALUED_STRING(grab_name.c_str()) && IS_VALUED_STRING(num.c_str()) )
	{
	    NoticeRPC::instance().notice_grab( user_id.c_str(), grab_name.c_str(), num.c_str(), push_channelid.c_str(), push_userid.c_str() );
	}
	/*2013.05.23 zhengxie 发送成功结果给消息系统 end */

    out.m_result_params()->set_result(res);
    out.m_result_params()->set_num(num.c_str());
    UB_LOG_TRACE( "user_grab_num end" );
    return 0;
}
Beispiel #9
0
int segment_words(const char * content,enum wtype type,int len,bool stepword,vector<funit>& features)
{
	if(!content || len <= 0){
		UB_LOG_WARNING("segment words,parameter error");
		return -1;
	}

    /*get word segment result buffer*/
    thread_data * tsd = (thread_data *)pthread_getspecific(key);
    if(!tsd){
        UB_LOG_FATAL("thread special data is null");
        exit(0);
    }
    scw_out_t *pout = tsd->pout;

	/*word segment*/
	if(scw_segment_words(pwdict,pout,content,len,LANGTYPE_SIMP_CHINESE,NULL) == -1){
		UB_LOG_WARNING("scw segment words failed");
		return -1;
	}

	/*get result to vectore features*/
	int i,count;
	token_t tokens[1024];
	funit tmp;
    
    /*word type,we just need SCW_OUT_WPCOMP*/
	u_int tsco[5] = {SCW_OUT_WPCOMP,SCW_OUT_BASIC,SCW_OUT_SUBPH,
		SCW_OUT_HUMANNAME,SCW_OUT_BOOKNAME};
    
	/*just SCW_OUT_WPCOMP mode,so j < 1*/
	for(int j = 0;j < 1;j ++)
	{
		count = scw_get_token_1(pout,tsco[j],tokens,1024);
		for(i = 0;i < count;i ++)
		{
			/*filter space and special punc*/
			trim_string(tokens[i].buffer);
			if(strlen(tokens[i].buffer) <= 1)
				continue;
            
            tmp.feature = tokens[i].buffer;
			tmp.weight = 1;
			features.push_back(tmp);
		}
	}

    /*get weight*/
    feature_weight(features,type);

	/*output result for debug*/
	for(i = 0;i < (int)features.size();i++)
	{
		tmp = features.at(i);
		UB_LOG_DEBUG("word[%s] weight[%f]",tmp.feature.c_str(),tmp.weight);
	}

	return 0;
}
Beispiel #10
0
/*
 * word segment init,dict and memory
 **/
int segment_init(const char * scwconfile,const char * wordictpath)
{
	pgconf = scw_load_conf(scwconfile);
	if(pgconf == NULL){
		UB_LOG_FATAL("scw conf load failed");
		return -1;
	}
	UB_LOG_DEBUG("scw load conf success");

	if((pwdict=scw_load_worddict(wordictpath))==NULL)
	{
		UB_LOG_FATAL("error: loading wordict failed: %s",wordictpath);
		return -1;
	}

	return 0;
}
Beispiel #11
0
/**
 * @brief 主处理函数
 *
 * @return  int 
 * @retval   
**/
int 
op_query()
{
    int opret = 0;

    nshead_t *req_head;
    nshead_t *res_head;
    ub_buff_t req_buf;
    ub_buff_t res_buf;
    in_addr_t req_ip;

    req_head = (nshead_t *) ub_server_get_read_buf();
    res_head = (nshead_t *) ub_server_get_write_buf();
    if(NULL == req_head || NULL == res_head)
    {
        UB_LOG_FATAL("get req_head[%ld] || res_head[%ld] failed.",
                     (long)req_head, (long)res_head);
        return -1;
    }
    req_buf.buf = (char *)(req_head + 1);
    req_buf.size = ub_server_get_read_size() - sizeof(nshead_t);
    res_buf.buf = (char *)(res_head + 1);
    res_buf.size = ub_server_get_write_size() - sizeof(nshead_t);

    //设置一些log需要的字段
    char ip_str[20];
    ip_str[0] = 0;
    req_ip = ub_server_get_ip();
    inet_ntop(AF_INET, &req_ip, ip_str, sizeof(ip_str));
    ub_log_setbasic(UB_LOG_REQIP, "%s", ip_str);
    ub_log_setbasic(UB_LOG_LOGID, "%u", req_head->log_id);
    ub_log_setbasic(UB_LOG_REQSVR, "%s", req_head->provider);
    ub_log_setbasic(UB_LOG_SVRNAME, "%s", g_cfg.svr_query.svr_name);

    ub_log_pushnotice("req_dlen", "%d", req_head->body_len);
    *res_head = *req_head;
    strncpy(res_head->provider, req_head->provider,
            sizeof(res_head->provider));
    res_head->body_len = 0;
    res_head->reserved = 0;

	//处理查询
	struct timeval total_s,total_e;
	gettimeofday(&total_s, NULL);
	opret = process_query(req_head, &req_buf, res_head, &res_buf);
    if(opret != 0){
            UB_LOG_WARNING("[function:op_update]>>>process_query failed! errno:%d", opret);
            return -1;
    }    
	gettimeofday(&total_e, NULL);
	ub_log_setbasic(UB_LOG_PROCTIME, "%luus", TIME_US_DIFF(total_s, total_e));
    
    return opret;
}
Beispiel #12
0
int Grab::getSendAmount()
{
    redisContext *c;
    uint32_t send_amount(0);
    c=redisConnPool::getInstance()->getConnection();
    if(c==NULL){
        UB_LOG_FATAL( "get redis connection failed, [%s:%d]", __FILE__, __LINE__ );
        return -5;
    }
    redisReply *reply;
    UB_LOG_DEBUG( "HGET GRAB:%d send_amount ", _id );
    reply = (redisReply*)redisCommand( c, "HGET GRAB:%d send_amount ", _id );
    redisConnPool::getInstance()->releaseConnection(c);
    if(reply!=NULL){
        if(reply->type!=REDIS_REPLY_ERROR){
            if(reply->type!=REDIS_REPLY_NIL){
                UB_LOG_NOTICE( "send amount:[%s]", reply->str );
                send_amount=atoi(reply->str);
            }else{
                send_amount=0;
            }
        }else{
            UB_LOG_FATAL( "REDIS ERROR[%s]", reply->str );
			if(reply != NULL)
			{
				freeReplyObject(reply);
			}
            return -5;
        }
        freeReplyObject(reply);
    }else{
        UB_LOG_FATAL( "get a NULL redis connection, [%s:%d]", __FILE__, __LINE__ );
        return -5;
    }
    return send_amount;
}
Beispiel #13
0
int Grab::getUserGrab(const string& user_id, string& num)
{
	int res(0);
	string query_sql;
	//DuokooMysql mysql;
    DuokooMysql mysql("grab_mysql");
	query_sql="SELECT num FROM `MCP`.`mcp_content_user_grab` WHERE `user_id` = " + user_id + " AND `grab_id` = " + toString(_id);
	res=mysql.query(query_sql);
	if(res<0){
		UB_LOG_FATAL( "query sql[%s] failed, [%s], [%s:%d]", query_sql.c_str(), mysql.getErrDes().c_str(), __FILE__, __LINE__ );
	}else if(res>0){
		UB_LOG_NOTICE( "user[%s] has already get a num[%s] in grab[%d]", user_id.c_str(), num.c_str(), _id );
		num = mysql.getResult(0,0);
	}
	return res;
}
Beispiel #14
0
int McpServlet::get_grab_info_by_id(const idl::mcp_get_grab_info_by_id_params& in, idl::mcp_get_grab_info_by_id_response& out)
{
    UB_LOG_TRACE( "get_grab_info_by_id start" );
    uint32_t id(in.id());
    grab_t grab_info;
    int res(0);
    Grab grab(id);
    res = grab.getGrabInfo(&grab_info);
    if(res!=0){
        UB_LOG_FATAL( "getGrabInfo faield, [%s:%d]", __FILE__, __LINE__ );
        goto end;
    }
end:
    out.m_result_params()->set_result(res);
    setGrabInfoResult(out.m_result_params()->mutable_info(), grab_info);
    UB_LOG_TRACE( "get_grab_info_by_id end" );
    return 0;
}
Beispiel #15
0
int McpServlet::get_user_grab_num(const idl::mcp_get_user_grab_num_params & in, idl::mcp_get_user_grab_num_response & out)
{
    UB_LOG_TRACE( "get_user_grab_num start" );
    string user_id(in.user_id());
    uint32_t grab_id(in.grab_id());
    Grab grab(grab_id);
    string num;
    int32_t occupy;
    int res=grab.getUserGrabEx(user_id, num, occupy);
    if(res<0){
            UB_LOG_FATAL( "getUserGrab failed, [%s:%d]", __FILE__, __LINE__ );
        }else if(res>1){
                res = 0;
            }   
    out.m_result_params()->set_result(res);
    out.m_result_params()->set_num(num.c_str());
    out.m_result_params()->set_occupy(occupy);
    UB_LOG_TRACE( "get_user_grab_num end" );
    return 0;
}
Beispiel #16
0
int McpServlet::get_grab_info_by_c_id(const idl::mcp_get_grab_info_by_c_id_params& in, idl::mcp_get_grab_info_by_c_id_response& out)
{
    UB_LOG_TRACE( "get_grab_info_by_c_id start" );
    string c_id(in.c_id());
    vector<grab_t> grab_list;
    int count=0;
    ContentInfo c_info(c_id);
    int res=c_info.getGrabList(count, grab_list);
    if(res!=0){
        UB_LOG_FATAL( "getGrabList failed, [%s:%d]", __FILE__, __LINE__ );
        count = res;
        goto end;
    }
end:
    out.m_result_params()->set_result(count);
    vector<grab_t>::const_iterator iter=grab_list.begin();
    for(int i=0; iter!=grab_list.end(); ++i, ++iter){
        setGrabInfoResult(out.m_result_params()->mutable_grab_list(i), *iter);
    }
    UB_LOG_TRACE( "get_grab_info_by_c_id start" );
    return 0;
}
/**
 *
 * @brief srch的命令处理回调函数
 *
 * @return
 *       0  : 成功
 *       -1 : 失败,socket将直接关闭,不给client返回错误信息
 **/
int
srch_cmdproc_callback()
{
    nshead_t *req_head;
    nshead_t *res_head;
    ub_buff_t req_buf;
    ub_buff_t res_buf;
    int cmd_no = -1;
    int ret = 0;

    //获取请求和回复buffer
    req_head = (nshead_t *) ub_server_get_read_buf();
    res_head = (nshead_t *) ub_server_get_write_buf();
    if(NULL == req_head || NULL == res_head) {
        UB_LOG_FATAL("srch process callback get buffer error.");
        return -1;
    }

    //获取请求和回复实际数据
    req_buf.buf = (char *) (req_head + 1);
    req_buf.size = ub_server_get_read_size() - sizeof (nshead_t);
    res_buf.buf = (char *) (res_head + 1);
    res_buf.size = ub_server_get_write_size() - sizeof (nshead_t);

    //取得命令号
    cmd_no = ((srch_req_t *) (req_buf.buf))->cmd_no;

    //ub_server中设置了: UB_LOG_REQIP, UB_LOGID, UB_LOG_PROCTIME, UB_LOG_ERRNO
    ub_log_setbasic(UB_LOG_REQSVR, "%s", req_head->provider);
    ub_log_setbasic(UB_LOG_SVRNAME, "%s", g_conf.srch.svr_name);
    ub_log_setbasic(UB_LOG_CMDNO, "%d", cmd_no);

    //ret = ub_process_cmdmap(srch_CMD_MAP, cmd_no, req_head, &req_buf, res_head, &res_buf);
	ret = srch_process(cmd_no, req_head, &req_buf, res_head, &res_buf);

    return ret;
}
	// 这里extern出来,是为了在后面debug,分析是否buf不够引起问题
	// extern bsl::xnofreepool * debug_pool = NULL;
	// 有可能在多进程中,同时使用该buf创建对象,关于_buf _pool _rp在多进程间使用全部要加锁
	CamelSuperStrategy::CamelSuperStrategy(char * buf, int bufSize) {
		//UB_LOG_DEBUG("new CamelSuperStrategy, bufSize:%d ConnectStatus:%u HealthyStatus:%u pthread_mutex_t:%u",
		//		bufSize, sizeof(ConnectStatus), sizeof(HealthyStatus), sizeof(pthread_mutex_t));
		if (NULL == buf && bufSize <= 0) {
			UB_LOG_FATAL("CamelSuperStrategy get buf fail buf[%p],bufSize[%d]", buf, bufSize);
		}
		_buf = buf;
		_bufSize = bufSize;
		_pool.create(_buf, _bufSize);
		_rp = new bsl::ResourcePool(_pool);

		ChainFilter* filter = new ChainFilter();

		filter->addFilter(new PrepareFilter());
		filter->addFilter(new BalanceFilter());
		filter->addFilter(new ConnectFilter());
		filter->addFilter(new HealthyFilter());
		filter->addFilter(new CrossRoomFilter());
		filter->addFilter(new SelectFilter());

		_chain_filter = filter;
		_isDebug = false;
		//UB_LOG_DEBUG("CamelSuperStrategy(): pool[%p],buf_size[%lu],buf_left[%lu],buf_free[%lu]", _pool._buffer, _pool._bufcap, _pool._bufleft, _pool._free);
	}
Beispiel #19
0
int Grab::getGrabNum(const string& user_id, string& num, int32_t is_pre)
{
	extern string IP;
	int res;
	string query_sql;
	//DuokooMysql mysql;
    DuokooMysql mysql("grab_mysql");
	if(!mysql.startTransaction()){
		return -5;
	}
	//get num from mysql
	//comment by zhengxie 2013.10.22
	/*query_sql="SELECT num FROM `MCP`.`mcp_content_grab_num` WHERE `is_occupy` = 0 AND `grab_id` = " + toString(_id) + " limit 0, 1";*/
	//UB_LOG_DEBUG("query_sql:[%s]", query_sql.c_str());
	//res=mysql.query(query_sql);
	//if(res<0){
		//UB_LOG_FATAL( "query sql[%s] failed, [%s], [%s:%d]", query_sql.c_str(), mysql.getErrDes().c_str(), __FILE__, __LINE__ );
		//mysql.rollback();
		//return -5;
	//}else if(res==0){
		//UB_LOG_FATAL( "query sql[%s] have no record, [%s:%d]", query_sql.c_str(), __FILE__, __LINE__ );
		//mysql.rollback();
		//return -3;
	//}
	/*num=mysql.getResult(0,0);*/

	/* 2013.10.22 add by zhengxie get num from mysql => get num from redis start */
	redisContext *c;
    uint32_t send_amount(0);
    c=redisConnPool::getInstance()->getConnection();
    if(c==NULL)
	{
        UB_LOG_FATAL( "get redis connection failed, [%s:%d]", __FILE__, __LINE__ );
        return -5;
    }
    redisReply *reply;
	/* 2013.12.03 add by zhengxie add lock */

	string thread_id = DuokooTools::toString(CommonInterface::get_thread_id());
	string random = DuokooTools::toString(getRandomNum(10000000));

	string key = "grab_and_user_id:" + DuokooTools::toString(_id) + "," + user_id;
	string value = IP + "," + thread_id + "," + random;

	redisCommand(c, "SETNX %s %s", key.c_str(), value.c_str());

	LOGD( "[zx]get_value key:%s ", key.c_str() );
    reply = (redisReply*)redisCommand( c, "GET %s", key.c_str() );

    redisConnPool::getInstance()->releaseConnection(c);
    if(reply!=NULL)
	{
        if(reply->type!=REDIS_REPLY_ERROR)
		{
            if(reply->type!=REDIS_REPLY_NIL)
			{
                string temp_value = reply->str;
				freeReplyObject(reply);
				reply = NULL;
				if(strcmp(temp_value.c_str(), value.c_str()))
				{
					LOGA( "[zx] shit, bad user[%s] ", user_id.c_str() );
					return -3;//同一个用户重复抢号
				}
            }
			else
			{
				freeReplyObject(reply);
				reply = NULL;
				return -3;
            }
		}
	}
	else
	{
		LOGA( "[zx] key not set[%s] ", key.c_str() );
		return -5;
	}

	/* 2013.12.03 add by zhengxie add lock */

	c=redisConnPool::getInstance()->getConnection();
    if(c==NULL)
	{
        UB_LOG_FATAL( "get redis connection failed, [%s:%d]", __FILE__, __LINE__ );
        return -5;
    }

	redisCommand(c, "EXPIRE %s %ld", key.c_str(), 10*24*60*60);
    //LOGD( "[zx]SPOP grab_id:%d ", _id );
    reply = (redisReply*)redisCommand( c, "SPOP grab_id:%d", _id );
    redisConnPool::getInstance()->releaseConnection(c);
    if(reply!=NULL)
	{
        if(reply->type!=REDIS_REPLY_ERROR)
		{
            if(reply->type!=REDIS_REPLY_NIL)
			{
                LOGA( "grab_id:[%d], user_id:[%s], grab_num:[%s]", _id, user_id.c_str(), reply->str );
                num=reply->str;
				freeReplyObject(reply);
				reply = NULL;
            }
			else
			{
				LOGD( "[zx] has no grab_nums grab_id[%d] ", _id );
				freeReplyObject(reply);
				reply = NULL;
				return -3;
            }
		}
	}
	else
	{
		LOGD( "[zx] not exist grab_id[%d] ", _id );
		return -5;
	}

	/* 2013.10.22 add by zhengxie get num from mysql => get num from redis end */
	//update num
	query_sql="UPDATE `MCP`.`mcp_content_grab_num` SET `is_occupy` = "+ toString(is_pre) +" WHERE num = '" + num + "' ";
	UB_LOG_DEBUG("query_sql:[%s]", query_sql.c_str());
	res=mysql.query(query_sql);
	if(res<0){
		UB_LOG_FATAL( "query sql[%s] failed, [%s], [%s:%d]", query_sql.c_str(), mysql.getErrDes().c_str(), __FILE__, __LINE__ );
		mysql.rollback();
		return -5;
	}
	//insert user_num
	query_sql="INSERT INTO `MCP`.`mcp_content_user_grab`( `id`, `grab_id`, `user_id`, `grab_time`, `use_time`, `num` )"
		"VALUES( NULL, '"+toString(_id)+"', '"+user_id+"', '"+NOW()+"', '0', '"+num+"' ) ";
	UB_LOG_DEBUG("query_sql:[%s]", query_sql.c_str());
	res=mysql.query(query_sql);
	if(res<0){
		UB_LOG_FATAL( "query sql[%s] failed, [%s], [%s:%d]", query_sql.c_str(), mysql.getErrDes().c_str(), __FILE__, __LINE__ );
		mysql.rollback();
		return -5;
	}
	return mysql.commit()?0:-5;
}