package com.cloudera.flume.agent;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.avro.ipc.AvroRemoteException;
import org.apache.avro.ipc.HttpTransceiver;
import org.apache.avro.specific.SpecificRequestor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudera.flume.conf.FlumeConfigData;
import com.cloudera.flume.conf.avro.AvroFlumeConfigData;
import com.cloudera.flume.reporter.server.avro.AvroFlumeReport;
import com.cloudera.flume.conf.avro.AvroFlumeClientServer;
import com.cloudera.flume.handlers.endtoend.CollectorAckListener;
import com.cloudera.flume.handlers.endtoend.AckListener;
import com.cloudera.flume.master.MasterClientServerAvro;
import com.cloudera.flume.master.StatusManager.NodeStatus;
import com.cloudera.flume.reporter.ReportEvent;
import com.cloudera.flume.reporter.server.AvroReportServer;
import com.google.common.base.Preconditions;
static final Logger LOG = LoggerFactory.getLogger(AvroMasterRPC.class);
protected String masterHostname;
protected int masterPort;
protected AvroFlumeClientServer masterClient;
protected HttpTransceiver trans;
AvroMasterRPC(String masterHost,
int masterPort)
throws IOException {
this.masterHostname = masterHost;
this.masterPort = masterPort;
URL url = new URL("http", masterHostname, masterPort, "/");
trans = new HttpTransceiver(url);
this.masterClient = (AvroFlumeClientServer) SpecificRequestor.getClient(
AvroFlumeClientServer.class, trans);
LOG.info("Connected to master at " + masterHostname + ":" + masterPort);
}
if (trans == null) {
LOG.debug("Double close of Flume Node");
return;
}
try {
trans.close();
} catch (IOException e) {
LOG.warn("Could not close Avro HTTP transceiver open to port "
+ masterPort);
}
this.trans = null;
this.masterClient = null;
}
Preconditions.checkNotNull(masterClient);
return new CollectorAckListener(this);
}
if (this.masterClient == null || this.trans == null) {
throw new IOException("MasterRPC called while not connected to master");
}
}
throws IOException {
try {
ensureInitialized();
List<CharSequence> res = masterClient.getLogicalNodes(physNode);
ArrayList<String> out = new ArrayList<String>((int) res.size());
for (CharSequence r : res) {
out.add(r.toString());
}
return out;
} catch (AvroRemoteException e) {
LOG.debug("RPC error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
public synchronized HashMap<String, Integer>
getChokeMap (String physNode)
throws IOException {
try {
ensureInitialized();
Map<CharSequence, Integer> chokeMap = masterClient
.getChokeMap(physNode);
HashMap<String, Integer> newMap = new HashMap<String, Integer>();
for (CharSequence s : chokeMap.keySet()) {
newMap.put(s.toString(), chokeMap.get(s));
}
return newMap;
} catch (AvroRemoteException e) {
LOG.debug("RPC error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
public synchronized FlumeConfigData
getConfig(LogicalNode n)
throws IOException {
try {
ensureInitialized();
AvroFlumeConfigData config = masterClient.getConfig(n.getName());
if (config == null) {
LOG.debug("Master does not have config data for me.");
return null;
}
return MasterClientServerAvro.configFromAvro(config);
} catch (AvroRemoteException e) {
LOG.debug("Avro error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
@Override
public synchronized boolean checkAck(String ackid)
throws IOException {
try {
ensureInitialized();
return masterClient.checkAck(ackid);
} catch (AvroRemoteException e) {
LOG.debug("Avro error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
@Override
public synchronized boolean heartbeat(LogicalNode n)
throws IOException {
try {
ensureInitialized();
NodeStatus status = n.getStatus();
return masterClient.heartbeat(n.getName(), status.physicalNode,
status.host, MasterClientServerAvro.stateToAvro(status.state), n
.getConfigVersion());
} catch (AvroRemoteException e) {
LOG.debug("Avro error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
@Override
public synchronized void acknowledge(String group)
throws IOException {
try {
ensureInitialized();
masterClient.acknowledge(group);
} catch (AvroRemoteException e) {
LOG.debug("Avro error on " + toString(), e);
throw new IOException(e.getMessage());
}
}
@Override
return "Avro Master RPC to " + masterHostname + ":" + masterPort;
}
@Override
public synchronized void putReports(Map<String, ReportEvent> reports)
throws IOException {
try {
ensureInitialized();
Map<CharSequence, AvroFlumeReport> flumeReports = new HashMap<CharSequence, AvroFlumeReport>();
for (Entry<String, ReportEvent> e : reports.entrySet()) {
flumeReports.put(e.getKey(), AvroReportServer.reportToAvro(e.getValue()));
}
masterClient.putReports(flumeReports);
} catch (AvroRemoteException e) {
LOG.debug("Avro error on" + toString(), e);
throw new IOException("Avro Error", e);
}
}
}