tsdbFS.h 4.3 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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_TSDB_FS_H_
#define _TD_TSDB_FS_H_

19
/**
C
Cary Xu 已提交
20 21 22
 * 1. The fileset .head/.data/.last/.sma use the same fver 0 before 2021.09.05.
 * 2. .head fver is 1 when extract aggregate block data from .data file and save to separate .sma file since 2021.09.05
 * // TODO update date and add release version.
23
 */
C
Cary Xu 已提交
24
typedef enum {
25 26 27
  TSDB_FS_VER_0,
  TSDB_FS_VER_1,
} ETsdbFsVer;
C
Cary Xu 已提交
28

29
#define TSDB_FVER_TYPE uint32_t
C
Cary Xu 已提交
30
#define TSDB_LATEST_FVER TSDB_FS_VER_1
31 32 33

static FORCE_INLINE uint32_t tsdbGetDFSVersion(TSDB_FILE_T fType) {  // latest version for DFile
  switch (fType) {
C
Cary Xu 已提交
34
    case TSDB_FILE_HEAD:
35
      return TSDB_FS_VER_1;
C
Cary Xu 已提交
36
    default:
37
      return TSDB_FS_VER_0;
C
Cary Xu 已提交
38 39 40
  }
}

41 42 43
static FORCE_INLINE uint32_t tsdbGetSFSVersion() { return TSDB_FS_VER_1; }  // latest version for current

int tsdbRefactorFS(STsdbRepo *pRepo);
H
Hongze Cheng 已提交
44

L
Liu Jicong 已提交
45 46 47
// ================== TSDB global config
extern bool tsdbForceKeepFile;

H
Hongze Cheng 已提交
48 49
// ================== CURRENT file header info
typedef struct {
H
Hongze Cheng 已提交
50
  uint32_t version;  // Current file system version (relating to code)
H
Hongze Cheng 已提交
51
  uint32_t len;      // Encode content length (including checksum)
H
Hongze Cheng 已提交
52 53 54
} SFSHeader;

// ================== TSDB File System Meta
H
refact  
Hongze Cheng 已提交
55
typedef struct {
H
Hongze Cheng 已提交
56
  uint32_t version;       // Commit version from 0 to increase
H
Hongze Cheng 已提交
57 58
  int64_t  totalPoints;   // total points
  int64_t  totalStorage;  // Uncompressed total storage
H
refact  
Hongze Cheng 已提交
59 60
} STsdbFSMeta;

H
Hongze Cheng 已提交
61
// ==================
H
refact  
Hongze Cheng 已提交
62
typedef struct {
H
Hongze Cheng 已提交
63
  STsdbFSMeta meta;  // FS meta
H
Hongze Cheng 已提交
64
  SMFile*     pmf;   // meta file pointer
H
Hongze Cheng 已提交
65 66 67
  SMFile      mf;    // meta file
  SArray*     df;    // data file array
} SFSStatus;
H
refact  
Hongze Cheng 已提交
68 69 70 71

typedef struct {
  pthread_rwlock_t lock;

72 73 74
  SFSStatus* cstatus;         // current status
  SHashObj*  metaCache;       // meta cache
  SHashObj*  metaCacheComp;   // meta cache for compact
H
Hongze Cheng 已提交
75
  bool       intxn;
H
Hongze Cheng 已提交
76
  SFSStatus* nstatus;  // new status
H
refact  
Hongze Cheng 已提交
77 78
} STsdbFS;

H
Hongze Cheng 已提交
79 80 81
#define FS_CURRENT_STATUS(pfs) ((pfs)->cstatus)
#define FS_NEW_STATUS(pfs) ((pfs)->nstatus)
#define FS_IN_TXN(pfs) (pfs)->intxn
H
refact  
Hongze Cheng 已提交
82
#define FS_VERSION(pfs) ((pfs)->cstatus->meta.version)
H
Hongze Cheng 已提交
83
#define FS_TXN_VERSION(pfs) ((pfs)->nstatus->meta.version)
H
Hongze Cheng 已提交
84

H
refact  
Hongze Cheng 已提交
85
typedef struct {
H
Hongze Cheng 已提交
86
  int        direction;
H
Hongze Cheng 已提交
87
  uint64_t   version;  // current FS version
H
Hongze Cheng 已提交
88 89 90
  STsdbFS*   pfs;
  int        index;  // used to position next fset when version the same
  int        fid;    // used to seek when version is changed
H
refact  
Hongze Cheng 已提交
91 92 93
  SDFileSet* pSet;
} SFSIter;

H
Hongze Cheng 已提交
94 95 96
#define TSDB_FS_ITER_FORWARD TSDB_ORDER_ASC
#define TSDB_FS_ITER_BACKWARD TSDB_ORDER_DESC

H
Hongze Cheng 已提交
97
STsdbFS *tsdbNewFS(STsdbCfg *pCfg);
H
Hongze Cheng 已提交
98
void *   tsdbFreeFS(STsdbFS *pfs);
H
Hongze Cheng 已提交
99 100
int      tsdbOpenFS(STsdbRepo *pRepo);
void     tsdbCloseFS(STsdbRepo *pRepo);
H
Hongze Cheng 已提交
101 102
void     tsdbStartFSTxn(STsdbRepo *pRepo, int64_t pointsAdd, int64_t storageAdd);
int      tsdbEndFSTxn(STsdbRepo *pRepo);
H
Hongze Cheng 已提交
103 104
int      tsdbEndFSTxnWithError(STsdbFS *pfs);
void     tsdbUpdateFSTxnMeta(STsdbFS *pfs, STsdbFSMeta *pMeta);
H
Hongze Cheng 已提交
105 106
void     tsdbUpdateMFile(STsdbFS *pfs, const SMFile *pMFile);
int      tsdbUpdateDFileSet(STsdbFS *pfs, const SDFileSet *pSet);
H
refact  
Hongze Cheng 已提交
107

H
Hongze Cheng 已提交
108 109 110
void       tsdbFSIterInit(SFSIter *pIter, STsdbFS *pfs, int direction);
void       tsdbFSIterSeek(SFSIter *pIter, int fid);
SDFileSet *tsdbFSIterNext(SFSIter *pIter);
H
Hongze Cheng 已提交
111
int        tsdbLoadMetaCache(STsdbRepo *pRepo, bool recoverMeta);
H
Hongze Cheng 已提交
112

H
Hongze Cheng 已提交
113
static FORCE_INLINE int tsdbRLockFS(STsdbFS* pFs) {
H
refact  
Hongze Cheng 已提交
114 115 116 117 118 119 120 121
  int code = pthread_rwlock_rdlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
122
static FORCE_INLINE int tsdbWLockFS(STsdbFS* pFs) {
H
refact  
Hongze Cheng 已提交
123 124 125 126 127 128 129 130
  int code = pthread_rwlock_wrlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
131
static FORCE_INLINE int tsdbUnLockFS(STsdbFS* pFs) {
H
refact  
Hongze Cheng 已提交
132 133 134 135 136 137 138 139
  int code = pthread_rwlock_unlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

L
Liu Jicong 已提交
140
#endif /* _TD_TSDB_FS_H_ */