Skip to content

Commit

Permalink
F #3393: Reduce number of updates referencing FTS field. Changed REPLACE
Browse files Browse the repository at this point in the history
operations to UPDATE to improve DB performance

co-authored-by: Christian González <cgonzalez@opennebula.systems>
(cherry picked from commit cbd9aee)
  • Loading branch information
rsmontero committed Jun 12, 2019
1 parent 0355990 commit 12f5be2
Show file tree
Hide file tree
Showing 33 changed files with 562 additions and 334 deletions.
21 changes: 11 additions & 10 deletions include/BitMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,6 @@ class BitMap : public Callbackable
{
std::ostringstream oss;

if (replace)
{
oss << "REPLACE ";
}
else
{
oss << "INSERT ";
}

std::string * zipped = one_util::zlib_compress(bs->to_string(), true);

if (zipped == 0)
Expand All @@ -315,8 +306,18 @@ class BitMap : public Callbackable

char * ezipped64 = db->escape_str(zipped->c_str());

oss << "INTO " << db_table << " (id, map) VALUES ("

if (replace)
{
oss << "UPDATE " << db_table << " SET "
<< "map = '" << ezipped64 << "' "
<< "WHERE id = " << id;
}
else
{
oss << "INSERT INTO " << db_table << " (id, map) VALUES ("
<< id << ",'" << ezipped64 << "')";
}

int rc = db->exec_wr(oss);

Expand Down
28 changes: 28 additions & 0 deletions include/RequestManagerRename.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class RequestManagerRename : public Request
pthread_mutex_unlock(&mutex);
}

/**
* Method por updating custom values not included in PoolSQL::update
* mainly used for updating search information in the VMs.
* @param object to be updated
* @return 0 on success
*/
virtual int extra_updates(PoolObjectSQL * obj)
{
return 0;
};

private:
/**
* Mutex to control concurrent access to the ongoing rename operations
Expand Down Expand Up @@ -121,6 +132,23 @@ class VirtualMachineRename : public RequestManagerRename
{
return -1;
}


int extra_updates(PoolObjectSQL * obj)
{
VirtualMachine * vm;

VirtualMachinePool * vmpool = static_cast<VirtualMachinePool *>(pool);

if (obj == 0)
{
return -1;
}

vm = static_cast<VirtualMachine *>(obj);

return vmpool->update_search(vm);
};
};

/* ------------------------------------------------------------------------- */
Expand Down
27 changes: 27 additions & 0 deletions include/RequestManagerUpdateTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ class RequestManagerUpdateTemplate: public Request

virtual int append_template(PoolObjectSQL * object, const string & tmpl,
const RequestAttributes &att, string &error_str);

/**
* Method por updating custom values not included in PoolSQL::update
* mainly used for updating search information in the VMs.
* @param object to be updated
* @return 0 on success
*/
virtual int extra_updates(PoolObjectSQL * obj)
{
return 0;
};
};

/* ------------------------------------------------------------------------- */
Expand Down Expand Up @@ -104,6 +115,22 @@ class VirtualMachineUpdateTemplate: public RequestManagerUpdateTemplate
};

~VirtualMachineUpdateTemplate(){};

int extra_updates(PoolObjectSQL * obj)
{
VirtualMachine * vm;

VirtualMachinePool * vmpool = static_cast<VirtualMachinePool *>(pool);

if (obj == 0)
{
return -1;
}

vm = static_cast<VirtualMachine *>(obj);

return vmpool->update_search(vm);
};
};

/* ------------------------------------------------------------------------- */
Expand Down
41 changes: 33 additions & 8 deletions include/VirtualMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1848,27 +1848,44 @@ class VirtualMachine : public PoolObjectSQL
*/
int update_history(SqlDB * db)
{
if ( history != 0 )
if ( history == 0 )
{
return history->update(db);
}
else
return -1;
}

return history->update(db);
};

