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這個類的作用



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