| 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.05; | ||
| 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 | delete( $hashes{ $host } ); | ||
| 235 | Time::HiRes::sleep( 0.3 ); | ||
| 236 | redo; | ||
| 237 | |||
| 238 | } elsif( $retries{ $host } < $max_retries ) { | ||
| 239 | |||
| 240 | delete( $hashes{ $host } ); | ||
| 241 | Time::HiRes::sleep( 0.3 ); | ||
| 242 | redo; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | my $hash_str = ( $hash -> { 'collections' } -> { $collection } // '' ); | ||
| 247 | my $dest = $tree{ $hash_str } //= { | ||
| 248 | |||
| 249 | hosts => [], | ||
| 250 | hash => $hash_str, | ||
| 251 | }; | ||
| 252 | |||
| 253 | push( @{ $dest -> { 'hosts' } }, $host ); | ||
| 254 | } | ||
| 255 | |||
| 256 | $hosts_count //= $self -> hosts_count(); | ||
| 257 | |||
| 258 | while( my ( undef, $data ) = each( %tree ) ) { | ||
| 259 | |||
| 260 | if( ( scalar( @{ $data -> { 'hosts' } } ) != $hosts_count ) || ( $data -> { 'hash' } eq '' ) ) { | ||
| 261 | |||
| 262 | push( @mismatch, { | ||
| 263 | hash => $data -> { 'hash' }, | ||
| 264 | collection => $collection, | ||
| 265 | hosts => [ grep( { ! exists $ignore_hosts{ $_ } } @{ $self -> remaining_hosts( @{ $data -> { 'hosts' } } ) } ) ], | ||
| 266 | msg => 'mismatch', | ||
| 267 | } ); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | return \@mismatch; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub db_hash { | ||
| 276 | |||
| 277 | my ( $self, $host, %args ) = @_; | ||
| 278 | my $mgr = $self -> get_host_manager( $host ); | ||
| 279 | my $rv = $mgr -> _run_config_command( { dbHash => 1 } ); | ||
| 280 | |||
| 281 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 282 | HashRef[Str] :collections!, | ||
| 283 | ArrayRef[Str] :fromCache!, | ||
| 284 | Bool :ok!, | ||
| 285 | Str :md5! | ||
| 286 | )' ); | ||
| 287 | |||
| 288 | if( $args{ 'only_cached' } ) { | ||
| 289 | |||
| 290 | my @from_cache = @{ $rv -> { 'fromCache' } }; | ||
| 291 | $_ =~ s/^config\.// for @from_cache; | ||
| 292 | |||
| 293 | my %collections = (); | ||
| 294 | |||
| 295 | @collections{ @from_cache } = @{ $rv -> { 'collections' } }{ @from_cache }; | ||
| 296 | |||
| 297 | $rv -> { 'collections' } = \%collections; | ||
| 298 | } | ||
| 299 | |||
| 300 | return $rv; | ||
| 301 | } | ||
| 302 | |||
| 303 | sub repl_set_status { | ||
| 304 | |||
| 305 | my ( $self, $host ) = @_; | ||
| 306 | my $mgr = $self -> get_host_manager( $host ); | ||
| 307 | my $rv = $mgr -> _run_admin_command( { replSetGetStatus => 1 } ); | ||
| 308 | |||
| 309 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 310 | ArrayRef[HashRef( | ||
| 311 | Int :_id!, | ||
| 312 | Str :name!, | ||
| 313 | Str :stateStr! | ||
| 314 | )] :members!, | ||
| 315 | Bool :ok!, | ||
| 316 | Str :set! | ||
| 317 | )' ); | ||
| 318 | |||
| 319 | return $rv; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub list_masters { | ||
| 323 | |||
| 324 | my ( $self ) = @_; | ||
| 325 | my @list = (); | ||
| 326 | |||
| 327 | foreach my $host ( @{ $self -> hosts_list() } ) { | ||
| 328 | |||
| 329 | my $mgr = $self -> get_host_manager( $host ); | ||
| 330 | |||
| 331 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 332 | |||
| 333 | eval { | ||
| 334 | |||
| 335 | if( $mgr -> metadata() -> { 'ismaster' } ) { | ||
| 336 | |||
| 337 | push( @list, $host ); | ||
| 338 | } | ||
| 339 | }; | ||
| 340 | |||
| 341 | if( $@ ) { | ||
| 342 | |||
| 343 | print STDERR $@, "\n"; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | return \@list; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub get_indexes { | ||
| 351 | |||
| 352 | my ( $self, $args ) = @_; | ||
| 353 | my ( $collection, $host ) = @$args; | ||
| 354 | |||
| 355 | Salvation::TC -> assert( [ $collection, $host ], 'ArrayRef( Str collection, Maybe[Str] host )' ); | ||
| 356 | |||
| 357 | my $mgr = $self -> get_host_manager( $host ); | ||
| 358 | |||
| 359 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 360 | |||
| 361 | return [ $mgr | ||
| 362 | -> { 'connection' } | ||
| 363 | -> get_collection( $collection ) | ||
| 364 | -> get_indexes() ]; | ||
| 365 | } | ||
| 366 | |||
| 367 | sub compare_indexes { | ||
| 368 | |||
| 369 | my ( $self, $collections ) = @_; | ||
| 370 | my @missing = (); | ||
| 371 | my $hosts_count = undef; | ||
| 372 | my %ignore_hosts = (); | ||
| 373 | |||
| 374 | Salvation::TC -> assert( $collections, 'ArrayRef[Str]{1,}' ); | ||
| 375 | |||
| 376 | foreach my $collection ( @$collections ) { | ||
| 377 | |||
| 378 | my %tree = (); | ||
| 379 | |||
| 380 | foreach my $host ( @{ $self -> hosts_list() } ) { | ||
| 381 | |||
| 382 | next if exists $ignore_hosts{ $host }; | ||
| 383 | |||
| 384 | my $mgr = $self -> get_host_manager( $host ); | ||
| 385 | |||
| 386 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 387 | |||
| 388 | my @indexes = eval{ $mgr | ||
| 389 | -> { 'connection' } | ||
| 390 | -> get_collection( $collection ) | ||
| 391 | -> get_indexes() }; | ||
| 392 | |||
| 393 | if( $@ ) { | ||
| 394 | |||
| 395 | print STDERR $@, "\n"; | ||
| 396 | $ignore_hosts{ $host } = 1; | ||
| 397 | $hosts_count //= $self -> hosts_count(); | ||
| 398 | --$hosts_count; | ||
| 399 | next; | ||
| 400 | } | ||
| 401 | |||
| 402 | foreach my $index ( @indexes ) { | ||
| 403 | |||
| 404 | next unless Salvation::TC -> is( $index, 'HashRef( | ||
| 405 | HashRef[Int] :key! | ||
| 406 | )' ); | ||
| 407 | |||
| 408 | my $dest = $tree{ join( "\0", map( { join( ':', ( $_, $index -> { 'key' } -> { $_ } ) ) } | ||
| 409 | sort( keys( %{ $index -> { 'key' } } ) ) ) ) } //= { | ||
| 410 | |||
| 411 | hosts => [], | ||
| 412 | index => $index, | ||
| 413 | }; | ||
| 414 | |||
| 415 | push( @{ $dest -> { 'hosts' } }, $host ); | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | $hosts_count //= $self -> hosts_count(); | ||
| 420 | |||
| 421 | while( my ( undef, $data ) = each( %tree ) ) { | ||
| 422 | |||
| 423 | if( scalar( @{ $data -> { 'hosts' } } ) != $hosts_count ) { | ||
| 424 | |||
| 425 | push( @missing, { | ||
| 426 | index => $data -> { 'index' }, | ||
| 427 | hosts => [ grep( { ! exists $ignore_hosts{ $_ } } @{ $self -> remaining_hosts( @{ $data -> { 'hosts' } } ) } ) ], | ||
| 428 | msg => 'missing', | ||
| 429 | } ); | ||
| 430 | } | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | return \@missing; | ||
| 435 | } | ||
| 436 | |||
| 437 | sub remaining_hosts { | ||
| 438 | |||
| 439 | my ( $self, @list ) = @_; | ||
| 440 | my %map = map( { $_ => 1 } @list ); | ||
| 441 | |||
| 442 | return [ grep( { ! exists $map{ $_ } } @{ $self -> hosts_list() } ) ]; | ||
| 443 | } | ||
| 444 | |||
| 445 | sub hosts_count { | ||
| 446 | |||
| 447 | my ( $self ) = @_; | ||
| 448 | |||
| 449 | return scalar( @{ $self -> hosts_list() } ); | ||
| 450 | } | ||
| 451 | |||
| 452 | sub hosts_list { | ||
| 453 | |||
| 454 | my ( $self ) = @_; | ||
| 455 | |||
| 456 | unless( exists $self -> { 'hosts_list' } ) { | ||
| 457 | |||
| 458 | if( $self -> { 'discovery' } ) { | ||
| 459 | |||
| 460 | if( $self -> is_mongos() ) { | ||
| 461 | |||
| 462 | my @out = (); | ||
| 463 | |||
| 464 | foreach my $shard ( @{ $self -> list_shards() } ) { | ||
| 465 | |||
| 466 | my $got_rs_status = false; | ||
| 467 | |||
| 468 | foreach my $host ( split( /\s*,\s*/, $shard -> { 'host' } ) ) { | ||
| 469 | |||
| 470 | $host =~ s/^.+?\///; | ||
| 471 | $host = lc( $host ); | ||
| 472 | |||
| 473 | push( @out, $host ); | ||
| 474 | |||
| 475 | unless( $got_rs_status ) { | ||
| 476 | |||
| 477 | my $status = eval{ $self -> repl_set_status( $host ) }; | ||
| 478 | |||
| 479 | if( $@ ) { | ||
| 480 | |||
| 481 | print STDERR "$@\n"; | ||
| 482 | |||
| 483 | } else { | ||
| 484 | |||
| 485 | foreach my $member ( @{ $status -> { 'members' } } ) { | ||
| 486 | |||
| 487 | push( @out, lc( $member -> { 'name' } ) ); | ||
| 488 | } | ||
| 489 | |||
| 490 | $got_rs_status = true; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | } | ||
| 494 | } | ||
| 495 | |||
| 496 | $self -> { 'hosts_list' } = \@out; | ||
| 497 | |||
| 498 | } else { | ||
| 499 | |||
| 500 | my $metadata = $self -> metadata(); | ||
| 501 | |||
| 502 | if( exists $metadata -> { 'hosts' } ) { | ||
| 503 | |||
| 504 | my @out = (); | ||
| 505 | |||
| 506 | foreach my $host ( @{ $metadata -> { 'hosts' } } ) { | ||
| 507 | |||
| 508 | $host = lc( $host ); | ||
| 509 | |||
| 510 | push( @out, $host ); | ||
| 511 | my $status = eval{ $self -> repl_set_status( $host ) }; | ||
| 512 | |||
| 513 | if( $@ ) { | ||
| 514 | |||
| 515 | print STDERR "$@\n"; | ||
| 516 | |||
| 517 | } else { | ||
| 518 | |||
| 519 | foreach my $member ( @{ $status -> { 'members' } } ) { | ||
| 520 | |||
| 521 | push( @out, lc( $member -> { 'name' } ) ); | ||
| 522 | } | ||
| 523 | } | ||
| 524 | } | ||
| 525 | |||
| 526 | $self -> { 'hosts_list' } = \@out; | ||
| 527 | |||
| 528 | } else { | ||
| 529 | |||
| 530 | $self -> { 'hosts_list' } = [ lc( $metadata -> { 'me' } ) ]; | ||
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 534 | } else { | ||
| 535 | |||
| 536 | $self -> { 'hosts_list' } = []; | ||
| 537 | } | ||
| 538 | |||
| 539 | if( exists $self -> { 'add_hosts' } ) { | ||
| 540 | |||
| 541 | push( @{ $self -> { 'hosts_list' } }, | ||
| 542 | map( { lc( $_ ) } @{ $self -> { 'add_hosts' } } ) ); | ||
| 543 | } | ||
| 544 | |||
| 545 | @{ $self -> { 'hosts_list' } } = uniq( @{ $self -> { 'hosts_list' } } ); | ||
| 546 | |||
| 547 | if( exists $self -> { 'exclude_hosts' } ) { | ||
| 548 | |||
| 549 | my %map = map( { lc( $_ ) => 1 } @{ $self -> { 'exclude_hosts' } } ); | ||
| 550 | my @new_list = (); | ||
| 551 | |||
| 552 | while( defined( my $host = shift( @{ $self -> { 'hosts_list' } } ) ) ) { | ||
| 553 | |||
| 554 | unless( exists $map{ $host } ) { | ||
| 555 | |||
| 556 | push( @new_list, $host ); | ||
| 557 | } | ||
| 558 | } | ||
| 559 | |||
| 560 | $self -> { 'hosts_list' } = \@new_list; | ||
| 561 | } | ||
| 562 | } | ||
| 563 | |||
| 564 | return $self -> { 'hosts_list' }; | ||
| 565 | } | ||
| 566 | |||
| 567 | sub list_shards { | ||
| 568 | |||
| 569 | my ( $self ) = @_; | ||
| 570 | |||
| 571 | unless( exists $self -> { 'list_shards' } ) { | ||
| 572 | |||
| 573 | my $rv = $self -> _run_admin_command( { listShards => 1 } ); | ||
| 574 | |||
| 575 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 576 | Bool :ok! | ||
| 577 | )' ); | ||
| 578 | |||
| 579 | if( $rv -> { 'ok' } ) { | ||
| 580 | |||
| 581 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 582 | ArrayRef[HashRef( Str :_id!, Str :host! )] :shards! | ||
| 583 | )' ); | ||
| 584 | |||
| 585 | $self -> { 'list_shards' } = $rv -> { 'shards' }; | ||
| 586 | |||
| 587 | } else { | ||
| 588 | |||
| 589 | $self -> { 'list_shards' } = []; | ||
| 590 | } | ||
| 591 | } | ||
| 592 | |||
| 593 | return $self -> { 'list_shards' }; | ||
| 594 | } | ||
| 595 | |||
| 596 | sub is_mongos { | ||
| 597 | |||
| 598 | my ( $self ) = @_; | ||
| 599 | |||
| 600 | return !! $self -> metadata() -> { 'msg' }; | ||
| 601 | } | ||
| 602 | |||
| 603 | sub metadata { | ||
| 604 | |||
| 605 | my ( $self ) = @_; | ||
| 606 | |||
| 607 | unless( exists $self -> { 'metadata' } ) { | ||
| 608 | |||
| 609 | my $rv = $self -> _run_admin_command( { isMaster => 1 } ); | ||
| 610 | |||
| 611 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 612 | Str :msg, | ||
| 613 | ArrayRef[Str] :hosts, | ||
| 614 | Str :me | ||
| 615 | )' ); | ||
| 616 | |||
| 617 | $self -> { 'metadata' } = $rv; | ||
| 618 | } | ||
| 619 | |||
| 620 | return $self -> { 'metadata' }; | ||
| 621 | } | ||
| 622 | |||
| 623 | sub _run_admin_command { | ||
| 624 | |||
| 625 | my ( $self, $spec ) = @_; | ||
| 626 | |||
| 627 | return $self -> { 'connection' } -> get_database( 'admin' ) -> run_command( $spec ); | ||
| 628 | } | ||
| 629 | |||
| 630 | sub _run_config_command { | ||
| 631 | |||
| 632 | my ( $self, $spec ) = @_; | ||
| 633 | |||
| 634 | return $self -> { 'connection' } -> get_database( 'config' ) -> run_command( $spec ); | ||
| 635 | } | ||
| 636 | |||
| 637 | sub reload { | ||
| 638 | |||
| 639 | my ( $self ) = @_; | ||
| 640 | |||
| 641 | delete( @$self{ 'metadata', 'list_shards', 'hosts_list' } ); | ||
| 642 | |||
| 643 | return; | ||
| 644 | } | ||
| 645 | |||
| 646 | sub get_host_manager { | ||
| 647 | |||
| 648 | my ( $self, $host ) = @_; | ||
| 649 | |||
| 650 | return ( defined $host ? $self -> new( | ||
| 651 | connection => { | ||
| 652 | %{ $self -> { '_connection_args' } }, | ||
| 653 | host => $host, | ||
| 654 | find_master => false, | ||
| 655 | }, | ||
| 656 | add_hosts => [ $host ], | ||
| 657 | discovery => false, | ||
| 658 | ) : $self ); | ||
| 659 | } | ||
| 660 | |||
| 661 | |||
| 662 | 1; | ||
| 663 | |||
| 664 | __END__ |