/**
* Insert a new VM history record
* @param db pointer to the db
* @return 0 on success
*/
int insert_history(SqlDB * db)
{
std::string error;

if ( history == 0 )
{
return -1;
}

return history->insert(db, error);
}

/**
* Updates the previous history record
* @param db pointer to the db
* @return 0 on success
*/
int update_previous_history(SqlDB * db)
{
if ( previous_history != 0 )
if ( previous_history == 0 )
{
return previous_history->update(db);
}
else
return -1;
}

return previous_history->update(db);
};

/**
Expand All @@ -1879,6 +1896,14 @@ class VirtualMachine : public PoolObjectSQL
*/
int update_monitoring(SqlDB * db);

/**
* Updates the VM search information.
*
* @param db pointer to the db
* @return 0 on success
*/
int update_search(SqlDB * db);

/**
* Function that renders the VM in XML format optinally including
* extended information (all history records)
Expand Down
22 changes: 22 additions & 0 deletions include/VirtualMachinePool.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ class VirtualMachinePool : public PoolSQL
// Virtual Machine DB access functions
//--------------------------------------------------------------------------

/**
* Insert a new history record of a VM, the vm's mutex SHOULD be locked
* @param vm pointer to the virtual machine object
* @return 0 on success
*/
int insert_history(
VirtualMachine * vm)
{
return vm->insert_history(db);
}

/**
* Updates the history record of a VM, the vm's mutex SHOULD be locked
* @param vm pointer to the virtual machine object
Expand Down Expand Up @@ -241,6 +252,17 @@ class VirtualMachinePool : public PoolSQL
return vm->update_monitoring(db);
};

/**
* Updates the VM's search information
* @param vm pointer to the virtual machine object
* @return 0 on success
*/
int update_search(
VirtualMachine * vm)
{
return vm->update_search(db);
}

/**
* Deletes the expired monitoring entries for all VMs
*
Expand Down
33 changes: 19 additions & 14 deletions src/cluster/Cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,30 @@ int Cluster::insert_replace(SqlDB *db, bool replace, string& error_str)

if ( replace )
{
oss << "REPLACE";
oss << "UPDATE " << table << " SET "
<< "name = '" << sql_name << "', "
<< "body = '" << sql_xml << "', "
<< "uid = " << uid << ", "
<< "gid = " << gid << ", "
<< "owner_u = " << owner_u << ", "
<< "group_u = " << group_u << ", "
<< "other_u = " << other_u
<< " WHERE oid = " << oid;

}
else
{
oss << "INSERT";
oss << "INSERT INTO " << table << " (" << db_names << ") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< gid << ","
<< owner_u << ","
<< group_u << ","
<< other_u << ")";
}

// Construct the SQL statement to Insert or Replace
oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< gid << ","
<< owner_u << ","
<< group_u << ","
<< other_u << ")";


rc = db->exec_wr(oss);

db->free_str(sql_name);
Expand Down
32 changes: 18 additions & 14 deletions src/datastore/Datastore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -701,25 +701,29 @@ int Datastore::insert_replace(SqlDB *db, bool replace, string& error_str)

if ( replace )
{
oss << "REPLACE";
oss << "UPDATE " << table << " SET "
<< "name = '" << sql_name << "', "
<< "body = '" << sql_xml << "', "
<< "uid = " << uid << ", "
<< "gid = " << gid << ", "
<< "owner_u = " << owner_u << ", "
<< "group_u = " << group_u << ", "
<< "other_u = " << other_u
<< " WHERE oid = " << oid;
}
else
{
oss << "INSERT";
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< gid << ","
<< owner_u << ","
<< group_u << ","
<< other_u << ")";
}

// Construct the SQL statement to Insert or Replace

oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< gid << ","
<< owner_u << ","
<< group_u << ","
<< other_u << ")";

rc = db->exec_wr(oss);

db->free_str(sql_name);
Expand Down
Loading

0 comments on commit 12f5be2

Please sign in to comment.