-
Notifications
You must be signed in to change notification settings - Fork 713
/
Copy pathsession_tracker.cc
1636 lines (1323 loc) · 50.9 KB
/
session_tracker.cc
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
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 "sql/session_tracker.h"
#include <string.h>
#include <algorithm>
#include <memory>
#include <new>
#include <string>
#include <utility>
#include <vector>
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h"
#include "map_helpers.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/status_var.h"
#include "mysql/thread_type.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/current_thd.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/rpl_context.h"
#include "sql/rpl_gtid.h"
#include "sql/set_var.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_show.h"
#include "sql/system_variables.h"
#include "sql/transaction_info.h"
#include "sql/xa.h"
#include "sql_string.h"
#include "template_utils.h"
static void store_lenenc_string(String &to, const char *from, size_t length);
/**
Session_sysvars_tracker
-----------------------
This is a tracker class that enables & manages the tracking of session
system variables. It internally maintains a hash of user supplied variable
names and a boolean field to store if the variable was changed by the last
statement.
*/
class Session_sysvars_tracker : public State_tracker {
private:
struct sysvar_node_st {
LEX_STRING m_sysvar_name;
bool m_changed;
};
class vars_list {
private:
/**
Registered system variables. (@@session_track_system_variables)
A hash to store the name of all the system variables specified by the
user.
*/
using sysvar_map =
collation_unordered_map<std::string,
unique_ptr_my_free<sysvar_node_st>>;
std::unique_ptr<sysvar_map> m_registered_sysvars;
char *variables_list;
/**
The boolean which when set to true, signifies that every variable
is to be tracked.
*/
bool track_all;
const CHARSET_INFO *m_char_set;
void init(const CHARSET_INFO *char_set) {
variables_list = NULL;
m_char_set = char_set;
m_registered_sysvars.reset(
new sysvar_map(char_set, key_memory_THD_Session_tracker));
}
void free_hash() { m_registered_sysvars.reset(); }
sysvar_node_st *search(const uchar *token, size_t length) {
return find_or_nullptr(
*m_registered_sysvars,
std::string(pointer_cast<const char *>(token), length));
}
public:
vars_list(const CHARSET_INFO *char_set) { init(char_set); }
void claim_memory_ownership() { my_claim(variables_list); }
~vars_list() {
if (variables_list) my_free(variables_list);
variables_list = NULL;
}
sysvar_node_st *search(sysvar_node_st *node, LEX_STRING tmp) {
sysvar_node_st *res;
res = search((const uchar *)tmp.str, tmp.length);
if (!res) {
if (track_all) {
insert(node, tmp);
return search((const uchar *)tmp.str, tmp.length);
}
}
return res;
}
sysvar_map::iterator begin() const { return m_registered_sysvars->begin(); }
sysvar_map::iterator end() const { return m_registered_sysvars->end(); }
const CHARSET_INFO *char_set() const { return m_char_set; }
bool insert(sysvar_node_st *node, LEX_STRING var);
void reset();
bool update(vars_list *from, THD *thd);
bool parse_var_list(THD *thd, LEX_STRING var_list, bool throw_error,
const CHARSET_INFO *char_set, bool session_created);
};
/**
Two objects of vars_list type are maintained to manage
various operations on variables_list.
*/
vars_list *orig_list, *tool_list;
public:
/** Constructor */
Session_sysvars_tracker(const CHARSET_INFO *char_set) {
orig_list = new (std::nothrow) vars_list(char_set);
tool_list = new (std::nothrow) vars_list(char_set);
}
/** Destructor */
~Session_sysvars_tracker() {
if (orig_list) delete orig_list;
if (tool_list) delete tool_list;
}
/**
Method used to check the validity of string provided
for session_track_system_variables during the server
startup.
*/
static bool server_init_check(const CHARSET_INFO *char_set,
LEX_STRING var_list) {
vars_list dummy(char_set);
bool result;
result = dummy.parse_var_list(NULL, var_list, false, char_set, true);
return result;
}
void reset();
bool enable(THD *thd);
bool check(THD *thd, set_var *var);
bool update(THD *thd);
bool store(THD *thd, String &buf);
void mark_as_changed(THD *thd, LEX_CSTRING *tracked_item_name);
/* callback */
static const uchar *sysvars_get_key(const uchar *entry, size_t *length);
virtual void claim_memory_ownership() {
if (orig_list != NULL) orig_list->claim_memory_ownership();
if (tool_list != NULL) tool_list->claim_memory_ownership();
}
};
/**
Current_schema_tracker
----------------------
This is a tracker class that enables & manages the tracking of current
schema for a particular connection.
*/
class Current_schema_tracker : public State_tracker {
private:
bool schema_track_inited;
void reset();
public:
/** Constructor */
Current_schema_tracker() { schema_track_inited = false; }
bool enable(THD *thd) { return update(thd); }
bool check(THD *, set_var *) { return false; }
bool update(THD *thd);
bool store(THD *thd, String &buf);
void mark_as_changed(THD *thd, LEX_CSTRING *tracked_item_name);
};
/* To be used in expanding the buffer. */
static const unsigned int EXTRA_ALLOC = 1024;
///////////////////////////////////////////////////////////////////////////////
/**
This is an interface for encoding the gtids in the payload of the
the OK packet.
In the future we may have different types of payloads, thence we may have
different encoders specifications/types. This implies changing either, the
encoding specification code, the actual encoding procedure or both at the
same time.
New encoders can extend this interface/abstract class or extend
other encoders in the hierarchy.
*/
class Session_gtids_ctx_encoder {
public:
Session_gtids_ctx_encoder() {}
virtual ~Session_gtids_ctx_encoder(){};
/*
This function SHALL encode the collected GTIDs into the buffer.
@param thd The session context.
@param buf The buffer that SHALL contain the encoded data.
@return false if the contents were successfully encoded, true otherwise.
if the return value is true, then the contents of the buffer is
undefined.
*/
virtual bool encode(THD *thd, String &buf) = 0;
/*
This function SHALL return the encoding specification used in the
packet sent to the client. The format of the encoded data will differ
according to the specification set here.
@return the encoding specification code.
*/
virtual ulonglong encoding_specification() = 0;
private:
// not implemented
Session_gtids_ctx_encoder(const Session_gtids_ctx_encoder &rsc);
Session_gtids_ctx_encoder &operator=(const Session_gtids_ctx_encoder &rsc);
};
class Session_gtids_ctx_encoder_string : public Session_gtids_ctx_encoder {
public:
Session_gtids_ctx_encoder_string() {}
~Session_gtids_ctx_encoder_string() {}
ulonglong encoding_specification() { return 0; }
bool encode(THD *thd, String &buf) {
const Gtid_set *state = thd->rpl_thd_ctx.session_gtids_ctx().state();
if (!state->is_empty()) {
/*
No need to use net_length_size in the following two fields.
These are constants in this class and will both be encoded using
only 1 byte.
*/
ulonglong tracker_type_enclen =
1 /* net_length_size((ulonglong)SESSION_TRACK_GTIDS); */;
ulonglong encoding_spec_enclen =
1 /* net_length_size(encoding_specification()); */;
ulonglong gtids_string_len =
state->get_string_length(&Gtid_set::default_string_format);
ulonglong gtids_string_len_enclen = net_length_size(gtids_string_len);
ulonglong entity_len =
encoding_spec_enclen + gtids_string_len_enclen + gtids_string_len;
ulonglong entity_len_enclen = net_length_size(entity_len);
ulonglong total_enclen = tracker_type_enclen + entity_len_enclen +
encoding_spec_enclen + gtids_string_len_enclen +
gtids_string_len;
/* prepare the buffer */
uchar *to = (uchar *)buf.prep_append(total_enclen, EXTRA_ALLOC);
/* format of the payload is as follows:
[ tracker type] [len] [ encoding spec ] [gtid string len] [gtid string]
*/
/* Session state type (SESSION_TRACK_SCHEMA) */
*to = (uchar)SESSION_TRACK_GTIDS;
to++;
/* Length of the overall entity. */
to = net_store_length(to, entity_len);
/* encoding specification */
*to = (uchar)encoding_specification();
to++;
/* the length of the gtid set string*/
to = net_store_length(to, gtids_string_len);
/* the actual gtid set string */
state->to_string((char *)to);
}
return false;
}
private:
// not implemented
Session_gtids_ctx_encoder_string(const Session_gtids_ctx_encoder_string &rsc);
Session_gtids_ctx_encoder_string &operator=(
const Session_gtids_ctx_encoder_string &rsc);
};
/**
Session_gtids_tracker
---------------------------------
This is a tracker class that enables & manages the tracking of gtids for
relaying to the connectors the information needed to handle session
consistency.
*/
class Session_gtids_tracker
: public State_tracker,
Session_consistency_gtids_ctx::Ctx_change_listener {
private:
void reset();
Session_gtids_ctx_encoder *m_encoder;
public:
/** Constructor */
Session_gtids_tracker()
: Session_consistency_gtids_ctx::Ctx_change_listener(), m_encoder(NULL) {}
~Session_gtids_tracker() {
/*
Unregister the listener if the tracker is being freed. This is needed
since this may happen after a change user command.
*/
if (m_enabled && current_thd)
current_thd->rpl_thd_ctx.session_gtids_ctx()
.unregister_ctx_change_listener(this);
if (m_encoder) delete m_encoder;
}
bool enable(THD *thd) { return update(thd); }
bool check(THD *, set_var *) { return false; }
bool update(THD *thd);
bool store(THD *thd, String &buf);
void mark_as_changed(THD *thd, LEX_CSTRING *tracked_item_name);
// implementation of the Session_gtids_ctx::Ctx_change_listener
void notify_session_gtids_ctx_change() { mark_as_changed(NULL, NULL); }
};
void Session_sysvars_tracker::vars_list::reset() {
if (m_registered_sysvars != nullptr) m_registered_sysvars->clear();
if (variables_list) {
my_free(variables_list);
variables_list = NULL;
}
}
/**
This function is used to update the members of one vars_list object with
the members from the other.
@@param from Source vars_list object.
@@param thd THD handle to retrive the charset in use.
@@return true if the m_registered_sysvars hash has any records.
Else the value of track_all.
*/
bool Session_sysvars_tracker::vars_list::update(vars_list *from, THD *thd) {
reset();
variables_list = from->variables_list;
track_all = from->track_all;
free_hash();
m_registered_sysvars = std::move(from->m_registered_sysvars);
from->init(thd->charset());
return m_registered_sysvars->empty() ? track_all : true;
}
/**
Inserts the variable to be tracked into m_registered_sysvars hash.
@@param node Node to be inserted.
@@param var LEX_STRING which has the name of variable.
@@return false success
true error
*/
bool Session_sysvars_tracker::vars_list::insert(sysvar_node_st *node,
LEX_STRING var) {
if (!node) {
if (!(node = (sysvar_node_st *)my_malloc(key_memory_THD_Session_tracker,
sizeof(sysvar_node_st), MY_WME))) {
reset();
return true; /* Error */
}
}
node->m_sysvar_name.str = var.str;
node->m_sysvar_name.length = var.length;
node->m_changed = false;
unique_ptr_my_free<sysvar_node_st> node_ptr(node);
if (!m_registered_sysvars->emplace(to_string(var), std::move(node_ptr))
.second) {
/* Duplicate entry. */
my_error(ER_DUP_LIST_ENTRY, MYF(0), var.str);
reset();
return true;
} /* Error */
return false;
}
/**
@brief Parse the specified system variables list. While parsing raise
warning/error on invalid/duplicate entries.
* In case of duplicate entry ER_DUP_LIST_ENTRY is raised.
* In case of invalid entry a warning is raised per invalid entry.
This is done in order to handle 'potentially' valid system
variables from uninstalled plugins which might get installed in
future.
Value of @@session_track_system_variables is initially put into
variables_list. This string is used to update the hash with valid
system variables.
@param thd The thd handle.
@param var_list System variable list.
@param throw_error bool when set to true, returns an error
in case of invalid/duplicate values.
@param char_set character set information used for string
manipulations.
@param session_created bool variable which says if the parse is
already executed once. The mutex on variables
is not acquired if this variable is false.
@return
true Error
false Success
*/
bool Session_sysvars_tracker::vars_list::parse_var_list(
THD *thd, LEX_STRING var_list, bool throw_error,
const CHARSET_INFO *char_set, bool session_created) {
const char *separator = ",";
char *token, *lasts = NULL; /* strtok_r */
if (!var_list.str) {
variables_list = NULL;
return false;
}
/*
Storing of the session_track_system_variables option
string to be used by strtok_r().
*/
variables_list = my_strndup(key_memory_THD_Session_tracker, var_list.str,
var_list.length, MYF(0));
if (variables_list) {
if (!strcmp(variables_list, (const char *)"*")) {
track_all = true;
return false;
}
}
token = my_strtok_r(variables_list, separator, &lasts);
track_all = false;
/*
If Lock to the plugin mutex is not acquired here itself, it results
in having to acquire it multiple times in find_sys_var_ex for each
token value. Hence the mutex is handled here to avoid a performance
overhead.
*/
if (!thd || session_created) lock_plugin_mutex();
while (token) {
LEX_STRING var;
var.str = token;
var.length = strlen(token);
/* Remove leading/trailing whitespace. */
trim_whitespace(char_set, &var);
if (!thd || session_created) {
if (find_sys_var_ex(thd, var.str, var.length, throw_error, true)) {
if (insert(NULL, var) == true) {
/* Error inserting into the hash. */
unlock_plugin_mutex();
return true; /* Error */
}
}
else if (throw_error) {
DBUG_ASSERT(thd);
push_warning_printf(
thd, Sql_condition::SL_WARNING, ER_WRONG_VALUE_FOR_VAR,
"%s is not a valid system variable and will be ignored.", token);
} else {
unlock_plugin_mutex();
return true;
}
} else {
if (insert(NULL, var) == true) {
/* Error inserting into the hash. */
return true; /* Error */
}
}
token = my_strtok_r(NULL, separator, &lasts);
}
if (!thd || session_created) unlock_plugin_mutex();
return false;
}
/**
@brief It is responsible for enabling this tracker when a session starts.
During the initialization, a session's system variable gets a copy
of the global variable. The new value of session_track_system_variables
is then verified & tokenized to create a hash, which is then updated to
orig_list which represents all the systems variables to be tracked.
@param thd The thd handle.
@return
true Error
false Success
*/
bool Session_sysvars_tracker::enable(THD *thd) {
LEX_STRING var_list;
if (!thd->variables.track_sysvars_ptr) return false;
var_list.str = thd->variables.track_sysvars_ptr;
var_list.length = strlen(thd->variables.track_sysvars_ptr);
if (tool_list->parse_var_list(thd, var_list, true, thd->charset(), false) ==
true)
return true;
m_enabled = orig_list->update(tool_list, thd);
return false;
}
/**
@brief Check if any of the system variable name(s) in the given list of
system variables is duplicate/invalid.
When the value of @@session_track_system_variables system variable is
updated, the new value is first verified in this function (called from
ON_CHECK()) and a hash is populated in tool_list.
@note This function is called from the ON_CHECK() function of the
session_track_system_variables' sys_var class.
@param thd The thd handle.
@param var A pointer to set_var holding the specified list of
system variable names.
@return
true Error
false Success
*/
inline bool Session_sysvars_tracker::check(THD *thd, set_var *var) {
tool_list->reset();
return tool_list->parse_var_list(thd, var->save_result.string_value, true,
thd->charset(), true);
}
/**
@brief Once the value of the @@session_track_system_variables has been
successfully updated, this function calls
Session_sysvars_tracker::vars_list::update updating the hash in
orig_list which represents the system variables to be tracked.
@note This function is called from the ON_UPDATE() function of the
session_track_system_variables' sys_var class.
@param thd The thd handle.
@return
true Error
false Success
*/
bool Session_sysvars_tracker::update(THD *thd) {
if (!thd->variables.track_sysvars_ptr) return false;
m_enabled = orig_list->update(tool_list, thd);
return false;
}
/**
@brief Store the data for changed system variables in the specified buffer.
Once the data is stored, we reset the flags related to state-change
(see reset()).
@param thd The thd handle.
@param[in,out] buf Buffer to store the information to.
@return
false Success
true Error
*/
bool Session_sysvars_tracker::store(THD *thd, String &buf) {
char val_buf[1024];
const char *value;
SHOW_VAR *show;
sys_var *var;
const CHARSET_INFO *charset;
size_t val_length, length;
uchar *to;
if (!(show = (SHOW_VAR *)thd->alloc(sizeof(SHOW_VAR)))) return true;
/* As its always system variable. */
show->type = SHOW_SYS;
/*
Return the variables in sorted order. This isn't a protocol requirement
(and thus, we don't need to care about collations), but it makes for easier
testing when things are deterministic and not in hash order.
*/
std::vector<sysvar_node_st *> vars;
for (const auto &key_and_value : *orig_list) {
vars.push_back(key_and_value.second.get());
}
std::sort(vars.begin(), vars.end(),
[](const sysvar_node_st *a, const sysvar_node_st *b) {
return to_string(a->m_sysvar_name) < to_string(b->m_sysvar_name);
});
for (sysvar_node_st *node : vars) {
if (node->m_changed &&
(var = find_sys_var_ex(thd, node->m_sysvar_name.str,
node->m_sysvar_name.length, true, false))) {
show->name = var->name.str;
show->value = (char *)var;
value = get_one_variable(thd, show, OPT_SESSION, show->type, NULL,
&charset, val_buf, &val_length);
length = net_length_size(node->m_sysvar_name.length) +
node->m_sysvar_name.length + net_length_size(val_length) +
val_length;
to = (uchar *)buf.prep_append(net_length_size(length) + 1, EXTRA_ALLOC);
/* Session state type (SESSION_TRACK_SYSTEM_VARIABLES) */
to = net_store_length(to, (ulonglong)SESSION_TRACK_SYSTEM_VARIABLES);
/* Length of the overall entity. */
net_store_length(to, (ulonglong)length);
/* System variable's name (length-encoded string). */
store_lenenc_string(buf, node->m_sysvar_name.str,
node->m_sysvar_name.length);
/* System variable's value (length-encoded string). */
store_lenenc_string(buf, value, val_length);
}
}
reset();
return false;
}
/**
@brief Mark the system variable with the specified name as changed.
@param thd Current thread
@param tracked_item_name Name of the system variable which got changed.
*/
void Session_sysvars_tracker::mark_as_changed(THD *thd,
LEX_CSTRING *tracked_item_name) {
DBUG_ASSERT(tracked_item_name->str);
sysvar_node_st *node = NULL;
LEX_STRING tmp;
tmp.str = (char *)tracked_item_name->str;
tmp.length = tracked_item_name->length;
/*
Check if the specified system variable is being tracked, if so
mark it as changed and also set the class's m_changed flag.
*/
if ((node = (sysvar_node_st *)(orig_list->search(node, tmp)))) {
node->m_changed = true;
m_changed = true;
/* do not cache the statement when there is change in session state */
thd->lex->safe_to_cache_query = 0;
}
}
/**
@brief Supply key to the hash implementation (to be used internally by the
implementation).
@param entry A single entry.
@param [out] length Length of the key.
@return Pointer to the key buffer.
*/
const uchar *Session_sysvars_tracker::sysvars_get_key(const uchar *entry,
size_t *length) {
char *key;
key = ((sysvar_node_st *)entry)->m_sysvar_name.str;
*length = ((sysvar_node_st *)entry)->m_sysvar_name.length;
return (uchar *)key;
}
/**
Prepare/reset the m_registered_sysvars hash for next statement.
*/
void Session_sysvars_tracker::reset() {
for (const auto &key_and_value : *orig_list) {
sysvar_node_st *node = key_and_value.second.get();
node->m_changed = false;
}
m_changed = false;
}
///////////////////////////////////////////////////////////////////////////////
/**
@brief Enable/disable the tracker based on @@session_track_schema's value.
@param thd The thd handle.
@return
false (always)
*/
bool Current_schema_tracker::update(THD *thd) {
m_enabled = (thd->variables.session_track_schema) ? true : false;
return false;
}
/**
@brief Store the schema name as length-encoded string in the specified
buffer. Once the data is stored, we reset the flags related to
state-change (see reset()).
@param thd The thd handle.
@param [in,out] buf Buffer to store the information to.
@return
false Success
true Error
*/
bool Current_schema_tracker::store(THD *thd, String &buf) {
ulonglong db_length, length;
length = db_length = thd->db().length;
length += net_length_size(length);
uchar *to =
(uchar *)buf.prep_append(net_length_size(length) + 1, EXTRA_ALLOC);
/* Session state type (SESSION_TRACK_SCHEMA) */
to = net_store_length(to, (ulonglong)SESSION_TRACK_SCHEMA);
/* Length of the overall entity. */
to = net_store_length(to, length);
/* Length of the changed current schema name. */
net_store_length(to, db_length);
/* Current schema name (length-encoded string). */
store_lenenc_string(buf, thd->db().str, thd->db().length);
reset();
return false;
}
/**
@brief Mark the tracker as changed.
@param thd Current thread
@param tracked_item_name Always null (unused).
*/
void Current_schema_tracker::mark_as_changed(
THD *thd, LEX_CSTRING *tracked_item_name MY_ATTRIBUTE((unused))) {
m_changed = true;
thd->lex->safe_to_cache_query = 0;
}
/**
@brief Reset the m_changed flag for next statement.
*/
void Current_schema_tracker::reset() { m_changed = false; }
///////////////////////////////////////////////////////////////////////////////
/**
@brief Constructor for transaction state tracker.
*/
Transaction_state_tracker::Transaction_state_tracker() {
m_enabled = false;
tx_changed = TX_CHG_NONE;
tx_curr_state = tx_reported_state = TX_EMPTY;
tx_read_flags = TX_READ_INHERIT;
tx_isol_level = TX_ISOL_INHERIT;
}
/**
@brief Enable/disable the tracker based on @@session_track_transaction_info's
value.
@param thd The thd handle.
@return
true if updating the tracking level failed, false otherwise
*/
bool Transaction_state_tracker::update(THD *thd) {
if (thd->variables.session_track_transaction_info != TX_TRACK_NONE) {
/*
If we only just turned reporting on (rather than changing between
state and chistics reporting), start from a defined state.
*/
if (!m_enabled) {
tx_curr_state = tx_reported_state = TX_EMPTY;
tx_changed |= TX_CHG_STATE;
m_enabled = true;
}
if (thd->variables.session_track_transaction_info == TX_TRACK_CHISTICS)
tx_changed |= TX_CHG_CHISTICS;
mark_as_changed(thd, NULL);
} else
m_enabled = false;
return false;
}
/**
@brief Store the transaction state (and, optionally, characteristics)
as length-encoded string in the specified buffer. Once the data
is stored, we reset the flags related to state-change (see reset()).
@param thd The thd handle.
@param [in,out] buf Buffer to store the information to.
@return
false Success
true Error
*/
bool Transaction_state_tracker::store(THD *thd, String &buf) {
/* STATE */
if (tx_changed & TX_CHG_STATE) {
uchar *to = (uchar *)buf.prep_append(11, EXTRA_ALLOC);
to = net_store_length((uchar *)to,
(ulonglong)SESSION_TRACK_TRANSACTION_STATE);
to = net_store_length((uchar *)to, (ulonglong)9);
to = net_store_length((uchar *)to, (ulonglong)8);
*(to++) = (tx_curr_state & TX_EXPLICIT)
? 'T'
: ((tx_curr_state & TX_IMPLICIT) ? 'I' : '_');
*(to++) = (tx_curr_state & TX_READ_UNSAFE) ? 'r' : '_';
*(to++) =
((tx_curr_state & TX_READ_TRX) || (tx_curr_state & TX_WITH_SNAPSHOT))
? 'R'
: '_';
*(to++) = (tx_curr_state & TX_WRITE_UNSAFE) ? 'w' : '_';
*(to++) = (tx_curr_state & TX_WRITE_TRX) ? 'W' : '_';
*(to++) = (tx_curr_state & TX_STMT_UNSAFE) ? 's' : '_';
*(to++) = (tx_curr_state & TX_RESULT_SET) ? 'S' : '_';
*(to++) = (tx_curr_state & TX_LOCKED_TABLES) ? 'L' : '_';
}
/* CHARACTERISTICS -- How to restart the transaction */
if ((thd->variables.session_track_transaction_info == TX_TRACK_CHISTICS) &&
(tx_changed & TX_CHG_CHISTICS)) {
bool is_xa =
!thd->get_transaction()->xid_state()->has_state(XID_STATE::XA_NOTR);
// worst case: READ UNCOMMITTED + READ WRITE + CONSISTENT SNAPSHOT
char buffer[110];
String tx(buffer, sizeof(buffer), &my_charset_bin);
tx.length(0);
// Any characteristics to print?
{
/*
We have four basic replay scenarios:
a) SET TRANSACTION was used, but before an actual transaction
was started, the load balancer moves the connection elsewhere.
In that case, the same one-shots should be set up in the
target session. (read-only/read-write; isolation-level)
b) The initial transaction has begun; the relevant characteristics
are the session defaults, possibly overridden by previous
SET TRANSACTION statements, possibly overridden or extended
by options passed to the START TRANSACTION statement.
If the load balancer wishes to move this transaction,
it needs to be replayed with the correct characteristics.
(read-only/read-write from SET or START;
isolation-level from SET only, snapshot from START only)
c) A subsequent transaction started with START TRANSACTION
(which is legal syntax in lieu of COMMIT AND CHAIN in MySQL)
may add/modify the current one-shots:
- It may set up a read-only/read-write one-shot.
This one-shot will override the value used in the previous
transaction (whether that came from the default or a one-shot),
and, like all one-shots currently do, it will carry over into
any subsequent transactions that don't explicitly override them
in turn. This behavior is not guaranteed in the docs and may
change in the future, but the tracker item should correctly
reflect whatever behavior a given version of mysqld implements.
- It may also set up a WITH CONSISTENT SNAPSHOT one-shot.
This one-shot does not currently carry over into subsequent
transactions (meaning that with "traditional syntax", WITH
CONSISTENT SNAPSHOT can only be requested for the first part
of a transaction chain). Again, the tracker item should reflect
mysqld behavior.
d) A subsequent transaction started using COMMIT AND CHAIN
(or, for that matter, BEGIN WORK, which is currently
legal and equivalent syntax in MySQL, or START TRANSACTION
sans options) will re-use any one-shots set up so far
(with SET before the first transaction started, and with
all subsequent STARTs), except for WITH CONSISTANT SNAPSHOT,
which will never be chained and only applies when explicitly
given.
It bears noting that if we switch sessions in a follow-up
transaction, SET TRANSACTION would be illegal in the old
session (as a transaction is active), whereas in the target
session which is being prepared, it should be legal, as no
transaction (chain) should have started yet.
Therefore, we are free to generate SET TRANSACTION as a replay
statement even for a transaction that isn't the first in an
ongoing chain. Consider
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITED;
START TRANSACTION READ ONLY, WITH CONSISTENT SNAPSHOT;
# work
COMMIT AND CHAIN;
If we switch away at this point, the replay in the new session
needs to be
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITED;
START TRANSACTION READ ONLY;
When a transaction ends (COMMIT/ROLLBACK sans CHAIN), all
per-transaction characteristics are reset to the session's
defaults.
This also holds for a transaction ended implicitly! (transaction.cc)
Once again, the aim is to have the tracker item reflect on a
given mysqld's actual behavior.
*/
/*
"ISOLATION LEVEL"
Only legal in SET TRANSACTION, so will always be replayed as such.
*/
if (tx_isol_level != TX_ISOL_INHERIT) {
/*
Unfortunately, we can't re-use tx_isolation_names /