dataDispatcher.c 10.3 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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 "dataSinkInt.h"
#include "dataSinkMgt.h"
18
#include "executorimpl.h"
X
Xiaoyu Wang 已提交
19 20
#include "planner.h"
#include "tcompression.h"
L
Liu Jicong 已提交
21
#include "tdatablock.h"
X
Xiaoyu Wang 已提交
22 23 24
#include "tglobal.h"
#include "tqueue.h"

D
dapan1121 已提交
25 26
extern SDataSinkStat gDataSinkStat;

27 28 29
typedef struct SDataDispatchBuf {
  int32_t useSize;
  int32_t allocSize;
L
Liu Jicong 已提交
30
  char*   pData;
31 32 33 34 35
} SDataDispatchBuf;

typedef struct SDataCacheEntry {
  int32_t dataLen;
  int32_t numOfRows;
36
  int32_t numOfCols;
37 38 39
  int8_t  compressed;
  char    data[];
} SDataCacheEntry;
X
Xiaoyu Wang 已提交
40 41

typedef struct SDataDispatchHandle {
L
Liu Jicong 已提交
42 43
  SDataSinkHandle     sink;
  SDataSinkManager*   pManager;
X
Xiaoyu Wang 已提交
44
  SDataBlockDescNode* pSchema;
L
Liu Jicong 已提交
45 46 47 48 49
  STaosQueue*         pDataBlocks;
  SDataDispatchBuf    nextOutput;
  int32_t             status;
  bool                queryEnd;
  uint64_t            useconds;
D
dapan1121 已提交
50
  uint64_t            cachedSize;
L
Liu Jicong 已提交
51
  TdThreadMutex       mutex;
X
Xiaoyu Wang 已提交
52 53
} SDataDispatchHandle;

L
Liu Jicong 已提交
54
static bool needCompress(const SSDataBlock* pData, int32_t numOfCols) {
X
Xiaoyu Wang 已提交
55 56 57 58
  if (tsCompressColData < 0 || 0 == pData->info.rows) {
    return false;
  }

X
Xiaoyu Wang 已提交
59
  for (int32_t col = 0; col < numOfCols; ++col) {
X
Xiaoyu Wang 已提交
60
    SColumnInfoData* pColRes = taosArrayGet(pData->pDataBlock, col);
L
Liu Jicong 已提交
61
    int32_t          colSize = pColRes->info.bytes * pData->info.rows;
X
Xiaoyu Wang 已提交
62 63 64 65 66 67 68 69
    if (NEEDTO_COMPRESS_QUERY(colSize)) {
      return true;
    }
  }

  return false;
}

70
// clang-format off
H
Haojun Liao 已提交
71
// data format:
H
Haojun Liao 已提交
72 73 74 75
// +----------------+------------------+--------------+--------------+------------------+--------------------------------------------+------------------------------------+-------------+-----------+-------------+-----------+
// |SDataCacheEntry |  version         | total length | numOfRows    |     group id     | col1_schema | col2_schema | col3_schema... | column#1 length, column#2 length...| col1 bitmap | col1 data | col2 bitmap | col2 data | .... |                |  (4 bytes)   |(8 bytes)
// |                |  sizeof(int32_t) |sizeof(int32) | sizeof(int32)| sizeof(uint64_t) | (sizeof(int8_t)+sizeof(int32_t))*numOfCols | sizeof(int32_t) * numOfCols        | actual size |           |
// +----------------+------------------+--------------+--------------+------------------+--------------------------------------------+------------------------------------+-------------+-----------+-------------+-----------+
H
Haojun Liao 已提交
76 77
// The length of bitmap is decided by number of rows of this data block, and the length of each column data is
// recorded in the first segment, next to the struct header
78
// clang-format on
D
dapan1121 已提交
79
static void toDataCacheEntry(SDataDispatchHandle* pHandle, const SInputData* pInput, SDataDispatchBuf* pBuf) {
80
  int32_t numOfCols = 0;
81
  SNode*  pNode;
82 83 84 85 86 87
  FOREACH(pNode, pHandle->pSchema->pSlots) {
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
    if (pSlotDesc->output) {
      ++numOfCols;
    }
  }
88
  SDataCacheEntry* pEntry = (SDataCacheEntry*)pBuf->pData;
L
Liu Jicong 已提交
89 90
  pEntry->compressed = (int8_t)needCompress(pInput->pData, numOfCols);
  pEntry->numOfRows = pInput->pData->info.rows;
S
shenglian zhou 已提交
91
  pEntry->numOfCols = numOfCols;
L
Liu Jicong 已提交
92
  pEntry->dataLen = 0;
X
Xiaoyu Wang 已提交
93

D
dapan1121 已提交
94
  pBuf->useSize = sizeof(SDataCacheEntry);
95
  blockEncode(pInput->pData, pEntry->data, &pEntry->dataLen, numOfCols, pEntry->compressed);
H
Hongze Cheng 已提交
96 97
  ASSERT(pEntry->numOfRows == *(int32_t*)(pEntry->data + 8));
  ASSERT(pEntry->numOfCols == *(int32_t*)(pEntry->data + 8 + 4));
98

L
Liu Jicong 已提交
99
  pBuf->useSize += pEntry->dataLen;
100 101 102

  atomic_add_fetch_64(&pHandle->cachedSize, pEntry->dataLen);
  atomic_add_fetch_64(&gDataSinkStat.cachedSize, pEntry->dataLen);
X
Xiaoyu Wang 已提交
103 104
}

