History:Development/Search server setup: Difference between revisions

From MusicBrainz Wiki
Jump to navigationJump to search
(fix links)
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
The Search Server provides the indexed search functions for MusicBrainz.
=Server setup for developers=


Its source code lives here: https://github.com/metabrainz/search-server
The following instructions are aimed at getting a search server up and running as a developer; I will assume you are installing things into your home directory, and running under your current user account. If you're looking for a more production ready setup follow the instructions in the README included with the search_server.

Some of the instructions below are taken from the search_server README.


==Requirements==
==Requirements==
------------


This server can be run on linux/osx/windows, but these instructions assume
This server can be run on linux/osx/windows, but these instructions assume
that you will be running linux and will be using the tomcat6 server to host
that you will be running linux and will be using the tomcat7 server to host
the servlet.
the servlet.

It also assumes you have created a user called search with a home folder of /home/search


To run this search server you will also need to have a MusicBrainz database
To run this search server you will also need to have a MusicBrainz database
with the core data set loaded. Please see
with the core data set loaded. Please see


https://github.com/metabrainz/musicbrainz-server/blob/master/INSTALL.md
FIXME: link to ngs server install here.


for details on how to setup at least a "database only install". This
for details on how to setup at least a "database only install". This
document assumes that you have completed the setup of this server.
document assumes that you have completed the setup of this server.


You will also need these tools:
You will also need a freedb dump, which you can download from http://www.freedb.org/en/download__database.10.html .


- Java complete with JDK 1.6, not just the JRE
And you will need these tools:
- Apache Tomcat 7

