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
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
M
Minghao Li 已提交
17
#include "syncRespMgr.h"
18
#include "syncRaftEntry.h"
19
#include "syncRaftStore.h"
M
Minghao Li 已提交
20 21

SSyncRespMgr *syncRespMgrCreate(void *data, int64_t ttl) {
S
Shengliang Guan 已提交
22
  SSyncRespMgr *pObj = taosMemoryCalloc(1, sizeof(SSyncRespMgr));
23 24 25 26
  if (pObj == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
M
Minghao Li 已提交
27 28 29

  pObj->pRespHash =
      taosHashInit(sizeof(uint64_t), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
S
Shengliang Guan 已提交
30 31
  if (pObj->pRespHash == NULL) return NULL;

M
Minghao Li 已提交
32 33 34 35 36 37 38 39 40
  pObj->ttl = ttl;
  pObj->data = data;
  pObj->seqNum = 0;
  taosThreadMutexInit(&(pObj->mutex), NULL);

  return pObj;
}

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

S
Shengliang Guan 已提交
50 51
uint64_t syncRespMgrAdd(SSyncRespMgr *pObj, const SRespStub *pStub) {
  taosThreadMutexLock(&pObj->mutex);
M
Minghao Li 已提交
52

S
Shengliang Guan 已提交
53 54 55 56
  uint64_t seq = ++(pObj->seqNum);
  int32_t  code = taosHashPut(pObj->pRespHash, &seq, sizeof(uint64_t), pStub, sizeof(SRespStub));
  sNTrace(pObj->data, "save message handle:%p, type:%s seq:%" PRIu64 " code:0x%x", pStub->rpcMsg.info.handle,
          TMSG_INFO(pStub->rpcMsg.msgType), seq, code);
M
Minghao Li 已提交
57

S
Shengliang Guan 已提交
58 59
  taosThreadMutexUnlock(&pObj->mutex);
  return seq;
M
Minghao Li 已提交
60 61
}

S
Shengliang Guan 已提交
62 63
int32_t syncRespMgrDel(SSyncRespMgr *pObj, uint64_t seq) {
  taosThreadMutexLock(&pObj->mutex);
M
Minghao Li 已提交
64

S
Shengliang Guan 已提交
65 66
  int32_t code = taosHashRemove(pObj->pRespHash, &seq, sizeof(seq));
  sNTrace(pObj->data, "remove message handle, seq:%" PRIu64 " code:%d", seq, code);
M
Minghao Li 已提交
67

S
Shengliang Guan 已提交
68 69
  taosThreadMutexUnlock(&pObj->mutex);
  return code;
M
Minghao Li 已提交
70 71
}

S
Shengliang Guan 已提交
72 73
int32_t syncRespMgrGet(SSyncRespMgr *pObj, uint64_t seq, SRespStub *pStub) {
  taosThreadMutexLock(&pObj->mutex);
M
Minghao Li 已提交
74

S
Shengliang Guan 已提交
75
  SRespStub *pTmp = taosHashGet(pObj->pRespHash, &seq, sizeof(uint64_t));
M
Minghao Li 已提交
76 77
  if (pTmp != NULL) {
    memcpy(pStub, pTmp, sizeof(SRespStub));
S
Shengliang Guan 已提交
78 79
    sNTrace(pObj->data, "get message handle, type:%s seq:%" PRIu64 " handle:%p", TMSG_INFO(pStub->rpcMsg.msgType), seq,
            pStub->rpcMsg.info.handle);
M
Minghao Li 已提交
80

S
Shengliang Guan 已提交
81
    taosThreadMutexUnlock(&pObj->mutex);
M
Minghao Li 已提交
82 83
    return 1;  // get one object
  }
S
Shengliang Guan 已提交
84 85

  taosThreadMutexUnlock(&pObj->mutex);
M
Minghao Li 已提交
86 87 88
  return 0;  // get none object
}

S
Shengliang Guan 已提交
89 90
int32_t syncRespMgrGetAndDel(SSyncRespMgr *pObj, uint64_t seq, SRpcHandleInfo *pInfo) {
  taosThreadMutexLock(&pObj->mutex);
M
Minghao Li 已提交
91

S
Shengliang Guan 已提交
92 93 94 95 96 97
  SRespStub *pStub = taosHashGet(pObj->pRespHash, &seq, sizeof(uint64_t));
  if (pStub != NULL) {
    *pInfo = pStub->rpcMsg.info;
    sNTrace(pObj->data, "get-and-del message handle:%p, type:%s seq:%" PRIu64, pStub->rpcMsg.info.handle,
            TMSG_INFO(pStub->rpcMsg.msgType), seq);
    taosHashRemove(pObj->pRespHash, &seq, sizeof(uint64_t));
M
Minghao Li 已提交
98

S
Shengliang Guan 已提交
99
    taosThreadMutexUnlock(&pObj->mutex);
M
Minghao Li 已提交
100 101 102
    return 1;  // get one object
  }

S
Shengliang Guan 已提交
103 104
  taosThreadMutexUnlock(&pObj->mutex);
  return 0;  // get none object
M
Minghao Li 已提交
105 106
}

S
Shengliang Guan 已提交
107
static void syncRespCleanByTTL(SSyncRespMgr *pObj, int64_t ttl, bool rsp) {
108 109
  SRespStub *pStub = (SRespStub *)taosHashIterate(pObj->pRespHash, NULL);
  int        cnt = 0;
M
Minghao Li 已提交
110
  int        sum = 0;
111 112
  SSyncNode *pSyncNode = pObj->data;

S
Shengliang Guan 已提交
113 114
  SArray *delIndexArray = taosArrayInit(4, sizeof(uint64_t));
  if (delIndexArray == NULL) return;
115

S
Shengliang Guan 已提交
116
  sDebug("vgId:%d, resp mgr begin clean by ttl", pSyncNode->vgId);
117
  while (pStub) {
M
Minghao Li 已提交
118
    size_t    len;
S
Shengliang Guan 已提交
119
    void     *key = taosHashGetKey(pStub, &len);
M
Minghao Li 已提交
120
    uint64_t *pSeqNum = (uint64_t *)key;
M
Minghao Li 已提交
121
    sum++;
122 123

    int64_t nowMS = taosGetTimestampMs();
124
    if (nowMS - pStub->createTime > ttl || -1 == ttl) {
M
Minghao Li 已提交
125
      taosArrayPush(delIndexArray, pSeqNum);
126 127
      cnt++;

S
Shengliang Guan 已提交
128
      SFsmCbMeta cbMeta = {
S
Shengliang Guan 已提交
129 130 131 132 133 134 135 136 137
          .index = SYNC_INDEX_INVALID,
          .lastConfigIndex = SYNC_INDEX_INVALID,
          .isWeak = false,
          .code = TSDB_CODE_SYN_TIMEOUT,
          .state = pSyncNode->state,
          .seqNum = *pSeqNum,
          .term = SYNC_TERM_INVALID,
          .currentTerm = pSyncNode->pRaftStore->currentTerm,
          .flag = 0,
S
Shengliang Guan 已提交
138
      };
M
Minghao Li 已提交
139

M
Minghao Li 已提交
140 141
      pStub->rpcMsg.pCont = NULL;
      pStub->rpcMsg.contLen = 0;
142 143

      // TODO: and make rpcMsg body, call commit cb
S
Shengliang Guan 已提交
144
      // pSyncNode->pFsm->FpCommitCb(pSyncNode->pFsm, &pStub->rpcMsg, cbMeta);
145 146 147

      pStub->rpcMsg.code = TSDB_CODE_SYN_NOT_LEADER;
      if (pStub->rpcMsg.info.handle != NULL) {
S
Shengliang Guan 已提交
148
        tmsgSendRsp(&pStub->rpcMsg);
149
      }
150 151
    }

S
Shengliang Guan 已提交
152
    pStub = taosHashIterate(pObj->pRespHash, pStub);
153 154 155
  }

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

  for (int32_t i = 0; i < arraySize; ++i) {
M
Minghao Li 已提交
159 160
    uint64_t *pSeqNum = taosArrayGet(delIndexArray, i);
    taosHashRemove(pObj->pRespHash, pSeqNum, sizeof(uint64_t));
161
    sDebug("vgId:%d, resp mgr clean by ttl, seq:%" PRId64 "", pSyncNode->vgId, *pSeqNum);
162 163 164
  }
  taosArrayDestroy(delIndexArray);
}
S
Shengliang Guan 已提交
165 166 167 168 169 170 171 172 173 174 175 176

void syncRespCleanRsp(SSyncRespMgr *pObj) {
  taosThreadMutexLock(&pObj->mutex);
  syncRespCleanByTTL(pObj, -1, true);
  taosThreadMutexUnlock(&pObj->mutex);
}

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