From db11338e57d614e71e716d9e655a1bb8033ffa99 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 29 Dec 2004 20:30:37 +0300 Subject: WL#1339 "Add per account max_user_connections limit (maximum number of concurrent connections for the same account)" Added support of account specific max_user_connections limit. Made all user limits to be counted per account instead of the old behavior, which was per user/host accounting. Added option which enables the old behavior. Added testing of these to the test suite. (After review version). client/mysqltest.c: Extended mysqltest to be able to handle connect() statements for which error is expected. - Added replace_dynstr_append() utility function. - added connect_n_handle_errors() function which connects with server without retries and handles errors as if "connect" was usual statement. - do_connect(): added ability to handle connects which are expected to return an error. - run_query_normal(): Moved all expected-error-handling code to separate normal_handle_error()/normal_handle_no_error() functions to be able to reuse them in connect_n_handle_errors(). mysql-test/r/grant.result: Fixed test results since one more column to mysql.user was added. mysql-test/r/system_mysql_db.result: Fixed test results since one more column to mysql.user was added. scripts/mysql_create_system_tables.sh: Added max_user_connections column to mysql.user table for storing new maximal concurrent connections per account limit. scripts/mysql_fix_privilege_tables.sql: Added max_user_connections column to mysql.user table for storing new maximal concurrent connections per account limit. sql/lex.h: Added MAX_USER_CONNECTIONS symbol used for specifying maximum number of concurrent connections per account. sql/mysql_priv.h: Added declaration of opt_old_style_user_limits variable which is defined in sql/mysqld.cc used in sql/sql_parse.cc. sql/mysqld.cc: Added "old-style-user-limits" option which forces user limits to behave in old way i.e. to be counted per user/host pair instead of per account. Added comment describing mqh_used variable. sql/set_var.cc: Added sys_var_max_user_conn class which implements support for the new behavior of max_user_connections variable. Now the global instance of this variable holds default maximum number of concurrent connections per account (as it was before) and the session instance gives read-only access to account-specific version of this limit. sql/set_var.h: Added sys_var_max_user_conn class which implements support for the new behavior of max_user_connections variable. Now the global instance of this variable holds default maximum number of concurrent connections per account (as it was before) and the session instance gives read-only access to account-specific version of this limit. sql/sql_acl.cc: Added support for account-specific MAX_USER_CONNECTIONS limit. Tweaked USER_RESOURCES and their handling for better clarity. sql/sql_parse.cc: Added support for account-specific MAX_USER_CONNECTIONS (maximum number of concurrent connections per account) limit. Changed default behavior of all user limits to be per account instead of per user+host. '--old-style-user-limits' option was added to enable the old behavior. Made maximum number of connections per hour to be independant on the value of global max_user_connections variable. sql/sql_yacc.yy: Added support of new MAX_USER_CONNECTIONS limit to grammar. Renamed USER_RESOURCES::connections member to conn_per_hour and bits member to specified_limits. Also enum is used instead of naked numbers when we are working with specified_limits. sql/structs.h: USER_RESOURCES struct: - Added user_conn member to store the maximum number of concurrent connections for an account. Renamed connections member to conn_per_hour for less ambiguity. - Renamed member 'bits' to 'specified_limits' for the sake of clarity. The member was used as a flag indicating which limits were mentioned in GRANT clause. - Added comments. USER_CONN struct: - Removed unused user_len member. - Added comments. --- sql/structs.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'sql/structs.h') diff --git a/sql/structs.h b/sql/structs.h index c02f8c502a2..dc7c00c11d1 100644 --- a/sql/structs.h +++ b/sql/structs.h @@ -205,16 +205,65 @@ typedef struct st_lex_user { } LEX_USER; +/* + This structure specifies the maximum amount of resources which + can be consumed by each account. Zero value of a member means + there is no limit. +*/ typedef struct user_resources { - uint questions, updates, connections, bits; + /* Maximum number of queries/statements per hour. */ + uint questions; + /* + Maximum number of updating statements per hour (which statements are + updating is defined by uc_update_queries array). + */ + uint updates; + /* Maximum number of connections established per hour. */ + uint conn_per_hour; + /* Maximum number of concurrent connections. */ + uint user_conn; + /* + Values of this enum and specified_limits member are used by the + parser to store which user limits were specified in GRANT statement. + */ + enum {QUERIES_PER_HOUR= 1, UPDATES_PER_HOUR= 2, CONNECTIONS_PER_HOUR= 4, + USER_CONNECTIONS= 8}; + uint specified_limits; } USER_RESOURCES; + +/* + This structure is used for counting resources consumed and for checking + them against specified user limits. +*/ typedef struct user_conn { - char *user, *host; - uint len, connections, conn_per_hour, updates, questions, user_len; + /* + Pointer to user+host key (pair separated by '\0') defining the entity + for which resources are counted (By default it is user account thus + priv_user/priv_host pair is used. If --old-style-user-limits option + is enabled, resources are counted for each user+host separately). + */ + char *user; + /* Pointer to host part of the key. */ + char *host; + /* Total length of the key. */ + uint len; + /* Current amount of concurrent connections for this account. */ + uint connections; + /* + Current number of connections per hour, number of updating statements + per hour and total number of statements per hour for this account. + */ + uint conn_per_hour, updates, questions; + /* Maximum amount of resources which account is allowed to consume. */ USER_RESOURCES user_resources; + /* + The moment of time when per hour counters were reset last time + (i.e. start of "hour" for conn_per_hour, updates, questions counters). + */ time_t intime; } USER_CONN; + /* Bits in form->update */ #define REG_MAKE_DUPP 1 /* Make a copy of record when read */ #define REG_NEW_RECORD 2 /* Write a new record if not found */ -- cgit v1.2.1