source: gs2-extensions/tdb-edit/trunk/src/perllib/dbutil/tdbserver.pm@ 28765

Last change on this file since 28765 was 28765, checked in by jmt12, 10 years ago

Making the waiting for TDBServer exit a little more finegrained - i.e. 1/100th of a second rather than 1 second

File size: 12.7 KB
Line 
1###########################################################################
2#
3# dbutil::tdbserver -- utility functions for writing to tdb databases but
4# implemented as a server with a single, persistent
5# connection
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 2012
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28
29package dbutil::tdbserver;
30
31# Pragma
32use strict;
33use warnings;
34
35# Modules
36# - we're going to have to delve into locking (a little) to prevent
37# multiple threads trying to launch the server at once
38use Fcntl qw(:flock);
39# - we'll also need to wait around for an indeterminate amount of time for the
40# TDBServer to exit (detected by its lockfile disappearing)
41use Time::HiRes qw( usleep );
42
43# Greenstone modules
44use TDBClient;
45use util;
46
47my $hyphen70 = '-' x 70;
48my $debug = 0;
49
50# We have a global reference to all of the TDB Server lockfiles that this
51# instance has created (as we'll be responsible for closing them)
52my %created_server_lockfile_paths;
53# Keep track of the lockfiles for server we have added ourselves as listeners
54# to.
55my %listener_server_lockfile_paths;
56# We also have a global of all of the listeners we have assigned as we'll
57# be responsible for removing them.
58my %registered_listeners;
59
60sub _spawnClient
61{
62 my ($infodb_file_path) = @_;
63 # 1. Check whether the server is already running by trying to locate the
64 # server 'lock' file.
65
66 my ($collection) = $infodb_file_path =~ /collect[\\\/]([^\\\/]+)/i;
67 my ($infodb_file, $extension) = $infodb_file_path =~ /([^\\\/]+)\.(t?db)/i;
68
69 my $tmp_dir = &util::filename_cat($ENV{'GSDLHOME'},'tmp');
70 if (!-d $tmp_dir)
71 {
72 mkdir($tmp_dir, 0755);
73 }
74
75 my $we_started_server = 0;
76 my $server_lockfile_path = &util::filename_cat($ENV{'GSDLHOME'},'collect',$collection,'tmp','tdbserver.lock');
77 print " * Searching for TDBServer lockfile...\n" if ($debug);
78 if (!-e $server_lockfile_path)
79 {
80 print "Not found.\n" if ($debug);
81 # We need to lock here to ensure only one thread enters the following code,
82 # sees a missing TDBServer, and launches it
83 my $tmp_lockfile_path = &util::filename_cat($ENV{'GSDLHOME'},'tmp','dbutil-tdbserver.lock');
84 open(TMPFH, '>', $tmp_lockfile_path) or die("Warning! Failed to open file for writing: " . $tmp_lockfile_path . "\nReason: " . $! . "\n");
85 flock(TMPFH, LOCK_EX) or die("Error! Cannot lock file exclusively: " . $tmp_lockfile_path . "\nReason: " . $! . "\n");
86 print TMPFH localtime();
87 # - If the file still doesn't exist...
88 if (!-e $server_lockfile_path)
89 {
90 $we_started_server = 1;
91 # ...start it!
92 my $launch_cmd = 'TDBServer.pl "' . $$ . '" "' . $collection . '"';
93 print "* Starting TDBServer for: " . $collection . " [" . $launch_cmd . "]...\n";
94 # @note I once had the below pipe ending with 2>&1 |, but that then blocks
95 # indefinitely when looping and reading <SERVERIN>.
96 open(SERVERIN, $launch_cmd . ' |') or die("Error! Failed to run launch command: " . $launch_cmd . "\nReason: " . $! . "\n");
97 # read all the output from the server
98 my $line = '';
99 my $server_lock_file_created = 0;
100 autoflush STDOUT 1;
101 print "* Waiting for TDBServer to start";
102 my $server_host = '';
103 my $server_port = '';
104 while ($line = <SERVERIN>)
105 {
106 print ".";
107 # - watch for the line indicating a lock file has been created and
108 # populated with a sexy port number
109 if ($line =~ /Server now listening on ([^:]+):(\d+)/)
110 {
111 $server_host = $1;
112 $server_port = $2;
113 $server_lock_file_created = 1;
114 }
115 # - we could also watch for errors here
116 if ($debug)
117 {
118 if ($line !~ /\n/)
119 {
120 $line .= "\n";
121 }
122 $|++; # autoflush
123 print "[tdbserver] $line";
124 $|--; # disable autoflush
125 }
126 }
127 close(SERVERIN);
128 print "\n";
129 if (!$server_lock_file_created)
130 {
131 die("Error! TDBServer failed to create lock file. Check server logs.");
132 }
133 elsif ($debug)
134 {
135 print "* Server now running on " . $server_host . ":" . $server_port . "\n";
136 }
137 # record this for later
138 $created_server_lockfile_paths{$server_lockfile_path} = 1;
139 }
140 flock(TMPFH, LOCK_UN);
141 close($tmp_lockfile_path);
142 unlink($tmp_lockfile_path);
143 }
144 else
145 {
146 print "Found!\n" if ($debug);
147 }
148 # record this for later
149 $listener_server_lockfile_paths{$server_lockfile_path} = $infodb_file_path;
150 return TDBClient->new($server_lockfile_path, $infodb_file, $we_started_server);
151}
152
153END
154{
155 # we ask the server to shutdown, but only the 'creator' thread will actually
156 # be able to, and only once all listeners have deregistered.
157 foreach my $server_lockfile_path (keys (%listener_server_lockfile_paths))
158 {
159 my $infodb_file_path = $listener_server_lockfile_paths{$server_lockfile_path};
160 my $tdb_client_handle = TDBClient->new($server_lockfile_path, '');
161 # Deregister all of our registered listeners
162 foreach my $listener_suffix (keys(%registered_listeners))
163 {
164 $tdb_client_handle->removeListener($listener_suffix);
165 }
166 # ask the servers we created to shut down (all other threads will have
167 # this request ignored)
168 if (defined $created_server_lockfile_paths{$infodb_file_path})
169 {
170 print "* Attempting to stop TDBServer for: " . $infodb_file_path . "\n";
171 }
172 $tdb_client_handle->stopServer();
173 }
174 # we should now wait until all of our server_lockfiles have actually been
175 # removed (otherwise people could mistakenly run import/build again
176 # immediately and things *might* go pearshaped). (Subsequent testing shows it
177 # won't - a new Greenstone build can't start until the previous lockfile is
178 # removed - but I think, for ATOMic sake, I may as well make waiting a part
179 # of the Greenstone import/build time.
180 my $wait_for_exit = 1;
181 foreach my $server_lockfile_path (keys (%created_server_lockfile_paths))
182 {
183 if ($wait_for_exit)
184 {
185 # While the file exists, we should wait
186 autoflush STDOUT 1;
187 my $blurb = '';
188 if ($debug)
189 {
190 $blurb = '[' . $server_lockfile_path . ']';
191 }
192 print '* Waiting for TDBServer to exit... ';
193 if (-e $server_lockfile_path)
194 {
195 while (-e $server_lockfile_path)
196 {
197 usleep(10000);
198 }
199 }
200 }
201 else
202 {
203 print '* NOT Waiting for TDBServer to exit... ';
204 }
205 print " Done!\n";
206 }
207}
208
209# -----------------------------------------------------------------------------
210# TDB SERVER IMPLEMENTATION
211# -----------------------------------------------------------------------------
212sub open_infodb_write_handle
213{
214 my $infodb_file_path = shift(@_);
215 my $opt_append = shift(@_);
216 if (defined $opt_append && $opt_append ne "append")
217 {
218 print "Warning! Modes other than 'append' not supported for TDBServer.\n";
219 }
220 my $tdb_client_handle = &_spawnClient($infodb_file_path);
221 # Register this client on the server if necessary
222 $tdb_client_handle->addListener('w');
223 $registered_listeners{'w'} = 1;
224 # and pass the handle to client around
225 return $tdb_client_handle;
226}
227
228# /** Destructor or near enough.
229# /*
230sub close_infodb_write_handle
231{
232 my $tdb_client_handle = shift(@_);
233 # @todo Is there meant to be something here?
234}
235# /** close_infodb_write_handle($infodb_handle) **/
236
237# /** @function get_info_db_file_path
238# * Exactly the same as vanilla TDB - as we are still using a TDB database
239# * just accessing it via a persistant server
240# */
241sub get_infodb_file_path
242{
243 my $collection_name = shift(@_);
244 my $infodb_directory_path = shift(@_);
245 my $create_server = shift(@_);
246
247 my $infodb_file_extension = ".tdb";
248 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
249 my $infodb_file_path = &util::filename_cat($infodb_directory_path, $infodb_file_name);
250
251 # Special Case for TDBServer
252 if (defined $create_server && $create_server == 1)
253 {
254 my $tdb_client_handle = &_spawnClient($infodb_file_path);
255 # Register this client on the server if necessary
256 $tdb_client_handle->addListener('i');
257 $registered_listeners{'i'} = 1;
258 }
259
260 # Resuming our regular programming
261 return $infodb_file_path;
262}
263
264sub read_infodb_file
265{
266 my $infodb_file_path = shift(@_);
267 my $infodb_map = shift(@_);
268
269 my $tdb_client_handle = &_spawnClient($infodb_file_path);
270 $tdb_client_handle->addListener('r');
271 $registered_listeners{'r'} = 1;
272 # retrieves all the keys in the form:
273 # [key1]\n[key2]\n[key3]\n...[keyn]
274 my $raw_infodb_keys = $tdb_client_handle->query('[*]?');
275
276 my @infodb_keys = split(/\r?\n/, $raw_infodb_keys);
277 foreach my $infodb_key (@infodb_keys)
278 {
279 if ($infodb_key =~ /.+/ && $infodb_key !~ /-{70}/)
280 {
281 # lookup each key
282 my $infodb_value = $tdb_client_handle->query('[' . $infodb_key . ']?');
283 # store it
284 $infodb_map->{$infodb_key} = $infodb_value;
285 }
286 }
287}
288
289sub read_infodb_keys
290{
291 my $infodb_file_path = shift(@_);
292 my $infodb_map = shift(@_);
293
294 # spawn client (creating server as necessary)
295 my $tdb_client_handle = &_spawnClient($infodb_file_path);
296 # register ourself as listener
297 $tdb_client_handle->addListener('k');
298 $registered_listeners{'k'} = 1;
299 # retrieves all the keys in the form:
300 # [key1]\n[key2]\n[key3]\n...[keyn]
301 my $raw_infodb_keys = $tdb_client_handle->query('[*]?');
302 my @infodb_keys = split(/\r?\n/, $raw_infodb_keys);
303 foreach my $infodb_key (@infodb_keys)
304 {
305 if ($infodb_key =~ /.+/ && $infodb_key !~ /-{70}/)
306 {
307 $infodb_map->{$infodb_key} = 1;
308 }
309 }
310}
311
312sub write_infodb_entry
313{
314 my $tdb_client_handle = shift(@_);
315 my $infodb_key = shift(@_);
316 my $infodb_map = shift(@_);
317 # - build up the tdb command
318 my $tdb_command = "[" . $infodb_key . "]+\n";
319 foreach my $infodb_value_key (keys(%$infodb_map))
320 {
321 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
322 {
323 if ($infodb_value =~ /-{70,}/)
324 {
325 # if value contains 70 or more hyphens in a row we need to escape them
326 # to prevent txt2db from treating them as a separator
327 $infodb_value =~ s/-/&\#045;/gi;
328 }
329 $tdb_command .= "<" . $infodb_value_key . ">" . $infodb_value . "\n";
330 }
331 }
332 $tdb_command .= $hyphen70 . "\n";
333 # - ask the client to transmit the command to the server
334 $tdb_client_handle->query($tdb_command);
335}
336
337sub write_infodb_rawentry
338{
339 my $tdb_client_handle = shift(@_);
340 my $infodb_key = shift(@_);
341 my $infodb_val = shift(@_);
342 # - build up the tdb command
343 my $tdb_command = "[" . $infodb_key . "]\n";
344 $tdb_command .= $infodb_val . "\n";
345 $tdb_command .= $hyphen70 . "\n";
346 # - ask the client to transmit the command to the server
347 $tdb_client_handle->query($tdb_command);
348 return 1;
349}
350
351sub set_infodb_entry
352{
353 my $infodb_file_path = shift(@_);
354 my $infodb_key = shift(@_);
355 my $infodb_map = shift(@_);
356 # spawn client (creating server as necessary)
357 my $tdb_client_handle = &_spawnClient($infodb_file_path);
358 $tdb_client_handle->addListener('s');
359 $registered_listeners{'s'} = 1;
360 # Protect metadata values that go inside quotes for tdbset
361 foreach my $k (keys %$infodb_map)
362 {
363 my @escaped_v = ();
364 foreach my $v (@{$infodb_map->{$k}})
365 {
366 if ($k eq "contains")
367 {
368 # protect quotes in ".2;".3 etc
369 $v =~ s/\"/\\\"/g;
370 push(@escaped_v, $v);
371 }
372 else
373 {
374 my $ev = &ghtml::unescape_html($v);
375 $ev =~ s/\"/\\\"/g;
376 push(@escaped_v, $ev);
377 }
378 }
379 $infodb_map->{$k} = \@escaped_v;
380 }
381 # Generate the record string (TDB command)
382 my $tdb_command = "[" . $infodb_key . "]\n";
383 $tdb_command .= &dbutil::convert_infodb_hash_to_string($infodb_map) . "\n";
384 $tdb_command .= $hyphen70 . "\n";
385 # Send command to server
386 $tdb_client_handle->query($tdb_command);
387}
388
389sub delete_infodb_entry
390{
391 my $tdb_client_handle = shift(@_);
392 my $infodb_key = shift(@_);
393 # - create command
394 my $tdb_command = "[" . $infodb_key . "]-\n";
395 # - and send
396 $tdb_client_handle->query($tdb_command);
397}
398
399
400
4011;
Note: See TracBrowser for help on using the repository browser.