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
6 changes: 2 additions & 4 deletions modules/balancer/agent/balancerpb/inspect.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ message PacketHandlerInspect {

// State memory usage
message StateInspect {
uint64 vs_registry_usage = 1;
uint64 reals_registry_usage = 2;
uint64 session_table_usage = 3;
uint64 total_usage = 4;
uint64 session_table_usage = 1;
uint64 total_usage = 2;
}

// Per-balancer memory inspection
Expand Down
6 changes: 2 additions & 4 deletions modules/balancer/agent/go/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1610,10 +1610,8 @@ func ConvertStateInspectToProto(
}

return &balancerpb.StateInspect{
VsRegistryUsage: inspect.VsRegistryUsage,
RealsRegistryUsage: inspect.RealsRegistryUsage,
SessionTableUsage: inspect.SessionTableUsage,
TotalUsage: inspect.TotalUsage,
SessionTableUsage: inspect.SessionTableUsage,
TotalUsage: inspect.TotalUsage,
}
}

Expand Down
6 changes: 2 additions & 4 deletions modules/balancer/agent/go/ffi/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,7 @@ func cToGoRealsUsage(cUsage *C.struct_reals_usage) *RealsUsage {

func cToGoStateInspect(cInspect *C.struct_state_inspect) *StateInspect {
return &StateInspect{
VsRegistryUsage: uint64(cInspect.vs_registry_usage),
RealsRegistryUsage: uint64(cInspect.reals_registry_usage),
SessionTableUsage: uint64(cInspect.session_table_usage),
TotalUsage: uint64(cInspect.total_usage),
SessionTableUsage: uint64(cInspect.session_table_usage),
TotalUsage: uint64(cInspect.total_usage),
}
}
23 changes: 14 additions & 9 deletions modules/balancer/agent/go/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package balancer
import (
"context"
"fmt"
"maps"
"net/netip"
"strconv"
"sync"
Expand All @@ -25,7 +26,6 @@ type BalancerManager struct {
realUpdateBuffer []ffi.RealUpdate

// Background task management
ctx context.Context
cancel context.CancelFunc

mu sync.Mutex
Expand Down Expand Up @@ -56,9 +56,7 @@ func (b *BalancerManager) newHandlerTracker(handle string, extraLabels ...metric
"config": b.Name(),
}
for _, extra := range extraLabels {
for k, v := range extra {
labels[k] = v
}
maps.Copy(labels, extra)
}
return newHandlerMetricTracker(handle, &b.handlerMetrics, defaultLatencyBoundsMS, labels)
}
Expand Down Expand Up @@ -127,6 +125,9 @@ func (b *BalancerManager) Update(
"vs_identifiers", updateInfo.ACLReusedVs)
}

// restart background tasks
b.startBackgroundTasks()

return updateInfo, nil
}

Expand Down Expand Up @@ -484,22 +485,25 @@ func (b *BalancerManager) Sessions(
}

func (b *BalancerManager) startBackgroundTasks() {
b.ctx, b.cancel = context.WithCancel(context.Background())
b.stopBackgroundTasks()

if b.handle.Config().RefreshPeriod == 0 {
return
}

var ctx context.Context
ctx, b.cancel = context.WithCancel(context.Background())

// Start background refresh task
go b.backgroundRefreshTask()
go b.backgroundRefreshTask(ctx)
}

