Allow to observe binlog cache of foreign threads

Summary:
This patch adds a new SHOW command to output binlog cache content for a
specified thread. The command format is as follows:

SHOW BINLOG CACHE [FOR thread_id] [LIMIT [offset,] row_count]

Notes: the output columns are "Event_type", "Server_id" and "Info". I didn't
add the thread_id in the output, since I think it appearing in the command
makes the output clear. By default (i.e. without thread_id option), it shows
the own binlog cache.

Some implementation notes:

The only lock I grabbed is the foreign thread's LOCK_thd_data to prevent
the thread being deleted. For the binlog cache, I made a copy (snapshot)
of the cache metadata (IO_CACHE), and reset the WRITE_CACHE to
READ_CACHE of the copy. When we read the cache content, we create a
log_event object from the buffer. In this way, the original cache
metadata are not affected and the foreign thread can continue
writing/appending to it. And the original cache buffer is protected by
LOCK_thd_data so that it won't be freed during reading. The side effect
is if the cache content is altered or cleared during reading, we may
read garbage data. That's OK since invalid log_event will not be
outputted.

Test Plan:
Added unit test cases in the new test file "show_binlog_cache.test".

Reviewers: santoshb

Reviewed By: santoshb
Improve mysqlbinlog --exclude-gtids

Summary:
https://phabricator.fb.com/D1357602 added
the functionality to use --exclude-gtids without giving the
binlog file name to mysqlbinlog. mysqlbinlog --exclude-gtids
without --to-last-log continues reading the binary logs
instead of reading only one binary log figured out using gtid
auto position which is confusing.

Fix this issue. Now mysqlbinlog --exclude-gtids reads only one
binary log. But mysqlbinlog --exclude-gtids --to-last-log
continues reading the binary logs until it reaches the end
of current log.

Test Plan:
mysqlbinlog --exclude-gtids on a test shadow reads only
one binary log. mysqlbinlog --exclude-gtids --to-last-log continues
reading the binary logs until it reaches the end.

Added the same in the test case.

Reviewers: rongrong

Reviewed By: rongrong
Make defragment_thead more friendly

Summary:
1. Fixing a bug in defragmentation that ignores async when release memory used by pcur.
2. Use C++ STL to implement defragment work queue replacing ib_wqueue_t.
3. Added a innodb_defragment_frequency parameter to set how frequent the defragment_thread can acquire x_lock on a single thread. Comment from code:
 "Do not defragment a single index more than this number of time per second."
  "This controls the number of time defragmentation thread can request X_LOCK "
  "on an index. Defragmentation thread will check whether "
  "1/defragment_frequency (s) has passed since it worked on this index last "
  "time, and put the index back to the queue if not enough time has passed. "
  "The actual frequency can only be lower than this given number."
4. Added a sys var innodb_defragment to enable/disable defragmentation. The parameter is TRUE by default, which means allow defragmentation. When it's set to FALSE, defragmentation will be poused. This means that all defragmentations already started will be paused and resume once defragmentation is re-enabled. But new defragmentation commands issued after defragmentation is disabled will fail.
5. Added a innodb status variable to track how frequent did the defragment_thread reorganizes page, but didn't end up freeing any. Idealy we want this to be 0 so all write amplification would attribute to something.
6. Added support to kill the defragment query.
7. Return error ER_SP_ALREADY_EXISTS when trying to defragment an already in-the-queue index. (error code is borrowed, will print out query '...' failed: 1304: table_name index_name already exists.)
8. Add THD_STAGE_INFO() when defragmentation start so show processlist will show more meaningful status info.

Test Plan:
* modified alter_table_defragment test to test async query (feature 1)
* added innodb_defragment_frequency_basic (feature 3)
* added innodb_defragment_basic (feature 4)
* Modified innodb_alter_table_defragment to test kill query (feature 6)
* modified innodb_alter_table_defragment to test index already in the queue (feature 7)
* other features are tested on shadow.

Reviewers: jtolmer, santoshb

Reviewed By: santoshb
fix rpl_gtid_mts_stress_crash

Summary:
slave options for rpl_gtid_mts_stress_crash
is changed to use innodb_flush_log_at_trx_commit=0.
The tests does a kill -9 on slave which caused
slave to miss some transactions that are applied
directly on slave. Use a flush logs and wait
for those transaction to get applied. Note that
flush logs forces innodb to synchronously flush
the transaction log.

Test Plan: mtr_run --repeat=32 rpl_gtid_mts_stress_crash

Reviewers: rudradevbasak, jtolmer

Reviewed By: jtolmer
Make kill defragment query and drop table stable

Summary:
1. In release build innodb.innodb_alter_table_defragment on kill query is not stable because
there's a delay that query thread realizes it's been killed. Stablize the test.
Also make sure when the query is killed it won't move on to defragment another index of
the table.
2. When a drop table command is issued while defragment is running, there could be issues
because defragment thread don't open the table so the table's ref count could drop to 0
if the defragment command is async (which also won't keep the table open).
Make sure we mark the table as removed when a drop table command is issued.

Test Plan:
Tested with parallel=8 and repeat=8 for release build.
mysqltest.sh --release --repeat=8 --parallel=8 innodb.innodb_alter_table_defragment innodb.innodb_alter_table_defragment innodb.innodb_alter_table_defragment innodb.innodb_alter_table_defragment innodb.innodb_alter_table_defragment innodb.innodb_alter_table_defragment innodb.innodb_alter_table_defragment innodb.innodb_alter_table_defragment

Reviewers: jtolmer, santoshb

Reviewed By: santoshb
Add thread_id option to show variables and show status

Summary:
This is to add a thread_id option to the show variables and show status commands. The new syntax of the two commands are as follows:

`SHOW [GLOBAL | SESSION [THREAD_ID]] VARIABLES [LIKE 'pattern' | WHERE expr]`
`SHOW [GLOBAL | SESSION [THREAD_ID]] STATUS [LIKE 'pattern' | WHERE expr]`

Notes:
- The thread id can be specified only with "SESSION" or "LOCAL" keyword.
- By default, SHOW will output the current thread's variables and status.
- SHOW SESSION [OWN THREAD ID] is equivelant to the default SHOW SESSION.
- The current thread does not have sufficient (admin) access right to the other threads, the command will fail.
- If a thread does not exist, the command will also fail.

Test Plan: Added new regression tests in show_thread_status_var.test

Reviewers: rongrong

Reviewed By: rongrong
fix segmentation fault in MTS

Summary:
fix segmentation fault in multi-threaded slave
code path (Slave_worker::commit_positions).

STOP SLAVE may result in partial transaction with
only GTID_NEXT event or BEGIN event. The job_group
information for the partial transaction which has
garbage information is enqueued to Group Assigned Queue(gaq),
but rli->checkpoint_seqno is not incremented. The logic
in MTS uses rli->checkpoint_seqno to determine whether
gaq is full or not (i still don't get the rationale
for this kind of design, using gaq->full() function
is much cleaner). Since rli->checkpoint_seqno
is not incremented previously, sql_thread tries to en_queue
a new job group even though gaq is full and fails. There is
no error check for this failure in the code

  gaq->assigned_group_index= gaq->en_queue((void *) &group);

assigned_group_index will be (ulong) -1 in this case which
points to garbage index in gaq. Slave worker thread may hit
seg fault when accessing the memory pointed by the index
(ulong) -1 in gaq.

Test Plan:
stop slave; start slave; under gdb on a shadow.
To deterministically repro the failure, i instrumented
the code to check whether gaq->en_queue() fails.
Without this fix, mysqld may hit seg-fault after stop slave; start slave;

Reviewers: jtolmer, rongrong

Reviewed By: rongrong
Fix low master_heartbeat_period causing slave error 1595

Summary:
Fix heartbeat bug http://bugs.mysql.com/bug.php?id=73087
To avoid the errors, Slave io_thread skips relay logging the heartbeats
when mi_description_event is old i,e; < 4.

Test Plan: mtr_test case hits error 1595 deterministically without the fix.

Reviewers: jtolmer

Reviewed By: jtolmer
Adding test for innodb_defragment_fill_factor

Summary:
Added test innodb.innodb_defragment_fill_factor to test the effectiveness of fill factor variables.
For table with large records, the effective variable is innodb_defragment_fill_factor, which is set
to 0.9 by default. For table with small records, the effective variable is
innodb_defragment_fill_factor_n_recs, which is set to 20 by default.

Test Plan: innodb.innodb_defragment_fill_factor

Reviewers: steaphan, santosh, santoshb

Reviewed By: santoshb
Prune dead code from xtrabackup.cc

Summary:
The code for xtrabackup was designed to work for a bunch of different
versions of mysql.  These are all either obsolete, or Percona-specific
except for 5.6, which is all we care about here, since we don't maintain
this as a separate diff, but as an integral part of our mysql repository.

So, we don't need all this other stuff.  All of this removed here is
completely dead code.  This should be a non-change.

Test Plan: Our xtrabackup mtr tests continue to pass.

Reviewers: tianx

Reviewed By: tianx
Assert a sane size when xtrabackup enlarges ibd file

Summary:
This should prevent it from forcing the ibdata file to grow > 512M pages
(which is 8T, for 16K pages).

Test Plan:
I am building a test rpm and testing this version on my repro set of instances, to make sure it fails right.

We also need to test this succeeds on a large, normal, copy, somewhere.

Reviewers: ebergen, santoshb

Reviewed By: santoshb
Revert "Write old FB crcs by default"

Summary:
This change was to ensure that, in a worst-case scenario, we could
always fall back to running 5.1 on the same data.  We no longer need
that, as 5.1 is extinct in all our production environments, and we
are now confident that any non-production stragglers will not need to
downgrade back after being upgraded to 5.6.

So, this removes this, making the written checksums of our branch
fully compatible with the new 5.6 checksums (CRC32 or CRC32_STRICT).

Test Plan:
This change was actually kept as a separate diff for this very purpose.
We have already tested this functionality without this diff.

Reviewers: jtolmer

Reviewed By: jtolmer