}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
BrokerConfig brokerConfig = new BrokerConfig(senseiConf, loadBalancerFactory, serializer, pluginRegistry);
brokerConfig.init();
_senseiBroker = brokerConfig.buildSenseiBroker();
_senseiSysBroker = brokerConfig.buildSysSenseiBroker(versionComparator);
_networkClient = brokerConfig.getNetworkClient();
_clusterClient = brokerConfig.getClusterClient();
federatedBroker = pluginRegistry.getBeanByFullPrefix(SenseiConfParams.SENSEI_FEDERATED_BROKER, LayeredBroker.class);
if (federatedBroker != null) {
federatedBroker.warmUp();
}
logger.info("Connecting to cluster: " + brokerConfig.getClusterName() +" ...");
_clusterClient.awaitConnectionUninterruptibly();
SenseiBrokerExport export = (SenseiBrokerExport)config.getServletContext().getAttribute("sensei.broker.export");
export.broker = _senseiBroker;
export.sysBroker = _senseiSysBroker;
export.networkClient = _networkClient;
export.clusterClient = _clusterClient;
export.servlet = this;
int count = 0;
while (true)
{
try
{
count++;
logger.info("Trying to get sysinfo");
SenseiSystemInfo sysInfo = _senseiSysBroker.browse(new SenseiRequest());
_facetInfoMap = sysInfo != null && sysInfo.getFacetInfos() != null ? extractFacetInfo(sysInfo) : new HashMap<String, String[]>();
_compiler = new BQLCompiler(_facetInfoMap);
break;
}
catch (Exception e)
{
logger.info("Hit exception trying to get sysinfo", e);
if (count > 10)
{
logger.error("Give up after 10 tries to get sysinfo");
throw new ServletException(e.getMessage(), e);
}
else
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException e2)
{
logger.error("Hit InterruptedException in getting sysinfo: ", e);
}
}
}
}
// Start the stat timer to get some of the sys stat:
_statTimer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
int totalDocs = 0;
try
{
SenseiRequest req = new SenseiRequest();
req.setQuery(new SenseiJSONQuery(new FastJSONObject().put("query", "dummy:dummy")));
SenseiResult res = _senseiBroker.browse(req);
totalDocs = res.getTotalDocs();
}
catch(Exception e)
{
logger.warn("Error getting result", e);
}
if (totalDocs > 0)
{
totalDocsCounter.clear();
totalDocsCounter.inc(totalDocs);
}
else
{
logger.warn("Unable to get total docs");
}
try
{
SenseiSystemInfo sysInfo = _senseiSysBroker.browse(new SenseiRequest());
if (sysInfo != null && sysInfo.getFacetInfos() != null)
{
_facetInfoMap = extractFacetInfo(sysInfo);
_compiler.setFacetInfoMap(_facetInfoMap);
}
}
catch (Exception e)
{
logger.info("Hit exception trying to get sysinfo", e);
}
}
}, 60000, 60000); // Every minute.
export.facetInfo = _facetInfoMap;
logger.info("Cluster: "+ brokerConfig.getClusterName() +" successfully connected ");
}