mndShow.c 8.7 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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
S
Shengliang Guan 已提交
17
#include "mndShow.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20
#define SHOW_STEP_SIZE 100

S
Shengliang Guan 已提交
21
static SShowObj *mndCreateShowObj(SMnode *pMnode, SShowReq *pReq);
S
Shengliang Guan 已提交
22
static void      mndFreeShowObj(SShowObj *pShow);
23
static SShowObj *mndAcquireShowObj(SMnode *pMnode, int64_t showId);
S
Shengliang Guan 已提交
24
static void      mndReleaseShowObj(SShowObj *pShow, bool forceRemove);
S
Shengliang Guan 已提交
25
static bool      mndCheckRetrieveFinished(SShowObj *pShow);
S
Shengliang Guan 已提交
26
static int32_t   mndProcessRetrieveSysTableReq(SNodeMsg *pReq);
S
Shengliang Guan 已提交
27 28 29 30

int32_t mndInitShow(SMnode *pMnode) {
  SShowMgmt *pMgmt = &pMnode->showMgmt;

S
Shengliang Guan 已提交
31
  pMgmt->cache = taosCacheInit(TSDB_DATA_TYPE_INT, 5, true, (__cache_free_fn_t)mndFreeShowObj, "show");
S
Shengliang Guan 已提交
32 33 34 35 36 37
  if (pMgmt->cache == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("failed to alloc show cache since %s", terrstr());
    return -1;
  }

38
  mndSetMsgHandle(pMnode, TDMT_MND_SYSTABLE_RETRIEVE, mndProcessRetrieveSysTableReq);
S
Shengliang Guan 已提交
39 40 41
  return 0;
}

S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49
void mndCleanupShow(SMnode *pMnode) {
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  if (pMgmt->cache != NULL) {
    taosCacheCleanup(pMgmt->cache);
    pMgmt->cache = NULL;
  }
}

S
Shengliang Guan 已提交
50
static SShowObj *mndCreateShowObj(SMnode *pMnode, SShowReq *pReq) {
S
Shengliang Guan 已提交
51 52
  SShowMgmt *pMgmt = &pMnode->showMgmt;

53 54
  int64_t showId = atomic_add_fetch_64(&pMgmt->showId, 1);
  if (showId == 0) atomic_add_fetch_64(&pMgmt->showId, 1);
S
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56
  int32_t  size = sizeof(SShowObj) + pReq->payloadLen;
S
Shengliang Guan 已提交
57 58 59
  SShowObj showObj = {0};
  showObj.id = showId;
  showObj.pMnode = pMnode;
S
Shengliang Guan 已提交
60 61 62
  showObj.type = pReq->type;
  showObj.payloadLen = pReq->payloadLen;
  memcpy(showObj.db, pReq->db, TSDB_DB_FNAME_LEN);
S
Shengliang Guan 已提交
63

S
Shengliang Guan 已提交
64
  int32_t   keepTime = tsShellActivityTimer * 6 * 1000;
65
  SShowObj *pShow = taosCachePut(pMgmt->cache, &showId, sizeof(int64_t), &showObj, size, keepTime);
S
Shengliang Guan 已提交
66
  if (pShow == NULL) {
S
Shengliang Guan 已提交
67
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
68
    mError("show:0x%" PRIx64 ", failed to put into cache since %s", showId, terrstr());
S
Shengliang Guan 已提交
69 70
    return NULL;
  }
S
Shengliang Guan 已提交
71

S
Shengliang Guan 已提交
72
  mTrace("show:0x%" PRIx64 ", is created, data:%p", showId, pShow);
S
Shengliang Guan 已提交
73
  return pShow;
S
Shengliang Guan 已提交
74 75
}

S
Shengliang Guan 已提交
76
static void mndFreeShowObj(SShowObj *pShow) {
S
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85 86
  SMnode    *pMnode = pShow->pMnode;
  SShowMgmt *pMgmt = &pMnode->showMgmt;

  ShowFreeIterFp freeFp = pMgmt->freeIterFps[pShow->type];
  if (freeFp != NULL) {
    if (pShow->pIter != NULL) {
      (*freeFp)(pMnode, pShow->pIter);
    }
  }

S
Shengliang Guan 已提交
87
  mTrace("show:0x%" PRIx64 ", is destroyed, data:%p", pShow->id, pShow);
S
Shengliang Guan 已提交
88 89
}

