Search in sources :

Example 1 with TimerContext

use of com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext in project pinot by linkedin.

the class NettyTCPClientConnection method connect.

/**
   * Open a connection
   */
@Override
public boolean connect() {
    try {
        checkTransition(State.CONNECTED);
        //Connect synchronously. At the end of this line, _channel should have been set
        TimerContext t = MetricsHelper.startTimer();
        ChannelFuture f = _bootstrap.connect(_server.getHostname(), _server.getPort()).sync();
        /**
       * Waiting for future alone does not guarantee that _channel is set. _channel is set
       * only when the channelActive() async callback runs. So we should also wait for it.
       */
        f.get();
        _channelSet.await();
        t.stop();
        _connState = State.CONNECTED;
        _clientMetric.addConnectStats(t.getLatencyMs());
        return true;
    } catch (Exception ie) {
        if (ie instanceof ConnectException && ie.getMessage() != null && ie.getMessage().startsWith("Connection refused")) {
            // Most common case when a server is down. Don't print the entire stack and fill the logs.
            LOGGER.error("Could not connect to server {}:{} connId:{}", _server, ie.getMessage(), getConnId());
        } else {
            LOGGER.error("Got exception when connecting to server {} connId {}", _server, ie, getConnId());
        }
    }
    return false;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) TimerContext(com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 2 with TimerContext

use of com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext in project pinot by linkedin.

the class ScatterGatherImpl method sendRequest.

/**
   *
   * Helper Function to send scatter-request. This method should be called after the servers are selected
   *
   * @param ctxt Scatter-Gather Request context with selected servers for each request.
   * @param scatterGatherStats scatter-gather statistics.
   * @param isOfflineTable whether the scatter-gather target is an OFFLINE table.
   * @param brokerMetrics broker metrics to track execution statistics.
   * @return a composite future representing the gather process.
   * @throws InterruptedException
   */
protected CompositeFuture<ServerInstance, ByteBuf> sendRequest(ScatterGatherRequestContext ctxt, ScatterGatherStats scatterGatherStats, Boolean isOfflineTable, BrokerMetrics brokerMetrics) throws InterruptedException {
    TimerContext t = MetricsHelper.startTimer();
    // Servers are expected to be selected at this stage
    Map<ServerInstance, SegmentIdSet> mp = ctxt.getSelectedServers();
    CountDownLatch requestDispatchLatch = new CountDownLatch(mp.size());
    // async checkout of connections and then dispatch of request
    List<SingleRequestHandler> handlers = new ArrayList<SingleRequestHandler>(mp.size());
    for (Entry<ServerInstance, SegmentIdSet> e : mp.entrySet()) {
        ServerInstance server = e.getKey();
        String serverName = server.toString();
        if (isOfflineTable != null) {
            if (isOfflineTable) {
                serverName += ScatterGatherStats.OFFLINE_TABLE_SUFFIX;
            } else {
                serverName += ScatterGatherStats.REALTIME_TABLE_SUFFIX;
            }
        }
        scatterGatherStats.initServer(serverName);
        SingleRequestHandler handler = new SingleRequestHandler(_connPool, server, ctxt.getRequest(), e.getValue(), ctxt.getTimeRemaining(), requestDispatchLatch, brokerMetrics);
        // Submit to thread-pool for checking-out and sending request
        _executorService.submit(handler);
        handlers.add(handler);
    }
    // Create the composite future for returning
    CompositeFuture<ServerInstance, ByteBuf> response = new CompositeFuture<ServerInstance, ByteBuf>("scatterRequest", GatherModeOnError.SHORTCIRCUIT_AND);
    // Wait for requests to be sent
    long timeRemaining = ctxt.getTimeRemaining();
    boolean sentSuccessfully = requestDispatchLatch.await(timeRemaining, TimeUnit.MILLISECONDS);
    if (sentSuccessfully) {
        List<KeyedFuture<ServerInstance, ByteBuf>> responseFutures = new ArrayList<KeyedFuture<ServerInstance, ByteBuf>>();
        for (SingleRequestHandler h : handlers) {
            responseFutures.add(h.getResponseFuture());
            String serverName = h.getServer().toString();
            if (isOfflineTable != null) {
                if (isOfflineTable) {
                    serverName += ScatterGatherStats.OFFLINE_TABLE_SUFFIX;
                } else {
                    serverName += ScatterGatherStats.REALTIME_TABLE_SUFFIX;
                }
            }
            scatterGatherStats.setSendStartTimeMillis(serverName, h.getConnStartTimeMillis());
            scatterGatherStats.setConnStartTimeMillis(serverName, h.getStartDelayMillis());
            scatterGatherStats.setSendCompletionTimeMillis(serverName, h.getSendCompletionTimeMillis());
        }
        response.start(responseFutures);
    } else {
        LOGGER.error("Request (" + ctxt.getRequest().getRequestId() + ") not sent completely within time (" + timeRemaining + " ms) !! Cancelling !!. NumSentFailed:" + requestDispatchLatch.getCount());
        response.start(null);
        // and so we cancel all of them here
        for (SingleRequestHandler h : handlers) {
            LOGGER.info("Request to {} was sent successfully:{}, cancelling.", h.getServer(), h.isSent());
            h.cancel();
        }
    }
    t.stop();
    _latency.update(t.getLatencyMs());
    return response;
}
Also used : CompositeFuture(com.linkedin.pinot.transport.common.CompositeFuture) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) KeyedFuture(com.linkedin.pinot.transport.common.KeyedFuture) TimerContext(com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext) SegmentIdSet(com.linkedin.pinot.transport.common.SegmentIdSet) ServerInstance(com.linkedin.pinot.common.response.ServerInstance)

