syncEntryCacheTest.cpp 7.5 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"
M
Minghao Li 已提交
9 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

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);

  SRaftEntryCache* pCache = raftCacheCreate(pSyncNode, maxCount);
  ASSERT(pCache != NULL);

  return pCache;
}

void test1() {
  int32_t          code = 0;
  SRaftEntryCache* pCache = createCache(5);
  for (int i = 0; i < 5; ++i) {
    SSyncRaftEntry* pEntry = createEntry(i);
    code = raftCachePutEntry(pCache, pEntry);
    ASSERT(code == 1);
    syncEntryDestory(pEntry);
  }
  raftCacheLog2((char*)"==test1 write 5 entries==", pCache);

  SyncIndex index;
  index = 1;
  code = raftCacheDelEntry(pCache, index);
  ASSERT(code == 0);
  index = 3;
  code = raftCacheDelEntry(pCache, index);
  ASSERT(code == 0);
  raftCacheLog2((char*)"==test1 delete 1,3==", pCache);

  code = raftCacheClear(pCache);
  ASSERT(code == 0);
  raftCacheLog2((char*)"==clear all==", pCache);
}

void test2() {
  int32_t          code = 0;
  SRaftEntryCache* pCache = createCache(5);
  for (int i = 0; i < 5; ++i) {
    SSyncRaftEntry* pEntry = createEntry(i);
    code = raftCachePutEntry(pCache, pEntry);
    ASSERT(code == 1);
    syncEntryDestory(pEntry);
  }
  raftCacheLog2((char*)"==test2 write 5 entries==", pCache);

  SyncIndex index;
  index = 1;
  SSyncRaftEntry* pEntry;
  code = raftCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 0);
  syncEntryDestory(pEntry);
  syncEntryLog2((char*)"==test2 get entry 1==", pEntry);

  index = 2;
  code = raftCacheGetEntryP(pCache, index, &pEntry);
  ASSERT(code == 0);
  syncEntryLog2((char*)"==test2 get entry pointer 2==", pEntry);

  // not found
  index = 8;
  code = raftCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == -1 && terrno == TSDB_CODE_WAL_LOG_NOT_EXIST);
  sTrace("==test2 get entry 8 not found==");

  // not found
  index = 9;
  code = raftCacheGetEntryP(pCache, index, &pEntry);
  ASSERT(code == -1 && terrno == TSDB_CODE_WAL_LOG_NOT_EXIST);
  sTrace("==test2 get entry pointer 9 not found==");
}

void test3() {
  int32_t          code = 0;
  SRaftEntryCache* pCache = createCache(5);
  for (int i = 0; i < 5; ++i) {
    SSyncRaftEntry* pEntry = createEntry(i);
    code = raftCachePutEntry(pCache, pEntry);
    ASSERT(code == 1);
    syncEntryDestory(pEntry);
  }
  for (int i = 6; i < 10; ++i) {
    SSyncRaftEntry* pEntry = createEntry(i);
    code = raftCachePutEntry(pCache, pEntry);
    ASSERT(code == 0);
    syncEntryDestory(pEntry);
  }
  raftCacheLog2((char*)"==test3 write 10 entries, max count is 5==", pCache);
}

void test4() {
  int32_t          code = 0;
  SRaftEntryCache* pCache = createCache(5);
  for (int i = 0; i < 5; ++i) {
    SSyncRaftEntry* pEntry = createEntry(i);
    code = raftCachePutEntry(pCache, pEntry);
    ASSERT(code == 1);
    syncEntryDestory(pEntry);
  }
  raftCacheLog2((char*)"==test4 write 5 entries==", pCache);

  SyncIndex index;
  index = 3;
  SSyncRaftEntry* pEntry;
  code = raftCacheGetAndDel(pCache, index, &pEntry);
  ASSERT(code == 0);
  syncEntryLog2((char*)"==test4 get-and-del entry 3==", pEntry);
  raftCacheLog2((char*)"==test4 after get-and-del entry 3==", pCache);
}

152 153
static char* keyFn(const void* pData) {
  SSyncRaftEntry* pEntry = (SSyncRaftEntry*)pData;
154
  return (char*)(&(pEntry->index));
155 156
}

157
static int cmpFn(const void* p1, const void* p2) { return memcmp(p1, p2, sizeof(SyncIndex)); }
158 159 160 161 162 163 164 165 166 167 168 169 170