105
static bool allocBuf(SDataDispatchHandle* pDispatcher, const SInputData* pInput, SDataDispatchBuf* pBuf) {
H
Hongze Cheng 已提交
106 107 108 109 110 111 112 113
  /*
    uint32_t capacity = pDispatcher->pManager->cfg.maxDataBlockNumPerQuery;
    if (taosQueueItemSize(pDispatcher->pDataBlocks) > capacity) {
      qError("SinkNode queue is full, no capacity, max:%d, current:%d, no capacity", capacity,
             taosQueueItemSize(pDispatcher->pDataBlocks));
      return false;
    }
  */
H
Haojun Liao 已提交
114

D
dapan1121 已提交
115
  pBuf->allocSize = sizeof(SDataCacheEntry) + blockGetEncodeSize(pInput->pData);
H
Haojun Liao 已提交
116

wafwerar's avatar
wafwerar 已提交
117
  pBuf->pData = taosMemoryMalloc(pBuf->allocSize);
H
Haojun Liao 已提交
118 119 120 121
  if (pBuf->pData == NULL) {
    qError("SinkNode failed to malloc memory, size:%d, code:%d", pBuf->allocSize, TAOS_SYSTEM_ERROR(errno));
  }

122 123 124 125
  return NULL != pBuf->pData;
}

static int32_t updateStatus(SDataDispatchHandle* pDispatcher) {
wafwerar's avatar
wafwerar 已提交
126
  taosThreadMutexLock(&pDispatcher->mutex);
127
  int32_t blockNums = taosQueueItemSize(pDispatcher->pDataBlocks);
L
Liu Jicong 已提交
128 129 130
  int32_t status =
      (0 == blockNums ? DS_BUF_EMPTY
                      : (blockNums < pDispatcher->pManager->cfg.maxDataBlockNumPerQuery ? DS_BUF_LOW : DS_BUF_FULL));
131
  pDispatcher->status = status;
wafwerar's avatar
wafwerar 已提交
132
  taosThreadMutexUnlock(&pDispatcher->mutex);
133 134 135 136
  return status;
}

static int32_t getStatus(SDataDispatchHandle* pDispatcher) {
wafwerar's avatar
wafwerar 已提交
137
  taosThreadMutexLock(&pDispatcher->mutex);
138
  int32_t status = pDispatcher->status;
wafwerar's avatar
wafwerar 已提交
139
  taosThreadMutexUnlock(&pDispatcher->mutex);
140 141 142
  return status;
}

