aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zma...@apache.org
Subject aurora git commit: Expose MyBatis PoolState via stats.
Date Wed, 03 Feb 2016 22:28:19 GMT
Repository: aurora
Updated Branches:
  refs/heads/master 2d91e18fb -> 52c19cf05


Expose MyBatis PoolState via stats.

To better understand the MyBatis connection pool this patch exposes the pool
state via stats.

Reviewed at https://reviews.apache.org/r/43150/


Project: http://git-wip-us.apache.org/repos/asf/aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/aurora/commit/52c19cf0
Tree: http://git-wip-us.apache.org/repos/asf/aurora/tree/52c19cf0
Diff: http://git-wip-us.apache.org/repos/asf/aurora/diff/52c19cf0

Branch: refs/heads/master
Commit: 52c19cf05d4dbd2284ce4a36b2e8d02602935447
Parents: 2d91e18
Author: Zameer Manji <zmanji@apache.org>
Authored: Wed Feb 3 14:28:12 2016 -0800
Committer: Zameer Manji <zmanji@apache.org>
Committed: Wed Feb 3 14:28:12 2016 -0800

----------------------------------------------------------------------
 .../aurora/scheduler/storage/db/DbStorage.java  | 35 +++++++++++++++++++-
 .../scheduler/storage/db/DbStorageTest.java     |  5 ++-
 2 files changed, 38 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aurora/blob/52c19cf0/src/main/java/org/apache/aurora/scheduler/storage/db/DbStorage.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/storage/db/DbStorage.java b/src/main/java/org/apache/aurora/scheduler/storage/db/DbStorage.java
index 7674b8a..c0f8d35 100644
--- a/src/main/java/org/apache/aurora/scheduler/storage/db/DbStorage.java
+++ b/src/main/java/org/apache/aurora/scheduler/storage/db/DbStorage.java
@@ -17,12 +17,16 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.nio.charset.StandardCharsets;
 
+import javax.sql.DataSource;
+
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Supplier;
 import com.google.common.io.CharStreams;
 import com.google.common.util.concurrent.AbstractIdleService;
 import com.google.inject.Inject;
 
 import org.apache.aurora.common.inject.TimedInterceptor.Timed;
+import org.apache.aurora.common.stats.StatsProvider;
 import org.apache.aurora.gen.CronCollisionPolicy;
 import org.apache.aurora.gen.JobUpdateAction;
 import org.apache.aurora.gen.JobUpdateStatus;
@@ -40,6 +44,8 @@ import org.apache.aurora.scheduler.storage.SchedulerStore;
 import org.apache.aurora.scheduler.storage.Storage;
 import org.apache.aurora.scheduler.storage.TaskStore;
 import org.apache.ibatis.builder.StaticSqlSource;
+import org.apache.ibatis.datasource.pooled.PoolState;
+import org.apache.ibatis.datasource.pooled.PooledDataSource;
 import org.apache.ibatis.exceptions.PersistenceException;
 import org.apache.ibatis.mapping.MappedStatement.Builder;
 import org.apache.ibatis.session.Configuration;
