Skip to content

Commit

Permalink
added jmx enable statistics gathering
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Shorrock committed May 14, 2010
1 parent 7862422 commit 1dac75c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
47 changes: 47 additions & 0 deletions src/main/scala/com/shorrockin/cascal/jmx/CascalStatistics.scala
@@ -0,0 +1,47 @@
package com.shorrockin.cascal.jmx


import management.ManagementFactory
import javax.management.ObjectName
import com.shorrockin.cascal.session.SessionPool

/**
* object used to capture various metrics related to cascal and expose them through
* a jmx interface.
*
* @author Chris Shorrock
*/
object CascalStatistics extends CascalStatistics$MBean {
ManagementFactory.getPlatformMBeanServer.registerMBean(this, new ObjectName("com.shorrockin.cascal:name=CascalStatistics"))

private var pools = List[SessionPool]()
private var creationCount = 0L
private var checkoutCount = 0L
private var checkinCount = 0L
private var totalUsageTime = 0L

def register(pool:SessionPool) = pools = pool :: pools
def unregister(pool:SessionPool) = pools = pools - pool

def creationInc = creationCount = creationCount + 1
def checkoutInc = checkoutCount = checkoutCount + 1
def checkinInc = checkinCount = checkinCount + 1
def usageInc(duration:Long) = totalUsageTime = totalUsageTime + duration

def getNumberOfActiveConnections():Int = pools.foldLeft(0) { _ + _.active }
def getNumberOfIdleConnections():Int = pools.foldLeft(0) { _ + _.idle }
def getNumberOfSessionsCreated():Long = creationCount
def getNumberOfConnectionsCheckedOut():Long = checkoutCount
def getNumberOfConnectionsCheckedIn():Long = checkinCount
def getAverageConnectionUsageTime():Long = totalUsageTime / getNumberOfConnectionsCheckedOut
}

trait CascalStatistics$MBean {
def getNumberOfActiveConnections():Int
def getNumberOfIdleConnections():Int
def getNumberOfSessionsCreated():Long
def getNumberOfConnectionsCheckedOut():Long
def getNumberOfConnectionsCheckedIn():Long
def getAverageConnectionUsageTime():Long

}
30 changes: 24 additions & 6 deletions src/main/scala/com/shorrockin/cascal/session/SessionPool.scala
Expand Up @@ -3,6 +3,8 @@ package com.shorrockin.cascal.session
import org.apache.commons.pool.PoolableObjectFactory
import org.apache.commons.pool.impl.{GenericObjectPoolFactory, GenericObjectPool}
import com.shorrockin.cascal.utils.Logging
import com.shorrockin.cascal.jmx.CascalStatistics


/**
* a session pool which maintains a collection of open sessions so that
Expand All @@ -15,6 +17,8 @@ class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consis
def this(hosts:Seq[Host], params:PoolParams, consistency:Consistency) = this(hosts, params, consistency, false)
def this(hosts:Seq[Host], params:PoolParams) = this(hosts, params, Consistency.One, false)

CascalStatistics.register(this)

private val pool = {
val factory = new GenericObjectPoolFactory(SessionFactory,
params.maxActive,
Expand All @@ -37,7 +41,10 @@ class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consis
/**
* closes this pool and releases any resources available to it.
*/
def close() { pool.close }
def close() {
pool.close
CascalStatistics.unregister(this)
}


/**
Expand Down Expand Up @@ -67,10 +74,13 @@ class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consis
var session:Session = null

try {
session = pool.borrowObject.asInstanceOf[Session]
f(session)
session = checkout
val before = System.currentTimeMillis
val out = f(session)
CascalStatistics.usageInc(System.currentTimeMillis - before)
out
} finally {
if (null != session) pool.returnObject(session)
if (null != session) checkin(session)
}
}

Expand All @@ -80,14 +90,21 @@ class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consis
* session it must be returned to the pool. failure to do so
* will result your pool shedding a tear.
*/
def checkout:Session = pool.borrowObject.asInstanceOf[Session]
def checkout:Session = {
val out = pool.borrowObject.asInstanceOf[Session]
CascalStatistics.checkoutInc
out
}


/**
* returns the session back to the pool. only necessary when a sessio
* is retrieved through the checkout methad.
*/
def checkin(session:Session) = pool.returnObject(session)
def checkin(session:Session) = {
CascalStatistics.checkinInc
pool.returnObject(session)
}


/**
Expand All @@ -111,6 +128,7 @@ class SessionPool(val hosts:Seq[Host], val params:PoolParams, consistency:Consis
log.debug("attempting to create connection to: " + host)
val session = new Session(host.address, host.port, host.timeout, consistency, framedTransport)
session.open
CascalStatistics.creationInc
session
} catch {
case e:Exception =>
Expand Down

0 comments on commit 1dac75c

Please sign in to comment.