| 1 | package ShardedKV::Storage::MySQL; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | $ShardedKV::Storage::MySQL::VERSION = '0.20'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: MySQL storage backend for ShardedKV | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Time::HiRes qw(sleep); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use ShardedKV::Error::ConnectFail; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use ShardedKV::Error::DeleteFail; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use ShardedKV::Error::ReadFail; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | with 'ShardedKV::Storage'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | has 'mysql_connector' => ( | |||||||||||||||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | isa => 'CodeRef', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | has 'mysql_endpoint' => ( | |||||||||||||||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | isa => 'CodeRef', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # This could be shared among many "::Storage::MySQL" objects since we're | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # single-threaded (and they would not work across multiple ithreads anyway). | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # All that fancy logic would be done by the user-supplied connector code ref | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # above which needs to know how to obtain a new or existing connection. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # This means that we can make each Storage::MySQL object be specific to | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # a particular table! | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | has 'mysql_connection' => ( | |||||||||||||||||||||||||||||||||||||||||
| 37 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | builder => 'build_mysql_connection', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | clearer => 'clear_mysql_connection', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | predicate => 'has_mysql_connection', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub build_mysql_connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_mysql_connection: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my $logger = $self->logger; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $logger->debug("Getting mysql connection") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my $dbh = $self->mysql_connector->(); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ($dbh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $logger->debug("Get connection") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $dbh->{RaiseError} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $dbh->{PrintError} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 54 | #$dbh->{AutoCommit} = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $endpoint = $self->mysql_endpoint->(); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ShardedKV::Error::ConnectFail->throw({ | |||||||||||||||||||||||||||||||||||||||||
| 59 | endpoint => $endpoint, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | storage_type => 'mysql', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | message => "Failed to make a connection to MySQL ($endpoint): ${\$DBI::errstr}", | |||||||||||||||||||||||||||||||||||||||||
| 62 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $logger->warn("Failed to get connection") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | has 'table_name' => ( | |||||||||||||||||||||||||||||||||||||||||
| 70 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 71 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 72 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | has 'key_col_name' => ( | |||||||||||||||||||||||||||||||||||||||||
| 77 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 78 | default => "keystr", | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | has 'key_col_type' => ( | |||||||||||||||||||||||||||||||||||||||||
| 83 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | default => "VARBINARY(16) NOT NULL", | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | has 'auto_increment_col_name' => ( | |||||||||||||||||||||||||||||||||||||||||
| 89 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 90 | default => 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | has 'auto_increment_col_type' => ( | |||||||||||||||||||||||||||||||||||||||||
| 95 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 96 | default => "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT", | ||||||||||||||||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | has 'value_col_names' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 101 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | default => sub {[qw(val last_change)]} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | has 'value_col_types' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 108 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | default => sub {[ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | 'MEDIUMBLOB NOT NULL', | |||||||||||||||||||||||||||||||||||||||||
| 112 | 'TIMESTAMP NOT NULL', | ||||||||||||||||||||||||||||||||||||||||||
| 113 | #'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', | ||||||||||||||||||||||||||||||||||||||||||
| 114 | ]}, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | has 'extra_indexes' => ( | |||||||||||||||||||||||||||||||||||||||||
| 119 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 120 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 121 | default => '', | ||||||||||||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | has 'max_num_reconnect_attempts' => ( | |||||||||||||||||||||||||||||||||||||||||
| 126 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 127 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 128 | default => 5, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | has 'reconnect_interval' => ( | |||||||||||||||||||||||||||||||||||||||||
| 133 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 134 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 135 | default => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | has 'refresh_mysql_connection_on_error_regex' => ( | |||||||||||||||||||||||||||||||||||||||||
| 140 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 141 | isa => 'Maybe[Regexp]', | ||||||||||||||||||||||||||||||||||||||||||
| 142 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 143 | builder => '_make_refresh_mysql_connection_on_error_regex', | ||||||||||||||||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | # Could be prepared, but that is kind of nasty wrt. reconnects, so let's not go | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # there unless we have to! | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 3 | has '_get_query' => ( | |||||||||||||||||||||||||||||||||||||||||
| 149 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 150 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 151 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | builder => '_make_get_query', | ||||||||||||||||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 3 | has '_set_query' => ( | |||||||||||||||||||||||||||||||||||||||||
| 156 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 157 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 158 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 159 | builder => '_make_set_query', | ||||||||||||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 3 | has '_delete_query' => ( | |||||||||||||||||||||||||||||||||||||||||
| 163 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 164 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 165 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | builder => '_make_delete_query', | ||||||||||||||||||||||||||||||||||||||||||
| 167 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | has '_number_of_params' => ( | |||||||||||||||||||||||||||||||||||||||||
| 170 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 172 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | builder => '_calc_no_params', | ||||||||||||||||||||||||||||||||||||||||||
| 174 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | $_[0]->_number_of_params; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $_[0]->refresh_mysql_connection_on_error_regex; | |||||||||||||||||||||||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub _calc_no_params { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_calc_no_params: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | return 1 + scalar(@{$self->value_col_names}); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub _make_get_query { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_get_query: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | $self->_number_of_params; # prepopulate | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $tbl = $self->table_name; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my ($key_col, $v_cols) = map $self->$_, qw(key_col_name value_col_names); | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my $v_col_str = join ',', @$v_cols; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | my $q = qq{SELECT $v_col_str FROM $tbl WHERE $key_col = ? LIMIT 1}; | |||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my $logger = $self->logger; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | $logger->debug("Generated the following get-query:\n$q") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | return $q; | |||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub _make_set_query { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_set_query: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $tbl = $self->table_name; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my ($key_col, $v_cols) = map $self->$_, qw(key_col_name value_col_names); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my $vcol_str = join ", ", @$v_cols; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | my $vcol_assign_str = ''; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | $vcol_assign_str .= "$_ = VALUES($_)," for @$v_cols; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | chop $vcol_assign_str; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | my $qs = join( ',', ('?') x $self->_number_of_params ); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | my $q = qq{ | |||||||||||||||||||||||||||||||||||||||||
| 211 | INSERT INTO $tbl ($key_col, $vcol_str) VALUES ($qs) | ||||||||||||||||||||||||||||||||||||||||||
| 212 | ON DUPLICATE KEY UPDATE | ||||||||||||||||||||||||||||||||||||||||||
| 213 | $vcol_assign_str | ||||||||||||||||||||||||||||||||||||||||||
| 214 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $logger = $self->logger; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | $logger->debug("Generated the following set-query:\n$q") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | return $q; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub _make_delete_query { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_delete_query: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $self->_number_of_params; # prepopulate | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | my $tbl = $self->table_name; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | my $key_col = $self->key_col_name; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | my $q = qq{DELETE FROM $tbl WHERE $key_col = ? LIMIT 1}; | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | my $logger = $self->{logger}; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | $logger->debug("Generated the following delete-query:\n$q") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | return $q; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | sub _make_refresh_mysql_connection_on_error_regex { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_refresh_mysql_connection_on_error_regex: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 237 | # Does this really need to be /i and can't it just be bounded? Not | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # changing how it was in the original source because I didn't have | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # time to check. | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | return qr/(?:MySQL server has gone away|Lost connection to MySQL server during query)/i; | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | sub prepare_table { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_table: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | $self->_number_of_params; # prepopulate | |||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my $tbl = $self->table_name; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my ($key_col, $key_type, $ainc_col, $ainc_type, $v_cols, $v_types) | |||||||||||||||||||||||||||||||||||||||||
| 249 | = map $self->$_, | ||||||||||||||||||||||||||||||||||||||||||
| 250 | qw(key_col_name key_col_type | ||||||||||||||||||||||||||||||||||||||||||
| 251 | auto_increment_col_name auto_increment_col_type | ||||||||||||||||||||||||||||||||||||||||||
| 252 | value_col_names value_col_types); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my @vcoldefs = map "$v_cols->[$_] $v_types->[$_]", 0..$#$v_cols; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | my $vcol_str = join ",\n", @vcoldefs; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | my $extra_indexes = $self->extra_indexes; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | if (not defined $extra_indexes or $extra_indexes !~ /\S/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $extra_indexes && $extra_indexes =~ /\S/
MC/DC0%0/2
defined $extra_indexes && $extra_indexes =~ /\S/
defined $extra_indexes
$extra_indexes =~ /\S/
Truth table0%0/3
A && B
A= defined $extra_indexesB= $extra_indexes =~ /\S/
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | $extra_indexes = ''; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | $extra_indexes = ",\n$extra_indexes"; | |||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | my $pk; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | my $ainc_col_spec = ''; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | if (defined $ainc_col) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | $pk = "PRIMARY KEY($ainc_col),\n" | |||||||||||||||||||||||||||||||||||||||||
| 266 | . "UNIQUE KEY ($key_col)"; | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $ainc_col_spec = "$ainc_col $ainc_type,"; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $pk = "PRIMARY KEY($key_col)"; | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | my $q = qq{ | |||||||||||||||||||||||||||||||||||||||||
| 273 | CREATE TABLE IF NOT EXISTS $tbl ( | ||||||||||||||||||||||||||||||||||||||||||
| 274 | $ainc_col_spec | ||||||||||||||||||||||||||||||||||||||||||
| 275 | $key_col $key_type, | ||||||||||||||||||||||||||||||||||||||||||
| 276 | $vcol_str, | ||||||||||||||||||||||||||||||||||||||||||
| 277 | $pk | ||||||||||||||||||||||||||||||||||||||||||
| 278 | $extra_indexes | ||||||||||||||||||||||||||||||||||||||||||
| 279 | ) ENGINE=InnoDb | ||||||||||||||||||||||||||||||||||||||||||
| 280 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | my $logger = $self->logger; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | $logger->info("Creating shard storage table:\n$q") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | $self->mysql_connection->do($q); | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | # Might not reconnect if the mysql_connector code ref just returns | ||||||||||||||||||||||||||||||||||||||||||
| 290 | # a cached connection. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | sub refresh_connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
refresh_connection: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | my $logger = $self->logger; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | $logger->info("Refreshing mysql connection") if $logger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | $self->clear_mysql_connection; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | return $self->mysql_connection; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub _run_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_sql: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | my ($self, $method, $query, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | my $iconn; | |||||||||||||||||||||||||||||||||||||||||
| 305 | my $rv; | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | my $refresh_mysql_connection_on_error_regex = $self->{refresh_mysql_connection_on_error_regex}; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my $dbh = $self->mysql_connection; | |||||||||||||||||||||||||||||||||||||||||
| 309 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | $rv = $dbh->$method($query, @args); | |||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ++$iconn; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | if (defined $refresh_mysql_connection_on_error_regex | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $refresh_mysql_connection_on_error_regex and $error =~ /$refresh_mysql_connection_on_error_regex/ and $iconn <= $self->max_num_reconnect_attempts
defined $refresh_mysql_connection_on_error_regex and $error =~ /$refresh_mysql_connection_on_error_regex/
MC/DC0%0/3
defined $refresh_mysql_connection_on_error_regex and $error =~ /$refresh_mysql_connection_on_error_regex/ and $iconn <= $self->max_num_reconnect_attempts
defined $refresh_mysql_connection_on_error_regex
$error =~ /$refresh_mysql_connection_on_error_regex/
$iconn <= $self->max_num_reconnect_attempts
Truth table0%0/4
A and B and C
A= defined $refresh_mysql_connection_on_error_regexB= $error =~ /$refresh_mysql_connection_on_error_regex/C= $iconn <= $self->max_num_reconnect_attempts
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | and $error =~ $refresh_mysql_connection_on_error_regex | ||||||||||||||||||||||||||||||||||||||||||
| 317 | and $iconn <= $self->max_num_reconnect_attempts) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | { | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | sleep($self->reconnect_interval * 2 ** ($iconn-2)) if $iconn > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | $self->refresh_connection; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | redo; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | Carp::confess("Despite trying hard: $error"); | |||||||||||||||||||||||||||||||||||||||||
| 324 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | return $rv; | |||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my $rv; | |||||||||||||||||||||||||||||||||||||||||
| 334 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | $rv = $self->_run_sql('selectall_arrayref', $self->_get_query, undef, $key); | |||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my $endpoint = $self->mysql_endpoint->(); | |||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ShardedKV::Error::ReadFail->throw({ | |||||||||||||||||||||||||||||||||||||||||
| 341 | endpoint => $endpoint, | ||||||||||||||||||||||||||||||||||||||||||
| 342 | key => $key, | ||||||||||||||||||||||||||||||||||||||||||
| 343 | storage_type => 'mysql', | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | message => "Failed to fetch key ($key) from MySQL ($endpoint): $error, ${\$DBI::errstr} ", | |||||||||||||||||||||||||||||||||||||||||
| 345 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 346 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | return ref($rv) ? $rv->[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | my ($self, $key, $value_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | Carp::croak("Need exactly " . ($self->{_number_of_params}-1) . " values, got " . scalar(@$value_ref)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | if not scalar(@$value_ref) == $self->_number_of_params-1; | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my $rv; | |||||||||||||||||||||||||||||||||||||||||
| 357 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | $rv = $self->_run_sql('do', $self->_set_query, undef, $key, @$value_ref); | |||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | my $endpoint = $self->mysql_endpoint->(); | |||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | ShardedKV::Error::ReadFail->throw({ | |||||||||||||||||||||||||||||||||||||||||
| 364 | endpoint => $endpoint, | ||||||||||||||||||||||||||||||||||||||||||
| 365 | key => $key, | ||||||||||||||||||||||||||||||||||||||||||
| 366 | storage_type => 'mysql', | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | message => "Failed to fetch key ($key) from MySQL ($endpoint): $error, ${\$DBI::errstr}", | |||||||||||||||||||||||||||||||||||||||||
| 368 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 369 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | return $rv ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | my $rv; | |||||||||||||||||||||||||||||||||||||||||
| 376 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | $rv = $self->_run_sql('do', $self->_delete_query, undef, $key); | |||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | my $endpoint = $self->mysql_endpoint->(); | |||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ShardedKV::Error::DeleteFail->throw({ | |||||||||||||||||||||||||||||||||||||||||
| 383 | endpoint => $endpoint, | ||||||||||||||||||||||||||||||||||||||||||
| 384 | key => $key, | ||||||||||||||||||||||||||||||||||||||||||
| 385 | storage_type => 'mysql', | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | message => "Failed to delete key ($key) to MySQL ($endpoint): $error, ${\$DBI::errstr}", | |||||||||||||||||||||||||||||||||||||||||
| 387 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 388 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | ▶ | return $rv ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | ▶ | sub reset_connection { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_connection: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | $self->clear_mysql_connection(); | |||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 3 | ▶ | no Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | ShardedKV::Storage::MySQL - MySQL storage backend for ShardedKV | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | version 0.20 | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | use ShardedKV; | ||||||||||||||||||||||||||||||||||||||||||
| 413 | use ShardedKV::Storage::MySQL; | ||||||||||||||||||||||||||||||||||||||||||
| 414 | ... create ShardedKV... | ||||||||||||||||||||||||||||||||||||||||||
| 415 | my $storage = ShardedKV::Storage::MySQL->new( | ||||||||||||||||||||||||||||||||||||||||||
| 416 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 417 | ... put storage into ShardedKV... | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | # values are array references | ||||||||||||||||||||||||||||||||||||||||||
| 420 | $skv->set("foo", ["bar"]); | ||||||||||||||||||||||||||||||||||||||||||
| 421 | my $value_ref = $skv->get("foo"); | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | A C<ShardedKV> storage backend that C<DBI> and C<DBD::mysql> to | ||||||||||||||||||||||||||||||||||||||||||
| 426 | store data in a MySQL table. | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | Implements the C<ShardedKV::Storage> role. | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | Each shard (== C<ShardedKV::Storage::MySQL> object) is represented by a | ||||||||||||||||||||||||||||||||||||||||||
| 431 | single table in some schema on some database server. | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =head1 PUBLIC ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | =head2 mysql_connector | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | A callback that must be supplied at object creation time. The storage | ||||||||||||||||||||||||||||||||||||||||||
| 438 | object will invoke the callback whenever it needs to get a NEW mysql | ||||||||||||||||||||||||||||||||||||||||||
| 439 | database handle. This means when: | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | - first connecting | ||||||||||||||||||||||||||||||||||||||||||
| 442 | - Error matches refresh_mysql_connection_on_error_regex => reconnect | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | The callback allows users to hook into the connection logic to implement | ||||||||||||||||||||||||||||||||||||||||||
| 445 | things such as connection caching. If you do use connection caching, then | ||||||||||||||||||||||||||||||||||||||||||
| 446 | do assert that the dbh is alive (eg. using C<$dbh-E<gt>ping()> before | ||||||||||||||||||||||||||||||||||||||||||
| 447 | returning a cached connection. | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | =head2 mysql_endpoint | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | A callback that should return a unique string that represents the endpoint. In | ||||||||||||||||||||||||||||||||||||||||||
| 452 | most cases this should be a hostname to allow for debugging connection issues. | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | The callback allows users to hook into the connection logic and update the | ||||||||||||||||||||||||||||||||||||||||||
| 455 | string that represents the particular endpoint that this storage instance | ||||||||||||||||||||||||||||||||||||||||||
| 456 | represents. | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | =head2 mysql_connection | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | This is the public attribute holding a MySQL database handle (which was | ||||||||||||||||||||||||||||||||||||||||||
| 461 | created using the C<mysql_connector>). Do not supply this at object | ||||||||||||||||||||||||||||||||||||||||||
| 462 | creation. | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | =head2 table_name | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | The name of the table that represents this shard. | ||||||||||||||||||||||||||||||||||||||||||
| 467 | Must be supplied at object creation. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | =head2 key_col_name | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | The name of the column to be used for the key. | ||||||||||||||||||||||||||||||||||||||||||
| 472 | If C<ShardedKV::Storage::MySQL> creates the shard table for you, then | ||||||||||||||||||||||||||||||||||||||||||
| 473 | this column is also used as the primary key unless | ||||||||||||||||||||||||||||||||||||||||||
| 474 | C<auto_increment_col_name> is set (see below). | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | There can only be one key column. | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | Defaults to 'keystr'. | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | =head2 key_col_type | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | The MySQL type of the key column. | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | Defaults to 'VARBINARY(16) NOT NULL'. | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | =head2 auto_increment_col_name | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | The name of the column to be used for the auto-increment pimrary key. | ||||||||||||||||||||||||||||||||||||||||||
| 489 | This is a virtually unused (by ShardedKV) column that, IF DEFINED, will | ||||||||||||||||||||||||||||||||||||||||||
| 490 | be used as an auto-increment primary key. It is not the column used to | ||||||||||||||||||||||||||||||||||||||||||
| 491 | fetch rows by, but rather facilitates faster insertion of new records | ||||||||||||||||||||||||||||||||||||||||||
| 492 | by allowing append instead of insertion at random order within the PK | ||||||||||||||||||||||||||||||||||||||||||
| 493 | tree. | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | If C<ShardedKV::Storage::MySQL> creates the shard table for you, then | ||||||||||||||||||||||||||||||||||||||||||
| 496 | this column is also used as the primary key. | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | There can only be one auto-increment key column. | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | Defaults to 'id'. | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | =head2 auto_increment_col_type | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | The MySQL type of the auto increment column. | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | Defaults to 'BIGINT UNSIGNED NOT NULL AUTO_INCREMENT'. | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | =head2 value_col_names | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | An array reference containing the names of all value columns in | ||||||||||||||||||||||||||||||||||||||||||
| 511 | the shard table. Needs to contain at least one value column. | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | Defaults to C<[qw(val last_change)]>. | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | =head2 value_col_types | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | An array reference containing the MySQL types of each value column | ||||||||||||||||||||||||||||||||||||||||||
| 518 | given in C<value_col_names>. | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | Defaults to: C<['MEDIUMBLOB NOT NULL', 'TIMESTAMP NOT NULL']>. | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | =head2 extra_indexes | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | A string that is included verbatim after the PRIMARY KEY line of the | ||||||||||||||||||||||||||||||||||||||||||
| 525 | CREATE TABLE IF NOT EXISTS statement that this class generates. This can be | ||||||||||||||||||||||||||||||||||||||||||
| 526 | used to add additional indexes to the shard tables, such as indexes on the | ||||||||||||||||||||||||||||||||||||||||||
| 527 | last modification (for expiration from the database, not handled by ShardedKV). | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | =head2 max_num_reconnect_attempts | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | The maximum number of reconnect attempts that the storage object | ||||||||||||||||||||||||||||||||||||||||||
| 532 | should perform if the MySQL server has gone away. | ||||||||||||||||||||||||||||||||||||||||||
| 533 | Reconnects are done with exponential back-off (see below). | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | Defaults to 5. | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | =head2 reconnect_interval | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | The base interval for reconnection attempts. Do note that | ||||||||||||||||||||||||||||||||||||||||||
| 540 | exponential reconnect back-off is used, so if the base reconnect_interval | ||||||||||||||||||||||||||||||||||||||||||
| 541 | is 1 second, then the first reconnect attempt is done immediately, | ||||||||||||||||||||||||||||||||||||||||||
| 542 | the second after one second, the third after two seconds, the fourth | ||||||||||||||||||||||||||||||||||||||||||
| 543 | after four seconds, and so on. | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | Default: 1 second | ||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | Can also be fractional seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | =head2 refresh_mysql_connection_on_error_regex | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | A regular expression that we'll match MySQL errors against, if it | ||||||||||||||||||||||||||||||||||||||||||
| 552 | matches the error is considered recoverable and we'll refresh the | ||||||||||||||||||||||||||||||||||||||||||
| 553 | connection. Set to C<Undef> to disable this feature. | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | Default: Matches "MySQL server has gone away" and "Lost connection to | ||||||||||||||||||||||||||||||||||||||||||
| 556 | MySQL server during query" errors. See the source. | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | =head2 prepare_table | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | This method will generate a C<CREATE TABLE> statement from the | ||||||||||||||||||||||||||||||||||||||||||
| 563 | various properties of the storage object and execute it on the MySQL | ||||||||||||||||||||||||||||||||||||||||||
| 564 | server to prepare the shard table. | ||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | =head2 refresh_connection | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | Explicitly drops the MySQL connection object and calls back into | ||||||||||||||||||||||||||||||||||||||||||
| 569 | the provided connect handler to get a new connection. | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | L<ShardedKV> | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | L<ShardedKV::Storage> | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | L<DBI> | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | L<DBD::mysql> | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | Steffen Mueller <smueller@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||
| 603 | Nick Perez <nperez@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||||
| 607 | Damian Gryski <dgryski@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||
| 613 | This software is copyright (c) 2013 by Steffen Mueller. | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 616 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||
| 618 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | __END__ | ||||||||||||||||||||||||||||||||||||||||||