void printSkipList(SSkipList* pSkipList) {
  ASSERT(pSkipList != NULL);

  SSkipListIterator* pIter = tSkipListCreateIter(pSkipList);
  while (tSkipListIterNext(pIter)) {
    SSkipListNode* pNode = tSkipListIterGet(pIter);
    ASSERT(pNode != NULL);
    SSyncRaftEntry* pEntry = (SSyncRaftEntry*)SL_GET_NODE_DATA(pNode);
    syncEntryPrint2((char*)"", pEntry);
  }
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
void delSkipListFirst(SSkipList* pSkipList, int n) {
  ASSERT(pSkipList != NULL);

  sTrace("delete first %d -------------", n);
  SSkipListIterator* pIter = tSkipListCreateIter(pSkipList);
  for (int i = 0; i < n; ++i) {
    tSkipListIterNext(pIter);
    SSkipListNode* pNode = tSkipListIterGet(pIter);
    tSkipListRemoveNode(pSkipList, pNode);
  }
}


SSyncRaftEntry* getLogEntry2(SSkipList* pSkipList, SyncIndex index) {
  SyncIndex index2 = index;
  SSyncRaftEntry *pEntry = NULL;
187 188 189 190 191 192 193 194
  int arraySize = 0;

  SArray*  entryPArray = tSkipListGet(pSkipList, (char*)(&index2));
  arraySize = taosArrayGetSize(entryPArray);
  if (arraySize > 0) {
    SSkipListNode** ppNode = (SSkipListNode**)taosArrayGet(entryPArray, 0);
    ASSERT(*ppNode != NULL);
    pEntry = (SSyncRaftEntry*)SL_GET_NODE_DATA(*ppNode);
195
  }
196
  taosArrayDestroy(entryPArray);
197

198 199
  sTrace("get index2: %ld, arraySize:%d -------------", index, arraySize);
  syncEntryLog2((char*)"getLogEntry2", pEntry);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  return pEntry;
}


SSyncRaftEntry* getLogEntry(SSkipList* pSkipList, SyncIndex index) {
  sTrace("get index: %ld -------------", index);
  SyncIndex index2 = index;
  SSyncRaftEntry *pEntry = NULL;
  SSkipListIterator* pIter = tSkipListCreateIterFromVal(pSkipList, (const char *)&index2, TSDB_DATA_TYPE_BINARY, TSDB_ORDER_ASC);
  if (tSkipListIterNext(pIter)) {
    SSkipListNode* pNode = tSkipListIterGet(pIter);
    ASSERT(pNode != NULL);
    pEntry = (SSyncRaftEntry*)SL_GET_NODE_DATA(pNode);
  }

215
  syncEntryLog2((char*)"getLogEntry", pEntry);
216 217 218
  return pEntry;
}

219
void test5() {
220 221
  SSkipList* pSkipList =
      tSkipListCreate(MAX_SKIP_LIST_LEVEL, TSDB_DATA_TYPE_BINARY, sizeof(SyncIndex), cmpFn, SL_ALLOW_DUP_KEY, keyFn);
222 223
  ASSERT(pSkipList != NULL);

224 225
  sTrace("insert 9 - 5");
  for (int i = 9; i >= 5; --i) {
226 227 228 229
    SSyncRaftEntry* pEntry = createEntry(i);
    SSkipListNode*  pSkipListNode = tSkipListPut(pSkipList, pEntry);
  }

230 231
  sTrace("insert 0 - 4");
  for (int i = 0; i <= 4; ++i) {
232 233 234 235
    SSyncRaftEntry* pEntry = createEntry(i);
    SSkipListNode*  pSkipListNode = tSkipListPut(pSkipList, pEntry);
  }

236 237 238 239 240 241 242
  sTrace("insert 7 7 7 7 7");
  for (int i = 0; i <= 4; ++i) {
    SSyncRaftEntry* pEntry = createEntry(7);
    SSkipListNode*  pSkipListNode = tSkipListPut(pSkipList, pEntry);
  }

  sTrace("print: -------------");
243 244
  printSkipList(pSkipList);

245 246 247 248 249 250 251 252 253 254
  delSkipListFirst(pSkipList, 3);

  sTrace("print: -------------");
  printSkipList(pSkipList);

  getLogEntry(pSkipList, 2);
  getLogEntry(pSkipList, 5);  
  getLogEntry(pSkipList, 7);
  getLogEntry(pSkipList, 7);

255 256 257 258 259
  getLogEntry2(pSkipList, 2);
  getLogEntry2(pSkipList, 5);  
  getLogEntry2(pSkipList, 7);
  getLogEntry2(pSkipList, 7);

260

261 262 263
  tSkipListDestroy(pSkipList);
}

M
Minghao Li 已提交
264 265 266 267 268
int main(int argc, char** argv) {
  gRaftDetailLog = true;
  tsAsyncLog = 0;
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_DEBUG;

269 270 271 272 273 274 275 276
  /*
    test1();
    test2();
    test3();
    test4();
  */

  test5();
M
Minghao Li 已提交
277 278 279

  return 0;
}