Commit d855c6b8 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

refactor(backend): keep track of instances

Instead of searching for machines in nodes we keep
track of instances so it is faster to remove them.

issue #1235
parent 7366e804
......@@ -1514,6 +1514,7 @@ sub _after_remove_domain {
$self->_remove_access_attributes_db();
$self->_remove_access_grants_db();
$self->_remove_ports_db();
$self->_remove_instance_db();
$self->_remove_volumes_db();
$self->_remove_bases_vm_db();
$self->_remove_domain_db();
......@@ -1532,13 +1533,11 @@ sub _remove_domain_cascade($self,$user, $cascade = 1) {
return if !$self->_vm;
my $domain_name = $self->name or confess "Unknown my self name $self ".Dumper($self->{_data});
my $sth = $$CONNECTOR->dbh->prepare("SELECT id,name FROM vms WHERE is_active=1");
my ($id, $name);
$sth->execute();
$sth->bind_columns(\($id, $name));
while ($sth->fetchrow) {
next if $id == $self->_vm->id;
my $vm = Ravada::VM->open($id);
my @instances = $self->list_instances();
return if !scalar(@instances);
for my $instance ( @instances ) {
next if $instance->{id_vm} == $self->_vm->id;
my $vm = Ravada::VM->open($instance->{id_vm});
my $domain;
eval { $domain = $vm->search_domain($domain_name) };
$domain->remove($user, $cascade) if $domain;
......@@ -1588,6 +1587,13 @@ sub _remove_bases_vm_db($self) {
$sth->finish;
}
sub _remove_instance_db($self) {
my $sth = $$CONNECTOR->dbh->prepare("DELETE FROM domain_instances "
." WHERE id_domain=? AND id_vm=?"
);
$sth->execute($self->id, $self->_vm->id);
}
sub _remove_domain_db {
my $self = shift;
......@@ -3578,6 +3584,7 @@ sub _pre_migrate($self, $node, $request = undef) {
}
$self->_set_base_vm_db($node->id,0);
$node->_add_instance_db($self->id);
}
sub _post_migrate($self, $node, $request = undef) {
......@@ -3700,6 +3707,7 @@ sub set_base_vm($self, %args) {
my $vm_local = $self->_vm->new( host => 'localhost' );
$self->_set_vm($vm_local, 1);
}
$vm->_add_instance_db($self->id);
return $self->_set_base_vm_db($vm->id, $value);
}
......@@ -4087,7 +4095,7 @@ sub _post_change_hardware($self, $hardware, $index, $data=undef) {
my @volumes = $self->list_volumes_info();
}
$self->info(Ravada::Utils::user_daemon) if $self->is_known();
$self->_remove_domain_cascade(Ravada::Utils::user_daemon,1);
$self->_remove_domain_cascade(Ravada::Utils::user_daemon,1) if $self->is_known();
$self->needs_restart(1) if $self->is_known && $self->_data('status') eq 'active';
}
......@@ -4498,7 +4506,8 @@ sub _create_base_as_old($self, $user, $new_base) {
my @reqs;
for my $vm ($old_base->list_vms) {
next if $vm->is_local;
my @after = (after_request => $reqs[0]->id ) if @reqs;
my @after;
@after = (after_request => $reqs[0]->id ) if @reqs;
push @reqs, Ravada::Request->set_base_vm(
uid => $user->id
,id_vm => $vm->id
......@@ -4587,4 +4596,19 @@ sub _check_rebase_vols($self, $new_base, $old) {
}
}
sub list_instances($self) {
return () if !$self->is_known();
my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM domain_instances "
." WHERE id_domain=?"
);
$sth->execute($self->id);
my @instances;
while (my $row = $sth->fetchrow_hashref) {
lock_hash(%$row);
push @instances, ( $row );
}
return @instances;
}
1;
......@@ -350,7 +350,7 @@ sub _ssh_channel($self) {
sleep 1;
}
if (!$ssh_channel) {
$ssh = $self->_connect_ssh(1);
$ssh = $self->_connect_ssh(1) or die "Error: I can't connect to ".$self->name;
$ssh_channel = $ssh->channel();
}
die $ssh->die_with_error if !$ssh_channel;
......@@ -421,6 +421,7 @@ sub _around_create_domain {
return $base->_search_pool_clone($owner) if $from_pool;
my $domain = $self->$orig(%args_create, volatile => $volatile);
$self->_add_instance_db($domain->id);
$domain->add_volume_swap( size => $swap ) if $swap;
if ($id_base) {
......@@ -452,6 +453,23 @@ sub _around_create_domain {
return $domain;
}
sub _add_instance_db($self, $id_domain) {
my $sth = $$CONNECTOR->dbh->prepare("SELECT * FROM domain_instances "
." WHERE id_domain=? AND id_vm=?"
);
$sth->execute($id_domain, $self->id);
my ($row) = $sth->fetchrow;
return if $row;
$sth = $$CONNECTOR->dbh->prepare("INSERT INTO domain_instances (id_domain, id_vm) "
." VALUES (?, ?)"
);
eval {
$sth->execute($id_domain, $self->id);
};
confess $@ if $@;
}
sub _define_spice_password($self, $remote_ip) {
my $spice_password = Ravada::Utils::random_name(4);
if ($remote_ip) {
......@@ -997,23 +1015,31 @@ Returns if the virtual manager connection is available
=cut
sub ping($self, $option=undef) {
sub ping($self, $option=undef, $cache=1) {
confess "ERROR: option unknown" if defined $option && $option ne 'debug';
return 1 if $self->is_local();
my $cache_key = "ping_".$self->host;
my $ping = $self->_get_cache($cache_key);
return $ping if defined $ping;
if ($cache) {
my $ping = $self->_get_cache($cache_key);
return $ping if defined $ping;
} else {
$self->_delete_cache($cache_key);
}
my $debug = 0;
$debug = 1 if defined $option && $option eq 'debug';
$ping = $self->_do_ping($self->host, $debug);
$self->_set_cache($cache_key => $ping);
my $ping = $self->_do_ping($self->host, $debug);
$self->_set_cache($cache_key => $ping) if $cache;
return $ping;
}
sub _delete_cache($self, $key) {
$key = "_cache_$key";
delete $self->{$key};
}
sub _set_cache($self, $key, $value) {
$key = "_cache_$key";
$self->{$key} = [ $value, time ];
......@@ -1053,11 +1079,14 @@ sub _do_ping($self, $host, $debug=0) {
return 0;
}
sub _around_ping($orig, $self, $option=undef) {
sub _around_ping($orig, $self, $option=undef, $cache=1) {
my $ping = $self->$orig($option);
$self->_cached_active($ping);
$self->_cached_active_time(time);
my $ping = $self->$orig($option, $cache);
if ($cache) {
$self->_cached_active($ping);
$self->_cached_active_time(time);
}
return $ping;
}
......
......@@ -292,6 +292,8 @@ sub _search_volume_remote($self, $pattern) {
my ($out, $err) = $self->run_command("ls -1 ".$self->dir_img);
confess $err if $err;
my $found;
for my $file ( split /\n/,$out ) {
$found = $self->dir_img."/".$file if $file eq $pattern;
......
CREATE TABLE `domain_instances` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT
, `id_domain` integer NOT NULL
, `id_vm` integer NOT NULL
, UNIQUE (`id_domain`,`id_vm`)
);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment