source: gs3-extensions/solr/trunk/src/bin/script/solr_passes.pl@ 24446

Last change on this file since 24446 was 24446, checked in by davidb, 13 years ago

Start of Solr extension for Greenstone3

File size: 11.1 KB
RevLine 
[24446]1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# solr_passes.pl -- perl wrapper, akin to mgpp_passes, for Solr
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# Heavily based on lucene_passes.pl, but does not need a SolrWrapper.jar
29# style solution as Solr has its own XML syntax:
30#
31# http://wiki.apache.org/solr/UpdateXmlMessages
32#
33# This syntax is rather similar to what we already use, so the
34# main task of monitor_xml() is to translate the XML syntax Greenstone uses
35# into that needed by the solr server.
36
37my $jetty_stop_key="greenstone-solr";
38
39BEGIN {
40 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
41 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
42 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
43 die "GEXT_SOLR not set\n" unless defined $ENV{'GEXT_SOLR'};
44}
45
46use strict;
47use util;
48
49
50# Not quite OO, but close enough for now
51#
52my $self = { 'full_server_jar' => undef,
53## 'full_post_jar' = undef,
54## 'full_index_propfile' = undef,
55 'jetty_explicitly_started' => undef
56 };
57
58
59
60sub locate_file
61{
62 my ($search_path,$suffix) = @_;
63
64 foreach my $sp (@$search_path) {
65 my $full_path = &util::filename_cat($sp,$suffix);
66
67 if (-f $full_path) {
68 return $full_path;
69 }
70 }
71
72 # if get to here, then failed to find match
73
74 print STDERR "Error: Failed to find '$suffix'\n";
75 print STDERR " Looked in: ", join(", ", @$search_path), "\n";
76 exit -1;
77}
78
79sub start_solr_server
80{
81 my ($search_path) = @_;
82
83 my $solr_home = $ENV{'GEXT_SOLR'};
84 my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'};
85
86 chdir($solr_home);
87
88 my $solr_etc = &util::filename_cat($solr_home,"etc");
89
90 my $server_props = "-DSTOP.PORT=$jetty_stop_port";
91 $server_props .= " -DSTOP.KEY=$jetty_stop_key";
92 $server_props .= " -Dsolr.solr.home=$solr_etc";
93
94 my $server_jar = &util::filename_cat("lib","java","solr-jetty-server.jar");
95 my $full_server_jar = locate_file($search_path,$server_jar);
96 $self->{'full_server_jar'} = $full_server_jar;
97
98 my $server_java_cmd = "java $server_props -jar \"$full_server_jar\"";
99# if ($ENV{'GSDLOS'} eq "windows") {
100# $server_java_cmd = "start $server_java_cmd";
101# }
102# else {
103# $server_java_cmd .= " &";
104# }
105
106
107 if (open(SIN,"$server_java_cmd 2>&1 |")) {
108
109 my $line;
110 while (defined($line=<SIN>)) {
111 # Scan through output until you see a line like:
112 # 2011-08-22 .. :INFO::Started [email protected]:8983
113 # which signifies that the server has started up and is
114 # "ready and listening"
115
116 last if ($line =~ m/INFO::Started SocketConnector/);
117 }
118
119 print STDERR "Jetty server ready and listening for connections\n";
120
121 # now we know the server is ready to accept connections, fork a
122 # child process that continues to listen to the output and
123 # prints out any lines that are not INFO lines
124
125 if (fork()==0) {
126 # child process
127
128 my $line;
129 while (defined ($line = <SIN>)) {
130 next if ($line =~ m/^INFO:/);
131 next if ($line =~ m/^[0-9 :-]*INFO::/);
132 next if ($line =~ m/^\d{2}\/\d{2}\/\d{4}\s+/);
133 }
134 close(SIN);
135
136 # And now stop nicely
137 exit 0;
138 }
139 }
140 else {
141 print STDERR "Error: failed to start solr-jetty-server\n";
142 print STDERR "!$\n";
143 exit -1;
144 }
145
146 # If get to here then server started (and ready and listening)
147 # *and* we are the parent process of the fork()
148
149 $self->{'jetty_explicitly_started'} = 1;
150
151}
152
153
154
155sub stop_solr_server
156{
157 my $full_server_jar = $self->{'full_server_jar'};
158 my $jetty_stop_port = $ENV{'JETTY_STOP_PORT'};
159
160 my $server_props = "-DSTOP.PORT=$jetty_stop_port -DSTOP.KEY=$jetty_stop_key";
161 my $server_java_cmd = "java $server_props -jar \"$full_server_jar\" --stop";
162
163 my $server_status = system($server_java_cmd);
164
165 if ($server_status!=0) {
166 print STDERR "Error: failed to stop solr-jetty-server\n";
167 print STDERR "!$\n";
168 exit -1;
169 }
170 else {
171 wait(); # let the child process finish
172 }
173}
174
175
176sub open_java_solr
177{
178 my ($collect, $doc_tag_level,$full_builddir,$indexdir,$removeold) = @_;
179
180
181 # if removeold set, then delete the curring $full_builddir
182 if ($removeold) {
183 my $full_indexdir = &util::filename_cat($full_builddir,$indexdir);
184 &util::rm_r($full_indexdir);
185 }
186
187# No longer used, as solr now run as two cores per collection (-Doc and -Sec)
188#
189# # Set up an 'index.properties' file in $full_builddir, with the line
190# # index=$indexdir
191# $full_index_propfile = &util::filename_cat($full_builddir,"index.properties");
192
193# if (open(IPOUT,">$full_index_propfile")) {
194# print IPOUT "index=$indexdir\n";
195# close(IPOUT);
196# }
197# else {
198# print STDERR "Failed to create $full_index_propfile\n";
199# print STDERR "!$\n";
200# exit -2;
201# }
202
203
204
205 my $search_path = [];
206
207 push(@$search_path,$ENV{'GSDLCOLLECTDIR'}) if defined $ENV{'GSDLCOLLECTDIR'};
208 push(@$search_path,$ENV{'GSDLHOME'}) if defined $ENV{'GSDLHOME'};
209 push(@$search_path,$ENV{'GEXT_SOLR'}) if defined $ENV{'GEXT_SOLR'};
210
211
212 # The following returns once Jetty has generated its
213 # "reading and listening" line
214 #
215 start_solr_server($search_path);
216
217
218 # Now run the solr-post command
219
220 chdir($ENV{'GEXT_SOLR'});
221
222
223 my $post_jar = &util::filename_cat("lib","java","solr-post.jar");
224 my $full_post_jar = locate_file($search_path,$post_jar);
225## $self->{'full_post_jar'} = $full_post_jar;
226
227 my $jetty_server_port = $ENV{'SOLR_JETTY_PORT'};
228
229 # Now run solr-post command
230 my $post_props = "-Durl=http://localhost:$jetty_server_port/solr/$collect-$doc_tag_level/update";
231 $post_props .= " -Ddata=stdin";
232 $post_props .= " -Dcommit=yes";
233
234 my $post_java_cmd = "java $post_props -jar \"$full_post_jar\"";
235
236 print STDERR "**** post cmd = $post_java_cmd\n";
237
238 open (PIPEOUT, "| $post_java_cmd")
239 || die "Error in solr_passes.pl: Failed to run $post_java_cmd\n!$\n";
240}
241
242
243
244sub close_java_solr
245{
246 # closing the pipe has the effect of shutting down solr-post.jar
247 close(PIPEOUT);
248
249 if ($self->{'jetty_explicitly_started'}) {
250 stop_solr_server();
251 }
252
253# No longer used
254# # $full_index_propfile is set up as a global variable so it can be shared
255# # between open_java_solr() and here
256# &util::rm($full_index_propfile);
257}
258
259
260
261
262
263sub save_xml_doc
264{
265 # This is identical to the one in lucene_passes.pl, and should be
266 # moved in to a package and shared ####
267
268 my ($full_textdir,$output_filename,$doc_xml) = @_;
269
270 my $dir_sep = &util::get_os_dirsep();
271
272 my $full_output_filename = &util::filename_cat($full_textdir,$output_filename);
273 my ($full_output_dir) = ($full_output_filename =~ m/^(.*$dir_sep)/x);
274 &util::mk_all_dir($full_output_dir);
275
276 open(DOCOUT,">$full_output_filename")
277 || die "Unable to open $full_output_filename";
278
279 print DOCOUT $doc_xml;
280 close(DOCOUT);
281
282 # What this the purpose of the following? ####
283 my @secs = ($doc_xml =~ m/<Sec\s+gs2:id="\d+"\s*>.*?<\/Sec>/sg);
284}
285
286
287sub compress_xml_doc
288{
289 # This is identical to the one in lucene_passes.pl, and should be
290 # moved in to a package and shared ####
291
292 my ($full_textdir,$output_filename) = @_;
293
294 my $full_output_filename
295 = &util::filename_cat($full_textdir,$output_filename);
296
297 # Greenstone ships with gzip for Windows
298 `gzip $full_output_filename`;
299}
300
301
302sub monitor_xml_stream
303{
304 # based on lucene's monitor_xml_stream, but simplified
305 # as only now used when in "text" mode
306
307 my ($full_textdir) = @_;
308
309 my $doc_xml = "";
310 my $output_filename = "";
311
312 my $line;
313 while (defined ($line = <STDIN>)) {
314
315 $doc_xml .= $line;
316
317 if ($line =~ m/^<Doc.+file=\"(.*?)\".*>$/) {
318 $output_filename = $1;
319 }
320
321 if ($line =~ m/^<\/Doc>$/) {
322 save_xml_doc($full_textdir,$output_filename,$doc_xml);
323
324 # Compress file
325 #
326 # The compress option was taken out for efficiency
327 # reasons. Consider putting it back in but making it a
328 # switch so a collection builder can decide for themselves on a
329 # case by case basis if they want to save on diskspace, but have
330 # the overhead of uncompressing at runtime
331
332### compress_xml_doc($full_textdir,$output_filename);
333
334 $doc_xml = "";
335 $output_filename = "";
336 }
337 }
338}
339
340
341sub pass_on_xml_stream
342{
343 my $line;
344 while (defined ($line = <STDIN>)) {
345 print PIPEOUT $line;
346 }
347}
348
349
350
351
352# /** This checks the arguments on the command line, filters the
353# * unknown command line arguments and then calls the open_java_solr
354# * function to begin processing.
355# */
356sub main
357{
358 my (@argv) = @_;
359 my $argc = scalar(@argv);
360
361 my $removeold = 0;
362 my @filtered_argv = ();
363
364 my $i = 0;
365 while ($i<$argc) {
366 if ($argv[$i] =~ m/^\-(.*)$/) {
367
368 my $option = $1;
369
370 # -removeold causes the existing index to be overwritten
371 if ($option eq "removeold") {
372 print STDERR "\n-removeold set (new index will be created)\n";
373 $removeold = 1;
374 }
375 # -verbosity <num>
376 elsif ($option eq "verbosity") {
377 $i++;
378 if ($i<$argc)
379 {
380 # solr indexing has no support for verbosity
381 # => parse to be compatible with calling program, but supress it
382 # for solr-post.jar
383 }
384 }
385 else {
386 print STDERR "Unrecognised minus option: -$option\n";
387 }
388 }
389 else {
390 push(@filtered_argv,$argv[$i]);
391 }
392 $i++;
393 }
394
395 my $filtered_argc = scalar(@filtered_argv);
396
397 if ($filtered_argc < 5) {
398 print STDERR "Usage: solr_passes.pl [-removeold|-verbosity num] collect \"text\"|\"index\" doc-tag-level build-dir index-name\n";
399 exit 1;
400 }
401
402 my $collect = $filtered_argv[0];
403 my $mode = $filtered_argv[1];
404 my $doc_tag_level = $filtered_argv[2];
405 my $full_builddir = $filtered_argv[3];
406 my $indexdir = $filtered_argv[4];
407
408 # We only need the Solr handle opened if we are indexing the
409 # documents, not if we are just storing the text
410 if ($mode eq "index") {
411 open_java_solr($collect, $doc_tag_level, $full_builddir, $indexdir, $removeold);
412 }
413
414 if ($mode eq "text") {
415 print STDERR "Monitoring for input!\n";
416 my $full_textdir = &util::filename_cat($full_builddir,"text");
417 monitor_xml_stream($full_textdir);
418 }
419 else {
420 print STDERR "Streaming input onto solr server!\n";
421 pass_on_xml_stream();
422 }
423
424
425 if ($mode eq "index") {
426 close_java_solr();
427 }
428}
429
430
431&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.