Where is the missing HTTP request when the CountDownLatch is getting zero count?

145 Views Asked by At

I am testing a multithread code that sends a set of http requests using a CloseableHttpAsyncClient client (see a snip of the code bellow).

I am obtaining the following output:

 Failed ->java.io.IOException: Connection reset by peer-null
 Failed ->org.apache.http.ConnectionClosedException: Connection closed-null
 Thread: 0-Time: 2955ms-Completed: 1000-Failed: 0-Cancelled: 0- Countdown: 0
 Thread: 1-Time: 2855ms-Completed: 999-Failed: 0-Cancelled: 0-Countdown: 0
 Thread: 2-Time: 2741ms-Completed: 999-Failed: 1-Cancelled: 0-Countdown: 0
 Thread: 3-Time: 2678ms-Completed: 999-Failed: 1-Cancelled: 0-Countdown: 0
 Thread: 4-Time: 2654ms-Completed: 1000-Failed: 0-Cancelled: 0-Countdown: 0

So, two of the threads executed all 1000 requests correctly, others two threads had connection errors which were correctly captured, and one of the thread (number 1) completed 999 requests, and did't has any notification of the failure or cancellation.

My questions are:

  1. is there any way in the failed method to understand which was the failed request, so I can make a post processing to resend those failed requests?

  2. why if there were no fails, cancellations or exception raised the number of countdown reach 0 when not all the requests were completed?

    class AsynchThread extends Thread{      
        CloseableHttpAsyncClient httpclient;
        int n;
        int ncompleted =0;
        int nfailed =0;
        int ncancelled =0;
        long time;
        CountDownLatch latch;
    
        public AsynchThread(CloseableHttpAsyncClient httpclient, int n) throws IOReactorException {
            this.jobs = jobs;
            this.httpclient = httpclient;
            this.n = n;
        }
    
        public void process() throws InterruptedException, IOException {
            latch = new CountDownLatch(n);
            long starttime = System.currentTimeMillis();
            for (int v=0;v<n; v++) {
                    HttpPost httppost = ...
                    httpclient.execute(httppost, new FutureCallback<HttpResponse>() {
    
                   public void completed(final HttpResponse response) {
                        latch.countDown();
                        ncompleted += 1;
                    }
    
                    public void failed(final Exception ex) {
                         latch.countDown();
                         nfailed += 1;
                         System.out.println("Failed ->" + ex);
                    }
    
                    public void cancelled() {
                        latch.countDown();
                        ncancelled += 1;
                        System.out.println("Cancelled ->" + ex);
                   }
              });
            }
            latch.await();
            time = System.currentTimeMillis()-starttime;
        }
    
        public void run() {
            try {
                process();
            }catch(Exception e) {
                System.out.println(e.getStackTrace());
            }
        }
    }
    
    public static void main(final String[] args) throws Exception {
        CloseableHttpAsyncClient httpclient = ...
        int n = 5;
        int nprocthread = 1000;
        AsynchThread[] threads = new AsynchThread[n];
        for (int i=0; i<n; i++) {
            threads[i] = acall.createThread(httpclient, nprocthread);
            threads[i].run();
        }
        for(int i = 0; i < threads.length; i++)
             threads[i].join();
        for(int i = 0; i < threads.length; i++) {
            System.out.println("Thread: " + i + "-Time: " + threads[i].time + "ms-Completed: " + 
                            threads[i].ncompleted + "-Failed: " + threads[i].nfailed + "-Cancelled: " + 
                            threads[i].ncancelled + "-Countdown: " + threads[i].latch.getCount());
        }
    }
    

Many thanks in advance.

1

There are 1 best solutions below

0
Roxana On

All the problems were solved when added the header "Connection: close"