tsdbHealth.c 2.8 KB
Newer Older
A
AlexDuan 已提交
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 "os.h"
H
Hongze Cheng 已提交
17
#include "tmsg.h"
A
AlexDuan 已提交
18 19 20
#include "tarray.h"
#include "query.h"
#include "tglobal.h"
A
AlexDuan 已提交
21
#include "tlist.h"
A
AlexDuan 已提交
22 23 24 25 26
#include "tsdbint.h"
#include "tsdbBuffer.h"
#include "tsdbLog.h"
#include "tsdbHealth.h"
#include "ttimer.h"
A
AlexDuan 已提交
27
#include "tthread.h"
A
AlexDuan 已提交
28

A
AlexDuan 已提交
29

A
AlexDuan 已提交
30 31 32 33 34
// return malloc new block count 
int32_t tsdbInsertNewBlock(STsdbRepo * pRepo) {
  STsdbBufPool *pPool = pRepo->pPool;
  int32_t cnt = 0;

35
  if(tsdbAllowNewBlock(pRepo)) {
A
AlexDuan 已提交
36 37 38 39 40 41
    STsdbBufBlock *pBufBlock = tsdbNewBufBlock(pPool->bufBlockSize);
    if (pBufBlock) {
        if (tdListAppend(pPool->bufBlockList, (void *)(&pBufBlock)) < 0) {
          // append error
          tsdbFreeBufBlock(pBufBlock);
        } else {
A
AlexDuan 已提交
42
          pPool->nElasticBlocks ++;
A
AlexDuan 已提交
43
          cnt ++ ;
A
AlexDuan 已提交
44 45 46 47 48 49 50
        }
    }
 } 
 return cnt;
}

// switch anther thread to run
A
AlexDuan 已提交
51 52
void* cbKillQueryFree(void* param) {
  STsdbRepo* pRepo =  (STsdbRepo*)param;
A
AlexDuan 已提交
53
  // vnode
54 55
  if(pRepo->appH.notifyStatus) {
    pRepo->appH.notifyStatus(pRepo->appH.appH, TSDB_STATUS_COMMIT_NOBLOCK, TSDB_CODE_SUCCESS);
A
AlexDuan 已提交
56
  }
A
AlexDuan 已提交
57 58 59 60 61 62 63 64 65

  // free 
  if(pRepo->pthread){
    void* p = pRepo->pthread;
    pRepo->pthread = NULL;
    free(p);
  }

  return NULL;
A
AlexDuan 已提交
66 67 68 69
}

// return true do free , false do nothing
bool tsdbUrgeQueryFree(STsdbRepo * pRepo) {
A
AlexDuan 已提交
70 71 72 73
  // check previous running
  if(pRepo->pthread && taosThreadRunning(pRepo->pthread)) {
    tsdbWarn("vgId:%d pre urge thread is runing. nBlocks=%d nElasticBlocks=%d", REPO_ID(pRepo), pRepo->pPool->nBufBlocks, pRepo->pPool->nElasticBlocks);
    return false;
A
AlexDuan 已提交
74
  }
A
AlexDuan 已提交
75 76 77 78 79 80 81
  // create new
  pRepo->pthread = taosCreateThread(cbKillQueryFree, pRepo);
  if(pRepo->pthread == NULL) {
    tsdbError("vgId:%d create urge thread error.", REPO_ID(pRepo));
    return false;
  }
  return true;
A
AlexDuan 已提交
82 83
}

84
bool tsdbAllowNewBlock(STsdbRepo* pRepo) {
85
  int32_t nMaxElastic = pRepo->config.totalBlocks/3;
A
AlexDuan 已提交
86
  STsdbBufPool* pPool = pRepo->pPool;
A
AlexDuan 已提交
87
  if(pPool->nElasticBlocks >= nMaxElastic) {
A
AlexDuan 已提交
88
    tsdbWarn("vgId:%d tsdbAllowNewBlock return fasle. nElasticBlock(%d) >= MaxElasticBlocks(%d)", REPO_ID(pRepo), pPool->nElasticBlocks, nMaxElastic);
A
AlexDuan 已提交
89 90 91 92
    return false;
  }
  return true;
}
A
AlexDuan 已提交
93 94

bool tsdbNoProblem(STsdbRepo* pRepo) {
A
AlexDuan 已提交
95
  if(listNEles(pRepo->pPool->bufBlockList) == 0) 
A
AlexDuan 已提交
96 97 98
     return false;
  return true;
}