Solr2.9.1
代码下面的分析有点散,围绕solrcore展开,重点是query流程,细入是getSearcher
内容中有一些直接从参考链接里面拷贝的。
【1】SolrCore功能点
0.JMX的初始化—》远程监控使用
进入参考链接http://lucidworks.lucidimagination.com/display/solr/Using+JMX+with+Solr
默认情况下不会起到JMX
1.初始化加载solrconfig.xml
配置信息—-》整个检索层的配置
2.updatehandler–》写索引,关联的updateProcessorchains,内部重新打开索引writer。
Commit的时候会getSearcher从而打开新的reader视图,solrIndexSearcher之后就可见。
3SolrIndexSearcher—》查询索引的入口,关联
查询解析的QquerParse、高亮、searchComponent、【读reader共享、写需要同步reader,SolrIndexSearch打开的都是readonlyreader
线程安全并且单例,提升性能】
4
事件支持:firstSearcherListeners、newSearcherListeners
这里可以作为扩展自己的reader服务的入口
5.
查询返回的responseWriter
6
前提准备索位置Directory、索引reader的readerfactory
,间接关联reader管理,
7
索引的删除策略加载
【2】execute分析
在SolrCore中有两个execute方法:
1.
execute(SolrQueryRequestreq,SolrQueryResponsersp)
2.execute(SolrRequestHandlerhandler,SolrQueryRequestreq,
SolrQueryResponsersp)
对于第一个方法,没有像第二个方法那样的handler参数,但是其实其内部通过这样一个方法来获得handler
的:SolrRequestHandler
handler=getRequestHandler(req.getQueryType())。也就是说我们通过在req中指定qt参数的值就可以获得我们想要的处理
器,当然这些处理器需要在solrconfig.xml的
requestHandler元素中定义(配置文件中有大量requestHandler)。这样服务器在建立时才能建立相应的处理器实例。
这里我发现在获取处理器时,参数为“”,null还是standard都能得到StandardRequestHandler的实例
SolrCore::execute的流程
1.进行handler合法性的检查,不能为null,否则抛出错误。
2.finalNamedListresponseHeader=new
SimpleOrderedMap();
rsp.add(“responseHeader”,responseHeader);
建立一个单序映射表并将其作为响应头加入到rsp中。
3.NamedListtoLog=rsp.getToLog();
获得rsp的ToLog对象,并从请求中获得一些相关信息加入,这些信息有webapp,path,params。
4.handler.handleRequest(req,rsp);
5.StringBuildersb=new
StringBuilder();….
从rsp的ToLog中获得信息并将其加入到sb中,然后在日志中输出。
分析了SolrCore中的execute方法的流程,而且可以看到具体执行查询过程的语句是:handler.handleRequest(req,rsp)。请注意查询时候,这里的变量的类型是:
handler:StandardRequestHandler
req:SolrRequestParsers类中的一个匿名类。
rsp:SolrQueryResponse
query调用流
StandardRequestHandlerextends
SearchHandler,solrcore::execute.handler.handlerRequest(),
转为solrcore::execute.StandardRequestHandler.handlerRequest
转为SearchHandler.handlerRequest()
而SearchHandlerextends
RequestHandlerBase
转为RequestHandlerBase.handlerRequest()
真正执行handlerRequest()
在RequestHandlerBase.handlerRequest()中
设置httpcaching、handleRequestBody(),
抽象的handlerRequestBody()在子类SearchHandler中实现了,
转回SearchHandler.handlerRequestBody(),其中对QueryComponent
执行prepare、process
在prepare中生成QParser,注意只有queryComponent中生成了Qparser,其他的没有生成。并且Qparser
是从static的getParser()
中拿到,而Qparser.getParser()中又是从SolrCore.getQueryPlugin()
通过solrconfig.xml中配置获取。
SolrCore中Searcher
Control部分有点复杂
—-getSearcher的逻辑
getSearcher–(forceNew,
returnSearcher,waitSearcher-Futures)
关注solr全局三个点调用getSearcher函数
:solrCore初始化时(false,false,null),QueryComponent处理查询请求时(false,true,null),UpdateHandler在处理commit请求时(true,false,newFuture[1])
外部调用
EmbededSolrServer.request–>在执行core.execute()
之前,从core中getRequestHandler,_parser中buildRequestFrom(),newSolrQueryResponse()
返回SolrQueryRequestBase,
SolrQueryRequestBase
中
protected RefCounted searcherHolder;
publicSolrIndexSearcher getSearcher()
{
if(core == null) return null;//a request for a core admin
will no have a core
// should this reach out and get a
searcher from the core singleton, or
// should the core populate one in a
factory method to create requests?
// or there could be a setSearcher()
method that Solr calls
if(searcherHolder==null) {
searcherHolder= core.getSearcher();
}
returnsearcherHolder.get();
}
public void close() {
if(searcherHolder!=null) {
searcherHolder.decref();
searcherHolder= null;
}
}
当调用SolrQueryRequestBase的getSearcher()时,如果是第一次调用,会转而调用core.getSearcher(),
其会使得到的searcherHolder的引用计数增一。而对称的,SolrQueryRequestBase的close()方法使searcherHolder的引用计数减一,一增一减平衡了。
这也使得当新的索引到来时,仍旧提供查询服务的SolrIndexSearcher不会立即关闭,直到其引用计数减为0才关闭。所以,在使用SolrQueryRequest时,要确保请求的最后调用其close方法,否则那些无用的SolrIndexSearcher就不会被释放,直到句柄耗尽或者OOM掉。
引用对象
引用对象的典型使用是SolrCore,看下相关代码:
<b><span lang="EN-US" style="font-family:">private</span></b><span lang="EN-US" style="font-family:"> <b><span style="color:black">final</span></b> AtomicInteger refCount <span style="color:#339933">=</span> <b><span style="color:black">new</span></b> AtomicInteger<span style="color:#009900">(</span><span style="color:#CC66CC">1</span><span style="color:#009900">)</span><span style="color:#339933">;</span></span>
<span lang="EN-US" style="font-family:"> </span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:black">final</span></b> <b><span style="color:#000066">void</span></b> open<span style="color:#009900">()</span> <span style="color:#009900">{</span></span>
<span lang="EN-US" style="font-family:"><span> </span>refCount.<span style="color:#006633">incrementAndGet</span><span style="color:#009900">()</span><span style="color:#339933">;</span></span>
<span lang="EN-US" style="font-family:"><span> </span><span style="color:#009900">}</span></span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:black">public</span></b> <b><span style="color:#000066">void</span></b> close<span style="color:#009900">()</span> <span style="color:#009900">{</span></span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:#000066">int</span></b> count <span style="color:#339933">=</span> refCount.<span style="color:#006633">decrementAndGet</span><span style="color:#009900">()</span><span style="color:#339933">;</span></span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:black">if</span></b> <span style="color:#009900">(</span>count <span style="color:#339933">></span> <span style="color:#CC66CC">0</span><span style="color:#009900">)</span> <b><span style="color:black">return</span></b><span style="color:#339933">;</span> <i><span style="color:#666666">// close is called often, and only actually closes if nothing is using it.</span></i></span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:black">if</span></b> <span style="color:#009900">(</span>count <span style="color:#339933"><</span> <span style="color:#CC66CC">0</span><span style="color:#009900">)</span> <span style="color:#009900">{</span></span></span>
<span lang="EN-US" style="font-family:"><span> </span>log.<span style="color:#006633">error</span><span style="color:#009900">(</span><span style="color:blue">"Too many close [count:{}] on {}. Please report this exception to solr-user@lucene.apache.org"</span>, count, <b><span style="color:black">this</span></b> <span style="color:#009900">)</span><span style="color:#339933">;</span></span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:black">return</span></b><span style="color:#339933">;</span></span>
<span lang="EN-US" style="font-family:"><span> </span><span style="color:#009900">}</span></span>
<span lang="EN-US" style="font-family:"><span> </span><i><span style="color:#666666">//</span></i></span><i><span>释放对象和资源</span></i>
<span lang="EN-US" style="font-family:"><span> </span><span style="color:#009900">}</span></span>
引用计数就是refCount了。SolrCore实例的获得是通过调用CoreContainer的SolrCoregetCore(Stringname)得到,其实现是:
<b><span lang="EN-US" style="font-family:">public</span></b><span lang="EN-US" style="font-family:"> SolrCore getCore<span style="color:#009900">(</span><span style="color:#003399">String</span> name<span style="color:#009900">)</span> <span style="color:#009900">{</span></span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:black">synchronized</span></b><span style="color:#009900">(</span>cores<span style="color:#009900">)</span> <span style="color:#009900">{</span></span>
<span lang="EN-US" style="font-family:"><span> </span>SolrCore core <span style="color:#339933">=</span> cores.<span style="color:#006633">get</span><span style="color:#009900">(</span>name<span style="color:#009900">)</span><span style="color:#339933">;</span></span>
<span lang="EN-US" style="font-family:"><span> </span><b><span style="color:black">if</span></b> <span style="color:#009900">(</span>core <span style="color:#339933">!=</span> <b><span style="color:#000066">null</span></b><span style="color:#009900">)</span></span>
<span lang="EN-US" style="font-family:"><span> </span>core.<span style="color:#006633">open</span><span style="color:#009900">()</span><span style="color:#339933">;</span><span> </span><i><span style="color:#666666">// increment the ref count while still synchronized</span></i></span>
<span lang="EN-US" style="font-family:"> <span> </span><b><span style="color:black">return</span></b> core<span style="color:#339933">;</span></span>
<span lang="EN-US" style="font-family:"><span> </span><span style="color:#009900">}</span></span>
<span lang="EN-US" style="font-family:"><span> </span><span style="color:#009900">}</span></span>
也就是说CoreContainer显示的open了SolrCore,所以在得到SolrCore实例后,也需要显示的close它。因为SolrQueryRequestBase用到了SolrCore,所以在处理请求的最后,要确保调用了SolrCore的close方法。当然,对于查询端,SolrCore实例在整个生命周期内通常并不会真正被close,除非显示的调用了reload等操作。
下一篇SolrCoregetSearcher
参考链接
http://www.cnblogs.com/mandela/archive/2011/05/10/2041754.html
转载于:https://blog.51cto.com/aliapp/1325784