Favor pointers over references

The use of a pointer instead of a reference conveys the message that the
lifetime of the object being pointed can, and most likely will, exceed the
lifetime of the function.

Also shuffled the member variables and internal functions around and
removed unneeded function declarations.
This commit is contained in:
Markus Mäkelä
2017-03-27 18:05:47 +03:00
parent 02796e7304
commit b9fae58891
5 changed files with 52 additions and 52 deletions

View File

@ -187,7 +187,7 @@ SchemaRouter* SchemaRouter::create(SERVICE* pService, char** pzOptions)
SchemaRouterSession* SchemaRouter::newSession(MXS_SESSION* pSession) SchemaRouterSession* SchemaRouter::newSession(MXS_SESSION* pSession)
{ {
return new SchemaRouterSession(pSession, *this); return new SchemaRouterSession(pSession, this);
} }
void SchemaRouter::diagnostics(DCB* dcb) void SchemaRouter::diagnostics(DCB* dcb)

View File

@ -39,9 +39,13 @@ public:
void diagnostics(DCB* pDcb); void diagnostics(DCB* pDcb);
uint64_t getCapabilities(); uint64_t getCapabilities();
protected: private:
friend class SchemaRouterSession; friend class SchemaRouterSession;
/** Internal functions */
SchemaRouter(SERVICE *service, char **options);
/** Member variables */
schemarouter_config_t m_config; /*< expanded config info from SERVICE */ schemarouter_config_t m_config; /*< expanded config info from SERVICE */
ShardManager m_shard_manager; /*< Shard maps hashed by user name */ ShardManager m_shard_manager; /*< Shard maps hashed by user name */
SERVICE* m_service; /*< Pointer to service */ SERVICE* m_service; /*< Pointer to service */
@ -54,6 +58,4 @@ protected:
* not cause the session to be terminated * not cause the session to be terminated
* if they are found on more than one server. */ * if they are found on more than one server. */
pcre2_match_data* m_ignore_match_data; pcre2_match_data* m_ignore_match_data;
SchemaRouter(SERVICE *service, char **options);
}; };

View File

