forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewriter.cc
More file actions
205 lines (168 loc) · 6.28 KB
/
rewriter.cc
File metadata and controls
205 lines (168 loc) · 6.28 KB
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
/* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "plugin/rewriter/rewriter.h"
#include "my_config.h"
#include <mysql/service_parser.h>
#include <mysql/service_rules_table.h>
#include <stddef.h>
#include <memory>
#include <optional>
#include <string>
#include "m_string.h" // Needed because debug_sync.h is not self-sufficient.
#include "my_dbug.h"
#include "mysql/components/services/bits/my_thread_bits.h"
#include "mysqld_error.h"
#include "plugin/rewriter/messages.h"
#include "plugin/rewriter/persisted_rule.h"
#include "plugin/rewriter/rule.h"
#include "sql/debug_sync.h"
#include "template_utils.h"
using rules_table_service::Cursor;
using std::string;
namespace messages = rewriter_messages;
namespace {
std::string hash_key_from_digest(const uchar *digest) {
return std::string(pointer_cast<const char *>(digest),
PARSER_SERVICE_DIGEST_LENGTH);
}
} // namespace
/**
@file rewriter.cc
Implementation of the Rewriter class's member functions.
*/
Rewriter::Rewriter() = default;
Rewriter::~Rewriter() = default;
bool Rewriter::load_rule(MYSQL_THD thd, Persisted_rule *diskrule) {
std::unique_ptr<Rule> memrule_ptr(new Rule);
Rule *memrule = memrule_ptr.get();
Rule::Load_status load_status = memrule->load(thd, diskrule);
switch (load_status) {
case Rule::OK:
m_digests.emplace(hash_key_from_digest(memrule_ptr->digest_buffer()),
std::move(memrule_ptr));
diskrule->message = std::optional<string>();
diskrule->pattern_digest =
services::print_digest(memrule->digest_buffer());
diskrule->normalized_pattern = memrule->normalized_pattern();
return false;
break;
case Rule::PATTERN_GOT_NO_DIGEST:
diskrule->set_message(messages::PATTERN_GOT_NO_DIGEST);
break;
case Rule::PATTERN_PARSE_ERROR:
diskrule->set_message(string(messages::PATTERN_PARSE_ERROR) +
": "
">>" +
memrule->pattern_parse_error_message() + "<<");
break;
case Rule::PATTERN_NOT_SUPPORTED_STATEMENT:
diskrule->set_message(messages::PATTERN_NOT_SUPPORTED_STATEMENT);
break;
case Rule::REPLACEMENT_PARSE_ERROR:
diskrule->set_message(string(messages::REPLACEMENT_PARSE_ERROR) +
": "
">>" +
memrule->replacement_parse_error_message() + "<<");
break;
case Rule::REPLACEMENT_HAS_MORE_MARKERS:
diskrule->set_message(messages::REPLACEMENT_HAS_MORE_MARKERS);
break;
}
return true;
}
#ifndef NDEBUG
/**
Normal debug sync points will not work in the THD that the plugin creates,
so we have to call the debug sync functions ourselves.
*/
static void do_debug_sync(MYSQL_THD thd) {
assert(opt_debug_sync_timeout > 0);
const char act[] = "now signal parked wait_for go";
assert(!debug_sync_set_action(thd, STRING_WITH_LEN(act)));
}
#endif
void Rewriter::do_refresh(MYSQL_THD session_thd) {
bool saw_rule_error = false;
DBUG_TRACE;
Cursor c(session_thd);
DBUG_PRINT("info", ("Rewriter::do_refresh cursor opened"));
DBUG_EXECUTE_IF("dbug.block_do_refresh", { do_debug_sync(session_thd); });
if (c.table_is_malformed()) {
m_refresh_status = ER_REWRITER_TABLE_MALFORMED_ERROR;
return;
}
m_digests.clear();
for (; c != rules_table_service::end(); ++c) {
Persisted_rule diskrule(&c);
if (diskrule.is_enabled) {
if (!diskrule.pattern.has_value()) {
diskrule.set_message("Pattern is NULL.");
saw_rule_error = true;
} else if (!diskrule.replacement.has_value()) {
diskrule.set_message("Replacement is NULL.");
saw_rule_error = true;
} else
saw_rule_error |= load_rule(session_thd, &diskrule);
diskrule.write_to(&c);
}
}
if (c.had_serious_read_error())
m_refresh_status = ER_REWRITER_READ_FAILED;
else if (saw_rule_error)
m_refresh_status = ER_REWRITER_LOAD_FAILED;
else
m_refresh_status = 0;
}
namespace {
struct Refresh_callback_args {
Rewriter *me;
MYSQL_THD session_thd;
};
extern "C" void *refresh_callback(void *p_args) {
Refresh_callback_args *args = pointer_cast<Refresh_callback_args *>(p_args);
(args->me->do_refresh)(args->session_thd);
return nullptr;
}
} // namespace
longlong Rewriter::refresh(MYSQL_THD thd) {
services::Session session(thd);
Refresh_callback_args args = {this, session.thd()};
m_refresh_status = 0;
my_thread_handle handle;
mysql_parser_start_thread(session.thd(), refresh_callback, &args, &handle);
mysql_parser_join_thread(&handle);
return m_refresh_status;
}
Rewrite_result Rewriter::rewrite_query(MYSQL_THD thd, const uchar *key) {
Rewrite_result result;
bool digest_matched = false;
auto it_range = m_digests.equal_range(hash_key_from_digest(key));
for (auto it = it_range.first; it != it_range.second; ++it) {
Rule *rule = it->second.get();
if (rule->matches(thd)) {
result = rule->create_new_query(thd);
if (result.was_rewritten) return result;
} else {
digest_matched = true;
}
}
result.was_rewritten = false;
result.digest_matched = digest_matched;
return result;
}