Remove Algorithm::Diff

1 view
Skip to first unread message

Baron Schwartz

unread,
Nov 20, 2009, 7:50:52 AM11/20/09
to mmm-devel
Anyone want to remove Algorithm::Diff so it is one less dependency to
install? You can replace it with a one-liner. I don't use bzr and
Launchpad, or I'd just do it myself.

- Baron

Pascal Hofmann

unread,
Nov 23, 2009, 2:53:27 PM11/23/09
to mmm-...@googlegroups.com
Hi Baron,

Please send the one-liner ;)

Baron Schwartz

unread,
Nov 23, 2009, 3:31:43 PM11/23/09
to mmm-...@googlegroups.com
Well, we have in Monitor.pm,

215 #_______________________________________________________________________
216 #
217 # ... determine if roles differ
218 #_______________________________________________________________________
219
220 my $changes = 0;
221 my $diff = new Algorithm::Diff:: (
222 $system_status->{$host}->{roles},
223 $agent->roles,
224 { keyGen => \&MMM::Common::Role::to_string }
225 );
226
227 while ($diff->Next) {
228 next if ($diff->Same);
229
230 ERROR sprintf(
231 "Switching to passive mode: Roles of host '$host' [%s]
differ from stored ones [%s]",
232 join(', ', @{$system_status->{$host}->{roles}}),
233 join(', ', @{$agent->roles})
234 );
235 $status = 0;
236 $host_status = 0;
237 last;
238 }

How about we just replace this with

my $system_roles = join(', ', @{$system_status->{$host}->{roles}});
my $agent_roles = join(', ', @{$agent->roles});
if ( $system_roles ne $agent_roles ) {
ERROR "Switching to passive mode: Roles of host '$host'
[$system_roles] differ from stored ones [$agent_roles]";
$status = 0;
$host_status = 0;
}

OK, so it's a 2-liner, but only because I want extra verbosity and I
want the $variables for later -- it could easily be a 1-liner :-)
BTW, $changes is unused as far as I can see.

In Agent.pm, we push the deleted roles and added roles:


184 # Process roles
185 my @added_roles = ();
186 my @deleted_roles = ();
187 my $changes_count = 0;
188
189 # Determine changes
190 my $diff = new Algorithm::Diff:: ($self->roles, \@new_roles, {
keyGen => \&MMM::Common::Role::to_string });
191 while ($diff->Next) {
192 next if ($diff->Same);
193
194 $changes_count++;
195 push (@deleted_roles, $diff->Items(1)) if ($diff->Items(1));
196 push (@added_roles, $diff->Items(2)) if ($diff->Items(2));
197 }

This could be

my %self_roles = map { $_ => 1 } @{$self->roles};
my %new_roles = map { $_ => 1 } @new_roles;
push @added_roles, grep { !$self_roles{$_} } @new_roles;
push @deleted_roles, grep { !$new_roles{$_} } @{$self->roles};

That one's a little harder to do in 1 line, but still possible, but of
course that would be stupid because it'd make it hard to debug and
understand. I think that takes care of Algorithm::Diff, correct me if
I'm wrong.

- Baron

On Mon, Nov 23, 2009 at 2:53 PM, Pascal Hofmann <ma...@pascalhofmann.de> wrote:
> Hi Baron,
>
> Please send the one-liner ;)
>
> --
>
> You received this message because you are subscribed to the Google Groups "MySQL Multi Master Manager Development" group.
> To post to this group, send email to mmm-...@googlegroups.com.
> To unsubscribe from this group, send email to mmm-devel+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mmm-devel?hl=.
>
>
>



--
Baron Schwartz, Director of Consulting, Percona Inc.
Our Blog: http://www.mysqlperformanceblog.com/
Our Services: http://www.percona.com/services.html

Pascal Hofmann

unread,
Nov 23, 2009, 3:54:37 PM11/23/09
to mmm-...@googlegroups.com
Hi Baron,

thanks for the code.

Cheers
Pascal
Reply all
Reply to author
Forward
0 new messages