This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Db.pm
3547 lines (2362 loc) · 105 KB
/
Db.pm
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Db;
=head1 NAME
Db
=head1 DESCRIPTION
This class is a non-OO database interface.
Fri Jan 9 12:15:06 2015 changed all table to ENGINE=INNODB for row
locking to increase parallelism. Only lock tables when we do not have
a primary key to select which rows to lock.
=head1 VERSION
=head1 SYNOPSIS
Coding example
=head1 METHODS
=over 8
=cut
use strict;
use warnings;
use Time::HiRes qw( time );
# App
use Utils;
use Debug::DUtils;
use Context;
use DbUtils;
use Identifier;
use Search::Constants;
use SLIP_Utils::States;
use SLIP_Utils::Common;
# Map from constants to integers for MySQL query building
our $C_NO_ERROR = IX_NO_ERROR;
our $C_INDEX_FAILURE = IX_INDEX_FAILURE;
our $C_INDEX_TIMEOUT = IX_INDEX_TIMEOUT;
our $C_SERVER_GONE = IX_SERVER_GONE;
our $C_ALREADY_FAILED = IX_ALREADY_FAILED;
our $C_DATA_FAILURE = IX_DATA_FAILURE;
our $C_METADATA_FAILURE = IX_METADATA_FAILURE;
our $C_CRITICAL_FAILURE = IX_CRITICAL_FAILURE;
our $C_NO_INDEXER_AVAIL = IX_NO_INDEXER_AVAIL;
our $C_GENERATOR_ERROR = IX_GENERATOR_ERROR;
our $MYSQL_ZERO_TIMESTAMP = '0000-00-00 00:00:00';
our $VSOLR_ZERO_TIMESTAMP = '00000000';
# ---------------------------------------------------------------------
=item __LOCK_TABLES, __UNLOCK_TABLES
Description
=cut
# ---------------------------------------------------------------------
sub __UNLOCK_TABLES {
my $dbh = shift;
DEBUG('lsdb', qq{DEBUG: COMMIT WORK});
DbUtils::commit($dbh);
}
sub __LOCK_TABLES {
my ($dbh, @tables) = @_;
DbUtils::begin_work($dbh);
DEBUG('lsdb', qq{DEBUG: BEGIN WORK});
}
# =====================================================================
# =====================================================================
#
# Shadow rights table [slip_rights][slip_rights_temp][slip_vsolr_timestamp] @@
#
# =====================================================================
# =====================================================================
# ---------------------------------------------------------------------
=item initialize_j_rights_temp
No keys. They are added after the table is fully populated.
=cut
# ---------------------------------------------------------------------
sub initialize_j_rights_temp {
my($C, $dbh) = @_;
my ($statement, $sth);
$statement = qq{DROP TABLE IF EXISTS ht_maintenance.slip_rights_temp};
DEBUG('lsdb', qq{DEBUG: $statement});
$sth = DbUtils::prep_n_execute($dbh, $statement);
$statement = qq{CREATE TABLE ht_maintenance.slip_rights_temp LIKE ht_maintenance.slip_rights};
DEBUG('lsdb', qq{DEBUG: $statement});
$sth = DbUtils::prep_n_execute($dbh, $statement);
}
# ---------------------------------------------------------------------
=item Drop_j_rights_Rename_j_rights_temp
Description
=cut
# ---------------------------------------------------------------------
sub Drop_j_rights_Rename_j_rights_temp {
my ($C, $dbh) = @_;
my ($statement, $sth);
$statement = qq{DROP TABLE ht_maintenance.slip_rights};
DEBUG('lsdb', qq{DEBUG: $statement});
$sth = DbUtils::prep_n_execute($dbh, $statement);
$statement = qq{ALTER TABLE ht_maintenance.slip_rights_temp RENAME TO ht_maintenance.slip_rights};
DEBUG('lsdb', qq{DEBUG: $statement});
$sth = DbUtils::prep_n_execute($dbh, $statement);
$statement = qq{CREATE OR REPLACE SQL SECURITY INVOKER VIEW ht.slip_rights AS SELECT * FROM ht_maintenance.slip_rights};
DEBUG('lsdb', qq{DEBUG: $statement});
$sth = DbUtils::prep_n_execute($dbh, $statement);
}
# ---------------------------------------------------------------------
=item Create_View_for_j_rights
Description
=cut
# ---------------------------------------------------------------------
sub Create_View_for_j_rights {
my ($C, $dbh) = @_;
my ($statement, $sth);
$statement = qq{CREATE OR REPLACE SQL SECURITY INVOKER VIEW ht.slip_rights AS SELECT * FROM ht_maintenance.slip_rights};
DEBUG('lsdb', qq{DEBUG: $statement});
$sth = DbUtils::prep_n_execute($dbh, $statement);
}
# ---------------------------------------------------------------------
=item init_vSolr_timestamp
Description
=cut
# ---------------------------------------------------------------------
sub init_vSolr_timestamp {
my ($C, $dbh, $time) = @_;
my ($statement, $sth);
my $timestamp = defined($time) ? $time : $Db::VSOLR_ZERO_TIMESTAMP;
$statement = qq{DELETE FROM slip_vsolr_timestamp};
$sth = DbUtils::prep_n_execute($dbh, $statement);
DEBUG('lsdb', qq{DEBUG: $statement});
$statement = qq{INSERT INTO slip_vsolr_timestamp SET time=$timestamp};
$sth = DbUtils::prep_n_execute($dbh, $statement);
DEBUG('lsdb', qq{DEBUG: $statement});
}
# ---------------------------------------------------------------------
=item Select_vSolr_timestamp
Time last used to query vufind for updates/inserts into slip_rights.
=cut
# ---------------------------------------------------------------------
sub Select_vSolr_timestamp {
my($C, $dbh) = @_;
my $statement = qq{SELECT time FROM slip_vsolr_timestamp};
my $sth = DbUtils::prep_n_execute($dbh, $statement);
my $timestamp = $sth->fetchrow_array || $Db::VSOLR_ZERO_TIMESTAMP;
DEBUG('lsdb', qq{DEBUG: $statement ::: $timestamp});
return $timestamp;
}
# ---------------------------------------------------------------------
=item update_vSolr_timestamp
Query VuFind Solr for ht_id_display times based on LAG days earlier
than this new MAX(update_time).
=cut
# ---------------------------------------------------------------------
sub update_vSolr_timestamp {
my($C, $dbh) = @_;
my ($statement, $sth);
$statement = qq{SELECT MAX(update_time) FROM slip_rights};
$sth = DbUtils::prep_n_execute($dbh, $statement);
my $latest_timestamp = $sth->fetchrow_array || $Db::VSOLR_ZERO_TIMESTAMP;
DEBUG('lsdb', qq{DEBUG: $statement ::: $latest_timestamp});
$statement = qq{UPDATE slip_vsolr_timestamp SET time=$latest_timestamp};
DEBUG('lsdb', qq{DEBUG: $statement});
$sth = DbUtils::prep_n_execute($dbh, $statement);
}
# ---------------------------------------------------------------------
=item Select_count_from_j_rights
Description
=cut
# ---------------------------------------------------------------------
sub Select_count_from_j_rights {
my ($C, $dbh, $where) = @_;
$where = $where ? $where : '';
my $statement = qq{SELECT count(*) FROM slip_rights $where};
my $sth = DbUtils::prep_n_execute($dbh, $statement);
my $size = $sth->fetchrow_array || 0;
DEBUG('lsdb', qq{DEBUG: $statement ::: $size});
return $size;
}
# ---------------------------------------------------------------------
=item Select_count_from_j_rights_temp
Description
=cut
# ---------------------------------------------------------------------
sub Select_count_from_j_rights_temp {
my ($C, $dbh) = @_;
my $statement = qq{SELECT count(*) FROM slip_rights_temp};
my $sth = DbUtils::prep_n_execute($dbh, $statement);
my $size = $sth->fetchrow_array || 0;
DEBUG('lsdb', qq{DEBUG: $statement ::: $size});
return $size;
}
# ---------------------------------------------------------------------
=item Get_ids_available_to_no_one
Returns list of ids with specified rights attribute
assumes attribute is $RightsGlobals::g_available_to_no_one_attribute_value;
=cut
# ---------------------------------------------------------------------
sub Get_ids_available_to_no_one {
my ($C, $dbh, $deleted_attr) = @_;
my $id_arr_ref;
my $statement = qq{SELECT CONCAT (namespace, '.' , id) as nid FROM ht.rights_current WHERE attr=?};
DEBUG('lsdb', qq{DEBUG: $statement : $deleted_attr});
my $sth = DbUtils::prep_n_execute($dbh, $statement, $deleted_attr);
my $ref_to_arr_of_arr_ref = $sth->fetchall_arrayref([0]);
if (scalar(@$ref_to_arr_of_arr_ref)) {
$id_arr_ref = [ map {$_->[0]} @$ref_to_arr_of_arr_ref ];
}
return $id_arr_ref;
}
# ---------------------------------------------------------------------
=item Select_latest_rights_row
Description
=cut
# ---------------------------------------------------------------------
sub Select_latest_rights_row {
my ($C, $dbh, $namespace, $id) = @_;
my $statement =
qq{SELECT CONCAT(namespace, '.', id) AS nid, attr, reason, source, user, time FROM ht.rights_current WHERE namespace=? AND id=?};
DEBUG('lsdb', qq{DEBUG: $statement : $namespace, $id});
my $sth = DbUtils::prep_n_execute($dbh, $statement, $namespace, $id);
my $row_hashref = $sth->fetchrow_hashref();
return $row_hashref;
}
# ---------------------------------------------------------------------
=item Insert_j_rights_temp_id
The table starts out empty during a full rebuild_rights-j session. nid
in slip_rights table is PRIMARY KEY so an nid can't appear more than
once.
=cut
# ---------------------------------------------------------------------
sub Replace_j_rights_temp_id {
my ($C, $dbh, $hashref) = @_;
# From ht_repository.rights_current
my $attr = $hashref->{attr};
my $reason = $hashref->{reason};
my $source = $hashref->{source};
my $user = $hashref->{user};
my $time = $hashref->{time};
# From vSolr query result
my $nid = $hashref->{nid};
# For reasons unknown, we sometimes have trailing spaces
Utils::trim_spaces(\$nid);
my $sysid = $hashref->{sysid};
my $updateTime_in_vSolr = $hashref->{timestamp_of_nid};
# CASE: nid is not in slip_rights_temp ==> NEW. Insert
my $statement = qq{REPLACE INTO slip_rights_temp SET nid=?, attr=?, reason=?, source=?, user=?, time=?, sysid=?, update_time=?};
DbUtils::prep_n_execute($dbh, $statement, $nid, $attr, $reason, $source, $user, $time, $sysid, $updateTime_in_vSolr);
DEBUG('lsdb', qq{DEBUG: $statement : $nid, $attr, $reason, $source, $user, $time, $sysid, $updateTime_in_vSolr});
}
# ---------------------------------------------------------------------
=item Replace_j_rights_id
Description
=cut
# ---------------------------------------------------------------------
sub Replace_j_rights_id {
my ($C, $dbh, $hashref, $Check_only, $Force) = @_;
# From mdp.rights, currently
my $attr = $hashref->{attr};
my $reason = $hashref->{reason};
my $source = $hashref->{source};
my $user = $hashref->{user};
my $time = $hashref->{time};
# From vSolr query result
my $nid = $hashref->{nid};
# For reasons unknown, we sometimes have trailing spaces
Utils::trim_spaces(\$nid);
my $sysid = $hashref->{sysid};
my $updateTime_in_vSolr = $hashref->{timestamp_of_nid};
my $case;
my ($statement, $sth);
# See what we already have in slip_rights
$statement = qq{SELECT nid, update_time, sysid FROM slip_rights WHERE nid=?};
DEBUG('lsdb', qq{DEBUG: $statement : $nid});
$sth = DbUtils::prep_n_execute($dbh, $statement, $nid);
my $ref_to_arr_of_hashref = $sth->fetchall_arrayref({});
my $nid_exists_in_slip_rights = $ref_to_arr_of_hashref->[0]->{nid};
my $sysid_in_slip_rights = $ref_to_arr_of_hashref->[0]->{sysid};
my $updateTime_in_slip_rights = $ref_to_arr_of_hashref->[0]->{update_time} || $Db::VSOLR_ZERO_TIMESTAMP;
# Pass the slip_rights timestamp in the input hashref
$hashref->{timestamp_in_slip_rights} = $updateTime_in_slip_rights;
# Pass the slip_rights_sysid in the input hashref
$hashref->{sysid_in_slip_rights} = $sysid_in_slip_rights;
unless ($nid_exists_in_slip_rights) {
# CASE: nid is not in slip_rights ==> NEW. Insert
$case = 'NEW';
DEBUG('lsdb', qq{DEBUG: $statement ::: (A) NEW});
}
else {
# If nid's update_time is the is same as update_time recorded
# in SLIP_RIGHTS_TABLE_NAME then we're seeing an update we
# already recorded due to range query [last_run_time-2d TO *].
# Use '<=' even though it should be impossible for the nid
# timestamp we are seeing now to be older than what we
# recorded when we saw it for the first time except for the
# FORCED case when repairing (*)
if ($Force) {
$case = 'FORCED';
DEBUG('lsdb', qq{DEBUG: $statement ::: FORCED});
}
elsif ($updateTime_in_vSolr <= $updateTime_in_slip_rights) {
$case = 'NOOP';
DEBUG('lsdb', qq{DEBUG: $statement ::: NOOP});
}
else {
# Seen but updated since last save to SLIP_RIGHTS_TABLE_NAME
if ($sysid_in_slip_rights eq $sysid) {
# CASE: nid from vSolr newer (>) that timestamp in
# slip_rights, same sysid: UPDATED
$case = 'UPDATED';
DEBUG('lsdb', qq{DEBUG: $statement ::: (D) UPDATED});
}
else {
# CASE: nid in slip_rights but different sysid: MOVED
$case = 'MOVED';
DEBUG('lsdb', qq{DEBUG: $statement ::: (C) MOVED});
}
}
}
$statement = qq{REPLACE INTO slip_rights SET nid=?, attr=?, reason=?, source=?, user=?, time=?, sysid=?, update_time=?};
DEBUG('lsdb', qq{DEBUG [Check=$Check_only, case=$case]: $statement : $nid, $attr, $reason, $source, $user, $time, $sysid, $updateTime_in_vSolr});
unless ($Check_only) {
if ($case ne 'NOOP') {
# insert or replace
$sth = DbUtils::prep_n_execute($dbh, $statement, $nid, $attr, $reason, $source, $user, $time, $sysid, $updateTime_in_vSolr);
}
}
return $case;
}
# ---------------------------------------------------------------------
=item Select_j_rights_id_attr
Get the current attr value for the id in slip_rights.
=cut
# ---------------------------------------------------------------------
sub Select_j_rights_id_attr {
my ($C, $dbh, $nid) = @_;
my $statement = qq{SELECT attr FROM slip_rights WHERE nid=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $nid);
my $attr = $sth->fetchrow_array() || ( $nid =~ m,^test\., ? 1 : 0 );
return $attr;
}
# ---------------------------------------------------------------------
=item Select_j_rights_id_sysid
Get the current sysid value for the id in slip_rights.
=cut
# ---------------------------------------------------------------------
sub Select_j_rights_id_sysid {
my ($C, $dbh, $nid) = @_;
my $statement = qq{SELECT sysid FROM slip_rights WHERE nid=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $nid);
my $sysid = $sth->fetchrow_array() || ( $nid =~ m,^test\., ? '100246857' : 0 );
return $sysid;
}
# =====================================================================
# =====================================================================
#
# Queue tables [slip_rights_timestamp][slip_queue][slip_errors][slip_timeouts] @@
#
# =====================================================================
# =====================================================================
# ---------------------------------------------------------------------
=item Test_j_rights_timestamp
Simple test to see if a run exists.
=cut
# ---------------------------------------------------------------------
sub Test_j_rights_timestamp {
my ($C, $dbh, $run) = @_;
my $statement = qq{SELECT count(*) FROM slip_rights_timestamp WHERE run=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $run);
my $ct = $sth->fetchrow_array;
DEBUG('lsdb', qq{DEBUG: $statement : $run ::: $ct});
return $ct;
}
# ---------------------------------------------------------------------
=item Select_j_rights_timestamp
Holds MAX(slip_rights.insert_time) timestamp when last enqueue to
slip_queue occured.
=cut
# ---------------------------------------------------------------------
sub Select_j_rights_timestamp {
my ($C, $dbh, $run) = @_;
my $statement = qq{SELECT time FROM slip_rights_timestamp WHERE run=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $run);
my $timestamp = $sth->fetchrow_array || $Db::VSOLR_ZERO_TIMESTAMP;
DEBUG('lsdb', qq{DEBUG: $statement : $run ::: $timestamp});
return $timestamp;
}
# ---------------------------------------------------------------------
=item update_j_rights_timestamp
Store MAX(slip_rights.insert_time) timestamp when last enqueue to
slip_queue occured.
=cut
# ---------------------------------------------------------------------
sub update_j_rights_timestamp {
my ($C, $dbh, $run) = @_;
my ($sth, $statement);
$statement = qq{SELECT MAX(insert_time) FROM slip_rights};
$sth = DbUtils::prep_n_execute($dbh, $statement);
my $timestamp = $sth->fetchrow_array;
DEBUG('lsdb', qq{DEBUG: $statement : $timestamp, $run});
$statement = qq{UPDATE slip_rights_timestamp SET time=? WHERE run=?};
$sth = DbUtils::prep_n_execute($dbh, $statement, $timestamp, $run);
DEBUG('lsdb', qq{DEBUG: $statement : $timestamp, $run});
}
# ---------------------------------------------------------------------
=item init_j_rights_timestamp
Description
=cut
# ---------------------------------------------------------------------
sub init_j_rights_timestamp {
my ($C, $dbh, $run, $time) = @_;
delete_j_rights_timestamp($C, $dbh, $run);
my $timestamp = defined($time) ? $time : $Db::MYSQL_ZERO_TIMESTAMP;
my $statement = qq{INSERT INTO slip_rights_timestamp SET run=?, time=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $run, $timestamp);
DEBUG('lsdb', qq{DEBUG: $statement : $run, $timestamp});
}
# ---------------------------------------------------------------------
=item delete_j_rights_timestamp
Description
=cut
# ---------------------------------------------------------------------
sub delete_j_rights_timestamp {
my ($C, $dbh, $run) = @_;
my $statement = qq{DELETE FROM slip_rights_timestamp WHERE run=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $run);
DEBUG('lsdb', qq{DEBUG: $statement : $run});
}
# ---------------------------------------------------------------------
=item Renumber_j_rights_timestamp
Description
=cut
# ---------------------------------------------------------------------
sub Renumber_j_rights_timestamp {
my ($C, $dbh, $from_run, $to_run) = @_;
my $statement = qq{UPDATE slip_rights_timestamp SET run=? WHERE run=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $to_run, $from_run);
DEBUG('lsdb', qq{DEBUG: $statement : $to_run, $from_run});
}
# ---------------------------------------------------------------------
=item Select_id_slice_from_queue
Description
=cut
# ---------------------------------------------------------------------
sub Select_id_slice_from_queue {
my ($C, $dbh, $run, $shard, $pid, $host, $slice_size) = @_;
my $sth;
my $statement;
my $proc_status_available = $SLIP_Utils::States::Q_AVAILABLE;
my $proc_status_processing = $SLIP_Utils::States::Q_PROCESSING;
__LOCK_TABLES($dbh, qw(slip_queue));
# mark a slice of available ids as being processed by a producer
# process
$statement = qq{UPDATE slip_queue SET pid=?, host=?, proc_status=? WHERE run=? AND (shard=0 OR shard=?) AND proc_status=? LIMIT $slice_size};
DEBUG('lsdb', qq{DEBUG: $statement : $pid, $host, PROCESSING, $run, $shard, AVAILABLE});
$sth = DbUtils::prep_n_execute($dbh, $statement, $pid, $host, $proc_status_processing, $run, $shard, $proc_status_available);
# get the ids in the slice just marked for this process and ...
$statement = qq{SELECT id FROM slip_queue WHERE run=? AND proc_status=? AND pid=? AND host=?};
DEBUG('lsdb', qq{DEBUG: $statement : $run, PROCESSING, $pid, $host});
$sth = DbUtils::prep_n_execute($dbh, $statement, $run, $proc_status_processing, $pid, $host);
my $ref_to_ary_of_hashref = $sth->fetchall_arrayref({});
DEBUG('lsdb', qq{DEBUG: SELECT returned num_items=} . scalar(@$ref_to_ary_of_hashref));
# ... delete from queue
$statement = qq{DELETE FROM slip_queue WHERE run=? AND proc_status=? AND pid=? AND host=?};
DEBUG('lsdb', qq{DEBUG: $statement : $run, PROCESSING, $pid, $host});
$sth = DbUtils::prep_n_execute($dbh, $statement, $run, $proc_status_processing, $pid, $host);
__UNLOCK_TABLES($dbh);
return $ref_to_ary_of_hashref;
}
# ---------------------------------------------------------------------
=item Delete_queue
Description
=cut
# ---------------------------------------------------------------------
my $DELETE_Q_SLICE_SIZE = 10000;
sub Delete_queue {
my ($C, $dbh, $run) = @_;
my $total_affected = 0;
my $num_affected = 0;
do {
my $begin = time;
my $statement = qq{DELETE FROM slip_queue WHERE run=? LIMIT $DELETE_Q_SLICE_SIZE};
DEBUG('lsdb', qq{DEBUG: $statement : $run});
my $sth = DbUtils::prep_n_execute($dbh, $statement, $run, \$num_affected);
$num_affected = ($num_affected == '0E0') ? 0 : $num_affected;
$total_affected += $num_affected;
my $elapsed = time - $begin;
sleep $elapsed/2;
} until ($num_affected <= 0);
return $total_affected;
}
# ---------------------------------------------------------------------
=item Renumber_queue
Description
=cut
# ---------------------------------------------------------------------
sub Renumber_queue {
my ($C, $dbh, $from_run, $to_run) = @_;
my $statement = qq{UPDATE slip_queue SET run=? WHERE run=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $to_run, $from_run);
DEBUG('lsdb', qq{DEBUG: $statement : $to_run, $from_run});
}
# ---------------------------------------------------------------------
=item __insert_queue_items
Note: called only locally due to locking.
=cut
# ---------------------------------------------------------------------
sub __insert_queue_items {
my ($C, $dbh, $run, $ref_to_ary_of_hashref) = @_;
my $sth;
my $statement;
my $num_inserted = 0;
my $proc_status_available = $SLIP_Utils::States::Q_AVAILABLE;
foreach my $hashref (@$ref_to_ary_of_hashref) {
my $id = $hashref->{id};
my $shard = $hashref->{shard};
$statement = qq{INSERT INTO slip_queue SET run=?, shard=?, id=?, pid=0, host='', proc_status=? ON DUPLICATE KEY UPDATE pid=0, host='', proc_status=?};
DEBUG('lsdb', qq{DEBUG: $statement : $run, $shard, $id, $proc_status_available, $proc_status_available});
$sth = DbUtils::prep_n_execute($dbh, $statement, $run, $shard, $id, $proc_status_available, $proc_status_available);
$num_inserted++;
}
return $num_inserted;
}
# ---------------------------------------------------------------------
=item handle_queue_insert
Description
=cut
# ---------------------------------------------------------------------
sub handle_queue_insert {
my $C = shift;
my $dbh = shift;
my $run = shift;
my $ref_to_arr_of_ids = shift;
my $total_start = time;
my $total_to_be_inserted = scalar @$ref_to_arr_of_ids;
my $total_num_inserted = 0;
while (1) {
my $start = time;
# Insert in blocks of 1000
my @queue_array = splice(@$ref_to_arr_of_ids, 0, 1000);
last
if (scalar(@queue_array) <= 0);
__LOCK_TABLES($dbh, qw(slip_indexed slip_queue));
my $ref_to_arr_of_hashref = [];
foreach my $id (@queue_array) {
my $shard = Select_item_id_shard($C, $dbh, $run, $id);
push(@$ref_to_arr_of_hashref, {id => $id, shard => $shard});
}
my $num_inserted = __insert_queue_items($C, $dbh, $run, $ref_to_arr_of_hashref);
__UNLOCK_TABLES($dbh);
$total_num_inserted += $num_inserted;
my $elapsed = time - $start;
my $ids_per_sec = ($total_num_inserted / (time - $total_start)) || 1;
my @parts = gmtime int(($total_to_be_inserted - $total_num_inserted) * (1 / $ids_per_sec));
my $time_remaining = sprintf("%dh %dm %ds", @parts[2,1,0]);
my $s0 = sprintf("--> added $num_inserted ids to queue, total=%d elapsed=%.2f rate=%.2f ids/sec remains=%s\n", $total_num_inserted, $elapsed, $ids_per_sec, $time_remaining);
__output($s0);
}
my $total_elapsed = time - $total_start;
my $s1 = sprintf("added %d total items to queue in %.0f sec.\n", $total_num_inserted, $total_elapsed);
__output($s1);
return $total_num_inserted;
}
# ---------------------------------------------------------------------
=item __get_update_time_WHERE_clause
Description
=cut
# ---------------------------------------------------------------------
sub __get_update_time_WHERE_clause {
my ($C, $dbh, $run) = @_;
my $timestamp = Select_j_rights_timestamp($C, $dbh, $run);
my $WHERE_clause;
if ($timestamp eq $Db::VSOLR_ZERO_TIMESTAMP) {
$WHERE_clause = qq{ WHERE insert_time >= ?};
}
else {
$WHERE_clause = qq{ WHERE insert_time > ?};
}
return ($WHERE_clause, $timestamp);
}
# ---------------------------------------------------------------------
=item count_insert_latest_into_queue
Coupled to insert_latest_into_queue via insert_time > $timestamp
=cut
# ---------------------------------------------------------------------
sub count_insert_latest_into_queue {
my ($C, $dbh, $run) = @_;
# NOTE: non-overlap (>) Talk to Tim and see
# insert_latest_into_queue(). If timestamp is 0, we want all.
my ($WHERE_clause, @params) = __get_update_time_WHERE_clause($C, $dbh, $run);
my $statement = qq{SELECT count(*) FROM slip_rights } . $WHERE_clause;
DEBUG('lsdb', qq{DEBUG: $statement : @params});
my $sth = DbUtils::prep_n_execute($dbh, $statement, @params);
my $num = $sth->fetchrow_array || 0;
return $num;
}
# ---------------------------------------------------------------------
=item insert_latest_into_queue
Coupled to count_insert_latest_into_queue via insert_time > $timestamp
=cut
# ---------------------------------------------------------------------
sub insert_latest_into_queue {
my ($C, $dbh, $run) = @_;
my ($sth, $statement);
__LOCK_TABLES($dbh, qw(slip_rights slip_rights_timestamp));
# Select IDs from slip_rights whose insert_time is > or >= than
# MAX(slip_rights.insert_time) recorded in
# slip_rights_timestamp.time when LAST we enqueued items from
# slip_rights and update slip_rights_timestamp.time to
# MAX(slip_rights.insert_time). This takes about 1 second per 1K
# IDs.
my ($WHERE_clause, @params) = __get_update_time_WHERE_clause($C, $dbh, $run);
$statement = qq{SELECT nid FROM slip_rights } . $WHERE_clause;
$sth = DbUtils::prep_n_execute($dbh, $statement, @params);
my $ref_to_arr_of_arr_ref = $sth->fetchall_arrayref([0]);
my $id_arr_ref = [];
if (scalar(@$ref_to_arr_of_arr_ref)) {
$id_arr_ref = [ map {$_->[0]} @$ref_to_arr_of_arr_ref ];
}
# Use the maximum insert_time in slip_rights to update
# slip_rights_timestamp.time
update_j_rights_timestamp($C, $dbh, $run);
__UNLOCK_TABLES($dbh);
# Insert them in blocks into slip_queue so queue is not locked for a
# very long time during large updates.
my $num_inserted = handle_queue_insert($C, $dbh, $run, $id_arr_ref);
return $num_inserted;
}
# ---------------------------------------------------------------------
=item Delete_id_from_j_queue
Description
=cut
# ---------------------------------------------------------------------
sub Delete_id_from_j_queue {
my ($C, $dbh, $run, $id) = @_;
my $statement = qq{DELETE FROM slip_queue WHERE run=? AND id=?};
my $sth = DbUtils::prep_n_execute($dbh, $statement, $run, $id);
DEBUG('lsdb', qq{DEBUG: $statement : $run, $id});
}
# ---------------------------------------------------------------------
=item update_unstick_inprocess
Description
=cut
# ---------------------------------------------------------------------
sub update_unstick_inprocess {
my ($C, $dbh, $run) = @_;
my $sth;
my $statement;
$statement = qq{SELECT count(*) FROM slip_queue WHERE run=? AND proc_status=?};
$sth = DbUtils::prep_n_execute($dbh, $statement, $run, $SLIP_Utils::States::Q_PROCESSING);
my $num_inprocess = $sth->fetchrow_array || 0;
DEBUG('lsdb', qq{DEBUG: $statement : $run, $SLIP_Utils::States::Q_PROCESSING ::: inprocess=$num_inprocess});
if ($num_inprocess > 0) {
# Mark a slice of ids being processed by a producer process as
# available
$statement = qq{UPDATE slip_queue SET proc_status=? WHERE run=? AND proc_status=?};
$sth = DbUtils::prep_n_execute($dbh, $statement, $SLIP_Utils::States::Q_AVAILABLE, $run, $SLIP_Utils::States::Q_PROCESSING);
DEBUG('lsdb', qq{DEBUG: $statement : $SLIP_Utils::States::Q_AVAILABLE, $run, $SLIP_Utils::States::Q_PROCESSING});
}
return $num_inprocess;
}
# ---------------------------------------------------------------------
=item insert_restore_errors_to_queue
Description
=cut
# ---------------------------------------------------------------------
sub insert_restore_errors_to_queue {
my ($C, $dbh, $run, $type) = @_;
my $sth;
my $statement;
my $num_inserted = 0;
__LOCK_TABLES($dbh, qw(slip_errors slip_queue slip_indexed));
$statement = qq{SELECT id, shard FROM slip_errors WHERE run=?};
if (defined $type) {
$statement .= qq{ AND reason=?};
$sth = DbUtils::prep_n_execute($dbh, $statement, $run, $type);
DEBUG('lsdb', qq{DEBUG: $statement : $run $type});
}
else {
$sth = DbUtils::prep_n_execute($dbh, $statement, $run);
DEBUG('lsdb', qq{DEBUG: $statement : $run});
}
my $ref_to_ary_of_hashref = $sth->fetchall_arrayref({});
foreach my $ref (@$ref_to_ary_of_hashref) {
my $id = $ref->{id};
my $shard = $ref->{shard};
unless ($shard) {
# See if this got indexed added to slip_errors with shard 0 and in
# the meantime successfully indexed to to a different shard.
my $real_shard = Select_item_id_shard($C, $dbh, $run, $id);
$shard = $real_shard if ($real_shard);
}
my $num = 0;
$statement = qq{REPLACE INTO slip_queue SET run=?, shard=?, id=?, pid=0, host='', proc_status=?};
$sth = DbUtils::prep_n_execute($dbh, $statement, $run, $shard, $id, $SLIP_Utils::States::Q_AVAILABLE, \$num);
DEBUG('lsdb', qq{DEBUG: $statement : $run, $id, $SLIP_Utils::States::Q_AVAILABLE});
$num_inserted += $num;