syncHashCacheTest.cpp 7.4 KB
Newer Older
1
#include "syncTest.h"
2 3 4 5 6 7 8 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

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

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

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

  return pCache;
}

void test1() {
  int32_t              code = 0;
  SRaftEntryHashCache* 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;
  SRaftEntryHashCache* 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;
  SRaftEntryHashCache* 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;
  SRaftEntryHashCache* 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);
}

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

static int cmpFn(const void* p1, const void* p2) { return memcmp(p1, p2, sizeof(SyncIndex)); }

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

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;
  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);
  }
  taosArrayDestroy(entryPArray);

S
Shengliang Guan 已提交
190
  sTrace("get index2: %" PRId64 ", arraySize:%d -------------", index, arraySize);
191 192 193 194 195
  syncEntryLog2((char*)"getLogEntry2", pEntry);
  return pEntry;
}

SSyncRaftEntry* getLogEntry(SSkipList* pSkipList, SyncIndex index) {
S
Shengliang Guan 已提交
196
  sTrace("get index: %" PRId64 " -------------", index);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  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);
  }

  syncEntryLog2((char*)"getLogEntry", pEntry);
  return pEntry;
}

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

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

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

  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: -------------");
  printSkipList(pSkipList);

  delSkipListFirst(pSkipList, 3);

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

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

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

  tSkipListDestroy(pSkipList);
}

int main(int argc, char** argv) {
  gRaftDetailLog = true;
  tsAsyncLog = 0;
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_DEBUG;

  /*
    test1();
    test2();
    test3();
    test4();
  */

  test5();

  return 0;
}