From a245b2f53162fb395211ce053bcda673905e026e Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Tue, 11 Dec 2018 23:22:56 +0300 Subject: [PATCH] OLTP scripts: add --reconnect option. oltp_*.lua scripts now support the --reconnect=N option. When specified, sysbench will reconnect after every N events. Fixes GH-90. --- src/drivers/pgsql/drv_pgsql.c | 18 ++++++++++++++++++ src/lua/oltp_common.lua | 14 ++++++++++++++ src/lua/oltp_delete.lua | 2 ++ src/lua/oltp_insert.lua | 2 ++ src/lua/oltp_point_select.lua | 2 ++ src/lua/oltp_read_only.lua | 2 ++ src/lua/oltp_read_write.lua | 2 ++ src/lua/oltp_update_index.lua | 1 + src/lua/oltp_update_non_index.lua | 2 ++ src/lua/oltp_write_only.lua | 2 ++ src/lua/select_random_points.lua | 2 ++ src/lua/select_random_ranges.lua | 2 ++ tests/include/script_oltp_common.sh | 9 +++++++++ tests/t/api_sql_pgsql.t | 3 +-- tests/t/script_oltp_delete_mysql.t | 2 ++ tests/t/script_oltp_delete_pgsql.t | 2 ++ tests/t/script_oltp_help.t | 1 + tests/t/script_oltp_insert_mysql.t | 2 ++ tests/t/script_oltp_insert_pgsql.t | 2 ++ tests/t/script_oltp_point_select_mysql.t | 2 ++ tests/t/script_oltp_point_select_pgsql.t | 2 ++ tests/t/script_oltp_read_write_mysql.t | 4 ++++ tests/t/script_oltp_read_write_pgsql.t | 2 ++ 23 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/drivers/pgsql/drv_pgsql.c b/src/drivers/pgsql/drv_pgsql.c index f7fceea..625fbf7 100644 --- a/src/drivers/pgsql/drv_pgsql.c +++ b/src/drivers/pgsql/drv_pgsql.c @@ -116,6 +116,7 @@ static int pgsql_drv_init(void); static int pgsql_drv_describe(drv_caps_t *); static int pgsql_drv_connect(db_conn_t *); static int pgsql_drv_disconnect(db_conn_t *); +static int pgsql_drv_reconnect(db_conn_t *); static int pgsql_drv_prepare(db_stmt_t *, const char *, size_t); static int pgsql_drv_bind_param(db_stmt_t *, db_bind_t *, size_t); static int pgsql_drv_bind_result(db_stmt_t *, db_bind_t *, size_t); @@ -141,6 +142,7 @@ static db_driver_t pgsql_driver = .describe = pgsql_drv_describe, .connect = pgsql_drv_connect, .disconnect = pgsql_drv_disconnect, + .reconnect = pgsql_drv_reconnect, .prepare = pgsql_drv_prepare, .bind_param = pgsql_drv_bind_param, .bind_result = pgsql_drv_bind_result, @@ -273,6 +275,22 @@ int pgsql_drv_disconnect(db_conn_t *sb_conn) return 0; } +/* Disconnect from database */ + +int pgsql_drv_reconnect(db_conn_t *sb_conn) +{ + if (pgsql_drv_disconnect(sb_conn)) + return DB_ERROR_FATAL; + + while (pgsql_drv_connect(sb_conn)) + { + if (sb_globals.error) + return DB_ERROR_FATAL; + } + + return DB_ERROR_IGNORABLE; +} + /* Prepare statement */ diff --git a/src/lua/oltp_common.lua b/src/lua/oltp_common.lua index 301bdf9..fdc5c83 100644 --- a/src/lua/oltp_common.lua +++ b/src/lua/oltp_common.lua @@ -68,6 +68,9 @@ sysbench.cmdline.options = { {"Use a secondary index in place of the PRIMARY KEY", false}, create_secondary = {"Create a secondary index in addition to the PRIMARY KEY", true}, + reconnect = + {"Reconnect after every N events. The default (0) is to not reconnect", + 0}, mysql_storage_engine = {"Storage engine, if MySQL is used", "innodb"}, pgsql_variant = @@ -505,3 +508,14 @@ function sysbench.hooks.before_restart_event(errdesc) prepare_statements() end end + +function check_reconnect() + if sysbench.opt.reconnect > 0 then + transactions = (transactions or 0) + 1 + if transactions % sysbench.opt.reconnect == 0 then + close_statements() + con:reconnect() + prepare_statements() + end + end +end diff --git a/src/lua/oltp_delete.lua b/src/lua/oltp_delete.lua index eae9c54..93ab5fa 100755 --- a/src/lua/oltp_delete.lua +++ b/src/lua/oltp_delete.lua @@ -31,4 +31,6 @@ function event() param[tnum].deletes[1]:set(id) stmt[tnum].deletes:execute() + + check_reconnect() end diff --git a/src/lua/oltp_insert.lua b/src/lua/oltp_insert.lua index ce99a45..3035d40 100755 --- a/src/lua/oltp_insert.lua +++ b/src/lua/oltp_insert.lua @@ -62,4 +62,6 @@ function event() "(%d, %d, '%s', '%s')", table_name, i, k_val, c_val, pad_val)) end + + check_reconnect() end diff --git a/src/lua/oltp_point_select.lua b/src/lua/oltp_point_select.lua index fccc0a4..f30e0c5 100755 --- a/src/lua/oltp_point_select.lua +++ b/src/lua/oltp_point_select.lua @@ -31,4 +31,6 @@ end function event() execute_point_selects() + + check_reconnect() end diff --git a/src/lua/oltp_read_only.lua b/src/lua/oltp_read_only.lua index 86c0fc8..ee7ca37 100755 --- a/src/lua/oltp_read_only.lua +++ b/src/lua/oltp_read_only.lua @@ -54,4 +54,6 @@ function event() if not sysbench.opt.skip_trx then commit() end + + check_reconnect() end diff --git a/src/lua/oltp_read_write.lua b/src/lua/oltp_read_write.lua index f5f05ce..9b5cf6e 100755 --- a/src/lua/oltp_read_write.lua +++ b/src/lua/oltp_read_write.lua @@ -62,4 +62,6 @@ function event() if not sysbench.opt.skip_trx then commit() end + + check_reconnect() end diff --git a/src/lua/oltp_update_index.lua b/src/lua/oltp_update_index.lua index cafb4f5..aab4973 100755 --- a/src/lua/oltp_update_index.lua +++ b/src/lua/oltp_update_index.lua @@ -27,4 +27,5 @@ end function event() execute_index_updates(con) + check_reconnect() end diff --git a/src/lua/oltp_update_non_index.lua b/src/lua/oltp_update_non_index.lua index 78d7c06..fbe9f42 100755 --- a/src/lua/oltp_update_non_index.lua +++ b/src/lua/oltp_update_non_index.lua @@ -27,4 +27,6 @@ end function event() execute_non_index_updates() + + check_reconnect() end diff --git a/src/lua/oltp_write_only.lua b/src/lua/oltp_write_only.lua index ddfd156..9e5c7e1 100755 --- a/src/lua/oltp_write_only.lua +++ b/src/lua/oltp_write_only.lua @@ -44,4 +44,6 @@ function event() if not sysbench.opt.skip_trx then commit() end + + check_reconnect() end diff --git a/src/lua/select_random_points.lua b/src/lua/select_random_points.lua index 827a14a..0afe2c9 100755 --- a/src/lua/select_random_points.lua +++ b/src/lua/select_random_points.lua @@ -69,4 +69,6 @@ function event() end stmt:execute() + + check_reconnect() end diff --git a/src/lua/select_random_ranges.lua b/src/lua/select_random_ranges.lua index a941eda..bea8b97 100755 --- a/src/lua/select_random_ranges.lua +++ b/src/lua/select_random_ranges.lua @@ -74,4 +74,6 @@ function event() end stmt:execute() + + check_reconnect() end diff --git a/tests/include/script_oltp_common.sh b/tests/include/script_oltp_common.sh index ee5b704..9297c54 100644 --- a/tests/include/script_oltp_common.sh +++ b/tests/include/script_oltp_common.sh @@ -84,3 +84,12 @@ ARGS="$ARGS --auto-inc=off --verbosity=1" sysbench $ARGS prepare sysbench $ARGS run sysbench $ARGS cleanup + +echo "# Test --reconnect" + +ARGS="${OLTP_SCRIPT_PATH} ${DB_DRIVER_ARGS} ${SB_EXTRA_ARGS} --events=100 \ +--reconnect=5" + +sysbench $ARGS prepare >/dev/null || true +sysbench $ARGS run | grep reconnects: +sysbench $ARGS cleanup >/dev/null || true diff --git a/tests/t/api_sql_pgsql.t b/tests/t/api_sql_pgsql.t index 64bc820..93aa760 100644 --- a/tests/t/api_sql_pgsql.t +++ b/tests/t/api_sql_pgsql.t @@ -66,10 +66,9 @@ SQL Lua API + PostgreSQL tests 301 400 0123456789 0123456789 -- 1 - ALERT: reconnect is not supported by the current driver 2 -- - reconnects = 0 + reconnects = 1 FATAL: Connection to database failed: could not translate host name "non-existing" to address: * (glob) connection creation failed diff --git a/tests/t/script_oltp_delete_mysql.t b/tests/t/script_oltp_delete_mysql.t index fa65748..12e9ccf 100644 --- a/tests/t/script_oltp_delete_mysql.t +++ b/tests/t/script_oltp_delete_mysql.t @@ -281,3 +281,5 @@ oltp_delete.lua + MySQL tests Inserting 10000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) diff --git a/tests/t/script_oltp_delete_pgsql.t b/tests/t/script_oltp_delete_pgsql.t index f226b7b..2ef680b 100644 --- a/tests/t/script_oltp_delete_pgsql.t +++ b/tests/t/script_oltp_delete_pgsql.t @@ -308,3 +308,5 @@ oltp_delete.lua + PostgreSQL tests Inserting 10000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) diff --git a/tests/t/script_oltp_help.t b/tests/t/script_oltp_help.t index ef79a38..c29f853 100644 --- a/tests/t/script_oltp_help.t +++ b/tests/t/script_oltp_help.t @@ -19,6 +19,7 @@ OLTP usage information test --point_selects=N Number of point SELECT queries per transaction [10] --range_selects[=on|off] Enable/disable all range SELECT queries [on] --range_size=N Range size for range SELECT queries [100] + --reconnect=N Reconnect after every N events. The default (0) is to not reconnect [0] --secondary[=on|off] Use a secondary index in place of the PRIMARY KEY [off] --simple_ranges=N Number of simple range SELECT queries per transaction [1] --skip_trx[=on|off] Don't start explicit transactions and execute all queries in the AUTOCOMMIT mode [off] diff --git a/tests/t/script_oltp_insert_mysql.t b/tests/t/script_oltp_insert_mysql.t index c93d10f..7fb6fa0 100644 --- a/tests/t/script_oltp_insert_mysql.t +++ b/tests/t/script_oltp_insert_mysql.t @@ -280,3 +280,5 @@ oltp_insert.lua + MySQL tests Creating table 'sbtest1'... Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) diff --git a/tests/t/script_oltp_insert_pgsql.t b/tests/t/script_oltp_insert_pgsql.t index 85a07f7..4f4fffd 100644 --- a/tests/t/script_oltp_insert_pgsql.t +++ b/tests/t/script_oltp_insert_pgsql.t @@ -307,3 +307,5 @@ oltp_insert.lua + PostgreSQL tests Creating table 'sbtest1'... Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) diff --git a/tests/t/script_oltp_point_select_mysql.t b/tests/t/script_oltp_point_select_mysql.t index 0d0a3b2..ea0fd21 100644 --- a/tests/t/script_oltp_point_select_mysql.t +++ b/tests/t/script_oltp_point_select_mysql.t @@ -281,3 +281,5 @@ oltp_point_select.lua + MySQL tests Inserting 10000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) diff --git a/tests/t/script_oltp_point_select_pgsql.t b/tests/t/script_oltp_point_select_pgsql.t index e993f38..2bf8a9c 100644 --- a/tests/t/script_oltp_point_select_pgsql.t +++ b/tests/t/script_oltp_point_select_pgsql.t @@ -308,3 +308,5 @@ oltp_point_select.lua + PostgreSQL tests Inserting 10000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) diff --git a/tests/t/script_oltp_read_write_mysql.t b/tests/t/script_oltp_read_write_mysql.t index fa4eac8..82d9933 100644 --- a/tests/t/script_oltp_read_write_mysql.t +++ b/tests/t/script_oltp_read_write_mysql.t @@ -285,6 +285,8 @@ oltp_read_write.lua + MySQL tests Inserting 10000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) $ DB_DRIVER_ARGS="--db-driver=mysql --mysql-storage-engine=innodb $SBTEST_MYSQL_ARGS" @@ -565,3 +567,5 @@ oltp_read_write.lua + MySQL tests Inserting 10000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob) diff --git a/tests/t/script_oltp_read_write_pgsql.t b/tests/t/script_oltp_read_write_pgsql.t index 3c29835..e05ed6c 100644 --- a/tests/t/script_oltp_read_write_pgsql.t +++ b/tests/t/script_oltp_read_write_pgsql.t @@ -311,3 +311,5 @@ oltp_read_write.lua + PostgreSQL tests Inserting 10000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Dropping table 'sbtest1'... + # Test --reconnect + reconnects: 20 (* per sec.) (glob)