Demo entry 6757926

cl

   

Submitted by lu on Aug 09, 2018 at 10:53
Language: C. Code size: 4.3 kB.

int32_t putCacheValueBySGL(uint32_t poolId,
                           KEY_STR_S *key,
                           VALUE_SGL_S *value,
                           KV_PARAM_S *params)
{
    WRITE_CTX_S ctx;
    //创建proc的指针,proc是执行多个步骤的机构
    WCACHE_WRITE_PROC_S *proc = NULL;
    //创建cache pool的指针
    CACHE_POOL_S *cachePool = NULL;
    //创建一个proc的参数param
    WB_PROC_INIT_PARAM_S param;
    //缓存值
    VALUE_SGL_S cacheValue;

    //取proc的地址
    uint64_t ftdsKey = (uint64_t)&proc;
    //统计开始,埋点相关
    CACHE_FTDS_DELAY_START(WCACHE_FTDS_WRITE, &ftdsKey);
    //使一个计数器中start变量加1
    WCACHE_IO_START(WCACHE_IO_COUNTER_WRITE);

    //检查cache是否开工,不开工进入if逻辑。不太理解unlikely和likely的意义所在
    if (unlikely(!g_notifyIsSysPowerOn))
    {
        INC_WCACHE_LABEL(1, WCACHE_NOT_POWER_ON);
        WCACHE_IO_BACKBAD(WCACHE_IO_COUNTER_WRITE);
        CACHE_LOGERROR_LIMIT(0, "Cache not power on.");
        CACHE_FTDS_DELAY_END(WCACHE_FTDS_WRITE, &ftdsKey, IO_STATUS_EXTERNAL_ERROR);
        return RETURN_CACHE_ERROR;
    }

    //根据shardId设置vNodeId
    params->vNodeId = VNODE_getVnodeIdByShardId(params->shardId);
    //检查vNode是否合法,不合法进入if逻辑
    if (unlikely(INVALID_VALUE16 == params->vNodeId))
    {
        INC_WCACHE_LABEL(1, WCACHE_WRITE_INVALID_PARAM);
        WCACHE_IO_BACKBAD(WCACHE_IO_COUNTER_WRITE);
        CACHE_LOGERROR_LIMIT(0, "Invalid shardId(%u).",params->shardId);
        CACHE_FTDS_DELAY_END(WCACHE_FTDS_WRITE, &ftdsKey, IO_STATUS_EXTERNAL_ERROR);
        return RETURN_CACHE_ERROR;
    }

    //根据传进来的poolId寻找cachePool
    cachePool = searchCachePoolByPoolId(poolId);
    //检查cachePool是否搜索获取成功,不成功进入if逻辑
    if (unlikely(NULL == cachePool))
    {
        CACHE_LOGERROR_LIMIT(0, "Get cache pool failed, cachePool id:%u", poolId);

        WCACHE_IO_BACKBAD(WCACHE_IO_COUNTER_WRITE);
        INC_WCACHE_LABEL(1, WCACHE_WRITE_GET_CACHEPOOL_FAIL);
        CACHE_FTDS_DELAY_END(WCACHE_FTDS_WRITE, &ftdsKey, IO_STATUS_EXTERNAL_ERROR);
        return RETURN_CACHE_ERROR;
    }

    //检查参数中的偏移量和长度是否大于cachePool的valueSize
    if (params->offset + params->len > cachePool->valueSize)
    {
        INC_WCACHE_LABEL(1, WCACHE_WRITE_INVALID_PARAM);
        WCACHE_IO_BACKBAD(WCACHE_IO_COUNTER_WRITE);
        CACHE_LOGERROR_LIMIT(0, "Not supported io: offset(%u) len(%u)", 
                             params->offset, params->len);
        CACHE_FTDS_DELAY_END(WCACHE_FTDS_WRITE, &ftdsKey, IO_STATUS_EXTERNAL_ERROR);        

        return RETURN_CACHE_ERROR;
    }

    //打印写IO信息?
    WCACHE_PRINT_WRITE_IO(W_PRINTIO_INFO_WRITE_START, (uint32_t)cachePool->poolId, (uint32_t)cachePool->objId,
                            key, value, params, RETURN_OK);

    //将各种参数设置到param中
    param.poolId = poolId;
    param.cachePool = cachePool;
    param.key       = key;
    //非对齐IO会修改value.sgl,需要创建一个临时的
    (void)memcpy_s(&cacheValue, sizeof(VALUE_SGL_S), value, sizeof(VALUE_SGL_S));
    param.value     = &cacheValue;
    param.param     = params;
    param.chunkCnt  = (uint32_t)GET_SLICE_CNT_2N((uint64_t)params->offset, params->len, CACHE_CHUNK_SIZE_BYTE);
        
    //创建CacheProc,主流程
    proc = (WCACHE_WRITE_PROC_S*)CP_createCacheProc(__initWcacheWriteProc, &param, CACHE_PROC_SCHED_POLICY_SYNC_LWT, NULL);
    //检查proc是否创建成功,不成功进入流程
    if (unlikely(NULL == proc))
    {
        CACHE_LOGERROR_LIMIT(0, "Create cache proc failed");
        //CACHE_PRINT_IO(G_PRINT_IO_INFO_WRITE_END, feGenIO->cacheObj, feGenIO, RETURN_ERROR, 0);
        //CACHE_FTDS_COUNT_END(NCACHE_FTDS_PHASEID_VOLUME_CACHE, OP_WRITE, CACHE_IO_RESULT_ERROR);

        WCACHE_IO_BACKRETRY(WCACHE_IO_COUNTER_WRITE);
        INC_WCACHE_LABEL(1, WCACHE_WRITE_CREATE_PROC_FAIL);
        CACHE_FTDS_DELAY_END(WCACHE_FTDS_WRITE, &ftdsKey, IO_STATUS_EXTERNAL_ERROR);

        return RETURN_CACHE_BUSY;
    }

    proc->ctx = &ctx;

    //同步执行缓存proc
    (void)CP_syncExecuteCacheProc(&proc->cpHandle);

    //通过检查ctx中的result来将对应返回结果加1,如成功、失败、重试
    WCACHE_IO_BACK(WCACHE_IO_COUNTER_WRITE, ctx.result);

    //埋点,对结果进行记录
    CACHE_FTDS_DELAY_END(WCACHE_FTDS_WRITE, &ftdsKey, RETURN_CACHE_OK == ctx.result ? IO_STATUS_OK : IO_STATUS_EXTERNAL_ERROR);    
    
    //返回运行结果
    return __convertRetCode(ctx.result);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).