- Maven 2
* Java complete with JDK 1.6, not just the JRE
* Apache Tomcat 6 ( http://tomcat.apache.org )
* Maven version 2 ( http://maven.apache.org/download.html )


In a recent Ubuntu, you can install these with this command:
In a recent Ubuntu, you can install these with this command:


apt-get install openjdk-6-jdk maven2 tomcat6
apt-get install openjdk-6-jdk maven2 tomcat7


Alternatively if you're not on Ubuntu, install the required tools from the following links
Ensure default Java installation is at least 1.6, and that you have the
JDK installed not just the JRE.


- Java complete with JDK 1.6, not just the JRE (http://www.oracle.com/technetwork/java/javase/downloads/index.html)
java -version
- Apache Tomcat 6 ( http://tomcat.apache.org )
- Maven version 2 ( http://maven.apache.org/download.html )


If you're not on Ubuntu, you can get an official version from http://java.sun.com/javase/downloads and setup Java Home directory in your profile (not needed on ubuntu):
setup Java Home directory in your profile (not needed on ubuntu):


export JAVA_HOME="/usr/bin/java"
export JAVA_HOME="/usr/bin/java"


To build you need to install Maven, if you haven't already got it. Download it from http://maven.apache.org/download.html .


==Check out and build server code==
==Download the search server and schemas==


You will obviously need the search server, but you also need the musicbrainz schema files:
Check out the mmd-schema and search-server repositories using Git as follows:
git clone https://github.com/metabrainz/mmd-schema.git
git clone https://bitbucket.org/metabrainz/search-server.git


Build and install the model:
svn co http://svn.musicbrainz.org/mmd-schema/trunk mmd-schema
svn co http://svn.musicbrainz.org/search_server/trunk search_server


cd mmd-schema/brainz-mmd2-jaxb
==Install the schema classes==
mvn install


By default the search server home will be at: /home/search and the indexes will live in /home/search/indexdata.
Enter the brainz-mmd2-jaxb directory in mmd-schema, and run "mvn install".
But the index location can be overridden by editing


... /webapps/ROOT/WEB-INF/web.xml
warp@bo-bomb:~/code/mmd-schema/brainz-mmd2-jaxb$ mvn install


so that the index dir is correct:
You should see it install mmd2-2.0.0.jar in a directory where maven2 will be able to find it later:


<param-value>/home/search/indexdata</param-value>
[INFO] Installing /home/warp/code/mmd-schema/brainz-mmd2-jaxb/target/mmd2-2.0.0.jar to
/home/warp/.m2/repository/org/musicbrainz/mmd2/mmd2/2.0.0/mmd2-2.0.0.jar


==Build the search server servlet==
Then build the indexer and the search application as follows:


cd search-server
Enter the search_server directory and run "mvn package" to build the servlet.
mvn package


This step will download any required java components and then compile and test the whole server.
You should see a line like this when it's done:


==Development==
[INFO] Building war: /home/warp/code/search_server/servlet/target/searchserver.war


For development environments there is a much simpler deployment possible
==Build the indexes==


mvn install (because jetty will uses the installed index.jar, not work directly from the code like the servlet code)
To build the indexes, the search server will need access to both your MusicBrainz database and the freedb dump. First enter the index/target directory and run "java -Xmx512M -jar index-2.0-SNAPSHOT-jar-with-dependencies.jar -?" to see the options.
cd search-server/servlet
mvn jetty:run


This will start the Jetty servlet container (instead of Tomcat), then just use the underlying url on port 8080
Figure out your database name and login details (if you already have mb_server running you can find them in lib/DBDefs.pm in the mb_server directory), and run the indexer like this (all on one line):


http://localhost:8080/?type=artist&query=fred
warp@bo-bomb:~/code/search_server/index/target$ java -Xmx512M -jar index-2.0-SNAPSHOT-jar-with-dependencies.jar
--indexes-dir /home/warp/indexes --db-name musicbrainz_db --db-user warp --db-password mypassword
--raw-db-name musicbrainz_db_raw --raw-db-user warp --raw-db-password mypassword
--freedb-dump ~/Downloads/freedb-complete-20100201.tar.bz2 --test


Adding '--test' at the end will build a small test index. After you are done with this setup, you may want to re-run this command without the '--test' option to build the full indexes.


==Deploy the Search Server Code==
When it is done you should have the following index directories in the index directory you specified:
Unjar the search server webapp into the ROOT directory under webapps for your tomcat installation. If you installed
Tomcat using apt this will be


/var/lib/tomcat7/webapps
warp@bo-bomb:~/indexes$ ls
annotation_index cdstub_index label_index releasegroup_index work_index
artist_index freedb_index recording_index release_index


If you installed tomcat manually the webapps directory is directly within the folder tomcat has been installed to.
==Install the servlet==


Automatic deployment of search apps didn't work so well in practice, so it is recommended that you
To install the servlet in Apache Tomcat, you obviously need a tomcat install. I downloaded a binary distribution and extracted in ~/opt. If you have java in your path you should be able to start it right away.
stop tomcat before upgrading the search application.


On Ubuntu this will work:
warp@bo-bomb:~/opt$ tar xfz apache-tomcat-6.0.24.tar.gz
warp@bo-bomb:~/opt$ cd apache-tomcat-6.0.24
warp@bo-bomb:~/opt/apache-tomcat-6.0.24$ bin/startup.sh
Using CATALINA_BASE: /home/warp/opt/apache-tomcat-6.0.24
Using CATALINA_HOME: /home/warp/opt/apache-tomcat-6.0.24
Using CATALINA_TMPDIR: /home/warp/opt/apache-tomcat-6.0.24/temp
Using JRE_HOME: /usr
Using CLASSPATH: /home/warp/opt/apache-tomcat-6.0.24/bin/bootstrap.jar
warp@bo-bomb:~/opt$


sudo /etc/init.d/tomcat7 stop
Try connecting to http://localhost:8080, and you should see the tomcat startup page.
cd /var/lib/tomcat6/webapps
rm -rf ROOT/*
cd ROOT
jar -xf <your lucene java src dir>/servlet/target/searchserver.war
sudo /etc/init.d/tomcat7 start


Shut down tomcat again by running "bin/shutdown.sh". To install the search server, enter the "webapps" directory, remove the current ROOT, and copy over the servlet as the new ROOT webapp:


==Build Indexes==
warp@bo-bomb:~/opt/apache-tomcat-6.0.24$ cd webapps/
warp@bo-bomb:~/opt/apache-tomcat-6.0.24/webapps$ ls
docs examples host-manager manager ROOT
warp@bo-bomb:~/opt/apache-tomcat-6.0.24/webapps$ rm -rf ROOT
warp@bo-bomb:~/opt/apache-tomcat-6.0.24/webapps$ cp ~/code/search_server/servlet/target/searchserver.war ROOT.war
warp@bo-bomb:~/opt/apache-tomcat-6.0.24/webapps$ cd ..
warp@bo-bomb:~/opt/apache-tomcat-6.0.24$ bin/startup.sh


Copy the Index building code
You should see a new ROOT directory tree appear under webapps, stop tomcat again to configure the indexes for the search server.


cp /home/search/searchserver/index/target/index-2.0-SNAPSHOT-jar-with-dependencies.jar /home/search
warp@bo-bomb:~/opt/apache-tomcat-6.0.24$ bin/shutdown.sh
warp@bo-bomb:~/opt/apache-tomcat-6.0.24$ $EDITOR webapps/ROOT/WEB-INF/web.xml
warp@bo-bomb:~/opt/apache-tomcat-6.0.24$ bin/startup.sh
warp@bo-bomb:~/opt/apache-tomcat-6.0.24$


Download the latest freedb dump (in tar.bz2 format) from:
In web.xml, you should replace the current index_dir value ("/home/search/indexdata") with the path to the index files, in my case this is "/home/warp/indexdata".


http://www.freedb.org/en/download__database.10.html
If you now query the search server, you should get some results. For example, try this: [http://localhost:8080/ws/2/artist/?query=%22alice%22 http://localhost:8080/ws/2/artist?query=%22alice%22].

The file you download should be a complete dump and should look like this:

freedb-complete-20090901.tar.bz2

Place the downloaded file into /home/search.

Now build indexes with these commands:

cd /home/search
java -Xmx512M -jar index-2.0-SNAPSHOT-jar-with-dependencies.jar --indexes-dir /home/search/indexdata
--freedb-dump /home/search/<freedb tar.bz2 file>

This will build all of the indexes using upto 512mb of memory using all of the defaults for connecting
to the database servers. If your database isn't on the same server and named according to the
defaults you will need to give the command line more options. To see the options execute this:

java -Xmx512M -jar index-2.0-SNAPSHOT-jar-with-dependencies.jar --help

Usually you will want to build the freedb index at a different time to the other indexes so you can also
build it separately. If you want to build just the freedb_index, you can specify

--indexes freedb

Building the search indexes will take some time -- even on a fast machine it will still take an hour.
Once indexes are built, ensure that your tomcat instance has the permissions to access your data.
In Ubuntu:

chown -R tomcat7:tomcat7 /home/search

==Update Indexes using the Live Data Feed==

It's possible to update search indexes using replication packets, the same way your slave MusicBrainz database is updated.

You need to edit the updateindex.sh and adapt the SETTINGS section to fit your setup:
vi /home/search/searchserver/updater/updateindex.cfg

Once you're done, you can launch the indexes updating:
/home/search/searchserver/updater/updateindex.sh

You can run this script hourly using cron.

Advanced options:

* You can get more verbose log by using the --verbose parameter:
/home/search/searchserver/updater/updateindex.sh --verbose

* The default settings allow you update most of the indexes excepted recording ones.
If you want to choose specifically the indexes to be updated, either update the INDEXES variable in updateindex.cfg,
or comment the INDEXES variable in updateindex.cfg and run the script using the --indexes args:
/home/search/searchserver/updater/updateindex.sh --indexes artist,label

* A lock file is created to ensure you're not running concurrently the script.
You can provide the lock file by setting the LOCK_FILE env variable. This can be useful if you can to update concurrently different indexes:
LOCK_FILE=/tmp/lock_updating_release /home/search/searchserver/updater/updateindex.sh --indexes release
LOCK_FILE=/tmp/lock_updating_label /home/search/searchserver/updater/updateindex.sh --indexes label


==Tuning Tomcat==
==Tuning Tomcat==


Ubuntu enables the Java security manager by default, this will prevent the search server working.
If you followed the instructions so far, you should have a running search server. The README included with the search server includes some hints about tuning tomcat, you may want to follow those. For example, you may want to force Tomcat to expect UTF-8 on the file-system and in the URL, please see the README for more information.
For Ubuntu, set TOMCAT6_SECURITY in /etc/default/tomcat7 to no:

# Use the Java security manager? (yes/no)
TOMCAT6_SECURITY=no

Next, configure Tomcat with the max heap memory you want to use for Search Server. This is
very important because by default it will only use a maximum of 32MB, adding
the following to your profile sets up Tomcat with 512MB. This is the optimum amount to use if you can afford it, do not
allocate more than this because search can use the unallocated memory to cache the indexes.

Also configure Tomcat to run with file encoding UTF-8, otherwise it will use the default which
will vary from platform to platform. To accomplish both, set the JAVA_OPTS like this:

export JAVA_OPTS="-Xms512M -Xmx512M -Dfile.encoding=UTF-8"

In Ubuntu, you can change JAVA_OPTS in /etc/default/tomcat7 -- you do not need to set an environment var.

Next, specify that Tomcat expects URIEncodings in UTF-8 format so they are decoded correctly.
In server.xml (in ubuntu thats in /etc/tomcat7/server.xml) add the URIEncoding parameter to this
line:

<Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443">

So it nows looks like:

<Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8">

By default Tomcat runs on port 8080. To switch it to port 80, edit the Connector line in server.xml again:

<Connector port="80" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8">

Finally, to enable a threadpool so that Tomcat can use multiple processors/cores, uncomment/add this line
in server.xml

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4" minThreads="8"/>

and add the executor attribute to the Connector:

<Connector port="80" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443"
URIEncoding="UTF-8" executor="tomcatThreadPool">

We opted to create one thread per available core our setup, which in this case is 8.

Now start (or restart) Tomcat to have the latest settings take effect.


==Testing the Search Server==

You can now lookup search for resources with a url like:

http://localhost/ws/2/artist/?query=fred

For compatibility with Webservice version 1 use:

http://localhost/ws/1/artist/?query=fred

You can also get the results in JSON format using a url like:

http://localhost/ws/2/artist/?query=fred&fmt=json


==Server administration==


If you have built new indexes you can inform the search server of the new indexes without restarting it using the init
command. Setting init to mmap will memory map the indexes (this now the default),

http://localhost:8080/?init=mmap

Or you can revert to the older filesystem based format using

http://localhost:8080/?init=nfio

You can enable the rate limiter with

http://localhost:8080/?rate=true

or disable it with

http://localhost:8080/?rate=false

The reload command is intended for use when an existing index has been updated rather than replaced, this is not currently used
http://localhost:8080/?reload=true

All the above commands can only be performed on the local search machine otherwise a 403 error will be returned.

The number of queries done against any index since the servlet was started can be obtained using the count parameter
and index name

http://localhost:8080/?count=artist


==Troubleshooting==

The search url is a mapping you can also use the underlying url directly as follows:

http://localhost:8080/?type=artist&query=fred


[[Category:Development]]

Revision as of 19:08, 9 March 2017

The Search Server provides the indexed search functions for MusicBrainz.

Its source code lives here: https://github.com/metabrainz/search-server

Requirements


This server can be run on linux/osx/windows, but these instructions assume that you will be running linux and will be using the tomcat7 server to host the servlet.

It also assumes you have created a user called search with a home folder of /home/search

To run this search server you will also need to have a MusicBrainz database with the core data set loaded. Please see

   https://github.com/metabrainz/musicbrainz-server/blob/master/INSTALL.md

for details on how to setup at least a "database only install". This document assumes that you have completed the setup of this server.

You will also need these tools:

   - Java complete with JDK 1.6, not just the JRE
   - Apache Tomcat 7
   - Maven 2

In a recent Ubuntu, you can install these with this command:

   apt-get install openjdk-6-jdk maven2 tomcat7

Alternatively if you're not on Ubuntu, install the required tools from the following links

   - Java complete with JDK 1.6, not just the JRE (http://www.oracle.com/technetwork/java/javase/downloads/index.html)
   - Apache Tomcat 6    ( http://tomcat.apache.org )
   - Maven version 2    ( http://maven.apache.org/download.html )

setup Java Home directory in your profile (not needed on ubuntu):

   export JAVA_HOME="/usr/bin/java"


Check out and build server code

Check out the mmd-schema and search-server repositories using Git as follows:

   git clone https://github.com/metabrainz/mmd-schema.git
   git clone https://bitbucket.org/metabrainz/search-server.git

Build and install the model:

   cd mmd-schema/brainz-mmd2-jaxb
   mvn install

By default the search server home will be at: /home/search and the indexes will live in /home/search/indexdata. But the index location can be overridden by editing

   ... /webapps/ROOT/WEB-INF/web.xml

so that the index dir is correct:

   <param-value>/home/search/indexdata</param-value>

Then build the indexer and the search application as follows:

   cd search-server
   mvn package

This step will download any required java components and then compile and test the whole server.

Development

For development environments there is a much simpler deployment possible

   mvn install  (because jetty will uses the installed index.jar, not work directly from the code like the servlet code)
   cd search-server/servlet
   mvn jetty:run

This will start the Jetty servlet container (instead of Tomcat), then just use the underlying url on port 8080

    http://localhost:8080/?type=artist&query=fred


Deploy the Search Server Code

Unjar the search server webapp into the ROOT directory under webapps for your tomcat installation. If you installed Tomcat using apt this will be

   /var/lib/tomcat7/webapps

If you installed tomcat manually the webapps directory is directly within the folder tomcat has been installed to.

Automatic deployment of search apps didn't work so well in practice, so it is recommended that you stop tomcat before upgrading the search application.

On Ubuntu this will work:

   sudo /etc/init.d/tomcat7 stop
   cd /var/lib/tomcat6/webapps
   rm -rf ROOT/*
   cd ROOT
   jar -xf <your lucene java src dir>/servlet/target/searchserver.war
   sudo /etc/init.d/tomcat7 start


Build Indexes

Copy the Index building code

   cp /home/search/searchserver/index/target/index-2.0-SNAPSHOT-jar-with-dependencies.jar /home/search

Download the latest freedb dump (in tar.bz2 format) from:

   http://www.freedb.org/en/download__database.10.html

The file you download should be a complete dump and should look like this:

   freedb-complete-20090901.tar.bz2

Place the downloaded file into /home/search.

Now build indexes with these commands:

   cd /home/search
   java -Xmx512M -jar index-2.0-SNAPSHOT-jar-with-dependencies.jar --indexes-dir /home/search/indexdata
        --freedb-dump /home/search/<freedb tar.bz2 file>

This will build all of the indexes using upto 512mb of memory using all of the defaults for connecting to the database servers. If your database isn't on the same server and named according to the defaults you will need to give the command line more options. To see the options execute this:

   java -Xmx512M -jar index-2.0-SNAPSHOT-jar-with-dependencies.jar --help

Usually you will want to build the freedb index at a different time to the other indexes so you can also build it separately. If you want to build just the freedb_index, you can specify

   --indexes freedb

Building the search indexes will take some time -- even on a fast machine it will still take an hour. Once indexes are built, ensure that your tomcat instance has the permissions to access your data. In Ubuntu:

   chown -R tomcat7:tomcat7 /home/search

Update Indexes using the Live Data Feed

It's possible to update search indexes using replication packets, the same way your slave MusicBrainz database is updated.

You need to edit the updateindex.sh and adapt the SETTINGS section to fit your setup: vi /home/search/searchserver/updater/updateindex.cfg

Once you're done, you can launch the indexes updating: /home/search/searchserver/updater/updateindex.sh

You can run this script hourly using cron.

Advanced options:

  • You can get more verbose log by using the --verbose parameter:

/home/search/searchserver/updater/updateindex.sh --verbose

  • The default settings allow you update most of the indexes excepted recording ones.

If you want to choose specifically the indexes to be updated, either update the INDEXES variable in updateindex.cfg, or comment the INDEXES variable in updateindex.cfg and run the script using the --indexes args: /home/search/searchserver/updater/updateindex.sh --indexes artist,label

  • A lock file is created to ensure you're not running concurrently the script.

You can provide the lock file by setting the LOCK_FILE env variable. This can be useful if you can to update concurrently different indexes: LOCK_FILE=/tmp/lock_updating_release /home/search/searchserver/updater/updateindex.sh --indexes release LOCK_FILE=/tmp/lock_updating_label /home/search/searchserver/updater/updateindex.sh --indexes label

Tuning Tomcat

Ubuntu enables the Java security manager by default, this will prevent the search server working. For Ubuntu, set TOMCAT6_SECURITY in /etc/default/tomcat7 to no:

   # Use the Java security manager? (yes/no)
   TOMCAT6_SECURITY=no

Next, configure Tomcat with the max heap memory you want to use for Search Server. This is very important because by default it will only use a maximum of 32MB, adding the following to your profile sets up Tomcat with 512MB. This is the optimum amount to use if you can afford it, do not allocate more than this because search can use the unallocated memory to cache the indexes.

Also configure Tomcat to run with file encoding UTF-8, otherwise it will use the default which will vary from platform to platform. To accomplish both, set the JAVA_OPTS like this:

   export JAVA_OPTS="-Xms512M -Xmx512M -Dfile.encoding=UTF-8"

In Ubuntu, you can change JAVA_OPTS in /etc/default/tomcat7 -- you do not need to set an environment var.

Next, specify that Tomcat expects URIEncodings in UTF-8 format so they are decoded correctly. In server.xml (in ubuntu thats in /etc/tomcat7/server.xml) add the URIEncoding parameter to this line:

   <Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443">

So it nows looks like:

   <Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8">

By default Tomcat runs on port 8080. To switch it to port 80, edit the Connector line in server.xml again:

   <Connector port="80" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8">

Finally, to enable a threadpool so that Tomcat can use multiple processors/cores, uncomment/add this line in server.xml

   <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
             maxThreads="150" minSpareThreads="4" minThreads="8"/>

and add the executor attribute to the Connector:

   <Connector port="80" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" 
              URIEncoding="UTF-8" executor="tomcatThreadPool">

We opted to create one thread per available core our setup, which in this case is 8.

Now start (or restart) Tomcat to have the latest settings take effect.


Testing the Search Server

You can now lookup search for resources with a url like:

   http://localhost/ws/2/artist/?query=fred

For compatibility with Webservice version 1 use:

   http://localhost/ws/1/artist/?query=fred

You can also get the results in JSON format using a url like:

   http://localhost/ws/2/artist/?query=fred&fmt=json


Server administration

If you have built new indexes you can inform the search server of the new indexes without restarting it using the init command. Setting init to mmap will memory map the indexes (this now the default),

   http://localhost:8080/?init=mmap

Or you can revert to the older filesystem based format using

   http://localhost:8080/?init=nfio

You can enable the rate limiter with

   http://localhost:8080/?rate=true

or disable it with

   http://localhost:8080/?rate=false

The reload command is intended for use when an existing index has been updated rather than replaced, this is not currently used

   http://localhost:8080/?reload=true

All the above commands can only be performed on the local search machine otherwise a 403 error will be returned.

The number of queries done against any index since the servlet was started can be obtained using the count parameter and index name

   http://localhost:8080/?count=artist


Troubleshooting

The search url is a mapping you can also use the underlying url directly as follows:

    http://localhost:8080/?type=artist&query=fred