tstreamUpdate.c 5.3 KB
Newer Older
R
root 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 "tstreamUpdate.h"
#include "ttime.h"

#define DEFAULT_FALSE_POSITIVE 0.01
L
Liu Jicong 已提交
20 21 22 23 24 25 26
#define DEFAULT_BUCKET_SIZE    1024
#define ROWS_PER_MILLISECOND   1
#define MAX_NUM_SCALABLE_BF    120
#define MIN_NUM_SCALABLE_BF    10
#define DEFAULT_PREADD_BUCKET  1
#define MAX_INTERVAL           MILLISECOND_PER_MINUTE
#define MIN_INTERVAL           (MILLISECOND_PER_SECOND * 10)
R
root 已提交
27 28

static void windowSBfAdd(SUpdateInfo *pInfo, uint64_t count) {
L
Liu Jicong 已提交
29
  if (pInfo->numSBFs < count) {
R
root 已提交
30 31 32
    count = pInfo->numSBFs;
  }
  for (uint64_t i = 0; i < count; ++i) {
L
Liu Jicong 已提交
33
    SScalableBf *tsSBF = tScalableBfInit(pInfo->interval * ROWS_PER_MILLISECOND, DEFAULT_FALSE_POSITIVE);
R
root 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    taosArrayPush(pInfo->pTsSBFs, &tsSBF);
  }
}

static void windowSBfDelete(SUpdateInfo *pInfo, uint64_t count) {
  if (count < pInfo->numSBFs - 1) {
    for (uint64_t i = 0; i < count; ++i) {
      SScalableBf *pTsSBFs = taosArrayGetP(pInfo->pTsSBFs, i);
      tScalableBfDestroy(pTsSBFs);
      taosArrayRemove(pInfo->pTsSBFs, i);
    }
  } else {
    taosArrayClearP(pInfo->pTsSBFs, (FDelete)tScalableBfDestroy);
  }
  pInfo->minTS += pInfo->interval * count;
}

static int64_t adjustInterval(int64_t interval, int32_t precision) {
  int64_t val = interval;
  if (precision != TSDB_TIME_PRECISION_MILLI) {
    val = convertTimePrecision(interval, precision, TSDB_TIME_PRECISION_MILLI);
  }
5
54liuyao 已提交
56 57

  if (val <= 0 || val > MAX_INTERVAL) {
R
root 已提交
58
    val = MAX_INTERVAL;
5
54liuyao 已提交
59 60 61 62 63 64
  } else if (val < MIN_INTERVAL) {
    val = MIN_INTERVAL;
  }

  if (precision != TSDB_TIME_PRECISION_MILLI) {
    val = convertTimePrecision(val, TSDB_TIME_PRECISION_MILLI, precision);
R
root 已提交
65 66 67 68
  }
  return val;
}

5
54liuyao 已提交
69 70 71 72 73 74 75 76 77
static int64_t adjustWatermark(int64_t interval, int32_t watermark) {
  if (watermark <= 0 || watermark > MAX_NUM_SCALABLE_BF * interval) {
    watermark = MAX_NUM_SCALABLE_BF * interval;
  } else if (watermark < MIN_NUM_SCALABLE_BF * interval) {
    watermark = MIN_NUM_SCALABLE_BF * interval;
  }
  return watermark;
}

L
Liu Jicong 已提交
78
SUpdateInfo *updateInfoInitP(SInterval *pInterval, int64_t watermark) {
R
root 已提交
79 80 81 82 83 84 85 86 87 88 89 90
  return updateInfoInit(pInterval->interval, pInterval->precision, watermark);
}

