syncEnv.c 4.2 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 "syncEnv.h"
M
Minghao Li 已提交
17
// #include <ASSERT.h>
M
Minghao Li 已提交
18 19 20

SSyncEnv *gSyncEnv = NULL;

M
Minghao Li 已提交
21
// local function -----------------
M
Minghao Li 已提交
22 23 24 25 26
static SSyncEnv *doSyncEnvStart();
static int32_t   doSyncEnvStop(SSyncEnv *pSyncEnv);
static int32_t   doSyncEnvStartTimer(SSyncEnv *pSyncEnv);
static int32_t   doSyncEnvStopTimer(SSyncEnv *pSyncEnv);
static void      syncEnvTick(void *param, void *tmrId);
M
Minghao Li 已提交
27 28
// --------------------------------

29 30 31 32 33 34 35 36
bool syncEnvIsStart() {
  if (gSyncEnv == NULL) {
    return false;
  }

  return atomic_load_8(&(gSyncEnv->isStart));
}

M
Minghao Li 已提交
37
int32_t syncEnvStart() {
38 39 40
  int32_t  ret = 0;
  uint32_t seed = (uint32_t)(taosGetTimestampNs() & 0x00000000FFFFFFFF);
  taosSeedRand(seed);
M
format  
Minghao Li 已提交
41
  // gSyncEnv = doSyncEnvStart(gSyncEnv);
L
Li Minghao 已提交
42
  gSyncEnv = doSyncEnvStart();
M
Minghao Li 已提交
43
  ASSERT(gSyncEnv != NULL);
44
  sTrace("sync env start ok");
M
Minghao Li 已提交
45 46 47 48 49 50 51 52
  return ret;
}

int32_t syncEnvStop() {
  int32_t ret = doSyncEnvStop(gSyncEnv);
  return ret;
}

M
Minghao Li 已提交
53 54 55
int32_t syncEnvStartTimer() {
  int32_t ret = doSyncEnvStartTimer(gSyncEnv);
  return ret;
M
Minghao Li 已提交
56 57
}

M
Minghao Li 已提交
58 59 60 61
int32_t syncEnvStopTimer() {
  int32_t ret = doSyncEnvStopTimer(gSyncEnv);
  return ret;
}
M
Minghao Li 已提交
62 63 64 65

// local function -----------------
static void syncEnvTick(void *param, void *tmrId) {
  SSyncEnv *pSyncEnv = (SSyncEnv *)param;
M
Minghao Li 已提交
66 67
  if (atomic_load_64(&pSyncEnv->envTickTimerLogicClockUser) <= atomic_load_64(&pSyncEnv->envTickTimerLogicClock)) {
    ++(pSyncEnv->envTickTimerCounter);
M
Minghao Li 已提交
68 69 70 71 72 73
    sTrace("syncEnvTick do ... envTickTimerLogicClockUser:%" PRIu64 ", envTickTimerLogicClock:%" PRIu64
           ", envTickTimerCounter:%" PRIu64
           ", "
           "envTickTimerMS:%d, tmrId:%p",
           pSyncEnv->envTickTimerLogicClockUser, pSyncEnv->envTickTimerLogicClock, pSyncEnv->envTickTimerCounter,
           pSyncEnv->envTickTimerMS, tmrId);
M
Minghao Li 已提交
74 75 76

    // do something, tick ...
    taosTmrReset(syncEnvTick, pSyncEnv->envTickTimerMS, pSyncEnv, pSyncEnv->pTimerManager, &pSyncEnv->pEnvTickTimer);
M
Minghao Li 已提交
77
  } else {
M
Minghao Li 已提交
78 79 80 81 82 83
    sTrace("syncEnvTick pass ... envTickTimerLogicClockUser:%" PRIu64 ", envTickTimerLogicClock:%" PRIu64
           ", envTickTimerCounter:%" PRIu64
           ", "
           "envTickTimerMS:%d, tmrId:%p",
           pSyncEnv->envTickTimerLogicClockUser, pSyncEnv->envTickTimerLogicClock, pSyncEnv->envTickTimerCounter,
           pSyncEnv->envTickTimerMS, tmrId);
M
Minghao Li 已提交
84
  }
M
Minghao Li 已提交
85 86
}

M
Minghao Li 已提交
87
static SSyncEnv *doSyncEnvStart() {
wafwerar's avatar
wafwerar 已提交
88
  SSyncEnv *pSyncEnv = (SSyncEnv *)taosMemoryMalloc(sizeof(SSyncEnv));
M
Minghao Li 已提交
89
  ASSERT(pSyncEnv != NULL);
90
  memset(pSyncEnv, 0, sizeof(SSyncEnv));
M
Minghao Li 已提交
91 92 93 94 95 96

  pSyncEnv->envTickTimerCounter = 0;
  pSyncEnv->envTickTimerMS = ENV_TICK_TIMER_MS;
  pSyncEnv->FpEnvTickTimer = syncEnvTick;
  atomic_store_64(&pSyncEnv->envTickTimerLogicClock, 0);
  atomic_store_64(&pSyncEnv->envTickTimerLogicClockUser, 0);
M
Minghao Li 已提交
97 98 99

  // start tmr thread
  pSyncEnv->pTimerManager = taosTmrInit(1000, 50, 10000, "SYNC-ENV");
100

M
format  
Minghao Li 已提交
101
  atomic_store_8(&(pSyncEnv->isStart), 1);
M
Minghao Li 已提交
102
  return pSyncEnv;
M
Minghao Li 已提交
103 104 105
}

static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) {
M
Minghao Li 已提交
106
  ASSERT(pSyncEnv == gSyncEnv);
L
Li Minghao 已提交
107
  if (pSyncEnv != NULL) {
M
format  
Minghao Li 已提交
108
    atomic_store_8(&(pSyncEnv->isStart), 0);
L
Li Minghao 已提交
109 110 111
    taosTmrCleanUp(pSyncEnv->pTimerManager);
    taosMemoryFree(pSyncEnv);
  }
L
Li Minghao 已提交
112
  gSyncEnv = NULL;
M
Minghao Li 已提交
113 114 115
  return 0;
}

M
Minghao Li 已提交
116 117
static int32_t doSyncEnvStartTimer(SSyncEnv *pSyncEnv) {
  int32_t ret = 0;
M
Minghao Li 已提交
118 119
  taosTmrReset(pSyncEnv->FpEnvTickTimer, pSyncEnv->envTickTimerMS, pSyncEnv, pSyncEnv->pTimerManager,
               &pSyncEnv->pEnvTickTimer);
M
Minghao Li 已提交
120 121
  atomic_store_64(&pSyncEnv->envTickTimerLogicClock, pSyncEnv->envTickTimerLogicClockUser);
  return ret;
M
Minghao Li 已提交
122
}
M
Minghao Li 已提交
123

M
Minghao Li 已提交
124
static int32_t doSyncEnvStopTimer(SSyncEnv *pSyncEnv) {
M
Minghao Li 已提交
125
  int32_t ret = 0;
M
Minghao Li 已提交
126 127 128
  atomic_add_fetch_64(&pSyncEnv->envTickTimerLogicClockUser, 1);
  taosTmrStop(pSyncEnv->pEnvTickTimer);
  pSyncEnv->pEnvTickTimer = NULL;
M
Minghao Li 已提交
129
  return ret;
M
Minghao Li 已提交
130
}