syncRespMgr.c 5.6 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "syncRespMgr.h"
17
#include "syncRaftEntry.h"
18
#include "syncRaftStore.h"
M
Minghao Li 已提交
19 20 21 22 23 24 25

SSyncRespMgr *syncRespMgrCreate(void *data, int64_t ttl) {
  SSyncRespMgr *pObj = (SSyncRespMgr *)taosMemoryMalloc(sizeof(SSyncRespMgr));
  memset(pObj, 0, sizeof(SSyncRespMgr));

  pObj->pRespHash =
      taosHashInit(sizeof(uint64_t), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
M
Minghao Li 已提交
26
  ASSERT(pObj->pRespHash != NULL);
M
Minghao Li 已提交
27 28 29 30 31 32 33 34 35
  pObj->ttl = ttl;
  pObj->data = data;
  pObj->seqNum = 0;
  taosThreadMutexInit(&(pObj->mutex), NULL);

  return pObj;
}

void syncRespMgrDestroy(SSyncRespMgr *pObj) {
M
Minghao Li 已提交
36 37 38 39 40 41 42
  if (pObj != NULL) {
    taosThreadMutexLock(&(pObj->mutex));
    taosHashCleanup(pObj->pRespHash);
    taosThreadMutexUnlock(&(pObj->mutex));
    taosThreadMutexDestroy(&(pObj->mutex));
    taosMemoryFree(pObj);
  }
M
Minghao Li 已提交
43 44 45 46 47 48 49 50
}

int64_t syncRespMgrAdd(SSyncRespMgr *pObj, SRespStub *pStub) {
  taosThreadMutexLock(&(pObj->mutex));

  uint64_t keyCode = ++(pObj->seqNum);
  taosHashPut(pObj->pRespHash, &keyCode, sizeof(keyCode), pStub, sizeof(SRespStub));

M
Minghao Li 已提交
51
  SSyncNode *pSyncNode = pObj->data;
M
Minghao Li 已提交
52
  char       eventLog[128];
S
Shengliang Guan 已提交
53 54
  snprintf(eventLog, sizeof(eventLog), "save response handle, type:%s, seq:%" PRIu64 ", handle:%p, ahandle:%p",
           TMSG_INFO(pStub->rpcMsg.msgType), keyCode, pStub->rpcMsg.info.handle, pStub->rpcMsg.info.ahandle);
M
Minghao Li 已提交
55
  syncNodeEventLog(pSyncNode, eventLog);
M
Minghao Li 已提交
56

M
Minghao Li 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  taosThreadMutexUnlock(&(pObj->mutex));
  return keyCode;
}

int32_t syncRespMgrDel(SSyncRespMgr *pObj, uint64_t index) {
  taosThreadMutexLock(&(pObj->mutex));

  taosHashRemove(pObj->pRespHash, &index, sizeof(index));

  taosThreadMutexUnlock(&(pObj->mutex));
  return 0;
}

int32_t syncRespMgrGet(SSyncRespMgr *pObj, uint64_t index, SRespStub *pStub) {
  taosThreadMutexLock(&(pObj->mutex));

  void *pTmp = taosHashGet(pObj->pRespHash, &index, sizeof(index));
  if (pTmp != NULL) {
    memcpy(pStub, pTmp, sizeof(SRespStub));
M
Minghao Li 已提交
76 77

    SSyncNode *pSyncNode = pObj->data;
M
Minghao Li 已提交
78
    char       eventLog[128];
S
Shengliang Guan 已提交
79 80
    snprintf(eventLog, sizeof(eventLog), "get response handle, type:%s, seq:%" PRIu64 ", handle:%p, ahandle:%p",
             TMSG_INFO(pStub->rpcMsg.msgType), index, pStub->rpcMsg.info.handle, pStub->rpcMsg.info.ahandle);
M
Minghao Li 已提交
81
    syncNodeEventLog(pSyncNode, eventLog);
M
Minghao Li 已提交
82

M
Minghao Li 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
    taosThreadMutexUnlock(&(pObj->mutex));
    return 1;  // get one object
  }
  taosThreadMutexUnlock(&(pObj->mutex));
  return 0;  // get none object
}

int32_t syncRespMgrGetAndDel(SSyncRespMgr *pObj, uint64_t index, SRespStub *pStub) {
  taosThreadMutexLock(&(pObj->mutex));

  void *pTmp = taosHashGet(pObj->pRespHash, &index, sizeof(index));
  if (pTmp != NULL) {
    memcpy(pStub, pTmp, sizeof(SRespStub));
M
Minghao Li 已提交
96 97

    SSyncNode *pSyncNode = pObj->data;
M
Minghao Li 已提交
98
    char       eventLog[128];
S
Shengliang Guan 已提交
99 100
    snprintf(eventLog, sizeof(eventLog), "get-and-del response handle, type:%s, seq:%" PRIu64 ", handle:%p, ahandle:%p",
             TMSG_INFO(pStub->rpcMsg.msgType), index, pStub->rpcMsg.info.handle, pStub->rpcMsg.info.ahandle);
M
Minghao Li 已提交
101
    syncNodeEventLog(pSyncNode, eventLog);
M
Minghao Li 已提交
102

M
Minghao Li 已提交
103
    taosHashRemove(pObj->pRespHash, &index, sizeof(index));
M
Minghao Li 已提交
104
    taosThreadMutexUnlock(&(pObj->mutex));
M
Minghao Li 已提交
105 106 107 108 109 110 111 112 113 114 115 116
    return 1;  // get one object
  }
  taosThreadMutexUnlock(&(pObj->mutex));
  return 0;  // get none object
}

void syncRespClean(SSyncRespMgr *pObj) {
  taosThreadMutexLock(&(pObj->mutex));
  syncRespCleanByTTL(pObj, pObj->ttl);
  taosThreadMutexUnlock(&(pObj->mutex));
}

117 118 119
void syncRespCleanByTTL(SSyncRespMgr *pObj, int64_t ttl) {
  SRespStub *pStub = (SRespStub *)taosHashIterate(pObj->pRespHash, NULL);
  int        cnt = 0;
M
Minghao Li 已提交
120
  int        sum = 0;
121 122
  SSyncNode *pSyncNode = pObj->data;

M
Minghao Li 已提交
123
  SArray *delIndexArray = taosArrayInit(0, sizeof(uint64_t));
124
  ASSERT(delIndexArray != NULL);
M
Minghao Li 已提交
125
  sDebug("vgId:%d, resp mgr begin clean by ttl", pSyncNode->vgId);
126 127

  while (pStub) {
M
Minghao Li 已提交
128
    size_t    len;
129
    void *    key = taosHashGetKey(pStub, &len);
M
Minghao Li 已提交
130
    uint64_t *pSeqNum = (uint64_t *)key;
M
Minghao Li 已提交
131
    sum++;
132 133 134

    int64_t nowMS = taosGetTimestampMs();
    if (nowMS - pStub->createTime > ttl) {
M
Minghao Li 已提交
135
      taosArrayPush(delIndexArray, pSeqNum);
136 137
      cnt++;

M
Minghao Li 已提交
138 139 140 141 142 143 144 145 146 147 148
      SFsmCbMeta cbMeta = {0};
      cbMeta.index = SYNC_INDEX_INVALID;
      cbMeta.lastConfigIndex = SYNC_INDEX_INVALID;
      cbMeta.isWeak = false;
      cbMeta.code = TSDB_CODE_SYN_TIMEOUT;
      cbMeta.state = pSyncNode->state;
      cbMeta.seqNum = *pSeqNum;
      cbMeta.term = SYNC_TERM_INVALID;
      cbMeta.currentTerm = pSyncNode->pRaftStore->currentTerm;
      cbMeta.flag = 0;

M
Minghao Li 已提交
149 150
      pStub->rpcMsg.pCont = NULL;
      pStub->rpcMsg.contLen = 0;
M
Minghao Li 已提交
151
      pSyncNode->pFsm->FpCommitCb(pSyncNode->pFsm, &(pStub->rpcMsg), cbMeta);
152 153 154 155 156 157
    }

    pStub = (SRespStub *)taosHashIterate(pObj->pRespHash, pStub);
  }

  int32_t arraySize = taosArrayGetSize(delIndexArray);
M
Minghao Li 已提交
158
  sDebug("vgId:%d, resp mgr end clean by ttl, sum:%d, cnt:%d, array-size:%d", pSyncNode->vgId, sum, cnt, arraySize);
159 160

  for (int32_t i = 0; i < arraySize; ++i) {
M
Minghao Li 已提交
161 162 163
    uint64_t *pSeqNum = taosArrayGet(delIndexArray, i);
    taosHashRemove(pObj->pRespHash, pSeqNum, sizeof(uint64_t));
    sDebug("vgId:%d, resp mgr clean by ttl, seq:%d", pSyncNode->vgId, *pSeqNum);
164 165 166
  }
  taosArrayDestroy(delIndexArray);
}