dataDispatcher.c 9.2 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;
}

H
Haojun Liao 已提交
70
// data format:
71 72 73 74
// +----------------+--------------+----------+--------------------------------------+-------------+-----------+-------------+-----------+
// |SDataCacheEntry | total length | group id | column#1 length, column#2 length ... | col1 bitmap | col1 data | col2 bitmap | col2 data | ....
// |                |  (4 bytes)   |(8 bytes) | sizeof(int32_t) * numOfCols          | actual size |           | actual size |           |
// +----------------+--------------+----------+--------------------------------------+-------------+-----------+-------------+-----------+
H
Haojun Liao 已提交
75 76
// 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
D
dapan1121 已提交
77
static void toDataCacheEntry(SDataDispatchHandle* pHandle, const SInputData* pInput, SDataDispatchBuf* pBuf) {
L
Liu Jicong 已提交
78 79
  int32_t numOfCols = LIST_LENGTH(pHandle->pSchema->pSlots);

80
  SDataCacheEntry* pEntry = (SDataCacheEntry*)pBuf->pData;
L
Liu Jicong 已提交
81 82
  pEntry->compressed = (int8_t)needCompress(pInput->pData, numOfCols);
  pEntry->numOfRows = pInput->pData->info.rows;
83
  pEntry->numOfCols = pInput->pData->info.numOfCols;
L
Liu Jicong 已提交
84
  pEntry->dataLen = 0;
X
Xiaoyu Wang 已提交
85

D
dapan1121 已提交
86
  pBuf->useSize = sizeof(SDataCacheEntry);
L
Liu Jicong 已提交
87
  blockCompressEncode(pInput->pData, pEntry->data, &pEntry->dataLen, numOfCols, pEntry->compressed);
88

L
Liu Jicong 已提交
89
  pBuf->useSize += pEntry->dataLen;
D
dapan1121 已提交
90 91 92
  
  atomic_add_fetch_64(&pHandle->cachedSize, pEntry->dataLen); 
  atomic_add_fetch_64(&gDataSinkStat.cachedSize, pEntry->dataLen); 
X
Xiaoyu Wang 已提交
93 94
}

95
static bool allocBuf(SDataDispatchHandle* pDispatcher, const SInputData* pInput, SDataDispatchBuf* pBuf) {
H
Haojun Liao 已提交
96
  uint32_t capacity = pDispatcher->pManager->cfg.maxDataBlockNumPerQuery;
97
  if (taosQueueItemSize(pDispatcher->pDataBlocks) > capacity) {
H
Haojun Liao 已提交
98
    qError("SinkNode queue is full, no capacity, max:%d, current:%d, no capacity", capacity,
99
           taosQueueItemSize(pDispatcher->pDataBlocks));
100 101
    return false;
  }
H
Haojun Liao 已提交
102

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

wafwerar's avatar
wafwerar 已提交
105
  pBuf->pData = taosMemoryMalloc(pBuf->allocSize);
H
Haojun Liao 已提交
106 107 108 109
  if (pBuf->pData == NULL) {
    qError("SinkNode failed to malloc memory, size:%d, code:%d", pBuf->allocSize, TAOS_SYSTEM_ERROR(errno));
  }

110 111 112 113
  return NULL != pBuf->pData;
}

static int32_t updateStatus(SDataDispatchHandle* pDispatcher) {
wafwerar's avatar
wafwerar 已提交
114
  taosThreadMutexLock(&pDispatcher->mutex);
115
  int32_t blockNums = taosQueueItemSize(pDispatcher->pDataBlocks);
L
Liu Jicong 已提交
116 117 118
  int32_t status =
      (0 == blockNums ? DS_BUF_EMPTY
                      : (blockNums < pDispatcher->pManager->cfg.maxDataBlockNumPerQuery ? DS_BUF_LOW : DS_BUF_FULL));
119
  pDispatcher->status = status;
wafwerar's avatar
wafwerar 已提交
120
  taosThreadMutexUnlock(&pDispatcher->mutex);
121 122 123 124
  return status;
}

