Change session registry to a template class

The template class wraps a HashMap such that only a few operations
are allowed. Usage requires specializing a RegistryTraits class
template for each entry type.
This commit is contained in:
Esa Korhonen
2017-05-16 13:24:00 +03:00
parent 322983a5f4
commit dbfd631fed
7 changed files with 148 additions and 63 deletions

View File

@ -0,0 +1,39 @@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2019-07-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxscale/cppdefs.hh>
#include <maxscale/session.h>
namespace maxscale
{
/**
* Specialization of RegistryTraits for the session registry.
*/
template<>
struct RegistryTraits<MXS_SESSION>
{
typedef uint64_t id_type;
typedef MXS_SESSION* entry_type;
static id_type get_id(entry_type entry)
{
return entry->ses_id;
}
static entry_type null_entry()
{
return NULL;
}
};
}

View File

@ -14,13 +14,14 @@
#include <maxscale/cppdefs.hh>
#include <memory>
#include <tr1/unordered_map>
#include <maxscale/platform.h>
#include <maxscale/session.h>
#include <maxscale/utils.hh>
#include "messagequeue.hh"
#include "poll.h"
#include "worker.h"
#include "workertask.hh"
#include "session.hh"
namespace maxscale
{
@ -69,7 +70,7 @@ public:
typedef WORKER_STATISTICS STATISTICS;
typedef WorkerTask Task;
typedef WorkerDisposableTask DisposableTask;
typedef std::tr1::unordered_map<uint64_t, MXS_SESSION*> SessionsById;
typedef Registry<MXS_SESSION> SessionsById;
enum state_t
{
@ -397,28 +398,11 @@ public:
bool post_message(uint32_t msg_id, intptr_t arg1, intptr_t arg2);
/**
* Add a session to the session container.
* Return a reference to the session registry of this worker.
*
* @param session The session to add
* @return true if successful
* @return Session registry.
*/
bool register_session(MXS_SESSION* session);
/**
* Remove a session from the session container.
*
* @param id Session id
* @return The removed session, or NULL if not found
*/
MXS_SESSION* deregister_session(uint64_t id);
/**
* Find a session in the session container.
*
* @param id Session id
* @return The found session, or NULL if not found
*/
MXS_SESSION* find_session(uint64_t id);
SessionsById& session_registry();
/**
* Broadcast a message to all worker.

View File

@ -109,7 +109,7 @@ public:
void execute(maxscale::Worker& worker)
{
MXS_SESSION* target = worker.find_session(m_target_id);
MXS_SESSION* target = worker.session_registry().lookup(m_target_id);
if (target && issuer_can_kill_target(m_issuer_user, m_issuer_host, target))
{
poll_fake_hangup_event(target->client_dcb);

View File

@ -728,55 +728,26 @@ bool mxs_worker_register_session(MXS_SESSION* session)
{
Worker* worker = Worker::get_current();
ss_dassert(worker);
return worker->register_session(session);
return worker->session_registry().add(session);
}
MXS_SESSION* mxs_worker_deregister_session(uint64_t id)
bool mxs_worker_deregister_session(uint64_t id)
{
MXS_SESSION* rval = NULL;
Worker* worker = Worker::get_current();
if (worker)
{
rval = worker->deregister_session(id);
}
return rval;
ss_dassert(worker);
return worker->session_registry().remove(id);
}
MXS_SESSION* mxs_worker_find_session(uint64_t id)
{
MXS_SESSION* rval = NULL;
Worker* worker = Worker::get_current();
if (worker)
{
rval = worker->find_session(id);
}
return rval;
ss_dassert(worker);
return worker->session_registry().lookup(id);
}
bool Worker::register_session(MXS_SESSION* session)
Worker::SessionsById& Worker::session_registry()
{
return m_sessions.insert(SessionsById::value_type(session->ses_id, session)).second;
}
MXS_SESSION* Worker::deregister_session(uint64_t id)
{
MXS_SESSION* rval = find_session(id);
if (rval)
{
m_sessions.erase(id);
}
return rval;
}
MXS_SESSION* Worker::find_session(uint64_t id)
{
MXS_SESSION* rval = NULL;
SessionsById::const_iterator iter = m_sessions.find(id);
if (iter != m_sessions.end())
{
rval = iter->second;
}
return rval;
return m_sessions;
}
class WorkerInfoTask: public maxscale::WorkerTask