1 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 190 191 192 193 194 195 196 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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
/*
* Copyright (C) 2011-2021 Codership Oy <info@codership.com>
*
* $Id$
*/
#define GCACHE_RB_UNIT_TEST
#include "gcache_rb_store.hpp"
#include "gcache_bh.hpp"
#include "gcache_rb_test.hpp"
#include <gu_logger.hpp>
#include <gu_throw.hpp>
using namespace gcache;
static gu::UUID const GID(NULL, 0);
static std::string const RB_NAME("rb_test");
static size_t const BH_SIZE(sizeof(gcache::BufferHeader));
typedef MemOps::size_type size_type;
static size_type ALLOC_SIZE(size_type s)
{
return MemOps::align_size(s + BH_SIZE);
}
START_TEST(test1)
{
::unlink(RB_NAME.c_str());
size_t const rb_size(ALLOC_SIZE(2) * 2);
seqno2ptr_t s2p(SEQNO_NONE);
gu::UUID gid(GID);
RingBuffer rb(NULL, RB_NAME, rb_size, s2p, gid, 0, false);
ck_assert_msg(rb.size() == rb_size,
"Expected %zd, got %zd", rb_size, rb.size());
if (gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << gid;
ck_abort_msg("%s", os.str().c_str());
}
void* buf1 = rb.malloc (MemOps::align_size(rb_size/2 + 1));
ck_assert(NULL == buf1); // > 1/2 size
buf1 = rb.malloc (ALLOC_SIZE(1));
ck_assert(NULL != buf1);
BufferHeader* bh1(ptr2BH(buf1));
ck_assert(bh1->seqno_g == SEQNO_NONE);
ck_assert(!BH_is_released(bh1));
void* buf2 = rb.malloc (ALLOC_SIZE(2));
ck_assert(NULL != buf2);
ck_assert(!BH_is_released(bh1));
BufferHeader* bh2(ptr2BH(buf2));
ck_assert(bh2->seqno_g == SEQNO_NONE);
ck_assert(!BH_is_released(bh2));
void* tmp = rb.realloc (buf1, ALLOC_SIZE(2));
// anything <= MemOps::ALIGNMENT should fit into original buffer
ck_assert(tmp == buf1 && MemOps::ALIGNMENT > 1);
tmp = rb.realloc (buf1, ALLOC_SIZE(MemOps::ALIGNMENT + 1));
// should require new buffer for which there's no space
ck_assert(bh2->seqno_g == SEQNO_NONE);
ck_assert(NULL == tmp);
BH_release(bh2);
rb.free (bh2);
tmp = rb.realloc (buf1, ALLOC_SIZE(3));
if (MemOps::ALIGNMENT > 2)
{
ck_assert(NULL != tmp);
ck_assert(buf1 == tmp);
}
else
{
ck_assert(NULL == tmp);
}
BH_release(bh1);
rb.free (bh1);
ck_assert(BH_is_released(bh1));
buf1 = rb.malloc(ALLOC_SIZE(1));
ck_assert(NULL != buf1);
tmp = rb.realloc (buf1, ALLOC_SIZE(2));
ck_assert(NULL != tmp);
ck_assert(tmp == buf1);
buf2 = rb.malloc (ALLOC_SIZE(1));
ck_assert(NULL != buf2);
tmp = rb.realloc (buf2, ALLOC_SIZE(2));
ck_assert(NULL != tmp);
ck_assert(tmp == buf2);
tmp = rb.malloc (ALLOC_SIZE(1));
ck_assert(NULL == tmp);
BH_release(ptr2BH(buf1));
rb.free(ptr2BH(buf1));
BH_release(ptr2BH(buf2));
rb.free(ptr2BH(buf2));
tmp = rb.malloc (ALLOC_SIZE(2));
ck_assert(NULL != tmp);
mark_point();
}
END_TEST
START_TEST(recovery)
{
struct msg
{
char msg;
seqno_t g;
seqno_t d;
size_t size() const { return sizeof(msg); }
};
#define MAX_MSGS 10
struct msg msgs[MAX_MSGS] =
{
{ '0', 1, 0 },
{ '1', 2, 0 },
{ '2', 4, 1 },
{ '3', SEQNO_ILL, SEQNO_ILL },
{ '4', 3, 1 },
{ '5', SEQNO_ILL, SEQNO_ILL },
{ '6', 5, SEQNO_ILL },
{ '7', SEQNO_ILL, SEQNO_ILL },
{ '8', 6, 4 },
{ '9', 7, 4 }
};
size_type const msg_size(ALLOC_SIZE(sizeof(reinterpret_cast<msg*>(0)->msg)));
struct rb_ctx
{
size_t const size;
seqno2ptr_t s2p;
gu::UUID gid;
RingBuffer rb;
rb_ctx(size_t s, bool recover = true) :
size(s), s2p(SEQNO_NONE), gid(GID),
rb(NULL, RB_NAME, size, s2p, gid, 0, recover)
{}
void seqno_assign (seqno2ptr_t& s2p, void* const ptr,
seqno_t const g, seqno_t const d)
{
s2p.insert(g, ptr);
BufferHeader* bh(ptr2BH(ptr));
bh->seqno_g = g;
if (d < 0) bh->flags |= BUFFER_SKIPPED;
}
void* add_msg(struct msg& m)
{
void* ret(rb.malloc(ALLOC_SIZE(m.size())));
if (ret)
{
::memcpy(ret, &m.msg, m.size());
if (m.g > 0) seqno_assign(s2p, ret, m.g, m.d);
BH_release(ptr2BH(ret));
rb.free(ptr2BH(ret));
}
return ret;
}
void print_map()
{
std::ostringstream os;
os << "S2P map:\n";
for (seqno2ptr_t::iterator i = s2p.begin(); i != s2p.end(); ++i)
{
log_info << "\tseqno: " << s2p.index(i) << ", msg: "
<< reinterpret_cast<const char*>(*i) << "\n";
}
log_info << os.str();
}
};
seqno_t seqno_min, seqno_max;
size_t const rb_5size(msg_size*5);
{
rb_ctx ctx(rb_5size);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(ctx.s2p.empty());
void* m(ctx.add_msg(msgs[0]));
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[0].g) == m);
m = ctx.add_msg(msgs[1]);
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[1].g) == m);
m = ctx.add_msg(msgs[2]);
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[2].g) == m);
m = ctx.add_msg(msgs[3]);
ck_assert(NULL != m);
ck_assert(msgs[3].g <= 0);
ck_assert(ctx.s2p.find(msgs[3].g) == ctx.s2p.end());
seqno_min = ctx.s2p.index_front();
seqno_max = ctx.s2p.index_back();
}
/* What we have now is |111222444***|----| */
/* Reopening of the file should:
* 1) discard messages 1, 2 since there is a hole at 3. Only 4 should remain
* 2) trim the trailing unordered message
*/
{
rb_ctx ctx(rb_5size);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx.s2p.empty());
ck_assert(ctx.s2p.size() == 1);
ck_assert(ctx.s2p.index_front() != seqno_min);
ck_assert(ctx.s2p.index_front() == seqno_max);
void* m(ctx.add_msg(msgs[4]));
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[4].g) == m);
m = ctx.add_msg(msgs[5]);
ck_assert(NULL != m);
ck_assert(msgs[5].g <= 0);
ck_assert(ctx.s2p.find(msgs[5].g) == ctx.s2p.end());
m = ctx.add_msg(msgs[6]);
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[6].g) <= m);
// here we should have rollover
ck_assert(ptr2BH(m) == BH_cast(ctx.rb.start()));
seqno_min = ctx.s2p.index_front();
seqno_max = ctx.s2p.index_back();
}
/* What we have now is |555|---|444333***| */
/* Reopening of the file should:
* 1) discard unordered message at the end
* 2) continuous seqno interval should be now 3,4,5
*/
{
rb_ctx ctx0(rb_5size);
ck_assert_msg(ctx0.rb.size() == ctx0.size,
"Expected %zd, got %zd", ctx0.size, ctx0.rb.size());
if (ctx0.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx0.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx0.s2p.empty());
ck_assert(ctx0.s2p.size() == 3);
ck_assert(ctx0.s2p.index_front() == seqno_min);
ck_assert(ctx0.s2p.index_back() == seqno_max);
/* now try to open unclosed file. Results should be the same */
rb_ctx ctx(rb_5size);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx.s2p.empty());
ck_assert_msg(ctx.s2p.size() == 3,
"Expected seqno2ptr size 3, got %zd", ctx.s2p.size());
ck_assert(ctx.s2p.index_front() == seqno_min);
ck_assert(ctx.s2p.index_back() == seqno_max);
seqno_min = ctx.s2p.index_front();
seqno_max = ctx.s2p.index_back();
}
size_t const rb_3size(msg_size*3);
/* now try to truncate the buffer. Only seqno 4,5 should remain */
/* |555---444| */
{
rb_ctx ctx(rb_3size);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx.s2p.empty());
ck_assert(ctx.s2p.size() == 2);
ck_assert(ctx.s2p.index_begin() != seqno_min);
ck_assert(ctx.s2p.index_back() == seqno_max);
void* m(ctx.add_msg(msgs[8]));
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[8].g) == m);
m = ctx.add_msg(msgs[9]);
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[9].g) == m);
m = ctx.add_msg(msgs[7]);
ck_assert(NULL != m);
ck_assert(msgs[7].g <= 0);
ck_assert(ctx.s2p.find(msgs[7].g) == ctx.s2p.end());
// here we should have rollover
ck_assert(ptr2BH(m) == BH_cast(ctx.rb.start()));
seqno_min = ctx.s2p.index_front();
seqno_max = ctx.s2p.index_back();
}
/* what we should have now is |***---777| - only one segment, at the end */
{
/* first open this with known offset */
rb_ctx ctx0(rb_3size);
ck_assert_msg(ctx0.rb.size() == ctx0.size,
"Expected %zd, got %zd", ctx0.size, ctx0.rb.size());
if (ctx0.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx0.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx0.s2p.empty());
ck_assert(ctx0.s2p.size() == 1);
ck_assert(ctx0.s2p.index_front() == seqno_max);
ck_assert(ctx0.s2p.index_back() == seqno_max);
/* now try to open unclosed file. Results should be the same */
rb_ctx ctx(rb_3size);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx.s2p.empty());
ck_assert(ctx.s2p.size() == 1);
ck_assert(ctx.s2p.index_front() == seqno_max);
ck_assert(ctx.s2p.index_back() == seqno_max);
ck_assert(seqno_max >= 1);
ck_assert(seqno_min == seqno_max);
}
::unlink(RB_NAME.c_str());
/* test for single segment in the middle */
ptrdiff_t third_buffer_offset(0);
{
rb_ctx ctx(rb_3size, false);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(ctx.s2p.empty());
void* m(ctx.add_msg(msgs[3]));
ck_assert(NULL != m);
ck_assert(ctx.s2p.find(msgs[3].g) == ctx.s2p.end());
m = ctx.add_msg(msgs[4]);
ck_assert(NULL != m);
ck_assert(*ctx.s2p.find(msgs[4].g) == m);
m = ctx.add_msg(msgs[5]);
ck_assert(NULL != m);
ck_assert(ctx.s2p.find(msgs[5].g) == ctx.s2p.end());
third_buffer_offset = ctx.rb.offset(m);
ck_assert(!ctx.s2p.empty());
ck_assert(ctx.s2p.size() == 1);
seqno_min = ctx.s2p.index_front();
seqno_max = ctx.s2p.index_back();
ck_assert(seqno_min == seqno_max);
}
/* now the situation should be |***444***| - only one segment, in the middle,
* reopen the file with a known position */
{
rb_ctx ctx(rb_3size);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx.s2p.empty());
ck_assert(ctx.s2p.size() == 1);
ck_assert(seqno_min == ctx.s2p.index_begin());
ck_assert(seqno_max == ctx.s2p.index_back());
ck_assert(seqno_min == seqno_max);
}
/* now the situation should be |---444---| - only one segment,in the middle,
* reopen the file a second time - to trigger a rollover bug */
{
rb_ctx ctx(rb_3size);
ck_assert_msg(ctx.rb.size() == ctx.size,
"Expected %zd, got %zd", ctx.size, ctx.rb.size());
if (ctx.gid != GID)
{
std::ostringstream os;
os << "Expected GID: " << GID << ", got: " << ctx.gid;
ck_abort_msg("%s", os.str().c_str());
}
ck_assert(!ctx.s2p.empty());
ck_assert(ctx.s2p.size() == 1);
ck_assert(seqno_min == ctx.s2p.index_front());
ck_assert(seqno_max == ctx.s2p.index_back());
ck_assert(seqno_min == seqno_max);
// must be allocated right after the recovered buffer
void* m(ctx.add_msg(msgs[3]));
ck_assert(NULL != m);
ck_assert_msg(third_buffer_offset == ctx.rb.offset(m),
"expected %zd, got %zd",
third_buffer_offset, ctx.rb.offset(m));
}
::unlink(RB_NAME.c_str());
}
END_TEST
Suite* gcache_rb_suite()
{
Suite* ts = suite_create("gcache::RbStore");
TCase* tc = tcase_create("test");
tcase_set_timeout(tc, 60);
tcase_add_test(tc, test1);
suite_add_tcase(ts, tc);
tc = tcase_create("recovery");
tcase_set_timeout(tc, 60);
tcase_add_test(tc, recovery);
suite_add_tcase(ts, tc);
return ts;
}
|