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