| 1 | package Salvation::MongoMgr; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use boolean; | ||
| 6 | |||
| 7 | use Socket 'AF_INET6'; | ||
| 8 | use Time::HiRes (); | ||
| 9 | use Salvation::TC (); | ||
| 10 | use List::MoreUtils 'uniq'; | ||
| 11 | use String::ShellQuote 'shell_quote'; | ||
| 12 | use Salvation::MongoMgr::Connection (); | ||
| 13 | |||
| 14 | our $VERSION = 0.04; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | |||
| 18 | my ( $proto, %args ) = @_; | ||
| 19 | |||
| 20 | Salvation::TC -> assert( \%args, 'HashRef( | ||
| 21 | HashRef :connection!, | ||
| 22 | ArrayRef[Str] :add_hosts, | ||
| 23 | ArrayRef[Str] :exclude_hosts, | ||
| 24 | Bool :discovery | ||
| 25 | )' ); | ||
| 26 | |||
| 27 | unless( exists $args{ 'discovery' } ) { | ||
| 28 | |||
| 29 | $args{ 'discovery' } = true; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $self = bless( \%args, ( ref( $proto ) || $proto ) ); | ||
| 33 | |||
| 34 | $self -> { '_connection_args' } = delete( $self -> { 'connection' } ); | ||
| 35 | |||
| 36 | $self -> { 'connection' } = Salvation::MongoMgr::Connection | ||
| 37 | -> new( %{ $self -> { '_connection_args' } } ); | ||
| 38 | |||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub run { | ||
| 43 | |||
| 44 | my ( $self, $args ) = @_; | ||
| 45 | my $host = shift( @$args ); | ||
| 46 | |||
| 47 | Salvation::TC -> assert( [ $host, $args ], 'ArrayRef( Str host, ArrayRef[Str] args )' ); | ||
| 48 | |||
| 49 | if( $host eq '.' ) { | ||
| 50 | |||
| 51 | undef( $host ); | ||
| 52 | } | ||
| 53 | |||
| 54 | my $system_args = $self -> _shellcmd( $host, 'mongo', [] ); | ||
| 55 | |||
| 56 | unshift( @$system_args, 'echo', shell_quote( join( ' ', @$args ) ), '|' ); | ||
| 57 | |||
| 58 | print STDERR join( ' ', '+', @$system_args ), "\n"; | ||
| 59 | |||
| 60 | if( ( my $status = system( join( ' ', @$system_args ) ) ) != 0 ) { | ||
| 61 | |||
| 62 | $status >>= 8; | ||
| 63 | die( "Command failed with code ${status}" ); | ||
| 64 | } | ||
| 65 | |||
| 66 | return []; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub exec { | ||
| 70 | |||
| 71 | my ( $self, $args ) = @_; | ||
| 72 | my $host = shift( @$args ); | ||
| 73 | my $cmd = shift( @$args ); | ||
| 74 | |||
| 75 | Salvation::TC -> assert( [ $host, $cmd, $args ], 'ArrayRef( Str host, Str cmd, ArrayRef[Str] args )' ); | ||
| 76 | |||
| 77 | if( $host eq '.' ) { | ||
| 78 | |||
| 79 | undef( $host ); | ||
| 80 | } | ||
| 81 | |||
| 82 | $args = $self -> _shellcmd( $host, $cmd, $args ); | ||
| 83 | |||
| 84 | print STDERR join( ' ', '+', @$args ), "\n"; | ||
| 85 | |||
| 86 | if( ( my $status = system( @$args ) ) != 0 ) { | ||
| 87 | |||
| 88 | $status >>= 8; | ||
| 89 | die( "Command failed with code ${status}" ); | ||
| 90 | } | ||
| 91 | |||
| 92 | return []; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub shell { | ||
| 96 | |||
| 97 | my ( $self, $args ) = @_; | ||
| 98 | my $host = shift( @$args ); | ||
| 99 | my $cmd = shift( @$args ); | ||
| 100 | |||
| 101 | Salvation::TC -> assert( [ $host, $cmd, $args ], 'ArrayRef( Str host, Str cmd, ArrayRef[Str] args )' ); | ||
| 102 | |||
| 103 | if( $host eq '.' ) { | ||
| 104 | |||
| 105 | undef( $host ); | ||
| 106 | } | ||
| 107 | |||
| 108 | $args = $self -> _shellcmd( $host, $cmd, $args ); | ||
| 109 | |||
| 110 | print STDERR join( ' ', '+', @$args ), "\n"; | ||
| 111 | |||
| 112 | CORE::exec @$args; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _shellcmd { | ||
| 116 | |||
| 117 | my ( $self, $host, $mode, $args ) = @_; | ||
| 118 | my $mgr = $self -> get_host_manager( $host ); | ||
| 119 | |||
| 120 | # Ensures that we have connection to the host | ||
| 121 | $mgr -> { 'connection' } -> get_connection(); | ||
| 122 | |||
| 123 | my $cmd = 'mongo'; | ||
| 124 | |||
| 125 | if( defined $mode ) { | ||
| 126 | |||
| 127 | $cmd = { | ||
| 128 | 'files' => 'mongofiles', | ||
| 129 | 'mongo' => $cmd, | ||
| 130 | |||
| 131 | } -> { $mode }; | ||
| 132 | |||
| 133 | die( "Unknown mode: ${mode}" ) unless defined $cmd; | ||
| 134 | } | ||
| 135 | |||
| 136 | my @db_args = (); | ||
| 137 | |||
| 138 | if( $cmd eq 'mongo' ) { | ||
| 139 | |||
| 140 | push( @db_args, ( | ||
| 141 | shell_quote( sprintf( '%s/%s', | ||
| 142 | join( ',', @{ $mgr -> { 'connection' } -> servers_list() } ), | ||
| 143 | $self -> { 'connection' } -> { 'db' } | ||
| 144 | ) ), | ||
| 145 | ) ); | ||
| 146 | } | ||
| 147 | |||
| 148 | if( $cmd eq 'mongofiles' ) { | ||
| 149 | |||
| 150 | push( @db_args, ( | ||
| 151 | '-h', shell_quote( $mgr -> { 'connection' } -> servers_list() -> [ 0 ] ), | ||
| 152 | '--db', shell_quote( $self -> { 'connection' } -> { 'db' } ), | ||
| 153 | ) ); | ||
| 154 | } | ||
| 155 | |||
| 156 | foreach my $node ( @{ $mgr -> { 'connection' } -> servers_list() } ) { | ||
| 157 | |||
| 158 | my @hp = split( /:/, $node ); | ||
| 159 | my $port = pop( @hp ); | ||
| 160 | my $host = join( ':', @hp ); | ||
| 161 | my $stop = ( $cmd eq 'mongofiles' ); | ||
| 162 | |||
| 163 | foreach my $ai ( Socket::getaddrinfo( $host, $port ) ) { | ||
| 164 | |||
| 165 | next unless Salvation::TC -> is( $ai, 'HashRef( Int :family! )' ); | ||
| 166 | |||
| 167 | if( $ai -> { 'family' } == AF_INET6 ) { | ||
| 168 | |||
| 169 | push( @db_args, '--ipv6' ); | ||
| 170 | $stop = true; | ||
| 171 | last; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | last if $stop; | ||
| 176 | } | ||
| 177 | |||
| 178 | my ( $login, $password ) = @{ $mgr -> { 'connection' } -> credentials() }{ 'login', 'password' }; | ||
| 179 | |||
| 180 | return [ | ||
| 181 | $cmd, @db_args, | ||
| 182 | ( defined $login ? ( '-u' => shell_quote( $login ) ) : () ), | ||
| 183 | ( defined $password ? ( '-p' => shell_quote( $password ) ) : () ), | ||
| 184 | ( ( defined $login || defined $password ) ? ( | ||
| 185 | '--authenticationDatabase', $self -> { 'connection' } -> { 'auth_db_name' }, | ||
| 186 | ) : () ), | ||
| 187 | map( { shell_quote( $_ ) } @$args ), | ||
| 188 | ]; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub compare_collection_hashes { | ||
| 192 | |||
| 193 | my ( $self, $collections ) = @_; | ||
| 194 | my @mismatch = (); | ||
| 195 | my $hosts_count = undef; | ||
| 196 | my %ignore_hosts = (); | ||
| 197 | my %hashes = (); | ||
| 198 | |||
| 199 | Salvation::TC -> assert( $collections, 'ArrayRef[Str]{1,}' ); | ||
| 200 | |||
| 201 | foreach my $collection ( @$collections ) { | ||
| 202 | |||
| 203 | my %tree = (); | ||
| 204 | my %retries = (); | ||
| 205 | my $max_retries = 5; | ||
| 206 | my $only_cached = true; | ||
| 207 | |||
| 208 | foreach my $host ( @{ $self -> hosts_list() } ) { | ||
| 209 | |||
| 210 | next if exists $ignore_hosts{ $host }; | ||
| 211 | |||
| 212 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 213 | |||
| 214 | my $hash = $hashes{ $host } //= eval{ $self -> db_hash( | ||
| 215 | $host, only_cached => $only_cached, | ||
| 216 | ) }; | ||
| 217 | |||
| 218 | $only_cached = true; | ||
| 219 | |||
| 220 | if( $@ ) { | ||
| 221 | |||
| 222 | print STDERR $@, "\n"; | ||
| 223 | $ignore_hosts{ $host } = 1; | ||
| 224 | $hosts_count //= $self -> hosts_count(); | ||
| 225 | --$hosts_count; | ||
| 226 | next; | ||
| 227 | } | ||
| 228 | |||
| 229 | unless( exists $hash -> { 'collections' } -> { $collection } ) { | ||
| 230 | |||
| 231 | if( ++$retries{ $host } == $max_retries ) { | ||
| 232 | |||
| 233 | $only_cached = false; | ||
| 234 | Time::HiRes::sleep( 0.3 ); | ||
| 235 | redo; | ||
| 236 | |||
| 237 | } elsif( $retries{ $host } < $max_retries ) { | ||
| 238 | |||
| 239 | Time::HiRes::sleep( 0.3 ); | ||
| 240 | redo; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | my $hash_str = ( $hash -> { 'collections' } -> { $collection } // '' ); | ||
| 245 | my $dest = $tree{ $hash_str } //= { | ||
| 246 | |||
| 247 | hosts => [], | ||
| 248 | hash => $hash_str, | ||
| 249 | }; | ||
| 250 | |||
| 251 | push( @{ $dest -> { 'hosts' } }, $host ); | ||
| 252 | } | ||
| 253 | |||
| 254 | $hosts_count //= $self -> hosts_count(); | ||
| 255 | |||
| 256 | while( my ( undef, $data ) = each( %tree ) ) { | ||
| 257 | |||
| 258 | if( ( scalar( @{ $data -> { 'hosts' } } ) != $hosts_count ) || ( $data -> { 'hash' } eq '' ) ) { | ||
| 259 | |||
| 260 | push( @mismatch, { | ||
| 261 | hash => $data -> { 'hash' }, | ||
| 262 | collection => $collection, | ||
| 263 | hosts => [ grep( { ! exists $ignore_hosts{ $_ } } @{ $self -> remaining_hosts( @{ $data -> { 'hosts' } } ) } ) ], | ||
| 264 | msg => 'mismatch', | ||
| 265 | } ); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | return \@mismatch; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub db_hash { | ||
| 274 | |||
| 275 | my ( $self, $host, %args ) = @_; | ||
| 276 | my $mgr = $self -> get_host_manager( $host ); | ||
| 277 | my $rv = $mgr -> _run_config_command( { dbHash => 1 } ); | ||
| 278 | |||
| 279 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 280 | HashRef[Str] :collections!, | ||
| 281 | ArrayRef[Str] :fromCache!, | ||
| 282 | Bool :ok!, | ||
| 283 | Str :md5! | ||
| 284 | )' ); | ||
| 285 | |||
| 286 | if( $args{ 'only_cached' } ) { | ||
| 287 | |||
| 288 | my @from_cache = @{ $rv -> { 'fromCache' } }; | ||
| 289 | $_ =~ s/^config\.// for @from_cache; | ||
| 290 | |||
| 291 | my %collections = (); | ||
| 292 | |||
| 293 | @collections{ @from_cache } = @{ $rv -> { 'collections' } }{ @from_cache }; | ||
| 294 | |||
| 295 | $rv -> { 'collections' } = \%collections; | ||
| 296 | } | ||
| 297 | |||
| 298 | return $rv; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub repl_set_status { | ||
| 302 | |||
| 303 | my ( $self, $host ) = @_; | ||
| 304 | my $mgr = $self -> get_host_manager( $host ); | ||
| 305 | my $rv = $mgr -> _run_admin_command( { replSetGetStatus => 1 } ); | ||
| 306 | |||
| 307 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 308 | ArrayRef[HashRef( | ||
| 309 | Int :_id!, | ||
| 310 | Str :name!, | ||
| 311 | Str :stateStr! | ||
| 312 | )] :members!, | ||
| 313 | Bool :ok!, | ||
| 314 | Str :set! | ||
| 315 | )' ); | ||
| 316 | |||
| 317 | return $rv; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub list_masters { | ||
| 321 | |||
| 322 | my ( $self ) = @_; | ||
| 323 | my @list = (); | ||
| 324 | |||
| 325 | foreach my $host ( @{ $self -> hosts_list() } ) { | ||
| 326 | |||
| 327 | my $mgr = $self -> get_host_manager( $host ); | ||
| 328 | |||
| 329 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 330 | |||
| 331 | eval { | ||
| 332 | |||
| 333 | if( $mgr -> metadata() -> { 'ismaster' } ) { | ||
| 334 | |||
| 335 | push( @list, $host ); | ||
| 336 | } | ||
| 337 | }; | ||
| 338 | |||
| 339 | if( $@ ) { | ||
| 340 | |||
| 341 | print STDERR $@, "\n"; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | return \@list; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub get_indexes { | ||
| 349 | |||
| 350 | my ( $self, $args ) = @_; | ||
| 351 | my ( $collection, $host ) = @$args; | ||
| 352 | |||
| 353 | Salvation::TC -> assert( [ $collection, $host ], 'ArrayRef( Str collection, Maybe[Str] host )' ); | ||
| 354 | |||
| 355 | my $mgr = $self -> get_host_manager( $host ); | ||
| 356 | |||
| 357 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 358 | |||
| 359 | return [ $mgr | ||
| 360 | -> { 'connection' } | ||
| 361 | -> get_collection( $collection ) | ||
| 362 | -> get_indexes() ]; | ||
| 363 | } | ||
| 364 | |||
| 365 | sub compare_indexes { | ||
| 366 | |||
| 367 | my ( $self, $collections ) = @_; | ||
| 368 | my @missing = (); | ||
| 369 | my $hosts_count = undef; | ||
| 370 | my %ignore_hosts = (); | ||
| 371 | |||
| 372 | Salvation::TC -> assert( $collections, 'ArrayRef[Str]{1,}' ); | ||
| 373 | |||
| 374 | foreach my $collection ( @$collections ) { | ||
| 375 | |||
| 376 | my %tree = (); | ||
| 377 | |||
| 378 | foreach my $host ( @{ $self -> hosts_list() } ) { | ||
| 379 | |||
| 380 | next if exists $ignore_hosts{ $host }; | ||
| 381 | |||
| 382 | my $mgr = $self -> get_host_manager( $host ); | ||
| 383 | |||
| 384 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 385 | |||
| 386 | my @indexes = eval{ $mgr | ||
| 387 | -> { 'connection' } | ||
| 388 | -> get_collection( $collection ) | ||
| 389 | -> get_indexes() }; | ||
| 390 | |||
| 391 | if( $@ ) { | ||
| 392 | |||
| 393 | print STDERR $@, "\n"; | ||
| 394 | $ignore_hosts{ $host } = 1; | ||
| 395 | $hosts_count //= $self -> hosts_count(); | ||
| 396 | --$hosts_count; | ||
| 397 | next; | ||
| 398 | } | ||
| 399 | |||
| 400 | foreach my $index ( @indexes ) { | ||
| 401 | |||
| 402 | next unless Salvation::TC -> is( $index, 'HashRef( | ||
| 403 | HashRef[Int] :key! | ||
| 404 | )' ); | ||
| 405 | |||
| 406 | my $dest = $tree{ join( "\0", map( { join( ':', ( $_, $index -> { 'key' } -> { $_ } ) ) } | ||
| 407 | sort( keys( %{ $index -> { 'key' } } ) ) ) ) } //= { | ||
| 408 | |||
| 409 | hosts => [], | ||
| 410 | index => $index, | ||
| 411 | }; | ||
| 412 | |||
| 413 | push( @{ $dest -> { 'hosts' } }, $host ); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | $hosts_count //= $self -> hosts_count(); | ||
| 418 | |||
| 419 | while( my ( undef, $data ) = each( %tree ) ) { | ||
| 420 | |||
| 421 | if( scalar( @{ $data -> { 'hosts' } } ) != $hosts_count ) { | ||
| 422 | |||
| 423 | push( @missing, { | ||
| 424 | index => $data -> { 'index' }, | ||
| 425 | hosts => [ grep( { ! exists $ignore_hosts{ $_ } } @{ $self -> remaining_hosts( @{ $data -> { 'hosts' } } ) } ) ], | ||
| 426 | msg => 'missing', | ||
| 427 | } ); | ||
| 428 | } | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | return \@missing; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub remaining_hosts { | ||
| 436 | |||
| 437 | my ( $self, @list ) = @_; | ||
| 438 | my %map = map( { $_ => 1 } @list ); | ||
| 439 | |||
| 440 | return [ grep( { ! exists $map{ $_ } } @{ $self -> hosts_list() } ) ]; | ||
| 441 | } | ||
| 442 | |||
| 443 | sub hosts_count { | ||
| 444 | |||
| 445 | my ( $self ) = @_; | ||
| 446 | |||
| 447 | return scalar( @{ $self -> hosts_list() } ); | ||
| 448 | } | ||
| 449 | |||
| 450 | sub hosts_list { | ||
| 451 | |||
| 452 | my ( $self ) = @_; | ||
| 453 | |||
| 454 | unless( exists $self -> { 'hosts_list' } ) { | ||
| 455 | |||
| 456 | if( $self -> { 'discovery' } ) { | ||
| 457 | |||
| 458 | if( $self -> is_mongos() ) { | ||
| 459 | |||
| 460 | my @out = (); | ||
| 461 | |||
| 462 | foreach my $shard ( @{ $self -> list_shards() } ) { | ||
| 463 | |||
| 464 | my $got_rs_status = false; | ||
| 465 | |||
| 466 | foreach my $host ( split( /\s*,\s*/, $shard -> { 'host' } ) ) { | ||
| 467 | |||
| 468 | $host =~ s/^.+?\///; | ||
| 469 | $host = lc( $host ); | ||
| 470 | |||
| 471 | push( @out, $host ); | ||
| 472 | |||
| 473 | unless( $got_rs_status ) { | ||
| 474 | |||
| 475 | my $status = eval{ $self -> repl_set_status( $host ) }; | ||
| 476 | |||
| 477 | if( $@ ) { | ||
| 478 | |||
| 479 | print STDERR "$@\n"; | ||
| 480 | |||
| 481 | } else { | ||
| 482 | |||
| 483 | foreach my $member ( @{ $status -> { 'members' } } ) { | ||
| 484 | |||
| 485 | push( @out, lc( $member -> { 'name' } ) ); | ||
| 486 | } | ||
| 487 | |||
| 488 | $got_rs_status = true; | ||
| 489 | } | ||
| 490 | } | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 494 | $self -> { 'hosts_list' } = \@out; | ||
| 495 | |||
| 496 | } else { | ||
| 497 | |||
| 498 | my $metadata = $self -> metadata(); | ||
| 499 | |||
| 500 | if( exists $metadata -> { 'hosts' } ) { | ||
| 501 | |||
| 502 | my @out = (); | ||
| 503 | |||
| 504 | foreach my $host ( @{ $metadata -> { 'hosts' } } ) { | ||
| 505 | |||
| 506 | $host = lc( $host ); | ||
| 507 | |||
| 508 | push( @out, $host ); | ||
| 509 | my $status = eval{ $self -> repl_set_status( $host ) }; | ||
| 510 | |||
| 511 | if( $@ ) { | ||
| 512 | |||
| 513 | print STDERR "$@\n"; | ||
| 514 | |||
| 515 | } else { | ||
| 516 | |||
| 517 | foreach my $member ( @{ $status -> { 'members' } } ) { | ||
| 518 | |||
| 519 | push( @out, lc( $member -> { 'name' } ) ); | ||
| 520 | } | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | $self -> { 'hosts_list' } = \@out; | ||
| 525 | |||
| 526 | } else { | ||
| 527 | |||
| 528 | $self -> { 'hosts_list' } = [ lc( $metadata -> { 'me' } ) ]; | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | } else { | ||
| 533 | |||
| 534 | $self -> { 'hosts_list' } = []; | ||
| 535 | } | ||
| 536 | |||
| 537 | if( exists $self -> { 'add_hosts' } ) { | ||
| 538 | |||
| 539 | push( @{ $self -> { 'hosts_list' } }, | ||
| 540 | map( { lc( $_ ) } @{ $self -> { 'add_hosts' } } ) ); | ||
| 541 | } | ||
| 542 | |||
| 543 | @{ $self -> { 'hosts_list' } } = uniq( @{ $self -> { 'hosts_list' } } ); | ||
| 544 | |||
| 545 | if( exists $self -> { 'exclude_hosts' } ) { | ||
| 546 | |||
| 547 | my %map = map( { lc( $_ ) => 1 } @{ $self -> { 'exclude_hosts' } } ); | ||
| 548 | my @new_list = (); | ||
| 549 | |||
| 550 | while( defined( my $host = shift( @{ $self -> { 'hosts_list' } } ) ) ) { | ||
| 551 | |||
| 552 | unless( exists $map{ $host } ) { | ||
| 553 | |||
| 554 | push( @new_list, $host ); | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | $self -> { 'hosts_list' } = \@new_list; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | return $self -> { 'hosts_list' }; | ||
| 563 | } | ||
| 564 | |||
| 565 | sub list_shards { | ||
| 566 | |||
| 567 | my ( $self ) = @_; | ||
| 568 | |||
| 569 | unless( exists $self -> { 'list_shards' } ) { | ||
| 570 | |||
| 571 | my $rv = $self -> _run_admin_command( { listShards => 1 } ); | ||
| 572 | |||
| 573 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 574 | Bool :ok! | ||
| 575 | )' ); | ||
| 576 | |||
| 577 | if( $rv -> { 'ok' } ) { | ||
| 578 | |||
| 579 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 580 | ArrayRef[HashRef( Str :_id!, Str :host! )] :shards! | ||
| 581 | )' ); | ||
| 582 | |||
| 583 | $self -> { 'list_shards' } = $rv -> { 'shards' }; | ||
| 584 | |||
| 585 | } else { | ||
| 586 | |||
| 587 | $self -> { 'list_shards' } = []; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | return $self -> { 'list_shards' }; | ||
| 592 | } | ||
| 593 | |||
| 594 | sub is_mongos { | ||
| 595 | |||
| 596 | my ( $self ) = @_; | ||
| 597 | |||
| 598 | return !! $self -> metadata() -> { 'msg' }; | ||
| 599 | } | ||
| 600 | |||
| 601 | sub metadata { | ||
| 602 | |||
| 603 | my ( $self ) = @_; | ||
| 604 | |||
| 605 | unless( exists $self -> { 'metadata' } ) { | ||
| 606 | |||
| 607 | my $rv = $self -> _run_admin_command( { isMaster => 1 } ); | ||
| 608 | |||
| 609 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 610 | Str :msg, | ||
| 611 | ArrayRef[Str] :hosts, | ||
| 612 | Str :me | ||
| 613 | )' ); | ||
| 614 | |||
| 615 | $self -> { 'metadata' } = $rv; | ||
| 616 | } | ||
| 617 | |||
| 618 | return $self -> { 'metadata' }; | ||
| 619 | } | ||
| 620 | |||
| 621 | sub _run_admin_command { | ||
| 622 | |||
| 623 | my ( $self, $spec ) = @_; | ||
| 624 | |||
| 625 | return $self -> { 'connection' } -> get_database( 'admin' ) -> run_command( $spec ); | ||
| 626 | } | ||
| 627 | |||
| 628 | sub _run_config_command { | ||
| 629 | |||
| 630 | my ( $self, $spec ) = @_; | ||
| 631 | |||
| 632 | return $self -> { 'connection' } -> get_database( 'config' ) -> run_command( $spec ); | ||
| 633 | } | ||
| 634 | |||
| 635 | sub reload { | ||
| 636 | |||
| 637 | my ( $self ) = @_; | ||
| 638 | |||
| 639 | delete( @$self{ 'metadata', 'list_shards', 'hosts_list' } ); | ||
| 640 | |||
| 641 | return; | ||
| 642 | } | ||
| 643 | |||
| 644 | sub get_host_manager { | ||
| 645 | |||
| 646 | my ( $self, $host ) = @_; | ||
| 647 | |||
| 648 | return ( defined $host ? $self -> new( | ||
| 649 | connection => { | ||
| 650 | %{ $self -> { '_connection_args' } }, | ||
| 651 | host => $host, | ||
| 652 | find_master => false, | ||
| 653 | }, | ||
| 654 | add_hosts => [ $host ], | ||
| 655 | discovery => false, | ||
| 656 | ) : $self ); | ||
| 657 | } | ||
| 658 | |||
| 659 | |||
| 660 | 1; | ||
| 661 | |||
| 662 | __END__ |