~diego-fmpwizard/mysql-proxy/bug-43424

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
--[[ $%BEGINLICENSE%$
 Copyright (C) 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 $%ENDLICENSE%$ --]]

-- proxy.auto-config will pick them up
local commands = require("proxy.commands")
local auto_config = require("proxy.auto-config")

--- init the global scope
if not proxy.global.active_queries then
	proxy.global.active_queries = {}
end

if not proxy.global.max_active_trx then
	proxy.global.max_active_trx = 0
end

-- default config for this script
if not proxy.global.config.active_queries then
	proxy.global.config.active_queries = {
		show_idle_connections = false
	}
end

---
-- track the active queries and dump all queries at each state-change
--

function collect_stats()
	local num_conns = 0
	local active_conns = 0

	for k, v in pairs(proxy.global.active_queries) do
		num_conns = num_conns + 1

		if v.state ~= "idle" then
			active_conns = active_conns + 1
		end
	end

	if active_conns > proxy.global.max_active_trx then
		proxy.global.max_active_trx = active_conns
	end

	return {
		active_conns = active_conns,
		num_conns = num_conns,
		max_active_trx = proxy.global.max_active_trx
	}
end

---
-- dump the state of the current queries
-- 
function print_stats(stats)
	local o = ""

	for k, v in pairs(proxy.global.active_queries) do
		if v.state ~= "idle" or proxy.global.config.active_queries.show_idle_connections then
			local cmd_query = ""
			if v.cmd then
				cmd_query = string.format("(%s) %q", v.cmd.type_name, v.cmd.query or "")
			end
			o = o .."  ["..k.."] (".. v.username .."@".. v.db ..") " .. cmd_query .." (state=" .. v.state .. ")\n"
		end
	end

	-- prepend the data and the stats about the number of connections and trx
	o = os.date("%Y-%m-%d %H:%M:%S") .. "\n" ..
		"  #connections: " .. stats.num_conns .. 
		", #active trx: " .. stats.active_conns .. 
		", max(active trx): ".. stats.max_active_trx .. 
		"\n" .. o

	print(o)
end

--- 
-- enable tracking the packets
function read_query(packet) 
	local cmd = commands.parse(packet)
	local r = auto_config.handle(cmd)
	if r then return r end
	
	proxy.queries:append(1, packet)

	-- add the query to the global scope
	local connection_id = proxy.connection.server.thread_id

	proxy.global.active_queries[connection_id] = { 
		state = "started",
		cmd = cmd,
		db = proxy.connection.client.default_db or "",
		username = proxy.connection.client.username or ""
	}

	print_stats(collect_stats())

	return proxy.PROXY_SEND_QUERY
end

---
-- statement is done, track the change
function read_query_result(inj)
	local connection_id = proxy.connection.server.thread_id

	proxy.global.active_queries[connection_id].state = "idle"
	proxy.global.active_queries[connection_id].cmd = nil
	
	if inj.resultset then
		local res = inj.resultset

		if res.flags.in_trans then
			proxy.global.active_queries[connection_id].state = "in_trans" 
		end
	end

	print_stats(collect_stats())
end

---
-- remove the information about the connection 
-- 
function disconnect_client()
	local connection_id = proxy.connection.server.thread_id
	if connection_id then
		proxy.global.active_queries[connection_id] = nil
	
		print_stats(collect_stats())
	end
end