~drizzle-trunk/drizzle/development

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
# -*- cperl -*-
# Copyright (C) 2006 MySQL AB
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# This is a library file used by the Perl version of drizzle-test-run,
# and is part of the translation of the Bourne shell script with the
# same name.

use strict;

# Private IM-related operations.

sub dtr_im_kill_process ($$$$);
sub dtr_im_load_pids ($);
sub dtr_im_terminate ($);
sub dtr_im_check_alive ($);
sub dtr_im_check_main_alive ($);
sub dtr_im_check_angel_alive ($);
sub dtr_im_check_drizzleds_alive ($);
sub dtr_im_check_drizzled_alive ($);
sub dtr_im_cleanup ($);
sub dtr_im_rm_file ($);
sub dtr_im_errlog ($);
sub dtr_im_kill ($);
sub dtr_im_wait_for_connection ($$$);
sub dtr_im_wait_for_drizzled($$$);

# Public IM-related operations.

sub dtr_im_start ($$);
sub dtr_im_stop ($);

##############################################################################
#
#  Private operations.
#
##############################################################################

sub dtr_im_kill_process ($$$$) {
  my $pid_lst= shift;
  my $signal= shift;
  my $total_retries= shift;
  my $timeout= shift;

  my %pids;

  foreach my $pid ( @{$pid_lst} )
  {
    $pids{$pid}= 1;
  }

  for ( my $cur_attempt= 1; $cur_attempt <= $total_retries; ++$cur_attempt )
  {
    foreach my $pid ( keys %pids )
    {
      dtr_debug("Sending $signal to $pid...");

      kill($signal, $pid);

      unless ( kill (0, $pid) )
      {
        dtr_debug("Process $pid died.");
        delete $pids{$pid};
      }
    }

    return if scalar keys %pids == 0;

    dtr_debug("Sleeping $timeout second(s) waiting for processes to die...");

    sleep($timeout);
  }

  dtr_debug("Process(es) " .
            join(' ', keys %pids) .
            " is still alive after $total_retries " .
            "of sending signal $signal.");
}

###########################################################################

sub dtr_im_load_pids($) {
  my $im= shift;

  dtr_debug("Loading PID files...");

  # Obtain drizzled-process pids.

  my $instances = $im->{'instances'};

  for ( my $idx= 0; $idx < 2; ++$idx )
  {
    dtr_debug("IM-guarded drizzled[$idx] PID file: '" .
              $instances->[$idx]->{'path_pid'} . "'.");

    my $drizzled_pid;

    if ( -r $instances->[$idx]->{'path_pid'} )
    {
      $drizzled_pid= dtr_get_pid_from_file($instances->[$idx]->{'path_pid'});
      dtr_debug("IM-guarded drizzled[$idx] PID: $drizzled_pid.");
    }
    else
    {
      $drizzled_pid= undef;
      dtr_debug("IM-guarded drizzled[$idx]: no PID file.");
    }

    $instances->[$idx]->{'pid'}= $drizzled_pid;
  }

  # Re-read Instance Manager PIDs from the file, since during tests Instance
  # Manager could have been restarted, so its PIDs could have been changed.

  #   - IM-main

  dtr_debug("IM-main PID file: '$im->{path_pid}'.");

  if ( -f $im->{'path_pid'} )
  {
    $im->{'pid'} =
      dtr_get_pid_from_file($im->{'path_pid'});

    dtr_debug("IM-main PID: $im->{pid}.");
  }
  else
  {
    dtr_debug("IM-main: no PID file.");
    $im->{'pid'}= undef;
  }

  #   - IM-angel

  dtr_debug("IM-angel PID file: '$im->{path_angel_pid}'.");

  if ( -f $im->{'path_angel_pid'} )
  {
    $im->{'angel_pid'} =
      dtr_get_pid_from_file($im->{'path_angel_pid'});

    dtr_debug("IM-angel PID: $im->{'angel_pid'}.");
  }
  else
  {
    dtr_debug("IM-angel: no PID file.");
    $im->{'angel_pid'} = undef;
  }
}

