acts_as_solr for development and production in one Tomcat instance

Plugins, Ruby on Rails Add comments

In my previous post on acts_as_solr, I noted that as of 0.7, acts_as_solr did not support placing the development and production Solr instances inside the same servlet containers. This is because it differentiates between the two based on the host and port, and a single servlet container is always going to be at a single host/port.

Luckily, the author did a great job of architecting the plugin, and there is only one line where it references the servlet path. So, to configure it to talk to a different servlet in the same container takes only a single change to a single line :)

Without further ado, here’s how to do it.

1. Get the patch (see update below)

First, download and apply the patch I made to acts_as_solr. This patch is for version 0.7, and it makes the changes I described above. You can get the patch here.

All the patch does is add another configuration option to the solr.yml file, then use this option when constructing the URL for the request to Solr.

Note: This patch is distinct from my earlier patch regarding acts_as_solr and single table inheritance. Feel free to apply either one or both, as per your needs.

Update: acts_as_solr was recently updated to 0.8, and this patch was included as one of the changes. So, if you’re using 0.8 or higher, it is unnecessary. The single table inheritance patch was included in 0.8 as well, so both are unnecessary.

2. Install Tomcat

Although the patch should work for any servlet container, the remainder of this HOWTO will deal with Tomcat 5.5 since that is the only servlet container I am really familiar with. For reference, I am using Tomcat 5.5.23, so if all else fails, grab that exact release and see if things work.

I will assume that you already have Tomcat installed and can get to the Tomcat welcome page at http://localhost:8080/. If not, there are plenty of other tutorials explaining these initial stages. Try this one I found with a quick Google search. Come back here once you can see the kittie-cat :)

3. Create the dual solr home directories

Each instance of Solr is going to need its own solr home directory. This directory will contain the configuration options as well as the index data itself. In our case, the configurations will be identical, but we want to maintain distinct indices.

Make two copies the solr directory that came as part of the Solr distribution:

cp -r apache_solr_directory/example/solr /some_path/solr_dev
cp -r apache_solr_directory/example/solr /some_path/solr_prod

The base path for these directories is up to you. Put them somewhere you’ll remember.

You will also need to copy the schema.xml file from acts_as_solr into the two directories as follows:

cp myrailsapp/vendor/plugins/acts_as_solr/schema.xml /some_path/solr_dev/conf
cp myrailsapp/vendor/plugins/acts_as_solr/schema.xml /some_path/solr_prod/conf

4. Create the dual solr webapps

Each instance of Solr is going to need its own web application archive (WAR) in the Tomcat webapps directory. We will name them in such a way as to make their servlet paths distinct.

Move two copies of the Solr war file to the Tomcat webapps directory:

cp apache_solr_directory/dist/apache-solr-XXXX.war /my/tomcat/home/webapps/solr_dev.war
cp apache_solr_directory/dist/apache-solr-XXXX.war /my/tomcat/home/webapps/solr_prod.war

At this point, we have two copies of the Solr war in the webapps directory, each with a distinct name. This will allow Tomcat to create two separate instances, each with its own path.

Note: At this point, you will not be able to get to the servlets if you start up Tomcat, so don’t bother testing yet. Do the next step first.

5. Create the Tomcat context files

Each Solr instance needs to know how to find its home directory. The Solr wiki describes this in general, but I’ll try to be explicit.

Place the following two context files in the /my/tomcat/home/conf/Catalina/localhost/ directory:

solr_dev.xml

<Context path="solr_dev" docBase="solr_dev.war" debug="0" crossContext="true">
  <Environment name="solr/home" type="java.lang.String"
    value="/some_path/solr_dev" override="false" />
</Context>

solr_prod.xml

<Context path="solr_prod" docBase="solr_prod.war" debug="0" crossContext="true">
  <Environment name="solr/home" type="java.lang.String"
    value="/some_path/solr_prod" override="false" />
</Context>

Just make sure that the two value attributes point to the two copies of the solr directory that you made in step 3.

If you restart Tomcat, you should be able to test your dual installations. There should be a Solr installation at two addresses now: http://localhost:8080/solr_dev/admin and http://localhost:8080/solr_prod/admin.

6. Update the solr.yml config file

If you look closely at the patch, you can see the new servlet_path option that needs to be set. I left the setting to solr for backward compatibility, but we’re going to need to change that.

Here is an example of a solr.yml config file that works with Tomcat/Solr as we have currently set it up.

solr.yml

# Config file for the acts_as_solr plugin.

development:
  host: localhost
  port: 8080
  servlet_path: solr_dev

production:
  host: localhost
  port: 8080
  servlet_path: solr_prod

7. Finished

That’s it! At this point, you have two separate instances of Solr running inside a single Tomcat container. Your development and production indices will remain distinct, and you don’t have to worry about running multiple servlet containers on multiple ports.

5 Comments

  • [...] make the Web a friendlier place for Dungeons & Dragons. « D&D Meetup in Decatur, GA acts_as_solr for development and production in one Tomcat instance [...]

  • Anjan Bacchu says:

    hi there,

    “, and a single servlet container is always going to be at a single host/port.”

    you can get tomcat to run at different ports using a different CATALINA_BASE value. That is how Netbeans uses its embedded tomcat.

    All the same, I get your point and understand that with a minimum of fuss, you’d like to have 2 identical apps (servlets) running on your container :-)

    BR,
    ~A

  • Anjan Bacchu says:

    hi there,

    After posting the comment, I realised that you can setup tomcat to listen to 2 different ports at the same time! Will that work for you ?

    BR,
    ~A

  • Micah says:

    @Anjan:

    The main issue is that acts_as_solr 0.7 differentiates the environment (development or production) based on the hostname and port. So, we need 2 different host/port configurations in order to have 2 separate environments.

    Your idea to have Tomcat listen to 2 ports might work, as long as Tomcat knows that when a Solr request comes in on port X, send it off to Solr servlet A, and when the same request comes in on port Y, send it off to Solr servlet B. I imagine there is probably a way to set that up, but I’m no Tomcat expert, so I wouldn’t even attempt it.

    Thanks for the Tomcat tip, though.

  • [...] started with acts_as_solr acts_as_solr for development and production in one Tomcat instance Optimizing Solr and Rails - Index in the [...]

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in