From e6d5b968fd96ae9c0adc91e1414a8fa7864cece0 Mon Sep 17 00:00:00 2001
From: "Dmitry E. Oboukhov" <unera@debian.org>
Date: Mon, 21 Jan 2013 14:58:36 +0400
Subject: [PATCH] Debian infrastructure uses opt preffix

https://bugs.launchpad.net/tarantool/+bug/1090381
---
 debian/README.Debian                          | 12 ++-
 debian/etc/example.cfg                        | 19 +++-
 debian/scripts/tarantool_instance             |  4 +-
 debian/scripts/tarantool_snapshot_rotate      | 98 ++++++++++++++-----
 ...ron.daily => tarantool-common.cron.hourly} |  2 +-
 5 files changed, 103 insertions(+), 32 deletions(-)
 rename debian/{tarantool-common.cron.daily => tarantool-common.cron.hourly} (72%)

diff --git a/debian/README.Debian b/debian/README.Debian
index d86419463c..bc2b491544 100644
--- a/debian/README.Debian
+++ b/debian/README.Debian
@@ -1,15 +1,19 @@
 Debian package provides some additional options in config-file:
 
 
-* file_descriptors = INTVALUE
+* opt file_descriptors = INTVALUE
 
   Init script will do 'ulimit -f INTVALUE' command before starting tarantool.
 
-* save_snapshots = COUNT
+* opt save_snapshots = COUNT
   
   Count of snapshots to save (default = 10). COUNT=0 disables removing
   old snapshots.
 
+* opt snapshot_period = HOURS
 
-There are script tarantool_snapshot_rotate (1) that is started every day
-using cron.daily.
+  Period between two snapshot (default 24).
+
+
+There are script tarantool_snapshot_rotate (1) that is started every hour
+using cron.hourly.
diff --git a/debian/etc/example.cfg b/debian/etc/example.cfg
index 5a42a36734..8c2bd75eb1 100644
--- a/debian/etc/example.cfg
+++ b/debian/etc/example.cfg
@@ -16,10 +16,25 @@ primary_port = 33013
 # Read-only port.
 secondary_port = 33014
 
-# 
+# ************* Debian option ***********************
 # count of file descriptors
 #
-file_descriptors = 8192
+opt file_descriptors = 8192
+# ***************************************************
+
+# ************* Debian option ***********************
+#  Count of snapshots to save (default = 10). COUNT=0
+# disables removing
+#
+opt save_snapshots = 10
+# ***************************************************
+  
+
+# ************* Debian option ***********************
+# Snapshot creating period (hours), (default = 24)
+#
+opt snapshot_period = 12
+# ***************************************************
 
 #
 # The port for administrative commands.
diff --git a/debian/scripts/tarantool_instance b/debian/scripts/tarantool_instance
index e03ee4fc31..bb2131a037 100755
--- a/debian/scripts/tarantool_instance
+++ b/debian/scripts/tarantool_instance
@@ -59,10 +59,11 @@ RUNDIR=$SNAPSHOT_DIR/$NAME
 LOG=$LOG_DIR/$NAME.log
 LOGGER="exec /usr/lib/tarantool/logger $LOG"
 SOCKETS=`grep \
-    '^[[:space:]]*file_descriptors[[:space:]]*=[[:space:]]*[[:digit:]]\+' $CFG \
+    '^[[:space:]]*\(opt[[:space:]]\+\)\?file_descriptors[[:space:]]*=[[:space:]]*[[:digit:]]\+' $CFG \
     | tail -n 1 \
     | sed 's/[^[:digit:]]//g'`
 
+
 if test -z $SOCKETS; then
     SOCKETS=1023
 fi
@@ -104,6 +105,7 @@ if [ $ACTION = 'start' ]; then
     echo -n "    Starting '$NAME' ... "
 else
     echo -n "    Stopping '$NAME' ... "
