Skip to content

Commit

Permalink
SNOW-878083: add connection parameters for retry strategy (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Xi authored Oct 27, 2023
1 parent a1e4947 commit 482c0f1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
2 changes: 2 additions & 0 deletions php_pdo_snowflake_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@ enum {
#define PDO_SNOWFLAKE_CONN_ATTR_NO_PROXY_IDX 16
#define PDO_SNOWFLAKE_CONN_ATTR_DISABLE_QUERY_CONTEXT_CACHE_IDX 17
#define PDO_SNOWFLAKE_CONN_ATTR_INCLUDE_RETRY_REASON_IDX 18
#define PDO_SNOWFLAKE_CONN_ATTR_LOGIN_TIMEOUT_IDX 19
#define PDO_SNOWFLAKE_CONN_ATTR_MAX_LOGIN_RETRIES_IDX 20

#endif /* PHP_PDO_SNOWFLAKE_INT_H */
25 changes: 23 additions & 2 deletions snowflake_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ pdo_snowflake_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
pdo_snowflake_db_handle *H;
size_t i;
int ret = 0;
int64 int_attr_value = 0;
int64 int_attr_value = 0;
int8 int8_attr_value = 0;
/* NOTE: the parameters are referenced by index, so if you change
* the order of parameters, ensure changing the index of vars
* in php_pdo_snowflake_int.h
Expand All @@ -581,7 +582,9 @@ pdo_snowflake_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
{"proxy", NULL, 0},
{"no_proxy", NULL, 0},
{"disablequerycontextcache", "false", 0},
{"includeretryreason", "true", 0}
{"includeretryreason", "true", 0},
{"login_timeout", "300", 0},
{"max_login_retries", "7", 0}
};

// Parse the input data parameters
Expand Down Expand Up @@ -786,6 +789,24 @@ pdo_snowflake_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
"includeretryreason: %s",
vars[PDO_SNOWFLAKE_CONN_ATTR_INCLUDE_RETRY_REASON_IDX].optval);

if (vars[PDO_SNOWFLAKE_CONN_ATTR_LOGIN_TIMEOUT_IDX].optval != NULL) {
int_attr_value = strtoll(vars[PDO_SNOWFLAKE_CONN_ATTR_LOGIN_TIMEOUT_IDX].optval, NULL, 10);
snowflake_set_attribute(
H->server, SF_CON_LOGIN_TIMEOUT,
&int_attr_value);
PDO_LOG_DBG(
"login_timeout: %d", int_attr_value);
}

if (vars[PDO_SNOWFLAKE_CONN_ATTR_MAX_LOGIN_RETRIES_IDX].optval != NULL) {
int8_attr_value = strtol(vars[PDO_SNOWFLAKE_CONN_ATTR_MAX_LOGIN_RETRIES_IDX].optval, NULL, 10);
snowflake_set_attribute(
H->server, SF_CON_MAX_CON_RETRY,
&int8_attr_value);
PDO_LOG_DBG(
"max_login_retries: %d", int8_attr_value);
}

if (snowflake_connect(H->server) > 0) {
pdo_snowflake_error(dbh);
goto cleanup;
Expand Down
2 changes: 1 addition & 1 deletion tests/connect.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pdo_snowflake.cacert=libsnowflakeclient/cacert.pem
include __DIR__ . "/common.php";

// full parameters
$dbh = new PDO("$dsn;application=phptest;authenticator=snowflake;priv_key_file=tests/p8test.pem;priv_key_file_pwd=test;disablequerycontext=true;includeretryreason=false", $user, $password);
$dbh = new PDO("$dsn;application=phptest;authenticator=snowflake;priv_key_file=tests/p8test.pem;priv_key_file_pwd=test;disablequerycontext=true;includeretryreason=false;login_timeout=350;max_login_retries=8", $user, $password);
// create table for testing autocommit later
$tablename = "autocommittest" . rand();
$count = $dbh->exec("create or replace table " . $tablename . "(c1 int)");
Expand Down
44 changes: 28 additions & 16 deletions tests/htap.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@ pdo_snowflake.loglevel=TRACE
echo 'Connected to Snowflake' . "\n";
// run queries on Windows only as the test account on Linux/Mac
// don't have the necessary privileges
// have random number in db name to avoid conflict between test cases
$randnum = rand();
$db1 = "db1$randnum";
$db2 = "db2$randnum";
$db3 = "db3$randnum";

if (PHP_OS_FAMILY === "Windows")
{
$sth = $dbh->query("create or replace database db1");
$sth = $dbh->query("create or replace database $db1");
$sth = $dbh->query("create or replace hybrid table t1 (a int primary key, b int)");
$sth = $dbh->query("insert into t1 values (1, 2), (2, 3), (3, 4)");
$sth = $dbh->query("create or replace database db2");
$sth = $dbh->query("create or replace database $db2");
$sth = $dbh->query("create or replace hybrid table t2 (a int primary key, b int)");
$sth = $dbh->query("insert into t2 values (1, 3), (2, 2), (3, 4)");
$sth = $dbh->query("create or replace database db3");
$sth = $dbh->query("create or replace database $db3");
$sth = $dbh->query("create or replace hybrid table t3 (a int primary key, b int)");
$sth = $dbh->query("insert into t3 values (1, 3), (2, 2), (3, 4)");
$sth = $dbh->query("select * from db1.public.t1 x, db2.public.t2 y, db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("insert into db2.public.t2 (select y.a*100*z.a, y.b*15*z.b from db1.public.t1 y, db3.public.t3 z where y.a=z.a);");
$sth = $dbh->query("select * from db1.public.t1 x, db2.public.t2 y, db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("select * from db1.public.t1 x, db2.public.t2 y where x.a = y.a;");
$sth = $dbh->query("select * from db2.public.t2 y, db3.public.t3 z where y.a = z.a;");
$sth = $dbh->query("select * from $db1.public.t1 x, $db2.public.t2 y, $db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("insert into $db2.public.t2 (select y.a*100*z.a, y.b*15*z.b from $db1.public.t1 y, $db3.public.t3 z where y.a=z.a);");
$sth = $dbh->query("select * from $db1.public.t1 x, $db2.public.t2 y, $db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("select * from $db1.public.t1 x, $db2.public.t2 y where x.a = y.a;");
$sth = $dbh->query("select * from $db2.public.t2 y, $db3.public.t3 z where y.a = z.a;");
$sth = $dbh->query("drop database $db1");
$sth = $dbh->query("drop database $db2");
$sth = $dbh->query("drop database $db3");
}
$dbh = null;

Expand All @@ -39,20 +48,23 @@ pdo_snowflake.loglevel=TRACE
echo 'Connected to Snowflake' . "\n";
if (PHP_OS_FAMILY === "Windows")
{
$sth = $dbh->query("create or replace database db1");
$sth = $dbh->query("create or replace database $db1");
$sth = $dbh->query("create or replace hybrid table t1 (a int primary key, b int)");
$sth = $dbh->query("insert into t1 values (1, 2), (2, 3), (3, 4)");
$sth = $dbh->query("create or replace database db2");
$sth = $dbh->query("create or replace database $db2");
$sth = $dbh->query("create or replace hybrid table t2 (a int primary key, b int)");
$sth = $dbh->query("insert into t2 values (1, 3), (2, 2), (3, 4)");
$sth = $dbh->query("create or replace database db3");
$sth = $dbh->query("create or replace database $db3");
$sth = $dbh->query("create or replace hybrid table t3 (a int primary key, b int)");
$sth = $dbh->query("insert into t3 values (1, 3), (2, 2), (3, 4)");
$sth = $dbh->query("select * from db1.public.t1 x, db2.public.t2 y, db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("insert into db2.public.t2 (select y.a*100*z.a, y.b*15*z.b from db1.public.t1 y, db3.public.t3 z where y.a=z.a);");
$sth = $dbh->query("select * from db1.public.t1 x, db2.public.t2 y, db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("select * from db1.public.t1 x, db2.public.t2 y where x.a = y.a;");
$sth = $dbh->query("select * from db2.public.t2 y, db3.public.t3 z where y.a = z.a;");
$sth = $dbh->query("select * from $db1.public.t1 x, $db2.public.t2 y, $db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("insert into $db2.public.t2 (select y.a*100*z.a, y.b*15*z.b from $db1.public.t1 y, $db3.public.t3 z where y.a=z.a);");
$sth = $dbh->query("select * from $db1.public.t1 x, $db2.public.t2 y, $db3.public.t3 z where x.a = y.a and y.a = z.a;");
$sth = $dbh->query("select * from $db1.public.t1 x, $db2.public.t2 y where x.a = y.a;");
$sth = $dbh->query("select * from $db2.public.t2 y, $db3.public.t3 z where y.a = z.a;");
$sth = $dbh->query("drop database $db1");
$sth = $dbh->query("drop database $db2");
$sth = $dbh->query("drop database $db3");
}
$dbh = null;
?>
Expand Down

0 comments on commit 482c0f1

Please sign in to comment.