90
static SShowObj *mndAcquireShowObj(SMnode *pMnode, int64_t showId) {
S
Shengliang Guan 已提交
91 92
  SShowMgmt *pMgmt = &pMnode->showMgmt;

93
  SShowObj *pShow = taosCacheAcquireByKey(pMgmt->cache, &showId, sizeof(showId));
S
Shengliang Guan 已提交
94
  if (pShow == NULL) {
S
Shengliang Guan 已提交
95
    mError("show:0x%" PRIx64 ", already destroyed", showId);
S
Shengliang Guan 已提交
96 97 98
    return NULL;
  }

S
Shengliang Guan 已提交
99
  mTrace("show:0x%" PRIx64 ", acquired from cache, data:%p", pShow->id, pShow);
S
Shengliang Guan 已提交
100 101 102 103 104
  return pShow;
}

static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) {
  if (pShow == NULL) return;
S
Shengliang Guan 已提交
105
  mTrace("show:0x%" PRIx64 ", released from cache, data:%p force:%d", pShow->id, pShow, forceRemove);
S
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107 108
  // A bug in tcache.c
  forceRemove = 0;
S
Shengliang Guan 已提交
109 110 111 112

  SMnode    *pMnode = pShow->pMnode;
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  taosCacheRelease(pMgmt->cache, (void **)(&pShow), forceRemove);
S
Shengliang Guan 已提交
113
}
S
Shengliang Guan 已提交
114

S
Shengliang Guan 已提交
115 116
static int32_t mndProcessRetrieveSysTableReq(SNodeMsg *pReq) {
  SMnode    *pMnode = pReq->pNode;
117
  SShowMgmt *pMgmt = &pMnode->showMgmt;
118
  SShowObj  *pShow = NULL;
119
  int32_t    rowsToRead = SHOW_STEP_SIZE;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  int32_t    size = 0;
  int32_t    rowsRead = 0;

  SRetrieveTableReq retrieveReq = {0};
  if (tDeserializeSRetrieveTableReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &retrieveReq) != 0) {
    terrno = TSDB_CODE_INVALID_MSG;
    return -1;
  }

  if (retrieveReq.showId == 0) {
    SShowReq req = {0};
    req.type = retrieveReq.type;
    strncpy(req.db, retrieveReq.db, tListLen(req.db));

    pShow = mndCreateShowObj(pMnode, &req);
    if (pShow == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      mError("failed to process show-meta req since %s", terrstr());
      return -1;
    }
H
Haojun Liao 已提交
140

141
    pShow->pMeta = (STableMetaRsp *)taosHashGet(pMnode->infosMeta, retrieveReq.tb, strlen(retrieveReq.tb) + 1);
142
    pShow->numOfColumns = pShow->pMeta->numOfColumns;
H
Haojun Liao 已提交
143
    int32_t offset = 0;
144

145
    for (int32_t i = 0; i < pShow->pMeta->numOfColumns; ++i) {
H
Haojun Liao 已提交
146 147
      pShow->offset[i] = offset;

148
      int32_t bytes = pShow->pMeta->pSchemas[i].bytes;
H
Haojun Liao 已提交
149 150 151 152
      pShow->rowSize += bytes;
      pShow->bytes[i] = bytes;
      offset += bytes;
    }
153 154 155 156 157 158 159 160 161 162 163
  } else {
    pShow = mndAcquireShowObj(pMnode, retrieveReq.showId);
    if (pShow == NULL) {
      terrno = TSDB_CODE_MND_INVALID_SHOWOBJ;
      mError("failed to process show-retrieve req:%p since %s", pShow, terrstr());
      return -1;
    }
  }

  ShowRetrieveFp retrieveFp = pMgmt->retrieveFps[pShow->type];
  if (retrieveFp == NULL) {
164
    mndReleaseShowObj(pShow, false);
165 166 167 168 169
    terrno = TSDB_CODE_MSG_NOT_PROCESSED;
    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
    return -1;
  }

170
  mDebug("show:0x%" PRIx64 ", start retrieve data, type:%d", pShow->id, pShow->type);
171

172 173
  int32_t      numOfCols = pShow->pMeta->numOfColumns;
  SSDataBlock *pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
174 175
  pBlock->pDataBlock = taosArrayInit(numOfCols, sizeof(SColumnInfoData));
  pBlock->info.numOfCols = numOfCols;