@ -36,16 +36,16 @@ bool detect_show_shards(GWBUF* query);
void write_error_to_client(DCB* dcb, int errnum, const char* mysqlstate, const char* errmsg); void write_error_to_client(DCB* dcb, int errnum, const char* mysqlstate, const char* errmsg);
SchemaRouterSession::SchemaRouterSession(MXS_SESSION* session, SchemaRouter& router): SchemaRouterSession::SchemaRouterSession(MXS_SESSION* session, SchemaRouter* router):
mxs::RouterSession(session), mxs::RouterSession(session),
m_closed(false), m_closed(false),
m_client(session->client_dcb), m_client(session->client_dcb),
m_mysql_session((MYSQL_session*)session->client_dcb->data), m_mysql_session((MYSQL_session*)session->client_dcb->data),
m_backends(NULL), m_backends(NULL),
m_config(router.m_config), m_config(m_router->m_config),
m_backend_count(0), m_backend_count(0),
m_router(router), m_router(router),
m_shard(router.m_shard_manager.get_shard(m_client->user, m_config.refresh_min_interval)), m_shard(m_router->m_shard_manager.get_shard(m_client->user, m_config.refresh_min_interval)),
m_state(0), m_state(0),
m_sent_sescmd(0), m_sent_sescmd(0),
m_replied_sescmd(0) m_replied_sescmd(0)
@ -79,11 +79,11 @@ SchemaRouterSession::SchemaRouterSession(MXS_SESSION* session, SchemaRouter& rou
m_state |= INIT_USE_DB; m_state |= INIT_USE_DB;
} }
int router_nservers = router.m_service->n_dbref; int router_nservers = m_router->m_service->n_dbref;
backend_ref_t* backend_ref = new backend_ref_t[router_nservers]; backend_ref_t* backend_ref = new backend_ref_t[router_nservers];
int i = 0; int i = 0;
for (SERVER_REF *ref = router.m_service->dbref; ref && i < router_nservers; ref = ref->next) for (SERVER_REF *ref = m_router->m_service->dbref; ref && i < router_nservers; ref = ref->next)
{ {
if (ref->active) if (ref->active)
{ {
@ -116,7 +116,7 @@ SchemaRouterSession::SchemaRouterSession(MXS_SESSION* session, SchemaRouter& rou
m_connect_db = db; m_connect_db = db;
} }
atomic_add(&router.m_stats.sessions, 1); atomic_add(&m_router->m_stats.sessions, 1);
} }
SchemaRouterSession::~SchemaRouterSession() SchemaRouterSession::~SchemaRouterSession()
@ -165,26 +165,26 @@ void SchemaRouterSession::close()
} }
} }
spinlock_acquire(&m_router.m_lock); spinlock_acquire(&m_router->m_lock);
if (m_router.m_stats.longest_sescmd < m_stats.longest_sescmd) if (m_router->m_stats.longest_sescmd < m_stats.longest_sescmd)
{ {
m_router.m_stats.longest_sescmd = m_stats.longest_sescmd; m_router->m_stats.longest_sescmd = m_stats.longest_sescmd;
} }
double ses_time = difftime(time(NULL), m_client->session->stats.connect); double ses_time = difftime(time(NULL), m_client->session->stats.connect);
if (m_router.m_stats.ses_longest < ses_time) if (m_router->m_stats.ses_longest < ses_time)
{ {
m_router.m_stats.ses_longest = ses_time; m_router->m_stats.ses_longest = ses_time;
} }
if (m_router.m_stats.ses_shortest > ses_time && m_router.m_stats.ses_shortest > 0) if (m_router->m_stats.ses_shortest > ses_time && m_router->m_stats.ses_shortest > 0)
{ {
m_router.m_stats.ses_shortest = ses_time; m_router->m_stats.ses_shortest = ses_time;
} }
m_router.m_stats.ses_average = m_router->m_stats.ses_average =
(ses_time + ((m_router.m_stats.sessions - 1) * m_router.m_stats.ses_average)) / (ses_time + ((m_router->m_stats.sessions - 1) * m_router->m_stats.ses_average)) /
(m_router.m_stats.sessions); (m_router->m_stats.sessions);
spinlock_release(&m_router.m_lock); spinlock_release(&m_router->m_lock);
} }
} }
@ -437,8 +437,8 @@ int32_t SchemaRouterSession::routeQuery(GWBUF* pPacket)
/** Session commands, route to all servers */ /** Session commands, route to all servers */
if (route_session_write(pPacket, command)) if (route_session_write(pPacket, command))
{ {
atomic_add(&m_router.m_stats.n_sescmd, 1); atomic_add(&m_router->m_stats.n_sescmd, 1);
atomic_add(&m_router.m_stats.n_queries, 1); atomic_add(&m_router->m_stats.n_queries, 1);
ret = 1; ret = 1;
} }
} }
@ -469,7 +469,7 @@ int32_t SchemaRouterSession::routeQuery(GWBUF* pPacket)
{ {
backend_ref_t* bref; backend_ref_t* bref;
atomic_add(&m_router.m_stats.n_queries, 1); atomic_add(&m_router->m_stats.n_queries, 1);
/** /**
* Add one query response waiter to backend reference * Add one query response waiter to backend reference
@ -624,7 +624,7 @@ void SchemaRouterSession::clientReply(GWBUF* pPacket, DCB* pDcb)
if (ret == 1) if (ret == 1)
{ {
atomic_add(&m_router.m_stats.n_queries, 1); atomic_add(&m_router->m_stats.n_queries, 1);
bref_set_state(bref, BREF_QUERY_ACTIVE); bref_set_state(bref, BREF_QUERY_ACTIVE);
bref_set_state(bref, BREF_WAITING_RESULT); bref_set_state(bref, BREF_WAITING_RESULT);
} }
@ -679,7 +679,7 @@ void SchemaRouterSession::handleError(GWBUF* pMessage,
* this user. * this user.
* *
* If the router doesn't have a shard map for this user then the current shard map * If the router doesn't have a shard map for this user then the current shard map
* of the client session is added to the router. If the shard map in the router is * of the client session is added to the m_router-> If the shard map in the router is
* out of date, its contents are replaced with the contents of the current client * out of date, its contents are replaced with the contents of the current client
* session. If the router has a usable shard map, the current shard map of the client * session. If the router has a usable shard map, the current shard map of the client
* is discarded and the router's shard map is used. * is discarded and the router's shard map is used.
@ -687,8 +687,8 @@ void SchemaRouterSession::handleError(GWBUF* pMessage,
*/ */
void SchemaRouterSession::synchronize_shard_map() void SchemaRouterSession::synchronize_shard_map()
{ {
m_router.m_stats.shmap_cache_miss++; m_router->m_stats.shmap_cache_miss++;
m_router.m_shard_manager.update_shard(m_shard, m_client->user); m_router->m_shard_manager.update_shard(m_shard, m_client->user);
} }
/** /**
@ -1476,11 +1476,11 @@ showdb_response_t SchemaRouterSession::parse_showdb_response(backend_ref_t* bref
} }
else else
{ {
if (!(m_router.m_ignored_dbs.find(data) != m_router.m_ignored_dbs.end() || if (!(m_router->m_ignored_dbs.find(data) != m_router->m_ignored_dbs.end() ||
(m_router.m_ignore_regex && (m_router->m_ignore_regex &&
pcre2_match(m_router.m_ignore_regex, (PCRE2_SPTR)data, pcre2_match(m_router->m_ignore_regex, (PCRE2_SPTR)data,
PCRE2_ZERO_TERMINATED, 0, 0, PCRE2_ZERO_TERMINATED, 0, 0,
m_router.m_ignore_match_data, NULL) >= 0))) m_router->m_ignore_match_data, NULL) >= 0)))
{ {
duplicate_found = true; duplicate_found = true;
SERVER *duplicate = m_shard.get_location(data); SERVER *duplicate = m_shard.get_location(data);

View File

@ -117,7 +117,7 @@ class SchemaRouterSession: public mxs::RouterSession
{ {
public: public:
SchemaRouterSession(MXS_SESSION* session, SchemaRouter& router); SchemaRouterSession(MXS_SESSION* session, SchemaRouter* router);
/** /**
* The RouterSession instance will be deleted when a client session * The RouterSession instance will be deleted when a client session
@ -159,22 +159,6 @@ public:
mxs_error_action_t action, mxs_error_action_t action,
bool* pSuccess); bool* pSuccess);
private: private:
bool m_closed; /**< True if session closed */
DCB* m_client; /**< The client DCB */
MYSQL_session* m_mysql_session; /**< Session client data (username, password, SHA1). */
backend_ref_t* m_backends; /**< Pointer to backend reference array */
schemarouter_config_t m_config; /**< Copied config info from router instance */
int m_backend_count; /**< Number of backends */
SchemaRouter& m_router; /**< The router instance */
Shard m_shard; /**< Database to server mapping */
string m_connect_db; /**< Database the user was trying to connect to */
string m_current_db; /**< Current active database */
int m_state; /**< Initialization state bitmask */
list<Buffer> m_queue; /**< Query that was received before the session was ready */
ROUTER_STATS m_stats; /**< Statistics for this router */
uint64_t m_sent_sescmd; /**< The latest session command being executed */
uint64_t m_replied_sescmd; /**< The last session command reply that was sent to the client */
/** Internal functions */ /** Internal functions */
SERVER* get_shard_target(GWBUF* buffer, uint32_t qtype); SERVER* get_shard_target(GWBUF* buffer, uint32_t qtype);
backend_ref_t* get_bref_from_dcb(DCB* dcb); backend_ref_t* get_bref_from_dcb(DCB* dcb);
@ -196,4 +180,21 @@ private:
void process_response(backend_ref_t* bref, GWBUF** ppPacket); void process_response(backend_ref_t* bref, GWBUF** ppPacket);
SERVER* resolve_query_target(GWBUF* pPacket, uint32_t type, uint8_t command, SERVER* resolve_query_target(GWBUF* pPacket, uint32_t type, uint8_t command,
route_target_t& route_target); route_target_t& route_target);
/** Member variables */
bool m_closed; /**< True if session closed */
DCB* m_client; /**< The client DCB */
MYSQL_session* m_mysql_session; /**< Session client data (username, password, SHA1). */
backend_ref_t* m_backends; /**< Pointer to backend reference array */
schemarouter_config_t m_config; /**< Copied config info from router instance */
int m_backend_count; /**< Number of backends */
SchemaRouter* m_router; /**< The router instance */
Shard m_shard; /**< Database to server mapping */
string m_connect_db; /**< Database the user was trying to connect to */
string m_current_db; /**< Current active database */
int m_state; /**< Initialization state bitmask */
list<Buffer> m_queue; /**< Query that was received before the session was ready */
ROUTER_STATS m_stats; /**< Statistics for this router */
uint64_t m_sent_sescmd; /**< The latest session command being executed */
uint64_t m_replied_sescmd; /**< The last session command reply that was sent to the client */
}; };

View File

@ -79,7 +79,4 @@ private:
uint8_t m_command; /**< The command being executed */ uint8_t m_command; /**< The command being executed */
uint64_t m_pos; /**< Unique position identifier */ uint64_t m_pos; /**< Unique position identifier */
bool m_reply_sent; /**< Whether the session command reply has been sent */ bool m_reply_sent; /**< Whether the session command reply has been sent */
SessionCommand();
SessionCommand& operator = (const SessionCommand& command);
}; };