net.opentsdb.tsd.KafkaRpcPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentsdb-rpc-kafka Show documentation
Show all versions of opentsdb-rpc-kafka Show documentation
A consumer and publisher for OpenTSDB Kafka messages
// This file is part of OpenTSDB.
// Copyright (C) 2018 The OpenTSDB Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package net.opentsdb.tsd;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.AtomicDouble;
import com.stumbleupon.async.Deferred;
import net.opentsdb.core.TSDB;
import net.opentsdb.stats.StatsCollector;
import net.opentsdb.tsd.KafkaRpcPluginThread.CounterType;
public class KafkaRpcPlugin extends RpcPlugin implements TimerTask {
private static final Logger LOG = LoggerFactory.getLogger(
KafkaRpcPlugin.class);
public static KafkaRpcPlugin KAFKA_RPC_REFERENCE = null;
private final String host;
private ExecutorService thread_pool;
private List consumer_groups;
private KafkaRpcPluginConfig config;
private boolean track_metric_prefix;
private TSDB tsdb;
private final ConcurrentHashMap> namespace_counters;
private final Map totals_counters;
private final AtomicLong messages_received = new AtomicLong();
private final AtomicLong datapoints_received = new AtomicLong();
private final AtomicLong deserialization_errors = new AtomicLong();
private final AtomicLong restarts = new AtomicLong();
private final AtomicLong rebalance_failures = new AtomicLong();
private final AtomicDouble cumulative_rate_delay = new AtomicDouble();
private final AtomicDouble kafka_wait_time = new AtomicDouble();
public KafkaRpcPlugin() {
try {
host = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
LOG.error("Unable to get hostname!", e);
throw new RuntimeException("WTF? Shouldn't be here", e);
}
thread_pool = Executors.newCachedThreadPool();
namespace_counters = new ConcurrentHashMap>(
CounterType.values().length);
totals_counters = Maps.newHashMapWithExpectedSize(CounterType.values().length);
}
@Override
public void initialize(final TSDB tsdb) {
this.tsdb = tsdb;
config = new KafkaRpcPluginConfig(tsdb.getConfig());
consumer_groups = createConsumerGroups();
LOG.info("Launching " + consumer_groups.size() + " Kafka consumer groups...");
for (final KafkaRpcPluginGroup group : consumer_groups) {
group.start();
}
LOG.info("Launched " + consumer_groups.size() + " Kafka consumer groups");
tsdb.getTimer().newTimeout(this, 100, TimeUnit.MILLISECONDS);
// Sync just in case the HTTP plugin loads or tries to fetch stats before
// we finish initializing.
synchronized (tsdb) {
KAFKA_RPC_REFERENCE = this;
}
LOG.info("Initialized KafkaRpcPlugin.");
}
@Override
public Deferred