Example 3 with TimerContext

use of com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext in project pinot by linkedin.

the class ScatterGatherPerfClient method run.

@Override
public void run() {
    System.out.println("Client starting !!");
    try {
        List<ServerInstance> s1 = new ArrayList<ServerInstance>();
        ServerInstance s = new ServerInstance("localhost", 9099);
        s1.add(s);
        SimpleScatterGatherRequest req = null;
        TimerContext tc = null;
        for (int i = 0; i < _numRequests; i++) {
            LOGGER.debug("Sending request number {}", i);
            do {
                req = getRequest();
            } while ((null == req));
            if (i == _numRequestsToSkipForMeasurement) {
                tc = MetricsHelper.startTimer();
                _beginFirstRequestTime = System.currentTimeMillis();
            }
            if (i >= _numRequestsToSkipForMeasurement) {
                _numRequestsMeasured++;
            }
            final ScatterGatherStats scatterGatherStats = new ScatterGatherStats();
            if (!_asyncRequestSubmit) {
                sendRequestAndGetResponse(req, scatterGatherStats);
                _endLastResponseTime = System.currentTimeMillis();
            } else {
                CompositeFuture<ServerInstance, ByteBuf> future = asyncSendRequestAndGetResponse(req, scatterGatherStats);
                _queue.offer(new QueueEntry(false, i >= _numRequestsToSkipForMeasurement, System.currentTimeMillis(), future));
            }
            //System.out.println("Response is :" + r);
            //System.out.println("\n\n");
            req = null;
        }
        if (_asyncRequestSubmit) {
            int numTerminalEntries = _readerThreads.size();
            for (int i = 0; i < numTerminalEntries; i++) {
                _queue.offer(new QueueEntry(true, false, System.currentTimeMillis(), null));
            }
            for (AsyncReader r : _readerThreads) {
                r.join();
            }
        }
        if (null != tc) {
            tc.stop();
            _timerContext = tc;
            System.out.println("Num Requests :" + _numRequestsMeasured);
            System.out.println("Total time :" + tc.getLatencyMs());
            System.out.println("Throughput (Requests/Second) :" + ((_numRequestsMeasured * 1.0 * 1000) / tc.getLatencyMs()));
            System.out.println("Latency :" + new LatencyMetric<Histogram>(_latencyHistogram));
            System.out.println("Scatter-Gather Latency :" + new LatencyMetric<Histogram>(_scatterGather.getLatency()));
        }
    } catch (Exception ex) {
        System.err.println("Client stopped abnormally ");
        ex.printStackTrace();
    }
    shutdown();
    System.out.println("Client done !!");
}
Also used : LatencyMetric(com.linkedin.pinot.common.metrics.LatencyMetric) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) JSONException(org.json.JSONException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimerContext(com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext) ScatterGatherStats(com.linkedin.pinot.transport.scattergather.ScatterGatherStats) ServerInstance(com.linkedin.pinot.common.response.ServerInstance)

Aggregations

TimerContext (com.linkedin.pinot.common.metrics.MetricsHelper.TimerContext)3 ServerInstance (com.linkedin.pinot.common.response.ServerInstance)2 ByteBuf (io.netty.buffer.ByteBuf)2 ArrayList (java.util.ArrayList)2 LatencyMetric (com.linkedin.pinot.common.metrics.LatencyMetric)1 CompositeFuture (com.linkedin.pinot.transport.common.CompositeFuture)1 KeyedFuture (com.linkedin.pinot.transport.common.KeyedFuture)1 SegmentIdSet (com.linkedin.pinot.transport.common.SegmentIdSet)1 ScatterGatherStats (com.linkedin.pinot.transport.scattergather.ScatterGatherStats)1 ChannelFuture (io.netty.channel.ChannelFuture)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONException (org.json.JSONException)1