-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwb-treebank_server.pl
executable file
·371 lines (327 loc) · 13.9 KB
/
cwb-treebank_server.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;
# roadmap
# 1.0: refactoring of handle_connection
use version; our $VERSION = qv('0.10.0');
use Carp;
use English qw( -no_match_vars );
use Fcntl qw(:flock);
use File::Spec;
use IO::Handle;
use IO::Socket;
use POSIX qw(:sys_wait_h SIGTERM SIGKILL);
use Time::HiRes;
use CWB;
use CWB::CQP;
use CWB::CL;
use DBI;
use JSON;
use FindBin;
use lib $FindBin::Bin;
use CWB::treebank;
use Data::Dumper;
# read config
my %config = do "cwb-treebank_server.cfg";
# POSIX::setuid( $config{"uid"} );
# fork once, and let the parent exit
{
my $pidfile = $config{"pidfile"};
my $pid = fork;
if ($pid) {
open my $PIDFILE, ">", $pidfile or croak "Can't open $pidfile: $OS_ERROR";
print {$PIDFILE} $pid or croak "Can't print to $pidfile: $OS_ERROR";
close $PIDFILE or croak "Can't close $pidfile: $OS_ERROR";
exit;
}
croak "Couldn't fork: $OS_ERROR" unless ( defined $pid );
}
# redirect STDERR, STDIN, STDOUT
open STDERR, ">>", $config{"logfile"} or croak "Can't reopen STDERR: $OS_ERROR";
open STDIN, "<", File::Spec->devnull() or croak "Can't reopen STDIN: $OS_ERROR";
open STDOUT, ">", File::Spec->devnull() or croak "Can't reopen STDOUT: $OS_ERROR";
# dissociate from the controlling terminal that started us and stop
# being part of whatever process group we had been a member of
POSIX::setsid() or croak "Can't start a new session: $OS_ERROR";
# clear file creation mask
umask 0;
# open logfile
open my $log, ">>", $config{"logfile"} or croak "Cannot open logfile: $OS_ERROR";
# make filehandle hot
$log->autoflush();
my $server_port = $config{"server_port"};
my $server = IO::Socket::INET->new(
LocalPort => $server_port,
Type => SOCK_STREAM,
ReuseAddr => 1,
Listen => 10
) or croak "Couldn't be a tcp server on port $server_port : $EVAL_ERROR";
my $parent = $PROCESS_ID;
my %child_processes : shared;
# don't fear the reaper ;o)
# avoid zombies (Perl Cookbook, 16.9)
sub REAPER {
while ( ( my $child_pid = waitpid -1, WNOHANG ) > 0 ) {
lock(%child_processes);
delete $child_processes{$child_pid};
}
local $SIG{CHLD} = \&REAPER;
return;
}
local $SIG{CHLD} = \&REAPER;
my $time_to_die = 0;
local $SIG{INT} = local $SIG{TERM} = local $SIG{HUP} = sub { log_message("Caught signal (1)"); $time_to_die = 1; };
log_message("Hello, here's your server speaking. My pid is $PROCESS_ID");
log_message("Waiting for clients on port #$server_port.");
while ( not $time_to_die ) {
while ( ( my $client = $server->accept ) ) {
if ( not $config{"clients"}->{ $client->peerhost() } ) {
log_message( sprintf "Ignored conncection from %s", $client->peerhost() );
next;
}
log_message( sprintf "Accepted conncection from %s", $client->peerhost() );
my $pid = fork;
croak "fork: $OS_ERROR" unless defined $pid;
if ( $pid == 0 ) {
# we're the child
handle_connection($client);
exit;
}
else {
# we're the parent
$client->close();
log_message("Forked child process $pid");
{
lock(%child_processes);
$child_processes{$pid}++;
}
my $thread = threads->create( \&kill_child, $pid )->detach;
}
}
}
log_message('Time to die');
sub handle_connection {
my $socket = shift;
my $output = shift || $socket;
my $json = JSON->new();
my ( $cqp, %corpus_handles, %registry_handles, $dbh );
local $SIG{INT} = local $SIG{TERM} = local $SIG{HUP} = sub { log_message('Caught signal (2)'); $socket->close(); undef $cqp; undef $corpus_handles{$_} foreach ( keys %corpus_handles ); undef $registry_handles{$_} foreach ( keys %registry_handles ); undef $dbh; exit; };
$cqp = CWB::CQP->new();
$cqp->set_error_handler('die');
$cqp->exec( q{set Registry '} . $config{'registry'} . q{'} );
$CWB::CL::Registry = $config{'registry'};
$CWB::DefaultRegistry = $config{'registry'};
$dbh = connect_to_cache_db();
# prepare SQL statements
my $select_qid = $dbh->prepare(qq{SELECT qid FROM queries WHERE corpus = ? AND casesensitivity = ? AND query = ?});
my $insert_query = $dbh->prepare(qq{INSERT INTO queries (corpus, casesensitivity, query, time) VALUES (?, ?, ?, strftime('%s','now'))});
my $update_query = $dbh->prepare(qq{UPDATE queries SET time = strftime('%s','now') WHERE qid = ?});
foreach my $corpus ( @{ $config{"corpora"} } ) {
$corpus_handles{$corpus} = CWB::CL::Corpus->new($corpus);
$registry_handles{$corpus} = CWB::RegistryFile->new($corpus);
}
my $corpus = $config{"default_corpus"};
my $corpus_handle = $corpus_handles{$corpus};
my $registry_handle = $registry_handles{$corpus};
my $querymode = "collo-word";
my $case_sensitivity = 0;
my $queryid = 0;
$cqp->exec($corpus);
# mode (collo-word|collo-lemma|sentence)
# case-sensitivity (yes|no)
while ( my $queryref = <$socket> ) {
$queryid++;
chomp $queryref;
$queryref =~ s/\s*$//xms;
if ( length $queryref > 240 ) {
log_message( "[$queryid] " . substr( $queryref, 0, 115 ) . " [...] " . substr( $queryref, -115));
}
else {
log_message( "[$queryid] " . $queryref );
}
# Switch corpus
if ( $queryref =~ /^corpus[ ]([\p{IsLu}_\d]+)$/xms ) {
if ( defined $corpus_handles{$1} ) {
$corpus = $1;
$corpus_handle = $corpus_handles{$corpus};
$registry_handle = $registry_handles{$corpus};
$cqp->exec($corpus);
log_message("Switched corpus to '$corpus'");
}
else {
log_message("Unknown corpus '$1'");
}
next;
}
# Switch mode
if ( $queryref =~ /^mode[ ](collo-(?:word|lower|lemma)|sentence|collo|corpus-position|frequency|frequencies-(?:word|lower|lemma))$/xms ) {
$querymode = $1;
$querymode = "collo-word" if ( $querymode eq "collo" );
log_message("Switched query mode to '$querymode'");
next;
}
# Switch case-sensitivity
if ( $queryref =~ /^case-sensitivity[ ](yes|no)$/xms ) {
if ( $1 eq "yes" ) {
$case_sensitivity = 1;
log_message("Switched on case-sensitivity");
}
elsif ( $1 eq "no" ) {
$case_sensitivity = 0;
log_message("Switched off case-sensitivity");
}
next;
}
# Perform frequency query
if ( $querymode eq "frequency" and $queryref =~ /^ [[] [{] .* [}] []] $/xms ) {
my ($t0, $t1);
$t0 = [ Time::HiRes::gettimeofday() ];
my $frequency = CWB::treebank::get_frequency( $cqp, $corpus_handle, $registry_handle, $corpus, $queryref );
print {$output} $frequency . "\n" or croak "Can't print to socket: $OS_ERROR";
print {$output} "finito\n" or croak "Can't print to socket: $OS_ERROR";
$t1 = [ Time::HiRes::gettimeofday() ];
log_message( sprintf "answered %s in %.3fs (%d)", $queryid, Time::HiRes::tv_interval( $t0, $t1 ), $frequency );
next;
}
# Perform multiple frequency queries
if ( $querymode =~ /^frequencies-(?:word|lower|lemma)$/xms and $queryref =~ /^ [[] " .* " []] $/xms ) {
my ($t0, $t1);
$t0 = [ Time::HiRes::gettimeofday() ];
$querymode =~ /^frequencies-(word|lower|lemma)$/xms;
my $p_attribute = $1;
my ( $frequencies, $nr_of_items ) = CWB::treebank::get_multiple_frequencies( $cqp, $corpus_handle, $registry_handle, $corpus, $p_attribute, $queryref );
print {$output} $frequencies . "\n" or croak "Can't print to socket: $OS_ERROR";
print {$output} "finito\n" or croak "Can't print to socket: $OS_ERROR";
$t1 = [ Time::HiRes::gettimeofday() ];
log_message( sprintf "answered %s in %.3fs (%d items)", $queryid, Time::HiRes::tv_interval( $t0, $t1 ), $nr_of_items );
next;
}
# Perform query
if ( $queryref =~ /^ [[] [[] [{] .* [}] []] []] $/xms ) {
my $cache_handle;
my ( $t0, $t1, $t2 );
$t0 = [ Time::HiRes::gettimeofday() ];
my $cached;
$dbh->do(qq{BEGIN EXCLUSIVE TRANSACTION});
$select_qid->execute( $corpus, $case_sensitivity, $queryref );
my $qids = $select_qid->fetchall_arrayref;
my $qid;
my $query_times = q{};
# query is not cached
if ( @{$qids} == 0 ) {
$cached = 0;
$insert_query->execute( $corpus, $case_sensitivity, $queryref );
$select_qid->execute( $corpus, $case_sensitivity, $queryref );
$qids = $select_qid->fetchall_arrayref;
$qid = $qids->[0]->[0];
open $cache_handle, ">", File::Spec->catfile( $config{'cache_dir'}, $qid ) or croak "Can't open " . File::Spec->catfile( $config{'cache_dir'}, $qid ) . ": $OS_ERROR";
flock $cache_handle, LOCK_EX;
$dbh->do(qq{COMMIT});
$t1 = [ Time::HiRes::gettimeofday() ];
#&CWB::treebank::match_graph( $output, $cqp, $corpus_handle, $corpus, $querymode, $queryref, $case_sensitivity, $cache_handle );
$query_times = sprintf " (%s + %s + %s)", CWB::treebank::match_graph( $output, $cqp, $corpus_handle, $registry_handle, $corpus, $querymode, $queryref, $case_sensitivity, $cache_handle );
flock $cache_handle, LOCK_UN;
close $cache_handle or croak "Can't open " . File::Spec->catfile( $config{"cache_dir"}, $qid ) . ": $OS_ERROR";
}
# query is cached
else {
$cached = 1;
$qid = $qids->[0]->[0];
$update_query->execute($qid);
$dbh->do(qq{COMMIT});
$t1 = [ Time::HiRes::gettimeofday() ];
open $cache_handle, "<", File::Spec->catfile( $config{"cache_dir"}, $qid ) or croak "Can't open " . File::Spec->catfile( $config{"cache_dir"}, $qid ) . ": $OS_ERROR";
flock $cache_handle, LOCK_SH;
my ( $s_attributes, $p_attributes ) = CWB::treebank::get_corpus_attributes($corpus_handle, $registry_handle);
while ( my $line = <$cache_handle> ) {
chomp $line;
my $stored = $json->decode($line);
my ( $sid, $result ) = @{$stored};
print {$output} CWB::treebank::transform_output( $s_attributes, $p_attributes, $querymode, $sid, $result ) . "\n" or croak "Can't print to socket: $OS_ERROR";
}
flock $cache_handle, LOCK_UN;
close $cache_handle or croak "Can't open " . File::Spec->catfile( $config{"cache_dir"}, $qid ) . ": $OS_ERROR";
}
print {$output} "finito\n" or croak "Can't print to socket: $OS_ERROR";
$t2 = [ Time::HiRes::gettimeofday() ];
log_message( sprintf "answered %s in %.3fs (%s, %.3f + %.3f%s)", $queryid, Time::HiRes::tv_interval( $t0, $t2 ), $cached ? "cached" : "not cached", Time::HiRes::tv_interval( $t0, $t1 ), Time::HiRes::tv_interval( $t1, $t2 ), $query_times );
next;
}
# malformed query, ignore
log_message("ignored $queryid");
}
undef $cqp;
undef $corpus_handles{$_} foreach ( keys %corpus_handles );
undef $registry_handles{$_} foreach ( keys %registry_handles );
undef $dbh;
return;
}
sub connect_to_cache_db {
my $cache_size = $config{"cache_size"};
my $dbh = DBI->connect( "dbi:SQLite:dbname=" . File::Spec->catfile( $config{"cache_dir"}, $config{"cache_db"} ), q{}, q{} ) or croak "Cannot connect: $DBI::errstr";
#$dbh->do(qq{SELECT icu_load_collation('en_GB', 'BE')});
#$dbh->do(qq{PRAGMA foreign_keys = ON});
$dbh->sqlite_create_function(
"rmfile", 1,
sub {
unlink map { File::Spec->catfile( $config{"cache_dir"}, $_ ) } @_;
}
);
my $create_statement = <<'END_CREATE';
CREATE TABLE IF NOT EXISTS queries (
qid INTEGER PRIMARY KEY,
corpus TEXT NOT NULL,
casesensitivity INTEGER NOT NULL,
query TEXT NOT NULL,
time INTEGER NOT NULL,
UNIQUE (corpus, casesensitivity, query)
)
END_CREATE
$dbh->do($create_statement);
my $trigger_statement = <<"END_TRIGGER";
CREATE TRIGGER IF NOT EXISTS limit_to_cache_size AFTER INSERT ON queries
WHEN (SELECT count(*) FROM queries) > $cache_size
BEGIN
SELECT rmfile(qid) FROM queries WHERE qid IN (
SELECT qid FROM queries ORDER BY time ASC LIMIT (
SELECT count(*) - $cache_size FROM queries
)
);
DELETE FROM queries WHERE qid IN (
SELECT qid FROM queries ORDER BY time ASC LIMIT (
SELECT count(*) - $cache_size FROM queries
)
);
END
END_TRIGGER
$dbh->do($trigger_statement);
return $dbh;
}
sub kill_child {
my ($child_pid) = @_;
sleep 1500; # timeout
if ( defined $child_processes{$child_pid} ) {
log_message("Sending SIGTERM to $child_pid");
kill SIGTERM, $child_pid;
}
return;
}
sub log_message {
my ($string) = @_;
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my ( $sec, $min, $hour, $mday, $mon ) = localtime;
my $time = sprintf "%s %02d %02d:%02d:%02d, %d", $abbr[$mon], $mday, $hour, $min, $sec, $PROCESS_ID;
flock $log, LOCK_EX or croak "Can't lock stdout: $OS_ERROR";
print {$log} "[$time] $string\n" or croak "Can't print to log: $OS_ERROR";
flock $log, LOCK_UN or croak "Can't unlock stdout: $OS_ERROR";
return;
}
END {
close $server or croak "Can't close server: $OS_ERROR" if ( defined $server );
if ( defined $log ) {
log_message("Shutdown $PROCESS_ID");
close $log or croak "Cannot close logfile: $OS_ERROR";
}
}