HttpSolrCall源码分析

HttpSolrCall是solr底层发送http请求的中转站,首先看一下它的构造函数

public HttpSolrCall(SolrDispatchFilter solrDispatchFilter, CoreContainer cores,
               HttpServletRequest request, HttpServletResponse response, boolean retry) {
    this.solrDispatchFilter = solrDispatchFilter;
    this.cores = cores;
    this.req = request;
    this.response = response;
    this.retry = retry;
    this.requestType = RequestType.UNKNOWN;
    queryParams = SolrRequestParsers.parseQueryString(req.getQueryString());
  }


没有什么特别之处,主要是进行了一些赋值操作


接下来这个类里面有个 private void init() throws Exception {}方法

主要是解析req从中获取core名称,并向req中写入一些属性,初始化一些core相关的参数,检查core在不在corecontainer中,并做一些错误异常抛出的处理,以及得到当前url的action类型。在这个方法里还调用了一个方法

extractHandlerFromURLPath(SolrRequestParsers parser) throws Exception {}

此方法从url中解析出当前请求需要用到的SolrRequestHandler,这个SolrRequestHandler在solr中很重要,每一种不同的request请求都对应着一个SolrRequestHandler,在后面的文章中我们将详细介绍

 solrReq = parser.parse(core, path, req);
 invalidStates = checkStateIsValid(solrReq.getParams().get(CloudSolrClient.STATE_VERSION));
 String qt = solrReq.getParams().get(CommonParams.QT);
 handler = core.getRequestHandler(qt);




下面这个方法是这个类的核心

public Action call() throws IOException {

   switch (action) {
        case ADMIN:
          handleAdminRequest();
          return RETURN;
        case REMOTEQUERY:
          remoteQuery(coreUrl + path, resp);
          return RETURN;
        case PROCESS:
          final Method reqMethod = Method.getMethod(req.getMethod());
          HttpCacheHeaderUtil.setCacheControlHeader(config, resp, reqMethod);
          // unless we have been explicitly told not to, do cache validation
          // if we fail cache validation, execute the query
          if (config.getHttpCachingConfig().isNever304() ||
              !HttpCacheHeaderUtil.doCacheHeaderValidation(solrReq, req, reqMethod, resp)) {
            SolrQueryResponse solrRsp = new SolrQueryResponse();
              /* even for HEAD requests, we need to execute the handler to
               * ensure we don't get an error (and to make sure the correct
               * QueryResponseWriter is selected and we get the correct
               * Content-Type)
               */
            SolrRequestInfo.setRequestInfo(new SolrRequestInfo(solrReq, solrRsp));
            execute(solrRsp);
            HttpCacheHeaderUtil.checkHttpCachingVeto(solrRsp, resp, reqMethod);
            Iterator<Map.Entry<String, String>> headers = solrRsp.httpHeaders();
            while (headers.hasNext()) {
              Map.Entry<String, String> entry = headers.next();
              resp.addHeader(entry.getKey(), entry.getValue());
            }
            QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
            if (invalidStates != null) solrReq.getContext().put(CloudSolrClient.STATE_VERSION, invalidStates);
            writeResponse(solrRsp, responseWriter, reqMethod);
          }
          return RETURN;
        default: return action;
      }

}


在这些代码里处理了ADMIN,REMOTEQUERY,PROCESS三种类型的请求,其他类型的action不做操作


ADMIN的请求在本机


这个类的分析到此结束,下一篇文章我们将分析CoreContainer这个类的作用



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章