tstreamUpdate.c 5.5 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
#define DEFAULT_BUCKET_SIZE    1024
#define ROWS_PER_MILLISECOND   1
5
54liuyao 已提交
22
#define MAX_NUM_SCALABLE_BF    100000
L
Liu Jicong 已提交
23 24 25 26
#define MIN_NUM_SCALABLE_BF    10
#define DEFAULT_PREADD_BUCKET  1
#define MAX_INTERVAL           MILLISECOND_PER_MINUTE
#define MIN_INTERVAL           (MILLISECOND_PER_SECOND * 10)
5
54liuyao 已提交
27 28 29 30 31
#define DEFAULT_EXPECTED_ENTRIES    10000

static int64_t adjustExpEntries(int64_t entries) {
  return TMIN(DEFAULT_EXPECTED_ENTRIES, entries);
}
R
root 已提交
32 33

static void windowSBfAdd(SUpdateInfo *pInfo, uint64_t count) {
L
Liu Jicong 已提交
34
  if (pInfo->numSBFs < count) {
R
root 已提交
35 36 37
    count = pInfo->numSBFs;
  }
  for (uint64_t i = 0; i < count; ++i) {
5
54liuyao 已提交
38 39
    int64_t rows = adjustExpEntries(pInfo->interval * ROWS_PER_MILLISECOND);
    SScalableBf *tsSBF = tScalableBfInit(rows, DEFAULT_FALSE_POSITIVE);
R
root 已提交
40 41 42 43 44 45 46
    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) {
5
54liuyao 已提交
47
      SScalableBf *pTsSBFs = taosArrayGetP(pInfo->pTsSBFs, 0);
R
root 已提交
48
      tScalableBfDestroy(pTsSBFs);
5
54liuyao 已提交
49
      taosArrayRemove(pInfo->pTsSBFs, 0);
R
root 已提交
50 51 52 53 54 55 56 57 58 59 60 61
    }
  } 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 已提交
62 63

  if (val <= 0 || val > MAX_INTERVAL) {
R
root 已提交
64
    val = MAX_INTERVAL;
5
54liuyao 已提交
65 66 67 68 69 70
  } else if (val < MIN_INTERVAL) {
    val = MIN_INTERVAL;
  }

  if (precision != TSDB_TIME_PRECISION_MILLI) {
    val = convertTimePrecision(val, TSDB_TIME_PRECISION_MILLI, precision);
R
root 已提交
71 72 73 74
  }
  return val;
}

5
54liuyao 已提交
75
static int64_t adjustWatermark(int64_t interval, int64_t watermark) {
5
54liuyao 已提交
76 77 78 79 80 81 82 83
  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 已提交
84
SUpdateInfo *updateInfoInitP(SInterval *pInterval, int64_t watermark) {
R
root 已提交
85 86 87 88 89 90 91 92 93 94 95 96
  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 已提交
97
  pInfo->watermark = adjustWatermark(pInfo->interval, watermark);
R
root 已提交
98

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

L
Liu Jicong 已提交
101
  pInfo->pTsSBFs = taosArrayInit(bfSize, sizeof(void *));
R
root 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115
  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 已提交
116
  for (uint64_t i = 0; i < DEFAULT_BUCKET_SIZE; ++i) {
R
root 已提交
117 118 119 120 121 122
    taosArrayPush(pInfo->pTsBuckets, &dumy);
  }
  pInfo->numBuckets = DEFAULT_BUCKET_SIZE;
  return pInfo;
}

L
Liu Jicong 已提交
123
static SScalableBf *getSBf(SUpdateInfo *pInfo, TSKEY ts) {
R
root 已提交
124 125 126 127 128 129
  if (ts <= 0) {
    return NULL;
  }
  if (pInfo->minTS < 0) {
    pInfo->minTS = (TSKEY)(ts / pInfo->interval * pInfo->interval);
  }
5
54liuyao 已提交
130 131 132 133
  int64_t index = (int64_t)((ts - pInfo->minTS) / pInfo->interval);
  if (index < 0) {
    return NULL;
  }
R
root 已提交
134 135 136 137 138 139 140 141
  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) {
5
54liuyao 已提交
142 143
    int64_t rows = adjustExpEntries(pInfo->interval * ROWS_PER_MILLISECOND);
    res = tScalableBfInit(rows, DEFAULT_FALSE_POSITIVE);
R
root 已提交
144 145 146 147 148
    taosArrayPush(pInfo->pTsSBFs, &res);
  }
  return res;
}

5
54liuyao 已提交
149
bool updateInfoIsUpdated(SUpdateInfo *pInfo, tb_uid_t tableId, TSKEY ts) {
L
Liu Jicong 已提交
150 151 152
  int32_t      res = TSDB_CODE_FAILED;
  uint64_t     index = ((uint64_t)tableId) % pInfo->numBuckets;
  SScalableBf *pSBf = getSBf(pInfo, ts);
R
root 已提交
153 154 155 156 157 158
  // pSBf may be a null pointer
  if (pSBf) {
    res = tScalableBfPut(pSBf, &ts, sizeof(TSKEY));
  }

  TSKEY maxTs = *(TSKEY *)taosArrayGet(pInfo->pTsBuckets, index);
L
Liu Jicong 已提交
159
  if (maxTs < ts) {
R
root 已提交
160 161 162 163 164 165 166 167 168 169
    taosArraySet(pInfo->pTsBuckets, index, &ts);
    return false;
  }

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

L
Liu Jicong 已提交
170
  // check from tsdb api
R
root 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
  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 已提交
185

R
root 已提交
186 187
  taosArrayDestroy(pInfo->pTsSBFs);
  taosMemoryFree(pInfo);
L
Liu Jicong 已提交
188
}