Stateless handler of HTTP graph requests (the /q endpoint).
| 63 | * Stateless handler of HTTP graph requests (the {@code /q} endpoint). |
| 64 | */ |
| 65 | final class GraphHandler implements HttpRpc { |
| 66 | |
| 67 | private static final Logger LOG = |
| 68 | LoggerFactory.getLogger(GraphHandler.class); |
| 69 | |
| 70 | private static final boolean IS_WINDOWS = |
| 71 | System.getProperty("os.name", "").contains("Windows"); |
| 72 | |
| 73 | private static Pattern RANGE_VALIDATOR = Pattern.compile( |
| 74 | "\\[\\\"?-?\\d+\\.?(\\d+)?([eE]-?\\d+)?\\\"?:\\\"?-?(\\d+\\.?\\d+?)?([eE]-?\\d+)?\\\"?\\]"); |
| 75 | private static Pattern LABEL_VALIDATOR = Pattern.compile("[a-zA-z0-9 \\-_]"); |
| 76 | private static Pattern KEY_VALIDATOR = Pattern.compile( |
| 77 | "(out|left|top|center|right|horiz|box|bottom)?\\s?"); |
| 78 | private static Pattern STYLE_VALIDATOR = Pattern.compile("(linespoint|points|circles|dots)"); |
| 79 | private static Pattern COLOR_VALIDATOR = Pattern.compile("(x|X)[a-fA-F0-9]{6}"); |
| 80 | private static Pattern SMOOTH_VALIDATOR = Pattern.compile("unique|frequency|fnormal|cumulative|cnormal|bins|csplines|acsplines|mcsplines|bezier|sbezier|unwrap|zsort"); |
| 81 | // NOTE: This one should be tightened for only time based formatters. |
| 82 | private static Pattern FORMAT_VALIDATOR = Pattern.compile("(%[a-zA-Z])+[:\\/]?\\s?"); |
| 83 | private static Pattern WXH_VALIDATOR = Pattern.compile("^\\d+x\\d+$"); |
| 84 | /** Number of times we had to do all the work up to running Gnuplot. */ |
| 85 | private static final AtomicInteger graphs_generated |
| 86 | = new AtomicInteger(); |
| 87 | /** Number of times a graph request was served from disk, no work needed. */ |
| 88 | private static final AtomicInteger graphs_diskcache_hit |
| 89 | = new AtomicInteger(); |
| 90 | |
| 91 | /** Keep track of the latency of graphing requests. */ |
| 92 | private static final Histogram graphlatency = |
| 93 | new Histogram(16000, (short) 2, 100); |
| 94 | |
| 95 | /** Keep track of the latency (in ms) introduced by running Gnuplot. */ |
| 96 | private static final Histogram gnuplotlatency = |
| 97 | new Histogram(16000, (short) 2, 100); |
| 98 | |
| 99 | /** Executor to run Gnuplot in separate bounded thread pool. */ |
| 100 | private final ThreadPoolExecutor gnuplot; |
| 101 | |
| 102 | /** |
| 103 | * Constructor. |
| 104 | */ |
| 105 | public GraphHandler() { |
| 106 | // Gnuplot is mostly CPU bound and does only a little bit of IO at the |
| 107 | // beginning to read the input data and at the end to write its output. |
| 108 | // We want to avoid running too many Gnuplot instances concurrently as |
| 109 | // it can steal a significant number of CPU cycles from us. Instead, we |
| 110 | // allow only one per core, and we nice it (the nicing is done in the |
| 111 | // shell script we use to start Gnuplot). Similarly, the queue we use |
| 112 | // is sized so as to have a fixed backlog per core. |
| 113 | final int ncores = Runtime.getRuntime().availableProcessors(); |
| 114 | gnuplot = new ThreadPoolExecutor( |
| 115 | ncores, ncores, // Thread pool of a fixed size. |
| 116 | /* 5m = */ 300000, MILLISECONDS, // How long to keep idle threads. |
| 117 | new ArrayBlockingQueue<Runnable>(20 * ncores), // XXX Don't hardcode? |
| 118 | thread_factory); |
| 119 | // ArrayBlockingQueue does not scale as much as LinkedBlockingQueue in terms |
| 120 | // of throughput but we don't need high throughput here. We use ABQ instead |
| 121 | // of LBQ because it creates far fewer references. |
| 122 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…