176

177
  for (int32_t i = 0; i < numOfCols; ++i) {
178
    SColumnInfoData idata = {0};
179
    SSchema        *p = &pShow->pMeta->pSchemas[i];
180 181

    idata.info.bytes = p->bytes;
182
    idata.info.type = p->type;
183 184 185 186 187 188 189
    idata.info.colId = p->colId;

    taosArrayPush(pBlock->pDataBlock, &idata);
    if (IS_VAR_DATA_TYPE(p->type)) {
      pBlock->info.hasVarCol = true;
    }
  }
H
Haojun Liao 已提交
190

191
  blockDataEnsureCapacity(pBlock, rowsToRead);
192
  if (mndCheckRetrieveFinished(pShow)) {
193 194 195
    mDebug("show:0x%" PRIx64 ", read finished, numOfRows:%d", pShow->id, pShow->numOfRows);
    rowsRead = 0;
  } else {
196
    rowsRead = (*retrieveFp)(pReq, pShow, pBlock, rowsToRead);
D
dapan1121 已提交
197 198 199
    if (rowsRead < 0) {
      terrno = rowsRead;
      mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
200
      mndReleaseShowObj(pShow, true);
D
dapan1121 已提交
201 202
      return -1;
    }
203

204
    pBlock->info.rows = rowsRead;
205 206
    mDebug("show:0x%" PRIx64 ", stop retrieve data, rowsRead:%d numOfRows:%d", pShow->id, rowsRead, pShow->numOfRows);
  }
207

208 209
  size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * pShow->pMeta->numOfColumns +
         blockDataGetSize(pBlock) + blockDataGetSerialMetaSize(pBlock);
210 211 212

  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
  if (pRsp == NULL) {
213
    mndReleaseShowObj(pShow, false);
214 215 216 217 218 219 220 221 222
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, terrstr());
    blockDataDestroy(pBlock);
    return -1;
  }

  pRsp->handle = htobe64(pShow->id);

  if (rowsRead > 0) {
223
    char    *pStart = pRsp->data;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    SSchema *ps = pShow->pMeta->pSchemas;

    *(int32_t *)pStart = htonl(pShow->pMeta->numOfColumns);
    pStart += sizeof(int32_t);  // number of columns

    for (int32_t i = 0; i < pShow->pMeta->numOfColumns; ++i) {
      SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
      pSchema->bytes = htonl(ps[i].bytes);
      pSchema->colId = htons(ps[i].colId);
      pSchema->type = ps[i].type;

      pStart += sizeof(SSysTableSchema);
    }

    int32_t len = 0;
    blockCompressEncode(pBlock, pStart, &len, pShow->pMeta->numOfColumns, false);
  }

242 243
  pRsp->numOfRows = htonl(rowsRead);
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
244 245
  pReq->pRsp = pRsp;
  pReq->rspLen = size;
246

247
  if (rowsRead == 0 || rowsRead < rowsToRead) {
248 249
    pRsp->completed = 1;
    mDebug("show:0x%" PRIx64 ", retrieve completed", pShow->id);
250
    mndReleaseShowObj(pShow, true);
251 252
  } else {
    mDebug("show:0x%" PRIx64 ", retrieve not completed yet", pShow->id);
253
    mndReleaseShowObj(pShow, false);
254 255
  }

256
  blockDataDestroy(pBlock);
257 258 259
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
260
static bool mndCheckRetrieveFinished(SShowObj *pShow) {
261
  if (pShow->pIter == NULL && pShow->numOfRows != 0) {
S
Shengliang Guan 已提交
262
    return true;
S
Shengliang Guan 已提交
263
  }
S
Shengliang Guan 已提交
264 265 266
  return false;
}

S
Shengliang Guan 已提交
267
void mndAddShowRetrieveHandle(SMnode *pMnode, EShowType showType, ShowRetrieveFp fp) {
S
Shengliang Guan 已提交
268 269 270 271
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  pMgmt->retrieveFps[showType] = fp;
}

S
Shengliang Guan 已提交
272
void mndAddShowFreeIterHandle(SMnode *pMnode, EShowType showType, ShowFreeIterFp fp) {
S
Shengliang Guan 已提交
273 274 275
  SShowMgmt *pMgmt = &pMnode->showMgmt;
  pMgmt->freeIterFps[showType] = fp;
}