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

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

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

  return pObj;
}

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

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

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

S
Shengliang Guan 已提交
55 56
  sNTrace(pObj->data, "save message handle, type:%s seq:%" PRIu64 " handle:%p", TMSG_INFO(pStub->rpcMsg.msgType),
          keyCode, pStub->rpcMsg.info.handle);
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

S
Shengliang Guan 已提交
77 78
    sNTrace(pObj->data, "get message handle, type:%s seq:%" PRIu64 " handle:%p", TMSG_INFO(pStub->rpcMsg.msgType),
            index, pStub->rpcMsg.info.handle);
M
Minghao Li 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
    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 已提交
92

S
Shengliang Guan 已提交
93 94
    sNTrace(pObj->data, "get-and-del message handle, type:%s seq:%" PRIu64 " handle:%p",
            TMSG_INFO(pStub->rpcMsg.msgType), index, pStub->rpcMsg.info.handle);
M
Minghao Li 已提交
95
    taosHashRemove(pObj->pRespHash, &index, sizeof(index));
M
Minghao Li 已提交
96
    taosThreadMutexUnlock(&(pObj->mutex));
M
Minghao Li 已提交
97 98 99 100 101 102
    return 1;  // get one object
  }
  taosThreadMutexUnlock(&(pObj->mutex));
  return 0;  // get none object
}

103 104 105 106 107 108
void syncRespCleanRsp(SSyncRespMgr *pObj) {
  taosThreadMutexLock(&(pObj->mutex));
  syncRespCleanByTTL(pObj, -1, true);
  taosThreadMutexUnlock(&(pObj->mutex));
}

M
Minghao Li 已提交
109 110
void syncRespClean(SSyncRespMgr *pObj) {
  taosThreadMutexLock(&(pObj->mutex));
111
  syncRespCleanByTTL(pObj, pObj->ttl, false);
M
Minghao Li 已提交
112 113 114
  taosThreadMutexUnlock(&(pObj->mutex));
}

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

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

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

    int64_t nowMS = taosGetTimestampMs();
132
    if (nowMS - pStub->createTime > ttl || -1 == ttl) {
M
Minghao Li 已提交
133
      taosArrayPush(delIndexArray, pSeqNum);
134 135
      cnt++;

S
Shengliang Guan 已提交
136
      SFsmCbMeta cbMeta = {
S
Shengliang Guan 已提交
137 138 139 140 141 142 143 144 145
          .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 已提交
146
      };
M
Minghao Li 已提交
147

M
Minghao Li 已提交
148 149
      pStub->rpcMsg.pCont = NULL;
      pStub->rpcMsg.contLen = 0;
150 151 152 153 154 155 156 157

      // TODO: and make rpcMsg body, call commit cb
      // pSyncNode->pFsm->FpCommitCb(pSyncNode->pFsm, &(pStub->rpcMsg), cbMeta);

      pStub->rpcMsg.code = TSDB_CODE_SYN_NOT_LEADER;
      if (pStub->rpcMsg.info.handle != NULL) {
        tmsgSendRsp(&(pStub->rpcMsg));
      }
158 159 160 161 162 163
    }

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

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

  for (int32_t i = 0; i < arraySize; ++i) {
M
Minghao Li 已提交
167 168
    uint64_t *pSeqNum = taosArrayGet(delIndexArray, i);
    taosHashRemove(pObj->pRespHash, pSeqNum, sizeof(uint64_t));
169
    sDebug("vgId:%d, resp mgr clean by ttl, seq:%" PRId64 "", pSyncNode->vgId, *pSeqNum);
170 171 172
  }
  taosArrayDestroy(delIndexArray);
}