Search in sources :

Example 1 with ClientResponse

use of org.apache.storm.opentsdb.client.ClientResponse in project storm by apache.

the class OpenTsdbBolt method execute.

@Override
public void execute(Tuple tuple) {
    try {
        if (batchHelper.shouldHandle(tuple)) {
            final List<OpenTsdbMetricDatapoint> metricDataPoints = getMetricPoints(tuple);
            for (OpenTsdbMetricDatapoint metricDataPoint : metricDataPoints) {
                metricPointsWithTuple.put(metricDataPoint, tuple);
            }
            batchHelper.addBatch(tuple);
        }
        if (batchHelper.shouldFlush()) {
            LOG.debug("Sending metrics of size [{}]", metricPointsWithTuple.size());
            ClientResponse.Details clientResponse = openTsdbClient.writeMetricPoints(metricPointsWithTuple.keySet());
            if (failTupleForFailedMetrics && clientResponse != null && clientResponse.getFailed() > 0) {
                final List<ClientResponse.Details.Error> errors = clientResponse.getErrors();
                LOG.error("Some of the metric points failed with errors: [{}]", clientResponse);
                if (errors != null && !errors.isEmpty()) {
                    Set<Tuple> failedTuples = new HashSet<>();
                    for (ClientResponse.Details.Error error : errors) {
                        final Tuple failedTuple = metricPointsWithTuple.get(error.getDatapoint());
                        if (failedTuple != null) {
                            failedTuples.add(failedTuple);
                        }
                    }
                    for (Tuple batchedTuple : batchHelper.getBatchTuples()) {
                        if (failedTuples.contains(batchedTuple)) {
                            collector.fail(batchedTuple);
                        } else {
                            collector.ack(batchedTuple);
                        }
                    }
                } else {
                    throw new RuntimeException("Some of the metric points failed with details: " + errors);
                }
            } else {
                LOG.debug("Acknowledging batched tuples");
                batchHelper.ack();
            }
            metricPointsWithTuple.clear();
        }
    } catch (Exception e) {
        batchHelper.fail(e);
        metricPointsWithTuple.clear();
    }
}
Also used : ClientResponse(org.apache.storm.opentsdb.client.ClientResponse) OpenTsdbMetricDatapoint(org.apache.storm.opentsdb.OpenTsdbMetricDatapoint) Tuple(org.apache.storm.tuple.Tuple) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 OpenTsdbMetricDatapoint (org.apache.storm.opentsdb.OpenTsdbMetricDatapoint)1 ClientResponse (org.apache.storm.opentsdb.client.ClientResponse)1 Tuple (org.apache.storm.tuple.Tuple)1