source: gs3-extensions/solr/trunk/src/perllib/solrserver.pm@ 33315

Last change on this file since 33315 was 33315, checked in by ak19, 5 years ago
  1. Bugfix to issue discovered on windows: when the GS3 server isn't running, and therefore the solr servlet isn't available either, wget testing the solr servlet URL on Windows returned 'failed: Bad file descriptor' msg, which wasn't one of the cases that the solrserver.pm was monitoring for and responding to. It only looked for 'ERROR ' and 'failed: Connection refused' messages. Added in a check for the bad file descriptor message, but also for any 'failed:' (case insensitive) messages in general, in case in other situations or on other OS we get a slightly different message. 2. When the server isn't running, this is not a fatal error as the solrserver will simply manually start tomcat to process the solr collection. So in such a case we don't want to print something as drastic as WGET_SERVICE got an error, as we hadn't been doing before. Instead on the 'failed:' messages, it prints the message and then adds that this probably means the GS3 server isn't running.
File size: 14.4 KB
Line 
1###########################################################################
2#
3# solrserver.pm -- class for starting and stopping the Solr with the
4# GS3 tomcat server.
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 1999 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27
28package solrserver;
29
30use strict;
31#no strict 'refs';
32
33use solrutil;
34
35sub new {
36 my $class = shift(@_);
37 my ($build_dir) = @_;
38
39 my $self = { 'build_dir' => $build_dir };
40
41 my $search_path = &solrutil::get_search_path();
42
43 $self->{'server_explicitly_started'} = undef;
44
45 # set SOLR_HOST and SOLR_PORT env vars (tomcat host and port, if not using jetty)
46 # by calling ant get-default-solr-servlet if possible. Else fallback on whatever the existing env vars are.
47 # tomcat host and port would have been set up in the env as SOLR_HOST and SOLR_PORT
48 # In case someone changed the tomcat host/port, we want to update the solr server variables too
49 my $solr_url = &solrutil::get_solr_servlet_url();
50 # get the url parts, though we won't be using most of them
51 my ($protocol, $server_host, $server_port, $servlet_name) = &solrutil::get_solr_url_parts($solr_url);
52
53 # set the solr server env vars to what was discovered, so that any other old perl code
54 # dependent on these env vars will have any changes propagated.
55 # (All perl code referencing these env vars should already be updated, but still...)
56 $ENV{'SOLR_HOST'} = $server_host;
57 $ENV{'SOLR_PORT'} = $server_port;
58
59 $self->{'base-url'} = $solr_url; # e.g. of the form http://localhost:8383/solr
60 $self->{'admin-url'} = "$solr_url/admin/cores";
61
62 return bless $self, $class;
63}
64
65sub get_solr_base_url {
66 my $self = shift (@_);
67 return $self->{'base-url'};
68}
69
70sub _wget_service
71{
72 my $self = shift (@_);
73 my ($output_format,$url,$cgi_get_args) = @_;
74
75 my $full_url = $url;
76
77 $url .= "?$cgi_get_args" if (defined $cgi_get_args);
78
79 print STDERR "\n\n**** _wget_service SOLR WEB URL: $url\n\n";
80
81 # the wget binary is dependent on the gnomelib_env (particularly lib/libiconv2.dylib) being set, particularly on Mac Lion binaries (android too?)
82 &util::set_gnomelib_env(); # this will set the gnomelib env once for each subshell launched, by first checking if GEXTGNOME is not already set
83
84 my $cmd = "wget -O - \"$url\" 2>&1";
85
86 my $preamble_output = "";
87 my $xml_output = "";
88 my $error_output = undef;
89 my $is_error = 0;
90
91 my $in_preamble = ($output_format eq "xml") ? 1 : 0;
92
93## print STDERR "**** wgetcmd = \n $cmd\n";
94
95 if (open(WIN,"$cmd |")) {
96
97 my $line;
98 while (defined ($line=<WIN>)) {
99
100 if ($line =~ m/ERROR \d+:/) {
101 chomp $line;
102 $error_output = $line;
103 $is_error = 1;
104 last;
105 }
106 elsif ($line =~ m/failed: (Connection refused|Bad file descriptor)/ || $line =~ m/failed:/i) {
107 # When the server wasn't running on windows, also got "failed: Bad file descriptor".
108 # But making more robust by adding support for any "failed:..." wget response msg
109 chomp $line;
110 $error_output = $line;
111 last;
112 }
113 elsif ($in_preamble) {
114 if ($line =~ m/<.*>/) {
115 $in_preamble = 0;
116 }
117 else {
118 $preamble_output .= $line;
119 }
120 }
121
122 if (! $in_preamble) {
123 $xml_output .= $line;
124 }
125 }
126 close(WIN);
127
128 }
129 else {
130 $error_output = "Error: failed to run $cmd\n";
131 $error_output .= " $!\n";
132 }
133
134 if(defined $error_output) {
135 if($is_error) {
136 print STDERR "\n\n**** WGET_SERVICE got an error: $error_output\n\n";
137 } else {
138 print STDERR "\n\n**** WGET_SERVICE got: $error_output. (GS3 server likely not running.)\n\n";
139 }
140 }
141
142 my $output = { 'url' => $full_url,
143 'preamble' => $preamble_output,
144 'output' => $xml_output,
145 'error' => $error_output };
146
147 return $output;
148}
149
150
151sub _base_service
152{
153 my $self = shift (@_);
154 my ($cgi_get_args) = @_;
155
156 my $base_url = $self->{'base-url'};
157
158 return $self->_wget_service("html",$base_url,$cgi_get_args);
159}
160
161sub _admin_service
162{
163 my $self = shift (@_);
164 my ($cgi_get_args) = @_;
165
166 my $admin_url = $self->{'admin-url'};
167
168 return $self->_wget_service("xml",$admin_url,$cgi_get_args);
169}
170
171
172sub server_running
173{
174 my $self = shift @_;
175
176 my $output = $self->_base_service();
177
178 my $have_error = defined $output->{'error'};
179
180 my $running = ($have_error) ? 0 : 1;
181
182 return $running;
183}
184
185
186sub admin_ping_core
187{
188 my $self = shift @_;
189 my ($core) = @_;
190
191 my $cgi_get_args = "action=STATUS&core=$core";
192
193 my $ping_status = 1;
194
195 my $output = $self->_admin_service($cgi_get_args);
196
197 if (defined $output->{'error'}) {
198 # severe error, such as failing to connect to the server
199 $ping_status = 0;
200
201 my $url = $output->{'url'};
202 my $preamble = $output->{'preamble'};
203 my $error = $output->{'error'};
204
205 print STDERR "----\n";
206 print STDERR "Error: Failed to get XML response from:\n";
207 print STDERR " $url\n";
208 print STDERR "Output was:\n";
209 print STDERR $preamble if ($preamble ne "");
210 print STDERR "$error\n";
211 print STDERR "----\n";
212 }
213 else {
214
215 # If the collection doesn't exist yet, then there will be
216 # an empty element of the form:
217 # <lst name="collect-doc"/>
218 # where 'collect' is the actual name of the collection,
219 # such as demo
220
221 my $xml_output = $output->{'output'};
222
223 my $empty_element="<lst\\s+name=\"$core\"\\s*\\/>";
224
225 $ping_status = !($xml_output =~ m/$empty_element/s);
226 }
227
228 return $ping_status;
229}
230
231sub filtered_copy
232{
233 my $self = shift @_;
234
235 my $src_file = shift @_;
236 my $dst_file = shift @_;
237 my $re_substitutions = shift @_;
238
239 # $re_substitutions is a hashmap of the form: [re_key] => subst_str
240
241 my $content = "";
242
243 if (open(FIN,'<:utf8',$src_file)) {
244
245 my $line;
246 while (defined($line=<FIN>)) {
247 $content .= $line;
248 }
249 }
250
251 close(FIN);
252
253 # perform RE string substitutions
254 foreach my $re_key (keys %$re_substitutions) {
255
256 my $subst_str = $re_substitutions->{$re_key};
257
258 # Perform substitution of the form:
259 # $content =~ s/$re_key/$subst_str/g;
260 # but allow allow separator char (default '/')
261 # and flags (default 'g') to be parameterized
262
263 $content =~ s/$re_key/$subst_str/g;
264 }
265
266 if (open(FOUT, '>:utf8', $dst_file)) {
267 print FOUT $content;
268 close(FOUT);
269 }
270 else {
271 print STDERR "Error: Failed to open file '$dst_file' for writing.\n$!\n";
272 }
273}
274
275sub solr_xml_to_solr_xml_in
276{
277 my $self = shift @_;
278 my ($solr_xml_dir) = @_;
279
280 my $gsdl3home = $ENV{'GSDL3HOME'};
281
282 if (!defined $solr_xml_dir || !-d $solr_xml_dir) {
283 # if not passed in, use stored solr_live_home
284 $solr_xml_dir = $self->{'solr_live_home'};
285 }
286
287 my $solrxml_in = &util::filename_cat($solr_xml_dir, "solr.xml.in");
288 my $solrxml = &util::filename_cat($solr_xml_dir, "solr.xml");
289
290 my $gsdl3home_re = &util::filename_to_regex($gsdl3home);
291
292 my $replacement_map = { qr/$gsdl3home_re/ => "\@gsdl3home\@" };
293
294 $self->filtered_copy($solrxml,$solrxml_in,$replacement_map);
295}
296
297
298sub solr_xml_in_to_solr_xml
299{
300 my $self = shift @_;
301 my ($solr_xml_dir) = @_;
302
303 my $gsdl3home = $ENV{'GSDL3HOME'};
304 if (!defined $solr_xml_dir || !-d $solr_xml_dir) {
305 # if not passed in, use stored solr home
306 $solr_xml_dir = $self->{'solr_live_home'};
307 }
308 my $solrxml_in = &util::filename_cat($solr_xml_dir, "solr.xml.in");
309 my $solrxml = &util::filename_cat($solr_xml_dir, "solr.xml");
310
311 my $gsdl3home_re = &util::filename_to_regex($gsdl3home);
312
313 my $replacement_map = { qr/\@gsdl3home\@/ => $gsdl3home_re };
314
315 $self->filtered_copy($solrxml_in,$solrxml,$replacement_map);
316}
317
318
319# Some of the Solr CoreAdmin API calls available.
320# See http://wiki.apache.org/solr/CoreAdmin
321sub admin_reload_core
322{
323 my $self = shift @_;
324 my ($core) = @_;
325
326 my $cgi_get_args = "action=RELOAD&core=$core";
327
328 $self->_admin_service($cgi_get_args);
329
330}
331
332sub admin_rename_core
333{
334 my $self = shift @_;
335 my ($oldcore, $newcore) = @_;
336
337 my $cgi_get_args = "action=RENAME&core=$oldcore&other=$newcore";
338
339 $self->_admin_service($cgi_get_args);
340
341}
342
343sub admin_swap_core
344{
345 my $self = shift @_;
346 my ($oldcore, $newcore) = @_;
347
348 my $cgi_get_args = "action=SWAP&core=$oldcore&other=$newcore";
349
350 $self->_admin_service($cgi_get_args);
351
352}
353
354# The ALIAS action is not supported in our version of solr (despite it
355# being marked as experimental in the documentation for Core Admin)
356sub admin_alias_core
357{
358 my $self = shift @_;
359 my ($oldcore, $newcore) = @_;
360
361 my $cgi_get_args = "action=ALIAS&core=$oldcore&other=$newcore";
362
363 $self->_admin_service($cgi_get_args);
364
365}
366
367sub admin_create_core
368{
369 my $self = shift @_;
370 my ($core, $data_parent_dir) = @_; # data_parent_dir is optional, can be index_dir. Defaults to builddir if not provided
371
372 my ($ds_idx) = ($core =~ m/^.*-(.*?)$/);
373
374 my $cgi_get_args = "action=CREATE&name=$core";
375
376 my $collect_home = $ENV{'GSDLCOLLECTDIR'};
377 my $etc_dirname = &util::filename_cat($collect_home,"etc");
378
379 if(!defined $data_parent_dir) {
380 $data_parent_dir = $self->{'build_dir'};
381 }
382
383 my $idx_dirname = &util::filename_cat($data_parent_dir,$ds_idx); # "dataDir"
384
385 $cgi_get_args .= "&instanceDir=$etc_dirname";
386 $cgi_get_args .= "&dataDir=$idx_dirname";
387
388 $self->_admin_service($cgi_get_args);
389
390}
391
392# removes (unloads) core from the ext/solr/sorl.xml config file
393sub admin_unload_core
394{
395 my $self = shift @_;
396 my ($core, $delete) = @_;
397
398 my $cgi_get_args = "action=UNLOAD&core=$core";
399 # &deleteIndex=true available from Solr3.3, see https://wiki.apache.org/solr/CoreAdmin.
400 # Also available since later Solr versions: deleteDataDir and deleteInstanceDir
401 if(defined $delete && $delete == 1) {
402 $cgi_get_args = $cgi_get_args."&deleteIndex=true";
403 }
404
405 $self->_admin_service($cgi_get_args);
406
407}
408
409sub start
410{
411 my $self = shift @_;
412 my ($verbosity) = @_;
413
414 $verbosity = 1 unless defined $verbosity;
415
416 my $solr_live_home = &util::filename_cat($ENV{'GSDL3HOME'}, "ext", "solr");
417 $self->{'solr_live_home'} = $solr_live_home; # will be used later to generate solr.xml.in from solr.xml and vice-versa
418 my $server_port = $ENV{'SOLR_PORT'};
419 my $server_host = $ENV{'SOLR_HOST'};
420
421 chdir($ENV{'GSDL3SRCHOME'});
422
423 my $server_java_cmd = "ant start";
424
425 my $server_status = "unknown";
426 if ($self->server_running()) {
427
428 $server_status = "already-running";
429 print STDERR "@@@@ server already running\n\n";
430 }
431 elsif (open(STARTIN,"$server_java_cmd 2>&1 |")) {
432
433 print STDERR "@@@@ need to start tomcat\n\n";
434 print STDERR "**** starting up tomcat server with cmd start =\n $server_java_cmd\n" if ($verbosity > 1);
435
436 my $line;
437 while (defined($line=<STARTIN>)) {
438
439 #if ($line =~ m/^(BUILD FAILED)/) {
440 print "Tomcat startup: $line";
441 #}
442 if ($line =~ m/^BUILD SUCCESSFUL/) {
443 last;
444 }
445 }
446
447 close(STARTIN);
448
449 if ($self->server_running()) {
450 $server_status = "explicitly-started";
451 #print STDERR "\n*** Tomcat server has started up now.\n\n";
452 } else {
453 $server_status = "failed-to-start"; # no need to set this, will be exiting below anyway
454
455 print STDERR "Error: failed to start greenstone tomcat server\n";
456 print STDERR "$!\n";
457 print STDERR "Command attempted was:\n";
458 print STDERR " $server_java_cmd\n";
459 print STDERR "run from directory:\n";
460 print STDERR " $ENV{'GSDL3SRCHOME'}\n";
461 print STDERR "----\n";
462
463 exit -1;
464 }
465 }
466 else {
467 print STDERR "@@@@ failed to start tomcat\n\n";
468 $server_status = "failed-to-start"; # no need to set this, will be exiting below anyway
469
470 print STDERR "Error: unable to start greenstone tomcat server\n";
471 print STDERR "$!\n";
472 print STDERR "Command attempted was:\n";
473 print STDERR " $server_java_cmd\n";
474 print STDERR "run from directory:\n";
475 print STDERR " $ENV{'GSDL3SRCHOME'}\n";
476 print STDERR "----\n";
477
478 exit -1;
479 }
480
481 if ($server_status eq "explicitly-started") {
482 $self->{'server_explicitly_started'} = 1;
483 print "Tomcat server ready and listening for connections at ";
484 print " $server_host:$server_port\n";
485
486 # now we know the server is ready to accept connections
487 }
488 elsif ($server_status eq "already-running") {
489 print STDERR "Using existing tomcat server detected at $server_host:$server_port\n";
490 $self->{'server_explicitly_started'} = 0;
491 }
492 elsif ($server_status eq "failed-to-start") {
493 print STDERR "Started Solr/Tomcat web server at $server_host:$server_port";
494 print STDERR ", but encountered an initialization error\n";
495 exit -1;
496 }
497
498}
499
500sub explicitly_started
501{
502 my $self = shift @_;
503
504 return $self->{'server_explicitly_started'};
505}
506
507sub stop
508{
509 my $self = shift @_;
510 my ($options) = @_;
511
512 my $solr_home = $ENV{'GEXT_SOLR'};
513
514 chdir($ENV{'GSDL3SRCHOME'});
515
516 # defaults
517 my $do_wait = 1;
518 my $output_verbosity = 1;
519
520 if (defined $options) {
521 if (defined $options->{'do_wait'}) {
522 $do_wait = $options->{'do_wait'};
523 }
524 if (defined $options->{'output_verbosity'}) {
525 $output_verbosity = $options->{'output_verbosity'};
526 }
527 }
528
529 my $server_java_cmd = "ant stop";
530
531 print STDERR "**** java server stop cmd:\n $server_java_cmd\n" if ($output_verbosity>1);
532
533 if (open(STOPIN,"$server_java_cmd 2>&1 |")) {
534
535 my $line;
536 while (defined($line=<STOPIN>)) {
537 print "@@@@ Tomcat shutdown: $line"; #if ($output_verbosity>1);
538 }
539 close(STOPIN);
540
541 if ($do_wait) {
542 wait(); # let the child process finish
543 }
544
545 if ($output_verbosity>0) {
546 print "@@@@@ Tomcat server shutdown\n";
547 }
548 }
549 else {
550 print STDERR "Error: failed to stop tomcat-server\n";
551 print STDERR "$!\n";
552 print STDERR "Command attempted was:\n";
553 print STDERR " $server_java_cmd\n";
554 print STDERR "run from directory:\n";
555 print STDERR " $solr_home\n";
556 print STDERR "----\n";
557
558 exit -2;
559 }
560}
561
562
563
5641;
Note: See TracBrowser for help on using the repository browser.