sma.h 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * 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/>.
 */

#ifndef _TD_VNODE_SMA_H_
#define _TD_VNODE_SMA_H_

#include "vnodeInt.h"

#ifdef __cplusplus
extern "C" {
#endif

// smaDebug ================
// clang-format off
#define smaFatal(...) do { if (smaDebugFlag & DEBUG_FATAL) { taosPrintLog("SMA FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }}     while(0)
#define smaError(...) do { if (smaDebugFlag & DEBUG_ERROR) { taosPrintLog("SMA ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }}     while(0)
#define smaWarn(...)  do { if (smaDebugFlag & DEBUG_WARN)  { taosPrintLog("SMA WARN ", DEBUG_WARN, 255, __VA_ARGS__); }}       while(0)
#define smaInfo(...)  do { if (smaDebugFlag & DEBUG_INFO)  { taosPrintLog("SMA ", DEBUG_INFO, 255, __VA_ARGS__); }}            while(0)
C
Cary Xu 已提交
31
#define smaDebug(...) do { if (smaDebugFlag & DEBUG_DEBUG) { taosPrintLog("SMA ", DEBUG_DEBUG, tsdbDebugFlag, __VA_ARGS__); }} while(0)
32 33 34
#define smaTrace(...) do { if (smaDebugFlag & DEBUG_TRACE) { taosPrintLog("SMA ", DEBUG_TRACE, tsdbDebugFlag, __VA_ARGS__); }} while(0)
// clang-format on

35 36
typedef struct SSmaEnv       SSmaEnv;
typedef struct SSmaStat      SSmaStat;
C
Cary Xu 已提交
37 38
typedef struct STSmaStat     STSmaStat;
typedef struct SRSmaStat     SRSmaStat;
39 40 41
typedef struct SSmaKey       SSmaKey;
typedef struct SRSmaInfo     SRSmaInfo;
typedef struct SRSmaInfoItem SRSmaInfoItem;
42 43 44 45 46 47 48

struct SSmaEnv {
  TdThreadRwlock lock;
  int8_t         type;
  SSmaStat      *pStat;
};

49 50 51 52 53
typedef struct {
  int32_t smaRef;
  int32_t refId;
} SSmaMgmt;

C
Cary Xu 已提交
54 55 56
#define SMA_ENV_LOCK(env) ((env)->lock)
#define SMA_ENV_TYPE(env) ((env)->type)
#define SMA_ENV_STAT(env) ((env)->pStat)
57

C
Cary Xu 已提交
58
struct STSmaStat {
C
Cary Xu 已提交
59 60 61
  int8_t    state;  // ETsdbSmaStat
  STSma    *pTSma;  // cache schema
  STSchema *pTSchema;
62 63
};

C
Cary Xu 已提交
64 65
struct SRSmaStat {
  SSma     *pSma;
66
  int64_t   refId;
C
Cary Xu 已提交
67 68 69
  void     *tmrHandle;
  tmr_h     tmrId;
  int32_t   tmrSeconds;
C
Cary Xu 已提交
70 71
  int8_t    triggerStat;
  int8_t    runningStat;
C
Cary Xu 已提交
72 73 74
  SHashObj *rsmaInfoHash;  // key: stbUid, value: SRSmaInfo;
};

75 76
struct SSmaStat {
  union {
C
Cary Xu 已提交
77 78
    STSmaStat tsmaStat;  // time-range-wise sma
    SRSmaStat rsmaStat;  // rollup sma
79 80 81
  };
  T_REF_DECLARE()
};
82

C
Cary Xu 已提交
83 84 85 86 87 88 89
#define SMA_TSMA_STAT(s)     (&(s)->tsmaStat)
#define SMA_RSMA_STAT(s)     (&(s)->rsmaStat)
#define RSMA_INFO_HASH(r)    ((r)->rsmaInfoHash)
#define RSMA_TMR_ID(r)       ((r)->tmrId)
#define RSMA_TMR_HANDLE(r)   ((r)->tmrHandle)
#define RSMA_TRIGGER_STAT(r) (&(r)->triggerStat)
#define RSMA_RUNNING_STAT(r) (&(r)->runningStat)
90
#define RSMA_REF_ID(r)       ((r)->refId)
C
Cary Xu 已提交
91 92 93 94 95 96 97 98

enum {
  TASK_TRIGGER_STAT_INIT = 0,
  TASK_TRIGGER_STAT_ACTIVE = 1,
  TASK_TRIGGER_STAT_INACTIVE = 2,
  TASK_TRIGGER_STAT_CANCELLED = 3,
  TASK_TRIGGER_STAT_FINISHED = 4,
};
99 100 101 102 103 104 105 106 107
void  tdDestroySmaEnv(SSmaEnv *pSmaEnv);
void *tdFreeSmaEnv(SSmaEnv *pSmaEnv);

int32_t tdDropTSma(SSma *pSma, char *pMsg);
int32_t tdDropTSmaData(SSma *pSma, int64_t indexUid);
int32_t tdInsertRSmaData(SSma *pSma, char *msg);

int32_t tdRefSmaStat(SSma *pSma, SSmaStat *pStat);
int32_t tdUnRefSmaStat(SSma *pSma, SSmaStat *pStat);
C
Cary Xu 已提交
108
int32_t tdCheckAndInitSmaEnv(SSma *pSma, int8_t smaType);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

int32_t tdLockSma(SSma *pSma);
int32_t tdUnLockSma(SSma *pSma);

static FORCE_INLINE int32_t tdRLockSmaEnv(SSmaEnv *pEnv) {
  int code = taosThreadRwlockRdlock(&(pEnv->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

static FORCE_INLINE int32_t tdWLockSmaEnv(SSmaEnv *pEnv) {
  int code = taosThreadRwlockWrlock(&(pEnv->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

static FORCE_INLINE int32_t tdUnLockSmaEnv(SSmaEnv *pEnv) {
  int code = taosThreadRwlockUnlock(&(pEnv->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

C
Cary Xu 已提交
140 141 142
static FORCE_INLINE int8_t tdSmaStat(STSmaStat *pTStat) {
  if (pTStat) {
    return atomic_load_8(&pTStat->state);
143 144 145 146
  }
  return TSDB_SMA_STAT_UNKNOWN;
}

C
Cary Xu 已提交
147 148
static FORCE_INLINE bool tdSmaStatIsOK(STSmaStat *pTStat, int8_t *state) {
  if (!pTStat) {
149 150 151 152
    return false;
  }

  if (state) {
C
Cary Xu 已提交
153
    *state = atomic_load_8(&pTStat->state);
154 155
    return *state == TSDB_SMA_STAT_OK;
  }
C
Cary Xu 已提交
156
  return atomic_load_8(&pTStat->state) == TSDB_SMA_STAT_OK;
157 158
}

C
Cary Xu 已提交
159 160
static FORCE_INLINE bool tdSmaStatIsExpired(STSmaStat *pTStat) {
  return pTStat ? (atomic_load_8(&pTStat->state) & TSDB_SMA_STAT_EXPIRED) : true;
161 162
}

C
Cary Xu 已提交
163 164
static FORCE_INLINE bool tdSmaStatIsDropped(STSmaStat *pTStat) {
  return pTStat ? (atomic_load_8(&pTStat->state) & TSDB_SMA_STAT_DROPPED) : true;
165 166
}

C
Cary Xu 已提交
167 168 169
static FORCE_INLINE void tdSmaStatSetOK(STSmaStat *pTStat) {
  if (pTStat) {
    atomic_store_8(&pTStat->state, TSDB_SMA_STAT_OK);
170 171 172
  }
}

C
Cary Xu 已提交
173 174 175
static FORCE_INLINE void tdSmaStatSetExpired(STSmaStat *pTStat) {
  if (pTStat) {
    atomic_or_fetch_8(&pTStat->state, TSDB_SMA_STAT_EXPIRED);
176 177 178
  }
}

C
Cary Xu 已提交
179 180 181
static FORCE_INLINE void tdSmaStatSetDropped(STSmaStat *pTStat) {
  if (pTStat) {
    atomic_or_fetch_8(&pTStat->state, TSDB_SMA_STAT_DROPPED);
182 183 184
  }
}

C
Cary Xu 已提交
185 186
static int32_t tdDestroySmaState(SSmaStat *pSmaStat, int8_t smaType);
void          *tdFreeSmaState(SSmaStat *pSmaStat, int8_t smaType);
187
void          *tdFreeRSmaInfo(SRSmaInfo *pInfo);
188

189 190
int32_t tdProcessRSmaCreateImpl(SSma *pSma, SRSmaParam *param, int64_t suid, const char *tbName);
int32_t tdProcessRSmaRestoreImpl(SSma *pSma);
C
Cary Xu 已提交
191
int32_t tdProcessTSmaCreateImpl(SSma *pSma, int64_t version, const char *pMsg);
C
Cary Xu 已提交
192 193
int32_t tdProcessTSmaInsertImpl(SSma *pSma, int64_t indexUid, const char *msg);
int32_t tdProcessTSmaGetDaysImpl(SVnodeCfg *pCfg, void *pCont, uint32_t contLen, int32_t *days);
C
Cary Xu 已提交
194

C
Cary Xu 已提交
195 196
// smaFileUtil ================

197 198
#define TD_FILE_HEAD_SIZE 512

C
Cary Xu 已提交
199 200 201 202
typedef struct STFInfo STFInfo;
typedef struct STFile  STFile;

struct STFInfo {
203
  // common fields
C
Cary Xu 已提交
204
  uint32_t magic;
C
Cary Xu 已提交
205
  uint32_t ftype;
C
Cary Xu 已提交
206
  uint32_t fver;
C
Cary Xu 已提交
207
  int64_t  fsize;
208 209 210 211 212 213 214

  // specific fields
  union {
    struct {
      int64_t applyVer[2];
    } qTaskInfo;
  };
C
Cary Xu 已提交
215 216 217 218 219 220 221 222 223
};

struct STFile {
  STFInfo   info;
  STfsFile  f;
  TdFilePtr pFile;
  uint8_t   state;
};

C
Cary Xu 已提交
224 225 226 227 228 229 230 231 232 233
#define TD_TFILE_F(tf)            (&((tf)->f))
#define TD_TFILE_PFILE(tf)        ((tf)->pFile)
#define TD_TFILE_OPENED(tf)       (TD_TFILE_PFILE(tf) != NULL)
#define TD_TFILE_FULL_NAME(tf)    (TD_TFILE_F(tf)->aname)
#define TD_TFILE_REL_NAME(tf)     (TD_TFILE_F(tf)->rname)
#define TD_TFILE_OPENED(tf)       (TD_TFILE_PFILE(tf) != NULL)
#define TD_TFILE_CLOSED(tf)       (!TD_TFILE_OPENED(tf))
#define TD_TFILE_SET_CLOSED(f)    (TD_TFILE_PFILE(f) = NULL)
#define TD_TFILE_SET_STATE(tf, s) ((tf)->state = (s))
#define TD_TFILE_DID(tf)          (TD_TFILE_F(tf)->did)
C
Cary Xu 已提交
234 235 236 237 238 239 240 241

int32_t tdInitTFile(STFile *pTFile, STfs *pTfs, const char *fname);
int32_t tdCreateTFile(STFile *pTFile, STfs *pTfs, bool updateHeader, int8_t fType);
int32_t tdOpenTFile(STFile *pTFile, int flags);
int64_t tdReadTFile(STFile *pTFile, void *buf, int64_t nbyte);
int64_t tdSeekTFile(STFile *pTFile, int64_t offset, int whence);
int64_t tdWriteTFile(STFile *pTFile, void *buf, int64_t nbyte);
int64_t tdAppendTFile(STFile *pTFile, void *buf, int64_t nbyte, int64_t *offset);
242
int64_t tdGetTFileSize(STFile *pTFile, int64_t *size);
C
Cary Xu 已提交
243 244 245 246 247
int32_t tdRemoveTFile(STFile *pTFile);
int32_t tdLoadTFileHeader(STFile *pTFile, STFInfo *pInfo);
int32_t tdUpdateTFileHeader(STFile *pTFile);
void    tdUpdateTFileMagic(STFile *pTFile, void *pCksm);
void    tdCloseTFile(STFile *pTFile);
248

249
void tdGetVndFileName(int32_t vgId, const char *dname, const char *fname, char *outputName);
C
Cary Xu 已提交
250

251 252 253 254
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
255
#endif /*_TD_VNODE_SMA_H_*/