###########################################################################

sub dtr_im_terminate($) {
  my $im= shift;

  # Load pids from pid-files. We should do it first of all, because IM deletes
  # them on shutdown.

  dtr_im_load_pids($im);

  dtr_debug("Shutting Instance Manager down...");

  # Ignoring SIGCHLD so that all children could rest in peace.

  start_reap_all();

  # Send SIGTERM to IM-main.

  if ( defined $im->{'pid'} )
  {
    dtr_debug("IM-main pid: $im->{pid}.");
    dtr_debug("Stopping IM-main...");

    dtr_im_kill_process([ $im->{'pid'} ], 'TERM', 10, 1);
  }
  else
  {
    dtr_debug("IM-main pid: n/a.");
  }

  # If IM-angel was alive, wait for it to die.

  if ( defined $im->{'angel_pid'} )
  {
    dtr_debug("IM-angel pid: $im->{'angel_pid'}.");
    dtr_debug("Waiting for IM-angel to die...");

    my $total_attempts= 10;

    for ( my $cur_attempt=1; $cur_attempt <= $total_attempts; ++$cur_attempt )
    {
      unless ( kill (0, $im->{'angel_pid'}) )
      {
        dtr_debug("IM-angel died.");
        last;
      }

      sleep(1);
    }
  }
  else
  {
    dtr_debug("IM-angel pid: n/a.");
  }

  stop_reap_all();

  # Re-load PIDs.

  dtr_im_load_pids($im);
}

###########################################################################

sub dtr_im_check_alive($) {
  my $im= shift;

  dtr_debug("Checking whether IM-components are alive...");

  return 1 if dtr_im_check_main_alive($im);

  return 1 if dtr_im_check_angel_alive($im);

  return 1 if dtr_im_check_drizzleds_alive($im);

  return 0;
}

###########################################################################

sub dtr_im_check_main_alive($) {
  my $im= shift;

  # Check that the process, that we know to be IM's, is dead.

  if ( defined $im->{'pid'} )
  {
    if ( kill (0, $im->{'pid'}) )
    {
      dtr_debug("IM-main (PID: $im->{pid}) is alive.");
      return 1;
    }
    else
    {
      dtr_debug("IM-main (PID: $im->{pid}) is dead.");
    }
  }
  else
  {
    dtr_debug("No PID file for IM-main.");
  }

  # Check that IM does not accept client connections.

  if ( dtr_ping_port($im->{'port'}) )
  {
    dtr_debug("IM-main (port: $im->{port}) " .
              "is accepting connections.");

    dtr_im_errlog("IM-main is accepting connections on port " .
                  "$im->{port}, but there is no " .
                  "process information.");
    return 1;
  }
  else
  {
    dtr_debug("IM-main (port: $im->{port}) " .
              "does not accept connections.");
    return 0;
  }
}

###########################################################################

sub dtr_im_check_angel_alive($) {
  my $im= shift;

  # Check that the process, that we know to be the Angel, is dead.

  if ( defined $im->{'angel_pid'} )
  {
    if ( kill (0, $im->{'angel_pid'}) )
    {
      dtr_debug("IM-angel (PID: $im->{angel_pid}) is alive.");
      return 1;
    }
    else
    {
      dtr_debug("IM-angel (PID: $im->{angel_pid}) is dead.");
      return 0;
    }
  }
  else
  {
    dtr_debug("No PID file for IM-angel.");
    return 0;
  }
}

###########################################################################

sub dtr_im_check_drizzleds_alive($) {
  my $im= shift;

  dtr_debug("Checking for IM-guarded drizzled instances...");

  my $instances = $im->{'instances'};

  for ( my $idx= 0; $idx < 2; ++$idx )
  {
    dtr_debug("Checking drizzled[$idx]...");

    return 1
      if dtr_im_check_drizzled_alive($instances->[$idx]);
  }
}

###########################################################################

