2010-10-07 15:54:07 +02:00
/*
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2010-08-18 02:25:52 +02:00
*/
#ifndef _DATABASEWORKERPOOL_H
#define _DATABASEWORKERPOOL_H
#include <ace/Atomic_Op_T.h>
#include <ace/Thread_Mutex.h>
2010-09-03 00:04:14 +02:00
#include "Common.h"
2010-08-18 19:48:51 +02:00
#include "Callback.h"
2010-08-18 02:25:52 +02:00
#include "MySQLConnection.h"
2010-09-03 00:04:14 +02:00
#include "Transaction.h"
2010-09-02 17:47:50 +02:00
#include "DatabaseWorker.h"
2010-09-03 00:08:20 +02:00
#include "PreparedStatement.h"
2010-09-02 17:19:53 -06:00
#include "Log.h"
2010-09-11 21:10:54 +02:00
#include "QueryResult.h"
2010-09-19 12:36:48 +02:00
#include "QueryHolder.h"
#include "AdhocStatement.h"
2010-08-18 02:25:52 +02:00
2010-09-25 01:05:24 +02:00
class PingOperation : public SQLOperation
{
/// Operation for idle delaythreads
bool Execute ()
{
2010-10-31 10:55:29 +01:00
if ( m_conn -> LockIfReady ())
{
m_conn -> Ping ();
m_conn -> Unlock ();
return true ;
}
2010-09-27 00:20:56 +02:00
return false ;
2010-09-25 01:05:24 +02:00
}
};
2010-09-02 17:47:50 +02:00
template < class T >
2010-08-18 02:25:52 +02:00
class DatabaseWorkerPool
{
2010-09-03 00:04:14 +02:00
private :
typedef ACE_Atomic_Op < ACE_SYNCH_MUTEX , uint32 > AtomicUInt ;
2010-08-18 02:25:52 +02:00
public :
2010-09-02 17:47:50 +02:00
DatabaseWorkerPool () :
m_queue ( new ACE_Activation_Queue ( new ACE_Message_Queue < ACE_MT_SYNCH > )),
m_connections ( 0 )
{
2010-09-27 00:20:56 +02:00
m_connections . resize ( IDX_SIZE );
2010-09-02 17:47:50 +02:00
mysql_library_init ( - 1 , NULL , NULL );
WPFatal ( mysql_thread_safe (), "Used MySQL library isn't thread-safe." );
}
~ DatabaseWorkerPool ()
{
2010-09-29 15:29:57 +02:00
sLog . outSQLDriver ( "~DatabaseWorkerPool for '%s'." , m_connectionInfo . database . c_str ());
2010-09-02 17:47:50 +02:00
mysql_library_end ();
}
2010-09-27 00:20:56 +02:00
bool Open ( const std :: string & infoString , uint8 async_threads , uint8 synch_threads )
2010-09-02 17:47:50 +02:00
{
2010-09-29 15:29:57 +02:00
m_connectionInfo = MySQLConnectionInfo ( infoString );
2010-09-29 08:48:06 +02:00
2010-09-29 15:29:57 +02:00
sLog . outSQLDriver ( "Opening databasepool '%s'. Async threads: %u, synch threads: %u" , m_connectionInfo . database . c_str (), async_threads , synch_threads );
2010-09-27 00:20:56 +02:00
/// Open asynchronous connections (delayed operations)
m_connections [ IDX_ASYNC ]. resize ( async_threads );
for ( uint8 i = 0 ; i < async_threads ; ++ i )
2010-09-03 10:52:32 +02:00
{
2010-09-29 15:29:57 +02:00
T * t = new T ( m_queue , m_connectionInfo );
t -> Open ();
2010-09-27 00:20:56 +02:00
m_connections [ IDX_ASYNC ][ i ] = t ;
++ m_connectionCount ;
2010-09-03 10:52:32 +02:00
}
2010-09-27 00:20:56 +02:00
/// Open synchronous connections (direct, blocking operations)
m_connections [ IDX_SYNCH ]. resize ( synch_threads );
for ( uint8 i = 0 ; i < synch_threads ; ++ i )
2010-09-02 17:47:50 +02:00
{
2010-09-29 15:29:57 +02:00
T * t = new T ( m_connectionInfo );
t -> Open ();
2010-09-27 00:20:56 +02:00
m_connections [ IDX_SYNCH ][ i ] = t ;
++ m_connectionCount ;
2010-09-02 17:47:50 +02:00
}
2010-09-27 00:20:56 +02:00
sLog . outSQLDriver ( "Databasepool opened succesfuly. %u connections running." , ( uint32 ) m_connectionCount . value ());
2010-09-02 17:47:50 +02:00
return true ;
}
void Close ()
{
2010-09-29 15:29:57 +02:00
sLog . outSQLDriver ( "Closing down databasepool '%s'." , m_connectionInfo . database . c_str ());
2010-09-02 17:47:50 +02:00
2010-09-27 00:20:56 +02:00
/// Shuts down delaythreads for this connection pool.
m_queue -> queue () -> deactivate ();
for ( uint8 i = 0 ; i < m_connections [ IDX_ASYNC ]. size (); ++ i )
2010-09-02 17:47:50 +02:00
{
2010-09-27 00:20:56 +02:00
/// TODO: Better way. probably should flip a boolean and check it on low level code before doing anything on the mysql ctx
/// Now we just wait until m_queue gives the signal to the worker threads to stop
T * t = m_connections [ IDX_ASYNC ][ i ];
t -> m_worker -> wait (); // t->Close(); is called from worker thread
-- m_connectionCount ;
2010-09-02 17:47:50 +02:00
}
2010-09-29 15:29:57 +02:00
sLog . outSQLDriver ( "Asynchronous connections on databasepool '%s' terminated. Proceeding with synchronous connections." , m_connectionInfo . database . c_str ());
2010-09-02 17:47:50 +02:00
2010-09-27 00:20:56 +02:00
/// Shut down the synchronous connections
for ( uint8 i = 0 ; i < m_connections [ IDX_SYNCH ]. size (); ++ i )
2010-09-02 17:47:50 +02:00
{
2010-09-27 00:20:56 +02:00
T * t = m_connections [ IDX_SYNCH ][ i ];
//while (1)
// if (t->LockIfReady()) -- For some reason deadlocks us
t -> Close ();
-- m_connectionCount ;
2010-09-02 17:47:50 +02:00
}
2010-09-27 00:20:56 +02:00
2010-09-29 15:29:57 +02:00
sLog . outSQLDriver ( "All connections on databasepool %s closed." , m_connectionInfo . database . c_str ());
2010-09-02 17:47:50 +02:00
}
void Execute ( const char * sql )
{
if ( ! sql )
return ;
BasicStatementTask * task = new BasicStatementTask ( sql );
Enqueue ( task );
}
void PExecute ( const char * sql , ...)
{
if ( ! sql )
return ;
va_list ap ;
char szQuery [ MAX_QUERY_LEN ];
va_start ( ap , sql );
vsnprintf ( szQuery , MAX_QUERY_LEN , sql , ap );
va_end ( ap );
Execute ( szQuery );
}
void DirectExecute ( const char * sql )
{
2010-09-27 00:20:56 +02:00
if ( ! sql )
return ;
T * t = GetFreeConnection ();
t -> Execute ( sql );
t -> Unlock ();
2010-09-02 17:47:50 +02:00
}
void DirectPExecute ( const char * sql , ...)
{
if ( ! sql )
return ;
va_list ap ;
char szQuery [ MAX_QUERY_LEN ];
va_start ( ap , sql );
vsnprintf ( szQuery , MAX_QUERY_LEN , sql , ap );
va_end ( ap );
return DirectExecute ( szQuery );
}
2010-09-27 00:20:56 +02:00
QueryResult Query ( const char * sql , MySQLConnection * conn = NULL )
2010-09-02 17:47:50 +02:00
{
2010-09-27 00:20:56 +02:00
if ( ! conn )
conn = GetFreeConnection ();
ResultSet * result = conn -> Query ( sql );
conn -> Unlock ();
2010-09-19 12:16:29 +02:00
if ( ! result || ! result -> GetRowCount ())
return QueryResult ( NULL );
result -> NextRow ();
return QueryResult ( result );
2010-09-02 17:47:50 +02:00
}
2010-09-27 00:20:56 +02:00
QueryResult PQuery ( const char * sql , MySQLConnection * conn , ...)
{
if ( ! sql )
return QueryResult ( NULL );
va_list ap ;
char szQuery [ MAX_QUERY_LEN ];
va_start ( ap , sql );
vsnprintf ( szQuery , MAX_QUERY_LEN , sql , ap );
va_end ( ap );
return Query ( szQuery , conn );
}
2010-09-11 21:10:54 +02:00
QueryResult PQuery ( const char * sql , ...)
2010-09-02 17:47:50 +02:00
{
if ( ! sql )
2010-09-11 21:10:54 +02:00
return QueryResult ( NULL );
2010-09-02 17:47:50 +02:00
va_list ap ;
char szQuery [ MAX_QUERY_LEN ];
va_start ( ap , sql );
vsnprintf ( szQuery , MAX_QUERY_LEN , sql , ap );
va_end ( ap );
return Query ( szQuery );
}
2010-09-11 21:10:54 +02:00
ACE_Future < QueryResult > AsyncQuery ( const char * sql )
2010-09-02 17:47:50 +02:00
{
QueryResultFuture res ;
BasicStatementTask * task = new BasicStatementTask ( sql , res );
Enqueue ( task );
return res ; //! Actual return value has no use yet
}
2010-09-11 21:10:54 +02:00
ACE_Future < QueryResult > AsyncPQuery ( const char * sql , ...)
2010-09-02 17:47:50 +02:00
{
va_list ap ;
char szQuery [ MAX_QUERY_LEN ];
va_start ( ap , sql );
vsnprintf ( szQuery , MAX_QUERY_LEN , sql , ap );
va_end ( ap );
return AsyncQuery ( szQuery );
}
QueryResultHolderFuture DelayQueryHolder ( SQLQueryHolder * holder )
{
QueryResultHolderFuture res ;
SQLQueryHolderTask * task = new SQLQueryHolderTask ( holder , res );
Enqueue ( task );
return res ; //! Fool compiler, has no use yet
}
2010-09-12 01:40:27 +02:00
2010-09-02 17:47:50 +02:00
SQLTransaction BeginTransaction ()
{
return SQLTransaction ( new Transaction );
}
void CommitTransaction ( SQLTransaction transaction )
{
2010-11-19 13:13:07 +01:00
if ( sLog . GetSQLDriverQueryLogging ())
2010-09-02 17:47:50 +02:00
{
2010-11-19 13:13:07 +01:00
if ( transaction -> GetSize () == 0 )
{
sLog . outSQLDriver ( "Transaction contains 0 queries. Not executing." );
return ;
}
if ( transaction -> GetSize () == 1 )
{
sLog . outSQLDriver ( "Warning: Transaction only holds 1 query, consider removing Transaction context in code." );
}
2010-09-02 17:47:50 +02:00
}
Enqueue ( new TransactionTask ( transaction ));
}
2010-08-18 02:25:52 +02:00
2010-09-02 20:54:43 +02:00
PreparedStatement * GetPreparedStatement ( uint32 index )
{
return new PreparedStatement ( index );
}
void Execute ( PreparedStatement * stmt )
{
PreparedStatementTask * task = new PreparedStatementTask ( stmt );
Enqueue ( task );
}
2010-11-21 18:50:11 +01:00
void DirectExecute ( PreparedStatement * stmt )
{
T * t = GetFreeConnection ();
t -> Execute ( stmt );
t -> Unlock ();
}
2010-09-02 20:54:43 +02:00
2010-08-18 02:25:52 +02:00
void escape_string ( std :: string & str )
{
if ( str . empty ())
return ;
char * buf = new char [ str . size () * 2 + 1 ];
escape_string ( buf , str . c_str (), str . size ());
str = buf ;
delete [] buf ;
}
2010-09-11 21:22:15 +02:00
PreparedQueryResult Query ( PreparedStatement * stmt )
2010-09-11 09:17:14 +02:00
{
2010-09-27 00:20:56 +02:00
T * t = GetFreeConnection ();
PreparedResultSet * ret = t -> Query ( stmt );
t -> Unlock ();
2010-09-19 12:16:29 +02:00
if ( ! ret || ! ret -> GetRowCount ())
2010-09-11 21:22:15 +02:00
return PreparedQueryResult ( NULL );
2010-09-11 09:17:14 +02:00
2010-09-11 21:22:15 +02:00
return PreparedQueryResult ( ret );
2010-09-11 09:17:14 +02:00
}
2010-09-25 01:05:24 +02:00
void KeepAlive ()
{
2010-10-31 10:55:29 +01:00
/// Ping synchronous connections
2010-09-27 00:20:56 +02:00
for ( uint8 i = 0 ; i < m_connections [ IDX_SYNCH ]. size (); ++ i )
2010-10-31 10:55:29 +01:00
{
T * t = m_connections [ IDX_SYNCH ][ i ];
if ( t -> LockIfReady ())
{
t -> Ping ();
t -> Unlock ();
}
}
2010-09-25 01:05:24 +02:00
/// Assuming all worker threads are free, every worker thread will receive 1 ping operation request
2010-09-25 22:03:57 +02:00
/// If one or more worker threads are busy, the ping operations will not be split evenly, but this doesn't matter
2010-09-25 01:05:24 +02:00
/// as the sole purpose is to prevent connections from idling.
2010-09-27 00:20:56 +02:00
for ( size_t i = 0 ; i < m_connections [ IDX_ASYNC ]. size (); ++ i )
2010-09-25 01:05:24 +02:00
Enqueue ( new PingOperation );
2010-09-25 22:03:57 +02:00
}
2010-09-25 01:05:24 +02:00
2010-09-03 10:52:32 +02:00
private :
2010-08-18 02:25:52 +02:00
unsigned long escape_string ( char * to , const char * from , unsigned long length )
{
if ( ! to || ! from || ! length )
return 0 ;
2010-09-27 00:20:56 +02:00
T * t = GetFreeConnection ();
unsigned long ret = mysql_real_escape_string ( t -> GetHandle (), to , from , length );
t -> Unlock ();
return ret ;
2010-08-18 02:25:52 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-18 02:25:52 +02:00
void Enqueue ( SQLOperation * op )
{
m_queue -> enqueue ( op );
}
2010-09-27 00:20:56 +02:00
T * GetFreeConnection ()
2010-09-02 17:47:50 +02:00
{
2010-09-27 00:20:56 +02:00
uint8 i = 0 ;
size_t num_cons = m_connections [ IDX_SYNCH ]. size ();
for (;;) /// Block forever until a connection is free
2010-09-02 17:47:50 +02:00
{
2010-09-27 00:20:56 +02:00
T * t = m_connections [ IDX_SYNCH ][ ++ i % num_cons ];
if ( t -> LockIfReady ()) /// Must be matched with t->Unlock() or you will get deadlocks
return t ;
2010-09-02 17:47:50 +02:00
}
2010-09-27 00:20:56 +02:00
// This will be called when Celine Dion learns to sing
return NULL ;
2010-09-02 17:47:50 +02:00
}
2010-08-18 02:25:52 +02:00
private :
2010-09-27 00:20:56 +02:00
enum
{
IDX_ASYNC ,
IDX_SYNCH ,
IDX_SIZE ,
};
2010-08-18 02:25:52 +02:00
ACE_Activation_Queue * m_queue ; //! Queue shared by async worker threads.
2010-09-27 00:20:56 +02:00
std :: vector < std :: vector < T *> > m_connections ;
AtomicUInt m_connectionCount ; //! Counter of MySQL connections;
2010-09-29 15:29:57 +02:00
MySQLConnectionInfo m_connectionInfo ;
2010-08-18 02:25:52 +02:00
};
2010-08-18 07:17:04 +02:00
#endif