Skip to content

Commit

Permalink
Merge pull request #593 from sebastianelsner/tickets-unordered-map
Browse files Browse the repository at this point in the history
use unordered maps for tickets #592
  • Loading branch information
timurhai authored Mar 11, 2024
2 parents 5d52f03 + cbe2845 commit 8279bf3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
10 changes: 5 additions & 5 deletions afanasy/src/libafanasy/affarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ void Farm::readwrite(Msg *msg)
rw_String (m_properties_host, msg);
}

void Farm::rw_Tickets(std::map<std::string, Tiks> & io_tickets, Msg * io_msg)
void Farm::rw_Tickets(std::unordered_map<std::string, Tiks> & io_tickets, Msg * io_msg)
{
uint32_t size = io_tickets.size();
rw_uint32_t(size, io_msg);

if (io_msg->isWriting())
for (std::map<std::string, Tiks>::iterator it = io_tickets.begin(); it != io_tickets.end(); it++)
for (std::unordered_map<std::string, Tiks>::iterator it = io_tickets.begin(); it != io_tickets.end(); it++)
{
w_String (it->first, io_msg);
rw_int32_t(it->second.count, io_msg);
Expand Down Expand Up @@ -91,7 +91,7 @@ void Farm::jsonRead(const JSON &i_object, std::string *io_changes)
jr_Tickets("tickets_host", m_tickets_host, i_object);
}

bool Farm::jr_Tickets(const char * i_name, std::map<std::string, Tiks> & o_tickets, const JSON & i_object)
bool Farm::jr_Tickets(const char * i_name, std::unordered_map<std::string, Tiks> & o_tickets, const JSON & i_object)
{
const JSON & jObj = i_object[i_name];
if( false == jObj.IsObject())
Expand Down Expand Up @@ -162,10 +162,10 @@ void Farm::jsonWrite(std::ostringstream &o_str, int i_type) const
o_str << ",\n\"properties_host\":\"" << m_properties_host << "\"";
}

void Farm::jw_Tickets(const char * i_name, const std::map<std::string, Tiks> & i_tickets, std::ostringstream & o_str)
void Farm::jw_Tickets(const char * i_name, const std::unordered_map<std::string, Tiks> & i_tickets, std::ostringstream & o_str)
{
o_str << ",\n\"" << i_name << "\":{";
for (std::map<std::string, Tiks>::const_iterator it = i_tickets.begin(); it != i_tickets.end(); it++)
for (std::unordered_map<std::string, Tiks>::const_iterator it = i_tickets.begin(); it != i_tickets.end(); it++)
{
if (it != i_tickets.begin())
o_str << "\n,";
Expand Down
10 changes: 5 additions & 5 deletions afanasy/src/libafanasy/affarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ struct Tiks
std::vector<std::string> m_services;
std::vector<std::string> m_services_disabled;

std::map<std::string, Tiks> m_tickets_pool;
std::map<std::string, Tiks> m_tickets_host;
std::unordered_map<std::string, Tiks> m_tickets_pool;
std::unordered_map<std::string, Tiks> m_tickets_host;

int32_t m_max_tasks_host;
int32_t m_capacity_host;
Expand All @@ -78,8 +78,8 @@ struct Tiks
std::string m_properties_host;

private:
static void rw_Tickets(std::map<std::string, Tiks> & io_tickets, Msg * io_msg);
static bool jr_Tickets(const char * i_name, std::map<std::string, Tiks> & o_map, const JSON & i_object);
static void jw_Tickets(const char * i_name, const std::map<std::string, Tiks> & i_map, std::ostringstream & o_str);
static void rw_Tickets(std::unordered_map<std::string, Tiks> & io_tickets, Msg * io_msg);
static bool jr_Tickets(const char * i_name, std::unordered_map<std::string, Tiks> & o_map, const JSON & i_object);
static void jw_Tickets(const char * i_name, const std::unordered_map<std::string, Tiks> & i_map, std::ostringstream & o_str);
};
}
1 change: 1 addition & 0 deletions afanasy/src/libafanasy/name_af.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <iostream>
#include <list>
#include <map>
#include <unordered_map>
#include <memory.h>
#include <stdint.h>
#include <stdio.h>
Expand Down
6 changes: 3 additions & 3 deletions afanasy/src/server/afnodefarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ bool AfNodeFarm::actionTicket(Action & i_action)
af::jr_bool("host", tk_host, operation);

// Choose to pool or host tickets to operate
std::map<std::string, af::Farm::Tiks> * tickets;
std::unordered_map<std::string, af::Farm::Tiks> * tickets;
if (tk_host)
{
tickets = &m_farm->m_tickets_host;
Expand All @@ -237,7 +237,7 @@ bool AfNodeFarm::actionTicket(Action & i_action)
}

// Find ticket and get usage:
std::map<std::string, af::Farm::Tiks>::iterator it = tickets->find(tk_name);
std::unordered_map<std::string, af::Farm::Tiks>::iterator it = tickets->find(tk_name);
int32_t tk_usage = 0;
if (it != tickets->end())
tk_usage = it->second.usage;
Expand Down Expand Up @@ -314,7 +314,7 @@ bool AfNodeFarm::canRunService(const std::string & i_service_name, bool i_hasSer

bool AfNodeFarm::hasHostTicket(const std::string & i_name, const int32_t & i_count) const
{
std::map<std::string, af::Farm::Tiks>::const_iterator it = m_farm->m_tickets_host.find(i_name);
std::unordered_map<std::string, af::Farm::Tiks>::const_iterator it = m_farm->m_tickets_host.find(i_name);
if (it != m_farm->m_tickets_host.end())
{
if (it->second.count == -1)
Expand Down
10 changes: 5 additions & 5 deletions afanasy/src/server/poolsrv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void PoolSrv::actionDeleteRenders(Action & i_action, const std::string & i_log)

bool PoolSrv::hasPoolTicket(const std::string & i_name, const int32_t & i_count, const bool i_ticket_running) const
{
std::map<std::string, af::Farm::Tiks>::const_iterator it = m_farm->m_tickets_pool.find(i_name);
std::unordered_map<std::string, af::Farm::Tiks>::const_iterator it = m_farm->m_tickets_pool.find(i_name);
if (it != m_farm->m_tickets_pool.end())
{
if (it->second.count == -1)
Expand Down Expand Up @@ -521,7 +521,7 @@ void PoolSrv::taskAcuire(const af::TaskExec * i_taskexec, const std::list<std::s
// Increment tickets:
for (auto const& eIt : i_taskexec->getTickets())
{
std::map<std::string, af::Farm::Tiks>::iterator it = m_tickets_pool.find(eIt.first);
std::unordered_map<std::string, af::Farm::Tiks>::iterator it = m_tickets_pool.find(eIt.first);
if (it != m_tickets_pool.end())
it->second.usage += eIt.second;
else
Expand Down Expand Up @@ -556,7 +556,7 @@ void PoolSrv::taskRelease(const af::TaskExec * i_taskexec, const std::list<std::
// Decrement tickets
for (auto const& eIt : i_taskexec->getTickets())
{
std::map<std::string, af::Farm::Tiks>::iterator it = m_tickets_pool.find(eIt.first);
std::unordered_map<std::string, af::Farm::Tiks>::iterator it = m_tickets_pool.find(eIt.first);
if (it != m_tickets_pool.end())
{
it->second.usage -= eIt.second;
Expand Down Expand Up @@ -681,15 +681,15 @@ void PoolSrv::v_refresh(time_t i_currentTime, AfContainer * i_container, Monitor
i_monitoring->addEvent(af::Monitor::EVT_pools_change, m_id);

// Remove dummy tickets that were needed to store usage only
std::map<std::string, Tiks>::iterator pIt = m_tickets_pool.begin();
std::unordered_map<std::string, Tiks>::iterator pIt = m_tickets_pool.begin();
while (pIt != m_tickets_pool.end())
{
if ((pIt->second.count < 0) && (pIt->second.usage <= 0))
pIt = m_tickets_pool.erase(pIt);
else
pIt++;
}
std::map<std::string, Tiks>::iterator hIt = m_tickets_host.begin();
std::unordered_map<std::string, Tiks>::iterator hIt = m_tickets_host.begin();
while (hIt != m_tickets_host.end())
{
if ((hIt->second.count < 0) && (hIt->second.usage <= 0))
Expand Down
8 changes: 4 additions & 4 deletions afanasy/src/server/renderaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ bool RenderAf::hasTickets(const std::map<std::string, int32_t> & i_tickets) cons
if (m_parent)
{
bool ticket_running = false;
std::map<std::string, af::Farm::Tiks>::const_iterator rIt = m_farm->m_tickets_host.find(it.first);
std::unordered_map<std::string, af::Farm::Tiks>::const_iterator rIt = m_farm->m_tickets_host.find(it.first);
if (rIt != m_tickets_host.end())
{
if (rIt->second.usage > 0)
Expand Down Expand Up @@ -848,7 +848,7 @@ void RenderAf::addTask(af::TaskExec * i_taskexec, MonitorContainer * i_monitorin
std::list<std::string> new_tickets;
for (auto & tIt : i_taskexec->getTickets())
{
std::map<std::string, af::Farm::Tiks>::iterator hIt = m_tickets_host.find(tIt.first);
std::unordered_map<std::string, af::Farm::Tiks>::iterator hIt = m_tickets_host.find(tIt.first);
if (hIt != m_tickets_host.end())
{
if (hIt->second.usage == 0)
Expand Down Expand Up @@ -901,7 +901,7 @@ void RenderAf::removeTask(const af::TaskExec * i_taskexec, MonitorContainer * i_
std::list<std::string> exp_tickets;
for (auto & tIt : i_taskexec->getTickets())
{
std::map<std::string, af::Farm::Tiks>::iterator hIt = m_tickets_host.find(tIt.first);
std::unordered_map<std::string, af::Farm::Tiks>::iterator hIt = m_tickets_host.find(tIt.first);
if (hIt != m_tickets_host.end())
{
hIt->second.usage -= tIt.second;
Expand Down Expand Up @@ -1005,7 +1005,7 @@ void RenderAf::v_refresh( time_t i_current_time, AfContainer * pointer, Monitor

// Remove dummy tickets that were needed to store usage only
bool dummy_ticket_found = false;
std::map<std::string, Tiks>::iterator hIt = m_tickets_host.begin();
std::unordered_map<std::string, Tiks>::iterator hIt = m_tickets_host.begin();
while (hIt != m_tickets_host.end())
{
if ((hIt->second.count < 0) && (hIt->second.usage <= 0))
Expand Down

0 comments on commit 8279bf3

Please sign in to comment.