// backgroundRefreshTask runs periodically to:
// 1. Get balancer info
// 2. Resize session table if load factor exceeds threshold
// 3. Adjust WLC weights based on active connections
// 4. Apply real updates if needed
func (b *BalancerManager) backgroundRefreshTask() {
func (b *BalancerManager) backgroundRefreshTask(ctx context.Context) {
for {
// Get current config to check refresh period
b.mu.Lock()
Expand All @@ -517,7 +521,7 @@ func (b *BalancerManager) backgroundRefreshTask() {

// Wait for refresh period or context cancellation
select {
case <-b.ctx.Done():
case <-ctx.Done():
b.log.Debugw("background refresh task stopped (context cancelled)")
return
case <-time.After(refreshPeriod):
Expand All @@ -541,7 +545,7 @@ func (b *BalancerManager) Refresh(now time.Time) error {
tracker := b.newHandlerTracker("refresh")
defer tracker.Fix()

b.log.Debug("refreshing")
b.log.Info("refreshing state")

// Get current config
config := b.handle.Config()
Expand Down Expand Up @@ -601,6 +605,7 @@ func (b *BalancerManager) Refresh(now time.Time) error {
func (b *BalancerManager) stopBackgroundTasks() {
if b.cancel != nil {
b.cancel()
b.cancel = nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/balancer/bench/go/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

var (
PacketsMemory int = (1 << 32) + (1 << 30)
TotalMemory int = (1 << 32) + PacketsMemory
CpMemory int = TotalMemory - PacketsMemory
TotalMemory int = CpMemory + PacketsMemory
CpMemory int = (1 << 33)
AgentMemory int = CpMemory - (1 << 27)
)

Expand Down
6 changes: 0 additions & 6 deletions modules/balancer/cli/src/json_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,6 @@ pub struct RealsUsageJson {

#[derive(Serialize)]
pub struct StateInspectJson {
pub vs_registry_usage: u64,
pub reals_registry_usage: u64,
pub session_table_usage: u64,
pub total_usage: u64,
}
Expand Down Expand Up @@ -937,14 +935,10 @@ fn convert_reals_usage(reals: Option<&balancerpb::RealsUsage>) -> RealsUsageJson
fn convert_state_inspect(state: Option<&balancerpb::StateInspect>) -> StateInspectJson {
match state {
Some(state) => StateInspectJson {
vs_registry_usage: state.vs_registry_usage,
reals_registry_usage: state.reals_registry_usage,
session_table_usage: state.session_table_usage,
total_usage: state.total_usage,
},
None => StateInspectJson {
vs_registry_usage: 0,
reals_registry_usage: 0,
session_table_usage: 0,
total_usage: 0,
},
Expand Down
40 changes: 0 additions & 40 deletions modules/balancer/cli/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2226,26 +2226,6 @@ fn print_show_inspect_normal(response: &balancerpb::ShowInspectResponse) -> Resu
format_bytes(state.total_usage).bright_green(),
state_percent
);
let vs_reg_pct = if balancer.total_usage > 0 {
(state.vs_registry_usage as f64 / balancer.total_usage as f64) * 100.0
} else {
0.0
};
println!(
" VS Registry: {} ({:.1}%)",
format_bytes(state.vs_registry_usage).bright_green(),
vs_reg_pct
);
let reals_reg_pct = if balancer.total_usage > 0 {
(state.reals_registry_usage as f64 / balancer.total_usage as f64) * 100.0
} else {
0.0
};
println!(
" Reals Registry: {} ({:.1}%)",
format_bytes(state.reals_registry_usage).bright_green(),
reals_reg_pct
);
let session_pct = if balancer.total_usage > 0 {
(state.session_table_usage as f64 / balancer.total_usage as f64) * 100.0
} else {
Expand Down Expand Up @@ -2685,26 +2665,6 @@ fn print_show_inspect_detail(response: &balancerpb::ShowInspectResponse) -> Resu
format_bytes(state.total_usage).bright_green(),
state_percent
);
let vs_reg_pct = if balancer.total_usage > 0 {
(state.vs_registry_usage as f64 / balancer.total_usage as f64) * 100.0
} else {
0.0
};
println!(
" VS Registry: {} ({:.1}%)",
format_bytes(state.vs_registry_usage).bright_green(),
vs_reg_pct
);
let reals_reg_pct = if balancer.total_usage > 0 {
(state.reals_registry_usage as f64 / balancer.total_usage as f64) * 100.0
} else {
0.0
};
println!(
" Reals Registry: {} ({:.1}%)",
format_bytes(state.reals_registry_usage).bright_green(),
reals_reg_pct
);
let session_pct = if balancer.total_usage > 0 {
(state.session_table_usage as f64 / balancer.total_usage as f64) * 100.0
} else {
Expand Down
2 changes: 0 additions & 2 deletions modules/balancer/controlplane/api/inspect.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ struct packet_handler_inspect {
};

struct state_inspect {
size_t vs_registry_usage;
size_t reals_registry_usage;
size_t session_table_usage;
size_t total_usage;
};
Expand Down
8 changes: 6 additions & 2 deletions modules/balancer/controlplane/handler/inspect.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "common/memory_address.h"
#include "compiler.h"
#include "map.h"
#include "registry.h"
#include "vs.h"
#include <stdlib.h>

Expand Down Expand Up @@ -50,8 +51,11 @@ packet_handler_inspect(
inspect->summary_vs_usage = inspect->vs_ipv4_inspect.summary_vs_usage +
inspect->vs_ipv6_inspect.summary_vs_usage +
sizeof(struct vs) * handler->vs_count;
inspect->reals_index_usage = map_memory_usage(&handler->reals_index);
inspect->vs_index_usage = map_memory_usage(&handler->vs_index);
inspect->reals_index_usage =
map_memory_usage(&handler->reals_index) +
reals_registry_usage(&handler->reals_registry);
inspect->vs_index_usage = map_memory_usage(&handler->vs_index) +
vs_registry_usage(&handler->vs_registry);
inspect->counters_usage = (sizeof(struct balancer_icmp_stats) * 2 +
sizeof(struct balancer_common_stats) +
sizeof(struct balancer_l4_stats)) *
Expand Down
16 changes: 16 additions & 0 deletions modules/balancer/controlplane/handler/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ service_registry_free(struct service_registry *registry) {
memset(registry, 0, sizeof(*registry));
}

size_t
service_registry_usage(struct service_registry *registry) {
return big_array_memory_usage(&registry->elems) +
big_array_memory_usage(&registry->indices);
}

ssize_t
service_registry_lookup(
struct service_registry *registry, const void *elem, registry_cmp cmp
Expand Down Expand Up @@ -217,6 +223,11 @@ vs_registry_lookup(vs_registry_t *registry, const struct vs_identifier *vs) {
return service_registry_lookup(registry, vs, vs_identifier_cmp);
}

size_t
vs_registry_usage(vs_registry_t *registry) {
return service_registry_usage(registry);
}

int
reals_registry_init(
reals_registry_t *registry,
Expand Down Expand Up @@ -246,4 +257,9 @@ reals_registry_lookup(
reals_registry_t *registry, const struct real_identifier *real
) {
return service_registry_lookup(registry, real, real_identifier_cmp);
}

size_t
reals_registry_usage(reals_registry_t *registry) {
return service_registry_usage(registry);
}
11 changes: 10 additions & 1 deletion modules/balancer/controlplane/handler/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ service_registry_lookup(
struct service_registry *registry, const void *elem, registry_cmp cmp
);

size_t
service_registry_usage(struct service_registry *registry);

typedef struct service_registry vs_registry_t;

int
Expand All @@ -55,6 +58,9 @@ vs_registry_free(vs_registry_t *registry);
ssize_t
vs_registry_lookup(vs_registry_t *registry, const struct vs_identifier *vs);

size_t
vs_registry_usage(vs_registry_t *registry);

typedef struct service_registry reals_registry_t;

int
Expand All @@ -72,4 +78,7 @@ reals_registry_free(reals_registry_t *registry);
ssize_t
reals_registry_lookup(
reals_registry_t *registry, const struct real_identifier *real
);
);

size_t
reals_registry_usage(reals_registry_t *registry);
2 changes: 0 additions & 2 deletions modules/balancer/controlplane/state/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,5 @@ balancer_state_inspect(
) {
inspect->session_table_usage =
session_table_memory_usage(&state->session_table);
inspect->vs_registry_usage = 0; // Moved to packet_handler
inspect->reals_registry_usage = 0; // Moved to packet_handler
inspect->total_usage = inspect->session_table_usage;
}
Loading