001/*
002 * (C) Copyright 2020 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.management.jtajca.internal;
020
021import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
022import org.nuxeo.ecm.core.management.jtajca.ConnectionPoolMonitor;
023import org.nuxeo.ecm.core.management.jtajca.internal.DefaultMonitorComponent.ServerInstance;
024import org.nuxeo.runtime.metrics.MetricsService;
025
026import io.dropwizard.metrics5.MetricName;
027import io.dropwizard.metrics5.MetricRegistry;
028import io.dropwizard.metrics5.SharedMetricRegistries;
029import io.dropwizard.metrics5.jvm.JmxAttributeGauge;
030
031/**
032 * Connection pool monitor for an Apache Commons Pool.
033 */
034public class ObjectPoolMonitor implements ConnectionPoolMonitor {
035
036    protected static final MetricRegistry METRICS = SharedMetricRegistries.getOrCreate(MetricsService.class.getName());
037
038    protected final String name;
039
040    protected final GenericKeyedObjectPool<String, ?> pool;
041
042    protected final String key;
043
044    protected MetricName countGauge;
045
046    protected MetricName idleGauge;
047
048    protected MetricName killedGauge;
049
050    protected ObjectPoolMonitor(String name, GenericKeyedObjectPool<String, ?> pool, String key) {
051        this.name = name;
052        this.pool = pool;
053        this.key = key;
054    }
055
056    protected ServerInstance self;
057
058    @Override
059    public void install() {
060        self = DefaultMonitorComponent.bind(this, name);
061        countGauge = MetricName.build("nuxeo", "repositories", "repository", "connection", "count")
062                               .tagged("repository", name);
063        idleGauge = MetricName.build("nuxeo", "repositories", "repository", "connection", "idle")
064                              .tagged("repository", name);
065        killedGauge = MetricName.build("nuxeo", "repositories", "repository", "connection", "killed")
066                                .tagged("repository", name);
067        METRICS.register(countGauge, new JmxAttributeGauge(self.name, "ConnectionCount"));
068        METRICS.register(idleGauge, new JmxAttributeGauge(self.name, "IdleConnectionCount"));
069        METRICS.register(killedGauge, new JmxAttributeGauge(self.name, "KilledActiveConnectionCount"));
070    }
071
072    @Override
073    public void uninstall() {
074        DefaultMonitorComponent.unbind(self);
075        METRICS.remove(countGauge);
076        METRICS.remove(idleGauge);
077        METRICS.remove(killedGauge);
078        self = null;
079    }
080
081    @Override
082    public String getName() {
083        return name;
084    }
085
086    @Override
087    public int getConnectionCount() {
088        return pool.getNumActive(key) + pool.getNumIdle(key);
089    }
090
091    @Override
092    public int getIdleConnectionCount() {
093        return pool.getNumIdle(key);
094    }
095
096    @Override
097    public int getBlockingTimeoutMilliseconds() {
098        return (int) pool.getMaxWaitMillis();
099    }
100
101    @Override
102    public int getIdleTimeoutMinutes() {
103        return -1;
104    }
105
106    @Override
107    public int getActiveTimeoutMinutes() {
108        return -1;
109    }
110
111    @Override
112    public void reset() {
113        pool.clear();
114    }
115
116    @Override
117    public long getKilledActiveConnectionCount() {
118        return 0;
119    }
120
121    @Override
122    public int killActiveTimedoutConnections() {
123        return 0;
124    }
125
126}