wenchanter的专栏

加载完solr.xml后,再回到createCoreContainer方法:

protected CoreContainer createCoreContainer() {SolrResourceLoader loader = new SolrResourceLoader(SolrResourceLoader.locateSolrHome());// 加载$solrhome/solr.xml作为ConfigSolrConfigSolr config = loadConfigSolr(loader);CoreContainer cores = new CoreContainer(loader, config);cores.load();return cores; }

开始实例化CoreContainer,然后用这个实例来加载cores,load方法比较长,,具体在注释中分析:

/** * Load the cores defined for this CoreContainer */ public void load() {log.info("Loading cores into CoreContainer [instanceDir={}]", loader.getInstanceDir());// add the sharedLib to the shared resource loader before initializing cfg based plugins// 这个cfg是solr.xml的属性封装的对象,取sharedLib属性。sharedLib: path to a lib directory that will be shared across all coresString libDir = cfg.getSharedLibDirectory();if (libDir != null) {File f = FileUtils.resolvePath(new File(solrHome), libDir);log.info("loading shared library: " + f.getAbsolutePath());loader.addToClassLoader(libDir, null, false);loader.reloadLuceneSPI();}// solr.xml中配置的shardHandlerFactoryshardHandlerFactory = ShardHandlerFactory.newInstance(cfg.getShardHandlerFactoryPluginInfo(), loader);// updateShardHandler实例updateShardHandler = new UpdateShardHandler( cfg);// 取transientCacheSize属性,默认是int最大值,如果不是int最大值,则不起作用solrCores.allocateLazyCores(cfg .getTransientCacheSize(), loader);logging = LogWatcher.newRegisteredLogWatcher(cfg.getLogWatcherConfig(), loader);// shareSchema属性,如果为true则共享IndexSchemashareSchema = cfg.hasSchemaCache();if (shareSchema) {indexSchemaCache = new ConcurrentHashMap<String,IndexSchema>();}hostName = cfg.getHost();log.info("Host Name: " + hostName);// 初始化zookeeperzkSys.initZooKeeper( this, solrHome, cfg);collectionsHandler = createHandler(cfg.getCollectionsHandlerClass(), CollectionsHandler.class);infoHandler= createHandler(cfg.getInfoHandlerClass(), InfoHandler.class);coreAdminHandler = createHandler(cfg.getCoreAdminHandlerClass(), CoreAdminHandler.class);containerProperties = cfg.getSolrProperties( "solr");// setup executor to load cores in parallel// do not limit the size of the executor in zk mode since cores may try and wait for each other.ExecutorService coreLoadExecutor = Executors.newFixedThreadPool(( zkSys.getZkController() == null ? cfg.getCoreLoadThreadCount() : Integer. MAX_VALUE ),new DefaultSolrThreadFactory( "coreLoadExecutor") );try {CompletionService<SolrCore> completionService = new ExecutorCompletionService<SolrCore>(coreLoadExecutor);Set<Future<SolrCore>> pending = new HashSet<Future<SolrCore>>();// 遍历$solrhome寻找有core.properties的文件,List<CoreDescriptor> cds = coresLocator.discover(this );checkForDuplicateCoreNames(cds);for (final CoreDescriptor cd : cds) {final String name = cd.getName();try {if (cd.isTransient() || ! cd.isLoadOnStartup()) {// Store it away for later use. includes non-transient but not// loaded at startup cores.solrCores.putDynamicDescriptor(name, cd);}if (cd.isLoadOnStartup()) { // The normal caseCallable<SolrCore> task = new Callable<SolrCore>() {@Overridepublic SolrCore call() {SolrCore c = null;try {if ( zkSys.getZkController() != null) {preRegisterInZk(cd);}// 基于descriptor创建一个未注册的corec = create(cd);// 注册coreregisterCore(cd.isTransient(), name, c, false, false );} catch (Exception e) {SolrException. log(log, null, e);try {/* if (isZooKeeperAware()) {try {zkSys.zkController.unregister(name, cd);} catch (InterruptedException e2) {Thread.currentThread().interrupt();SolrException.log(log, null, e2);} catch (KeeperException e3) {SolrException.log(log, null, e3);}}*/} finally {if (c != null) {c.close();}}}return c;}};pending.add(completionService.submit(task));}} catch (Exception e) {SolrException. log(log, null, e);}}while (pending != null && pending.size() > 0) {try {Future<SolrCore> future = completionService.take();if (future == null) return;pending.remove(future);try {SolrCore c = future.get();// track original namesif (c != null) {solrCores.putCoreToOrigName(c, c.getName());}} catch (ExecutionException e) {SolrException. log(SolrCore.log, "Error loading core", e);}} catch (InterruptedException e) {throw new SolrException(SolrException.ErrorCode .SERVICE_UNAVAILABLE,"interrupted while loading core", e);}}// Start the background threadbackgroundCloser = new CloserThread( this, solrCores, cfg );backgroundCloser.start();} finally {if (coreLoadExecutor != null) {ExecutorUtil.shutdownNowAndAwaitTermination(coreLoadExecutor);}}if (isZooKeeperAware()) {// register in zk in background threadsCollection<SolrCore> cores = getCores();if (cores != null) {for (SolrCore core : cores) {try {zkSys.registerInZk(core, true);} catch (Throwable t) {SolrException. log(log, "Error registering SolrCore", t);}}}} }其中通过遍历solrhome文件夹下的所有文件夹,寻找core.properties文件,每找到一个配置文件视为找到一个core,并终止此文件夹的寻找,因此core.properties文件不能在嵌套的文件夹中出现,找到了core.properties文件夹,里面配置的属性封装为CoreDescriptor实例,同时将properties属性配置的文件作为扩展配置文件一并加载(默认solrcore.properties),solrcore.properties的作用可见https://cwiki.apache.org/confluence/display/solr/Configuring+solrconfig.xml#Configuringsolrconfig.xml-SubstitutingPropertiesinSolrConfigFiles

下面具体看几个相关的重要方法,以注释的形式分析:

先是coresLocator.discover(this)方法,coresLocator有两个子类:CorePropertiesLocator和SolrXMLCoresLocator,此处使用的是CorePropertiesLocator:

每一发奋努力的背后,必有加倍的赏赐。

wenchanter的专栏

相关文章:

你感兴趣的文章:

标签云: