您的位置:首页 > 其它

solr 启动过程 分析

2013-02-23 19:00 183 查看

一、环境介绍:

1:我使用的版本是:apache-solr-4.0.0

2:本机在eclipse中调试源码。

3:工程结构:



    在根目录下创建netboy新文件夹,将\solr\example\solr目录下的文件全部拷贝到netboy文件中,修改solr.solr.home为: netboy\

二、现在开始我们启动solr的旅程:

     solr首先实例化一个SolrDispatchFilter对象,在实例化过程中,启用Config类的构造方法,Config类-->config()

if( loader == null ) {
loader = new SolrResourceLoader( null );
}
               创建一个“solr资源加载器”,同时也会创建一个“类加载器”,
this.classLoader = createClassLoader(null, parent);
addToClassLoader("./lib/", null);
               加载lib目录下的所有jar文件。

     接着启用SolrDispatchFilter-->init(FilterConfig config)方法,

                            

CoreContainer.Initializer init = createInitializer();
                          创建一个Initializer的实例init对象,然后      this.cores = init.initialize()获得CoreContainer对象;在init.initialize()方法中加载“netboy\solr.xml”文件查找到core的配置信息,加载core目录下的配置信息如solrConfig.xml、schema.xml等,创建Solrcore对象并将其注册。

                        

 三、创建solrCore的过程:

public void load(String dir, File configFile ) throws ParserConfigurationException, IOException, SAXException {
this.configFile = configFile;
this.load(dir, new InputSource(configFile.toURI().toASCIIString()));
}
加载solr.xml文件,将solr.xml中的配置信息解析出来,如defaultCoreName="core0" 、adminPath="/admin/cores"、config="solrconfig.xml" 、dataDir="data"等

public void load(String dir, InputSource cfgis){
。。。。。。。。。。
for (int i=0; i<nodes.getLength(); i++) {

Node node = nodes.item(i);
。。。。。。。。。。
SolrCore core = create(p);
register(name, core, false);

// track original names
coreToOrigName.put(core, rawName);
}
 。。。。。。。。。。
}


遍历配置的core,进行逐一创建。

/**
* Creates a new core based on a descriptor but does not register it.
*
* @param dcore a core descriptor
* @return the newly created core
*/
public SolrCore create(CoreDescriptor dcore)  throws ParserConfigurationException, IOException, SAXException {

// :TODO: would be really nice if this method wrapped any underlying errors and only threw SolrException

final String name = dcore.getName();
Exception failure = null;

try {
// Make the instanceDir relative to the cores instanceDir if not absolute
File idir = new File(dcore.getInstanceDir());
if (!idir.isAbsolute()) {
idir = new File(solrHome, dcore.getInstanceDir());
}
String instanceDir = idir.getPath();
log.info("Creating SolrCore '{}' using instanceDir: {}",
dcore.getName(), instanceDir);
// Initialize the solr config
SolrResourceLoader solrLoader = null;

SolrConfig config = null;
String zkConfigName = null;
if(zkController == null) {
solrLoader = new SolrResourceLoader(instanceDir, libLoader, getCoreProps(instanceDir, dcore.getPropertiesName(),dcore.getCoreProperties()));
config = new SolrConfig(solrLoader, dcore.getConfigName(), null);
} else {
try {
String collection = dcore.getCloudDescriptor().getCollectionName();
zkController.createCollectionZkNode(dcore.getCloudDescriptor());

zkConfigName = zkController.readConfigName(collection);
if (zkConfigName == null) {
log.error("Could not find config name for collection:" + collection);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
"Could not find config name for collection:" + collection);
}
/*加载solrconfig.xml文件*/
solrLoader = new ZkSolrResourceLoader(instanceDir, zkConfigName, libLoader, getCoreProps(instanceDir, dcore.getPropertiesName(),dcore.getCoreProperties()), zkController);
config = getSolrConfigFromZk(zkConfigName, dcore.getConfigName(), solrLoader);
} catch (KeeperException e) {
log.error("", e);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
"", e);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
log.error("", e);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
"", e);
}
}

IndexSchema schema = null;
if (indexSchemaCache != null) {
if (zkController == null) {
File schemaFile = new File(dcore.getSchemaName());
if (!schemaFile.isAbsolute()) {
schemaFile = new File(solrLoader.getInstanceDir() + "conf"
+ File.separator + dcore.getSchemaName());

4000
}
if (schemaFile.exists()) {
String key = schemaFile.getAbsolutePath()
+ ":"
+ new SimpleDateFormat("yyyyMMddHHmmss", Locale.ROOT).format(new Date(
schemaFile.lastModified()));
schema = indexSchemaCache.get(key);
if (schema == null) {
log.info("creating new schema object for core: " + dcore.name);
schema = new IndexSchema(config, dcore.getSchemaName(), null);
indexSchemaCache.put(key, schema);
} else {
log.info("re-using schema object for core: " + dcore.name);
}
}
} else {
// TODO: handle caching from ZooKeeper - perhaps using ZooKeepers versioning
// Don't like this cache though - how does it empty as last modified changes?
}
}
if(schema == null){
if(zkController != null) {
try {
schema = getSchemaFromZk(zkConfigName, dcore.getSchemaName(), config, solrLoader);
} catch (KeeperException e) {
log.error("", e);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
"", e);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
log.error("", e);
throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,
"", e);
}
} else {

/*创建schema对象*/
schema = new IndexSchema(config, dcore.getSchemaName(), null);
}
}
/*根据schema对象、config对象、core名等创建solrcore对象*/
SolrCore core = new SolrCore(dcore.getName(), null, config, schema, dcore);

if (zkController == null && core.getUpdateHandler().getUpdateLog() != null) {
// always kick off recovery if we are in standalone mode.
core.getUpdateHandler().getUpdateLog().recoverFromLog();
}

return core;

// :TODO: Java7...
// http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html } catch (ParserConfigurationException e1) {
failure = e1;
throw e1;
} catch (IOException e2) {
failure = e2;
throw e2;
} catch (SAXException e3) {
failure = e3;
throw e3;
} catch (RuntimeException e4) {
failure = e4;
throw e4;
} finally {
if (null != failure) {
log.error("Unable to create core: " + name, failure);
}
synchronized (coreInitFailures) {
// remove first so insertion order is updated and newest is last
coreInitFailures.remove(name);
if (null != failure) {
coreInitFailures.put(name, failure);
}
}
}
}


注意:每个核的实例建立的过程中会注册一个搜索器的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: