-
Notifications
You must be signed in to change notification settings - Fork 1
/
pt-online-schema-change-plugin-to-ramdisk.pm
56 lines (46 loc) · 1.9 KB
/
pt-online-schema-change-plugin-to-ramdisk.pm
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
##### pt_online_schema_change_plugin - Move to ramdisk for MyISAM table
# NinhSTH
# 20160227
# Base on: https://github.com/percona/percona-toolkit/blob/43aaf2588d73614e8c115e5b40dd6903438a8e3a/t/pt-online-schema-change/samples/plugins/block_create_triggers.pm
# Usage: only add following to pt_online_schema_change command: " --plugin path-to-this-script"
package pt_online_schema_change_plugin;
use strict;
use warnings FATAL => 'all';
use File::Path qw(make_path);
use Data::Dump 'dump';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
use constant RAMDISK_DIR => "/dev/shm/mysql";
sub new {
my ($class, %args) = @_;
my $self = { %args };
return bless $self, $class;
}
sub init {
my ($self, %args) = @_;
print "PLUGIN: init()\n";
$self->{orig_tbl} = $args{orig_tbl};
}
sub after_alter_new_table {
my $plugin_name = "PLUGIN: after_alter_new_table()";
print "$plugin_name: START\n";
my ($self, %args) = @_;
my $new_tbl = $args{new_tbl};
my $dbh = $self->{cxn}->dbh;
my $row = $dbh->selectrow_arrayref("SHOW CREATE TABLE $new_tbl->{name}");
my $new_tbl_ramdisk = $row->[1];
#dump $new_tbl;
my $dir_option = " DATA DIRECTORY='".RAMDISK_DIR."/$new_tbl->{db}/' INDEX DIRECTORY='".RAMDISK_DIR."/$new_tbl->{db}/'";
if ((index($new_tbl_ramdisk, $dir_option) == -1) && (lc $new_tbl->{tbl_struct}->{engine} eq "myisam")) {
print "$plugin_name: $new_tbl->{name} is not on Ramdisk and is MyISAM: moving to Ramdisk\n";
make_path("$ramdisk_dir_db", {owner=>'mysql', group=>'mysql'});
$new_tbl_ramdisk = $new_tbl_ramdisk . $dir_option;
print "$plugin_name: DROP and CREATE new table on Ramdisk\n $new_tbl_ramdisk\n\n";
$dbh->do("DROP TABLE IF EXISTS $new_tbl->{name}");
$dbh->do("$new_tbl_ramdisk");
} else {
print "$plugin_name: $new_tbl->{name} is already on Ramdisk or not MyISAM\n";
}
warn "$plugin_name: DONE\n";
}
1;