syncEntryCacheTest.cpp 5.0 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
8
#include "tskiplist.h"
9
#include "tref.h"
M
Minghao Li 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

void logTest() {
  sTrace("--- sync log test: trace");
  sDebug("--- sync log test: debug");
  sInfo("--- sync log test: info");
  sWarn("--- sync log test: warn");
  sError("--- sync log test: error");
  sFatal("--- sync log test: fatal");
}

SSyncRaftEntry* createEntry(int i) {
  int32_t         dataLen = 20;
  SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
  assert(pEntry != NULL);
  pEntry->msgType = 88;
  pEntry->originalRpcType = 99;
  pEntry->seqNum = 3;
  pEntry->isWeak = true;
  pEntry->term = 100 + i;
  pEntry->index = i;
  snprintf(pEntry->data, dataLen, "value%d", i);

  return pEntry;
}

SSyncNode* createFakeNode() {
  SSyncNode* pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
  ASSERT(pSyncNode != NULL);
  memset(pSyncNode, 0, sizeof(SSyncNode));

  return pSyncNode;
}

SRaftEntryCache* createCache(int maxCount) {
  SSyncNode* pSyncNode = createFakeNode();
  ASSERT(pSyncNode != NULL);

47
  SRaftEntryCache* pCache = raftEntryCacheCreate(pSyncNode, maxCount);
M
Minghao Li 已提交
48 49 50 51 52 53
  ASSERT(pCache != NULL);

  return pCache;
}

void test1() {
54
  int32_t              code = 0;
M
Minghao Li 已提交
55
  SRaftEntryCache* pCache = createCache(5);
56
  for (int i = 0; i < 10; ++i) {
M
Minghao Li 已提交
57
    SSyncRaftEntry* pEntry = createEntry(i);
58 59
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
M
Minghao Li 已提交
60
  }
61
  raftEntryCacheLog2((char*)"==test1 write 5 entries==", pCache);
M
Minghao Li 已提交
62

63 64
  raftEntryCacheClear(pCache, 3);
  raftEntryCacheLog2((char*)"==test1 evict 3 entries==", pCache);
M
Minghao Li 已提交
65

66 67
  raftEntryCacheClear(pCache, -1);
  raftEntryCacheLog2((char*)"==test1 evict -1(all) entries==", pCache);
M
Minghao Li 已提交
68 69 70
}

void test2() {
71
  int32_t              code = 0;
M
Minghao Li 已提交
72
  SRaftEntryCache* pCache = createCache(5);
73
  for (int i = 0; i < 10; ++i) {
M
Minghao Li 已提交
74
    SSyncRaftEntry* pEntry = createEntry(i);
75 76
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
M
Minghao Li 已提交
77
  }
78
  raftEntryCacheLog2((char*)"==test1 write 5 entries==", pCache);
M
Minghao Li 已提交
79

80 81
  SyncIndex index = 2;
  SSyncRaftEntry* pEntry = NULL;
M
Minghao Li 已提交
82

83 84 85
  code = raftEntryCacheGetEntryP(pCache, index, &pEntry);
  ASSERT(code == 1 && index == pEntry->index);
  sTrace("get entry:%p for %ld", pEntry, index);
M
Minghao Li 已提交
86 87
  syncEntryLog2((char*)"==test2 get entry pointer 2==", pEntry);

88 89 90 91 92 93
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 1 && index == pEntry->index);
  sTrace("get entry:%p for %ld", pEntry, index);
  syncEntryLog2((char*)"==test2 get entry 2==", pEntry);
  syncEntryDestory(pEntry);

M
Minghao Li 已提交
94 95
  // not found
  index = 8;
96 97 98
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 0);
  sTrace("get entry:%p for %ld", pEntry, index);
M
Minghao Li 已提交
99 100 101 102
  sTrace("==test2 get entry 8 not found==");

  // not found
  index = 9;
103 104 105 106
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 0);
  sTrace("get entry:%p for %ld", pEntry, index);
  sTrace("==test2 get entry 9 not found==");
M
Minghao Li 已提交
107 108 109
}

void test3() {
110 111 112
  int32_t              code = 0;
  SRaftEntryCache* pCache = createCache(20);
  for (int i = 0; i <= 4; ++i) {
M
Minghao Li 已提交
113
    SSyncRaftEntry* pEntry = createEntry(i);
114 115
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
116 117
  }
  for (int i = 9; i >= 5; --i) {
118
    SSyncRaftEntry* pEntry = createEntry(i);
119 120
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
121
  }
122
  raftEntryCacheLog2((char*)"==test3 write 10 entries==", pCache);
123 124
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145


static void freeObj(void* param) {
  SSyncRaftEntry* pEntry = (SSyncRaftEntry*)param;
  syncEntryLog2((char*)"freeObj: ", pEntry);
  syncEntryDestory(pEntry);
}

void test4() {
  int32_t testRefId = taosOpenRef(200, freeObj);

  SSyncRaftEntry* pEntry = createEntry(10);
  ASSERT(pEntry != NULL);

  int64_t rid = taosAddRef(testRefId, pEntry);
  sTrace("rid: %ld", rid);
  
  do {
    SSyncRaftEntry* pAcquireEntry = (SSyncRaftEntry*)taosAcquireRef(testRefId, rid);
    syncEntryLog2((char*)"acquire: ", pAcquireEntry);

146
    taosAcquireRef(testRefId, rid);
147 148 149
    taosAcquireRef(testRefId, rid);
    taosAcquireRef(testRefId, rid);

150
    //taosReleaseRef(testRefId, rid);
151 152 153 154
    //taosReleaseRef(testRefId, rid);
  } while (0);

  taosRemoveRef(testRefId, rid);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

  for (int i = 0; i < 10; ++i) {
    sTrace("taosReleaseRef, %d", i);
    taosReleaseRef(testRefId, rid);
  }
}

void test5() {
  int32_t testRefId = taosOpenRef(5, freeObj);
  for (int i = 0; i < 100; i++) {
    SSyncRaftEntry* pEntry = createEntry(i);
    ASSERT(pEntry != NULL);

    int64_t rid = taosAddRef(testRefId, pEntry);
    sTrace("rid: %ld", rid);
  }

  for (int64_t rid = 2; rid < 101; rid++) {
    SSyncRaftEntry* pAcquireEntry = (SSyncRaftEntry*)taosAcquireRef(testRefId, rid);
    syncEntryLog2((char*)"taosAcquireRef: ", pAcquireEntry);
  }
176 177
}

M
Minghao Li 已提交
178 179 180 181 182
int main(int argc, char** argv) {
  gRaftDetailLog = true;
  tsAsyncLog = 0;
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_DEBUG;

183
/*
184 185 186
  test1();
  test2();
  test3();
187 188 189
*/
  test4();
  //test5();
190

M
Minghao Li 已提交
191 192
  return 0;
}