Debian Codegraph @

src/interfaces/libpq/t/003_load_balance_host_list.pl

raw
1 # Copyright (c) 2023-2025, PostgreSQL Global Development Group
2 use strict;
3 use warnings FATAL => 'all';
4 use Config;
5 use PostgreSQL::Test::Utils;
6 use PostgreSQL::Test::Cluster;
7 use Test::More;
8
9 # This tests load balancing across the list of different hosts in the host
10 # parameter of the connection string.
11
12 # Cluster setup which is shared for testing both load balancing methods
13 my $node1 = PostgreSQL::Test::Cluster->new('node1');
14 my $node2 = PostgreSQL::Test::Cluster->new('node2', own_host => 1);
15 my $node3 = PostgreSQL::Test::Cluster->new('node3', own_host => 1);
16
17 # Create a data directory with initdb
18 $node1->init();
19 $node2->init();
20 $node3->init();
21
22 # Start the PostgreSQL server
23 $node1->start();
24 $node2->start();
25 $node3->start();
26
27 # Start the tests for load balancing method 1
28 my $hostlist = $node1->host . ',' . $node2->host . ',' . $node3->host;
29 my $portlist = $node1->port . ',' . $node2->port . ',' . $node3->port;
30
31 $node1->connect_fails(
32 "host=$hostlist port=$portlist load_balance_hosts=doesnotexist",
33 "load_balance_hosts doesn't accept unknown values",
34 expected_stderr => qr/invalid load_balance_hosts value: "doesnotexist"/);
35
36 # load_balance_hosts=disable should always choose the first one.
37 $node1->connect_ok(
38 "host=$hostlist port=$portlist load_balance_hosts=disable",
39 "load_balance_hosts=disable connects to the first node",
40 sql => "SELECT 'connect1'",
41 log_like => [qr/statement: SELECT 'connect1'/]);
42
43 # Statistically the following loop with load_balance_hosts=random will almost
44 # certainly connect at least once to each of the nodes. The chance of that not
45 # happening is so small that it's negligible: (2/3)^50 = 1.56832855e-9
46 foreach my $i (1 .. 50)
47 {
48 $node1->connect_ok(
49 "host=$hostlist port=$portlist load_balance_hosts=random",
50 "repeated connections with random load balancing",
51 sql => "SELECT 'connect2'");
52 }
53
54 my $node1_occurrences = () =
55 $node1->log_content() =~ /statement: SELECT 'connect2'/g;
56 my $node2_occurrences = () =
57 $node2->log_content() =~ /statement: SELECT 'connect2'/g;
58 my $node3_occurrences = () =
59 $node3->log_content() =~ /statement: SELECT 'connect2'/g;
60
61 my $total_occurrences =
62 $node1_occurrences + $node2_occurrences + $node3_occurrences;
63
64 ok($node1_occurrences > 1, "received at least one connection on node1");
65 ok($node2_occurrences > 1, "received at least one connection on node2");
66 ok($node3_occurrences > 1, "received at least one connection on node3");
67 ok($total_occurrences == 50, "received 50 connections across all nodes");
68
69 $node1->stop();
70 $node2->stop();
71
72 # load_balance_hosts=disable should continue trying hosts until it finds a
73 # working one.
74 $node3->connect_ok(
75 "host=$hostlist port=$portlist load_balance_hosts=disable",
76 "load_balance_hosts=disable continues until it connects to the a working node",
77 sql => "SELECT 'connect3'",
78 log_like => [qr/statement: SELECT 'connect3'/]);
79
80 # Also with load_balance_hosts=random we continue to the next nodes if previous
81 # ones are down. Connect a few times to make sure it's not just lucky.
82 foreach my $i (1 .. 5)
83 {
84 $node3->connect_ok(
85 "host=$hostlist port=$portlist load_balance_hosts=random",
86 "load_balance_hosts=random continues until it connects to the a working node",
87 sql => "SELECT 'connect4'",
88 log_like => [qr/statement: SELECT 'connect4'/]);
89 }
90
91 done_testing();