sub dtr_im_check_drizzled_alive($) {
  my $drizzled_instance= shift;

  # Check that the process is dead.

  if ( defined $drizzled_instance->{'pid'} )
  {
    if ( kill (0, $drizzled_instance->{'pid'}) )
    {
      dtr_debug("drizzled instance (PID: $drizzled_instance->{pid}) is alive.");
      return 1;
    }
    else
    {
      dtr_debug("drizzled instance (PID: $drizzled_instance->{pid}) is dead.");
    }
  }
  else
  {
    dtr_debug("No PID file for drizzled instance.");
  }

  # Check that drizzled does not accept client connections.

  if ( dtr_ping_port($drizzled_instance->{'port'}) )
  {
    dtr_debug("drizzled instance (port: $drizzled_instance->{port}) " .
              "is accepting connections.");

    dtr_im_errlog("drizzled is accepting connections on port " .
                  "$drizzled_instance->{port}, but there is no " .
                  "process information.");
    return 1;
  }
  else
  {
    dtr_debug("drizzled instance (port: $drizzled_instance->{port}) " .
              "does not accept connections.");
    return 0;
  }
}

###########################################################################

sub dtr_im_cleanup($) {
  my $im= shift;

  dtr_im_rm_file($im->{'path_pid'});
  dtr_im_rm_file($im->{'path_sock'});

  dtr_im_rm_file($im->{'path_angel_pid'});

  for ( my $idx= 0; $idx < 2; ++$idx )
  {
    dtr_im_rm_file($im->{'instances'}->[$idx]->{'path_pid'});
    dtr_im_rm_file($im->{'instances'}->[$idx]->{'path_sock'});
  }
}

###########################################################################

sub dtr_im_rm_file($)
{
  my $file_path= shift;

  if ( -f $file_path )
  {
    dtr_debug("Removing '$file_path'...");

    unless ( unlink($file_path) )
    {
      dtr_warning("Can not remove '$file_path'.")
    }
  }
  else
  {
    dtr_debug("File '$file_path' does not exist already.");
  }
}

###########################################################################

sub dtr_im_errlog($) {
  my $msg= shift;

  # Complain in error log so that a warning will be shown.
  # 
  # TODO: unless BUG#20761 is fixed, we will print the warning to stdout, so
  # that it can be seen on console and does not produce pushbuild error.

  # my $errlog= "$opt_vardir/log/drizzle-test-run.pl.err";
  # 
  # open (ERRLOG, ">>$errlog") ||
  #   dtr_error("Can not open error log ($errlog)");
  # 
  # my $ts= localtime();
  # print ERRLOG
  #   "Warning: [$ts] $msg\n";
  # 
  # close ERRLOG;

  my $ts= localtime();
  print "Warning: [$ts] $msg\n";
}

###########################################################################

sub dtr_im_kill($) {
  my $im= shift;

  # Re-load PIDs. That can be useful because some processes could have been
  # restarted.

  dtr_im_load_pids($im);

  # Ignoring SIGCHLD so that all children could rest in peace.

  start_reap_all();

  # Kill IM-angel first of all.

  if ( defined $im->{'angel_pid'} )
  {
    dtr_debug("Killing IM-angel (PID: $im->{angel_pid})...");
    dtr_im_kill_process([ $im->{'angel_pid'} ], 'KILL', 10, 1)
  }
  else
  {
    dtr_debug("IM-angel is dead.");
  }

  # Re-load PIDs again.

  dtr_im_load_pids($im);

  # Kill IM-main.
  
  if ( defined $im->{'pid'} )
  {
    dtr_debug("Killing IM-main (PID: $im->pid})...");
    dtr_im_kill_process([ $im->{'pid'} ], 'KILL', 10, 1);
  }
  else
  {
    dtr_debug("IM-main is dead.");
  }

  # Re-load PIDs again.

  dtr_im_load_pids($im);

  # Kill guarded drizzled instances.

  my @drizzled_pids;

  dtr_debug("Collecting PIDs of drizzled instances to kill...");

  for ( my $idx= 0; $idx < 2; ++$idx )
  {
    my $pid= $im->{'instances'}->[$idx]->{'pid'};

    unless ( defined $pid )
    {
      next;
    }

    dtr_debug("  - IM-guarded drizzled[$idx] PID: $pid.");

    push (@drizzled_pids, $pid);
  }

  if ( scalar @drizzled_pids > 0 )
  {
    dtr_debug("Killing IM-guarded drizzled instances...");
    dtr_im_kill_process(\@drizzled_pids, 'KILL', 10, 1);
  }

  # That's all.

  stop_reap_all();
}

