File: mysql.lua

package info (click to toggle)
lua-sandbox-extensions 0~git20161128-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,596 kB
  • ctags: 1,458
  • sloc: ansic: 4,402; cpp: 2,102; makefile: 8
file content (285 lines) | stat: -rw-r--r-- 9,073 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

require "string"
local mysql = require "lpeg.mysql"

local slow_query_log = {
[[
# Time: 140507 18:14:18
# User@Host: syncrw[syncrw] @  [127.0.0.1]
# Query_time: 2.964652  Lock_time: 0.000050 Rows_sent: 251  Rows_examined: 9773
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]],
[[
# User@Host: syncrw[syncrw] @  [127.0.0.1]
# Query_time: 2.964652  Lock_time: 0.000050 Rows_sent: 251  Rows_examined: 9773
use widget;
SET last_insert_id=999,insert_id=1000,timestamp=1399500744;
# administrator command: do something
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]],
[[
# Query_time: 2.964652  Lock_time: 0.000050 Rows_sent: 251  Rows_examined: 9773
SET last_insert_id=999,timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]],
[[
# Time: 140507 18:14:18
# User@Host: sync.rw[sync.rw] @ db01.example.com [127.0.0.1]
# Query_time: 2.964652  Lock_time: 0.000050 Rows_sent: 251  Rows_examined: 9773
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]],
[[
# Time: 140507 18:14:18
# User@Host: sync_rw[sync_rw] @ db-01.example.com [127.0.0.1]
# Query_time: 2.964652  Lock_time: 0.000050 Rows_sent: 251  Rows_examined: 9773
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]]
}

local mariadb_slow_query_log = {
[[
# User@Host: syncrw[syncrw] @  [127.0.0.1]
# Thread_id: 110804  Schema: weave0  QC_hit: No
# Query_time: 1.178108  Lock_time: 0.000053  Rows_sent: 198  Rows_examined: 198
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]],
[[
# Thread_id: 110804  Schema: weave0  QC_hit: No
# Query_time: 1.178108  Lock_time: 0.000053  Rows_sent: 198  Rows_examined: 198
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]],
[[
# Time: 140623 16:43:22
# User@Host: syncrw[syncrw] @  [127.0.0.1]
# Thread_id: 110804  Schema: weave0  QC_hit: No
# Query_time: 1.178108  Lock_time: 0.000053  Rows_sent: 198  Rows_examined: 198
# Full_scan: Yes  Full_join: Yes  Tmp_table: Yes  Tmp_table_on_disk: No
# Filesort: Yes  Filesort_on_disk: No  Merge_passes: 3
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]]
}

local percona_slow_query_log = {
[[
# User@Host: syncrw[syncrw] @ db-01.example.com [127.0.0.1]  Id:    42
# Schema: imdb  Last_errno: 0  Killed: 0
# Query_time: 7.725616  Lock_time: 0.000328  Rows_sent: 4  Rows_examined: 1543720  Rows_affected: 0
# Bytes_sent: 272  Tmp_tables: 0  Tmp_disk_tables: 0  Tmp_table_sizes: 0
# QC_Hit: No  Full_scan: Yes  Full_join: No  Tmp_table: No  Tmp_table_on_disk: No
# Filesort: No  Filesort_on_disk: No  Merge_passes: 0
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]],
[[
# Time: 130601  8:01:06
# User@Host: syncrw[syncrw] @ db-01.example.com [127.0.0.1]  Id:    42
# Schema: imdb  Last_errno: 0  Killed: 0
# Query_time: 7.725616  Lock_time: 0.000328  Rows_sent: 4  Rows_examined: 1543720  Rows_affected: 0  Rows_read: 30
# Bytes_sent: 272  Tmp_tables: 0  Tmp_disk_tables: 0  Tmp_table_sizes: 0
# QC_Hit: No  Full_scan: Yes  Full_join: No  Tmp_table: No  Tmp_table_on_disk: No
# Filesort: No  Filesort_on_disk: No  Merge_passes: 0
#   InnoDB_IO_r_ops: 6415  InnoDB_IO_r_bytes: 105103360  InnoDB_IO_r_wait: 0.001279
#   InnoDB_rec_lock_wait: 0.000000  InnoDB_queue_wait: 0.000000
#   InnoDB_pages_distinct: 6430
SET timestamp=1399500744;
/* [queryName=FIND_ITEMS] */ SELECT *
FROM widget
WHERE id = 10;
]]
}

local fields = {
    {"Query_time", 2.964652, "s"},
    {"Rows_examined", 9773},
    {"Lock_time", 0.000050, "s"},
    {"Rows_sent", 251}
}

local mariadb_fields = {
    {"Query_time", 1.178108, "s"},
    {"Rows_examined", 198},
    {"Lock_time", 0.000053, "s"},
    {"Rows_sent", 198},
    {"QC_hit", "No"},
    {"Thread_id", 110804},
    {"Schema", "weave0"}
}

local mariadb_verbose_fields = {
    {"Query_time", 1.178108, "s"},
    {"Rows_examined", 198},
    {"Lock_time", 0.000053, "s"},
    {"Rows_sent", 198},
    {"QC_hit", "No"},
    {"Thread_id", 110804},
    {"Schema", "weave0"},
    {"Full_scan", "Yes"},
    {"Full_join", "Yes"},
    {"Filesort", "Yes"},
    {"Filesort_on_disk", "No"},
    {"Tmp_table_on_disk", "No"},
    {"Tmp_table", "Yes"},
    {"Merge_passes", 3}
}