SUpdateInfo *updateInfoInit(int64_t interval, int32_t precision, int64_t watermark) {
  SUpdateInfo *pInfo = taosMemoryCalloc(1, sizeof(SUpdateInfo));
  if (pInfo == NULL) {
    return NULL;
  }
  pInfo->pTsBuckets = NULL;
  pInfo->pTsSBFs = NULL;
  pInfo->minTS = -1;
  pInfo->interval = adjustInterval(interval, precision);
5
54liuyao 已提交
91
  pInfo->watermark = adjustWatermark(pInfo->interval, watermark);
R
root 已提交
92

5
54liuyao 已提交
93
  uint64_t bfSize = (uint64_t)(pInfo->watermark / pInfo->interval);
R
root 已提交
94

L
Liu Jicong 已提交
95
  pInfo->pTsSBFs = taosArrayInit(bfSize, sizeof(void *));
R
root 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
  if (pInfo->pTsSBFs == NULL) {
    updateInfoDestroy(pInfo);
    return NULL;
  }
  pInfo->numSBFs = bfSize;
  windowSBfAdd(pInfo, bfSize);

  pInfo->pTsBuckets = taosArrayInit(DEFAULT_BUCKET_SIZE, sizeof(TSKEY));
  if (pInfo->pTsBuckets == NULL) {
    updateInfoDestroy(pInfo);
    return NULL;
  }

  TSKEY dumy = 0;
L
Liu Jicong 已提交
110
  for (uint64_t i = 0; i < DEFAULT_BUCKET_SIZE; ++i) {
R
root 已提交
111 112 113 114 115 116
    taosArrayPush(pInfo->pTsBuckets, &dumy);
  }
  pInfo->numBuckets = DEFAULT_BUCKET_SIZE;
  return pInfo;
}

L
Liu Jicong 已提交
117
static SScalableBf *getSBf(SUpdateInfo *pInfo, TSKEY ts) {
R
root 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  if (ts <= 0) {
    return NULL;
  }
  if (pInfo->minTS < 0) {
    pInfo->minTS = (TSKEY)(ts / pInfo->interval * pInfo->interval);
  }
  uint64_t index = (uint64_t)((ts - pInfo->minTS) / pInfo->interval);
  if (index >= pInfo->numSBFs) {
    uint64_t count = index + 1 - pInfo->numSBFs;
    windowSBfDelete(pInfo, count);
    windowSBfAdd(pInfo, count);
    index = pInfo->numSBFs - 1;
  }
  SScalableBf *res = taosArrayGetP(pInfo->pTsSBFs, index);
  if (res == NULL) {
L
Liu Jicong 已提交
133
    res = tScalableBfInit(pInfo->interval * ROWS_PER_MILLISECOND, DEFAULT_FALSE_POSITIVE);
R
root 已提交
134 135 136 137 138
    taosArrayPush(pInfo->pTsSBFs, &res);
  }
  return res;
}

5
54liuyao 已提交
139
bool updateInfoIsUpdated(SUpdateInfo *pInfo, tb_uid_t tableId, TSKEY ts) {
L
Liu Jicong 已提交
140 141 142
  int32_t      res = TSDB_CODE_FAILED;
  uint64_t     index = ((uint64_t)tableId) % pInfo->numBuckets;
  SScalableBf *pSBf = getSBf(pInfo, ts);
R
root 已提交
143 144 145 146 147 148
  // pSBf may be a null pointer
  if (pSBf) {
    res = tScalableBfPut(pSBf, &ts, sizeof(TSKEY));
  }

  TSKEY maxTs = *(TSKEY *)taosArrayGet(pInfo->pTsBuckets, index);
L
Liu Jicong 已提交
149
  if (maxTs < ts) {
R
root 已提交
150 151 152 153 154 155 156 157 158 159
    taosArraySet(pInfo->pTsBuckets, index, &ts);
    return false;
  }

  if (ts < pInfo->minTS) {
    return true;
  } else if (res == TSDB_CODE_SUCCESS) {
    return false;
  }

L
Liu Jicong 已提交
160
  // check from tsdb api
R
root 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174
  return true;
}

void updateInfoDestroy(SUpdateInfo *pInfo) {
  if (pInfo == NULL) {
    return;
  }
  taosArrayDestroy(pInfo->pTsBuckets);

  uint64_t size = taosArrayGetSize(pInfo->pTsSBFs);
  for (uint64_t i = 0; i < size; i++) {
    SScalableBf *pSBF = taosArrayGetP(pInfo->pTsSBFs, i);
    tScalableBfDestroy(pSBF);
  }
L
Liu Jicong 已提交
175

R
root 已提交
176 177
  taosArrayDestroy(pInfo->pTsSBFs);
  taosMemoryFree(pInfo);
L
Liu Jicong 已提交
178
}