Skip to content

Commit

Permalink
B #6325: Fix Scheduled Actions for VM Template (#2746)
Browse files Browse the repository at this point in the history
  • Loading branch information
paczerny authored Sep 21, 2023
1 parent 253e2d3 commit a97b746
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 21 deletions.
2 changes: 2 additions & 0 deletions include/ScheduledActionPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class ScheduledActionPool : public PoolSQL
* Return list of due actions <sched_id, resource_id> for specific object type
*/
std::vector<std::pair<int, int>> get_is_due_actions(PoolObjectSQL::ObjectType ot);

int drop_sched_actions(const std::vector<int>& sa_ids);
};

#endif
24 changes: 4 additions & 20 deletions src/rm/RequestManagerAllocate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,6 @@ void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params,

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
static int drop_sched_actions(ScheduledActionPool *pool, std::vector<int> sa_ids)
{
std::string error;
int i = 0;

for (const auto& id : sa_ids)
{
if (auto sa = pool->get(id))
{
pool->drop(sa.get(), error);
i++;
}
}

return i;
}

Request::ErrorCode VirtualMachineAllocate::pool_allocate(
xmlrpc_c::paramList const& paramList,
Expand Down Expand Up @@ -348,7 +332,7 @@ Request::ErrorCode VirtualMachineAllocate::pool_allocate(
/* ---------------------------------------------------------------------- */
if (sa_error)
{
drop_sched_actions(sapool, sa_ids);
sapool->drop_sched_actions(sa_ids);

goto error_drop_vm;
}
Expand All @@ -369,7 +353,7 @@ Request::ErrorCode VirtualMachineAllocate::pool_allocate(
{
att.resp_msg = "VM deleted while setting up SCHED_ACTION";

drop_sched_actions(sapool, sa_ids);
sapool->drop_sched_actions(sa_ids);

return Request::INTERNAL;
}
Expand Down Expand Up @@ -1531,7 +1515,7 @@ Request::ErrorCode BackupJobAllocate::pool_allocate(
/* ---------------------------------------------------------------------- */
if (sa_error)
{
drop_sched_actions(sapool, sa_ids);
sapool->drop_sched_actions(sa_ids);

if ( auto bj = bjpool->get(id) )
{
Expand All @@ -1558,7 +1542,7 @@ Request::ErrorCode BackupJobAllocate::pool_allocate(
else
{
// BackupJob no longer exits, delete SchedActions
drop_sched_actions(sapool, sa_ids);
sapool->drop_sched_actions(sa_ids);

att.resp_msg = "BACKUPJOB deleted while setting up SCHED_ACTION";

Expand Down
72 changes: 72 additions & 0 deletions src/rm/RequestManagerVMTemplate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "VirtualMachine.h"
#include "VirtualMachineDisk.h"
#include "VirtualMachinePool.h"
#include "ScheduledActionPool.h"
#include "PoolObjectAuth.h"
#include "Nebula.h"
#include "RequestManagerClone.h"
Expand Down Expand Up @@ -276,6 +277,13 @@ Request::ErrorCode VMTemplateInstantiate::request_execute(int id, const string&
return AUTHORIZATION;
}

/* ---------------------------------------------------------------------- */
/* Save SCHED_ACTION attributes for allocation */
/* ---------------------------------------------------------------------- */
std::vector<unique_ptr<VectorAttribute>> sas;

tmpl->remove("SCHED_ACTION", sas);

rc = vmpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask,
move(tmpl), &vid, att.resp_msg, on_hold);

Expand All @@ -291,6 +299,70 @@ Request::ErrorCode VMTemplateInstantiate::request_execute(int id, const string&
return ALLOCATE;
}

/* ---------------------------------------------------------------------- */
/* Create ScheduleAction and associate to the VM */
/* ---------------------------------------------------------------------- */
auto sapool = Nebula::instance().get_sapool();

time_t stime = time(0);
bool sa_error = false;

std::vector<int> sa_ids;

for (const auto& sa : sas)
{
int sa_id = sapool->allocate(PoolObjectSQL::VM, vid, stime, sa.get(), att.resp_msg);

if (sa_id < 0)
{
sa_error = true;
break;
}

sa_ids.push_back(sa_id);
}

/* ---------------------------------------------------------------------- */
/* Error creating a SCHED_ACTION rollback created objects */
/* ---------------------------------------------------------------------- */
if (sa_error)
{
// Consistency check, the VM template should not have parsing errors
// of Scheduled Actions at this point.
sapool->drop_sched_actions(sa_ids);

// Test the rollback quota, not sure if it's correct
quota_rollback(&extended_tmpl, Quotas::VIRTUALMACHINE, att);

for ( auto& ds : applied )
{
quota_rollback(ds.get(), Quotas::DATASTORE, att);
}

return Request::INTERNAL;
}

/* ---------------------------------------------------------------------- */
/* Associate SCHED_ACTIONS to the VM */
/* ---------------------------------------------------------------------- */
if ( auto vm = vmpool->get(vid) )
{
for (const auto sa_id: sa_ids)
{
vm->sched_actions().add(sa_id);
}

vmpool->update(vm.get());
}
else
{
att.resp_msg = "VM deleted while setting up SCHED_ACTION";

sapool->drop_sched_actions(sa_ids);

return Request::INTERNAL;
}

return SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion src/sam/ScheduledAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ time_t ScheduledAction::parse_time(std::string str_time, time_t origin)

std::istringstream iss;

if ( origin == 0 && str_time[0] == '+')
if ( _type == BACKUPJOB && str_time[0] == '+' )
{
return -1;
}
Expand Down
20 changes: 20 additions & 0 deletions src/sam/ScheduledActionPool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,23 @@ std::vector<std::pair<int, int>> ScheduledActionPool::get_is_due_actions(PoolObj

return actions;
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

int ScheduledActionPool::drop_sched_actions(const std::vector<int>& sa_ids)
{
std::string error;
int i = 0;

for (const auto& id : sa_ids)
{
if (auto sa = get(id))
{
drop(sa.get(), error);
i++;
}
}

return i;
}

0 comments on commit a97b746

Please sign in to comment.