Solr 9.0をソースコードからビルドしてIntelliJでデバッグする


ソースコードをclone

ソースコードをcloneしてくる。
https://github.com/apache/solr

IntelliJでCloneしたソースコードをプロジェクトとして開いておく。

ビルド

READMEに書いてある通りビルドコマンドを実行。

./gradlew assemble

実行ファイルなどはルートディレクトリから見て solr/packaging/build/solr-9.0.0-SNAPSHOT/にできる。
ここによく見かけるbinディレクトリとかがある。

$ ls
LICENSE         buildSrc        gradle.properties   settings.gradle
README.md       dev-docs        gradlew         solr
build           dev-tools       gradlew.bat     versions.lock
build.gradle        gradle          help            versions.props
$ ls solr/packaging/build/solr-9.0.0-SNAPSHOT/
CHANGES.txt NOTICE.txt  bin     dist        docs        licenses
LICENSE.txt README.md   contrib     docker      example     server

debugモードで起動

-aオプションでJVMのオプションを追加で指定できるのでそれで起動する。
実際にSolrが起動するのは次の手順で説明するIntelliJから虫マークを押したタイミングで、solr startしてから180秒以内にそれをしないと多分起動プロセスが中断するので注意。

./bin/solr start -a "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=4044"

IntelliJでCloneしたソースコード

Edit Configration→Add New Configrationでデバッグ用のコンフィグを追加

↓の虫のアイコンを押せばSolrが起動する

コアとコレクションを作る

以下からSolr Adminにアクセス
http://localhost:8983/solr/#/

以下からコアを追加する。
今回はコア名new_coreで追加。

ただし、初回実行時は以下のようなエラーが出る。

Error CREATEing SolrCore 'new_core': Unable to create core [new_core] Caused by: Can't find resource 'solrconfig.xml'

以下のディレクトリにコア名と同じディレクトリが作成されている。

$ ls server/solr/
README.md   configsets  new_core    solr.xml    zoo.cfg

server/solr/configsets/_default/ にsolrconfigなどが用意されているのでnew_coreにまるごとコピーしてくる。

$ cd server/solr/new_core/
$ mkdir data 
$ cp -r ../configsets/_default/ .

ドキュメントを追加

UIからid=1というドキュメントを追加

検索できることを確認

$curl http://localhost:8983/solr/new_core/select?q=*%3A*
{
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "q":"*:*",
      "q.op":"OR"}},
  "response":{"numFound":1,"start":0,"numFoundExact":true,"docs":[
      {
        "id":"1",
        "_version_":1700292289915518976}]
  }}

ブレークポイントをおいてデバッグしてみる

検索処理を行うコードにブレークポイント置いてみる
https://github.com/apache/solr/blob/f36262d7426b52c07a4fe12ebb21919ac2447006/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java#L178

検索リクエストを実行

curl http://localhost:8983/solr/new_core/select?q.op=OR&q=*%3A*

IntelliJでちゃんとブレークポイントで止まることが確認できる。