【15.7.4.2. Internal Structures】
在 MySQL Proxy 的脚本元素中有一些基本的内部结构须要知道。其中最主要的结构就是 proxy ,其提供了访问贯穿脚本中的许多公共结构的接口,例如链接列表和配置的 backend server 。其余结构,例如来自客户端的包和返回的结果集等,只有在具体的脚本函数的上下文环境中才是能够访问的。
下表中描述了 MySQL proxy 脚本元素的公共属性。
Attribute |
Description |
connection |
A structure containing the active client connections. For a list of attributes, seeproxy.connection. |
servers |
A structure containing the list of configured backend servers. For a list of attributes, seeproxy.global.backends. |
queries |
A structure containing the queue of queries that will be sent to the server during a single client query. For a list of attributes, see proxy.queries. |
PROXY_VERSION |
The version number of MySQL Proxy, encoded in hex. You can use this to check that the version number supports a particular option from within the Lua script. Note that the value is encoded as a hex value, so to check the version is at least 0.5.1 you compare against0x00501. |
proxy.connection
proxy.connection 对象是只读的,提供了关于当前链接的信息,其内容被分红了客户端和服务器两张表。这样就容许你能够既检查来自于客户端到 proxy 的链接信息(保存在client中),又能够检查 proxy 到服务器的链接信息(保存在server中)。
下表描述了 proxy.connection 对象中客户端和服务器属性。
Attribute |
Description |
client.default_db |
Default database requested by the client |
client.username |
User name used to authenticate |
client.scrambled_password |
The scrambled version of the password used to authenticate |
client.dst.name |
The combined address:port of the Proxy port used by this client (should match the --proxy-address configuration parameter) |
client.dst.address |
The IP address of the of the Proxy port used by this client |
client.dst.port |
The port number of the of the Proxy port used by this client |
client.src.name |
The combined address:port of the client (originating) TCP/IP endpoint |
client.src.address |
The IP address of the client (originating) TCP/IP port |
client.src.port |
The port of the client (originating) TCP/IP endpoint |
server.scramble_buffer |
The scramble buffer used to scramble the password |
server.mysqld_version |
The MySQL version number of the server |
server.thread_id |
The ID of the thread handling the connection to the current server |
server.dst.name |
The combined address:port for the backend server for the current connection (i.e. the connection to the MySQL server) |
server.dst.address |
The address for the backend server |
server.dst.port |
The port for the backend server |
server.src.name |
The combined address:port for the TCP/IP endpoint used by the Proxy to connect to the backend server |
server.src.address |
The address of the endpoint for the proxy-side connection to the MySQL server |
server.src.port |
The port of the endpoint for the proxy-side connection to the MySQL server |
proxy.global.backends
proxy.global.backends 表示部分可写的,而且其以数组的形式包含了全部的已配置 backend server 和服务器元数据(IP地址,状态等)的信息。你能够经过 proxy.connection["backend_ndx"] 的方式指定当前链接的数组索引值,backend_ndx 是被有效链接使用了的 backend server 表的索引值。
下表描述了表 proxy.global.backends 中的每个入口的属性值。
Attribute |
Description |
dst.name |
The combined address:port of the backend server. |
dst.address |
The IP address of the backend server. |
dst.port |
The port of the backend server. |
connected_clients |
The number of clients currently connected. |
state |
The status of the backend server. See Backend State/Type Constants. |
type |
The type of the backend server. You can use this to identify whether the backed was configured as a standard read/write backend, or a read-only backend. You can compare this value to the proxy.BACKEND_TYPE_RW andproxy.BACKEND_TYPE_RO. |
proxy.queries
proxy.queries 对象是一个队列,用于存储将要发送到服务器的的 query 列表。该队列不会被自动填入(populated),故若是你不显式地向该队列填入内容,query 将会被不作任何修改的发送到 backend server,一样,若是你不手动将 query 添加到队列中,read_query_result() 函数将不会被触发。
下面的函数能够用于对 proxy.queries 对象进行操做。
Function |
Description |
append(id,packet,[options]) |
Appends a query to the end of the query queue. The id is an integer identifier that you can use to recognize the query results when they are returned by the server. The packet should be a properly formatted query packet. The optional options should be a table containing the options specific to this packet. |
prepend(id,packet) |
Prepends a query to the query queue. The id is an identifier that you can use to recognize the query results when they are returned by the server. The packet should be a properly formatted query packet. |
reset() |
Empties the query queue. |
len() |
Returns the number of query packets in the queue. |
例如,你能够经过使用函数 append() 将一个 query 包附加到 proxy.queries 队列的尾部:
proxy.queries:append(1,packet)
函数 append() 的可选的第三个参数应该包含针对当前包的一些选项设置。为了可以在函数 read_query_result() 中访问结果集,须要设置选项 resultset_is_needed 为 true :
proxy.queries:append( 1, packet, { resultset_is_needed = true } )
若是该选项设置为 false(默认设置),proxy 将:
- 在收到结果集时马上将其发送给客户端
- 减小内存使用(由于结果集不须要为处理在proxy内部存储)
- 减小返回结果给客户端的延迟
- 从服务器到客户端的传输数据不做改变
由此可知,若是你仅仅想要监控被发送的 query 和基本的统计信息,那么在默认模式下速度会更快,也更要求。
为了在返回数据上进行任何形式的操做,都必须将 resultset_is_needed 设置为 true ,设置后:
- proxy 会存储结果集以备后续处理使用
- 使能将结果集返回给客户端前能够进行修改的能力
- 使能将结果集返回给客户端前能够丢弃结果集的能力
proxy.response
proxy.response 结构被用于在你打算返回你本身的 MySQL response 的状况下,而不返回给客户端来自服务器的应答包。该结构中包含了 response 的类型信息,一个可选的错误信息,以及(行/列)结果集。
下表描述了 proxy.response 结构的属性值。
Attribute |
Description |
type |
The type of the response. The type must be either MYSQLD_PACKET_OK orMYSQLD_PACKET_ERR. If the MYSQLD_PACKET_ERR, you should set the value of themysql.response.errmsg with a suitable error message. |
errmsg |
A string containing the error message that will be returned to the client. |
resultset |
A structure containing the result set information (columns and rows), identical to what would be returned when returning a results from a SELECT query. |
当使用 proxy.response 时,或者你设置 proxy.response.type 的值为 proxy.MYSQLD_PACKET_OK ,并构建包含要返回的结果的自定义结果集;或者你设置 proxy.response.type 的值为 proxy.MYSQLD_PACKET_ERR ,并设置 proxy.response.errmsg 值为表示错误消息内容的字符串。为了发送完整的结果集或者错误消息,你应该返回 proxy.PROXY_SEND_RESULT 以触发在函数中构建内容的返回。
上述说明的一个具体例子能够参阅 MySQL Proxy 源码包中 tutorial-resultset.lua 脚本:
if string.lower(command) == "show" and string.lower(option) == "querycounter" then
---
-- proxy.PROXY_SEND_RESULT requires
--
-- proxy.response.type to be either
-- * proxy.MYSQLD_PACKET_OK or
-- * proxy.MYSQLD_PACKET_ERR
--
-- for proxy.MYSQLD_PACKET_OK you need a resultset
-- * fields
-- * rows
--
-- for proxy.MYSQLD_PACKET_ERR
-- * errmsg
proxy.response.type = proxy.MYSQLD_PACKET_OK
proxy.response.resultset = {
fields = {
{ type = proxy.MYSQL_TYPE_LONG, name = "global_query_counter", },
{ type = proxy.MYSQL_TYPE_LONG, name = "query_counter", },
},
rows = {
{ proxy.global.query_counter, query_counter }
}
}
-- we have our result, send it back
return proxy.PROXY_SEND_RESULT
elseif string.lower(command) == "show" and string.lower(option) == "myerror" then
proxy.response.type = proxy.MYSQLD_PACKET_ERR
proxy.response.errmsg = "my first error"
return proxy.PROXY_SEND_RESULT
proxy.response.resultset
proxy.response.resultset 结构应该被填入返回数据的 row 和 column 信息。该结构中包含了整个结果集的信息,其中涉及到的单独的元素在下面表述。
下表中描述了 proxy.response.resultset 结构的属性值。
Attribute |
Description |
fields |
The definition of the columns being returned. This should be a dictionary structure with thetype specifying the MySQL data type, and the name specifying the column name. Columns should be listed in the order of the column data that will be returned. |
flags |
A number of flags related to the result set. Valid flags include auto_commit (whether an automatic commit was triggered), no_good_index_used (the query executed without using an appropriate index), and no_index_used (the query executed without using any index). |
rows |
The actual row data. The information should be returned as an array of arrays. Each inner array should contain the column data, with the outer array making up the entire result set. |
warning_count |
The number of warnings for this result set. |
affected_rows |
The number of rows affected by the original statement. |
insert_id |
The last insert ID for an auto-incremented column in a table. |
query_status |
The status of the query operation. You can use the MYSQLD_PACKET_OK orMYSQLD_PACKET_ERR constants to populate this parameter. |
================ 下面是一些常量定义 ===================
[[Proxy Return State Constants]]
下表中给出的是被 proxy 在内部使用的常量值定义,用于标识发送到客户端和服务器的应答包。全部的常量都在 proxy 表中以常量值的形式可见。
Constant |
Description |
PROXY_SEND_QUERY |
Causes the proxy to send the current contents of the queries queue to the server. |
PROXY_SEND_RESULT |
Causes the proxy to send a result set back to the client. |
PROXY_IGNORE_RESULT |
Causes the proxy to drop the result set (nothing is returned to the client). |
做为常量值,这些实体在 Lua 脚本中是无条件的可用。例如,在函数 read_query_result() 的结尾,你可能返回常量 PROXY_IGNORE_RESULT :
return proxy.PROXY_IGNORE_RESULT
[[Packet State Constants]]
下面的状态值用于定义网络包的状态信息。这些值也存在于 proxy 表中。
Constant |
Description |
MYSQLD_PACKET_OK |
The packet is OK |
MYSQLD_PACKET_ERR |
The packet contains error information |
MYSQLD_PACKET_RAW |
The packet contains raw data |
[[Backend State/Type Constants]]
下面的常量被用于定义 backend MySQL server 的状态和类型。这些值也存在于 proxy 表中。
Constant |
Description |
BACKEND_STATE_UNKNOWN |
The current status is unknown |
BACKEND_STATE_UP |
The backend is known to be up (available) |
BACKEND_STATE_DOWN |
The backend is known to be down (unavailable) |
BACKEND_TYPE_UNKNOWN |
Backend type is unknown |
BACKEND_TYPE_RW |
Backend is available for read/write |
BACKEND_TYPE_RO |
Backend is available only for read-only use |
[[Server Command Constants]]
下表中描述的值被用于客户端和服务器进行包交换过中标识包中其他内容的信息类型。这些值也存在于 proxy 表中。包类型由发送包的第一个字节指定。例如,当为了修改或者监听的目的拦截来自于客户端的包时,你须要检查包的第一个字节是否为 proxy.COM_QUERY 类型。
Constant |
Description |
COM_SLEEP |
Sleep |
COM_QUIT |
Quit |
COM_INIT_DB |
Initialize database |
COM_QUERY |
Query |
COM_FIELD_LIST |
Field List |
COM_CREATE_DB |
Create database |
COM_DROP_DB |
Drop database |
COM_REFRESH |
Refresh |
COM_SHUTDOWN |
Shutdown |
COM_STATISTICS |
Statistics |
COM_PROCESS_INFO |
Process List |
COM_CONNECT |
Connect |
COM_PROCESS_KILL |
Kill |
COM_DEBUG |
Debug |
COM_PING |
Ping |
COM_TIME |
Time |
COM_DELAYED_INSERT |
Delayed insert |
COM_CHANGE_USER |
Change user |
COM_BINLOG_DUMP |
Binlog dump |
COM_TABLE_DUMP |
Table dump |
COM_CONNECT_OUT |
Connect out |
COM_REGISTER_SLAVE |
Register slave |
COM_STMT_PREPARE |
Prepare server-side statement |
COM_STMT_EXECUTE |
Execute server-side statement |
COM_STMT_SEND_LONG_DATA |
Long data |
COM_STMT_CLOSE |
Close server-side statement |
COM_STMT_RESET |
Reset statement |
COM_SET_OPTION |
Set option |
COM_STMT_FETCH |
Fetch statement |
COM_DAEMON |
Daemon (MySQL 5.1 only) |
COM_ERROR |
Error |
[[MySQL Type Constants]]
下面的常量被用于对 query 的结果中的数据进行域类型标识。这些值也存在于 proxy 表中。
Constant |
Field Type |
MYSQL_TYPE_DECIMAL |
Decimal |
MYSQL_TYPE_NEWDECIMAL |
Decimal (MySQL 5.0 or later) |
MYSQL_TYPE_TINY |
Tiny |
MYSQL_TYPE_SHORT |
Short |
MYSQL_TYPE_LONG |
Long |
MYSQL_TYPE_FLOAT |
Float |
MYSQL_TYPE_DOUBLE |
Double |
MYSQL_TYPE_NULL |
Null |
MYSQL_TYPE_TIMESTAMP |
Timestamp |
MYSQL_TYPE_LONGLONG |
Long long |
MYSQL_TYPE_INT24 |
Integer |
MYSQL_TYPE_DATE |
Date |
MYSQL_TYPE_TIME |
Time |
MYSQL_TYPE_DATETIME |
Datetime |
MYSQL_TYPE_YEAR |
Year |
MYSQL_TYPE_NEWDATE |
Date (MySQL 5.0 or later) |
MYSQL_TYPE_ENUM |
Enumeration |
MYSQL_TYPE_SET |
Set |
MYSQL_TYPE_TINY_BLOB |
Tiny Blob |
MYSQL_TYPE_MEDIUM_BLOB |
Medium Blob |
MYSQL_TYPE_LONG_BLOB |
Long Blob |
MYSQL_TYPE_BLOB |
Blob |
MYSQL_TYPE_VAR_STRING |
Varstring |
MYSQL_TYPE_STRING |
String |
MYSQL_TYPE_TINY |
Tiny (compatible with MYSQL_TYPE_CHAR) |
MYSQL_TYPE_ENUM |
Enumeration (compatible with MYSQL_TYPE_INTERVAL) |
MYSQL_TYPE_GEOMETRY |
Geometry |
MYSQL_TYPE_BIT |
Bit |