2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: planner.c 10421 2008-03-06 18:48:30Z martineau $
29 * backup schedule planner for the Amanda backup system.
44 #include "amfeatures.h"
45 #include "server_util.h"
47 #include "timestamp.h"
50 #define planner_debug(i,x) do { \
51 if ((i) <= debug_planner) { \
56 #define MAX_LEVELS 3 /* max# of estimates per filesys */
58 #define RUNS_REDZONE 5 /* should be in conf file? */
60 #define PROMOTE_THRESHOLD 0.05 /* if <5% unbalanced, don't promote */
61 #define DEFAULT_DUMPRATE 1024.0 /* K/s */
63 /* configuration file stuff */
66 gint64 conf_maxdumpsize;
69 int conf_runspercycle;
74 int conf_usetimestamps;
76 #define HOST_READY ((void *)0) /* must be 0 */
77 #define HOST_ACTIVE ((void *)1)
78 #define HOST_DONE ((void *)2)
80 #define DISK_READY 0 /* must be 0 */
82 #define DISK_PARTIALY_DONE 2
85 typedef struct one_est_s {
87 gint64 nsize; /* native size */
88 gint64 csize; /* compressed size */
90 int guessed; /* If server guessed the estimate size */
92 static one_est_t default_one_est = {-1, -1, -1, "INVALID_DATE", 0};
94 typedef struct est_s {
100 one_est_t estimate[MAX_LEVELS];
102 gint64 last_lev0size;
107 double fullrate, incrrate;
108 double fullcomp, incrcomp;
113 #define est(dp) ((est_t *)(dp)->up)
115 /* pestq = partial estimate */
116 disklist_t startq, waitq, pestq, estq, failq, schedq;
118 double total_lev0, balanced_size, balance_threshold;
124 size_t tt_blocksize_kb;
125 int runs_per_cycle = 0;
127 char *planner_timestamp = NULL;
129 static am_feature_t *our_features = NULL;
130 static char *our_feature_string = NULL;
132 /* We keep a LIFO queue of before images for all modifications made
133 * to schedq in our attempt to make the schedule fit on the tape.
134 * Enough information is stored to reinstate a dump if it turns out
135 * that it shouldn't have been touched after all.
137 typedef struct bi_s {
140 int deleted; /* 0=modified, 1=deleted */
141 disk_t *dp; /* The disk that was changed */
142 int level; /* The original level */
143 gint64 nsize; /* The original native size */
144 gint64 csize; /* The original compressed size */
145 char *errstr; /* A message describing why this disk is here */
148 typedef struct bilist_s {
152 bilist_t biq; /* The BI queue itself */
155 * ========================================================================
160 static void setup_estimate(disk_t *dp);
161 static void get_estimates(void);
162 static void analyze_estimate(disk_t *dp);
163 static void handle_failed(disk_t *dp);
164 static void delay_dumps(void);
165 static int promote_highest_priority_incremental(void);
166 static int promote_hills(void);
167 static void output_scheduleline(disk_t *dp);
168 static void server_estimate(disk_t *dp, int i, info_t *info, int level);
169 int main(int, char **);
185 times_t section_start;
189 config_overrides_t *cfg_ovr = NULL;
190 char *cfg_opt = NULL;
192 int exit_status = EXIT_SUCCESS;
193 gboolean no_taper = FALSE;
194 gboolean from_client = FALSE;
196 if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
197 printf("planner-%s\n", VERSION);
202 * Configure program for internationalization:
203 * 1) Only set the message locale for now.
204 * 2) Set textdomain for all amanda related programs to "amanda"
205 * We don't want to be forced to support dozens of message catalogs.
207 setlocale(LC_MESSAGES, "C");
208 textdomain("amanda");
210 /* drop root privileges */
211 planner_setuid = set_root_privs(0);
215 set_pname("planner");
217 dbopen(DBG_SUBDIR_SERVER);
219 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
223 set_config_overrides(cfg_ovr);
224 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
226 /* conf_diskfile is freed later, as it may be used in an error message */
227 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
228 read_diskfile(conf_diskfile, &origq);
229 disable_skip_disk(&origq);
231 /* Don't die when child closes pipe */
232 signal(SIGPIPE, SIG_IGN);
234 setvbuf(stderr, (char *)NULL, (int)_IOLBF, 0);
236 add_amanda_log_handler(amanda_log_stderr);
237 add_amanda_log_handler(amanda_log_trace_log);
239 if (!planner_setuid) {
240 error(_("planner must be run setuid root"));
243 if (config_errors(NULL) >= CFGERR_ERRORS) {
244 g_critical(_("errors processing config file"));
249 check_running_as(RUNNING_AS_ROOT | RUNNING_AS_UID_ONLY);
251 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
254 section_start = curclock();
256 our_features = am_init_feature_set();
257 our_feature_string = am_feature_to_string(our_features);
259 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
260 g_fprintf(stderr, _("%s: pid %ld executable %s version %s\n"),
261 get_pname(), (long) getpid(), argv[0], VERSION);
262 for (i = 0; version_info[i] != NULL; i++)
263 g_fprintf(stderr, _("%s: %s"), get_pname(), version_info[i]);
266 if (argc - diskarg_offset > 1 && strcmp(argv[diskarg_offset], "--starttime") == 0) {
267 planner_timestamp = stralloc(argv[diskarg_offset+1]);
270 if (argc - diskarg_offset > 0 && strcmp(argv[diskarg_offset], "--no-taper") == 0) {
274 if (argc - diskarg_offset > 0 && strcmp(argv[diskarg_offset], "--from-client") == 0) {
280 run_server_global_scripts(EXECUTE_ON_PRE_ESTIMATE, get_config_name());
283 * 1. Networking Setup
290 * 2. Read in Configuration Information
292 * All the Amanda configuration files are loaded before we begin.
295 g_fprintf(stderr,_("READING CONF INFO...\n"));
297 if(origq.head == NULL) {
298 error(_("empty disklist \"%s\""), conf_diskfile);
302 amfree(conf_diskfile);
304 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
305 if(read_tapelist(conf_tapelist)) {
306 error(_("could not load tapelist \"%s\""), conf_tapelist);
309 amfree(conf_tapelist);
311 conf_infofile = config_dir_relative(getconf_str(CNF_INFOFILE));
312 if(open_infofile(conf_infofile)) {
313 error(_("could not open info db \"%s\""), conf_infofile);
316 if (check_infofile(conf_infofile, &origq, &errstr) == -1) {
317 log_add(L_WARNING, "problem copying infofile: %s", errstr);
320 amfree(conf_infofile);
322 conf_tapetype = getconf_str(CNF_TAPETYPE);
323 conf_maxdumpsize = getconf_int64(CNF_MAXDUMPSIZE);
324 conf_runtapes = getconf_int(CNF_RUNTAPES);
325 conf_dumpcycle = getconf_int(CNF_DUMPCYCLE);
326 conf_runspercycle = getconf_int(CNF_RUNSPERCYCLE);
327 conf_tapecycle = getconf_int(CNF_TAPECYCLE);
328 conf_etimeout = (time_t)getconf_int(CNF_ETIMEOUT);
329 conf_reserve = getconf_int(CNF_RESERVE);
330 conf_autoflush = getconf_no_yes_all(CNF_AUTOFLUSH);
331 conf_usetimestamps = getconf_boolean(CNF_USETIMESTAMPS);
334 if (planner_timestamp) {
335 if (conf_usetimestamps == 0) {
336 planner_timestamp[8] = '\0';
338 } else if(conf_usetimestamps == 0) {
339 planner_timestamp = get_datestamp_from_time(0);
342 planner_timestamp = get_timestamp_from_time(0);
344 log_add(L_START, _("date %s"), planner_timestamp);
345 g_printf("DATE %s\n", planner_timestamp);
347 g_fprintf(stderr, _("%s: timestamp %s\n"),
348 get_pname(), planner_timestamp);
350 errstr = match_disklist(&origq, argc-diskarg_offset,
351 argv+diskarg_offset);
353 g_fprintf(stderr,"%s",errstr);
354 exit_status = EXIT_FAILURE;
357 for (dp = origq.head; dp != NULL; dp = dp->next) {
360 if (!dp->dump_limit || !dp->dump_limit->same_host)
363 if (dp->dump_limit && !dp->dump_limit->server)
370 for (dp = origq.head; dp != NULL; dp = dp->next) {
372 qname = quote_string(dp->name);
373 log_add(L_DISK, "%s %s", dp->host->hostname, qname);
381 error(_("no DLE to backup; %s"), errstr);
383 error(_("no DLE to backup"));
387 log_add(L_WARNING, "WARNING: %s", errstr);
391 /* some initializations */
393 if(conf_runspercycle == 0) {
394 runs_per_cycle = conf_dumpcycle;
395 } else if(conf_runspercycle == -1 ) {
396 runs_per_cycle = guess_runs_from_tapelist();
398 runs_per_cycle = conf_runspercycle;
400 if (runs_per_cycle <= 0) {
405 * do some basic sanity checking
407 if(conf_tapecycle <= runs_per_cycle) {
408 log_add(L_WARNING, _("tapecycle (%d) <= runspercycle (%d)"),
409 conf_tapecycle, runs_per_cycle);
412 tape = lookup_tapetype(conf_tapetype);
413 if(conf_maxdumpsize > (gint64)0) {
414 tape_length = conf_maxdumpsize;
415 g_fprintf(stderr, "planner: tape_length is set from maxdumpsize (%jd KB)\n",
416 (intmax_t)conf_maxdumpsize);
419 tape_length = tapetype_get_length(tape) * (gint64)conf_runtapes;
420 g_fprintf(stderr, "planner: tape_length is set from tape length (%jd KB) * runtapes (%d) == %jd KB\n",
421 (intmax_t)tapetype_get_length(tape),
423 (intmax_t)tape_length);
425 tape_mark = (size_t)tapetype_get_filemark(tape);
426 tt_blocksize_kb = (size_t)tapetype_get_blocksize(tape);
427 tt_blocksize = tt_blocksize_kb * 1024;
429 g_fprintf(stderr, _("%s: time %s: startup took %s secs\n"),
431 walltime_str(curclock()),
432 walltime_str(timessub(curclock(), section_start)));
435 * 3. Send autoflush dumps left on the holding disks
437 * This should give us something to do while we generate the new
441 g_fprintf(stderr,_("\nSENDING FLUSHES...\n"));
443 if(conf_autoflush && !no_taper) {
445 GSList *holding_list, *holding_file;
446 char *qdisk, *qhname;
448 /* get *all* flushable files in holding, without checking against
449 * the disklist (which may not contain some of the dumps) */
450 holding_list = holding_get_files_for_flush(NULL);
451 for(holding_file=holding_list; holding_file != NULL;
452 holding_file = holding_file->next) {
453 holding_file_get_dumpfile((char *)holding_file->data, &file);
455 if (holding_file_size((char *)holding_file->data, 1) <= 0) {
456 log_add(L_INFO, "%s: removing file with no data.",
457 (char *)holding_file->data);
458 holding_file_unlink((char *)holding_file->data);
459 dumpfile_free_data(&file);
463 /* see if this matches the command-line arguments */
464 if (conf_autoflush == 1 &&
465 !match_dumpfile(&file, argc-diskarg_offset,
466 argv+diskarg_offset)) {
470 qdisk = quote_string(file.disk);
471 qhname = quote_string((char *)holding_file->data);
472 log_add(L_DISK, "%s %s", file.name, qdisk);
474 "FLUSH %s %s %s %d %s\n",
481 "FLUSH %s %s %s %d %s\n",
489 dumpfile_free_data(&file);
491 slist_free_full(holding_list, g_free);
494 g_fprintf(stderr, _("ENDFLUSH\n"));
495 g_fprintf(stdout, _("ENDFLUSH\n"));
499 * 4. Calculate Preliminary Dump Levels
501 * Before we can get estimates from the remote slave hosts, we make a
502 * first attempt at guessing what dump levels we will be dumping at
503 * based on the curinfo database.
506 g_fprintf(stderr,_("\nSETTING UP FOR ESTIMATES...\n"));
507 section_start = curclock();
509 startq.head = startq.tail = NULL;
510 while(!empty(origq)) {
511 disk_t *dp = dequeue_disk(&origq);
517 g_fprintf(stderr, _("%s: time %s: setting up estimates took %s secs\n"),
519 walltime_str(curclock()),
520 walltime_str(timessub(curclock(), section_start)));
524 * 5. Get Dump Size Estimates from Remote Client Hosts
526 * Each host is queried (in parallel) for dump size information on all
527 * of its disks, and the results gathered as they come in.
530 /* go out and get the dump estimates */
532 g_fprintf(stderr,_("\nGETTING ESTIMATES...\n"));
533 section_start = curclock();
535 estq.head = estq.tail = NULL;
536 pestq.head = pestq.tail = NULL;
537 waitq.head = waitq.tail = NULL;
538 failq.head = failq.tail = NULL;
542 g_fprintf(stderr, _("%s: time %s: getting estimates took %s secs\n"),
544 walltime_str(curclock()),
545 walltime_str(timessub(curclock(), section_start)));
548 * At this point, all disks with estimates are in estq, and
549 * all the disks on hosts that didn't respond to our inquiry
553 dump_queue("FAILED", failq, 15, stderr);
554 dump_queue("DONE", estq, 15, stderr);
557 exit_status = EXIT_FAILURE;
561 * 6. Analyze Dump Estimates
563 * Each disk's estimates are looked at to determine what level it
564 * should dump at, and to calculate the expected size and time taking
565 * historical dump rates and compression ratios into account. The
566 * total expected size is accumulated as well.
569 g_fprintf(stderr,_("\nANALYZING ESTIMATES...\n"));
570 section_start = curclock();
572 /* an empty tape still has a label and an endmark */
573 total_size = ((gint64)tt_blocksize_kb + (gint64)tape_mark) * (gint64)2;
577 schedq.head = schedq.tail = NULL;
578 while(!empty(estq)) analyze_estimate(dequeue_disk(&estq));
579 while(!empty(failq)) handle_failed(dequeue_disk(&failq));
581 run_server_global_scripts(EXECUTE_ON_POST_ESTIMATE, get_config_name());
584 * At this point, all the disks are on schedq sorted by priority.
585 * The total estimated size of the backups is in total_size.
591 g_fprintf(stderr, _("INITIAL SCHEDULE (size %lld):\n"),
592 (long long)total_size);
593 for(dp = schedq.head; dp != NULL; dp = dp->next) {
594 qname = quote_string(dp->name);
595 g_fprintf(stderr, _(" %s %s pri %d lev %d nsize %lld csize %lld\n"),
596 dp->host->hostname, qname, est(dp)->dump_priority,
597 est(dp)->dump_est->level,
598 (long long)est(dp)->dump_est->nsize,
599 (long long)est(dp)->dump_est->csize);
606 * 7. Delay Dumps if Schedule Too Big
608 * If the generated schedule is too big to fit on the tape, we need to
609 * delay some full dumps to make room. Incrementals will be done
610 * instead (except for new or forced disks).
612 * In extreme cases, delaying all the full dumps is not even enough.
613 * If so, some low-priority incrementals will be skipped completely
614 * until the dumps fit on the tape.
617 g_fprintf(stderr, _("\nDELAYING DUMPS IF NEEDED, total_size %lld, tape length %lld mark %zu\n"),
618 (long long)total_size,
619 (long long)tape_length,
622 initial_size = total_size;
626 /* XXX - why bother checking this? */
627 if(empty(schedq) && total_size < initial_size) {
628 error(_("cannot fit anything on tape, bailing out"));
634 * 8. Promote Dumps if Schedule Too Small
636 * Amanda attempts to balance the full dumps over the length of the
637 * dump cycle. If this night's full dumps are too small relative to
638 * the other nights, promote some high-priority full dumps that will be
639 * due for the next run, to full dumps for tonight, taking care not to
640 * overflow the tape size.
642 * This doesn't work too well for small sites. For these we scan ahead
643 * looking for nights that have an excessive number of dumps and promote
646 * Amanda never delays full dumps just for the sake of balancing the
647 * schedule, so it can take a full cycle to balance the schedule after
652 _("\nPROMOTING DUMPS IF NEEDED, total_lev0 %1.0lf, balanced_size %1.0lf...\n"),
653 total_lev0, balanced_size);
655 balance_threshold = balanced_size * PROMOTE_THRESHOLD;
657 while((balanced_size - total_lev0) > balance_threshold && moved_one)
658 moved_one = promote_highest_priority_incremental();
660 moved_one = promote_hills();
662 g_fprintf(stderr, _("%s: time %s: analysis took %s secs\n"),
664 walltime_str(curclock()),
665 walltime_str(timessub(curclock(), section_start)));
671 * The schedule goes to stdout, presumably to driver. A copy is written
672 * on stderr for the debug file.
675 g_fprintf(stderr,_("\nGENERATING SCHEDULE:\n--------\n"));
677 exit_status = EXIT_FAILURE;
678 g_fprintf(stderr, _("--> Generated empty schedule! <--\n"));
680 while(!empty(schedq)) output_scheduleline(dequeue_disk(&schedq));
682 g_fprintf(stderr, _("--------\n"));
685 log_add(L_FINISH, _("date %s time %s"), planner_timestamp, walltime_str(curclock()));
686 log_add(L_INFO, "pid-done %ld", (long)getpid());
689 amfree(planner_timestamp);
690 amfree(our_feature_string);
691 am_release_feature_set(our_features);
702 * ========================================================================
703 * SETUP FOR ESTIMATES
707 static void askfor(est_t *, int, int, info_t *);
708 static int last_level(info_t *info); /* subroutines */
709 static one_est_t *est_for_level(disk_t *dp, int level);
710 static void est_csize(disk_t *dp, one_est_t *one_est);
711 static gint64 est_tape_size(disk_t *dp, int level);
712 static int next_level0(disk_t *dp, info_t *info);
713 static int runs_at(info_t *info, int lev);
714 static gint64 bump_thresh(int level, gint64 size_level_0, int bumppercent, gint64 bumpsize, double bumpmult);
715 static int when_overwrite(char *label);
718 est_t *ep, /* esimate data block */
719 int seq, /* sequence number of request */
720 int lev, /* dump level being requested */
721 info_t *info) /* info block for disk */
723 if(seq < 0 || seq >= MAX_LEVELS) {
724 error(_("error [planner askfor: seq out of range 0..%d: %d]"),
728 if(lev < -1 || lev >= DUMP_LEVELS) {
729 error(_("error [planner askfor: lev out of range -1..%d: %d]"),
735 ep->estimate[seq].level = -1;
736 ep->estimate[seq].dumpdate = (char *)0;
737 ep->estimate[seq].nsize = (gint64)-3;
738 ep->estimate[seq].csize = (gint64)-3;
739 ep->estimate[seq].guessed = 0;
743 ep->estimate[seq].level = lev;
745 ep->estimate[seq].dumpdate = stralloc(get_dumpdate(info,lev));
747 ep->estimate[seq].nsize = (gint64)-3;
748 ep->estimate[seq].csize = (gint64)-3;
749 ep->estimate[seq].guessed = 0;
764 assert(dp && dp->host);
766 qname = quote_string(dp->name);
767 g_fprintf(stderr, _("%s: time %s: setting up estimates for %s:%s\n"),
768 get_pname(), walltime_str(curclock()),
769 dp->host->hostname, qname);
771 /* get current information about disk */
773 if(get_info(dp->host->hostname, dp->name, &info)) {
774 /* no record for this disk, make a note of it */
775 log_add(L_INFO, _("Adding new disk %s:%s."), dp->host->hostname, qname);
778 if (dp->data_path == DATA_PATH_DIRECTTCP) {
779 if (dp->compress != COMP_NONE) {
780 log_add(L_FAIL, _("%s %s %s 0 [Can't compress directtcp data-path]"),
781 dp->host->hostname, qname, planner_timestamp);
782 g_fprintf(stderr,_("%s:%s lev 0 skipped can't compress directtcp data-path\n"),
783 dp->host->hostname, qname);
787 if (dp->encrypt != ENCRYPT_NONE) {
788 log_add(L_FAIL, _("%s %s %s 0 [Can't encrypt directtcp data-path]"),
789 dp->host->hostname, qname, planner_timestamp);
790 g_fprintf(stderr,_("%s:%s lev 0 skipped can't encrypt directtcp data-path\n"),
791 dp->host->hostname, qname);
795 if (dp->to_holdingdisk == HOLD_REQUIRED) {
796 log_add(L_FAIL, _("%s %s %s 0 [Holding disk can't be use for directtcp data-path]"),
797 dp->host->hostname, qname, planner_timestamp);
798 g_fprintf(stderr,_("%s:%s lev 0 skipped Holding disk can't be use for directtcp data-path\n"),
799 dp->host->hostname, qname);
802 } else if (dp->to_holdingdisk == HOLD_AUTO) {
803 g_fprintf(stderr,_("%s:%s Disabling holding disk\n"),
804 dp->host->hostname, qname);
805 dp->to_holdingdisk = HOLD_NEVER;
809 /* setup working data struct for disk */
811 ep = alloc(SIZEOF(est_t));
812 dp->up = (void *) ep;
813 ep->state = DISK_READY;
814 ep->dump_priority = dp->priority;
818 ep->degr_mesg = NULL;
819 ep->dump_est = &default_one_est;
820 ep->degr_est = &default_one_est;
822 /* calculated fields */
824 if (ISSET(info.command, FORCE_FULL)) {
825 /* force a level 0, kind of like a new disk */
826 if(dp->strategy == DS_NOFULL) {
828 * XXX - Not sure what it means to force a no-full disk. The
829 * purpose of no-full is to just dump changes relative to a
830 * stable base, for example root partitions that vary only
831 * slightly from a site-wide prototype. Only the variations
834 * If we allow a level 0 onto the Amanda cycle, then we are
835 * hosed when that tape gets re-used next. Disallow this for
839 _("Cannot force full dump of %s:%s with no-full option."),
840 dp->host->hostname, qname);
842 /* clear force command */
843 CLR(info.command, FORCE_FULL);
844 ep->last_level = last_level(&info);
845 ep->next_level0 = next_level0(dp, &info);
846 } else if (dp->strategy == DS_INCRONLY) {
848 _("Cannot force full dump of %s:%s with incronly option."),
849 dp->host->hostname, qname);
851 /* clear force command */
852 CLR(info.command, FORCE_FULL);
853 ep->last_level = last_level(&info);
854 ep->next_level0 = next_level0(dp, &info);
856 ep->degr_mesg = _("Skipping: force-full disk can't be dumped in degraded mode");
858 ep->next_level0 = -conf_dumpcycle;
859 log_add(L_INFO, _("Forcing full dump of %s:%s as directed."),
860 dp->host->hostname, qname);
863 else if(dp->strategy == DS_NOFULL) {
864 /* force estimate of level 1 */
866 ep->next_level0 = next_level0(dp, &info);
869 ep->last_level = last_level(&info);
870 ep->next_level0 = next_level0(dp, &info);
873 /* adjust priority levels */
875 /* warn if dump will be overwritten */
876 if (ep->last_level > -1 && strlen(info.inf[0].label) > 0) {
877 overwrite_runs = when_overwrite(info.inf[0].label);
878 if(overwrite_runs == 0) {
879 log_add(L_WARNING, _("Last full dump of %s:%s "
880 "on tape %s overwritten on this run."),
881 dp->host->hostname, qname, info.inf[0].label);
882 } else if(overwrite_runs <= RUNS_REDZONE) {
884 plural(_("Last full dump of %s:%s on tape %s overwritten in %d run."),
885 _("Last full dump of %s:%s on tape %s overwritten in %d runs."), overwrite_runs),
886 dp->host->hostname, qname, info.inf[0].label,
891 /* warn if last level 1 will be overwritten */
892 if (ep->last_level > 1 && strlen(info.inf[1].label) > 0) {
893 overwrite_runs = when_overwrite(info.inf[1].label);
894 if(overwrite_runs == 0) {
895 log_add(L_WARNING, _("Last level 1 dump of %s:%s "
896 "on tape %s overwritten on this run, resetting to level 1"),
897 dp->host->hostname, qname, info.inf[1].label);
899 } else if(overwrite_runs <= RUNS_REDZONE) {
901 plural(_("Last level 1 dump of %s:%s on tape %s overwritten in %d run."),
902 _("Last level 1 dump of %s:%s on tape %s overwritten in %d runs."), overwrite_runs),
903 dp->host->hostname, qname, info.inf[1].label,
908 if(ep->next_level0 < 0) {
909 g_fprintf(stderr,plural(_("%s:%s overdue %d day for level 0\n"),
910 _("%s:%s overdue %d days for level 0\n"),
912 dp->host->hostname, qname, (-ep->next_level0));
913 ep->dump_priority -= ep->next_level0;
915 else if (ISSET(info.command, FORCE_FULL))
916 ep->dump_priority += 1;
917 /* else XXX bump up the priority of incrementals that failed last night */
919 /* handle external level 0 dumps */
921 if(dp->skip_full && dp->strategy != DS_NOINC) {
922 if(ep->next_level0 <= 0) {
923 /* update the date field */
924 info.inf[0].date = today;
925 CLR(info.command, FORCE_FULL);
926 ep->next_level0 += conf_dumpcycle;
928 if(put_info(dp->host->hostname, dp->name, &info)) {
929 error(_("could not put info record for %s:%s: %s"),
930 dp->host->hostname, qname, strerror(errno));
933 log_add(L_INFO, _("Skipping full dump of %s:%s today."),
934 dp->host->hostname, qname);
935 g_fprintf(stderr,_("%s:%s lev 0 skipped due to skip-full flag\n"),
936 dp->host->hostname, qname);
937 /* don't enqueue the disk */
938 askfor(ep, 0, -1, &info);
939 askfor(ep, 1, -1, &info);
940 askfor(ep, 2, -1, &info);
941 g_fprintf(stderr, _("%s: SKIPPED %s %s 0 [skip-full]\n"),
942 get_pname(), dp->host->hostname, qname);
943 log_add(L_SUCCESS, _("%s %s %s 0 [skipped: skip-full]"),
944 dp->host->hostname, qname, planner_timestamp);
949 if(ep->last_level == -1) {
950 /* probably a new disk, but skip-full means no full! */
954 if(ep->next_level0 == 1) {
955 log_add(L_WARNING, _("Skipping full dump of %s:%s tomorrow."),
956 dp->host->hostname, qname);
960 if(dp->strategy == DS_INCRONLY && ep->last_level == -1 && !ISSET(info.command, FORCE_FULL)) {
961 /* don't enqueue the disk */
962 askfor(ep, 0, -1, &info);
963 askfor(ep, 1, -1, &info);
964 askfor(ep, 2, -1, &info);
965 log_add(L_FAIL, _("%s %s 19000101 1 [Skipping incronly because no full dump were done]"),
966 dp->host->hostname, qname);
967 g_fprintf(stderr,_("%s:%s lev 1 skipped due to strategy incronly and no full dump were done\n"),
968 dp->host->hostname, qname);
973 /* handle "skip-incr" type archives */
975 if(dp->skip_incr && ep->next_level0 > 0) {
976 g_fprintf(stderr,_("%s:%s lev 1 skipped due to skip-incr flag\n"),
977 dp->host->hostname, qname);
978 /* don't enqueue the disk */
979 askfor(ep, 0, -1, &info);
980 askfor(ep, 1, -1, &info);
981 askfor(ep, 2, -1, &info);
983 g_fprintf(stderr, _("%s: SKIPPED %s %s 1 [skip-incr]\n"),
984 get_pname(), dp->host->hostname, qname);
986 log_add(L_SUCCESS, _("%s %s %s 1 [skipped: skip-incr]"),
987 dp->host->hostname, qname, planner_timestamp);
992 if( ep->last_level == -1 && ep->next_level0 > 0 &&
993 dp->strategy != DS_NOFULL && dp->strategy != DS_INCRONLY &&
994 conf_reserve == 100) {
995 log_add(L_WARNING, _("%s:%s mismatch: no tapelist record, "
996 "but curinfo next_level0: %d."),
997 dp->host->hostname, qname, ep->next_level0);
1001 if(ep->last_level == 0) ep->level_days = 0;
1002 else ep->level_days = runs_at(&info, ep->last_level);
1003 ep->last_lev0size = info.inf[0].csize;
1005 ep->fullrate = perf_average(info.full.rate, 0.0);
1006 ep->incrrate = perf_average(info.incr.rate, 0.0);
1008 ep->fullcomp = perf_average(info.full.comp, dp->comprate[0]);
1009 ep->incrcomp = perf_average(info.incr.comp, dp->comprate[1]);
1011 /* determine which estimates to get */
1015 if (dp->strategy == DS_NOINC ||
1017 (!ISSET(info.command, FORCE_BUMP) ||
1019 ep->last_level == -1))) {
1020 if(ISSET(info.command, FORCE_BUMP) && ep->last_level == -1) {
1022 _("Remove force-bump command of %s:%s because it's a new disk."),
1023 dp->host->hostname, qname);
1025 switch (dp->strategy) {
1028 askfor(ep, i++, 0, &info);
1029 if (ep->last_level == -1)
1030 ep->degr_mesg = _("Skipping: new disk can't be dumped in degraded mode");
1032 ep->degr_mesg = _("Skipping: strategy NOINC can't be dumped in degraded mode");
1034 log_add(L_INFO, _("Ignoring skip-full for %s:%s "
1035 "because the strategy is NOINC."),
1036 dp->host->hostname, qname);
1038 if(ISSET(info.command, FORCE_BUMP)) {
1040 _("Ignoring FORCE_BUMP for %s:%s because the strategy is NOINC."),
1041 dp->host->hostname, qname);
1050 if (ISSET(info.command, FORCE_FULL))
1056 if(!dp->skip_incr && !(dp->strategy == DS_NOINC)) {
1057 if(ep->last_level == -1) { /* a new disk */
1058 if (ep->degr_mesg == NULL)
1059 ep->degr_mesg = _("Skipping: new disk can't be dumped in degraded mode");
1060 if(dp->strategy == DS_NOFULL || dp->strategy == DS_INCRONLY) {
1061 askfor(ep, i++, 1, &info);
1063 assert(!dp->skip_full); /* should be handled above */
1065 } else { /* not new, pick normally */
1068 curr_level = ep->last_level;
1070 if (ISSET(info.command, FORCE_NO_BUMP)) {
1071 if(curr_level > 0) { /* level 0 already asked for */
1072 askfor(ep, i++, curr_level, &info);
1074 log_add(L_INFO,_("Preventing bump of %s:%s as directed."),
1075 dp->host->hostname, qname);
1076 ep->degr_mesg = _("Skipping: force-no-bump disk can't be dumped in degraded mode");
1077 } else if (ISSET(info.command, FORCE_BUMP)
1078 && curr_level + 1 < DUMP_LEVELS) {
1079 askfor(ep, i++, curr_level+1, &info);
1080 log_add(L_INFO,_("Bumping of %s:%s at level %d as directed."),
1081 dp->host->hostname, qname, curr_level+1);
1082 ep->degr_mesg = _("Skipping: force-bump disk can't be dumped in degraded mode");
1083 } else if (curr_level == 0) {
1084 askfor(ep, i++, 1, &info);
1086 askfor(ep, i++, curr_level, &info);
1088 * If last time we dumped less than the threshold, then this
1089 * time we will too, OR the extra size will be charged to both
1090 * cur_level and cur_level + 1, so we will never bump. Also,
1091 * if we haven't been at this level 2 days, or the dump failed
1092 * last night, we can't bump.
1094 if((info.inf[curr_level].size == (gint64)0 || /* no data, try it anyway */
1095 (((info.inf[curr_level].size > bump_thresh(curr_level, info.inf[0].size,dp->bumppercent, dp->bumpsize, dp->bumpmult)))
1096 && ep->level_days >= dp->bumpdays))
1097 && curr_level + 1 < DUMP_LEVELS) {
1098 askfor(ep, i++, curr_level+1, &info);
1104 while(i < MAX_LEVELS) /* mark end of estimates */
1105 askfor(ep, i++, -1, &info);
1109 g_fprintf(stderr, _("setup_estimate: %s:%s: command %u, options: %s "
1110 "last_level %d next_level0 %d level_days %d getting estimates "
1111 "%d (%lld) %d (%lld) %d (%lld)\n"),
1112 dp->host->hostname, qname, info.command,
1113 dp->strategy == DS_NOFULL ? "no-full" :
1114 dp->strategy == DS_INCRONLY ? "incr-only" :
1115 dp->skip_full ? "skip-full" :
1116 dp->skip_incr ? "skip-incr" : "none",
1117 ep->last_level, ep->next_level0, ep->level_days,
1118 ep->estimate[0].level, (long long)ep->estimate[0].nsize,
1119 ep->estimate[1].level, (long long)ep->estimate[1].nsize,
1120 ep->estimate[2].level, (long long)ep->estimate[2].nsize);
1122 assert(ep->estimate[0].level != -1);
1123 enqueue_disk(&startq, dp);
1127 static int when_overwrite(
1133 runtapes = conf_runtapes;
1134 if(runtapes == 0) runtapes = 1;
1136 if((tp = lookup_tapelabel(label)) == NULL)
1137 return 1; /* "shouldn't happen", but trigger warning message */
1138 else if(tp->reuse == 0)
1140 else if(lookup_nb_tape() > conf_tapecycle)
1141 return (lookup_nb_tape() - tp->position) / runtapes;
1143 return (conf_tapecycle - tp->position) / runtapes;
1146 /* Return the estimated size for a particular dump */
1154 if (level < 0 || level >= DUMP_LEVELS)
1155 return &default_one_est;
1157 for (i = 0; i < MAX_LEVELS; i++) {
1158 if (level == est(dp)->estimate[i].level) {
1159 if (est(dp)->estimate[i].csize <= -1) {
1160 est_csize(dp, &est(dp)->estimate[i]);
1162 return &est(dp)->estimate[i];
1165 return &default_one_est;
1168 /* Return the estimated on-tape size of a particular dump */
1174 gint64 size = one_est->nsize;
1177 if (dp->compress == COMP_NONE) {
1178 one_est->csize = one_est->nsize;
1182 if (one_est->level == 0) ratio = est(dp)->fullcomp;
1183 else ratio = est(dp)->incrcomp;
1186 * make sure over-inflated compression ratios don't throw off the
1187 * estimates, this is mostly for when you have a small dump getting
1188 * compressed which takes up alot more disk/tape space relatively due
1189 * to the overhead of the compression. This is specifically for
1190 * Digital Unix vdump. This patch is courtesy of Rudolf Gabler
1191 * (RUG@USM.Uni-Muenchen.DE)
1194 if (ratio > 1.1) ratio = 1.1;
1196 size = (gint64)((double)size * ratio);
1199 * Ratio can be very small in some error situations, so make sure
1200 * size goes back greater than zero. It may not be right, but
1201 * indicates we did get an estimate.
1203 if (size <= (gint64)0) {
1207 one_est->csize = size;
1210 static gint64 est_tape_size(
1214 one_est_t *dump_est;
1216 dump_est = est_for_level(dp, level);
1217 if (dump_est->level >= 0 && dump_est->csize <= -1)
1218 est_csize(dp, dump_est);
1219 return dump_est->csize;
1223 /* what was the level of the last successful dump to tape? */
1224 static int last_level(
1227 int min_pos, min_level, i;
1228 time_t lev0_date, last_date;
1231 if(info->last_level != -1)
1232 return info->last_level;
1234 /* to keep compatibility with old infofile */
1235 min_pos = 1000000000;
1239 for(i = 0; i < 9; i++) {
1240 if(conf_reserve < 100) {
1241 if(i == 0) lev0_date = info->inf[0].date;
1242 else if(info->inf[i].date < lev0_date) continue;
1243 if(info->inf[i].date > last_date) {
1244 last_date = info->inf[i].date;
1249 if((tp = lookup_tapelabel(info->inf[i].label)) == NULL) continue;
1250 /* cull any entries from previous cycles */
1251 if(i == 0) lev0_date = info->inf[0].date;
1252 else if(info->inf[i].date < lev0_date) continue;
1254 if(tp->position < min_pos) {
1255 min_pos = tp->position;
1260 info->last_level = i;
1264 /* when is next level 0 due? 0 = today, 1 = tomorrow, etc*/
1270 if(dp->strategy == DS_NOFULL || dp->strategy == DS_INCRONLY)
1271 return 1; /* fake it */
1272 else if (dp->strategy == DS_NOINC)
1274 else if(info->inf[0].date < (time_t)0)
1275 return -days_diff(EPOCH, today); /* new disk */
1277 return dp->dumpcycle - days_diff(info->inf[0].date, today);
1280 /* how many runs at current level? */
1285 tape_t *cur_tape, *old_tape;
1288 last = last_level(info);
1289 if(lev != last) return 0;
1290 if(lev == 0) return 1;
1292 if(info->consecutive_runs != -1)
1293 return info->consecutive_runs;
1295 /* to keep compatibility with old infofile */
1296 cur_tape = lookup_tapelabel(info->inf[lev].label);
1297 old_tape = lookup_tapelabel(info->inf[lev-1].label);
1298 if(cur_tape == NULL || old_tape == NULL) return 0;
1300 if(conf_runtapes == 0)
1301 nb_runs = (old_tape->position - cur_tape->position) / 1;
1303 nb_runs = (old_tape->position - cur_tape->position) / conf_runtapes;
1304 info->consecutive_runs = nb_runs;
1310 static gint64 bump_thresh(
1312 gint64 size_level_0,
1319 if ((bumppercent != 0) && (size_level_0 > (gint64)1024)) {
1320 bump = ((double)size_level_0 * (double)bumppercent) / 100.0;
1323 bump = (double)bumpsize;
1325 while(--level) bump = bump * bumpmult;
1327 return (gint64)bump;
1333 * ========================================================================
1334 * GET REMOTE DUMP SIZE ESTIMATES
1338 static void getsize(am_host_t *hostp);
1339 static disk_t *lookup_hostdisk(am_host_t *hp, char *str);
1340 static void handle_result(void *datap, pkt_t *pkt, security_handle_t *sech);
1343 static void get_estimates(void)
1347 int something_started;
1349 something_started = 1;
1350 while(something_started) {
1351 something_started = 0;
1352 for(dp = startq.head; dp != NULL; dp = dp->next) {
1354 if(hostp->up == HOST_READY) {
1355 something_started = 1;
1356 run_server_host_scripts(EXECUTE_ON_PRE_HOST_ESTIMATE,
1357 get_config_name(), hostp);
1358 for(dp1 = hostp->disks; dp1 != NULL; dp1 = dp1->hostnext) {
1360 run_server_dle_scripts(EXECUTE_ON_PRE_DLE_ESTIMATE,
1361 get_config_name(), dp1,
1362 est(dp1)->estimate[0].level);
1367 * dp is no longer on startq, so dp->next is not valid
1368 * and we have to start all over.
1376 while(!empty(waitq)) {
1377 disk_t *dp = dequeue_disk(&waitq);
1378 est(dp)->errstr = _("hmm, disk was stranded on waitq");
1379 enqueue_disk(&failq, dp);
1382 while(!empty(pestq)) {
1383 disk_t *dp = dequeue_disk(&pestq);
1384 char * qname = quote_string(dp->name);
1387 for (i=0; i < MAX_LEVELS; i++) {
1388 if (est(dp)->estimate[i].level != -1 &&
1389 est(dp)->estimate[i].nsize < (gint64)0) {
1390 if (est(dp)->estimate[i].nsize == (gint64)-3) {
1392 _("disk %s:%s, estimate of level %d timed out."),
1393 dp->host->hostname, qname, est(dp)->estimate[i].level);
1395 est(dp)->estimate[i].level = -1;
1399 if ((est(dp)->estimate[0].level != -1 &&
1400 est(dp)->estimate[0].nsize > (gint64)0) ||
1401 (est(dp)->estimate[1].level != -1 &&
1402 est(dp)->estimate[1].nsize > (gint64)0) ||
1403 (est(dp)->estimate[2].level != -1 &&
1404 est(dp)->estimate[2].nsize > (gint64)0)) {
1405 enqueue_disk(&estq, dp);
1408 est(dp)->errstr = vstralloc("disk ", qname,
1409 _(", all estimate timed out"), NULL);
1410 enqueue_disk(&failq, dp);
1416 static void getsize(
1419 char number[NUM_STR_SIZE], *req;
1422 time_t estimates, timeout;
1424 const security_driver_t *secdrv;
1426 char * qname, *b64disk = NULL;
1427 char * qdevice, *b64device = NULL;
1428 estimate_t estimate;
1431 assert(hostp->disks != NULL);
1433 if(hostp->up != HOST_READY) {
1438 * The first time through here we send a "noop" request. This will
1439 * return the feature list from the client if it supports that.
1440 * If it does not, handle_result() will set the feature list to an
1441 * empty structure. In either case, we do the disks on the second
1442 * (and subsequent) pass(es).
1444 if(hostp->features != NULL) { /* sendsize service */
1448 int has_features = am_has_feature(hostp->features,
1449 fe_req_options_features);
1450 int has_hostname = am_has_feature(hostp->features,
1451 fe_req_options_hostname);
1452 int has_maxdumps = am_has_feature(hostp->features,
1453 fe_req_options_maxdumps);
1454 int has_config = am_has_feature(hostp->features,
1455 fe_req_options_config);
1457 g_snprintf(number, SIZEOF(number), "%d", hostp->maxdumps);
1458 req = vstralloc("SERVICE ", "sendsize", "\n",
1460 has_features ? "features=" : "",
1461 has_features ? our_feature_string : "",
1462 has_features ? ";" : "",
1463 has_maxdumps ? "maxdumps=" : "",
1464 has_maxdumps ? number : "",
1465 has_maxdumps ? ";" : "",
1466 has_hostname ? "hostname=" : "",
1467 has_hostname ? hostp->hostname : "",
1468 has_hostname ? ";" : "",
1469 has_config ? "config=" : "",
1470 has_config ? get_config_name() : "",
1471 has_config ? ";" : "",
1474 req_len = strlen(req);
1475 req_len += 128; /* room for SECURITY ... */
1477 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1481 GPtrArray *errarray;
1483 if(dp->todo == 0) continue;
1485 if(est(dp)->state != DISK_READY) continue;
1487 est(dp)->got_estimate = 0;
1488 if (est(dp)->estimate[0].level == -1) {
1489 est(dp)->state = DISK_DONE;
1493 qname = quote_string(dp->name);
1495 errarray = validate_optionstr(dp);
1496 if (errarray->len > 0) {
1498 for (i=0; i < errarray->len; i++) {
1499 log_add(L_FAIL, _("%s %s %s 0 [%s]"),
1500 dp->host->hostname, qname,
1502 (char *)g_ptr_array_index(errarray, i));
1505 est(dp)->state = DISK_DONE;
1509 b64disk = amxml_format_tag("disk", dp->name);
1510 qdevice = quote_string(dp->device);
1511 estimate = (estimate_t)GPOINTER_TO_INT(dp->estimatelist->data);
1513 b64device = amxml_format_tag("diskdevice", dp->device);
1515 estimate = ES_CLIENT;
1516 for (el = dp->estimatelist; el != NULL; el = el->next) {
1517 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
1518 if (estimate == ES_SERVER)
1521 if (estimate == ES_SERVER) {
1524 get_info(dp->host->hostname, dp->name, &info);
1525 for(i = 0; i < MAX_LEVELS; i++) {
1526 int lev = est(dp)->estimate[i].level;
1528 if(lev == -1) break;
1529 server_estimate(dp, i, &info, lev);
1531 g_fprintf(stderr,_("%s time %s: got result for host %s disk %s:"),
1532 get_pname(), walltime_str(curclock()),
1533 dp->host->hostname, qname);
1534 g_fprintf(stderr,_(" %d -> %lldK, %d -> %lldK, %d -> %lldK\n"),
1535 est(dp)->estimate[0].level,
1536 (long long)est(dp)->estimate[0].nsize,
1537 est(dp)->estimate[1].level,
1538 (long long)est(dp)->estimate[1].nsize,
1539 est(dp)->estimate[2].level,
1540 (long long)est(dp)->estimate[2].nsize);
1541 if (!am_has_feature(hostp->features, fe_xml_estimate)) {
1542 est(dp)->state = DISK_DONE;
1543 remove_disk(&startq, dp);
1544 enqueue_disk(&estq, dp);
1548 estimate = ES_SERVER;
1549 for (el = dp->estimatelist; el != NULL; el = el->next) {
1550 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
1551 if (estimate == ES_CLIENT || estimate == ES_CALCSIZE)
1554 if (estimate == ES_CLIENT ||
1555 estimate == ES_CALCSIZE ||
1556 (am_has_feature(hostp->features, fe_req_xml) &&
1557 am_has_feature(hostp->features, fe_xml_estimate))) {
1561 if (am_has_feature(hostp->features, fe_req_xml)) {
1562 char *levelstr = NULL;
1563 char *spindlestr = NULL;
1564 char level[NUM_STR_SIZE];
1565 char spindle[NUM_STR_SIZE];
1570 get_info(dp->host->hostname, dp->name, &info);
1571 for(i = 0; i < MAX_LEVELS; i++) {
1573 int lev = est(dp)->estimate[i].level;
1574 if (lev == -1) break;
1575 g_snprintf(level, SIZEOF(level), "%d", lev);
1576 if (am_has_feature(hostp->features, fe_xml_level_server) &&
1577 server_can_do_estimate(dp, &info, lev)) {
1578 server = "<server>YES</server>";
1582 vstrextend(&levelstr, " <level>",
1584 "</level>\n", NULL);
1586 g_snprintf(spindle, SIZEOF(spindle), "%d", dp->spindle);
1587 spindlestr = vstralloc(" <spindle>",
1589 "</spindle>\n", NULL);
1590 o = xml_optionstr(dp, 0);
1592 if (strcmp(dp->program,"DUMP") == 0 ||
1593 strcmp(dp->program,"GNUTAR") == 0) {
1594 l = vstralloc("<dle>\n",
1597 "</program>\n", NULL);
1599 l = vstralloc("<dle>\n",
1600 " <program>APPLICATION</program>\n",
1602 if (dp->application) {
1603 application_t *application;
1606 application = lookup_application(dp->application);
1607 g_assert(application != NULL);
1608 xml_app = xml_application(dp, application,
1610 vstrextend(&l, xml_app, NULL);
1615 es = xml_estimate(dp->estimatelist, hostp->features);
1616 vstrextend(&l, es, "\n", NULL);
1618 vstrextend(&l, " ", b64disk, "\n", NULL);
1620 vstrextend(&l, " ", b64device, "\n", NULL);
1621 vstrextend(&l, levelstr, spindlestr, o, "</dle>\n", NULL);
1628 } else if (strcmp(dp->program,"DUMP") != 0 &&
1629 strcmp(dp->program,"GNUTAR") != 0) {
1630 est(dp)->errstr = newvstrallocf(est(dp)->errstr,
1631 _("does not support application-api"));
1633 for(i = 0; i < MAX_LEVELS; i++) {
1635 char *exclude1 = "";
1636 char *exclude2 = "";
1637 char *excludefree = NULL;
1638 char *include1 = "";
1639 char *include2 = "";
1640 char *includefree = NULL;
1641 char spindle[NUM_STR_SIZE];
1642 char level[NUM_STR_SIZE];
1643 int lev = est(dp)->estimate[i].level;
1645 if(lev == -1) break;
1647 g_snprintf(level, SIZEOF(level), "%d", lev);
1648 g_snprintf(spindle, SIZEOF(spindle), "%d", dp->spindle);
1649 if (am_has_feature(hostp->features,
1650 fe_sendsize_req_options)){
1651 exclude1 = " OPTIONS |";
1652 exclude2 = optionstr(dp);
1653 if ( exclude2 == NULL ) {
1654 error(_("problem with option string, check the dumptype definition.\n"));
1656 excludefree = exclude2;
1659 if (dp->exclude_file &&
1660 dp->exclude_file->nb_element == 1) {
1661 exclude1 = " exclude-file=";
1662 exclude2 = quote_string(
1663 dp->exclude_file->first->name);
1664 excludefree = exclude2;
1666 else if (dp->exclude_list &&
1667 dp->exclude_list->nb_element == 1) {
1668 exclude1 = " exclude-list=";
1669 exclude2 = quote_string(
1670 dp->exclude_list->first->name);
1671 excludefree = exclude2;
1673 if (dp->include_file &&
1674 dp->include_file->nb_element == 1) {
1675 include1 = " include-file=";
1676 include2 = quote_string(
1677 dp->include_file->first->name);
1678 includefree = include2;
1680 else if (dp->include_list &&
1681 dp->include_list->nb_element == 1) {
1682 include1 = " include-list=";
1683 include2 = quote_string(
1684 dp->include_list->first->name);
1685 includefree = include2;
1689 if (estimate == ES_CALCSIZE &&
1690 !am_has_feature(hostp->features,
1691 fe_calcsize_estimate)) {
1693 _("%s:%s does not support CALCSIZE for estimate, using CLIENT.\n"),
1694 hostp->hostname, qname);
1695 estimate = ES_CLIENT;
1697 if(estimate == ES_CLIENT)
1700 calcsize = "CALCSIZE ";
1702 l = vstralloc(calcsize,
1705 " ", dp->device ? qdevice : "",
1707 " ", est(dp)->estimate[i].dumpdate,
1709 " ", exclude1, exclude2,
1710 ((includefree != NULL) ? " " : ""),
1717 amfree(includefree);
1718 amfree(excludefree);
1726 if (est(dp)->state == DISK_DONE) {
1727 remove_disk(&estq, dp);
1728 est(dp)->state = DISK_PARTIALY_DONE;
1729 enqueue_disk(&pestq, dp);
1731 remove_disk(&startq, dp);
1732 est(dp)->state = DISK_ACTIVE;
1734 } else if (est(dp)->state != DISK_DONE) {
1735 remove_disk(&startq, dp);
1736 est(dp)->state = DISK_DONE;
1737 if (est(dp)->errstr == NULL) {
1738 est(dp)->errstr = vstrallocf(
1739 _("Can't request estimate"));
1741 enqueue_disk(&failq, dp);
1750 if(estimates == 0) {
1752 hostp->up = HOST_DONE;
1756 if (conf_etimeout < 0) {
1757 timeout = - conf_etimeout;
1759 timeout = estimates * conf_etimeout;
1761 } else { /* noop service */
1762 req = vstralloc("SERVICE ", "noop", "\n",
1764 "features=", our_feature_string, ";",
1768 * We use ctimeout for the "noop" request because it should be
1769 * very fast and etimeout has other side effects.
1771 timeout = (time_t)getconf_int(CNF_CTIMEOUT);
1774 dbprintf(_("send request:\n----\n%s\n----\n\n"), req);
1775 secdrv = security_getdriver(hostp->disks->auth);
1776 if (secdrv == NULL) {
1777 hostp->up = HOST_DONE;
1779 _("Could not find security driver '%s' for host '%s'"),
1780 hostp->disks->auth, hostp->hostname);
1784 hostp->up = HOST_ACTIVE;
1786 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1790 if(est(dp)->state == DISK_ACTIVE) {
1791 est(dp)->errstr = NULL;
1792 enqueue_disk(&waitq, dp);
1796 protocol_sendreq(hostp->hostname, secdrv, amhost_get_security_conf,
1797 req, timeout, handle_result, hostp);
1802 static disk_t *lookup_hostdisk(
1803 /*@keep@*/ am_host_t *hp,
1808 for(dp = hp->disks; dp != NULL; dp = dp->hostnext)
1809 if(strcmp(str, dp->name) == 0) return dp;
1815 static void handle_result(
1818 security_handle_t *sech)
1824 char *msg, msg_undo;
1825 char *remoterr, *errbuf = NULL;
1836 hostp = (am_host_t *)datap;
1837 hostp->up = HOST_READY;
1840 if (strcmp(security_geterror(sech), "timeout waiting for REP") == 0) {
1841 errbuf = vstrallocf("Some estimate timeout on %s, using server estimate if possible", hostp->hostname);
1843 errbuf = vstrallocf(_("Request to %s failed: %s"),
1844 hostp->hostname, security_geterror(sech));
1848 if (pkt->type == P_NAK) {
1850 if(strncmp_const_skip(s, "ERROR ", s, ch) == 0) {
1853 goto NAK_parse_failed;
1855 skip_whitespace(s, ch);
1856 if(ch == '\0') goto NAK_parse_failed;
1858 if((s = strchr(remoterr, '\n')) != NULL) {
1859 if(s == remoterr) goto NAK_parse_failed;
1862 if (strcmp(remoterr, "unknown service: noop") != 0
1863 && strcmp(remoterr, "noop: invalid service") != 0) {
1864 errbuf = vstralloc(hostp->hostname, " NAK: ", remoterr, NULL);
1870 dbprintf(_("got reply:\n----\n%s\n----\n\n"), pkt->body);
1876 if(strncmp_const(line, "OPTIONS ") == 0) {
1877 t = strstr(line, "features=");
1878 if(t != NULL && (g_ascii_isspace((int)t[-1]) || t[-1] == ';')) {
1879 char *u = strchr(t, ';');
1882 t += SIZEOF("features=")-1;
1883 am_release_feature_set(hostp->features);
1884 if((hostp->features = am_string_to_feature(t)) == NULL) {
1885 errbuf = vstrallocf(hostp->hostname,
1886 _(": bad features value: %s\n"), line);
1892 skip_quoted_line(s, ch);
1897 if ((strncmp_const_skip(t, "ERROR ", t, tch) == 0) ||
1898 (strncmp_const_skip(t, "WARNING ", t, tch) == 0)) {
1900 skip_whitespace(t, tch);
1906 * If the "error" is that the "noop" service is unknown, it
1907 * just means the client is "old" (does not support the servie).
1908 * We can ignore this.
1910 if(hostp->features == NULL
1911 && pkt->type == P_NAK
1912 && (strcmp(t - 1, "unknown service: noop") == 0
1913 || strcmp(t - 1, "noop: invalid service") == 0)) {
1914 skip_quoted_line(s, ch);
1918 if (t) /* truncate after the first line */
1920 errbuf = vstralloc(hostp->hostname,
1921 (pkt->type == P_NAK) ? "NAK " : "",
1930 skip_quoted_string(t, tch);
1932 disk = unquote_string(msg);
1934 skip_whitespace(t, tch);
1936 if (sscanf(t - 1, "%d", &level) != 1) {
1940 skip_integer(t, tch);
1941 skip_whitespace(t, tch);
1943 dp = lookup_hostdisk(hostp, disk);
1945 log_add(L_ERROR, _("%s: invalid reply from sendsize: `%s'\n"),
1946 hostp->hostname, line);
1951 if (strncmp_const(t-1,"SIZE ") == 0) {
1952 if (sscanf(t - 1, "SIZE %lld", &size_) != 1) {
1955 size = (gint64)size_;
1956 } else if ((strncmp_const(t-1,"ERROR ") == 0) ||
1957 (strncmp_const(t-1,"WARNING ") == 0)) {
1958 skip_non_whitespace(t, tch);
1959 skip_whitespace(t, tch);
1961 skip_quoted_string(t,tch);
1964 if (pkt->type == P_REP && !est(dp)->errstr) {
1965 est(dp)->errstr = unquote_string(msg);
1974 for (i = 0; i < MAX_LEVELS; i++) {
1975 if (est(dp)->estimate[i].level == level) {
1976 if (size == (gint64)-2) {
1977 est(dp)->estimate[i].nsize = -1; /* remove estimate */
1978 est(dp)->estimate[i].guessed = 0;
1979 } else if (size > (gint64)-1) {
1980 /* take the size returned by the client */
1981 est(dp)->estimate[i].nsize = size;
1982 est(dp)->estimate[i].guessed = 0;
1987 if (i == MAX_LEVELS && level > 0) {
1988 /* client always report level 0 for some error */
1989 goto bad_msg; /* this est wasn't requested */
1991 est(dp)->got_estimate++;
1995 skip_quoted_line(s, ch);
1998 if(hostp->up == HOST_READY && hostp->features == NULL) {
2000 * The client does not support the features list, so give it an
2003 dbprintf(_("no feature set from host %s\n"), hostp->hostname);
2004 hostp->features = am_set_default_feature_set();
2007 security_close_connection(sech, hostp->hostname);
2009 /* XXX what about disks that only got some estimates... do we care? */
2010 /* XXX amanda 2.1 treated that case as a bad msg */
2012 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
2013 if(dp->todo == 0) continue;
2014 if(est(dp)->state != DISK_ACTIVE &&
2015 est(dp)->state != DISK_PARTIALY_DONE) continue;
2017 if(est(dp)->state == DISK_ACTIVE) {
2018 remove_disk(&waitq, dp);
2020 else if(est(dp)->state == DISK_PARTIALY_DONE) {
2021 remove_disk(&pestq, dp);
2024 if(pkt->type == P_REP) {
2025 est(dp)->state = DISK_DONE;
2027 else if(pkt->type == P_PREP) {
2028 est(dp)->state = DISK_PARTIALY_DONE;
2031 if (est(dp)->estimate[0].level == -1) continue; /* ignore this disk */
2034 qname = quote_string(dp->name);
2035 if(pkt->type == P_PREP) {
2036 g_fprintf(stderr,_("%s: time %s: got partial result for host %s disk %s:"),
2037 get_pname(), walltime_str(curclock()),
2038 dp->host->hostname, qname);
2039 g_fprintf(stderr,_(" %d -> %lldK, %d -> %lldK, %d -> %lldK\n"),
2040 est(dp)->estimate[0].level,
2041 (long long)est(dp)->estimate[0].nsize,
2042 est(dp)->estimate[1].level,
2043 (long long)est(dp)->estimate[1].nsize,
2044 est(dp)->estimate[2].level,
2045 (long long)est(dp)->estimate[2].nsize);
2046 enqueue_disk(&pestq, dp);
2048 else if(pkt->type == P_REP) {
2049 g_fprintf(stderr,_("%s: time %s: got result for host %s disk %s:"),
2050 get_pname(), walltime_str(curclock()),
2051 dp->host->hostname, qname);
2052 g_fprintf(stderr,_(" %d -> %lldK, %d -> %lldK, %d -> %lldK\n"),
2053 est(dp)->estimate[0].level,
2054 (long long)est(dp)->estimate[0].nsize,
2055 est(dp)->estimate[1].level,
2056 (long long)est(dp)->estimate[1].nsize,
2057 est(dp)->estimate[2].level,
2058 (long long)est(dp)->estimate[2].nsize);
2059 if ((est(dp)->estimate[0].level != -1 &&
2060 est(dp)->estimate[0].nsize > (gint64)0) ||
2061 (est(dp)->estimate[1].level != -1 &&
2062 est(dp)->estimate[1].nsize > (gint64)0) ||
2063 (est(dp)->estimate[2].level != -1 &&
2064 est(dp)->estimate[2].nsize > (gint64)0)) {
2066 for (i=MAX_LEVELS-1; i >=0; i--) {
2067 if (est(dp)->estimate[i].level != -1 &&
2068 est(dp)->estimate[i].nsize < (gint64)0) {
2069 est(dp)->estimate[i].level = -1;
2072 enqueue_disk(&estq, dp);
2075 enqueue_disk(&failq, dp);
2076 if(est(dp)->got_estimate && !est(dp)->errstr) {
2077 est(dp)->errstr = vstrallocf("disk %s, all estimate failed",
2082 _("error result for host %s disk %s: missing estimate\n"),
2083 dp->host->hostname, qname);
2084 if (est(dp)->errstr == NULL) {
2085 est(dp)->errstr = vstrallocf(_("missing result for %s in %s response"),
2086 qname, dp->host->hostname);
2090 hostp->up = HOST_DONE;
2092 if (est(dp)->post_dle == 0 &&
2093 (pkt->type == P_REP ||
2094 ((est(dp)->estimate[0].level == -1 ||
2095 est(dp)->estimate[0].nsize > (gint64)0) &&
2096 (est(dp)->estimate[1].level == -1 ||
2097 est(dp)->estimate[1].nsize > (gint64)0) &&
2098 (est(dp)->estimate[2].level == -1 ||
2099 est(dp)->estimate[2].nsize > (gint64)0)))) {
2100 run_server_dle_scripts(EXECUTE_ON_POST_DLE_ESTIMATE,
2101 get_config_name(), dp,
2102 est(dp)->estimate[0].level);
2103 est(dp)->post_dle = 1;
2108 if(hostp->up == HOST_DONE) {
2109 if (pkt->type == P_REP) {
2110 run_server_host_scripts(EXECUTE_ON_POST_HOST_ESTIMATE,
2111 get_config_name(), hostp);
2116 /* try to clean up any defunct processes, since Amanda doesn't wait() for
2118 while(waitpid(-1, NULL, WNOHANG)> 0);
2123 errbuf = vstrallocf(_("%s NAK: [NAK parse failed]"), hostp->hostname);
2124 g_fprintf(stderr, _("got strange nak from %s:\n----\n%s----\n\n"),
2125 hostp->hostname, pkt->body);
2129 g_fprintf(stderr,_("got a bad message, stopped at:\n"));
2131 g_fprintf(stderr,_("----\n%s----\n\n"), line);
2132 errbuf = stralloc2(_("badly formatted response from "), hostp->hostname);
2137 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
2139 if(est(dp)->state == DISK_ACTIVE) {
2140 qname = quote_string(dp->name);
2141 est(dp)->state = DISK_DONE;
2142 remove_disk(&waitq, dp);
2143 enqueue_disk(&failq, dp);
2146 est(dp)->errstr = stralloc(errbuf);
2147 g_fprintf(stderr, _("error result for host %s disk %s: %s\n"),
2148 dp->host->hostname, qname, errbuf);
2155 * If there were no disks involved, make sure the error gets
2158 log_add(L_ERROR, "%s", errbuf);
2160 hostp->up = HOST_DONE;
2162 /* try to clean up any defunct processes, since Amanda doesn't wait() for
2164 while(waitpid(-1, NULL, WNOHANG)> 0);
2171 * ========================================================================
2176 static int schedule_order(disk_t *a, disk_t *b); /* subroutines */
2177 static one_est_t *pick_inclevel(disk_t *dp);
2179 static void analyze_estimate(
2185 char *qname = quote_string(dp->name);
2189 g_fprintf(stderr, _("pondering %s:%s... "),
2190 dp->host->hostname, qname);
2191 g_fprintf(stderr, _("next_level0 %d last_level %d "),
2192 ep->next_level0, ep->last_level);
2194 if(get_info(dp->host->hostname, dp->name, &info) == 0) {
2198 ep->degr_est = &default_one_est;
2200 if (ep->next_level0 <= 0 || (have_info && ep->last_level == 0
2201 && (ISSET(info.command, FORCE_NO_BUMP)))) {
2202 if (ep->next_level0 <= 0) {
2203 g_fprintf(stderr,_("(due for level 0) "));
2205 ep->dump_est = est_for_level(dp, 0);
2206 if (ep->dump_est->csize <= (gint64)0) {
2208 _("(no estimate for level 0, picking an incr level)\n"));
2209 ep->dump_est = pick_inclevel(dp);
2211 if (ep->dump_est->nsize == (gint64)-1) {
2212 ep->dump_est = est_for_level(dp, ep->dump_est->level + 1);
2216 total_lev0 += (double) ep->dump_est->csize;
2217 if(ep->last_level == -1 || dp->skip_incr) {
2218 g_fprintf(stderr,_("(%s disk, can't switch to degraded mode)\n"),
2219 dp->skip_incr? "skip-incr":_("new"));
2220 if (dp->skip_incr && ep->degr_mesg == NULL) {
2221 ep->degr_mesg = _("Skpping: skip-incr disk can't be dumped in degraded mode");
2223 ep->degr_est = &default_one_est;
2226 /* fill in degraded mode info */
2227 g_fprintf(stderr,_("(picking inclevel for degraded mode)"));
2228 ep->degr_est = pick_inclevel(dp);
2229 if (ep->degr_est->csize == (gint64)-1) {
2230 ep->degr_est = est_for_level(dp, ep->degr_est->level + 1);
2232 if (ep->degr_est->csize == (gint64)-1) {
2233 g_fprintf(stderr,_("(no inc estimate)"));
2234 if (ep->degr_mesg == NULL)
2235 ep->degr_mesg = _("Skipping: an incremental estimate could not be performed, so disk cannot be dumped in degraded mode");
2236 ep->degr_est = &default_one_est;
2238 g_fprintf(stderr,"\n");
2243 g_fprintf(stderr,_("(not due for a full dump, picking an incr level)\n"));
2244 /* XXX - if this returns -1 may be we should force a total? */
2245 ep->dump_est = pick_inclevel(dp);
2247 if (ep->dump_est->csize == (gint64)-1) {
2248 ep->dump_est = est_for_level(dp, ep->last_level);
2250 if (ep->dump_est->csize == (gint64)-1) {
2251 ep->dump_est = est_for_level(dp, ep->last_level + 1);
2253 if (ep->dump_est->csize == (gint64)-1) {
2254 ep->dump_est = est_for_level(dp, 0);
2256 if (ep->degr_mesg == NULL) {
2257 ep->degr_mesg = _("Skipping: a full is not planned, so can't dump in degraded mode");
2261 if (ep->dump_est->level < 0) {
2263 char *q = quote_string("no estimate");
2265 g_fprintf(stderr,_(" no valid estimate\n"));
2266 for(i=0; i<MAX_LEVELS; i++) {
2267 if (est(dp)->estimate[i].level >= 0) {
2268 g_fprintf(stderr,(" level: %d nsize: %lld csize: %lld\n"),
2269 est(dp)->estimate[i].level,
2270 (long long)est(dp)->estimate[i].nsize,
2271 (long long)est(dp)->estimate[i].csize);
2274 log_add(L_WARNING, _("%s %s %s 0 %s"), dp->host->hostname, qname,
2275 planner_timestamp, q);
2279 g_fprintf(stderr,_(" curr level %d nsize %lld csize %lld "),
2280 ep->dump_est->level, (long long)ep->dump_est->nsize,
2281 (long long)ep->dump_est->csize);
2283 insert_disk(&schedq, dp, schedule_order);
2285 total_size += (gint64)tt_blocksize_kb + ep->dump_est->csize + tape_mark;
2287 /* update the balanced size */
2288 if(!(dp->skip_full || dp->strategy == DS_NOFULL ||
2289 dp->strategy == DS_INCRONLY)) {
2292 lev0size = est_tape_size(dp, 0);
2293 if(lev0size == (gint64)-1) lev0size = ep->last_lev0size;
2295 if (dp->strategy == DS_NOINC) {
2296 balanced_size += (double)lev0size;
2297 } else if (dp->dumpcycle == 0) {
2298 balanced_size += (double)(lev0size * conf_dumpcycle / (gint64)runs_per_cycle);
2299 } else if (dp->dumpcycle != conf_dumpcycle) {
2300 balanced_size += (double)(lev0size * (conf_dumpcycle / dp->dumpcycle) / (gint64)runs_per_cycle);
2302 balanced_size += (double)(lev0size / (gint64)runs_per_cycle);
2306 g_fprintf(stderr,_("total size %lld total_lev0 %1.0lf balanced-lev0size %1.0lf\n"),
2307 (long long)total_size, total_lev0, balanced_size);
2309 /* Log errstr even if the estimate succeeded */
2310 /* It can be an error from a script */
2311 if (est(dp)->errstr) {
2312 char *qerrstr = quote_string(est(dp)->errstr);
2313 /* Log only a warning if a server estimate is available */
2314 if (est(dp)->estimate[0].nsize > 0 ||
2315 est(dp)->estimate[1].nsize > 0 ||
2316 est(dp)->estimate[2].nsize > 0) {
2317 log_add(L_WARNING, _("%s %s %s 0 %s"), dp->host->hostname, qname,
2318 planner_timestamp, qerrstr);
2320 log_add(L_FAIL, _("%s %s %s 0 %s"), dp->host->hostname, qname,
2321 planner_timestamp, qerrstr);
2329 static void handle_failed(
2332 char *errstr, *errstr1, *qerrstr;
2333 char *qname = quote_string(dp->name);
2335 errstr = est(dp)->errstr? est(dp)->errstr : _("hmm, no error indicator!");
2336 errstr1 = vstralloc("[",errstr,"]", NULL);
2337 qerrstr = quote_string(errstr1);
2340 g_fprintf(stderr, _("%s: FAILED %s %s %s 0 %s\n"),
2341 get_pname(), dp->host->hostname, qname, planner_timestamp, qerrstr);
2343 log_add(L_FAIL, _("%s %s %s 0 %s"), dp->host->hostname, qname,
2344 planner_timestamp, qerrstr);
2348 /* XXX - memory leak with *dp */
2353 * insert-sort by decreasing priority, then
2354 * by decreasing size within priority levels.
2357 static int schedule_order(
2364 diff = est(b)->dump_priority - est(a)->dump_priority;
2365 if(diff != 0) return diff;
2367 ldiff = est(b)->dump_est->csize - est(a)->dump_est->csize;
2368 if(ldiff < (gint64)0) return -1; /* XXX - there has to be a better way to dothis */
2369 if(ldiff > (gint64)0) return 1;
2374 static one_est_t *pick_inclevel(
2377 one_est_t *level0_est, *base_est, *bump_est;
2381 level0_est = est_for_level(dp, 0);
2382 base_est = est_for_level(dp, est(dp)->last_level);
2384 /* if last night was level 0, do level 1 tonight, no ifs or buts */
2385 if (base_est->level == 0) {
2386 g_fprintf(stderr,_(" picklev: last night 0, so tonight level 1\n"));
2387 return est_for_level(dp, 1);
2390 /* if no-full option set, always do level 1 */
2391 if(dp->strategy == DS_NOFULL) {
2392 g_fprintf(stderr,_(" picklev: no-full set, so always level 1\n"));
2393 return est_for_level(dp, 1);
2396 /* if we didn't get an estimate, we can't do an inc */
2397 if (base_est->nsize == (gint64)-1) {
2398 bump_est = est_for_level(dp, est(dp)->last_level + 1);
2399 if (bump_est->nsize > (gint64)0) { /* FORCE_BUMP */
2400 g_fprintf(stderr,_(" picklev: bumping to level %d\n"), bump_est->level);
2403 g_fprintf(stderr,_(" picklev: no estimate for level %d, so no incs\n"), base_est->level);
2407 thresh = bump_thresh(base_est->level, level0_est->nsize, dp->bumppercent,
2408 dp->bumpsize, dp->bumpmult);
2411 _(" pick: size %lld level %d days %d (thresh %lldK, %d days)\n"),
2412 (long long)base_est->nsize, base_est->level, est(dp)->level_days,
2413 (long long)thresh, dp->bumpdays);
2415 if(base_est->level == (DUMP_LEVELS - 1)
2416 || est(dp)->level_days < dp->bumpdays
2417 || base_est->nsize <= thresh)
2420 bump_est = est_for_level(dp, base_est->level + 1);
2422 if (bump_est->nsize == (gint64)-1)
2425 g_fprintf(stderr, _(" pick: next size %lld... "),
2426 (long long)bump_est->nsize);
2428 if (base_est->nsize - bump_est->nsize < thresh) {
2429 g_fprintf(stderr, _("not bumped\n"));
2433 qname = quote_string(dp->name);
2434 g_fprintf(stderr, _("BUMPED\n"));
2435 log_add(L_INFO, _("Incremental of %s:%s bumped to level %d."),
2436 dp->host->hostname, qname, bump_est->level);
2446 ** ========================================================================
2449 ** We have two strategies here:
2453 ** If we are trying to fit too much on the tape something has to go. We
2454 ** try to delay totals until tomorrow by converting them into incrementals
2455 ** and, if that is not effective enough, dropping incrementals altogether.
2456 ** While we are searching for the guilty dump (the one that is really
2457 ** causing the schedule to be oversize) we have probably trampled on a lot of
2458 ** innocent dumps, so we maintain a "before image" list and use this to
2459 ** put back what we can.
2461 ** 2. Promote dumps.
2463 ** We try to keep the amount of tape used by total dumps the same each night.
2464 ** If there is some spare tape in this run we have a look to see if any of
2465 ** tonights incrementals could be promoted to totals and leave us with a
2466 ** more balanced cycle.
2469 static void delay_one_dump(disk_t *dp, int delete, ...);
2470 static int promote_highest_priority_incremental(void);
2471 static int promote_hills(void);
2473 /* delay any dumps that will not fit */
2474 static void delay_dumps(void)
2481 gint64 new_total; /* New total_size */
2482 char est_kb[20]; /* Text formatted dump size */
2483 int nb_forced_level_0;
2489 biq.head = biq.tail = NULL;
2492 ** 1. Delay dumps that are way oversize.
2494 ** Dumps larger that the size of the tapes we are using are just plain
2495 ** not going to fit no matter how many other dumps we drop. Delay
2496 ** oversize totals until tomorrow (by which time my owner will have
2497 ** resolved the problem!) and drop incrementals altogether. Naturally
2498 ** a large total might be delayed into a large incremental so these
2499 ** need to be checked for separately.
2502 for(dp = schedq.head; dp != NULL; dp = ndp) {
2503 int avail_tapes = 1;
2504 if (dp->splitsize > (gint64)0 || dp->allow_split)
2505 avail_tapes = conf_runtapes;
2507 ndp = dp->next; /* remove_disk zaps this */
2509 full_size = est_tape_size(dp, 0);
2510 if (full_size > tapetype_get_length(tape) * (gint64)avail_tapes) {
2511 char *qname = quote_string(dp->name);
2512 if (conf_runtapes > 1 && dp->splitsize == (gint64)0) {
2513 log_add(L_WARNING, _("disk %s:%s, full dump (%lldKB) will be larger than available tape space"
2514 ", you could define a splitsize"),
2515 dp->host->hostname, qname,
2516 (long long)full_size);
2518 log_add(L_WARNING, _("disk %s:%s, full dump (%lldKB) will be larger than available tape space"),
2519 dp->host->hostname, qname,
2520 (long long)full_size);
2525 if (est(dp)->dump_est->csize == (gint64)-1 ||
2526 est(dp)->dump_est->csize <= tapetype_get_length(tape) * (gint64)avail_tapes) {
2530 /* Format dumpsize for messages */
2531 g_snprintf(est_kb, 20, "%lld KB,",
2532 (long long)est(dp)->dump_est->csize);
2534 if(est(dp)->dump_est->level == 0) {
2537 message = _("but cannot incremental dump skip-incr disk");
2539 else if(est(dp)->last_level < 0) {
2541 message = _("but cannot incremental dump new disk");
2543 else if(est(dp)->degr_est->level < 0) {
2545 message = _("but no incremental estimate");
2547 else if (est(dp)->degr_est->csize > tapetype_get_length(tape)) {
2549 message = _("incremental dump also larger than tape");
2553 message = _("full dump delayed");
2558 message = _("skipping incremental");
2560 delay_one_dump(dp, delete, _("dump larger than available tape space,"),
2561 est_kb, message, NULL);
2565 ** 2. Delay total dumps.
2567 ** Delay total dumps until tomorrow (or the day after!). We start with
2568 ** the lowest priority (most dispensable) and work forwards. We take
2569 ** care not to delay *all* the dumps since this could lead to a stale
2570 ** mate [for any one disk there are only three ways tomorrows dump will
2571 ** be smaller than todays: 1. we do a level 0 today so tomorows dump
2572 ** will be a level 1; 2. the disk gets more data so that it is bumped
2573 ** tomorrow (this can be a slow process); and, 3. the disk looses some
2574 ** data (when does that ever happen?)].
2577 nb_forced_level_0 = 0;
2579 for(dp = schedq.head; dp != NULL && preserve == NULL; dp = dp->next)
2580 if(est(dp)->dump_est->level == 0)
2583 /* 2.a. Do not delay forced full */
2584 for(dp = schedq.tail;
2585 dp != NULL && total_size > tape_length;
2589 if(est(dp)->dump_est->level != 0) continue;
2591 get_info(dp->host->hostname, dp->name, &info);
2592 if(ISSET(info.command, FORCE_FULL)) {
2593 nb_forced_level_0 += 1;
2598 if(dp != preserve) {
2600 /* Format dumpsize for messages */
2601 g_snprintf(est_kb, 20, "%lld KB,",
2602 (long long)est(dp)->dump_est->csize);
2606 message = _("but cannot incremental dump skip-incr disk");
2608 else if(est(dp)->last_level < 0) {
2610 message = _("but cannot incremental dump new disk");
2612 else if(est(dp)->degr_est->level < 0) {
2614 message = _("but no incremental estimate");
2618 message = _("full dump delayed");
2620 delay_one_dump(dp, delete, _("dumps too big,"), est_kb,
2625 /* 2.b. Delay forced full if needed */
2626 if(nb_forced_level_0 > 0 && total_size > tape_length) {
2627 for(dp = schedq.tail;
2628 dp != NULL && total_size > tape_length;
2632 if(est(dp)->dump_est->level == 0 && dp != preserve) {
2634 /* Format dumpsize for messages */
2635 g_snprintf(est_kb, 20, "%lld KB,",
2636 (long long)est(dp)->dump_est->csize);
2640 message = _("but cannot incremental dump skip-incr disk");
2642 else if(est(dp)->last_level < 0) {
2644 message = _("but cannot incremental dump new disk");
2646 else if(est(dp)->degr_est->level < 0) {
2648 message = _("but no incremental estimate");
2652 message = _("full dump delayed");
2654 delay_one_dump(dp, delete, _("dumps too big,"), est_kb,
2661 ** 3. Delay incremental dumps.
2663 ** Delay incremental dumps until tomorrow. This is a last ditch attempt
2664 ** at making things fit. Again, we start with the lowest priority (most
2665 ** dispensable) and work forwards.
2668 for(dp = schedq.tail;
2669 dp != NULL && total_size > tape_length;
2673 if(est(dp)->dump_est->level != 0) {
2675 /* Format dumpsize for messages */
2676 g_snprintf(est_kb, 20, "%lld KB,",
2677 (long long)est(dp)->dump_est->csize);
2679 delay_one_dump(dp, 1,
2680 _("dumps way too big,"),
2682 _("must skip incremental dumps"),
2688 ** 4. Reinstate delayed dumps.
2690 ** We might not have needed to stomp on all of the dumps we have just
2691 ** delayed above. Try to reinstate them all starting with the last one
2692 ** and working forwards. It is unlikely that the last one will fit back
2693 ** in but why complicate the code?
2696 /*@i@*/ for(bi = biq.tail; bi != NULL; bi = nbi) {
2697 int avail_tapes = 1;
2700 if(dp->splitsize > (gint64)0)
2701 avail_tapes = conf_runtapes;
2704 new_total = total_size + (gint64)tt_blocksize_kb +
2705 bi->csize + (gint64)tape_mark;
2707 new_total = total_size - est(dp)->dump_est->csize + bi->csize;
2709 if((new_total <= tape_length) &&
2710 (bi->csize < (tapetype_get_length(tape) * (gint64)avail_tapes))) {
2712 total_size = new_total;
2714 if(bi->level == 0) {
2715 total_lev0 += (double) bi->csize;
2717 insert_disk(&schedq, dp, schedule_order);
2720 est(dp)->dump_est = est_for_level(dp, bi->level);
2724 if(bi->next == NULL)
2725 biq.tail = bi->prev;
2727 (bi->next)->prev = bi->prev;
2728 if(bi->prev == NULL)
2729 biq.head = bi->next;
2731 (bi->prev)->next = bi->next;
2739 ** 5. Output messages about what we have done.
2741 ** We can't output messages while we are delaying dumps because we might
2742 ** reinstate them later. We remember all the messages and output them
2746 /*@i@*/ for(bi = biq.head; bi != NULL; bi = nbi) {
2749 g_fprintf(stderr, "%s: FAILED %s\n", get_pname(), bi->errstr);
2750 log_add(L_FAIL, "%s", bi->errstr);
2754 g_fprintf(stderr, _(" delay: %s now at level %d\n"),
2755 bi->errstr, est(dp)->dump_est->level);
2756 log_add(L_INFO, "%s", bi->errstr);
2764 g_fprintf(stderr, _(" delay: Total size now %lld.\n"),
2765 (long long)total_size);
2772 * Remove a dump or modify it from full to incremental.
2773 * Keep track of it on the bi q in case we can add it back later.
2776 static void delay_one_dump,
2782 char level_str[NUM_STR_SIZE];
2785 char *qname = quote_string(dp->name);
2786 char *errstr, *qerrstr;
2788 arglist_start(argp, delete);
2790 total_size -= (gint64)tt_blocksize_kb + est(dp)->dump_est->csize + (gint64)tape_mark;
2791 if(est(dp)->dump_est->level == 0) {
2792 total_lev0 -= (double) est(dp)->dump_est->csize;
2795 bi = alloc(SIZEOF(bi_t));
2797 bi->prev = biq.tail;
2798 if(biq.tail == NULL)
2801 biq.tail->next = bi;
2804 bi->deleted = delete;
2806 bi->level = est(dp)->dump_est->level;
2807 bi->nsize = est(dp)->dump_est->nsize;
2808 bi->csize = est(dp)->dump_est->csize;
2810 g_snprintf(level_str, SIZEOF(level_str), "%d", est(dp)->dump_est->level);
2811 bi->errstr = vstralloc(dp->host->hostname,
2813 " ", planner_timestamp ? planner_timestamp : "?",
2818 while ((next = arglist_val(argp, char *)) != NULL) {
2819 vstrextend(&errstr, sep, next, NULL);
2822 strappend(errstr, "]");
2823 qerrstr = quote_string(errstr);
2824 vstrextend(&bi->errstr, " ", qerrstr, NULL);
2830 remove_disk(&schedq, dp);
2832 est(dp)->dump_est = est(dp)->degr_est;
2833 total_size += (gint64)tt_blocksize_kb + est(dp)->dump_est->csize + (gint64)tape_mark;
2840 static int promote_highest_priority_incremental(void)
2842 disk_t *dp, *dp1, *dp_promote;
2843 gint64 new_total, new_lev0;
2845 int nb_today, nb_same_day, nb_today2;
2846 int nb_disk_today, nb_disk_same_day;
2850 * return 1 if did so; must update total_size correctly; must not
2851 * cause total_size to exceed tape_length
2855 for(dp = schedq.head; dp != NULL; dp = dp->next) {
2856 one_est_t *level0_est = est_for_level(dp, 0);
2857 est(dp)->promote = -1000;
2859 if (level0_est->nsize <= (gint64)0)
2862 if(est(dp)->next_level0 <= 0)
2865 if(est(dp)->next_level0 > dp->maxpromoteday)
2868 new_total = total_size - est(dp)->dump_est->csize + level0_est->csize;
2869 new_lev0 = (gint64)total_lev0 + level0_est->csize;
2874 nb_disk_same_day = 0;
2875 for(dp1 = schedq.head; dp1 != NULL; dp1 = dp1->next) {
2876 if(est(dp1)->dump_est->level == 0)
2878 else if(est(dp1)->next_level0 == est(dp)->next_level0)
2880 if(strcmp(dp->host->hostname, dp1->host->hostname) == 0) {
2881 if(est(dp1)->dump_est->level == 0)
2883 else if(est(dp1)->next_level0 == est(dp)->next_level0)
2888 /* do not promote if overflow tape */
2889 if(new_total > tape_length)
2892 /* do not promote if overflow balanced size and something today */
2893 /* promote if nothing today */
2894 if((new_lev0 > (gint64)(balanced_size + balance_threshold)) &&
2895 (nb_disk_today > 0))
2898 /* do not promote if only one disk due that day and nothing today */
2899 if(nb_disk_same_day == 1 && nb_disk_today == 0)
2902 nb_today2 = nb_today*nb_today;
2903 if(nb_today == 0 && nb_same_day > 1)
2906 if(nb_same_day >= nb_today2) {
2907 est(dp)->promote = ((nb_same_day - nb_today2)*(nb_same_day - nb_today2)) +
2908 conf_dumpcycle - est(dp)->next_level0;
2911 est(dp)->promote = -nb_today2 +
2912 conf_dumpcycle - est(dp)->next_level0;
2915 qname = quote_string(dp->name);
2916 if(!dp_promote || est(dp_promote)->promote < est(dp)->promote) {
2918 g_fprintf(stderr," try %s:%s %d %d %d = %d\n",
2919 dp->host->hostname, qname, nb_same_day, nb_today, est(dp)->next_level0, est(dp)->promote);
2922 g_fprintf(stderr,"no try %s:%s %d %d %d = %d\n",
2923 dp->host->hostname, qname, nb_same_day, nb_today, est(dp)->next_level0, est(dp)->promote);
2929 one_est_t *level0_est;
2931 level0_est = est_for_level(dp, 0);
2933 qname = quote_string(dp->name);
2934 new_total = total_size - est(dp)->dump_est->csize + level0_est->csize;
2935 new_lev0 = (gint64)total_lev0 + level0_est->csize;
2937 total_size = new_total;
2938 total_lev0 = (double)new_lev0;
2939 check_days = est(dp)->next_level0;
2940 est(dp)->degr_est = est(dp)->dump_est;
2941 est(dp)->dump_est = level0_est;
2942 est(dp)->next_level0 = 0;
2945 _(" promote: moving %s:%s up, total_lev0 %1.0lf, total_size %lld\n"),
2946 dp->host->hostname, qname,
2947 total_lev0, (long long)total_size);
2950 plural(_("Full dump of %s:%s promoted from %d day ahead."),
2951 _("Full dump of %s:%s promoted from %d days ahead."),
2953 dp->host->hostname, qname, check_days);
2961 static int promote_hills(void)
2964 struct balance_stats {
2975 /* If we are already doing a level 0 don't bother */
2979 /* Do the guts of an "amadmin balance" */
2980 my_dumpcycle = conf_dumpcycle;
2981 if(my_dumpcycle > 10000) my_dumpcycle = 10000;
2983 sp = (struct balance_stats *)
2984 alloc(SIZEOF(struct balance_stats) * my_dumpcycle);
2986 for(days = 0; days < my_dumpcycle; days++) {
2988 sp[days].size = (gint64)0;
2991 for(dp = schedq.head; dp != NULL; dp = dp->next) {
2992 days = est(dp)->next_level0;
2993 if (days < 0) days = 0;
2994 if(days<my_dumpcycle && !dp->skip_full && dp->strategy != DS_NOFULL &&
2995 dp->strategy != DS_INCRONLY) {
2997 sp[days].size += est(dp)->last_lev0size;
3001 /* Search for a suitable big hill and cut it down */
3003 /* Find the tallest hill */
3004 hill_size = (gint64)0;
3005 for(days = 0; days < my_dumpcycle; days++) {
3006 if(sp[days].disks > 1 && sp[days].size > hill_size) {
3007 hill_size = sp[days].size;
3012 if(hill_size <= (gint64)0) break; /* no suitable hills */
3014 /* Find all the dumps in that hill and try and remove one */
3015 for(dp = schedq.head; dp != NULL; dp = dp->next) {
3016 one_est_t *level0_est;
3017 if(est(dp)->next_level0 != hill_days ||
3018 est(dp)->next_level0 > dp->maxpromoteday ||
3020 dp->strategy == DS_NOFULL ||
3021 dp->strategy == DS_INCRONLY)
3023 level0_est = est_for_level(dp, 0);
3024 if (level0_est->nsize <= (gint64)0)
3026 new_total = total_size - est(dp)->dump_est->csize + level0_est->csize;
3027 if(new_total > tape_length)
3029 /* We found a disk we can promote */
3030 qname = quote_string(dp->name);
3031 total_size = new_total;
3032 total_lev0 += (double)level0_est->csize;
3033 est(dp)->degr_est = est(dp)->dump_est;
3034 est(dp)->dump_est = level0_est;
3035 est(dp)->next_level0 = 0;
3038 _(" promote: moving %s:%s up, total_lev0 %1.0lf, total_size %lld\n"),
3039 dp->host->hostname, qname,
3040 total_lev0, (long long)total_size);
3043 plural(_("Full dump of %s:%s specially promoted from %d day ahead."),
3044 _("Full dump of %s:%s specially promoted from %d days ahead."),
3046 dp->host->hostname, qname, hill_days);
3052 /* All the disks in that hill were unsuitable. */
3053 sp[hill_days].disks = 0; /* Don't get tricked again */
3061 * ========================================================================
3064 * XXX - memory leak - we shouldn't just throw away *dp
3066 static void output_scheduleline(
3070 time_t dump_time = 0, degr_time = 0;
3071 double dump_kps = 0, degr_kps = 0;
3072 char *schedline = NULL, *degr_str = NULL;
3073 char dump_priority_str[NUM_STR_SIZE];
3074 char dump_level_str[NUM_STR_SIZE];
3075 char dump_nsize_str[NUM_STR_SIZE];
3076 char dump_csize_str[NUM_STR_SIZE];
3077 char dump_time_str[NUM_STR_SIZE];
3078 char dump_kps_str[NUM_STR_SIZE];
3079 char degr_level_str[NUM_STR_SIZE];
3080 char degr_nsize_str[NUM_STR_SIZE];
3081 char degr_csize_str[NUM_STR_SIZE];
3082 char degr_time_str[NUM_STR_SIZE];
3083 char degr_kps_str[NUM_STR_SIZE];
3084 char *dump_date, *degr_date;
3086 char *qname = quote_string(dp->name);
3090 if(ep->dump_est->csize == (gint64)-1) {
3091 /* no estimate, fail the disk */
3093 _("%s: FAILED %s %s %s %d \"[no estimate]\"\n"),
3095 dp->host->hostname, qname, planner_timestamp, ep->dump_est->level);
3096 log_add(L_FAIL, _("%s %s %s %d [no estimate]"),
3097 dp->host->hostname, qname, planner_timestamp, ep->dump_est->level);
3102 dump_date = ep->dump_est->dumpdate;
3103 degr_date = ep->degr_est->dumpdate;
3105 #define fix_rate(rate) (rate < 1.0 ? DEFAULT_DUMPRATE : rate)
3107 if(ep->dump_est->level == 0) {
3108 dump_kps = fix_rate(ep->fullrate);
3109 dump_time = (time_t)((double)ep->dump_est->csize / dump_kps);
3111 if(ep->degr_est->csize != (gint64)-1) {
3112 degr_kps = fix_rate(ep->incrrate);
3113 degr_time = (time_t)((double)ep->degr_est->csize / degr_kps);
3117 dump_kps = fix_rate(ep->incrrate);
3118 dump_time = (time_t)((double)ep->dump_est->csize / dump_kps);
3121 if(ep->dump_est->level == 0 && ep->degr_est->csize != (gint64)-1) {
3122 g_snprintf(degr_level_str, sizeof(degr_level_str),
3123 "%d", ep->degr_est->level);
3124 g_snprintf(degr_nsize_str, sizeof(degr_nsize_str),
3125 "%lld", (long long)ep->degr_est->nsize);
3126 g_snprintf(degr_csize_str, sizeof(degr_csize_str),
3127 "%lld", (long long)ep->degr_est->csize);
3128 g_snprintf(degr_time_str, sizeof(degr_time_str),
3129 "%lld", (long long)degr_time);
3130 g_snprintf(degr_kps_str, sizeof(degr_kps_str),
3132 degr_str = vstralloc(" ", degr_level_str,
3134 " ", degr_nsize_str,
3135 " ", degr_csize_str,
3141 if (ep->degr_mesg) {
3142 degr_mesg = quote_string(ep->degr_mesg);
3144 degr_mesg = quote_string(_("Skipping: cannot dump in degraded mode for unknown reason"));
3146 degr_str = vstralloc(" ", degr_mesg, NULL);
3149 g_snprintf(dump_priority_str, SIZEOF(dump_priority_str),
3150 "%d", ep->dump_priority);
3151 g_snprintf(dump_level_str, SIZEOF(dump_level_str),
3152 "%d", ep->dump_est->level);
3153 g_snprintf(dump_nsize_str, sizeof(dump_nsize_str),
3154 "%lld", (long long)ep->dump_est->nsize);
3155 g_snprintf(dump_csize_str, sizeof(dump_csize_str),
3156 "%lld", (long long)ep->dump_est->csize);
3157 g_snprintf(dump_time_str, sizeof(dump_time_str),
3158 "%lld", (long long)dump_time);
3159 g_snprintf(dump_kps_str, sizeof(dump_kps_str),
3161 features = am_feature_to_string(dp->host->features);
3162 schedline = vstralloc("DUMP ",dp->host->hostname,
3165 " ", planner_timestamp,
3166 " ", dump_priority_str,
3167 " ", dump_level_str,
3169 " ", dump_nsize_str,
3170 " ", dump_csize_str,
3173 degr_str ? degr_str : "",
3176 if (est(dp)->dump_est->guessed == 1) {
3177 log_add(L_WARNING, _("WARNING: no history available for %s:%s; guessing that size will be %lld KB\n"), dp->host->hostname, qname, (long long)est(dp)->dump_est->csize);
3179 fputs(schedline, stdout);
3180 fputs(schedline, stderr);
3197 size = internal_server_estimate(dp, info, level, &stats);
3199 est(dp)->dump_est = &est(dp)->estimate[i];
3200 est(dp)->estimate[i].nsize = size;
3202 est(dp)->estimate[i].guessed = 1;