static int32_t getStatus(SDataDispatchHandle* pDispatcher) {
wafwerar's avatar
wafwerar 已提交
125
  taosThreadMutexLock(&pDispatcher->mutex);
126
  int32_t status = pDispatcher->status;
wafwerar's avatar
wafwerar 已提交
127
  taosThreadMutexUnlock(&pDispatcher->mutex);
128 129 130
  return status;
}

X
Xiaoyu Wang 已提交
131
static int32_t putDataBlock(SDataSinkHandle* pHandle, const SInputData* pInput, bool* pContinue) {
X
Xiaoyu Wang 已提交
132
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
133
  SDataDispatchBuf*    pBuf = taosAllocateQitem(sizeof(SDataDispatchBuf), DEF_QITEM);
134 135 136 137 138
  if (NULL == pBuf || !allocBuf(pDispatcher, pInput, pBuf)) {
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
  toDataCacheEntry(pDispatcher, pInput, pBuf);
  taosWriteQitem(pDispatcher->pDataBlocks, pBuf);
X
Xiaoyu Wang 已提交
139
  *pContinue = (DS_BUF_LOW == updateStatus(pDispatcher) ? true : false);
140
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
141 142
}

D
dapan1121 已提交
143
static void endPut(struct SDataSinkHandle* pHandle, uint64_t useconds) {
144
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
wafwerar's avatar
wafwerar 已提交
145
  taosThreadMutexLock(&pDispatcher->mutex);
X
Xiaoyu Wang 已提交
146 147
  pDispatcher->queryEnd = true;
  pDispatcher->useconds = useconds;
wafwerar's avatar
wafwerar 已提交
148
  taosThreadMutexUnlock(&pDispatcher->mutex);
149
}
X
Xiaoyu Wang 已提交
150

X
Xiaoyu Wang 已提交
151
static void getDataLength(SDataSinkHandle* pHandle, int32_t* pLen, bool* pQueryEnd) {
152 153
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
  if (taosQueueEmpty(pDispatcher->pDataBlocks)) {
X
Xiaoyu Wang 已提交
154 155 156
    *pQueryEnd = pDispatcher->queryEnd;
    *pLen = 0;
    return;
157
  }
158

159 160 161 162
  SDataDispatchBuf* pBuf = NULL;
  taosReadQitem(pDispatcher->pDataBlocks, (void**)&pBuf);
  memcpy(&pDispatcher->nextOutput, pBuf, sizeof(SDataDispatchBuf));
  taosFreeQitem(pBuf);
X
Xiaoyu Wang 已提交
163
  *pLen = ((SDataCacheEntry*)(pDispatcher->nextOutput.pData))->dataLen;
L
Liu Jicong 已提交
164
  *pQueryEnd = pDispatcher->queryEnd;
D
dapan1121 已提交
165
  qDebug("got data len %d, row num %d in sink", *pLen, ((SDataCacheEntry*)(pDispatcher->nextOutput.pData))->numOfRows);
X
Xiaoyu Wang 已提交
166 167
}

X
Xiaoyu Wang 已提交
168
static int32_t getDataBlock(SDataSinkHandle* pHandle, SOutputData* pOutput) {
169
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
170 171 172
  if (NULL == pDispatcher->nextOutput.pData) {
    assert(pDispatcher->queryEnd);
    pOutput->useconds = pDispatcher->useconds;
X
Xiaoyu Wang 已提交
173
    pOutput->precision = pDispatcher->pSchema->precision;
D
dapan1121 已提交
174 175
    pOutput->bufStatus = DS_BUF_EMPTY;
    pOutput->queryEnd = pDispatcher->queryEnd;
176 177
    return TSDB_CODE_SUCCESS;
  }
178 179 180
  SDataCacheEntry* pEntry = (SDataCacheEntry*)(pDispatcher->nextOutput.pData);
  memcpy(pOutput->pData, pEntry->data, pEntry->dataLen);
  pOutput->numOfRows = pEntry->numOfRows;
181
  pOutput->numOfCols = pEntry->numOfCols;
182
  pOutput->compressed = pEntry->compressed;
D
dapan1121 已提交
183 184 185 186

  atomic_sub_fetch_64(&pDispatcher->cachedSize, pEntry->dataLen);  
  atomic_sub_fetch_64(&gDataSinkStat.cachedSize, pEntry->dataLen); 

wafwerar's avatar
wafwerar 已提交
187
  taosMemoryFreeClear(pDispatcher->nextOutput.pData);  // todo persistent
X
Xiaoyu Wang 已提交
188
  pOutput->bufStatus = updateStatus(pDispatcher);
wafwerar's avatar
wafwerar 已提交
189
  taosThreadMutexLock(&pDispatcher->mutex);
X
Xiaoyu Wang 已提交
190 191
  pOutput->queryEnd = pDispatcher->queryEnd;
  pOutput->useconds = pDispatcher->useconds;
X
Xiaoyu Wang 已提交
192
  pOutput->precision = pDispatcher->pSchema->precision;
wafwerar's avatar
wafwerar 已提交
193
  taosThreadMutexUnlock(&pDispatcher->mutex);
D
dapan1121 已提交
194 195

  
196 197
  return TSDB_CODE_SUCCESS;
}
X
Xiaoyu Wang 已提交
198

199 200
static int32_t destroyDataSinker(SDataSinkHandle* pHandle) {
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;
D
dapan1121 已提交
201
  atomic_sub_fetch_64(&gDataSinkStat.cachedSize, pDispatcher->cachedSize);
wafwerar's avatar
wafwerar 已提交
202
  taosMemoryFreeClear(pDispatcher->nextOutput.pData);
203 204 205
  while (!taosQueueEmpty(pDispatcher->pDataBlocks)) {
    SDataDispatchBuf* pBuf = NULL;
    taosReadQitem(pDispatcher->pDataBlocks, (void**)&pBuf);
wafwerar's avatar
wafwerar 已提交
206
    taosMemoryFreeClear(pBuf->pData);
207 208 209
    taosFreeQitem(pBuf);
  }
  taosCloseQueue(pDispatcher->pDataBlocks);
wafwerar's avatar
wafwerar 已提交
210
  taosThreadMutexDestroy(&pDispatcher->mutex);
211
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
212 213
}

D
dapan1121 已提交
214
static int32_t getCacheSize(struct SDataSinkHandle* pHandle, uint64_t* size) {
D
dapan1121 已提交
215 216 217 218 219 220
  SDataDispatchHandle* pDispatcher = (SDataDispatchHandle*)pHandle;

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

X
Xiaoyu Wang 已提交
221
int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSinkNode* pDataSink, DataSinkHandle* pHandle) {
wafwerar's avatar
wafwerar 已提交
222
  SDataDispatchHandle* dispatcher = taosMemoryCalloc(1, sizeof(SDataDispatchHandle));
X
Xiaoyu Wang 已提交
223 224
  if (NULL == dispatcher) {
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
225
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
226 227
  }
  dispatcher->sink.fPut = putDataBlock;
D
dapan1121 已提交
228
  dispatcher->sink.fEndPut = endPut;
229 230
  dispatcher->sink.fGetLen = getDataLength;
  dispatcher->sink.fGetData = getDataBlock;
X
Xiaoyu Wang 已提交
231
  dispatcher->sink.fDestroy = destroyDataSinker;
D
dapan1121 已提交
232
  dispatcher->sink.fGetCacheSize = getCacheSize;
233
  dispatcher->pManager = pManager;
X
Xiaoyu Wang 已提交
234
  dispatcher->pSchema = pDataSink->pInputDataBlockDesc;
X
Xiaoyu Wang 已提交
235 236
  dispatcher->status = DS_BUF_EMPTY;
  dispatcher->queryEnd = false;
X
Xiaoyu Wang 已提交
237
  dispatcher->pDataBlocks = taosOpenQueue();
wafwerar's avatar
wafwerar 已提交
238
  taosThreadMutexInit(&dispatcher->mutex, NULL);
X
Xiaoyu Wang 已提交
239 240
  if (NULL == dispatcher->pDataBlocks) {
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
241
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
242 243 244 245
  }
  *pHandle = dispatcher;
  return TSDB_CODE_SUCCESS;
}