X
Xiaoyu Wang 已提交
143
static int32_t putDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue) {
X
Xiaoyu Wang 已提交
144
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
145
  SDataDispatchBuf*    pBuf = taosAllocateQitem(sizeof(SDataDispatchBuf), DEF_QITEM);
D
dapan1121 已提交
146
  if (NULL == pBuf) {
147 148
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
D
dapan1121 已提交
149 150 151 152 153 154

  if (!allocBuf(pDispatcher, pInput, pBuf)) {
    taosFreeQitem(pBuf);
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
  
155 156
  toDataCacheEntry(pDispatcher, pInput, pBuf);
  taosWriteQitem(pDispatcher->pDataBlocks, pBuf);
X
Xiaoyu Wang 已提交
157
  *pContinue = (DS_BUF_LOW == updateStatus(pDispatcher) ? true : false);
158
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
159 160
}

D
dapan1121 已提交
161
static void endPut(struct SDataSinkHandle* pHandle, uint64_t useconds) {
162
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
wafwerar's avatar
wafwerar 已提交
163
  taosThreadMutexLock(&pDispatcher->mutex);
X
Xiaoyu Wang 已提交
164 165
  pDispatcher->queryEnd = true;
  pDispatcher->useconds = useconds;
wafwerar's avatar
wafwerar 已提交
166
  taosThreadMutexUnlock(&pDispatcher->mutex);
167
}
X
Xiaoyu Wang 已提交
168

D
dapan1121 已提交
169
static void getDataLength(SDataSinkHandle* pHandle, int64_t* pLen, bool* pQueryEnd) {
170 171
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
  if (taosQueueEmpty(pDispatcher->pDataBlocks)) {
X
Xiaoyu Wang 已提交
172 173 174
    *pQueryEnd = pDispatcher->queryEnd;
    *pLen = 0;
    return;
175
  }
176

177 178
  SDataDispatchBuf* pBuf = NULL;
  taosReadQitem(pDispatcher->pDataBlocks, (void**)&pBuf);
X
Xiaoyu Wang 已提交
179
  ASSERT(NULL != pBuf);
180 181
  memcpy(&pDispatcher->nextOutput, pBuf, sizeof(SDataDispatchBuf));
  taosFreeQitem(pBuf);
H
Haojun Liao 已提交
182 183 184 185

  SDataCacheEntry* pEntry = (SDataCacheEntry*)pDispatcher->nextOutput.pData;
  *pLen = pEntry->dataLen;

H
Hongze Cheng 已提交
186 187
  ASSERT(pEntry->numOfRows == *(int32_t*)(pEntry->data + 8));
  ASSERT(pEntry->numOfCols == *(int32_t*)(pEntry->data + 8 + 4));
H
Haojun Liao 已提交
188

L
Liu Jicong 已提交
189
  *pQueryEnd = pDispatcher->queryEnd;
H
Hongze Cheng 已提交
190 191
  qDebug("got data len %" PRId64 ", row num %d in sink", *pLen,
         ((SDataCacheEntry*)(pDispatcher->nextOutput.pData))->numOfRows);
X
Xiaoyu Wang 已提交
192 193
}

X
Xiaoyu Wang 已提交
194
static int32_t getDataBlock(SDataSinkHandle* pHandle, SOutputData* pOutput) {
195
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
196 197 198
  if (NULL == pDispatcher->nextOutput.pData) {
    assert(pDispatcher->queryEnd);
    pOutput->useconds = pDispatcher->useconds;
X
Xiaoyu Wang 已提交
199
    pOutput->precision = pDispatcher->pSchema->precision;
D
dapan1121 已提交
200 201
    pOutput->bufStatus = DS_BUF_EMPTY;
    pOutput->queryEnd = pDispatcher->queryEnd;
202 203
    return TSDB_CODE_SUCCESS;
  }
204 205 206
  SDataCacheEntry* pEntry = (SDataCacheEntry*)(pDispatcher->nextOutput.pData);
  memcpy(pOutput->pData, pEntry->data, pEntry->dataLen);
  pOutput->numOfRows = pEntry->numOfRows;
207
  pOutput->numOfCols = pEntry->numOfCols;
208
  pOutput->compressed = pEntry->compressed;
D
dapan1121 已提交
209

H
Hongze Cheng 已提交
210 211
  ASSERT(pEntry->numOfRows == *(int32_t*)(pEntry->data + 8));
  ASSERT(pEntry->numOfCols == *(int32_t*)(pEntry->data + 8 + 4));
H
Haojun Liao 已提交
212

213 214
  atomic_sub_fetch_64(&pDispatcher->cachedSize, pEntry->dataLen);
  atomic_sub_fetch_64(&gDataSinkStat.cachedSize, pEntry->dataLen);
D
dapan1121 已提交
215

wafwerar's avatar
wafwerar 已提交
216
  taosMemoryFreeClear(pDispatcher->nextOutput.pData);  // todo persistent
X
Xiaoyu Wang 已提交
217
  pOutput->bufStatus = updateStatus(pDispatcher);
wafwerar's avatar
wafwerar 已提交
218
  taosThreadMutexLock(&pDispatcher->mutex);
X
Xiaoyu Wang 已提交
219 220
  pOutput->queryEnd = pDispatcher->queryEnd;
  pOutput->useconds = pDispatcher->useconds;
X
Xiaoyu Wang 已提交
221
  pOutput->precision = pDispatcher->pSchema->precision;
wafwerar's avatar
wafwerar 已提交
222
  taosThreadMutexUnlock(&pDispatcher->mutex);
D
dapan1121 已提交
223

224 225
  return TSDB_CODE_SUCCESS;
}
X
Xiaoyu Wang 已提交
226

227 228
static int32_t destroyDataSinker(SDataSinkHandle* pHandle) {
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
D
dapan1121 已提交
229
  atomic_sub_fetch_64(&gDataSinkStat.cachedSize, pDispatcher->cachedSize);
wafwerar's avatar
wafwerar 已提交
230
  taosMemoryFreeClear(pDispatcher->nextOutput.pData);
231 232 233
  while (!taosQueueEmpty(pDispatcher->pDataBlocks)) {
    SDataDispatchBuf* pBuf = NULL;
    taosReadQitem(pDispatcher->pDataBlocks, (void**)&pBuf);
wafwerar's avatar
wafwerar 已提交
234
    taosMemoryFreeClear(pBuf->pData);
235 236 237
    taosFreeQitem(pBuf);
  }
  taosCloseQueue(pDispatcher->pDataBlocks);
wafwerar's avatar
wafwerar 已提交
238
  taosThreadMutexDestroy(&pDispatcher->mutex);
239
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
240 241
}

D
dapan1121 已提交
242
static int32_t getCacheSize(struct SDataSinkHandle* pHandle, uint64_t* size) {
D
dapan1121 已提交
243 244 245 246 247 248
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;

  *size = atomic_load_64(&pDispatcher->cachedSize);
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
249
int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSinkNode* pDataSink, DataSinkHandle* pHandle) {
wafwerar's avatar
wafwerar 已提交
250
  SDataDispatchHandle* dispatcher = taosMemoryCalloc(1, sizeof(SDataDispatchHandle));
X
Xiaoyu Wang 已提交
251 252
  if (NULL == dispatcher) {
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
253
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
254 255
  }
  dispatcher->sink.fPut = putDataBlock;
D
dapan1121 已提交
256
  dispatcher->sink.fEndPut = endPut;
257 258
  dispatcher->sink.fGetLen = getDataLength;
  dispatcher->sink.fGetData = getDataBlock;
X
Xiaoyu Wang 已提交
259
  dispatcher->sink.fDestroy = destroyDataSinker;
D
dapan1121 已提交
260
  dispatcher->sink.fGetCacheSize = getCacheSize;
261
  dispatcher->pManager = pManager;
X
Xiaoyu Wang 已提交
262
  dispatcher->pSchema = pDataSink->pInputDataBlockDesc;
X
Xiaoyu Wang 已提交
263 264
  dispatcher->status = DS_BUF_EMPTY;
  dispatcher->queryEnd = false;
X
Xiaoyu Wang 已提交
265
  dispatcher->pDataBlocks = taosOpenQueue();
wafwerar's avatar
wafwerar 已提交
266
  taosThreadMutexInit(&dispatcher->mutex, NULL);
X
Xiaoyu Wang 已提交
267
  if (NULL == dispatcher->pDataBlocks) {
X
Xiaoyu Wang 已提交
268
    taosMemoryFree(dispatcher);
X
Xiaoyu Wang 已提交
269
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
270
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
271 272 273 274
  }
  *pHandle = dispatcher;
  return TSDB_CODE_SUCCESS;
}