solr 启动过程 分析

一、环境介绍:

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());
          }
          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);
        }
      }
    }
  }

注意:每个核的实例建立的过程中会注册一个搜索器的








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值