Debian Codegraph @

src/interfaces/libpq/t/004_load_balance_dns.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 if (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\bload_balance\b/)
10 {
11 plan skip_all =>
12 'Potentially unsafe test load_balance not enabled in PG_TEST_EXTRA';
13 }
14
15 # This tests loadbalancing based on a DNS entry that contains multiple records
16 # for different IPs. Since setting up a DNS server is more effort than we
17 # consider reasonable to run this test, this situation is instead imitated by
18 # using a hosts file where a single hostname maps to multiple different IP
19 # addresses. This test requires the administrator to add the following lines to
20 # the hosts file (if we detect that this hasn't happened we skip the test):
21 #
22 # 127.0.0.1 pg-loadbalancetest
23 # 127.0.0.2 pg-loadbalancetest
24 # 127.0.0.3 pg-loadbalancetest
25 #
26 # Windows or Linux are required to run this test because these OSes allow
27 # binding to 127.0.0.2 and 127.0.0.3 addresses by default, but other OSes
28 # don't. We need to bind to different IP addresses, so that we can use these
29 # different IP addresses in the hosts file.
30 #
31 # The hosts file needs to be prepared before running this test. We don't do it
32 # on the fly, because it requires root permissions to change the hosts file. In
33 # CI we set up the previously mentioned rules in the hosts file, so that this
34 # load balancing method is tested.
35
36 # Cluster setup which is shared for testing both load balancing methods
37 my $can_bind_to_127_0_0_2 =
38 $Config{osname} eq 'linux' || $PostgreSQL::Test::Utils::windows_os;
39
40 # Checks for the requirements for testing load balancing method 2
41 if (!$can_bind_to_127_0_0_2)
42 {
43 plan skip_all => 'load_balance test only supported on Linux and Windows';
44 }
45
46 my $hosts_path;
47 if ($windows_os)
48 {
49 $hosts_path = 'c:\Windows\System32\Drivers\etc\hosts';
50 }
51 else
52 {
53 $hosts_path = '/etc/hosts';
54 }
55
56 my $hosts_content = PostgreSQL::Test::Utils::slurp_file($hosts_path);
57
58 my $hosts_count = () =
59 $hosts_content =~ /127\.0\.0\.[1-3] pg-loadbalancetest/g;
60 if ($hosts_count != 3)
61 {
62 # Host file is not prepared for this test
63 plan skip_all => "hosts file was not prepared for DNS load balance test";
64 }
65
66 $PostgreSQL::Test::Cluster::use_tcp = 1;
67 $PostgreSQL::Test::Cluster::test_pghost = '127.0.0.1';
68 my $port = PostgreSQL::Test::Cluster::get_free_port();
69 my $node1 = PostgreSQL::Test::Cluster->new('node1', port => $port);
70 my $node2 =
71 PostgreSQL::Test::Cluster->new('node2', port => $port, own_host => 1);
72 my $node3 =
73 PostgreSQL::Test::Cluster->new('node3', port => $port, own_host => 1);
74
75 # Create a data directory with initdb
76 $node1->init();
77 $node2->init();
78 $node3->init();
79
80 # Start the PostgreSQL server
81 $node1->start();
82 $node2->start();
83 $node3->start();
84
85 # load_balance_hosts=disable should always choose the first one.
86 $node1->connect_ok(
87 "host=pg-loadbalancetest port=$port load_balance_hosts=disable",
88 "load_balance_hosts=disable connects to the first node",
89 sql => "SELECT 'connect1'",
90 log_like => [qr/statement: SELECT 'connect1'/]);
91
92
93 # Statistically the following loop with load_balance_hosts=random will almost
94 # certainly connect at least once to each of the nodes. The chance of that not
95 # happening is so small that it's negligible: (2/3)^50 = 1.56832855e-9
96 foreach my $i (1 .. 50)
97 {
98 $node1->connect_ok(
99 "host=pg-loadbalancetest port=$port load_balance_hosts=random",
100 "repeated connections with random load balancing",
101 sql => "SELECT 'connect2'");
102 }
103
104 my $node1_occurrences = () =
105 $node1->log_content() =~ /statement: SELECT 'connect2'/g;
106 my $node2_occurrences = () =
107 $node2->log_content() =~ /statement: SELECT 'connect2'/g;
108 my $node3_occurrences = () =
109 $node3->log_content() =~ /statement: SELECT 'connect2'/g;
110
111 my $total_occurrences =
112 $node1_occurrences + $node2_occurrences + $node3_occurrences;
113
114 ok($node1_occurrences > 1, "received at least one connection on node1");
115 ok($node2_occurrences > 1, "received at least one connection on node2");
116 ok($node3_occurrences > 1, "received at least one connection on node3");
117 ok($total_occurrences == 50, "received 50 connections across all nodes");
118
119 $node1->stop();
120 $node2->stop();
121
122 # load_balance_hosts=disable should continue trying hosts until it finds a
123 # working one.
124 $node3->connect_ok(
125 "host=pg-loadbalancetest port=$port load_balance_hosts=disable",
126 "load_balance_hosts=disable continues until it connects to the a working node",
127 sql => "SELECT 'connect3'",
128 log_like => [qr/statement: SELECT 'connect3'/]);
129
130 # Also with load_balance_hosts=random we continue to the next nodes if previous
131 # ones are down. Connect a few times to make sure it's not just lucky.
132 foreach my $i (1 .. 5)
133 {
134 $node3->connect_ok(
135 "host=pg-loadbalancetest port=$port load_balance_hosts=random",
136 "load_balance_hosts=random continues until it connects to the a working node",
137 sql => "SELECT 'connect4'",
138 log_like => [qr/statement: SELECT 'connect4'/]);
139 }
140
141 done_testing();