/***************************************************************************
                Database Connection Pool
                -----------------------------------------
	begin      : 2012/05/13
	copyright  : (C) 2011 SolutionBox Inc.
	author     : Service 1 Team
	email      : svc1@solbox.com
	version    : 3.1.0

    CopyRight(C) 2011 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/
#include "rc_cmove.h"
#include "dbconnpool.h"
#include "Database.h"
#include "Logger.h"


#define  GET_SLEEP 1000 // microsecond

masterdbpool *masterdbpool::m_inst = NULL;
slavedbpool *slavedbpool::m_inst = NULL;

conndbpool::conndbpool(databaseinfo info)
: m_dbinfo(info), m_exitpool(false), m_lastset(NULL), m_alivetime(-1), m_tabletype(-1)
{
	pthread_mutex_init(&m_mutex, NULL);
	pthread_mutex_init(&m_alivemutex, NULL);
	pthread_cond_init(&m_alivecond, NULL);
}

conndbpool::~conndbpool()
{
	pthread_mutex_destroy(&m_mutex);
	pthread_mutex_destroy(&m_alivemutex);
	pthread_cond_destroy(&m_alivecond);
}

int conndbpool::CreatePool( int poolcnt /* = 4 */ )
{
	// keep alive thread
	int nRet = pthread_create(&m_keepalive, 0, conndbpool::KeepPoolAlive, this);
	if( nRet )
	{
		cerr << "Thread create failed.: errno: " << errno << endl;
		return -1;
	}

	// pool
	for(int i=0 ; i < poolcnt ; i++)
	{
		DataBase * tmp = new DataBase();

		if( tmp->PgOpenDB( m_dbinfo.m_hostaddr, m_dbinfo.m_port, m_dbinfo.m_dbname, m_dbinfo.m_user, m_dbinfo.m_pw ) == NULL )
		{
#ifdef _DEBUG
			cerr << "Database connect error : " << tmp->GetErrorMessage() << endl;
#endif // _DEBUG
			delete tmp;
			continue;
		}

		// 2014.06.06 dadamin
		// Pool 생성 시 현재 생성된 DB의 형상을 체크한다.
		if (m_tabletype < 0)
		{
			if (SetTargetTableType(tmp) == false)
			{
				delete tmp;
				break;
			}				
		}

		m_poolmap.insert(make_pair(tmp, conndbpool::POOL_FREE));
	}

	if (m_poolmap.size() == 0)
	{
		// alive thread end
		m_exitpool = true;
		pthread_cond_signal(&m_alivecond);
		pthread_join(m_keepalive, NULL);
		m_exitpool = false;
		return -1;
	}
		

	return m_poolmap.size();
}

int conndbpool::DestroyPool()
{
	m_exitpool = true;
	map<DataBase*, short>::iterator iter;
	pthread_mutex_lock(&m_mutex);
	while(m_poolmap.size() > 0 )
	{
		iter =  m_poolmap.begin();
		if(iter->second == conndbpool::POOL_FREE || iter->second == conndbpool::POOL_ERR)
		{
			DataBase *data = static_cast<DataBase *>(iter->first);
			delete (DataBase *) data;
			m_poolmap.erase(iter);
		}
		else
			sleep(1);
	}
	pthread_mutex_unlock(&m_mutex);

	//
	pthread_cond_signal(&m_alivecond);
	pthread_join(m_keepalive, NULL);

	/*
	for( iter = m_poolmap.begin(); !m_poolmap.empty()&& iter != m_poolmap.end(); iter++ )
	{
		if(iter->second == conndbpool::POOL_FREE)
		{
			DataBase *data = static_cast<DataBase *>(iter->first);
			delete (DataBase *) data;
			m_poolmap.f
			m_poolmap.erase(iter);
		}
	}
	*/

	return m_poolmap.size();
}