##############################################################################

sub dtr_im_wait_for_connection($$$) {
  my $im= shift;
  my $total_attempts= shift;
  my $connect_timeout= shift;

  dtr_debug("Waiting for IM on port $im->{port} " .
            "to start accepting connections...");

  for ( my $cur_attempt= 1; $cur_attempt <= $total_attempts; ++$cur_attempt )
  {
    dtr_debug("Trying to connect to IM ($cur_attempt of $total_attempts)...");

    if ( dtr_ping_port($im->{'port'}) )
    {
      dtr_debug("IM is accepting connections " .
                "on port $im->{port}.");
      return 1;
    }

    dtr_debug("Sleeping $connect_timeout...");
    sleep($connect_timeout);
  }

  dtr_debug("IM does not accept connections " .
            "on port $im->{port} after " .
            ($total_attempts * $connect_timeout) . " seconds.");

  return 0;
}

##############################################################################

sub dtr_im_wait_for_drizzled($$$) {
  my $drizzled= shift;
  my $total_attempts= shift;
  my $connect_timeout= shift;

  dtr_debug("Waiting for IM-guarded drizzled on port $drizzled->{port} " .
            "to start accepting connections...");

  for ( my $cur_attempt= 1; $cur_attempt <= $total_attempts; ++$cur_attempt )
  {
    dtr_debug("Trying to connect to drizzled " .
              "($cur_attempt of $total_attempts)...");

    if ( dtr_ping_port($drizzled->{'port'}) )
    {
      dtr_debug("drizzled is accepting connections " .
                "on port $drizzled->{port}.");
      return 1;
    }

    dtr_debug("Sleeping $connect_timeout...");
    sleep($connect_timeout);
  }

  dtr_debug("drizzled does not accept connections " .
            "on port $drizzled->{port} after " .
            ($total_attempts * $connect_timeout) . " seconds.");

  return 0;
}

##############################################################################
#
#  Public operations.
#
##############################################################################

