package com.cloudera.flume.agent;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudera.flume.conf.thrift.ThriftFlumeClientServer;
import com.cloudera.flume.conf.FlumeConfigData;
import com.cloudera.flume.conf.FlumeConfiguration;
import com.cloudera.flume.conf.thrift.ThriftFlumeClientServer.Client;
import com.cloudera.flume.handlers.endtoend.AckListener;
import com.cloudera.flume.handlers.endtoend.CollectorAckListener;
import com.cloudera.flume.master.MasterClientServerThrift;
import com.cloudera.flume.master.StatusManager.NodeStatus;
import com.cloudera.flume.reporter.ReportEvent;
import com.cloudera.flume.reporter.server.thrift.ThriftFlumeReport;
import com.cloudera.flume.reporter.server.ThriftReportServer;
import com.google.common.base.Preconditions;
static final Logger LOG = LoggerFactory.getLogger(ThriftMasterRPC.class);
protected String masterHostname;
protected int masterPort;
protected ThriftFlumeClientServer.Iface masterClient;
ThriftMasterRPC(String masterHostname,
int masterPort)
throws IOException {
Preconditions.checkState(masterClient == null,
"client already initialized -- double init not allowed");
int timeout = FlumeConfiguration.get().getThriftSocketTimeoutMs();
TTransport masterTransport = new TSocket(masterHostname, masterPort, timeout);
TProtocol protocol = new TBinaryProtocol(masterTransport);
try {
masterTransport.open();
} catch (TTransportException e) {
throw new IOException(e.getMessage());
}
masterClient = new Client(protocol);
LOG.info("Connected to master at " + masterHostname + ":" + masterPort);
}
throws TTransportException, IOException {
if (masterClient == null) {
throw new IOException(
"MasterRPC called while disconnected.");
}
}
public synchronized void close() {
if (masterClient != null) {
TTransport masterTransport = ((ThriftFlumeClientServer.Client) masterClient)
.getOutputProtocol().getTransport();
masterTransport.close();
LOG.info("Connection from node to master closed");
} else {
LOG.debug("double close of flume node");
}
masterClient = null;
}
Preconditions.checkNotNull(masterClient);
return new CollectorAckListener(this);
}
throws IOException {
try {
ensureConnected();
return masterClient.getLogicalNodes(physNode);
} catch (TException e) {
LOG.debug("RPC error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
public synchronized Map<String, Integer>
getChokeMap (String physNode)
throws IOException {
try {
ensureConnected();
return (HashMap<String, Integer>) masterClient.getChokeMap(physNode);
} catch (TException e) {
LOG.debug("RPC error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
public synchronized FlumeConfigData
getConfig(LogicalNode n)
throws IOException {
try {
ensureConnected();
return MasterClientServerThrift.configFromThrift(
masterClient.getConfig(n.getName()));
} catch (TApplicationException e) {
LOG.debug(e.getMessage());
return null;
} catch (TException e) {
LOG.debug("Thrift error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
@Override
public synchronized boolean checkAck(String ackid)
throws IOException {
try {
ensureConnected();
return masterClient.checkAck(ackid);
} catch (TException e) {
LOG.debug("Thrift error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
public synchronized boolean heartbeat(LogicalNode n)
throws IOException {
try {
ensureConnected();
NodeStatus status = n.getStatus();
return masterClient.heartbeat(n.getName(), status.physicalNode,
status.host, MasterClientServerThrift.stateToThrift(status.state), n
.getConfigVersion());
} catch (TException e) {
LOG.debug("Thrift error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
@Override
public synchronized void acknowledge(String group)
throws IOException {
try {
ensureConnected();
masterClient.acknowledge(group);
} catch (TException e) {
LOG.debug("Thrift error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
return "Thrift Master RPC to " + masterHostname + ":" + masterPort;
}
@Override
public synchronized void putReports(Map<String, ReportEvent> reports)
throws IOException {
try {
ensureConnected();
Map<String, ThriftFlumeReport> flumeReports = new HashMap<String, ThriftFlumeReport>();
for (Entry<String, ReportEvent> e : reports.entrySet()) {
flumeReports.put(e.getKey(), ThriftReportServer.reportToThrift(e.getValue()));
}
masterClient.putReports(flumeReports);
} catch (TException e) {
LOG.debug("Thrift error on" + toString(), e);
throw new IOException("Thrift Error", e);
}
}
}