sub select_new_master { my $self = shift; my $prio_new_master_host = shift; my $prio_new_master_port = shift; my $check_replication_delay = shift; $check_replication_delay = 1 if ( !defined($check_replication_delay) );
my $log = $self->{logger}; my @latest = $self->get_latest_slaves(); my @slaves = $self->get_alive_slaves();
my @pref = $self->get_candidate_masters(); my @bad = $self->get_bad_candidate_masters( $latest[0], $check_replication_delay ); #切换指定了master,即优先级最高的 if ( $prio_new_master_host && $prio_new_master_port ) { my $new_master = $self->get_alive_server_by_hostport( $prio_new_master_host, $prio_new_master_port ); if ($new_master) { my $a = $self->get_server_from_by_id( /@bad, $new_master->{id} ); unless ($a) { $log->info("$prio_new_master_host can be new master."); return $new_master; } else { $log->error("$prio_new_master_host is bad as a new master!"); return; } } else { $log->error("$prio_new_master_host is not alive!"); return;
# none of latest servers can not be a master $log->info(" Searching from all slaves.."); foreach my $s (@slaves) { my $a = $self->get_server_from_by_id( /@bad, $s->{id} ); return $s unless ($a); } $log->info(" Not found.");