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"
S
Shengliang Guan 已提交
20
#include "syncUtil.h"
M
Minghao Li 已提交
21 22

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

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

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

  return pObj;
}

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

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

S
Shengliang Guan 已提交
54 55 56 57
  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 已提交
58

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

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

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

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

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

S
Shengliang Guan 已提交
76
  SRespStub *pTmp = taosHashGet(pObj->pRespHash, &seq, sizeof(uint64_t));
M
Minghao Li 已提交
77 78
  if (pTmp != NULL) {
    memcpy(pStub, pTmp, sizeof(SRespStub));
S
Shengliang Guan 已提交
79 80
    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 已提交
81

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

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

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

S
Shengliang Guan 已提交
93 94 95 96 97 98
  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 已提交
99

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

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

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

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

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

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

S
Shengliang Guan 已提交
129
      SFsmCbMeta cbMeta = {
S
Shengliang Guan 已提交
130 131 132 133 134 135 136 137 138
          .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 已提交
139
      };
M
Minghao Li 已提交
140

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

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

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

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

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

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

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);
}