Factor out caching decision making

This commit is contained in:
Johan Wikman
2017-03-01 15:09:13 +02:00
parent 7daafd33fc
commit aa51528481
2 changed files with 135 additions and 118 deletions

View File

@ -130,66 +130,7 @@ int CacheFilterSession::routeQuery(GWBUF* pPacket)
break; break;
case MYSQL_COM_QUERY: case MYSQL_COM_QUERY:
{ if (should_consult_cache(pPacket))
bool consult_cache = false;
uint32_t type_mask = qc_get_type_mask(pPacket);
if (qc_query_is_type(type_mask, QUERY_TYPE_BEGIN_TRX))
{
// When a transaction is started, we initially assume it is read-only.
m_is_read_only = true;
}
else if (!qc_query_is_type(type_mask, QUERY_TYPE_READ))
{
// Thereafter, if there's any non-read statement we mark it as non-readonly.
// Note that the state of m_is_read_only is not consulted if there is no
// on-going transaction of if there is an explicitly read-only transaction.
m_is_read_only = false;
}
if (!session_trx_is_active(m_pSession))
{
if (log_decisions())
{
MXS_NOTICE("Cache can be used and stored to, since there is no transaction.");
}
consult_cache = true;
}
else if (session_trx_is_read_only(m_pSession))
{
if (log_decisions())
{
MXS_NOTICE("Cache can be used and stored to since there is an explicitly "
"read-only transaction.");
}
consult_cache = true;
}
else if (m_is_read_only)
{
if (log_decisions())
{
MXS_NOTICE("Cache can be used and stored to, since the current transaction "
"(not explicitly read-only) has so far been read-only.");
}
consult_cache = true;
}
else
{
if (log_decisions())
{
MXS_NOTICE("Cache can not be used, since a not explicitly read-only transaction "
"is active and the transaction has executed non-read statements.");
}
}
if (consult_cache)
{
// We do not care whether the query was fully parsed or not.
// If a query cannot be fully parsed, the worst thing that can
// happen is that caching is not used, even though it would be
// possible.
if (qc_get_operation(pPacket) == QUERY_OP_SELECT)
{ {
if (m_pCache->should_store(m_zDefaultDb, pPacket)) if (m_pCache->should_store(m_zDefaultDb, pPacket))
{ {
@ -267,8 +208,6 @@ int CacheFilterSession::routeQuery(GWBUF* pPacket)
m_state = CACHE_IGNORING_RESPONSE; m_state = CACHE_IGNORING_RESPONSE;
} }
} }
}
}
break; break;
default: default:
@ -704,3 +643,79 @@ void CacheFilterSession::store_result()
m_refreshing = false; m_refreshing = false;
} }
} }
/**
* Whether the cache should be consulted.
*
* @param pParam The GWBUF being handled.
*
* @return True, if the cache should be consulted, false otherwise.
*/
bool CacheFilterSession::should_consult_cache(GWBUF* pPacket)
{
bool consult_cache = false;
uint32_t type_mask = qc_get_type_mask(pPacket);
if (qc_query_is_type(type_mask, QUERY_TYPE_BEGIN_TRX))
{
// When a transaction is started, we initially assume it is read-only.
m_is_read_only = true;
}
else if (!qc_query_is_type(type_mask, QUERY_TYPE_READ))
{
// Thereafter, if there's any non-read statement we mark it as non-readonly.
// Note that the state of m_is_read_only is not consulted if there is no
// on-going transaction of if there is an explicitly read-only transaction.
m_is_read_only = false;
}
if (!session_trx_is_active(m_pSession))
{
if (log_decisions())
{
MXS_NOTICE("Cache can be used and stored to, since there is no transaction.");
}
consult_cache = true;
}
else if (session_trx_is_read_only(m_pSession))
{
if (log_decisions())
{
MXS_NOTICE("Cache can be used and stored to since there is an explicitly "
"read-only transaction.");
}
consult_cache = true;
}
else if (m_is_read_only)
{
if (log_decisions())
{
MXS_NOTICE("Cache can be used and stored to, since the current transaction "
"(not explicitly read-only) has so far been read-only.");
}
consult_cache = true;
}
else
{
if (log_decisions())
{
MXS_NOTICE("Cache can not be used, since a not explicitly read-only transaction "
"is active and the transaction has executed non-read statements.");
}
}
if (consult_cache)
{
// We do not care whether the query was fully parsed or not.
// If a query cannot be fully parsed, the worst thing that can
// happen is that caching is not used, even though it would be
// possible.
if (qc_get_operation(pPacket) != QUERY_OP_SELECT)
{
consult_cache = false;
}
}
return consult_cache;
}

View File

@ -105,6 +105,8 @@ private:
void store_result(); void store_result();
bool should_consult_cache(GWBUF* pPacket);
private: private:
CacheFilterSession(MXS_SESSION* pSession, Cache* pCache, char* zDefaultDb); CacheFilterSession(MXS_SESSION* pSession, Cache* pCache, char* zDefaultDb);