[ 
https://issues.apache.org/jira/browse/SOLR-6734?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16505714#comment-16505714
 ] 

Shawn Heisey commented on SOLR-6734:
------------------------------------

Putting a bit of code in a comment so that I don't lose track of it.

{code:java}
package foo;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class Main {
  private static final AtomicInteger threadCounter = new AtomicInteger(0);

  public static void main(String[] args) throws InterruptedException {
    Thread gce = new GCExerciser();
    /*
     * To see the difference made by the GC exerciser, run the program once 
as-is,
     * and then again with the following start() call commented.
     */
    gce.start();
    Thread pd = new PauseDetector();
    pd.start();
  }

  /**
   * Background thread to allocate a byte array every 10 milliseconds. This 
serves
   * to create garbage, so that there will be GC pauses.
   */
  public static class GCExerciser extends Thread {
    @Override
    public void run() {
      Thread.currentThread().setName("GCExerciser" + 
threadCounter.incrementAndGet());
      final long maxHeap = Runtime.getRuntime().maxMemory();
      final long thirtyOneGig = 31L * 1024 * 1024 * 1024;
      final int size;
      if (maxHeap > thirtyOneGig) {
        size = Integer.MAX_VALUE - 1;
      } else {
        size = (int) (maxHeap / 16);
      }

      byte[] b = null;
      while (true) {
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          //
        }
        b = new byte[size];
        @SuppressWarnings("unused")
        int i = b.length;
      }
    }
  }

  public static class PauseDetector extends Thread {
    private static final AtomicLong accumulatedNanos = new AtomicLong(0);
    private static final int sleepMillis = 500;
    private static final long sleepNanos = 
TimeUnit.NANOSECONDS.convert(sleepMillis, TimeUnit.MILLISECONDS);
    private static final long fiveMinuteNanos = TimeUnit.NANOSECONDS.convert(5, 
TimeUnit.MINUTES);

    @Override
    public void run() {
      Thread.currentThread().setName("PauseDetector" + 
threadCounter.incrementAndGet());
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
      long fiveMinuteStart = System.nanoTime();
      while (true) {
        long sleepStart = System.nanoTime();
        try {
          Thread.sleep(sleepMillis);
        } catch (InterruptedException e) {
          //
        }
        long sleepStop = System.nanoTime();
        long sleepElapsed = sleepStop - sleepStart;

        /*
         * If elapsed time is more than the half second sleep, add the 
difference to the
         * accumulation counter.
         */
        if (sleepElapsed > sleepNanos) {
          accumulatedNanos.addAndGet(sleepElapsed - sleepNanos);
        }

        /*
         * If total elapsed time is five minutes or longer, output the 
accumulated time
         * and reset everything.
         */
        if ((sleepStop - fiveMinuteStart) >= fiveMinuteNanos) {
          System.out.printf("Accumulated pause estimate for five minutes: %d 
ms\n",
              TimeUnit.MILLISECONDS.convert(accumulatedNanos.get(), 
TimeUnit.NANOSECONDS));
          fiveMinuteStart = System.nanoTime();
          accumulatedNanos.set(0);
        }
      }
    }
  }
}
{code}

The PauseDetector thread in that code approximates what the jHiccup tool from 
Azul does.

https://www.azul.com/products/open-source-tools/jhiccup-performance-tool/

With a version of this thread in Solr and any other services, it would be very 
easy to detect and log severe application pauses, which typically are caused by 
GC.


> Standalone solr as *two* applications -- Solr and a controlling agent
> ---------------------------------------------------------------------
>
>                 Key: SOLR-6734
>                 URL: https://issues.apache.org/jira/browse/SOLR-6734
>             Project: Solr
>          Issue Type: Sub-task
>            Reporter: Shawn Heisey
>            Priority: Major
>
> In a message to the dev list outlining reasons to switch from a webapp to a 
> standalone app, Mark Miller included the idea of making Solr into two 
> applications, rather than just one.  There would be Solr itself, and an agent 
> to control Solr.
> http://mail-archives.apache.org/mod_mbox/lucene-dev/201305.mbox/%3C807476C6-E4C3-4E7E-9F67-2BECB63990DE%40gmail.com%3E



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org
For additional commands, e-mail: dev-h...@lucene.apache.org

Reply via email to