Search in sources :

Example 1 with HttpClientConnection

use of org.apache.http.HttpClientConnection in project hadoop-pcap by RIPE-NCC.

the class HttpPcapReader method processPacketPayload.

@Override
protected void processPacketPayload(Packet packet, final byte[] payload) {
    String protocol = (String) packet.get(Packet.PROTOCOL);
    if (!PcapReader.PROTOCOL_TCP.equals(protocol))
        return;
    HttpPacket httpPacket = (HttpPacket) packet;
    Integer srcPort = (Integer) packet.get(Packet.SRC_PORT);
    Integer dstPort = (Integer) packet.get(Packet.DST_PORT);
    if ((HTTP_PORT == srcPort || HTTP_PORT == dstPort) && packet.containsKey(Packet.REASSEMBLED_TCP_FRAGMENTS)) {
        final SessionInputBuffer inBuf = new AbstractSessionInputBuffer() {

            {
                init(new ByteArrayInputStream(payload), 1024, params);
            }

            @Override
            public boolean isDataAvailable(int timeout) throws IOException {
                return true;
            }
        };
        final SessionOutputBuffer outBuf = new AbstractSessionOutputBuffer() {
        };
        if (HTTP_PORT == srcPort) {
            HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inBuf, null, respFactory, params);
            HttpClientConnection conn = new DefaultClientConnection() {

                {
                    init(inBuf, outBuf, params);
                }

                @Override
                protected void assertNotOpen() {
                }

                @Override
                protected void assertOpen() {
                }
            };
            try {
                HttpResponse response = parser.parse();
                conn.receiveResponseEntity(response);
                propagateHeaders(httpPacket, response.getAllHeaders());
            } catch (IOException e) {
                LOG.error("IOException when decoding HTTP response", e);
            } catch (HttpException e) {
                LOG.error("HttpException when decoding HTTP response", e);
            }
        } else if (HTTP_PORT == dstPort) {
            HttpMessageParser<HttpRequest> parser = new DefaultHttpRequestParser(inBuf, null, reqFactory, params);
            try {
                HttpRequest request = parser.parse();
                propagateHeaders(httpPacket, request.getAllHeaders());
            } catch (IOException e) {
                LOG.error("IOException when decoding HTTP request", e);
            } catch (HttpException e) {
                LOG.error("HttpException when decoding HTTP request", e);
            }
        }
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) SessionInputBuffer(org.apache.http.io.SessionInputBuffer) AbstractSessionInputBuffer(org.apache.http.impl.io.AbstractSessionInputBuffer) DefaultClientConnection(org.apache.http.impl.conn.DefaultClientConnection) HttpPacket(net.ripe.hadoop.pcap.packet.HttpPacket) HttpClientConnection(org.apache.http.HttpClientConnection) HttpMessageParser(org.apache.http.io.HttpMessageParser) HttpResponse(org.apache.http.HttpResponse) AbstractSessionInputBuffer(org.apache.http.impl.io.AbstractSessionInputBuffer) IOException(java.io.IOException) SessionOutputBuffer(org.apache.http.io.SessionOutputBuffer) AbstractSessionOutputBuffer(org.apache.http.impl.io.AbstractSessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpResponseParser(org.apache.http.impl.io.DefaultHttpResponseParser) AbstractSessionOutputBuffer(org.apache.http.impl.io.AbstractSessionOutputBuffer) HttpException(org.apache.http.HttpException) DefaultHttpRequestParser(org.apache.http.impl.io.DefaultHttpRequestParser)

Example 2 with HttpClientConnection

use of org.apache.http.HttpClientConnection in project lucene-solr by apache.

the class ConnectionReuseTest method testConnectionReuse.

@Test
public void testConnectionReuse() throws Exception {
    URL url = cluster.getJettySolrRunners().get(0).getBaseUrl();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClientUtil.createClient(null, cm);
    try (SolrClient client = buildClient(httpClient, url)) {
        HttpHost target = new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http");
        HttpRoute route = new HttpRoute(target);
        ConnectionRequest mConn = getClientConnectionRequest(httpClient, route, cm);
        HttpClientConnection conn1 = getConn(mConn);
        headerRequest(target, route, conn1, cm);
        cm.releaseConnection(conn1, null, -1, TimeUnit.MILLISECONDS);
        int queueBreaks = 0;
        int cnt1 = atLeast(3);
        int cnt2 = atLeast(30);
        for (int j = 0; j < cnt1; j++) {
            boolean done = false;
            for (int i = 0; i < cnt2; i++) {
                AddUpdateCommand c = new AddUpdateCommand(null);
                c.solrDoc = sdoc("id", id.incrementAndGet());
                try {
                    client.add(c.solrDoc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (!done && i > 0 && i < cnt2 - 1 && client instanceof ConcurrentUpdateSolrClient && random().nextInt(10) > 8) {
                    queueBreaks++;
                    done = true;
                    // wait past streaming client poll time of 250ms
                    Thread.sleep(350);
                }
            }
            if (client instanceof ConcurrentUpdateSolrClient) {
                ((ConcurrentUpdateSolrClient) client).blockUntilFinished();
            }
        }
        route = new HttpRoute(new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http"));
        mConn = cm.requestConnection(route, HttpSolrClient.cacheKey);
        HttpClientConnection conn2 = getConn(mConn);
        HttpConnectionMetrics metrics = conn2.getMetrics();
        headerRequest(target, route, conn2, cm);
        cm.releaseConnection(conn2, null, -1, TimeUnit.MILLISECONDS);
        assertNotNull("No connection metrics found - is the connection getting aborted? server closing the connection? " + client.getClass().getSimpleName(), metrics);
        // we try and make sure the connection we get has handled all of the requests in this test
        if (client instanceof ConcurrentUpdateSolrClient) {
            // we can't fully control queue polling breaking up requests - allow a bit of leeway
            int exp = cnt1 + queueBreaks + 2;
            assertTrue("We expected all communication via streaming client to use one connection! expected=" + exp + " got=" + metrics.getRequestCount(), Math.max(exp, metrics.getRequestCount()) - Math.min(exp, metrics.getRequestCount()) < 3);
        } else {
            assertTrue("We expected all communication to use one connection! " + client.getClass().getSimpleName() + " " + metrics.getRequestCount(), cnt1 * cnt2 + 2 <= metrics.getRequestCount());
        }
    } finally {
        HttpClientUtil.close(httpClient);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClientConnection(org.apache.http.HttpClientConnection) URL(java.net.URL) ConnectionPoolTimeoutException(org.apache.http.conn.ConnectionPoolTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpException(org.apache.http.HttpException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpConnectionMetrics(org.apache.http.HttpConnectionMetrics) HttpRoute(org.apache.http.conn.routing.HttpRoute) ConnectionRequest(org.apache.http.conn.ConnectionRequest) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpHost(org.apache.http.HttpHost) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 HttpClientConnection (org.apache.http.HttpClientConnection)2 HttpException (org.apache.http.HttpException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URL (java.net.URL)1 ExecutionException (java.util.concurrent.ExecutionException)1 HttpPacket (net.ripe.hadoop.pcap.packet.HttpPacket)1 HttpConnectionMetrics (org.apache.http.HttpConnectionMetrics)1 HttpHost (org.apache.http.HttpHost)1 HttpRequest (org.apache.http.HttpRequest)1 HttpResponse (org.apache.http.HttpResponse)1 ConnectionPoolTimeoutException (org.apache.http.conn.ConnectionPoolTimeoutException)1 ConnectionRequest (org.apache.http.conn.ConnectionRequest)1 HttpRoute (org.apache.http.conn.routing.HttpRoute)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 DefaultClientConnection (org.apache.http.impl.conn.DefaultClientConnection)1 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)1 AbstractSessionInputBuffer (org.apache.http.impl.io.AbstractSessionInputBuffer)1 AbstractSessionOutputBuffer (org.apache.http.impl.io.AbstractSessionOutputBuffer)1 DefaultHttpRequestParser (org.apache.http.impl.io.DefaultHttpRequestParser)1