1# ==== Purpose ====
2#
3# Check that anonymous ownership is acquired and released as expected.
4#
5# When a client sets GTID_NEXT='ANONYMOUS', it 'acquires anonymous
6# ownership'.  Then, when it commits or rolls back, it 'releases
7# anonymous ownership.  If GTID_NEXT='AUTOMATIC' and GTID_MODE=OFF or
8# OFF_PERMISSIVE, then a client 'acquires anonymous ownership' when
9# the transaction is flushed to the binary log, and releases it on
10# commit.  If a client already has GTID_NEXT='ANONYMOUS' but anonymous
11# ownership has been released by a commit or rollback, then anonymous
12# ownership is re-acquired when the next statement begins to execute.
13# As an exception, "innocent" statements do not re-acquire anonymous
14# ownership; here "innocent" statements include SET, SHOW, SELECT, and
15# DO statements that do not invoke stored functions.
16#
17# Multiple clients can 'hold anonymous ownership' at the same time.
18# The global status variable ONGOING_ANONYMOUS_TRANSACTION_COUNT
19# contains the number of clients that 'hold anonymous ownership'.  The
20# session variable @@session.gtid_owned contains the string
21# 'ANONYMOUS' if the client hold anonymous ownership.
22#
23# This test checks different ways to acquire and release ownership,
24# and verifies that ONGOING_ANONYMOUS_TRANSACTION_COUNT and
25# @@SESSION.GTID_OWNED change accordingly.
26#
27# ==== Implementation ====
28#
29# We test a number of statements.  For each statement we specify what
30# is the expected values of GTID_NEXT, GTID_OWNED, and
31# ONGOING_ANONYMOUS_TRANSACTION_COUNT after the statement.  Each
32# statement is stored together with the expected values of these
33# variables in one row of the table 'statements'.  Thus the test
34# scenarios are specified by the contents of this table in the
35# 'Initialize' section below.  In the 'Test' section, we execute one
36# row at a time from the table and check that the variables change as
37# specified.
38#
39# ==== References ====
40#
41# WL#7083: GTIDs: set gtid_mode=ON online
42# - Behavior of anonymous ownership was specified in this worklog.
43# - Most of test was implemented in this worklog.
44#
45# WL#7592: GTIDs: generate Gtid_log_event and Previous_gtids_log_event always
46# BUG#20341210: WL7592: ASSERT AT BINLOG.CC:1139 FOR IMPLICIT COMMIT WHEN GTID_NEXT=ANONYMOUS
47# - The bugfix was verified by the test case
48#   binlog_gtid_next_anonymous_implicit_commit.test, which was
49#   subsequently moved into the present test (the cases for implicit
50#   commit inside a transaction).
51
52# Test sets gtid_mode explicitly, no need to run in multiple combinations.
53--source include/not_gtid_enabled.inc
54
55--source include/have_debug_sync.inc
56
57--let $rpl_extra_connections_per_server= 3
58--let $rpl_topology= none
59--source include/rpl_init.inc
60
61
62--echo ==== Initialize ====
63
64SET GLOBAL ENFORCE_GTID_CONSISTENCY = ON;
65
66CREATE TABLE statements (
67  id INT PRIMARY KEY AUTO_INCREMENT,
68  comment VARCHAR(100) NOT NULL,
69  connection INT,
70  statement VARCHAR(1000),
71  gtid_next VARCHAR(100) NOT NULL,
72  gtid_owned VARCHAR(100) NOT NULL,
73  anonymous_count INT NOT NULL,
74  sync_point VARCHAR(100) NOT NULL,
75  error VARCHAR(100) NOT NULL
76) ENGINE = InnoDB;
77
78INSERT INTO statements
79(comment, connection, statement, gtid_next, gtid_owned, anonymous_count,
80 sync_point, error)
81VALUES
82#comment
83#con statement                       gtid_next    gtid_owned count sync error
84('Nothing should be owned by default.',
85 1, '',                              'AUTOMATIC', '',          0,  '',  ''),
86
87('Set gtid_next (automatic->anonymous) acquires ownership.',
88 1, 'SET GTID_NEXT="ANONYMOUS"',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
89
90('Implicit commit releases ownership.',
91 1, 'CREATE TABLE t1 (a INT)',       'ANONYMOUS', '',          0,  '',  ''),
92
93('Implicitly committing statement re-acquires ownership.',
94 1, 'CREATE TABLE t2 (a INT)',       '',          '',          1,  '->before_execute_sql_command',  ''),
95
96('Implicitly committing statement releases ownership at the end.',
97 1, '#CREATE TABLE t2 (a INT)',      'ANONYMOUS', '',          0,  'before_execute_sql_command->',  ''),
98
99('Set gtid_next (anonymous->anonymous) acquires ownership.',
100 1, 'SET GTID_NEXT="ANONYMOUS"',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
101
102('Nothing special happens with ownership while inside a transaction.',
103 1, 'BEGIN',                         'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
104('',
105 1, 'INSERT INTO t1 VALUES (1)',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
106
107('Commit releases ownership.',
108 1, 'COMMIT',                        'ANONYMOUS', '',          0,  '',  ''),
109
110('Begin acquires ownership.',
111 1, 'BEGIN',                         'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
112
113('Commit releases ownership even if nothing executed.',
114 1, 'COMMIT',                        'ANONYMOUS', '',          0,  '',  ''),
115
116('Rollback releases ownership.',
117 1, 'BEGIN',                         'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
118
119('',
120 1, 'ROLLBACK',                      'ANONYMOUS', '',          0,  '',  ''),
121
122('Implicit commit in transaction releases ownership.',
123 1, 'BEGIN',                         'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
124
125('',
126 1, 'INSERT INTO t1 VALUES (1)',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
127
128('',
129 1, 'DROP TABLE t2',                 'ANONYMOUS', '',          0,  '->after_implicit_pre_commit', ''),
130
131('',
132 1, '#DROP TABLE t2',                'ANONYMOUS', '',          0,  'after_implicit_pre_commit->', ''),
133
134('Autocommit transaction acquires ownership.',
135 1, 'INSERT INTO t1 VALUES (1)',     '',          '',          1,  '->before_execute_sql_command', ''),
136
137('Autocommit transaction releases ownership at end.',
138 1, '#INSERT INTO t1 VALUES (1)',    'ANONYMOUS', '',          0,  'before_execute_sql_command->', ''),
139
140('SET does not acquire ownership.',
141 1, 'SET AUTOCOMMIT = 0',            'ANONYMOUS', '',          0,  '',  ''),
142
143('Non-autocommitted DML acquires ownership.',
144 1, 'INSERT INTO t1 VALUES (1)',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
145('',
146 1, 'INSERT INTO t1 VALUES (1)',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
147('',
148 1, 'ROLLBACK',                      'ANONYMOUS', '',          0,  '',  ''),
149
150('Client disconnect releases ownership.',
151 1, 'BEGIN',                         'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
152('',
153 1, 'reconnect',                     'AUTOMATIC', '',          0,  '',  ''),
154
155('Ongoing_anonymous_transaction_count > 1 when there are concurrent transactions.',
156 1, 'SET GTID_NEXT="ANONYMOUS"',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
157('',
158 2, '',                              'AUTOMATIC', '',          1,  '',  ''),
159('',
160 2, 'SET GTID_NEXT="ANONYMOUS"',     'ANONYMOUS', 'ANONYMOUS', 2,  '',  ''),
161('',
162 3, '',                              'AUTOMATIC', '',          2,  '',  ''),
163('',
164 3, 'SET GTID_NEXT="ANONYMOUS"',     'ANONYMOUS', 'ANONYMOUS', 3,  '',  ''),
165('',
166 2, 'reconnect',                     'AUTOMATIC', '',          2,  '',  ''),
167('',
168 1, 'COMMIT',                        'ANONYMOUS', '',          1,  '',  ''),
169('',
170 3, 'ROLLBACK',                      'ANONYMOUS', '',          0,  '',  ''),
171
172('Set gtid_next (anonymous->automatic) works.',
173 1, 'SET GTID_NEXT="AUTOMATIC"',     'AUTOMATIC', '',          0,  '',  ''),
174
175('Set gtid_next (automatic->automatic) works.',
176 1, 'SET GTID_NEXT="AUTOMATIC"',     'AUTOMATIC', '',          0,  '',  ''),
177
178('Can set gtid_mode!=on when Ongoing_anonymous_transaction_count > 0.',
179 2, 'SET GTID_NEXT="ANONYMOUS"',     'ANONYMOUS', 'ANONYMOUS', 1,  '',  ''),
180('',
181 1, 'SET GLOBAL GTID_MODE="OFF_PERMISSIVE"','AUTOMATIC', '',   1,  '',  ''),
182('',
183 1, 'SET GLOBAL GTID_MODE="ON_PERMISSIVE"', 'AUTOMATIC', '',   1,  '',  ''),
184('',
185 1, 'SET GLOBAL GTID_MODE="ON_PERMISSIVE"', 'AUTOMATIC', '',   1,  '',  ''),
186('',
187 1, 'SET GLOBAL GTID_MODE="OFF_PERMISSIVE"','AUTOMATIC', '',   1,  '',  ''),
188('',
189 1, 'SET GLOBAL GTID_MODE="OFF_PERMISSIVE"','AUTOMATIC', '',   1,  '',  ''),
190('',
191 1, 'SET GLOBAL GTID_MODE="OFF"',    'AUTOMATIC', '',          1,  '',  ''),
192('',
193 1, 'SET GLOBAL GTID_MODE="OFF"',    'AUTOMATIC', '',          1,  '',  ''),
194('',
195 2, 'ROLLBACK',                      'ANONYMOUS', '',          0,  '',  '');
196
197
198--echo ==== Test ====
199
200--let $statement_count= `SELECT COUNT(*) FROM statements`
201--let $statement_number= 0
202while ($statement_number < $statement_count)
203{
204  --connection default
205  --let $suffix=            FROM statements ORDER BY id LIMIT $statement_number, 1
206  --let $comment=           `SELECT comment $suffix`
207  --let $statement=         `SELECT statement $suffix`
208  --let $connection_number= `SELECT connection $suffix`
209  --let $connection=        server_1_$connection_number
210  --let $gtid_next=         `SELECT gtid_next $suffix`
211  --let $gtid_owned=        `SELECT gtid_owned $suffix`
212  --let $anonymous_count=   `SELECT anonymous_count $suffix`
213  --let $sync_point=        `SELECT sync_point $suffix`
214  --let $error=             `SELECT error $suffix`
215
216  if ($comment != '')
217  {
218    --echo ---- Comment ----
219  }
220  --echo # Statement $statement_number:
221
222  --let $rpl_connection_name= $connection
223  --source include/rpl_connection.inc
224
225  if ($statement == 'reconnect')
226  {
227    # Get connection id.
228    --connection $connection
229    --let $thread_id= `SELECT CONNECTION_ID()`
230
231    # Disconnect
232    --connection default
233    --disconnect $connection
234
235    # Wait for session to disappear.
236    --let $wait_condition= SELECT COUNT(*) = 0 FROM performance_schema.threads WHERE PROCESSLIST_ID = $thread_id
237    --source include/wait_condition.inc
238
239    # Reconnect
240    --let $rpl_connection_name= $connection
241    --let $rpl_server_number= 1
242    --source include/rpl_connect.inc
243
244    --let $statement=
245  }
246
247  if ($sync_point != '')
248  {
249    --connection default
250    --let $sync_point_from= `SELECT SUBSTRING_INDEX('$sync_point', '->', 1)`
251    --let $sync_point_to= `SELECT SUBSTRING_INDEX('$sync_point', '->', -1)`
252    if ($sync_point_to != '')
253    {
254      --let $statement_connection= $connection
255      --let $auxiliary_connection= default
256      --let $sync_point= $sync_point_to
257      --source include/execute_to_sync_point.inc
258    }
259    if ($sync_point_from != '')
260    {
261      --let $statement_connection= $connection
262      --let $auxiliary_connection= default
263      --let $sync_point= $sync_point_from
264      --source include/execute_from_sync_point.inc
265    }
266  }
267
268  if ($sync_point == '')
269  {
270    if ($statement != '')
271    {
272      #Disabled because of BUG#13687542.
273      #if ($error != '')
274      #{
275      #  --error $error
276      #}
277
278      eval $statement;
279    }
280  }
281
282  # If we used execute_to_sync_point.inc above, then the connection
283  # has changed and we cannot execute on $connection. So skip the
284  # checks for session variables in that case.
285  if ($CURRENT_CONNECTION == $connection)
286  {
287    --let $actual_gtid_owned= `SELECT @@SESSION.GTID_OWNED`
288    --let $actual_gtid_next= `SELECT @@SESSION.GTID_NEXT`
289
290    --connection default
291    --let $assert_text= GTID_NEXT should be '$gtid_next'
292    --let $assert_cond= "$actual_gtid_next" = "$gtid_next"
293    --source include/assert.inc
294
295    --let $assert_text= GTID_OWNED should be '$gtid_owned'
296    --let $assert_cond= "$actual_gtid_owned" = "$gtid_owned"
297    --source include/assert.inc
298  }
299
300  --let $assert_text= ONGOING_ANONYMOUS_TRANSACTION_COUNT should be $anonymous_count
301  --let $assert_cond= [SHOW STATUS LIKE "ONGOING_ANONYMOUS_TRANSACTION_COUNT", Value, 1] = $anonymous_count
302  --source include/assert.inc
303
304  --inc $statement_number
305}
306
307--connection server_1
308SET GTID_NEXT = 'ANONYMOUS';
309--connection server_1_1
310SET GLOBAL GTID_MODE = 'OFF_PERMISSIVE';
311SET GLOBAL GTID_MODE = 'ON_PERMISSIVE';
312--error ER_CANT_SET_GTID_MODE
313--connection server_1
314ROLLBACK;
315--connection server_1_1
316SET GLOBAL GTID_MODE = 'ON';
317--connection server_1
318SELECT @@GLOBAL.GTID_MODE;
319--error ER_CANT_SET_GTID_NEXT_TO_ANONYMOUS_WHEN_GTID_MODE_IS_ON
320SET GTID_NEXT = 'ANONYMOUS';
321
322
323--echo ==== Clean up ====
324
325SET GLOBAL GTID_MODE = 'ON_PERMISSIVE';
326SET GLOBAL GTID_MODE = 'OFF_PERMISSIVE';
327SET GLOBAL GTID_MODE = 'OFF';
328DROP TABLE statements;
329DROP TABLE t1;
330SET GLOBAL ENFORCE_GTID_CONSISTENCY = OFF;
331
332--source include/rpl_end.inc
333