From: Aaron Lauterer <a.lauterer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH cluster 2/3] RRD: fetch data from old rrd file if present and needed
Date: Thu, 4 Sep 2025 16:09:13 +0200 [thread overview]
Message-ID: <20250904140914.3060859-3-a.lauterer@proxmox.com> (raw)
In-Reply-To: <20250904140914.3060859-1-a.lauterer@proxmox.com>
One side effect of the RRD migration is that MAX spikes are flattened
due to the new more fine grained resolution.
If we request RRD data that was still covered in the old rrd file, we
can fetch those data from there and then append newer data from the new
RRD files. This way, we still have the older more coarse resolution for
the old data and keep spikes present.
Old RRD files can be either in {rrd}.old or just {rrd} without the .old
suffix. This depends on if the rrd migration tool has been run on the
host or not.
One side effect of this more dynamic approach is that the step size
determined by RRDs::fetch will not match up in many situations.
Therefore we drop the check for the step size!
For example, if we choose Year as the time frame, but the new file only
has a much shorter amound of data present, RRDs::fetch will use a
smaller step size, if it can still cover the requested data with it.
Visually this will result in very small step sizes, that should get
wider once we get more or all data from the new RRD files, where it
needs to then switch to the longer step sizes to cover the requested
time frame.
Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---
src/PVE/RRD.pm | 109 ++++++++++++++++++++++++++++++++++---------------
1 file changed, 76 insertions(+), 33 deletions(-)
diff --git a/src/PVE/RRD.pm b/src/PVE/RRD.pm
index 34dafef..a6ce34b 100644
--- a/src/PVE/RRD.pm
+++ b/src/PVE/RRD.pm
@@ -7,6 +7,32 @@ use RRDs;
use PVE::Tools;
+my $get_rrd_data = sub {
+ my ($rrd, $cf, $is_node, $reso, $args, $res) = @_;
+ my ($start, $step, $names, $data) = RRDs::fetch($rrd, $cf, @$args);
+
+ my $err = RRDs::error;
+ die "RRD error: $err\n" if $err;
+
+ my $fields = scalar(@$names);
+ for my $line (@$data) {
+ my $entry = { 'time' => $start };
+ $start += $step;
+ for (my $i = 0; $i < $fields; $i++) {
+ my $name = $names->[$i];
+ if (defined(my $val = $line->[$i])) {
+ $entry->{$name} = $val;
+ $entry->{memavailable} = $val
+ if $is_node && $name eq 'memfree' && !exists($entry->{memavailable});
+ } else {
+ # leave empty fields undefined
+ # maybe make this configurable?
+ }
+ }
+ push @$res, $entry;
+ }
+};
+
sub create_rrd_data {
my ($rrdname, $timeframe, $cf) = @_;
@@ -33,54 +59,71 @@ sub create_rrd_data {
decade => [86400 * 7, 570], # 1 week resolution, 10 years
};
+ my $is_node = !!($rrdname =~ /^pve-node/);
+ $cf = "AVERAGE" if !$cf;
+ my $res = [];
+
if ($rrdname =~ /^pve2/) {
$setup = $setup_pve2;
$timeframe = "year" if $timeframe eq "decade"; # we only store up to one year in the old format
}
- my $is_node = !!($rrdname =~ /^pve-node/);
my ($reso, $count) = @{ $setup->{$timeframe} };
my $ctime = $reso * int(time() / $reso);
my $req_start = $ctime - $reso * $count;
- $cf = "AVERAGE" if !$cf;
-
- my @args = (
- "-s" => $req_start,
- "-e" => $ctime - 1,
- "-r" => $reso,
- );
-
- my $socket = "/var/run/rrdcached.sock";
- push @args, "--daemon" => "unix:$socket" if -S $socket;
-
- my ($start, $step, $names, $data) = RRDs::fetch($rrd, $cf, @args);
-
- my $err = RRDs::error;
- die "RRD error: $err\n" if $err;
-
- die "got wrong time resolution ($step != $reso)\n"
- if $step != $reso;
+ my $last_old;
+ # check if we have old rrd file and if the start point is still covered by
+ # it, fetch that data from it for any data not available in the old file we
+ # will fetch it from the new file.
+ if ($rrdname =~ /pve-(?<type>node|vm|storage)-[0-9]*\.[0-9]*\/(?<resource>.*)/) {
+ my $old_rrd = "${rrddir}/pve2-$+{type}/$+{resource}";
+ my $old_exists = 0;
+
+ # we can have already migrated rrd files that have the .old suffix too
+ if (-e $old_rrd) {
+ $old_exists = 1;
+ } elsif (-e "${old_rrd}.old") {
+ $old_exists = 1;
+ $old_rrd = "${old_rrd}.old";
+ }
- my $res = [];
- my $fields = scalar(@$names);
- for my $line (@$data) {
- my $entry = { 'time' => $start };
- $start += $step;
- for (my $i = 0; $i < $fields; $i++) {
- my $name = $names->[$i];
- if (defined(my $val = $line->[$i])) {
- $entry->{$name} = $val;
- $entry->{memavailable} = $val
- if $is_node && $name eq 'memfree' && !exists($entry->{memavailable});
+ if ($old_exists) {
+ $last_old = RRDs::last($old_rrd);
+ if ($req_start < $last_old) {
+ my ($reso_old, $count_old) = @{ $setup_pve2->{$timeframe} };
+ my $ctime_old = $reso_old * int(time() / $reso_old);
+ my $req_start_old = $ctime_old - $reso_old * $count_old;
+ my $args = [];
+ push(@$args, "-s" => $req_start_old);
+ push(@$args, "-e" => $last_old);
+ push(@$args, "-r" => $reso_old);
+
+ my $socket = "/var/run/rrdcached.sock";
+ push @$args, "--daemon" => "unix:$socket" if -S $socket;
+
+ $get_rrd_data->($old_rrd, $cf, $is_node, $reso_old, $args, $res);
} else {
- # leave empty fields undefined
- # maybe make this configurable?
+ $last_old = undef;
}
}
- push @$res, $entry;
}
+ my $args = [];
+ if ($last_old) {
+ push(@$args, "-s" => $last_old);
+ } else {
+ push(@$args, "-s" => $req_start);
+ }
+
+ push(@$args, "-e" => $ctime - 1);
+ push(@$args, "-r" => $reso);
+
+ my $socket = "/var/run/rrdcached.sock";
+ push @$args, "--daemon" => "unix:$socket" if -S $socket;
+
+ $get_rrd_data->($rrd, $cf, $is_node, $reso, $args, $res);
+
return $res;
}
--
2.47.2
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
next prev parent reply other threads:[~2025-09-04 14:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-04 14:09 [pve-devel] [RFC many 0/3] combine and simplify RRD handling Aaron Lauterer
2025-09-04 14:09 ` [pve-devel] [PATCH cluster 1/3] rrd: fix rrd time frames Aaron Lauterer
2025-09-04 14:09 ` Aaron Lauterer [this message]
2025-09-04 14:09 ` [pve-devel] [PATCH cluster 3/3] pmxcfs: status.c: always use 9.0 rrd files Aaron Lauterer
2025-09-04 18:20 ` [pve-devel] [RFC many 0/3] combine and simplify RRD handling Thomas Lamprecht
2025-09-05 8:04 ` Aaron Lauterer
2025-09-05 8:12 ` Thomas Lamprecht
2025-09-05 12:54 ` Aaron Lauterer
2025-09-05 13:58 ` Aaron Lauterer
2025-09-05 13:55 [pve-devel] [PATCH cluster/container/manager/qemu-server/storage 0/7] " Aaron Lauterer
2025-09-05 13:55 ` [pve-devel] [PATCH cluster 2/3] RRD: fetch data from old rrd file if present and needed Aaron Lauterer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250904140914.3060859-3-a.lauterer@proxmox.com \
--to=a.lauterer@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.