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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
|
// vim:sw=2:ai
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <stdlib.h>
#include <memory>
#include <errno.h>
#include <mysql.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "util.hpp"
#include "auto_ptrcontainer.hpp"
#include "socket.hpp"
#include "hstcpcli.hpp"
#include "string_util.hpp"
#include "mutex.hpp"
namespace dena {
struct auto_mysql : private noncopyable {
auto_mysql() : db(0) {
reset();
}
~auto_mysql() {
if (db) {
mysql_close(db);
}
}
void reset() {
if (db) {
mysql_close(db);
}
if ((db = mysql_init(0)) == 0) {
fatal_exit("failed to initialize mysql client");
}
}
operator MYSQL *() const { return db; }
private:
MYSQL *db;
};
struct auto_mysql_res : private noncopyable {
auto_mysql_res(MYSQL *db) {
res = mysql_store_result(db);
}
~auto_mysql_res() {
if (res) {
mysql_free_result(res);
}
}
operator MYSQL_RES *() const { return res; }
private:
MYSQL_RES *res;
};
struct auto_mysql_stmt : private noncopyable {
auto_mysql_stmt(MYSQL *db) {
stmt = mysql_stmt_init(db);
}
~auto_mysql_stmt() {
if (stmt) {
mysql_stmt_close(stmt);
}
}
operator MYSQL_STMT *() const { return stmt; }
private:
MYSQL_STMT *stmt;
};
double
gettimeofday_double()
{
struct timeval tv = { };
if (gettimeofday(&tv, 0) != 0) {
fatal_abort("gettimeofday");
}
return static_cast<double>(tv.tv_usec) / 1000000 + tv.tv_sec;
}
struct record_value {
mutex lock;
bool deleted;
bool unknown_state;
std::string key;
std::vector<std::string> values;
record_value() : deleted(true), unknown_state(false) { }
};
struct hs_longrun_shared {
config conf;
socket_args arg;
int verbose;
long num_threads;
int usleep;
volatile mutable int running;
auto_ptrcontainer< std::vector<record_value *> > records;
hs_longrun_shared() : verbose(0), num_threads(0), usleep(0), running(1) { }
};
struct thread_base {
thread_base() : need_join(false), stack_size(256 * 1024) { }
virtual ~thread_base() {
join();
}
virtual void run() = 0;
void start() {
if (!start_nothrow()) {
fatal_abort("thread::start");
}
}
bool start_nothrow() {
if (need_join) {
return need_join; /* true */
}
void *const arg = this;
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
fatal_abort("pthread_attr_init");
}
if (pthread_attr_setstacksize(&attr, stack_size) != 0) {
fatal_abort("pthread_attr_setstacksize");
}
const int r = pthread_create(&thr, &attr, thread_main, arg);
if (pthread_attr_destroy(&attr) != 0) {
fatal_abort("pthread_attr_destroy");
}
if (r != 0) {
return need_join; /* false */
}
need_join = true;
return need_join; /* true */
}
void join() {
if (!need_join) {
return;
}
int e = 0;
if ((e = pthread_join(thr, 0)) != 0) {
fatal_abort("pthread_join");
}
need_join = false;
}
private:
static void *thread_main(void *arg) {
thread_base *p = static_cast<thread_base *>(arg);
p->run();
return 0;
}
private:
pthread_t thr;
bool need_join;
size_t stack_size;
};
struct hs_longrun_stat {
unsigned long long verify_error_count;
unsigned long long runtime_error_count;
unsigned long long unknown_count;
unsigned long long success_count;
hs_longrun_stat()
: verify_error_count(0), runtime_error_count(0),
unknown_count(0), success_count(0) { }
void add(const hs_longrun_stat& x) {
verify_error_count += x.verify_error_count;
runtime_error_count += x.runtime_error_count;
unknown_count += x.unknown_count;
success_count += x.success_count;
}
};
struct hs_longrun_thread_base : public thread_base {
struct arg_type {
int id;
std::string worker_type;
char op;
int lock_flag;
const hs_longrun_shared& sh;
arg_type(int id, const std::string& worker_type, char op, int lock_flag,
const hs_longrun_shared& sh)
: id(id), worker_type(worker_type), op(op), lock_flag(lock_flag),
sh(sh) { }
};
arg_type arg;
hs_longrun_stat stat;
drand48_data randbuf;
unsigned int seed;
hs_longrun_thread_base(const arg_type& arg)
: arg(arg), seed(0) {
seed = time(0) + arg.id + 1;
srand48_r(seed, &randbuf);
}
virtual ~hs_longrun_thread_base() { }
virtual void run() = 0;
size_t rand_record() {
double v = 0;
drand48_r(&randbuf, &v);
const size_t sz = arg.sh.records.size();
size_t r = size_t(v * sz);
if (r >= sz) {
r = 0;
}
return r;
}
int verify_update(const std::string& k, const std::string& v1,
const std::string& v2, const std::string& v3, record_value& rec,
uint32_t num_rows, bool cur_unknown_state);
int verify_read(const std::string& k, uint32_t num_rows, uint32_t num_flds,
const std::string rrec[4], record_value& rec);
int verify_readnolock(const std::string& k, uint32_t num_rows,
uint32_t num_flds, const std::string rrec[4]);
};
int
hs_longrun_thread_base::verify_update(const std::string& k,
const std::string& v1, const std::string& v2, const std::string& v3,
record_value& rec, uint32_t num_rows, bool cur_unknown_state)
{
const bool op_success = num_rows == 1;
int ret = 0;
if (!rec.unknown_state) {
if (!rec.deleted && !op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_update_failure\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
} else if (rec.deleted && op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_update_success\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
}
}
if (op_success) {
rec.values.resize(4);
rec.values[0] = k;
rec.values[1] = v1;
rec.values[2] = v2;
rec.values[3] = v3;
if (ret == 0 && !rec.unknown_state) {
++stat.success_count;
}
}
rec.unknown_state = cur_unknown_state;
if (arg.sh.verbose >= 100 && ret == 0) {
fprintf(stderr, "%s %s %s %s %s\n", arg.worker_type.c_str(),
k.c_str(), v1.c_str(), v2.c_str(), v3.c_str());
}
return ret;
}
int
hs_longrun_thread_base::verify_read(const std::string& k,
uint32_t num_rows, uint32_t num_flds, const std::string rrec[4],
record_value& rec)
{
const bool op_success = num_rows != 0;
int ret = 0;
if (!rec.unknown_state) {
if (!rec.deleted && !op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_read_failure\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
} else if (rec.deleted && op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_read_success\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
} else if (num_flds != 4) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_read_fldnum %d\n",
arg.worker_type.c_str(), arg.id, k.c_str(),
static_cast<int>(num_flds));
}
ret = 1;
} else if (rec.deleted) {
/* nothing to verify */
} else {
int diff = 0;
for (size_t i = 0; i < 4; ++i) {
if (rec.values[i] == rrec[i]) {
/* ok */
} else {
diff = 1;
}
}
if (diff) {
std::string mess;
for (size_t i = 0; i < 4; ++i) {
const std::string& expected = rec.values[i];
const std::string& val = rrec[i];
mess += " " + val + "/" + expected;
}
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_read_value %s\n",
arg.worker_type.c_str(), arg.id, k.c_str(), mess.c_str());
}
ret = 1;
}
}
}
if (arg.sh.verbose >= 100 && ret == 0) {
fprintf(stderr, "%s %s\n", arg.worker_type.c_str(), k.c_str());
}
if (ret == 0 && !rec.unknown_state) {
++stat.success_count;
}
return ret;
}
int
hs_longrun_thread_base::verify_readnolock(const std::string& k,
uint32_t num_rows, uint32_t num_flds, const std::string rrec[4])
{
int ret = 0;
if (num_rows != 1 || num_flds != 4) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_read_failure\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
}
if (arg.sh.verbose >= 100 && ret == 0) {
fprintf(stderr, "%s -> %s %s %s %s %s\n", arg.worker_type.c_str(),
k.c_str(), rrec[0].c_str(), rrec[1].c_str(), rrec[2].c_str(),
rrec[3].c_str());
}
if (ret == 0) {
++stat.success_count;
}
return ret;
}
struct hs_longrun_thread_hs : public hs_longrun_thread_base {
hs_longrun_thread_hs(const arg_type& arg)
: hs_longrun_thread_base(arg) { }
void run();
int check_hs_error(const char *mess, record_value *rec);
int op_insert(record_value& rec);
int op_delete(record_value& rec);
int op_update(record_value& rec);
int op_read(record_value& rec);
int op_readnolock(int k);
hstcpcli_ptr cli;
socket_args sockargs;
};
struct lock_guard : noncopyable {
lock_guard(mutex& mtx) : mtx(mtx) {
mtx.lock();
}
~lock_guard() {
mtx.unlock();
}
mutex& mtx;
};
string_ref
to_string_ref(const std::string& s)
{
return string_ref(s.data(), s.size());
}
std::string
to_string(const string_ref& s)
{
return std::string(s.begin(), s.size());
}
void
hs_longrun_thread_hs::run()
{
config c = arg.sh.conf;
if (arg.op == 'R' || arg.op == 'N') {
c["port"] = to_stdstring(arg.sh.conf.get_int("hsport", 9998));
} else {
c["port"] = to_stdstring(arg.sh.conf.get_int("hsport_wr", 9999));
}
sockargs.set(c);
while (arg.sh.running) {
if (cli.get() == 0 || !cli->stable_point()) {
cli = hstcpcli_i::create(sockargs);
if (check_hs_error("connect", 0) != 0) {
cli.reset();
continue;
}
cli->request_buf_open_index(0, "hstestdb", "hstesttbl", "PRIMARY",
"k,v1,v2,v3", "k,v1,v2,v3");
cli->request_send();
if (check_hs_error("openindex_send", 0) != 0) {
cli.reset();
continue;
}
size_t num_flds = 0;
cli->response_recv(num_flds);
if (check_hs_error("openindex_recv", 0) != 0) {
cli.reset();
continue;
}
cli->response_buf_remove();
}
const size_t rec_id = rand_record();
if (arg.lock_flag) {
record_value& rec = *arg.sh.records[rec_id];
lock_guard g(rec.lock);
int e = 0;
switch (arg.op) {
case 'I':
e = op_insert(rec);
break;
case 'D':
e = op_delete(rec);
break;
case 'U':
e = op_update(rec);
break;
case 'R':
e = op_read(rec);
break;
default:
break;
}
} else {
int e = 0;
switch (arg.op) {
case 'N':
e = op_readnolock(rec_id);
break;
default:
break;
}
}
}
}
int
hs_longrun_thread_hs::op_insert(record_value& rec)
{
const std::string k = rec.key;
const std::string v1 = "iv1_" + k + "_" + to_stdstring(arg.id);
const std::string v2 = "iv2_" + k + "_" + to_stdstring(arg.id);
const std::string v3 = "iv3_" + k + "_" + to_stdstring(arg.id);
const string_ref op_ref("+", 1);
const string_ref op_args[4] = {
to_string_ref(k),
to_string_ref(v1),
to_string_ref(v2),
to_string_ref(v3)
};
cli->request_buf_exec_generic(0, op_ref, op_args, 4, 1, 0,
string_ref(), 0, 0, 0, 0);
cli->request_send();
if (check_hs_error("op_insert_send", &rec) != 0) { return 1; }
size_t numflds = 0;
cli->response_recv(numflds);
if (arg.sh.verbose > 10) {
const string_ref *row = cli->get_next_row();
fprintf(stderr, "HS op=+ errrcode=%d errmess=[%s]\n", cli->get_error_code(),
row ? to_string(row[0]).c_str() : "");
}
const bool op_success = cli->get_error_code() == 0;
int ret = 0;
if (!rec.unknown_state) {
if (rec.deleted && !op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_insert_failure\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
} else if (!rec.deleted && op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_insert_success\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
}
} else {
++stat.unknown_count;
}
if (op_success) {
rec.values.resize(4);
rec.values[0] = k;
rec.values[1] = v1;
rec.values[2] = v2;
rec.values[3] = v3;
rec.deleted = false;
if (arg.sh.verbose >= 100 && ret == 0) {
fprintf(stderr, "HS_INSERT %s %s %s %s\n", k.c_str(), v1.c_str(),
v2.c_str(), v3.c_str());
}
if (ret == 0 && !rec.unknown_state) {
++stat.success_count;
}
rec.unknown_state = false;
}
cli->response_buf_remove();
return ret;
}
int
hs_longrun_thread_hs::op_delete(record_value& rec)
{
const std::string k = rec.key;
const string_ref op_ref("=", 1);
const string_ref op_args[1] = {
to_string_ref(k),
};
const string_ref modop_ref("D", 1);
cli->request_buf_exec_generic(0, op_ref, op_args, 1, 1, 0,
modop_ref, 0, 0, 0, 0);
cli->request_send();
if (check_hs_error("op_delete_send", &rec) != 0) { return 1; }
size_t numflds = 0;
cli->response_recv(numflds);
if (check_hs_error("op_delete_recv", &rec) != 0) { return 1; }
const string_ref *row = cli->get_next_row();
const bool op_success = (numflds > 0 && row != 0 &&
to_string(row[0]) == "1");
int ret = 0;
if (!rec.unknown_state) {
if (!rec.deleted && !op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_delete_failure\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
} else if (rec.deleted && op_success) {
++stat.verify_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "VERIFY_ERROR: %s wid=%d k=%s "
"unexpected_delete_success\n",
arg.worker_type.c_str(), arg.id, k.c_str());
}
ret = 1;
}
}
cli->response_buf_remove();
if (op_success) {
rec.deleted = true;
if (ret == 0 && !rec.unknown_state) {
++stat.success_count;
}
rec.unknown_state = false;
}
if (arg.sh.verbose >= 100 && ret == 0) {
fprintf(stderr, "HS_DELETE %s\n", k.c_str());
}
return ret;
}
int
hs_longrun_thread_hs::op_update(record_value& rec)
{
const std::string k = rec.key;
const std::string v1 = "uv1_" + k + "_" + to_stdstring(arg.id);
const std::string v2 = "uv2_" + k + "_" + to_stdstring(arg.id);
const std::string v3 = "uv3_" + k + "_" + to_stdstring(arg.id);
const string_ref op_ref("=", 1);
const string_ref op_args[1] = {
to_string_ref(k),
};
const string_ref modop_ref("U", 1);
const string_ref modop_args[4] = {
to_string_ref(k),
to_string_ref(v1),
to_string_ref(v2),
to_string_ref(v3)
};
cli->request_buf_exec_generic(0, op_ref, op_args, 1, 1, 0,
modop_ref, modop_args, 4, 0, 0);
cli->request_send();
if (check_hs_error("op_update_send", &rec) != 0) { return 1; }
size_t numflds = 0;
cli->response_recv(numflds);
if (check_hs_error("op_update_recv", &rec) != 0) { return 1; }
const string_ref *row = cli->get_next_row();
uint32_t num_rows = row
? atoi_uint32_nocheck(row[0].begin(), row[0].end()) : 0;
cli->response_buf_remove();
const bool cur_unknown_state = (num_rows == 1);
return verify_update(k, v1, v2, v3, rec, num_rows, cur_unknown_state);
}
int
hs_longrun_thread_hs::op_read(record_value& rec)
{
const std::string k = rec.key;
const string_ref op_ref("=", 1);
const string_ref op_args[1] = {
to_string_ref(k),
};
cli->request_buf_exec_generic(0, op_ref, op_args, 1, 1, 0,
string_ref(), 0, 0, 0, 0);
cli->request_send();
if (check_hs_error("op_read_send", 0) != 0) { return 1; }
size_t num_flds = 0;
size_t num_rows = 0;
cli->response_recv(num_flds);
if (check_hs_error("op_read_recv", 0) != 0) { return 1; }
const string_ref *row = cli->get_next_row();
std::string rrec[4];
if (row != 0 && num_flds == 4) {
for (int i = 0; i < 4; ++i) {
rrec[i] = to_string(row[i]);
}
++num_rows;
}
row = cli->get_next_row();
if (row != 0) {
++num_rows;
}
cli->response_buf_remove();
return verify_read(k, num_rows, num_flds, rrec, rec);
}
int
hs_longrun_thread_hs::op_readnolock(int key)
{
const std::string k = to_stdstring(key);
const string_ref op_ref("=", 1);
const string_ref op_args[1] = {
to_string_ref(k),
};
cli->request_buf_exec_generic(0, op_ref, op_args, 1, 1, 0,
string_ref(), 0, 0, 0, 0);
cli->request_send();
if (check_hs_error("op_read_send", 0) != 0) { return 1; }
size_t num_flds = 0;
size_t num_rows = 0;
cli->response_recv(num_flds);
if (check_hs_error("op_read_recv", 0) != 0) { return 1; }
const string_ref *row = cli->get_next_row();
std::string rrec[4];
if (row != 0 && num_flds == 4) {
for (int i = 0; i < 4; ++i) {
rrec[i] = to_string(row[i]);
}
++num_rows;
}
row = cli->get_next_row();
if (row != 0) {
++num_rows;
}
cli->response_buf_remove();
return verify_readnolock(k, num_rows, num_flds, rrec);
}
int
hs_longrun_thread_hs::check_hs_error(const char *mess, record_value *rec)
{
const int err = cli->get_error_code();
if (err == 0) {
return 0;
}
++stat.runtime_error_count;
if (arg.sh.verbose > 0) {
const std::string estr = cli->get_error();
fprintf(stderr, "RUNTIME_ERROR: op=%c wid=%d %s: %d %s\n",
arg.op, arg.id, mess, err, estr.c_str());
}
if (rec) {
rec->unknown_state = true;
}
return 1;
}
struct hs_longrun_thread_my : public hs_longrun_thread_base {
hs_longrun_thread_my(const arg_type& arg)
: hs_longrun_thread_base(arg), connected(false) { }
void run();
void show_mysql_error(const char *mess, record_value *rec);
int op_insert(record_value& rec);
int op_delete(record_value& rec);
int op_update(record_value& rec);
int op_delins(record_value& rec);
int op_read(record_value& rec);
auto_mysql db;
bool connected;
};
void
hs_longrun_thread_my::run()
{
const std::string mysql_host = arg.sh.conf.get_str("host", "localhost");
const std::string mysql_user = arg.sh.conf.get_str("mysqluser", "root");
const std::string mysql_passwd = arg.sh.conf.get_str("mysqlpass", "");
const std::string mysql_dbname = "hstestdb";
while (arg.sh.running) {
if (!connected) {
if (!mysql_real_connect(db, mysql_host.c_str(), mysql_user.c_str(),
mysql_passwd.c_str(), mysql_dbname.c_str(), mysql_port, 0, 0)) {
show_mysql_error("mysql_real_connect", 0);
continue;
}
}
connected = true;
const size_t rec_id = rand_record();
record_value& rec = *arg.sh.records[rec_id];
lock_guard g(rec.lock);
int e = 0;
switch (arg.op) {
#if 0
case 'I':
e = op_insert(rec);
break;
case 'D':
e = op_delete(rec);
break;
case 'U':
e = op_update(rec);
break;
#endif
case 'T':
e = op_delins(rec);
break;
case 'R':
e = op_read(rec);
break;
default:
break;
}
}
}
int
hs_longrun_thread_my::op_delins(record_value& rec)
{
const std::string k = rec.key;
const std::string v1 = "div1_" + k + "_" + to_stdstring(arg.id);
const std::string v2 = "div2_" + k + "_" + to_stdstring(arg.id);
const std::string v3 = "div3_" + k + "_" + to_stdstring(arg.id);
int success = 0;
bool cur_unknown_state = false;
do {
char query[1024];
#if 1
if (mysql_query(db, "begin") != 0) {
if (arg.sh.verbose >= 20) {
fprintf(stderr, "mysql: e=[%s] q=[%s]\n", mysql_error(db), "begin");
}
break;
}
#endif
cur_unknown_state = true;
snprintf(query, 1024,
"delete from hstesttbl where k = '%s'", k.c_str());
if (mysql_query(db, query) != 0) {
if (arg.sh.verbose >= 20) {
fprintf(stderr, "mysql: e=[%s] q=[%s]\n", mysql_error(db), query);
}
break;
}
if (mysql_affected_rows(db) != 1) {
if (arg.sh.verbose >= 20) {
fprintf(stderr, "mysql: notfound: [%s]\n", query);
}
break;
}
snprintf(query, 1024,
"insert into hstesttbl values ('%s', '%s', '%s', '%s')",
k.c_str(), v1.c_str(), v2.c_str(), v3.c_str());
if (mysql_query(db, query) != 0) {
if (arg.sh.verbose >= 20) {
fprintf(stderr, "mysql: e=[%s] q=[%s]\n", mysql_error(db), query);
}
break;
}
#if 1
if (mysql_query(db, "commit") != 0) {
if (arg.sh.verbose >= 20) {
fprintf(stderr, "mysql: e=[%s] q=[%s]\n", mysql_error(db), "commit");
}
break;
}
#endif
success = true;
cur_unknown_state = false;
} while (false);
return verify_update(k, v1, v2, v3, rec, (success != 0), cur_unknown_state);
}
int
hs_longrun_thread_my::op_read(record_value& rec)
{
const std::string k = rec.key;
char query[1024] = { 0 };
const int len = snprintf(query, 1024,
"select k,v1,v2,v3 from hstesttbl where k='%s'", k.c_str());
const int r = mysql_real_query(db, query, len > 0 ? len : 0);
if (r != 0) {
show_mysql_error(query, 0);
return 1;
}
MYSQL_ROW row = 0;
unsigned long *lengths = 0;
unsigned int num_rows = 0;
unsigned int num_flds = 0;
auto_mysql_res res(db);
std::string rrec[4];
if (res != 0) {
num_flds = mysql_num_fields(res);
row = mysql_fetch_row(res);
if (row != 0) {
lengths = mysql_fetch_lengths(res);
if (num_flds == 4) {
for (int i = 0; i < 4; ++i) {
rrec[i] = std::string(row[i], lengths[i]);
}
}
++num_rows;
row = mysql_fetch_row(res);
if (row != 0) {
++num_rows;
}
}
}
return verify_read(k, num_rows, num_flds, rrec, rec);
}
void
hs_longrun_thread_my::show_mysql_error(const char *mess, record_value *rec)
{
++stat.runtime_error_count;
if (arg.sh.verbose > 0) {
fprintf(stderr, "RUNTIME_ERROR: op=%c wid=%d [%s]: %s\n",
arg.op, arg.id, mess, mysql_error(db));
}
if (rec) {
rec->unknown_state = true;
}
db.reset();
connected = false;
}
void
mysql_do(MYSQL *db, const char *query)
{
if (mysql_real_query(db, query, strlen(query)) != 0) {
fprintf(stderr, "mysql: e=[%s] q=[%s]\n", mysql_error(db), query);
fatal_exit("mysql_do");
}
}
void
hs_longrun_init_table(const config& conf, int num_prepare,
hs_longrun_shared& shared)
{
const std::string mysql_host = conf.get_str("host", "localhost");
const std::string mysql_user = conf.get_str("mysqluser", "root");
const std::string mysql_passwd = conf.get_str("mysqlpass", "");
const std::string mysql_dbname = "";
auto_mysql db;
if (!mysql_real_connect(db, mysql_host.c_str(), mysql_user.c_str(),
mysql_passwd.c_str(), mysql_dbname.c_str(), mysql_port, 0, 0)) {
fprintf(stderr, "mysql: error=[%s]\n", mysql_error(db));
fatal_exit("hs_longrun_init_table");
}
mysql_do(db, "drop database if exists hstestdb");
mysql_do(db, "create database hstestdb");
mysql_do(db, "use hstestdb");
mysql_do(db,
"create table hstesttbl ("
"k int primary key,"
"v1 varchar(32) not null,"
"v2 varchar(32) not null,"
"v3 varchar(32) not null"
") character set utf8 collate utf8_bin engine = innodb");
for (int i = 0; i < num_prepare; ++i) {
const std::string i_str = to_stdstring(i);
const std::string v1 = "pv1_" + i_str;
const std::string v2 = "pv2_" + i_str;
const std::string v3 = "pv3_" + i_str;
char buf[1024];
snprintf(buf, 1024, "insert into hstesttbl(k, v1, v2, v3) values"
"(%d, '%s', '%s', '%s')", i, v1.c_str(), v2.c_str(), v3.c_str());
mysql_do(db, buf);
record_value *rec = shared.records[i];
rec->key = i_str;
rec->values.resize(4);
rec->values[0] = i_str;
rec->values[1] = v1;
rec->values[2] = v2;
rec->values[3] = v3;
rec->deleted = false;
}
}
int
hs_longrun_main(int argc, char **argv)
{
hs_longrun_shared shared;
parse_args(argc, argv, shared.conf);
shared.conf["host"] = shared.conf.get_str("host", "localhost");
shared.verbose = shared.conf.get_int("verbose", 1);
const int table_size = shared.conf.get_int("table_size", 10000);
for (int i = 0; i < table_size; ++i) {
std::auto_ptr<record_value> rec(new record_value());
rec->key = to_stdstring(i);
shared.records.push_back_ptr(rec);
}
mysql_library_init(0, 0, 0);
const int duration = shared.conf.get_int("duration", 10);
const int num_hsinsert = shared.conf.get_int("num_hsinsert", 10);
const int num_hsdelete = shared.conf.get_int("num_hsdelete", 10);
const int num_hsupdate = shared.conf.get_int("num_hsupdate", 10);
const int num_hsread = shared.conf.get_int("num_hsread", 10);
const int num_myread = shared.conf.get_int("num_myread", 10);
const int num_mydelins = shared.conf.get_int("num_mydelins", 10);
int num_hsreadnolock = shared.conf.get_int("num_hsreadnolock", 10);
const bool always_filled = (num_hsinsert == 0 && num_hsdelete == 0);
if (!always_filled) {
num_hsreadnolock = 0;
}
hs_longrun_init_table(shared.conf, always_filled ? table_size : 0,
shared);
/* create worker threads */
static const struct thrtmpl_type {
const char *type; char op; int num; int hs; int lock;
} thrtmpl[] = {
{ "hsinsert", 'I', num_hsinsert, 1, 1 },
{ "hsdelete", 'D', num_hsdelete, 1, 1 },
{ "hsupdate", 'U', num_hsupdate, 1, 1 },
{ "hsread", 'R', num_hsread, 1, 1 },
{ "hsreadnolock", 'N', num_hsreadnolock, 1, 0 },
{ "myread", 'R', num_myread, 0, 1 },
{ "mydelins", 'T', num_mydelins, 0, 1 },
};
typedef auto_ptrcontainer< std::vector<hs_longrun_thread_base *> > thrs_type;
thrs_type thrs;
for (size_t i = 0; i < sizeof(thrtmpl)/sizeof(thrtmpl[0]); ++i) {
const thrtmpl_type& e = thrtmpl[i];
for (int j = 0; j < e.num; ++j) {
int id = thrs.size();
const hs_longrun_thread_hs::arg_type arg(id, e.type, e.op, e.lock,
shared);
std::auto_ptr<hs_longrun_thread_base> thr;
if (e.hs) {
thr.reset(new hs_longrun_thread_hs(arg));
} else {
thr.reset(new hs_longrun_thread_my(arg));
}
thrs.push_back_ptr(thr);
}
}
shared.num_threads = thrs.size();
/* start threads */
fprintf(stderr, "START\n");
shared.running = 1;
for (size_t i = 0; i < thrs.size(); ++i) {
thrs[i]->start();
}
/* wait */
sleep(duration);
/* stop thread */
shared.running = 0;
for (size_t i = 0; i < thrs.size(); ++i) {
thrs[i]->join();
}
fprintf(stderr, "DONE\n");
/* summary */
typedef std::map<std::string, hs_longrun_stat> stat_map;
stat_map sm;
for (size_t i = 0; i < thrs.size(); ++i) {
hs_longrun_thread_base *const thr = thrs[i];
const std::string wt = thr->arg.worker_type;
hs_longrun_stat& v = sm[wt];
v.add(thr->stat);
}
hs_longrun_stat total;
for (stat_map::const_iterator i = sm.begin(); i != sm.end(); ++i) {
if (i->second.verify_error_count != 0) {
fprintf(stderr, "%s verify_error %llu\n", i->first.c_str(),
i->second.verify_error_count);
}
if (i->second.runtime_error_count) {
fprintf(stderr, "%s runtime_error %llu\n", i->first.c_str(),
i->second.runtime_error_count);
}
if (i->second.unknown_count) {
fprintf(stderr, "%s unknown %llu\n", i->first.c_str(),
i->second.unknown_count);
}
fprintf(stderr, "%s success %llu\n", i->first.c_str(),
i->second.success_count);
total.add(i->second);
}
if (total.verify_error_count != 0) {
fprintf(stderr, "TOTAL verify_error %llu\n", total.verify_error_count);
}
if (total.runtime_error_count != 0) {
fprintf(stderr, "TOTAL runtime_error %llu\n", total.runtime_error_count);
}
if (total.unknown_count != 0) {
fprintf(stderr, "TOTAL unknown %llu\n", total.unknown_count);
}
fprintf(stderr, "TOTAL success %llu\n", total.success_count);
mysql_library_end();
return 0;
}
};
int
main(int argc, char **argv)
{
return dena::hs_longrun_main(argc, argv);
}
|