Ejemplo n.º 1
0
/*
 * _get_db_index - get ID in database of a job
 *
 * IN pg_conn: database connection
 * IN submit: submit time of job
 * IN jobid: jobid of job
 * IN associd: association id of job
 * RET: db id of job, 0 on error
 */
static int
_get_db_index(pgsql_conn_t *pg_conn, time_t submit, uint32_t jobid,
	      uint32_t associd)
{
	DEF_VARS;
	int db_index = 0;

	query = xstrdup_printf(
		"SELECT job_db_inx FROM %s.%s WHERE time_submit=%ld"
		" AND id_job=%u AND id_assoc=%u", pg_conn->cluster_name,
		job_table, submit, jobid, associd);
	result = DEF_QUERY_RET;
	if (!result)
		return 0;

	if (!PQntuples(result)) {
		debug("We can't get a db_index for this combo, "
		      "time_submit=%ld and id_job=%u and id_assoc=%u."
		      "We must not have heard about the start yet, "
		      "no big deal, we will get one right after this.",
		      submit, jobid, associd);
	} else {
		db_index = atoi(PG_VAL(0));
	}
	PQclear(result);
	return db_index;
}
Ejemplo n.º 2
0
/* rollup usage for one cluster */
static int
_cluster_rollup_usage(pgsql_conn_t *pg_conn,  char *cluster,
		      time_t sent_start, time_t sent_end,
		      uint16_t archive_data)
{
	DEF_VARS;
	int rc = SLURM_SUCCESS;
	time_t last_hour = sent_start;
	time_t last_day = sent_start;
	time_t last_month = sent_start;
	time_t start_time = 0;
  	time_t end_time = 0;
	time_t my_time = sent_end;
	struct tm start_tm, end_tm;
	DEF_TIMERS;
	char *ru_fields = "hourly_rollup, daily_rollup, monthly_rollup";
	enum {
		F_HOUR,
		F_DAY,
		F_MONTH,
		F_COUNT
	};

	if (!sent_start) {
		query = xstrdup_printf("SELECT %s FROM %s.%s LIMIT 1",
				       ru_fields, cluster, last_ran_table);
		result = DEF_QUERY_RET;
		if (!result)
			return SLURM_ERROR;

		if (PQntuples(result)) {
			last_hour = atoi(PG_VAL(F_HOUR));
			last_day = atoi(PG_VAL(F_DAY));
			last_month = atoi(PG_VAL(F_MONTH));
			PQclear(result);
		} else {
			time_t now = time(NULL);
			PQclear(result);
			query = xstrdup_printf("SELECT %s.init_last_ran(%ld);",
					       cluster, now);
			result = DEF_QUERY_RET;
			if (!result)
				return SLURM_ERROR;
			last_hour = last_day = last_month =
				atoi(PG_VAL(0));
			PQclear(result);
			if (last_hour < 0) {
				debug("cluster %s not registered, "
				      "not doing rollup", cluster);
				return SLURM_SUCCESS;
			}
		}
	}

	if (!my_time)
		my_time = time(NULL);

	if (!localtime_r(&last_hour, &start_tm)) {
		error("Couldn't get localtime from hour start %ld", last_hour);
		return SLURM_ERROR;
	}
	if (!localtime_r(&my_time, &end_tm)) {
		error("Couldn't get localtime from hour end %ld", my_time);
		return SLURM_ERROR;
	}

	/* below and anywhere in a rollup plugin when dealing with
	 * epoch times we need to set the tm_isdst = -1 so we don't
	 * have to worry about the time changes.  Not setting it to -1
	 * will cause problems in the day and month with the date change.
	 */

	/* align to hour boundary */
	start_tm.tm_sec = 0;
	start_tm.tm_min = 0;
	start_tm.tm_isdst = -1;
	start_time = mktime(&start_tm);
	end_tm.tm_sec = 0;
	end_tm.tm_min = 0;
	end_tm.tm_isdst = -1;
	end_time = mktime(&end_tm);

/* 	info("hour start %s", ctime(&start_time)); */
/* 	info("hour end %s", ctime(&end_time)); */
/* 	info("diff is %d", end_time-start_time); */

	//slurm_mutex_lock(&rollup_lock);
	global_last_rollup = end_time;
	//slurm_mutex_unlock(&rollup_lock);

	if (end_time-start_time > 0) {
		START_TIMER;
		if ((rc = pgsql_hourly_rollup(pg_conn, cluster, start_time, end_time))
		   != SLURM_SUCCESS)
			return rc;
		END_TIMER3("hourly_rollup", 5000000);
		/* If we have a sent_end do not update the last_run_table */
		if (!sent_end)
			query = xstrdup_printf(
				"UPDATE %s.%s SET hourly_rollup=%ld",
				cluster, last_ran_table, end_time);
	} else {
		debug2("no need to run this hour %ld <= %ld",
		       end_time, start_time);
	}


	if (!localtime_r(&last_day, &start_tm)) {
		error("Couldn't get localtime from day %ld", last_day);
		return SLURM_ERROR;
	}
	/* align to day boundary */
	start_tm.tm_sec = 0;
	start_tm.tm_min = 0;
	start_tm.tm_hour = 0;
	start_tm.tm_isdst = -1;
	start_time = mktime(&start_tm);
	end_tm.tm_hour = 0;
	end_tm.tm_isdst = -1;
	end_time = mktime(&end_tm);

/* 	info("day start %s", ctime(&start_time)); */
/* 	info("day end %s", ctime(&end_time)); */
/* 	info("diff is %d", end_time-start_time); */

	if (end_time-start_time > 0) {
		START_TIMER;
		if ((rc = pgsql_daily_rollup(pg_conn, cluster, start_time,
					    end_time, archive_data))
		   != SLURM_SUCCESS)
			return rc;
		END_TIMER2("daily_rollup");
		if (query && !sent_end)
			xstrfmtcat(query, ", daily_rollup=%ld", (long)end_time);
		else if (!sent_end)
			query = xstrdup_printf(
				"UPDATE %s.%s SET daily_rollup=%ld",
				cluster, last_ran_table, (long)end_time);
	} else {
		debug2("no need to run this day %ld <= %ld",
		       (long)end_time, (long)start_time);
	}

	if (!localtime_r(&last_month, &start_tm)) {
		error("Couldn't get localtime from month %ld", last_month);
		return SLURM_ERROR;
	}

	/* align to month boundary */
	start_tm.tm_sec = 0;
	start_tm.tm_min = 0;
	start_tm.tm_hour = 0;
	start_tm.tm_mday = 1;
	start_tm.tm_isdst = -1;
	start_time = mktime(&start_tm);
	end_time = mktime(&end_tm);

	end_tm.tm_sec = 0;
	end_tm.tm_min = 0;
	end_tm.tm_hour = 0;
	end_tm.tm_mday = 1;
	end_tm.tm_isdst = -1;
	end_time = mktime(&end_tm);

/* 	info("month start %s", ctime(&start_time)); */
/* 	info("month end %s", ctime(&end_time)); */
/* 	info("diff is %d", end_time-start_time); */

	if (end_time-start_time > 0) {
		START_TIMER;
		if ((rc = pgsql_monthly_rollup(pg_conn, cluster, start_time,
					      end_time, archive_data))
		   != SLURM_SUCCESS)
			return rc;
		END_TIMER2("monthly_rollup");

		if (query && !sent_end)
			xstrfmtcat(query, ", monthly_rollup=%ld",
				   (long)end_time);
		else if (!sent_end)
			query = xstrdup_printf(
				"UPDATE %s.%s SET monthly_rollup=%ld",
				cluster, last_ran_table, (long)end_time);
	} else {
		debug2("no need to run this month %ld <= %ld",
		       (long)end_time, (long)start_time);
	}

	if (query) {
		rc = DEF_QUERY_RET_RC;
	}
	return rc;
}