DataBase * conndbpool::GetConnFromPool(int timeout)
{
	ostringstream msg;
	DataBase * r = NULL;

	int64_t usetime = 0;
	int64_t out = timeout*1000*1000;

	time_t t = time(NULL);
	pthread_mutex_lock(&m_mutex);
	map<DataBase*, short>::iterator iter = m_poolmap.begin();
	if(m_lastset)
	{
		iter = m_poolmap.find(m_lastset);
		if( iter != m_poolmap.end() )
		{
			++iter;
		}
	}

	do
	{		

		if( m_poolmap.empty() )
		{
			msg << "DB Pool empty.";
			LOGACERR(LERR, msg);
			break;
		}

		if( iter == m_poolmap.end() )
		{
			iter = m_poolmap.begin();
		}

		for( ; !m_poolmap.empty()&& iter != m_poolmap.end(); )
		{
			if(iter->second == conndbpool::POOL_FREE)
			{
				iter->second = conndbpool::POOL_USE;
				r = iter->first;
				m_lastset = r;
				break;
			}
			else if (iter->second == conndbpool::POOL_ERR)
			{
				DataBase * d = iter->first;
				m_poolmap.erase(iter++);
				if(d == m_lastset)
					m_lastset = NULL;
				delete d;				
			}
			else 
				iter++;
		}
		
		if( r == NULL)
		{
			solusleep(GET_SLEEP);
			if( usetime > 10*1000*1000)
			{
				msg << "GetConnFromPool have waited longer than 10 seconds.";
				LOGACOUT(LDBG, msg);
			}

			if( out > 0 )
			{			
				usetime += GET_SLEEP;

				if(usetime > out)
				{				
					msg << "GetConnFromPool Timeout." << usetime << "," << out << 
						"," << time(NULL) -t;
					LOGACERR(LERR, msg);
					break;
				}
			}
		}	

	} while (r == NULL);
	pthread_mutex_unlock(&m_mutex);

	return r;
}

void conndbpool::ReleaseConnToPool( DataBase * t, bool success )
{
	//pthread_mutex_lock(&m_mutex);

	map<DataBase*, short>::iterator iter = m_poolmap.find(t);
	if( iter != m_poolmap.end() )
	{
		if(iter->second==conndbpool::POOL_USE)
		{
			if (success)
			{
				iter->second=conndbpool::POOL_FREE;
			}
			else 
			{
				ostringstream msg;
				iter->second=conndbpool::POOL_ERR;
				msg << "DB Pool used failed.";
				LOGACERR(LERR, msg);
			}
		}
	}

	//pthread_mutex_unlock(&m_mutex);
}

bool conndbpool::IsTimeoutAlive()
{
	bool r = false;
	pthread_mutex_lock(&m_alivemutex);
	struct timespec to;

	if(m_alivetime > 0 )
		to.tv_sec = time(NULL) + m_alivetime;
	else
		to.tv_sec = time(NULL) + 5;

	to.tv_nsec = 0;

	int err = pthread_cond_timedwait(&m_alivecond, &m_alivemutex, &to);

	if (err == ETIMEDOUT)
	{
		if( m_alivetime > 0 )
			r = true;
	}
	else if ( err == 0 )
	{
#ifdef _DEBUG
		cout << "Set Keep alive Timeout : " << m_alivetime << endl;
#endif //_DEBUG
		r = false;
	}
	else
	{
		/* nothing */
	}

	pthread_mutex_unlock(&m_alivemutex);

	return r;
}

bool conndbpool::SendAliveMsg()
{
	ostringstream msg;
	ostringstream sql;
	
	pthread_mutex_lock(&m_mutex);
	map<DataBase*, short>::iterator iter = m_poolmap.begin();
	map<DataBase*, short>::iterator enditer = m_poolmap.end();

	sql << "SELECT sp_user_seq, sp_svc_tran_id FROM t_sms_sp_svc_product LIMIT 1";
	while(iter != enditer)
	{
		DataBase * d = iter->first;
		if(iter->second == conndbpool::POOL_FREE)
		{
			iter->second = conndbpool::POOL_USE;
			d->PgDoExec( const_cast<char*> (sql.str().c_str()) );
			if( d->PgResult(DataBase::CLEAR) < 0 )
			{				
				msg << "Send alive : message failed : " << iter->first->GetErrorMessage();
				LOGACERR(LERR, msg);
				iter->second=conndbpool::POOL_ERR;
				iter++;
				//m_poolmap.erase(iter++);
				//delete d;
			}
			else
			{
				iter->second = conndbpool::POOL_FREE;
				msg << "Send alive : message success.";
				_LOGACOUT(LDBG, msg);
				++iter;
			}
		}
		else if (iter->second == conndbpool::POOL_ERR)
		{
			msg << "Send alive : Error Pool remove.";
			_LOGACOUT(LDBG, msg);
			m_poolmap.erase(iter++);
			delete d;
		}
		else 
			++iter;
		
	}
	pthread_mutex_unlock(&m_mutex);
	return true;
}

