NamedConnectionPool.java 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *
 * Copyright 2013 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.netflix.http4;

import java.util.concurrent.TimeUnit;

import org.apache.http.conn.ClientConnectionOperator;
23
import org.apache.http.conn.ConnectionPoolTimeoutException;
24 25 26 27 28 29
import org.apache.http.conn.params.ConnPerRoute;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.conn.tsccm.BasicPoolEntry;
import org.apache.http.impl.conn.tsccm.ConnPoolByRoute;
import org.apache.http.impl.conn.tsccm.PoolEntryRequest;
import org.apache.http.impl.conn.tsccm.RouteSpecificPool;
30
import org.apache.http.impl.conn.tsccm.WaitingThreadAborter;
31 32 33
import org.apache.http.params.HttpParams;

import com.google.common.base.Preconditions;
34 35
import com.netflix.servo.annotations.DataSourceType;
import com.netflix.servo.annotations.Monitor;
36 37
import com.netflix.servo.monitor.Counter;
import com.netflix.servo.monitor.Monitors;
38 39
import com.netflix.servo.monitor.Stopwatch;
import com.netflix.servo.monitor.Timer;
40 41 42 43 44 45 46 47 48 49 50 51 52 53

/**
 * A connection pool that provides Servo counters to monitor the efficiency. 
 * Three counters are provided: counter for getting free entries (or reusing entries),
 * counter for creating new entries, and counter for every connection request.
 * 
 * @author awang
 *
 */
public class NamedConnectionPool extends ConnPoolByRoute {

    private Counter freeEntryCounter;
    private Counter createEntryCounter;
    private Counter requestCounter;
54 55
    private Counter releaseCounter;
    private Counter deleteCounter;
56
    private Timer requestTimer;
57
    private Timer creationTimer;
A
Allen Wang 已提交
58
    private String name;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    
    public NamedConnectionPool(String name, ClientConnectionOperator operator,
            ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL,
            TimeUnit connTTLTimeUnit) {
        super(operator, connPerRoute, maxTotalConnections, connTTL, connTTLTimeUnit);
        initMonitors(name);
    }

    public NamedConnectionPool(String name, ClientConnectionOperator operator,
            ConnPerRoute connPerRoute, int maxTotalConnections) {
        super(operator, connPerRoute, maxTotalConnections);
        initMonitors(name);
    }
    
    public NamedConnectionPool(String name, ClientConnectionOperator operator,
            HttpParams params) {
        super(operator, params);
        initMonitors(name);
    }
    
    NamedConnectionPool(ClientConnectionOperator operator,
            ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL,
            TimeUnit connTTLTimeUnit) {
        super(operator, connPerRoute, maxTotalConnections, connTTL, connTTLTimeUnit);
    }

    NamedConnectionPool(ClientConnectionOperator operator,
            ConnPerRoute connPerRoute, int maxTotalConnections) {
        super(operator, connPerRoute, maxTotalConnections);
    }
    
    NamedConnectionPool(ClientConnectionOperator operator,
            HttpParams params) {
        super(operator, params);
    }
    
    void initMonitors(String name) {
        Preconditions.checkNotNull(name);
97 98 99 100 101
        freeEntryCounter = Monitors.newCounter(name + "_Reuse");
        createEntryCounter = Monitors.newCounter(name + "_CreateNew");
        requestCounter = Monitors.newCounter(name + "_Request");
        releaseCounter = Monitors.newCounter(name + "_Release");
        deleteCounter = Monitors.newCounter(name + "_Delete");
102 103
        requestTimer = Monitors.newTimer(name + "_RequestConnectionTimer", TimeUnit.MILLISECONDS);
        creationTimer = Monitors.newTimer(name + "_CreateConnectionTimer", TimeUnit.MILLISECONDS);
A
Allen Wang 已提交
104
        this.name = name;
105
        Monitors.registerObject(name, this);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }

    @Override
    public PoolEntryRequest requestPoolEntry(HttpRoute route, Object state) {
        requestCounter.increment();
        return super.requestPoolEntry(route, state);
    }

    @Override
    protected BasicPoolEntry getFreeEntry(RouteSpecificPool rospl, Object state) {
        BasicPoolEntry entry = super.getFreeEntry(rospl, state);
        if (entry != null) {
            freeEntryCounter.increment();
        }
        return entry;
    }

    @Override
    protected BasicPoolEntry createEntry(RouteSpecificPool rospl,
            ClientConnectionOperator op) {
A
Allen Wang 已提交
126
        createEntryCounter.increment();
127 128 129 130 131 132
        Stopwatch stopWatch = creationTimer.start();
        try {
            return super.createEntry(rospl, op);
        } finally {
            stopWatch.stop();
        }
133 134
    }
    
135 136 137 138
    @Override
    protected BasicPoolEntry getEntryBlocking(HttpRoute route, Object state,
            long timeout, TimeUnit tunit, WaitingThreadAborter aborter)
            throws ConnectionPoolTimeoutException, InterruptedException {
139
        Stopwatch stopWatch = requestTimer.start();
140 141 142
        try {
            return super.getEntryBlocking(route, state, timeout, tunit, aborter);
        } finally {
143
            stopWatch.stop();
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        }
    }

    @Override
    public void freeEntry(BasicPoolEntry entry, boolean reusable,
            long validDuration, TimeUnit timeUnit) {
        releaseCounter.increment();
        super.freeEntry(entry, reusable, validDuration, timeUnit);
    }

    @Override
    protected void deleteEntry(BasicPoolEntry entry) {
        deleteCounter.increment();
        super.deleteEntry(entry);
    }

160
    public final long getFreeEntryCount() {
161
        return freeEntryCounter.getValue().longValue();
162 163 164
    }
    
    public final long getCreatedEntryCount() {
165
        return createEntryCounter.getValue().longValue();
166 167 168
    }
    
    public final long getRequestsCount() {
169
        return requestCounter.getValue().longValue();
170 171 172
    }   
    
    public final long getReleaseCount() {
173
        return releaseCounter.getValue().longValue();
174 175 176
    }
    
    public final long getDeleteCount() {
177
        return deleteCounter.getValue().longValue();
178
    }
179 180 181 182 183 184
    
    @Monitor(name="connectionCount", type=DataSourceType.GAUGE)
    public int getConnectionCount() {
        return this.getConnectionsInPool();
    }
    
A
Allen Wang 已提交
185 186 187 188 189
    @Override
    public void shutdown() {
        super.shutdown();
        Monitors.unregisterObject(name, this);
    }
190
}