是否有任何Solr API可以告诉特定Solr节点上的所有核心何时加载并可以提供查询(遗留模式)?

biswetbf  于 2022-11-23  发布在  Solr
关注(0)|答案(1)|浏览(168)

我尝试了/solr/api/cores,我想这就是我想要的,尽管,如果Solr刚刚重新启动,那么它会返回到目前为止加载了多少个内核的状态。它不会对所有内核都停止。有没有办法让它停止,直到所有内核都加载并可查询?

kg7wmglp

kg7wmglp1#

最后我写了一个自定义的Solr插件。代码如下:

public class CustomHealthCheckHandler extends RequestHandlerBase {
private static final Logger LOG = LoggerFactory.getLogger(CustomHealthCheckHandler.class);

@Override
public String getDescription() {
    return "A Simple healthcheck handler that checks if all cores are loaded and queriable";
}

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse resp) {
    CoreContainer coreContainer = req.getCore().getCoreContainer();

    // Get list of core names regardless of whether they are loaded or not
    // Notice that we are intenetionally not using coreContainer.getLoadedCoreNames()
    // or coreContainer.geAllCoreNames() here because they might not return all the cores
    // in the case Solr is just restarted and all the cores are not loaded yet.
    Path solrHome = Paths.get(coreContainer.getSolrHome());
    CorePropertiesLocator locator = new CorePropertiesLocator(solrHome);
    List<CoreDescriptor> coreDescriptors = locator.discover(coreContainer);
    Collection<String> cores = coreDescriptors.stream().map(cd -> cd.getName()).collect(java.util.stream.Collectors.toList());

    for (String core : cores) {
        // get the /admin/ping handler for each core
        SolrRequestHandler handler = coreContainer.getCore(core).getRequestHandler("/admin/ping");
        // if handler is null, then return with UNHEALTHY status
        if (handler == null) {
            resp.add("status", "UNHEALTHY");
            return;
        }

        SolrQueryResponse response = new SolrQueryResponse();
        SolrQuery query = new SolrQuery();
        query.set("wt", "json");
        query.set("indent", true);

        // execute the query
        handler.handleRequest(new SolrQueryRequestBase(coreContainer.getCore(core), query) {}, response);
        String status = ((String) response.getValues().get("status"));
        // if status is null or not OK, then return
        if (status == null || !status.equals("OK")) {
            resp.add("status", "UNHEALTHY");
            return;
        }
    }
    resp.add("status", "HEALTHY");
}

}

相关问题