sub dtr_im_start($$) {
  my $im = shift;
  my $opts = shift;

  dtr_debug("Starting Instance Manager...");

  my $args;
  dtr_init_args(\$args);
  dtr_add_arg($args, "--defaults-file=%s", $im->{'defaults_file'});

  foreach my $opt ( @{$opts} )
  {
    dtr_add_arg($args, $opt);
  }

  $im->{'spawner_pid'} =
    dtr_spawn(
      $::exe_im,                        # path to the executable
      $args,                            # cmd-line args
      '',                               # stdin
      $im->{'path_log'},                # stdout
      $im->{'path_err'},                # stderr
      '',                               # pid file path (not used)
      { append_log_file => 1 }          # append log files
      );

  unless ( $im->{'spawner_pid'} )
  {
    dtr_error('Could not start Instance Manager.')
  }

  # Instance Manager can be run in daemon mode. In this case, it creates
  # several processes and the parent process, created by dtr_spawn(), exits just
  # after start. So, we have to obtain Instance Manager PID from the PID file.

  dtr_debug("Waiting for IM to create PID file (" .
            "path: '$im->{path_pid}'; " .
            "timeout: $im->{start_timeout})...");

  unless ( sleep_until_file_created($im->{'path_pid'},
                                    $im->{'start_timeout'},
                                    -1) ) # real PID is still unknown
  {
    dtr_debug("IM has not created PID file in $im->{start_timeout} secs.");
    dtr_debug("Aborting test suite...");

    dtr_kill_leftovers();

    dtr_report("IM has not created PID file in $im->{start_timeout} secs.");
    return 0;
  }

  $im->{'pid'}= dtr_get_pid_from_file($im->{'path_pid'});

  dtr_debug("Instance Manager started. PID: $im->{pid}.");

  # Wait until we can connect to IM.

  my $IM_CONNECT_TIMEOUT= 30;

  unless ( dtr_im_wait_for_connection($im,
                                      $IM_CONNECT_TIMEOUT, 1) )
  {
    dtr_debug("Can not connect to Instance Manager " .
              "in $IM_CONNECT_TIMEOUT seconds after start.");
    dtr_debug("Aborting test suite...");

    dtr_kill_leftovers();

    dtr_report("Can not connect to Instance Manager " .
               "in $IM_CONNECT_TIMEOUT seconds after start.");
    return 0;
  }

  # Wait for IM to start guarded instances:
  #   - wait for PID files;

  dtr_debug("Waiting for guarded drizzleds instances to create PID files...");

  for ( my $idx= 0; $idx < 2; ++$idx )
  {
    my $drizzled= $im->{'instances'}->[$idx];

    if ( exists $drizzled->{'nonguarded'} )
    {
      next;
    }

    dtr_debug("Waiting for drizzled[$idx] to create PID file (" .
              "path: '$drizzled->{path_pid}'; " .
              "timeout: $drizzled->{start_timeout})...");

    unless ( sleep_until_file_created($drizzled->{'path_pid'},
                                      $drizzled->{'start_timeout'},
                                      -1) ) # real PID is still unknown
    {
      dtr_debug("drizzled[$idx] has not created PID file in " .
                 "$drizzled->{start_timeout} secs.");
      dtr_debug("Aborting test suite...");

      dtr_kill_leftovers();

      dtr_report("drizzled[$idx] has not created PID file in " .
                 "$drizzled->{start_timeout} secs.");
      return 0;
    }

    dtr_debug("PID file for drizzled[$idx] ($drizzled->{path_pid} created.");
  }

  # Wait until we can connect to guarded drizzled-instances
  # (in other words -- wait for IM to start guarded instances).

  dtr_debug("Waiting for guarded drizzleds to start accepting connections...");

  for ( my $idx= 0; $idx < 2; ++$idx )
  {
    my $drizzled= $im->{'instances'}->[$idx];

    if ( exists $drizzled->{'nonguarded'} )
    {
      next;
    }

    dtr_debug("Waiting for drizzled[$idx] to accept connection...");

    unless ( dtr_im_wait_for_drizzled($drizzled, 30, 1) )
    {
      dtr_debug("Can not connect to drizzled[$idx] " .
                "in $IM_CONNECT_TIMEOUT seconds after start.");
      dtr_debug("Aborting test suite...");

      dtr_kill_leftovers();

      dtr_report("Can not connect to drizzled[$idx] " .
                 "in $IM_CONNECT_TIMEOUT seconds after start.");
      return 0;
    }

    dtr_debug("drizzled[$idx] started.");
  }

  dtr_debug("Instance Manager and its components are up and running.");

  return 1;
}

##############################################################################

sub dtr_im_stop($) {
  my $im= shift;

  dtr_debug("Stopping Instance Manager...");

  # Try graceful shutdown.

  dtr_im_terminate($im);

  # Check that all processes died.

  unless ( dtr_im_check_alive($im) )
  {
    dtr_debug("Instance Manager has been stopped successfully.");
    dtr_im_cleanup($im);
    return 1;
  }

  # Instance Manager don't want to die. We should kill it.

  dtr_im_errlog("Instance Manager did not shutdown gracefully.");

  dtr_im_kill($im);

  # Check again that all IM-related processes have been killed.

  my $im_is_alive= dtr_im_check_alive($im);

  dtr_im_cleanup($im);

  if ( $im_is_alive )
  {
    dtr_debug("Can not kill Instance Manager or its children.");
    return 0;
  }

  dtr_debug("Instance Manager has been killed successfully.");
  return 1;
}

###########################################################################

1;