Changeset 25909 for gs3-extensions/solr

Show
Ignore:
Timestamp:
06.07.2012 18:01:29 (7 years ago)
Author:
ak19
Message:

Making SolrSearch? uptodate with the changes to GS2SolrSearch. SolrSearch? remains untested.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • gs3-extensions/solr/trunk/src/src/java/org/greenstone/gsdl3/service/SolrSearch.java

    r25808 r25909  
    1515import java.util.HashMap; 
    1616import java.util.ArrayList; 
     17import java.util.Iterator; 
    1718 
    1819 
     
    2930import org.apache.solr.servlet.SolrRequestParsers; 
    3031import org.apache.solr.core.CoreContainer; 
     32import org.apache.solr.core.SolrCore; 
    3133 
    3234import java.io.File; 
     
    5658         
    5759        String solr_home_str = GSFile.extHome(gsdl3_home,solr_ext_name); 
    58         File solr_home = new File(solr_home_str); 
    59         File solr_xml = new File( solr_home,"solr.xml" ); 
    60  
    61         solr_cores = new CoreContainer(solr_home_str,solr_xml);              
     60 
     61        solr_cores = new CoreContainer(solr_home_str); 
    6262        } 
    6363        catch (Exception e) { 
     
    6666    } 
    6767    } 
     68     
    6869 
    6970    // Overriding the cleanUp() method here, so as to parallel the structure of GS2SolrSearch  
     
    7172    // However, this class has not yet been tested, so it's not certain that this method is  
    7273    // required here. 
     74    // Adjusted  to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested 
    7375    public void cleanUp() { 
    7476    super.cleanUp(); 
    75     solr_cores.shutdown(); 
    76     } 
    77  
     77     
     78        // 1. clear the map keeping track of the solrcores' EmbeddedSolrServers in this collection 
     79        solr_server.clear(); 
     80 
     81        // 2. Remove all SolrCores in the CoreContainer (solr_cores) that are specific to this collection 
     82        String collection_core_name_prefix = getCollectionCoreNamePrefix(); 
     83 
     84        Collection<String> coreNames = solr_cores.getCoreNames(); 
     85        if(!coreNames.isEmpty()) { 
     86            Iterator<String> coreIterator = coreNames.iterator(); 
     87            while(coreIterator.hasNext()) { 
     88 
     89            String solrCoreName = coreIterator.next();       
     90            if(solrCoreName.startsWith(collection_core_name_prefix)) { 
     91 
     92                logger.error("**** Removing collection-specific core: " + solrCoreName + " from CoreContainer"); 
     93 
     94                // CoreContainer.remove(String name): removes and returns registered core w/o decrementing it's reference count 
     95                // http://lucene.apache.org/solr/api/index.html?org/apache/solr/core/CoreContainer.html 
     96                SolrCore solr_core = solr_cores.remove(solrCoreName); 
     97                while(!solr_core.isClosed()) { 
     98                logger.error("@@@@@@ " + solrCoreName + " was not closed. Closing...."); 
     99                solr_core.close(); // http://lucene.apache.org/solr/api/org/apache/solr/core/SolrCore.html 
     100                }  
     101                if(solr_core.isClosed()) { 
     102                logger.error("@@@@@@ " + solrCoreName + " is closed."); 
     103                } 
     104                solr_core = null; 
     105            } 
     106            } 
     107        } 
     108 
     109        // 3. if there are no more solr cores in Greenstone, then solr_cores will be empty, null the CoreContainer 
     110        // All going well, this will happen when we're ant stopping the Greenstone server and the last Solr collection 
     111        // is being deactivated 
     112        Collection<String> coreNamesRemaining = solr_cores.getCoreNames(); 
     113        if(coreNamesRemaining.isEmpty()) { 
     114            logger.error("**** CoreContainer contains 0 solrCores. Shutting down..."); 
     115 
     116            solr_cores.shutdown(); // wouldn't do anything anyway for 0 cores I think 
     117            solr_cores = null; 
     118        }  
     119        else { // else part is just for debugging 
     120            Iterator coreIterator = coreNamesRemaining.iterator(); 
     121            while(coreIterator.hasNext()) { 
     122            logger.error("**** Core: " + coreIterator.next() + " still exists in CoreContainer"); 
     123            } 
     124        } 
     125     
     126    } 
     127 
     128     
     129    // adjusted configure to bring it up to speed with changes in GS2SolrSearch (for activate.pl) - not yet tested 
    78130    public boolean configure(Element info, Element extra_info) { 
    79     if (!super.configure(info, extra_info)){ 
    80         return false; 
    81     } 
     131        boolean success = super.configure(info, extra_info); 
     132     
     133        // 1. Make the CoreContainer reload solr.xml 
     134        // This is particularly needed for when activate.pl is executed during 
     135        // a running GS3 server. At that point, the solr collection is reactivated and  
     136        // we need to tell Greenstone that the solr index has changed. This requires 
     137        // the CoreContainer to reload the solr.xml file, and it all works again. 
     138 
     139        solr_server.clear(); // clear the map of solr cores for this collection added to the map upon querying 
     140         
     141        // Reload the updated solr.xml into the CoreContainer 
     142        // (Doing a solr_cores.shutdown() first doesn't seem to be required) 
     143        try {    
     144            String solr_home_str = solr_cores.getSolrHome(); 
     145            File solr_home = new File(solr_home_str); 
     146            File solr_xml = new File( solr_home,"solr.xml" ); 
     147             
     148            solr_cores.load(solr_home_str,solr_xml);             
     149        } catch (Exception e) { 
     150            logger.error("Exception in SolrSearch.configure(): " + e.getMessage()); 
     151            e.printStackTrace(); 
     152            return false; 
     153        } 
    82154 
    83155    // initialize required number of SolrCores based on values 
    84156    // in 'index_ids' that are set by LuceneSearch::configure() 
    85157 
    86     String site_name = this.router.getSiteName(); 
    87     String coll_name = this.cluster_name; 
     158    String core_name_prefix = getCollectionCoreNamePrefix(); 
    88159 
    89160    for (int i=0; i<index_ids.size(); i++) { 
    90161 
    91162        String idx_name = (String)index_ids.get(i); 
    92         String core_name = site_name + "-" + coll_name + "-" + idx_name; 
     163        String core_name = core_name_prefix + "-" + idx_name; 
    93164 
    94165        EmbeddedSolrServer solr_core 
     
    98169    } 
    99170 
    100     return true; 
     171    return success; 
    101172    } 
    102173     
     
    139210        //solrParams.set("rows", nbDocuments); 
    140211 
    141         String site_name = this.router.getSiteName(); 
    142         String coll_name = this.cluster_name; 
    143  
    144         String core_name = site_name + "-" + coll_name + "-" + index; 
     212        String core_name = getCollectionCoreNamePrefix() + "-" + index; 
    145213 
    146214        EmbeddedSolrServer solr_core = (EmbeddedSolrServer)solr_server.get(core_name); 
     
    176244    } 
    177245 
     246    protected String getCollectionCoreNamePrefix() { 
     247        String site_name = this.router.getSiteName(); 
     248        String coll_name = this.cluster_name; 
     249        String collection_core_name_prefix = site_name + "-" + coll_name; 
     250        return collection_core_name_prefix; 
     251    } 
    178252     
    179253}