tsdbWrite.c 6.8 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "tsdb.h"
H
Hongze Cheng 已提交
17

18 19 20 21 22 23 24 25 26
/**
 * @brief max key by precision
 *  approximately calculation:
 *  ms: 3600*1000*8765*1000         // 1970 + 1000 years
 *  us: 3600*1000000*8765*1000      // 1970 + 1000 years
 *  ns: 3600*1000000000*8765*292    // 1970 + 292 years
 */
static int64_t tsMaxKeyByPrecision[] = {31556995200000L, 31556995200000000L, 9214646400000000000L};

C
Cary Xu 已提交
27
// static int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq *pMsg);
H
Hongze Cheng 已提交
28

29 30 31 32
int tsdbInsertData(STsdb *pTsdb, int64_t version, SSubmitReq2 *pMsg, SSubmitRsp2 *pRsp) {
  int32_t arrSize = 0;
  int32_t affectedrows = 0;
  int32_t numOfRows = 0;
H
Hongze Cheng 已提交
33 34 35

  ASSERT(pTsdb->mem != NULL);

36 37 38 39
  if (pMsg) {
    arrSize = taosArrayGetSize(pMsg->aSubmitTbData);
  }

H
Hongze Cheng 已提交
40 41 42
  // scan and convert
  if (tsdbScanAndConvertSubmitMsg(pTsdb, pMsg) < 0) {
    if (terrno != TSDB_CODE_TDB_TABLE_RECONFIGURE) {
H
Hongze Cheng 已提交
43
      tsdbError("vgId:%d, failed to insert data since %s", TD_VID(pTsdb->pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
44 45 46 47 48
    }
    return -1;
  }

  // loop to insert
49 50
  for (int32_t i = 0; i < arrSize; ++i) {
    if ((terrno = tsdbInsertTableData(pTsdb, version, taosArrayGet(pMsg->aSubmitTbData, i), &affectedrows)) < 0) {
H
Hongze Cheng 已提交
51 52 53 54 55
      return -1;
    }
  }

  if (pRsp != NULL) {
H
Hongze Cheng 已提交
56 57
    // pRsp->affectedRows = affectedrows;
    // pRsp->numOfRows = numOfRows;
H
Hongze Cheng 已提交
58 59 60 61 62
  }

  return 0;
}

C
Cary Xu 已提交
63 64 65 66 67
#if 0
static FORCE_INLINE int tsdbCheckRowRange(STsdb *pTsdb, STable *pTable, STSRow *row, TSKEY minKey, TSKEY maxKey,
                                          TSKEY now) {
  TSKEY rowKey = TD_ROW_KEY(row);
  if (rowKey < minKey || rowKey > maxKey) {
S
Shengliang Guan 已提交
68
    tsdbError("vgId:%d, table %s tid %d uid %" PRIu64 " timestamp is out of range! now %" PRId64 " minKey %" PRId64
C
Cary Xu 已提交
69 70 71 72 73 74 75 76 77 78 79
              " maxKey %" PRId64 " row key %" PRId64,
              REPO_ID(pTsdb), TABLE_CHAR_NAME(pTable), TABLE_TID(pTable), TABLE_UID(pTable), now, minKey, maxKey,
              rowKey);
    terrno = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
    return -1;
  }

  return 0;
}
#endif

80
static FORCE_INLINE int tsdbCheckRowRange(STsdb *pTsdb, tb_uid_t uid, TSKEY rowKey, TSKEY minKey, TSKEY maxKey,
C
Cary Xu 已提交
81 82
                                          TSKEY now) {
  if (rowKey < minKey || rowKey > maxKey) {
S
Shengliang Guan 已提交
83
    tsdbError("vgId:%d, table uid %" PRIu64 " timestamp is out of range! now %" PRId64 " minKey %" PRId64
C
Cary Xu 已提交
84
              " maxKey %" PRId64 " row key %" PRId64,
H
Hongze Cheng 已提交
85
              TD_VID(pTsdb->pVnode), uid, now, minKey, maxKey, rowKey);
C
Cary Xu 已提交
86 87 88 89 90 91 92
    terrno = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
    return -1;
  }

  return 0;
}

93
#if 0
H
Hongze Cheng 已提交
94
int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq *pMsg) {
H
Hongze Cheng 已提交
95 96 97 98 99 100
  ASSERT(pMsg != NULL);
  // STsdbMeta *    pMeta = pTsdb->tsdbMeta;
  SSubmitMsgIter msgIter = {0};
  SSubmitBlk    *pBlock = NULL;
  SSubmitBlkIter blkIter = {0};
  STSRow        *row = NULL;
H
Hongze Cheng 已提交
101
  STsdbKeepCfg  *pCfg = &pTsdb->keepCfg;
H
Hongze Cheng 已提交
102
  TSKEY          now = taosGetTimestamp(pCfg->precision);
103
  TSKEY          minKey = now - tsTickPerMin[pCfg->precision] * pCfg->keep2;
104
  TSKEY          maxKey = tsMaxKeyByPrecision[pCfg->precision];
H
Hongze Cheng 已提交
105 106

  terrno = TSDB_CODE_SUCCESS;
C
Cary Xu 已提交
107 108
  // pMsg->length = htonl(pMsg->length);
  // pMsg->numOfBlocks = htonl(pMsg->numOfBlocks);
H
Hongze Cheng 已提交
109 110 111 112 113 114

  if (tInitSubmitMsgIter(pMsg, &msgIter) < 0) return -1;
  while (true) {
    if (tGetSubmitMsgNext(&msgIter, &pBlock) < 0) return -1;
    if (pBlock == NULL) break;

C
Cary Xu 已提交
115 116 117 118 119
      // pBlock->uid = htobe64(pBlock->uid);
      // pBlock->suid = htobe64(pBlock->suid);
      // pBlock->sversion = htonl(pBlock->sversion);
      // pBlock->dataLen = htonl(pBlock->dataLen);
      // pBlock->schemaLen = htonl(pBlock->schemaLen);
120
      // pBlock->numOfRows = htonl(pBlock->numOfRows);
H
Hongze Cheng 已提交
121 122 123

#if 0
    if (pBlock->tid <= 0 || pBlock->tid >= pMeta->maxTables) {
S
Shengliang Guan 已提交
124
      tsdbError("vgId:%d, failed to get table to insert data, uid %" PRIu64 " tid %d", REPO_ID(pTsdb), pBlock->uid,
H
Hongze Cheng 已提交
125 126 127 128 129 130 131
                pBlock->tid);
      terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
      return -1;
    }

    STable *pTable = pMeta->tables[pBlock->tid];
    if (pTable == NULL || TABLE_UID(pTable) != pBlock->uid) {
S
Shengliang Guan 已提交
132
      tsdbError("vgId:%d, failed to get table to insert data, uid %" PRIu64 " tid %d", REPO_ID(pTsdb), pBlock->uid,
H
Hongze Cheng 已提交
133 134 135 136 137 138
                pBlock->tid);
      terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
      return -1;
    }

    if (TABLE_TYPE(pTable) == TSDB_SUPER_TABLE) {
S
Shengliang Guan 已提交
139
      tsdbError("vgId:%d, invalid action trying to insert a super table %s", REPO_ID(pTsdb), TABLE_CHAR_NAME(pTable));
H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148 149 150 151
      terrno = TSDB_CODE_TDB_INVALID_ACTION;
      return -1;
    }

    // Check schema version and update schema if needed
    if (tsdbCheckTableSchema(pTsdb, pBlock, pTable) < 0) {
      if (terrno == TSDB_CODE_TDB_TABLE_RECONFIGURE) {
        continue;
      } else {
        return -1;
      }
    }
C
Cary Xu 已提交
152 153 154 155
#endif
    tInitSubmitBlkIter(&msgIter, pBlock, &blkIter);
    while ((row = tGetSubmitBlkNext(&blkIter)) != NULL) {
      if (tsdbCheckRowRange(pTsdb, msgIter.uid, row, minKey, maxKey, now) < 0) {
H
Hongze Cheng 已提交
156 157 158 159 160
        return -1;
      }
    }
  }

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  if (terrno != TSDB_CODE_SUCCESS) return -1;
  return 0;
}
#endif

int tsdbScanAndConvertSubmitMsg(STsdb *pTsdb, SSubmitReq2 *pMsg) {
  ASSERT(pMsg != NULL);
  STsdbKeepCfg *pCfg = &pTsdb->keepCfg;
  TSKEY         now = taosGetTimestamp(pCfg->precision);
  TSKEY         minKey = now - tsTickPerMin[pCfg->precision] * pCfg->keep2;
  TSKEY         maxKey = tsMaxKeyByPrecision[pCfg->precision];
  int32_t       size = taosArrayGetSize(pMsg->aSubmitTbData);

  terrno = TSDB_CODE_SUCCESS;

  for (int32_t i = 0; i < size; ++i) {
    SSubmitTbData *pData = TARRAY_GET_ELEM(pMsg->aSubmitTbData, i);
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
      uint64_t  nColData = TARRAY_SIZE(pData->aCol);
      SColData *aColData = (SColData *)TARRAY_DATA(pData->aCol);
      if (nColData > 0) {
        int32_t nRows = aColData[0].nVal;
        TSKEY  *aKey = (TSKEY *)aColData[0].pData;
        for (int32_t r = 0; r < nRows; ++r) {
          if (tsdbCheckRowRange(pTsdb, pData->uid, aKey[r], minKey, maxKey, now) < 0) {
            return -1;
          }
        }
      }
    } else {
      int32_t nRows = taosArrayGetSize(pData->aRowP);
      for (int32_t r = 0; r < nRows; ++r) {
        SRow *pRow = (SRow *)taosArrayGetP(pData->aRowP, r);
        if (tsdbCheckRowRange(pTsdb, pData->uid, pRow->ts, minKey, maxKey, now) < 0) {
          return -1;
        }
      }
    }
  }

H
Hongze Cheng 已提交
201 202
  if (terrno != TSDB_CODE_SUCCESS) return -1;
  return 0;
C
Cary Xu 已提交
203
}