| 1 | package Salvation::MongoMgr; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use boolean; | ||
| 6 | |||
| 7 | use Salvation::TC (); | ||
| 8 | use List::MoreUtils 'uniq'; | ||
| 9 | use Salvation::MongoMgr::Connection (); | ||
| 10 | |||
| 11 | our $VERSION = 0.02; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | |||
| 15 | my ( $proto, %args ) = @_; | ||
| 16 | |||
| 17 | Salvation::TC -> assert( \%args, 'HashRef( | ||
| 18 | HashRef :connection!, | ||
| 19 | ArrayRef[Str] :add_hosts, | ||
| 20 | ArrayRef[Str] :exclude_hosts, | ||
| 21 | Bool :discovery | ||
| 22 | )' ); | ||
| 23 | |||
| 24 | unless( exists $args{ 'discovery' } ) { | ||
| 25 | |||
| 26 | $args{ 'discovery' } = true; | ||
| 27 | } | ||
| 28 | |||
| 29 | my $self = bless( \%args, ( ref( $proto ) || $proto ) ); | ||
| 30 | |||
| 31 | $self -> { '_connection_args' } = delete( $self -> { 'connection' } ); | ||
| 32 | |||
| 33 | $self -> { 'connection' } = Salvation::MongoMgr::Connection | ||
| 34 | -> new( %{ $self -> { '_connection_args' } } ); | ||
| 35 | |||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub list_masters { | ||
| 40 | |||
| 41 | my ( $self ) = @_; | ||
| 42 | my @list = (); | ||
| 43 | |||
| 44 | foreach my $host ( @{ $self -> hosts_list() } ) { | ||
| 45 | |||
| 46 | my $mgr = $self -> new( | ||
| 47 | connection => { | ||
| 48 | %{ $self -> { '_connection_args' } }, | ||
| 49 | host => $host, | ||
| 50 | find_master => false, | ||
| 51 | }, | ||
| 52 | add_hosts => [ $host ], | ||
| 53 | discovery => false, | ||
| 54 | ); | ||
| 55 | |||
| 56 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 57 | |||
| 58 | eval { | ||
| 59 | |||
| 60 | if( $mgr -> metadata() -> { 'ismaster' } ) { | ||
| 61 | |||
| 62 | push( @list, $host ); | ||
| 63 | } | ||
| 64 | }; | ||
| 65 | |||
| 66 | if( $@ ) { | ||
| 67 | |||
| 68 | print STDERR $@, "\n"; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | return \@list; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub get_indexes { | ||
| 76 | |||
| 77 | my ( $self, $args ) = @_; | ||
| 78 | my ( $collection, $host ) = @$args; | ||
| 79 | |||
| 80 | Salvation::TC -> assert( [ $collection, $host ], 'ArrayRef( Str collection, Maybe[Str] host )' ); | ||
| 81 | |||
| 82 | my $mgr = ( defined $host ? $self -> new( | ||
| 83 | connection => { | ||
| 84 | %{ $self -> { '_connection_args' } }, | ||
| 85 | host => $host, | ||
| 86 | find_master => false, | ||
| 87 | }, | ||
| 88 | add_hosts => [ $host ], | ||
| 89 | discovery => false, | ||
| 90 | ) : $self ); | ||
| 91 | |||
| 92 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 93 | |||
| 94 | return [ $mgr | ||
| 95 | -> { 'connection' } | ||
| 96 | -> get_collection( $collection ) | ||
| 97 | -> get_indexes() ]; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub compare_indexes { | ||
| 101 | |||
| 102 | my ( $self, $collections ) = @_; | ||
| 103 | my @missing = (); | ||
| 104 | my $hosts_count = undef; | ||
| 105 | my %ignore_hosts = (); | ||
| 106 | |||
| 107 | Salvation::TC -> assert( $collections, 'ArrayRef[Str]{1,}' ); | ||
| 108 | |||
| 109 | foreach my $collection ( @$collections ) { | ||
| 110 | |||
| 111 | my %tree = (); | ||
| 112 | |||
| 113 | foreach my $host ( @{ $self -> hosts_list() } ) { | ||
| 114 | |||
| 115 | next if exists $ignore_hosts{ $host }; | ||
| 116 | |||
| 117 | my $mgr = $self -> new( | ||
| 118 | connection => { | ||
| 119 | %{ $self -> { '_connection_args' } }, | ||
| 120 | host => $host, | ||
| 121 | find_master => false, | ||
| 122 | }, | ||
| 123 | add_hosts => [ $host ], | ||
| 124 | discovery => false, | ||
| 125 | ); | ||
| 126 | |||
| 127 | local $MongoDB::Cursor::slave_okay = 1; | ||
| 128 | |||
| 129 | my @indexes = eval{ $mgr | ||
| 130 | -> { 'connection' } | ||
| 131 | -> get_collection( $collection ) | ||
| 132 | -> get_indexes() }; | ||
| 133 | |||
| 134 | if( $@ ) { | ||
| 135 | |||
| 136 | print STDERR $@, "\n"; | ||
| 137 | $ignore_hosts{ $host } = 1; | ||
| 138 | $hosts_count //= $self -> hosts_count(); | ||
| 139 | --$hosts_count; | ||
| 140 | next; | ||
| 141 | } | ||
| 142 | |||
| 143 | foreach my $index ( @indexes ) { | ||
| 144 | |||
| 145 | next unless Salvation::TC -> is( $index, 'HashRef( | ||
| 146 | HashRef[Int] :key! | ||
| 147 | )' ); | ||
| 148 | |||
| 149 | my $dest = $tree{ join( "\0", map( { join( ':', ( $_, $index -> { 'key' } -> { $_ } ) ) } | ||
| 150 | sort( keys( %{ $index -> { 'key' } } ) ) ) ) } //= { | ||
| 151 | |||
| 152 | hosts => [], | ||
| 153 | index => $index, | ||
| 154 | }; | ||
| 155 | |||
| 156 | push( @{ $dest -> { 'hosts' } }, $host ); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | $hosts_count //= $self -> hosts_count(); | ||
| 161 | |||
| 162 | while( my ( undef, $data ) = each( %tree ) ) { | ||
| 163 | |||
| 164 | if( scalar( @{ $data -> { 'hosts' } } ) != $hosts_count ) { | ||
| 165 | |||
| 166 | push( @missing, { | ||
| 167 | index => $data -> { 'index' }, | ||
| 168 | hosts => [ grep( { ! exists $ignore_hosts{ $_ } } @{ $self -> remaining_hosts( @{ $data -> { 'hosts' } } ) } ) ], | ||
| 169 | msg => 'missing', | ||
| 170 | } ); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | return \@missing; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub remaining_hosts { | ||
| 179 | |||
| 180 | my ( $self, @list ) = @_; | ||
| 181 | my %map = map( { $_ => 1 } @list ); | ||
| 182 | |||
| 183 | return [ grep( { ! exists $map{ $_ } } @{ $self -> hosts_list() } ) ]; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub hosts_count { | ||
| 187 | |||
| 188 | my ( $self ) = @_; | ||
| 189 | |||
| 190 | return scalar( @{ $self -> hosts_list() } ); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub hosts_list { | ||
| 194 | |||
| 195 | my ( $self ) = @_; | ||
| 196 | |||
| 197 | unless( exists $self -> { 'hosts_list' } ) { | ||
| 198 | |||
| 199 | if( $self -> { 'discovery' } ) { | ||
| 200 | |||
| 201 | if( $self -> is_mongos() ) { | ||
| 202 | |||
| 203 | my @out = (); | ||
| 204 | |||
| 205 | foreach my $shard ( @{ $self -> list_shards() } ) { | ||
| 206 | |||
| 207 | foreach my $host ( split( /\s*,\s*/, $shard -> { 'host' } ) ) { | ||
| 208 | |||
| 209 | $host =~ s/^.+?\///; | ||
| 210 | |||
| 211 | push( @out, lc( $host ) ); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | $self -> { 'hosts_list' } = \@out; | ||
| 216 | |||
| 217 | } else { | ||
| 218 | |||
| 219 | my $metadata = $self -> metadata(); | ||
| 220 | |||
| 221 | if( exists $metadata -> { 'hosts' } ) { | ||
| 222 | |||
| 223 | $self -> { 'hosts_list' } = [ map( { lc( $_ ) } @{ $metadata -> { 'hosts' } } ) ]; | ||
| 224 | |||
| 225 | } else { | ||
| 226 | |||
| 227 | $self -> { 'hosts_list' } = [ lc( $metadata -> { 'me' } ) ]; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | } else { | ||
| 232 | |||
| 233 | $self -> { 'hosts_list' } = []; | ||
| 234 | } | ||
| 235 | |||
| 236 | if( exists $self -> { 'add_hosts' } ) { | ||
| 237 | |||
| 238 | push( @{ $self -> { 'hosts_list' } }, | ||
| 239 | map( { lc( $_ ) } @{ $self -> { 'add_hosts' } } ) ); | ||
| 240 | } | ||
| 241 | |||
| 242 | @{ $self -> { 'hosts_list' } } = uniq( @{ $self -> { 'hosts_list' } } ); | ||
| 243 | |||
| 244 | if( exists $self -> { 'exclude_hosts' } ) { | ||
| 245 | |||
| 246 | my %map = map( { lc( $_ ) => 1 } @{ $self -> { 'exclude_hosts' } } ); | ||
| 247 | my @new_list = (); | ||
| 248 | |||
| 249 | while( defined( my $host = shift( @{ $self -> { 'hosts_list' } } ) ) ) { | ||
| 250 | |||
| 251 | unless( exists $map{ $host } ) { | ||
| 252 | |||
| 253 | push( @new_list, $host ); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | $self -> { 'hosts_list' } = \@new_list; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | return $self -> { 'hosts_list' }; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub list_shards { | ||
| 265 | |||
| 266 | my ( $self ) = @_; | ||
| 267 | |||
| 268 | unless( exists $self -> { 'list_shards' } ) { | ||
| 269 | |||
| 270 | my $rv = $self -> _run_admin_command( { listShards => 1 } ); | ||
| 271 | |||
| 272 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 273 | Bool :ok! | ||
| 274 | )' ); | ||
| 275 | |||
| 276 | if( $rv -> { 'ok' } ) { | ||
| 277 | |||
| 278 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 279 | ArrayRef[HashRef( Str :_id!, Str :host! )] :shards! | ||
| 280 | )' ); | ||
| 281 | |||
| 282 | $self -> { 'list_shards' } = $rv -> { 'shards' }; | ||
| 283 | |||
| 284 | } else { | ||
| 285 | |||
| 286 | $self -> { 'list_shards' } = []; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | return $self -> { 'list_shards' }; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub is_mongos { | ||
| 294 | |||
| 295 | my ( $self ) = @_; | ||
| 296 | |||
| 297 | return !! $self -> metadata() -> { 'msg' }; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub metadata { | ||
| 301 | |||
| 302 | my ( $self ) = @_; | ||
| 303 | |||
| 304 | unless( exists $self -> { 'metadata' } ) { | ||
| 305 | |||
| 306 | my $rv = $self -> _run_admin_command( { isMaster => 1 } ); | ||
| 307 | |||
| 308 | Salvation::TC -> assert( $rv, 'HashRef( | ||
| 309 | Str :msg, | ||
| 310 | ArrayRef[Str] :hosts, | ||
| 311 | Str :me | ||
| 312 | )' ); | ||
| 313 | |||
| 314 | $self -> { 'metadata' } = $rv; | ||
| 315 | } | ||
| 316 | |||
| 317 | return $self -> { 'metadata' }; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub _run_admin_command { | ||
| 321 | |||
| 322 | my ( $self, $spec ) = @_; | ||
| 323 | |||
| 324 | return $self -> { 'connection' } -> get_database( 'admin' ) -> run_command( $spec ); | ||
| 325 | } | ||
| 326 | |||
| 327 | sub reload { | ||
| 328 | |||
| 329 | my ( $self ) = @_; | ||
| 330 | |||
| 331 | delete( @$self{ 'metadata', 'list_shards', 'hosts_list' } ); | ||
| 332 | |||
| 333 | return; | ||
| 334 | } | ||
| 335 | |||
| 336 | |||
| 337 | 1; | ||
| 338 | |||
| 339 | __END__ |