vnodeRead.h 9.9 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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 TDENGINE_VNODEREAD_H
#define TDENGINE_VNODEREAD_H

#ifdef __cplusplus
extern "C" {
#endif

23
#include "os.h"
24
#include "qresultBuf.h"
H
hzcheng 已提交
25

26
#include "qinterpolation.h"
H
hzcheng 已提交
27 28 29 30 31 32 33 34 35 36 37 38
#include "vnodeTagMgmt.h"

/*
 * use to keep the first point position, consisting of position in blk and block
 * id, file id
 */
typedef struct {
  int32_t pos;
  int32_t slot;
  int32_t fileId;
} SPositionInfo;

39
typedef struct SLoadDataBlockInfo {
H
hzcheng 已提交
40 41 42 43
  int32_t fileListIndex; /* index of this file in files list of this vnode */
  int32_t fileId;
  int32_t slotIdx;
  int32_t sid;
H
hjxilinx 已提交
44
  bool    tsLoaded;      // if timestamp column of current block is loaded or not
45
} SLoadDataBlockInfo;
H
hzcheng 已提交
46

47
typedef struct SLoadCompBlockInfo {
H
hzcheng 已提交
48 49 50
  int32_t sid; /* meter sid */
  int32_t fileId;
  int32_t fileListIndex;
51
} SLoadCompBlockInfo;
52

H
hzcheng 已提交
53 54 55
/*
 * the header file info for one vnode
 */
56
typedef struct SHeaderFileInfo {
H
hjxilinx 已提交
57
  int32_t fileID;  // file id
58
} SHeaderFileInfo;
H
hzcheng 已提交
59

S
slguan 已提交
60
typedef struct SQueryCostSummary {
H
hzcheng 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  double cacheTimeUs;
  double fileTimeUs;

  int64_t numOfFiles;   // opened files during query
  int64_t numOfTables;  // num of queries tables
  int64_t numOfSeek;    // number of seek operation

  int64_t readDiskBlocks;     // accessed disk block
  int64_t skippedFileBlocks;  // skipped blocks
  int64_t blocksInCache;      // accessed cache blocks

  int64_t readField;       // field size
  int64_t totalFieldSize;  // total read fields size
  double  loadFieldUs;     // total elapsed time to read fields info

  int64_t totalBlockSize;  // read data blocks
  double  loadBlocksUs;    // total elapsed time to read data blocks

  int64_t totalGenData;  // in-memory generated data

  int64_t readCompInfo;       // read compblock info
  int64_t totalCompInfoSize;  // total comp block size
  double  loadCompInfoUs;     // total elapsed time to read comp block info

S
slguan 已提交
85 86 87
  int64_t tmpBufferInDisk;  // size of buffer for intermediate result
} SQueryCostSummary;

H
hjxilinx 已提交
88
typedef struct SPosInfo {
89 90
  int16_t pageId;
  int16_t rowId;
H
hjxilinx 已提交
91 92
} SPosInfo;

93 94 95 96 97 98 99 100 101 102
typedef struct STimeWindow {
  TSKEY skey;
  TSKEY ekey;
} STimeWindow;

typedef struct SWindowStatus {
  bool closed;
} SWindowStatus;

typedef struct SWindowResult {
S
slguan 已提交
103
  uint16_t     numOfRows;
104 105 106 107 108
  SPosInfo     pos;         // Position of current result in disk-based output buffer
  SResultInfo* resultInfo;  // For each result column, there is a resultInfo
  STimeWindow  window;      // The time window that current result covers.
  SWindowStatus status;
} SWindowResult;
H
hzcheng 已提交
109

110 111 112 113 114 115 116 117 118
/*
 * header files info, avoid to iterate the directory, the data is acquired
 * during in query preparation function
 */
typedef struct SQueryFilesInfo {
  SHeaderFileInfo* pFileInfo;
  uint32_t         numOfFiles;  // the total available number of files for this virtual node during query execution
  int32_t          current;     // the memory mapped header file, NOTE: only one header file can be mmap.
  int32_t          vnodeId;
119 120 121 122 123
  
  int32_t          headerFd;         // header file fd
  int64_t          headerFileSize;
  int32_t          dataFd;
  int32_t          lastFd;
H
hjxilinx 已提交
124 125 126 127 128

  char headerFilePath[PATH_MAX];  // current opened header file name
  char dataFilePath[PATH_MAX];    // current opened data file name
  char lastFilePath[PATH_MAX];    // current opened last file path
  char dbFilePathPrefix[PATH_MAX];
129 130
} SQueryFilesInfo;

131 132
typedef struct SWindowResInfo {
  SWindowResult*      pResult;    // reference to SQuerySupporter->pResult
H
hjxilinx 已提交
133 134 135 136 137 138 139 140 141
  void*               hashList;   // hash list for quick access
  int16_t             type;       // data type for hash key
  int32_t             capacity;   // max capacity
  int32_t             curIndex;   // current start active index
  int32_t             size;
  
  int64_t             startTime;  // start time of the first time window for sliding query
  int64_t             prevSKey;   // previous (not completed) sliding window start key
  int64_t             threshold;  // threshold for return completed results.
142
} SWindowResInfo;
H
hjxilinx 已提交
143 144

typedef struct SQueryRuntimeEnv {
H
hjxilinx 已提交
145 146 147 148
  SPositionInfo       startPos; /* the start position, used for secondary/third iteration */
  SPositionInfo       endPos;   /* the last access position in query, served as the start pos of reversed order query */
  SPositionInfo       nextPos;  /* start position of the next scan */
  SData*              colDataBuffer[TSDB_MAX_COLUMNS];
149
  SResultInfo*        resultInfo;   // todo refactor to merge with SWindowResInfo
H
hjxilinx 已提交
150 151 152 153 154 155 156 157
  uint8_t             blockStatus;  // Indicate if data block is loaded, the block is first/last/internal block
  int32_t             unzipBufSize;
  SData*              primaryColBuffer;
  char*               unzipBuffer;
  char*               secondaryUnzipBuffer;
  SQuery*             pQuery;
  SMeterObj*          pMeterObj;
  SQLFunctionCtx*     pCtx;
158 159 160 161 162
  SLoadDataBlockInfo  loadBlockInfo;         /* record current block load information */
  SLoadCompBlockInfo  loadCompBlockInfo; /* record current compblock information in SQuery */
  SQueryFilesInfo     vnodeFileInfo;
  int16_t             numOfRowsPerPage;
  int16_t             offset[TSDB_MAX_COLUMNS];
H
hjxilinx 已提交
163
  uint16_t            scanFlag;  // denotes reversed scan of data or not
164 165
  SInterpolationInfo  interpoInfo;
  SData**             pInterpoBuf;
H
hjxilinx 已提交
166
  
167
  SWindowResInfo      windowResInfo;
H
hjxilinx 已提交
168
  
169 170 171
  STSBuf*             pTSBuf;
  STSCursor           cur;
  SQueryCostSummary   summary;
H
hjxilinx 已提交
172 173
  bool                stableQuery;  // is super table query or not
  SQueryDiskbasedResultBuf*    pResultBuf;   // query result buffer based on blocked-wised disk file
H
hjxilinx 已提交
174
  
175 176
  /*
   * Temporarily hold the in-memory cache block info during scan cache blocks
H
hjxilinx 已提交
177 178
   * Here we do not use the cache block info from pMeterObj, simple because it may change anytime
   * during the query by the submit/insert handling threads.
179 180 181
   * So we keep a copy of the support structure as well as the cache block data itself.
   */
  SCacheBlock         cacheBlock;
H
hzcheng 已提交
182 183
} SQueryRuntimeEnv;

H
hjxilinx 已提交
184
/* intermediate pos during multimeter query involves interval */
H
hzcheng 已提交
185
typedef struct SMeterQueryInfo {
S
slguan 已提交
186 187 188 189 190 191 192
  int64_t      lastKey;
  int64_t      skey;
  int64_t      ekey;
  int32_t      numOfRes;
  int16_t      queryRangeSet;   // denote if the query range is set, only available for interval query
  int64_t      tag;
  STSCursor    cur;
193
  int32_t      sid; // for retrieve the page id list
194 195
  
  SWindowResInfo windowResInfo;
H
hzcheng 已提交
196 197 198 199 200 201
} SMeterQueryInfo;

typedef struct SMeterDataInfo {
  uint64_t     offsetInHeaderFile;
  int32_t      numOfBlocks;
  int32_t      start;  // start block index
202
  SCompBlock*  pBlock;
H
hzcheng 已提交
203 204
  int32_t      meterOrderIdx;
  SMeterObj*   pMeterObj;
205
  int32_t      groupIdx;    // group id in meter list
H
hzcheng 已提交
206 207 208
  SMeterQueryInfo* pMeterQInfo;
} SMeterDataInfo;

H
hjxilinx 已提交
209
typedef struct STableQuerySupportObj {
210
  void* pMetersHashTable;   // meter table hash list
H
hzcheng 已提交
211 212 213 214 215 216 217 218 219 220 221 222

  SMeterSidExtInfo** pMeterSidExtInfo;
  int32_t            numOfMeters;

  /*
   * multimeter query resultset.
   * In multimeter queries, the result is temporarily stored on this structure, instead of
   * directly put result into output buffer, since we have no idea how many number of
   * rows may be generated by a specific subgroup. When query on all subgroups is executed,
   * the result is copy to output buffer. This attribution is not used during single meter query processing.
   */
  SQueryRuntimeEnv runtimeEnv;
S
slguan 已提交
223 224 225 226
  int64_t          rawSKey;
  int64_t          rawEKey;
  int32_t          subgroupIdx;
  int32_t          offset; /* offset in group result set of subgroup */
H
hzcheng 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240
  tSidSet* pSidSet;

  /*
   * the query is executed position on which meter of the whole list.
   * when the index reaches the last one of the list, it means the query is completed.
   * We later may refactor to remove this attribution by using another flag to denote
   * whether a multimeter query is completed or not.
   */
  int32_t meterIdx;

  int32_t numOfGroupResultPages;
  int32_t groupResultSize;
  SMeterDataInfo* pMeterDataInfo;

H
hjxilinx 已提交
241
  TSKEY* tsList;
H
hjxilinx 已提交
242
} STableQuerySupportObj;
H
hzcheng 已提交
243 244

typedef struct _qinfo {
H
hjxilinx 已提交
245 246
  uint64_t       signature;
  int32_t        refCount;  // QInfo reference count, when the value is 0, it can be released safely
S
slguan 已提交
247
  char           user[TSDB_TABLE_ID_LEN + 1];
H
hzcheng 已提交
248 249 250 251 252 253 254 255
  char           sql[TSDB_SHOW_SQL_LEN];
  uint8_t        stream;
  uint16_t       port;
  uint32_t       ip;
  uint64_t       startTime;
  int64_t        useconds;
  int            killed;
  struct _qinfo *prev, *next;
H
hjxilinx 已提交
256 257 258 259 260 261 262 263 264 265 266
  SQuery         query;
  int            totalPoints;
  int            pointsRead;
  int            pointsReturned;
  int            pointsInterpo;
  int            code;
  char           bufIndex;
  char           changed;
  char           over;
  SMeterObj*     pObj;
  sem_t          dataReady;
H
hzcheng 已提交
267

H
hjxilinx 已提交
268
  STableQuerySupportObj* pTableQuerySupporter;
H
hjxilinx 已提交
269
  int (*fp)(SMeterObj*, SQuery*);
H
hzcheng 已提交
270 271
} SQInfo;

H
hjxilinx 已提交
272
int32_t vnodeQueryTablePrepare(SQInfo* pQInfo, SMeterObj* pMeterObj, STableQuerySupportObj* pSMultiMeterObj,
S
slguan 已提交
273
                                     void* param);
H
hzcheng 已提交
274 275 276 277 278

void vnodeQueryFreeQInfoEx(SQInfo* pQInfo);

bool vnodeParametersSafetyCheck(SQuery* pQuery);

H
hjxilinx 已提交
279
int32_t vnodeSTableQueryPrepare(SQInfo* pQInfo, SQuery* pQuery, void* param);
H
hzcheng 已提交
280 281 282 283 284 285 286 287 288

/**
 * decrease the numofQuery of each table that is queried, enable the
 * remove/close operation can be executed
 * @param pQInfo
 */
void vnodeDecMeterRefcnt(SQInfo* pQInfo);

/* sql query handle in dnode */
H
hjxilinx 已提交
289
void vnodeSingleTableQuery(SSchedMsg* pMsg);
H
hzcheng 已提交
290 291 292 293 294 295 296 297 298 299 300

/*
 * handle multi-meter query process
 */
void vnodeMultiMeterQuery(SSchedMsg* pMsg);

#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_VNODEREAD_H