@@ -63,6 +69,7 @@ class DbStorage extends AbstractIdleService implements Storage {
   private final MutableStoreProvider storeProvider;
   private final EnumValueMapper enumValueMapper;
   private final GatedWorkQueue gatedWorkQueue;
+  private final StatsProvider statsProvider;
 
   @Inject
   DbStorage(
@@ -75,7 +82,8 @@ class DbStorage extends AbstractIdleService implements Storage {
       final AttributeStore.Mutable attributeStore,
       final LockStore.Mutable lockStore,
       final QuotaStore.Mutable quotaStore,
-      final JobUpdateStore.Mutable jobUpdateStore) {
+      final JobUpdateStore.Mutable jobUpdateStore,
+      StatsProvider statsProvider) {
 
     this.sessionFactory = requireNonNull(sessionFactory);
     this.enumValueMapper = requireNonNull(enumValueMapper);
@@ -128,6 +136,7 @@ class DbStorage extends AbstractIdleService implements Storage {
         return jobUpdateStore;
       }
     };
+    this.statsProvider = requireNonNull(statsProvider);
   }
 
   @Timed("db_storage_read_operation")
@@ -250,10 +259,34 @@ class DbStorage extends AbstractIdleService implements Storage {
     for (ScheduleStatus status : ScheduleStatus.values()) {
       enumValueMapper.addEnumValue("task_states", status.getValue(), status.name());
     }
+
+    createPoolMetrics();
   }
 
   @Override
   protected void shutDown() {
     // noop
   }
+
+  private void createPoolMetrics() {
+    DataSource dataSource = sessionFactory.getConfiguration().getEnvironment().getDataSource();
+    // Should not fail because we specify a PoolDataSource in DbModule
+    PoolState poolState = ((PooledDataSource) dataSource).getPoolState();
+
+    createPoolGauge("requests", poolState::getRequestCount);
+    createPoolGauge("average_request_time_ms", poolState::getAverageRequestTime);
+    createPoolGauge("average_wait_time_ms", poolState::getAverageWaitTime);
+    createPoolGauge("connections_had_to_wait", poolState::getHadToWaitCount);
+    createPoolGauge("bad_connections", poolState::getBadConnectionCount);
+    createPoolGauge("claimed_overdue_connections", poolState::getClaimedOverdueConnectionCount);
+    createPoolGauge("average_overdue_checkout_time_ms", poolState::getAverageOverdueCheckoutTime);
+    createPoolGauge("average_checkout_time_ms", poolState::getAverageCheckoutTime);
+    createPoolGauge("idle_connections", poolState::getIdleConnectionCount);
+    createPoolGauge("active_connections", poolState::getActiveConnectionCount);
+  }
+
+  private void createPoolGauge(String name, Supplier<? extends Number> gauge) {
+    String prefix = "db_storage_mybatis_connection_pool_";
+    statsProvider.makeGauge(prefix + name, gauge);
+  }
 }

http://git-wip-us.apache.org/repos/asf/aurora/blob/52c19cf0/src/test/java/org/apache/aurora/scheduler/storage/db/DbStorageTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/storage/db/DbStorageTest.java b/src/test/java/org/apache/aurora/scheduler/storage/db/DbStorageTest.java
index 3ab95c6..420d444 100644
--- a/src/test/java/org/apache/aurora/scheduler/storage/db/DbStorageTest.java
+++ b/src/test/java/org/apache/aurora/scheduler/storage/db/DbStorageTest.java
@@ -13,6 +13,7 @@
  */
 package org.apache.aurora.scheduler.storage.db;
 
+import org.apache.aurora.common.stats.StatsProvider;
 import org.apache.aurora.common.testing.easymock.EasyMockTest;
 import org.apache.aurora.scheduler.async.GatedWorkQueue;
 import org.apache.aurora.scheduler.async.GatedWorkQueue.GatedOperation;
@@ -55,6 +56,7 @@ public class DbStorageTest extends EasyMockTest {
     gatedWorkQueue = createMock(GatedWorkQueue.class);
     readWork = createMock(new Clazz<Work.Quiet<String>>() { });
     writeWork = createMock(new Clazz<MutateWork.NoResult.Quiet>() { });
+    StatsProvider statsProvider = createMock(StatsProvider.class);
 
     storage = new DbStorage(
         sessionFactory,
@@ -66,7 +68,8 @@ public class DbStorageTest extends EasyMockTest {
         createMock(AttributeStore.Mutable.class),
         createMock(LockStore.Mutable.class),
         createMock(QuotaStore.Mutable.class),
-        createMock(JobUpdateStore.Mutable.class));
+        createMock(JobUpdateStore.Mutable.class),
+        statsProvider);
   }
 
   @Test(expected = StorageException.class)


Mime
View raw message