syncEntryCacheTest.cpp 4.9 KB
Newer Older
1
#include "syncTest.h"
M
Minghao Li 已提交
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

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

39
  SRaftEntryCache* pCache = raftEntryCacheCreate(pSyncNode, maxCount);
M
Minghao Li 已提交
40 41 42 43 44 45
  ASSERT(pCache != NULL);

  return pCache;
}

void test1() {
46
  int32_t          code = 0;
M
Minghao Li 已提交
47
  SRaftEntryCache* pCache = createCache(5);
48
  for (int i = 0; i < 10; ++i) {
M
Minghao Li 已提交
49
    SSyncRaftEntry* pEntry = createEntry(i);
50 51
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
M
Minghao Li 已提交
52
  }
53
  raftEntryCacheLog2((char*)"==test1 write 5 entries==", pCache);
M
Minghao Li 已提交
54

55 56
  raftEntryCacheClear(pCache, 3);
  raftEntryCacheLog2((char*)"==test1 evict 3 entries==", pCache);
M
Minghao Li 已提交
57

58 59
  raftEntryCacheClear(pCache, -1);
  raftEntryCacheLog2((char*)"==test1 evict -1(all) entries==", pCache);
M
Minghao Li 已提交
60 61 62
}

void test2() {
63
  int32_t          code = 0;
M
Minghao Li 已提交
64
  SRaftEntryCache* pCache = createCache(5);
65
  for (int i = 0; i < 10; ++i) {
M
Minghao Li 已提交
66
    SSyncRaftEntry* pEntry = createEntry(i);
67 68
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
M
Minghao Li 已提交
69
  }
70
  raftEntryCacheLog2((char*)"==test1 write 5 entries==", pCache);
M
Minghao Li 已提交
71

72
  SyncIndex       index = 2;
73
  SSyncRaftEntry* pEntry = NULL;
M
Minghao Li 已提交
74

75 76
  code = raftEntryCacheGetEntryP(pCache, index, &pEntry);
  ASSERT(code == 1 && index == pEntry->index);
S
Shengliang Guan 已提交
77
  sTrace("get entry:%p for %" PRId64, pEntry, index);
M
Minghao Li 已提交
78 79
  syncEntryLog2((char*)"==test2 get entry pointer 2==", pEntry);

80 81
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 1 && index == pEntry->index);
S
Shengliang Guan 已提交
82
  sTrace("get entry:%p for %" PRId64, pEntry, index);
83 84 85
  syncEntryLog2((char*)"==test2 get entry 2==", pEntry);
  syncEntryDestory(pEntry);

M
Minghao Li 已提交
86 87
  // not found
  index = 8;
88 89
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 0);
S
Shengliang Guan 已提交
90
  sTrace("get entry:%p for %" PRId64, pEntry, index);
M
Minghao Li 已提交
91 92 93 94
  sTrace("==test2 get entry 8 not found==");

  // not found
  index = 9;
95 96
  code = raftEntryCacheGetEntry(pCache, index, &pEntry);
  ASSERT(code == 0);
S
Shengliang Guan 已提交
97
  sTrace("get entry:%p for %" PRId64, pEntry, index);
98
  sTrace("==test2 get entry 9 not found==");
M
Minghao Li 已提交
99 100 101
}

void test3() {
102
  int32_t          code = 0;
103 104
  SRaftEntryCache* pCache = createCache(20);
  for (int i = 0; i <= 4; ++i) {
M
Minghao Li 已提交
105
    SSyncRaftEntry* pEntry = createEntry(i);
106 107
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
108 109
  }
  for (int i = 9; i >= 5; --i) {
110
    SSyncRaftEntry* pEntry = createEntry(i);
111 112
    code = raftEntryCachePutEntry(pCache, pEntry);
    sTrace("put entry code:%d, pEntry:%p", code, pEntry);
113
  }
114
  raftEntryCacheLog2((char*)"==test3 write 10 entries==", pCache);
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129
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);
S
Shengliang Guan 已提交
130
  sTrace("rid: %" PRId64, rid);
131

132 133 134 135
  do {
    SSyncRaftEntry* pAcquireEntry = (SSyncRaftEntry*)taosAcquireRef(testRefId, rid);
    syncEntryLog2((char*)"acquire: ", pAcquireEntry);

136
    taosAcquireRef(testRefId, rid);
137 138 139
    taosAcquireRef(testRefId, rid);
    taosAcquireRef(testRefId, rid);

140 141
    // taosReleaseRef(testRefId, rid);
    // taosReleaseRef(testRefId, rid);
142 143 144
  } while (0);

  taosRemoveRef(testRefId, rid);
145 146 147 148 149 150 151 152 153 154 155 156 157 158

  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);
S
Shengliang Guan 已提交
159
    sTrace("rid: %" PRId64, rid);
160 161 162 163 164 165
  }

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

M
Minghao Li 已提交
168 169 170 171 172
int main(int argc, char** argv) {
  gRaftDetailLog = true;
  tsAsyncLog = 0;
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_DEBUG;

173 174 175 176 177
  /*
    test1();
    test2();
    test3();
  */
178
  test4();
179
  // test5();
180

M
Minghao Li 已提交
181 182
  return 0;
}