~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to lib/active-transactions.lua

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
--[[ $%BEGINLICENSE%$
2
 
 Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
3
 
 
4
 
 This program is free software; you can redistribute it and/or
5
 
 modify it under the terms of the GNU General Public License as
6
 
 published by the Free Software Foundation; version 2 of the
7
 
 License.
8
 
 
9
 
 This program is distributed in the hope that it will be useful,
10
 
 but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 
 GNU General Public License for more details.
13
 
 
14
 
 You should have received a copy of the GNU General Public License
15
 
 along with this program; if not, write to the Free Software
16
 
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17
 
 02110-1301  USA
18
 
 
19
 
 $%ENDLICENSE%$ --]]
20
 
--[[
21
 
 
22
 
print the statements of all transactions as soon as one of them aborted  
23
 
 
24
 
for now we know about:
25
 
* Lock wait timeout exceeded
26
 
* Deadlock found when trying to get lock
27
 
 
28
 
--]]
29
 
 
30
 
if not proxy.global.trxs then
31
 
        proxy.global.trxs = { }
32
 
end
33
 
 
34
 
function read_query(packet)
35
 
        if packet:byte() ~= proxy.COM_QUERY then return end
36
 
 
37
 
        if not proxy.global.trxs[proxy.connection.server.thread_id] then
38
 
                proxy.global.trxs[proxy.connection.server.thread_id] = { }
39
 
        end
40
 
 
41
 
        proxy.queries:append(1, packet, { resultset_is_needed = true })
42
 
 
43
 
        local t = proxy.global.trxs[proxy.connection.server.thread_id]
44
 
 
45
 
        t[#t + 1] = packet:sub(2)
46
 
 
47
 
        return proxy.PROXY_SEND_QUERY
48
 
end
49
 
 
50
 
function read_query_result(inj)
51
 
        local res = inj.resultset
52
 
        local flags = res.flags
53
 
 
54
 
        if res.query_status == proxy.MYSQLD_PACKET_ERR then
55
 
                local err_code     = res.raw:byte(2) + (res.raw:byte(3) * 256)
56
 
                local err_sqlstate = res.raw:sub(5, 9)
57
 
                local err_msg      = res.raw:sub(10)
58
 
                -- print("-- error-packet: " .. err_code)
59
 
 
60
 
                if err_code == 1205 or     -- Lock wait timeout exceeded
61
 
                   err_code == 1213 then   -- Deadlock found when trying to get lock
62
 
                        print(("[%d] received a ERR(%d, %s), dumping all active transactions"):format( 
63
 
                                proxy.connection.server.thread_id,
64
 
                                err_code,
65
 
                                err_msg))
66
 
                        
67
 
                        for thread_id, statements in pairs(proxy.global.trxs) do
68
 
                                for stmt_id, statement in ipairs(statements) do
69
 
                                        print(("  [%d].%d: %s"):format(thread_id, stmt_id, statement))
70
 
                                end
71
 
                        end
72
 
                end
73
 
        end
74
 
 
75
 
        -- we are done, free the statement list
76
 
        if not flags.in_trans then
77
 
                proxy.global.trxs[proxy.connection.server.thread_id] = nil
78
 
        end
79
 
end
80
 
 
81
 
function disconnect_client()
82
 
        proxy.global.trxs[proxy.connection.server.thread_id] = nil
83
 
end
84