local percona_fields = {
    {"Schema", "imdb"},
    {"Last_errno", 0},
    {"Killed", 0},
    {"Query_time", 7.725616, "s"},
    {"Lock_time", 0.000328, "s"},
    {"Rows_sent", 4},
    {"Rows_examined", 1543720},
    {"Rows_affected", 0},
    {"Bytes_sent", 272, "B"},
    {"Tmp_tables", 0},
    {"Tmp_disk_tables", 0},
    {"Tmp_table_sizes", 0, "B"},
    {"QC_hit", "No"},
    {"Full_scan", "Yes"},
    {"Full_join", "No"},
    {"Tmp_table", "No"},
    {"Tmp_table_on_disk", "No"},
    {"Filesort", "No"},
    {"Filesort_on_disk", "No"},
    {"Merge_passes", 0}
}

local percona_verbose_fields = {
    {"Schema", "imdb"},
    {"Last_errno", 0},
    {"Killed", 0},
    {"Query_time", 7.725616, "s"},
    {"Lock_time", 0.000328, "s"},
    {"Rows_sent", 4},
    {"Rows_examined", 1543720},
    {"Rows_affected", 0},
    {"Rows_read", 30},
    {"Bytes_sent", 272, "B"},
    {"Tmp_tables", 0},
    {"Tmp_disk_tables", 0},
    {"Tmp_table_sizes", 0, "B"},
    {"QC_hit", "No"},
    {"Full_scan", "Yes"},
    {"Full_join", "No"},
    {"Tmp_table", "No"},
    {"Tmp_table_on_disk", "No"},
    {"Filesort", "No"},
    {"Filesort_on_disk", "No"},
    {"Merge_passes", 0},
    {"InnoDB_IO_r_ops", 6415},
    {"InnoDB_IO_r_bytes", 105103360, "B"},
    {"InnoDB_IO_r_wait", 0.001279, "s"},
    {"InnoDB_rec_lock_wait", 0, "s"},
    {"InnoDB_queue_wait", 0, "s"},
    {"InnoDB_pages_distinct", 6430}
}

local function validate(fields, t)
    if t.Timestamp ~= "1399500744000000000" then return error("Timestamp:" .. t.Timestamp) end
    if t.Payload ~= "/* [queryName=FIND_ITEMS] */ SELECT *\nFROM widget\nWHERE id = 10;\n" then return error("Payload:" .. t.Payload) end
    for i, v in ipairs(fields) do
        if #v == 3 then
            if t.Fields[v[1]].value ~= v[2] or t.Fields[v[1]].representation ~= v[3] then
                if type(v[2]) == "string" then
                    return error(string.format("field:%s: %s (%s)", v[1], t.Fields[v[1]].value, t.Fields[v[1]].representation), 2)
                else
                    return error(string.format("field:%s: %G (%s)", v[1], t.Fields[v[1]].value, t.Fields[v[1]].representation), 2)
                end
            end
        else
            print("Output:" .. v[1])
            if t.Fields[v[1]] ~= v[2] then return error(string.format("field:%s:", v[1]) .. t.Fields[v[1]], 2) end
        end
    end
end

local function header()
    local t = mysql.slow_query_grammar:match(slow_query_log[1])
    if not t then return error("no match") end
    if t.Hostname ~= "127.0.0.1" then return error("Hostname: " .. t.Hostname) end
    validate(fields, t)
end

local function standard()
    local t = mysql.slow_query_grammar:match(slow_query_log[2])
    if not t then return error("no match") end
    if t.Hostname ~= "127.0.0.1" then return error("Hostname: " .. t.Hostname) end
    validate(fields, t)
end

local function short()
    local t = mysql.short_slow_query_grammar:match(slow_query_log[3])
    if not t then return error("no match") end
    validate(fields, t)
end

local function mariadb_standard()
    local t = mysql.mariadb_slow_query_grammar:match(mariadb_slow_query_log[1])
    if not t then return error("no match") end
    if t.Hostname ~= "127.0.0.1" then return error("Hostname: " .. t.Hostname) end
    validate(mariadb_fields, t)
end

local function mariadb_short()
    local t = mysql.mariadb_short_slow_query_grammar:match(mariadb_slow_query_log[2])
    if not t then return error("no match") end
    validate(mariadb_fields, t)
end

local function mariadb_verbose()
    local t = mysql.mariadb_slow_query_grammar:match(mariadb_slow_query_log[3])
    if not t then return error("no match") end
    if t.Hostname ~= "127.0.0.1" then return error("Hostname: " .. t.Hostname) end
    validate(mariadb_verbose_fields, t)
end

local function percona_standard()
    local t = mysql.percona_slow_query_grammar:match(percona_slow_query_log[1])
    if not t then return error("no match") end
    if t.Hostname ~= "127.0.0.1" then return error("Hostname: " .. t.Hostname) end
    validate(percona_fields, t)
end

local function percona_verbose()
    local t = mysql.percona_slow_query_grammar:match(percona_slow_query_log[2])
    if not t then return error("no match") end
    if t.Hostname ~= "127.0.0.1" then return error("Hostname: " .. t.Hostname) end
    validate(percona_verbose_fields, t)
end

header()
standard()
short()
mariadb_standard()
mariadb_short()
mariadb_verbose()
percona_standard()
percona_verbose()