Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ See each [GitHub Release](https://github.com/TheGrandWazoo/truenas-proxmox/relea

---

## [Unreleased] — 4.0.4

### Fixed
- **`status()` made TrueNAS build the whole dataset tree on every poll**
(#310, reported in #309 by @esaudet). `pvestatd` calls `status()` every
~10 s on every node for every storage. Each call queried the pool root
with all its children, which kept `middlewared` at 300–400 % CPU on a
~600-dataset pool with 8 nodes and timed out `alloc_image`. `status()` now
queries `/pool/dataset/id/<pool>` with `extra.retrieve_children=false`.
It still reports the pool root, so PVE shows the same numbers.
- The WebSocket transport (SCALE 25.04+) dropped query options, so the
option is now mapped onto the JSON-RPC query options. Without that, the
fix would only have helped REST.
- CORE 13 ignores the option (no change there). An `?id=` filter combined
with the option makes CORE return `[]`, so the `/id/` form is the only
one that is safe on every supported version.
- Measured on the pool root response (small lab pools): SCALE 25.04 WS
10.6 KB → 3.4 KB, 25.10 WS 14.2 KB → 3.5 KB. The saving grows with the
number of datasets.

---

## [4.0.3] — 2026-10-14

WebSocket reliability. No config changes. Milestone: *v4.0.3 — WebSocket reliability*.
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,19 @@ Other symptoms:
- If you can't upgrade yet, `systemctl restart pvedaemon` clears the stale connections, but only until they go idle again.
- A VM left in `lock: create` with no disks can be removed with `qm unlock <vmid> && qm destroy <vmid>`.

### High `middlewared` CPU on TrueNAS, or disk creation timing out, on large pools (4.0.3 and earlier)

**Symptom:** TrueNAS `middlewared` stays at several hundred percent CPU while Proxmox is running. It drops when `pvestatd` is stopped. VM disk creation, or other plugin calls, time out. This shows up first on pools with hundreds of datasets and on clusters with many PVE nodes.

**Cause:** `pvestatd` calls the plugin's `status()` every ~10 s, on every node, for every TrueNAS storage. Up to 4.0.3, each call made TrueNAS build the full dataset tree under the pool, so the load grows with *nodes × storages × datasets*. **Fixed in v4.0.4** ([#310](https://github.com/TheGrandWazoo/freenas-proxmox/issues/310)): `status()` now fetches only the pool root, without its children, on both REST and WebSocket. The usage numbers PVE shows do not change.

**On 4.0.3 and earlier:**
- Upgrade.
- To confirm the cause, run `systemctl stop pvestatd` on all nodes briefly and watch `middlewared` CPU on TrueNAS. Then run `systemctl start pvestatd` again.
- `journalctl -u pvestatd | grep "status update time"` shows cycles that took longer than 5 s.

If load is still high on 4.0.4, please add your numbers (`middlewared` CPU, dataset count, node count, TrueNAS version) to [#311](https://github.com/TheGrandWazoo/freenas-proxmox/issues/311).

### Filing a Bug Report

Please use the [GitHub issue tracker](https://github.com/TheGrandWazoo/truenas-proxmox/issues) and include the [bug report template](.github/ISSUE_TEMPLATE/bug_report.md). Include relevant log lines from `syslog` (search for `FreeNAS::`).
Expand Down
30 changes: 25 additions & 5 deletions perl5/PVE/Storage/Custom/TrueNAS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,20 @@ sub _ws_call {
return $result;
}

# Maps REST "extra.<key>=<value>" query parameters onto JSON-RPC query
# options {extra => {<key> => <value>}}, with "true"/"false" as booleans.
# Without this the WS transport silently drops them (#310).
sub _ws_query_opts {
my ($q) = @_;
my %extra;
for my $k (grep { /^extra\./ } keys %$q) {
my $v = $q->{$k};
$v = $v eq 'true' ? JSON::true : $v eq 'false' ? JSON::false : $v;
$extra{ substr($k, 6) } = $v;
}
return %extra ? { extra => \%extra } : {};
}

# Translates a REST-style (method, path, data) call onto its JSON-RPC 2.0
# equivalent and executes it over _ws_connect's transport. Read (query)
# mappings are confirmed live (ADR-012); write mappings are inferred from
Expand Down Expand Up @@ -594,7 +608,7 @@ sub _api_ws {
my @filter;
push @filter, [ 'id', '=', $q{id} ] if defined $q{id};
push @filter, [ 'type', '=', $q{type} ] if defined $q{type};
$params = [\@filter, {}];
$params = [\@filter, _ws_query_opts(\%q)];
}

} elsif ($base =~ m{^/pool/dataset/id/(.+)$}) {
Expand All @@ -607,7 +621,7 @@ sub _api_ws {
$params = [$id, $data];
} else {
$rpc_method = 'pool.dataset.query';
$params = [[[ 'id', '=', $id ]], {}];
$params = [[[ 'id', '=', $id ]], _ws_query_opts(\%q)];
$single = 1;
}

Expand Down Expand Up @@ -890,10 +904,16 @@ sub status {
my ($class, $storeid, $scfg, $cache) = @_;
_resolve_token($storeid, $scfg);

# pvestatd calls this every ~10 s on every node. Without
# retrieve_children=false TrueNAS builds the whole dataset tree under the
# pool on each call, which pins middlewared on large pools (#310). Must be
# the /id/ form: CORE 13 ignores the option there but returns [] when it
# is combined with an ?id= filter (verified live 2026-11-26).
my $pool_name = (split m{/}, $scfg->{truenas_pool})[0];
my $datasets = _api($scfg, 'GET', "/pool/dataset?id=$pool_name") // [];
my ($ds) = grep { $_->{name} eq $pool_name } @$datasets;
die "Pool dataset '$pool_name' not found on $scfg->{truenas_host}\n" unless $ds;
my $enc = uri_escape($pool_name, "^A-Za-z0-9\\-_.~");
my $ds = _api($scfg, 'GET', "/pool/dataset/id/$enc?extra.retrieve_children=false");
die "Pool dataset '$pool_name' not found on $scfg->{truenas_host}\n"
unless ref($ds) eq 'HASH' && ($ds->{name} // '') eq $pool_name;

my $free = $ds->{available}{parsed} // 0;
my $used = $ds->{used}{parsed} // 0;
Expand Down
2 changes: 1 addition & 1 deletion t/lib/MockTrueNAS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ sub _route {
my ($t) = grep { $_->{id} == $1 } @{ $db->{targets} // [] };
return $t;
}
if ($path =~ m{^/pool/dataset/id/(.+)$}) {
if ($path =~ m{^/pool/dataset/id/([^?]+)}) {
my $name = URI::Escape::uri_unescape($1);
return $db->{datasets}{$name};
}
Expand Down
75 changes: 75 additions & 0 deletions t/status.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/perl
# status() polling cost (#310): pvestatd calls status() every ~10 s on every
# node. It must fetch only the pool root dataset, without its children, on
# both transports — the WS shim has to carry extra.retrieve_children through.

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/lib", "$FindBin::Bin/../perl5";

use Test::More;
use JSON;

use MockTrueNAS;
use PVE::Storage::Custom::TrueNAS;

my $TN = 'PVE::Storage::Custom::TrueNAS';

subtest 'status() reports the pool root, without children' => sub {
my $api = MockTrueNAS->install(datasets => {
tank => { name => 'tank', used => { parsed => 300 }, available => { parsed => 700 } },
});
my $scfg = { %{ MockTrueNAS::scfg() }, truenas_pool => 'tank/proxmox' };

my @r = $TN->status('store', $scfg, {});
is_deeply(\@r, [ 1000, 700, 300, 1 ], '(total, free, used, active) from the pool root');

my @gets = grep { $_->[1] =~ m{^/pool/dataset} } @{ $api->calls };
is(scalar @gets, 1, 'one dataset call');
is($gets[0][1], '/pool/dataset/id/tank?extra.retrieve_children=false',
'/id/ form with retrieve_children=false (not ?id=, which CORE answers with [])');
};

subtest 'status() dies when the pool is missing' => sub {
MockTrueNAS->install(datasets => {});
eval { $TN->status('store', MockTrueNAS::scfg(), {}) };
like($@, qr/Pool dataset 'tank' not found/, 'dies');
};

# Runs _api_ws with the transport stubbed out and returns the JSON-RPC call.
sub ws_call_for {
my ($method, $path) = @_;
my @sent;
no warnings 'redefine'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
local *PVE::Storage::Custom::TrueNAS::_ws_connect = sub { return; };
local *PVE::Storage::Custom::TrueNAS::_ws_call = sub {
my (undef, $rpc, $params) = @_;
push @sent, [ $rpc, $params ];
return { result => [ { name => 'tank' } ] };
};
$TN->can('_api_ws')->(MockTrueNAS::scfg(), $method, $path);
return $sent[0];
}

subtest 'WS shim maps extra.retrieve_children into query options' => sub {
my ($rpc, $params) = @{ ws_call_for('GET', '/pool/dataset/id/tank?extra.retrieve_children=false') };
is($rpc, 'pool.dataset.query', 'method');
is_deeply($params->[0], [ [ 'id', '=', 'tank' ] ], 'filter on the id, query string stripped');
ok(JSON::is_bool($params->[1]{extra}{retrieve_children}), 'value is a JSON boolean');
ok(!$params->[1]{extra}{retrieve_children}, 'retrieve_children is false');

(undef, $params) = @{ ws_call_for('GET', '/pool/dataset?id=tank&extra.retrieve_children=false') };
ok(!$params->[1]{extra}{retrieve_children}, 'same on the filtered list form');
};

subtest 'WS shim sends empty options when there are none' => sub {
my (undef, $params) = @{ ws_call_for('GET', '/pool/dataset/id/tank%2Fvm%2Fvm-100-disk-0') };
is_deeply($params, [ [ [ 'id', '=', 'tank/vm/vm-100-disk-0' ] ], {} ], 'unchanged for other callers');

(undef, $params) = @{ ws_call_for('GET', '/pool/dataset?type=VOLUME') };
is_deeply($params->[1], {}, 'list form unchanged');
};

done_testing();