void* conndbpool::KeepPoolAlive( void* pdata )
{
	conndbpool* pObject = reinterpret_cast<conndbpool *>(pdata);
	ostringstream msg;

	while( pObject->IsExit() == false )
	{
		bool b = pObject->IsTimeoutAlive();
		msg << "KeepPoolAlive Timeout - run : " << b;
		_LOGACOUT(LDEV2, msg);
		if( pObject->IsExit() == false && b )
		{
			// send keep alive message
			pObject->SendAliveMsg();
		}
	}
	return 0;
}

int	conndbpool::SetKeepAliveTimeout(int sec)
{
	m_alivetime = sec;
	pthread_cond_signal(&m_alivecond);
	return m_alivetime;
}

int conndbpool::GetFreePoolCnt()
{
	int r = 0;
	map<DataBase*, short>::iterator iter;
//	pthread_mutex_lock(&m_mutex);
	for( iter = m_poolmap.begin(); !m_poolmap.empty()&& iter != m_poolmap.end(); iter++ )
	{
		if(iter->second == conndbpool::POOL_FREE)
		{
			r++;
		}
	}
//	pthread_mutex_unlock(&m_mutex);
	return r;
}

// 2014.06.06 dadamin
// 현재 DB 형상 체크
bool conndbpool::SetTargetTableType(DataBase * d)
{
	ostringstream msg;

	if (d == NULL)
		return false;

	d->PgDoExec("SELECT *  FROM pg_tables WHERE schemaname =  'public' AND tablename =  't_dav_resource'");
	if (d->PgResult(DataBase::NOT_CLEAR) < 0)
	{
		d->PgClear();
		msg << "SetTargetTableType failed : " << d->GetErrorMessage();
		LOGACERR(LERR, msg);

		return false;
	}

	if (d->GetNoTuples() > 0)
	{
		m_tabletype = 0;
	}
	else
	{
		m_tabletype = 1;
	}
	
	d->PgClear();
	msg << "Set Target Table Type : " << m_tabletype;

	LOGACOUT(LDBG, msg);

	return true;
}

// 작업할 메타 테이블명
string conndbpool::GetMetaTableName(const char * tranid)
{
	string r; 

	switch (m_tabletype)
	{
	case 1:
		r = "t_meta_";
		r += tranid;
		break;
	case 0:
	default:
		r = "t_dav_resource";
		break;
	}

	LOG(LDBG, "Working Table Name %s", r.c_str());

	return r;
}

void conndbpool::printstatus(string prefixed)
{	
	int f = 0, u = 0;
	map<DataBase*, short>::iterator iter;
	
	if(m_exitpool == true ) return;
	//	pthread_mutex_lock(&m_mutex);
	for( iter = m_poolmap.begin(); !m_poolmap.empty()&& iter != m_poolmap.end(); iter++ )
	{
		if(iter->second == conndbpool::POOL_FREE)
		{
			f++;
		}
		else 
		{
			u++;
		}
	}

	ostringstream msg;
	if( prefixed.empty() == false )
		msg << "["<< prefixed <<"]";

	msg <<"DB connection Pool - " <<"total : " << m_poolmap.size() << "(" << u << 
		"/" << f << ")";
	
	_LOGACOUT(LINF, msg);
}

// master db pool
masterdbpool::masterdbpool(databaseinfo info)
	: conndbpool(info)
{
}

masterdbpool::~masterdbpool()
{
}

void masterdbpool::init(databaseinfo info)
{
	if( masterdbpool::m_inst == NULL )
	{
		masterdbpool::m_inst = new masterdbpool(info);
	}
}

masterdbpool* masterdbpool::getInstance()
{
	return masterdbpool::m_inst;
}

int masterdbpool::release()
{
	int r = -1;
	if( masterdbpool::m_inst != NULL )
	{
		r = masterdbpool::m_inst->DestroyPool();
		delete masterdbpool::m_inst;
		masterdbpool::m_inst = NULL;
	}

	return r;
}

// slave db pool
slavedbpool::slavedbpool(databaseinfo info)
	: conndbpool(info)
{
}

slavedbpool::~slavedbpool()
{
}

void slavedbpool::init(databaseinfo info)
{
	if( slavedbpool::m_inst == NULL )
	{
		slavedbpool::m_inst = new slavedbpool(info);
	}
}

slavedbpool* slavedbpool::getInstance()
{
	return slavedbpool::m_inst;
}

int slavedbpool::release()
{
	int r = -1;
	if(slavedbpool::m_inst != NULL )
	{
		r = slavedbpool::m_inst->DestroyPool();
		delete slavedbpool::m_inst;
		slavedbpool::m_inst = NULL;
	}

	return r;
}