+    SSDARGS="--retry 15 $SSDARGS"
 fi
 
 if $SSD --$ACTION $SSDARGS $BOX -- -B -v -c $SCFG; then
diff --git a/debian/scripts/tarantool_snapshot_rotate b/debian/scripts/tarantool_snapshot_rotate
index 6b24336cc3..0fd2c29cc0 100644
--- a/debian/scripts/tarantool_snapshot_rotate
+++ b/debian/scripts/tarantool_snapshot_rotate
@@ -16,11 +16,12 @@ use Data::Dumper;
 
 pod2usage(-exitstatus => 0, -verbose => 2) unless
     GetOptions
-        'help|h'            => \my $help,
-        'verbose|v'         => \my $verbose,
-        'snapshots|s=i'     => \my $snapshots,
+        'help|h'                => \my $help,
+        'verbose|v'             => \my $verbose,
+        'snapshots|s=i'         => \my $snapshots,
+        'snapshot_period|p=i'   => \my $period,
 ;
-$snapshots //= 10;
+
 
 sub DEBUGF($;@) {
     return unless $verbose;
@@ -52,11 +53,20 @@ sub list_files($) {
     return @files;
 }
 
-sub rotate_snapshot($) {
-    my ($pidfile) = @_;
+sub rotate_snapshot($$$) {
+    my ($pidfile, $snap_count, $snap_period) = @_;
+
+    $snap_count = $snapshots || 10 unless defined $snap_count;
+    $snap_period = $period || 24 unless defined $snap_period;
+    $snap_count = $snapshots if defined $snapshots;
+    $snap_period = $period if defined $period;
+    $snap_period ||= 0;
 
     my $pid;
 
+    DEBUGF "\tBegin rotating process period=%s, count=%s",
+        $snap_period, $snap_count;
+
 
     if (open my $ph, '<', $pidfile) {
         $pid = <$ph>;
@@ -71,19 +81,42 @@ sub rotate_snapshot($) {
         return;
     }
 
+
     my $sndir = catfile SNAPSHOT_DIR, basename $pidfile, '.pid';
     my @files = list_files $sndir;
     goto FINISH unless @files;
 
+    my ($last_snap) = grep /\.snap$/, reverse @files;
+    if ($last_snap) {{
+        my @stat = stat $last_snap;
+        last unless @stat;
+        my $ctime = $stat[10];
+        if (time - $ctime < $snap_period * 3600 - 3600 / 2) {
+            DEBUGF "\tLast snapshot was created %3.2f hours ago, ".
+                "do not create new",
+                (time - $ctime) / 3600;
+            return;
+        }
+        DEBUGF "\tLast snapshot was created %3.2f hours ago, creating new",
+            (time - $ctime) / 3600;
+    }} else {
+        DEBUGF "\tLast snapshot was not found, creating new";
+    }
+
     while(@files and $files[0] =~ /\.xlog$/) {
-        DEBUGF 'Remove orphaned %s', $files[0];
+        DEBUGF "\tRemove orphaned %s", $files[0];
         unless (unlink $files[0]) {
-            DEBUGF "Can't unlink file %s: %s", $files[0], $!;
+            DEBUGF "\tCan't unlink file %s: %s", $files[0], $!;
             return;
         }
         shift @files;
     }
 
+    unless (kill 0 => $pid) {
+        DEBUGF "\tProcess %s is not started", $pidfile;
+        return;
+    }
+
     if (kill USR1 => $pid) {
         goto FINISH unless @files;
         for (my $i = 0; $i < 5; $i++) {
@@ -91,7 +124,7 @@ sub rotate_snapshot($) {
             my @inpr = sort glob catfile SNAPSHOT_DIR, '*.snap.inprogress';
             last unless @inpr;
             if ($inpr[-1] and $inpr[-1] gt $files[-1]) {
-                DEBUGF "snapshot %s is still in progress...", $inpr[-1];
+                DEBUGF "\tsnapshot %s is still in progress...", $inpr[-1];
                 next;
             }
         }
@@ -100,31 +133,33 @@ sub rotate_snapshot($) {
         return;
     }
 
-    if ($snapshots) {
+    if ($snap_count) {
         @files = list_files $sndir;
         my $snaps = grep /\.snap$/, @files;
-        if ($snaps > $snapshots) {
-            my $to_remove = $snaps - $snapshots;
+        if ($snaps > $snap_count) {
+            my $to_remove = $snaps - $snap_count;
 
             while (@files) {
                 my $file = shift @files;
                 $to_remove-- if $file =~ /\.snap$/;
-                DEBUGF "Unlink file: %s...", $file;
+                DEBUGF "\tUnlink file: %s...", $file;
                 unless (unlink $file) {
-                    DEBUGF "Can't unlink file %s: %s", $file, $!;
+                    DEBUGF "\tCan't unlink file %s: %s", $file, $!;
                     return;
                 }
                 last unless $to_remove > 0;
             }
             while(@files and $files[0] =~ /\.xlog$/) {
-                DEBUGF 'Remove orphaned %s', $files[0];
+                DEBUGF "\tRemove orphaned %s", $files[0];
                 unless (unlink $files[0]) {
-                    DEBUGF "Can't unlink file %s: %s", $files[0], $!;
+                    DEBUGF "\tCan't unlink file %s: %s", $files[0], $!;
                     return;
                 }
                 shift @files;
             }
         }
+    } else {
+        DEBUGF "\tDon't remove any old snapshots";
     }
 
     FINISH:
@@ -145,22 +180,37 @@ for (glob catfile PID_DIR, '*.pid') {
         my @lines = <$fh>;
 
         my ($user_snapshots) =
-            grep /^\s*save_snapshots\s*=\s*\d+\s*(?:#.*)?$/,
+            grep /^\s*(?:opt\s+)?save_snapshots\s*=\s*\d+\s*(?:#.*)?$/,
+                reverse @lines;
+
+        my ($snapshot_period) =
+            grep /^\s*(?:opt\s+)?snapshot_period\s*=\s*\d+\s*(?:#.*)?$/,
                 reverse @lines;
 
         if ($user_snapshots) {
             for ($user_snapshots) {
                 s/#.*//;
-                s/\D+//g;
+                s/\D//g;
+            }
+            unless($user_snapshots =~ /^[1-9]\d*$/) {
+                warn "wrong format of save_snapshots\n";
+                $user_snapshots = undef;
+            }
+        }
+
+        if ($snapshot_period) {
+            for ($snapshot_period) {
+                s/#.*//;
+                s/\D//g;
             }
-            DEBUGF "Found user's option save_snapshots=%s, use it",
-                $user_snapshots;
-            $snapshots = $user_snapshots;
-        } else {
-            DEBUGF "Use default value: save_snapshots=%s", $snapshots;
+            unless($snapshot_period =~ /^[1-9]\d*$/) {
+                warn "wrong format of snapshot_period\n";
+                $snapshot_period = undef;
+            }
+
         }
 
-        rotate_snapshot $_;
+        rotate_snapshot $_, $user_snapshots, $snapshot_period;
 
 
     } else {
diff --git a/debian/tarantool-common.cron.daily b/debian/tarantool-common.cron.hourly
similarity index 72%
rename from debian/tarantool-common.cron.daily
rename to debian/tarantool-common.cron.hourly
index 8c57a6b0dc..f9854d38a2 100644
--- a/debian/tarantool-common.cron.daily
+++ b/debian/tarantool-common.cron.hourly
@@ -3,4 +3,4 @@
 test -x /usr/bin/tarantool_box || exit 0
 test -x /usr/sbin/tarantool_snapshot_rotate || exit 0
 
-/usr/sbin/tarantool_snapshot_rotate
+exec /usr/sbin/tarantool_snapshot_rotate
-- 
GitLab