PostgreSQL Source Code git master
Loading...
Searching...
No Matches
basebackup_throttle.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * basebackup_throttle.c
4 * Basebackup sink implementing throttling. Data is forwarded to the
5 * next base backup sink in the chain at a rate no greater than the
6 * configured maximum.
7 *
8 * Portions Copyright (c) 2010-2026, PostgreSQL Global Development Group
9 *
10 * IDENTIFICATION
11 * src/backend/backup/basebackup_throttle.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
18#include "miscadmin.h"
19#include "pgstat.h"
20#include "storage/latch.h"
21#include "utils/timestamp.h"
22#include "utils/wait_event.h"
23
24typedef struct bbsink_throttle
25{
26 /* Common information for all types of sink. */
28
29 /* The actual number of bytes, transfer of which may cause sleep. */
31
32 /* Amount of data already transferred but not yet throttled. */
34
35 /* The minimum time required to transfer throttling_sample bytes. */
37
38 /* The last check of the transfer rate. */
41
45static void throttle(bbsink_throttle *sink, size_t increment);
46
49 .begin_archive = bbsink_forward_begin_archive,
50 .archive_contents = bbsink_throttle_archive_contents,
51 .end_archive = bbsink_forward_end_archive,
52 .begin_manifest = bbsink_forward_begin_manifest,
53 .manifest_contents = bbsink_throttle_manifest_contents,
54 .end_manifest = bbsink_forward_end_manifest,
55 .end_backup = bbsink_forward_end_backup,
56 .cleanup = bbsink_forward_cleanup
57};
58
59/*
60 * How frequently to throttle, as a fraction of the specified rate-second.
61 */
62#define THROTTLING_FREQUENCY 8
63
64/*
65 * Create a new basebackup sink that performs throttling and forwards data
66 * to a successor sink.
67 */
68bbsink *
70{
72
73 Assert(next != NULL);
74 Assert(maxrate > 0);
75
77 *((const bbsink_ops **) &sink->base.bbs_ops) = &bbsink_throttle_ops;
78 sink->base.bbs_next = next;
79
80 sink->throttling_sample =
82
83 /*
84 * The minimum amount of time for throttling_sample bytes to be
85 * transferred.
86 */
87 sink->elapsed_min_unit = USECS_PER_SEC / THROTTLING_FREQUENCY;
88
89 return &sink->base;
90}
91
92/*
93 * There's no real work to do here, but we need to record the current time so
94 * that it can be used for future calculations.
95 */
96static void
98{
100
102
103 /* The 'real data' starts now (header was ignored). */
104 mysink->throttled_last = GetCurrentTimestamp();
105}
106
107/*
108 * First throttle, and then pass archive contents to next sink.
109 */
110static void
117
118/*
119 * First throttle, and then pass manifest contents to next sink.
120 */
121static void
128
129/*
130 * Increment the network transfer counter by the given number of bytes,
131 * and sleep if necessary to comply with the requested network transfer
132 * rate.
133 */
134static void
135throttle(bbsink_throttle *sink, size_t increment)
136{
138
139 Assert(sink->throttling_counter >= 0);
140
141 sink->throttling_counter += increment;
142 if (sink->throttling_counter < sink->throttling_sample)
143 return;
144
145 /* How much time should have elapsed at minimum? */
146 elapsed_min = sink->elapsed_min_unit *
147 (sink->throttling_counter / sink->throttling_sample);
148
149 /*
150 * Since the latch could be set repeatedly because of concurrently WAL
151 * activity, sleep in a loop to ensure enough time has passed.
152 */
153 for (;;)
154 {
156 sleep;
157 int wait_result;
158
159 /* Time elapsed since the last measurement (and possible wake up). */
160 elapsed = GetCurrentTimestamp() - sink->throttled_last;
161
162 /* sleep if the transfer is faster than it should be */
164 if (sleep <= 0)
165 break;
166
168
169 /* We're eating a potentially set latch, so check for interrupts */
171
172 /*
173 * (TAR_SEND_SIZE / throttling_sample * elapsed_min_unit) should be
174 * the maximum time to sleep. Thus the cast to long is safe.
175 */
178 (long) (sleep / 1000),
180
183
184 /* Done waiting? */
186 break;
187 }
188
189 /*
190 * As we work with integers, only whole multiple of throttling_sample was
191 * processed. The rest will be done during the next call of this function.
192 */
193 sink->throttling_counter %= sink->throttling_sample;
194
195 /*
196 * Time interval for the remaining amount and possible next increments
197 * starts now.
198 */
199 sink->throttled_last = GetCurrentTimestamp();
200}
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1636
void bbsink_forward_begin_backup(bbsink *sink)
void bbsink_forward_begin_manifest(bbsink *sink)
void bbsink_forward_end_backup(bbsink *sink, XLogRecPtr endptr, TimeLineID endtli)
void bbsink_forward_cleanup(bbsink *sink)
void bbsink_forward_manifest_contents(bbsink *sink, size_t len)
void bbsink_forward_end_archive(bbsink *sink)
void bbsink_forward_archive_contents(bbsink *sink, size_t len)
void bbsink_forward_begin_archive(bbsink *sink, const char *archive_name)
void bbsink_forward_end_manifest(bbsink *sink)
static void bbsink_throttle_archive_contents(bbsink *sink, size_t len)
static void throttle(bbsink_throttle *sink, size_t increment)
bbsink * bbsink_throttle_new(bbsink *next, uint32 maxrate)
static void bbsink_throttle_begin_backup(bbsink *sink)
#define THROTTLING_FREQUENCY
static void bbsink_throttle_manifest_contents(bbsink *sink, size_t len)
static const bbsink_ops bbsink_throttle_ops
static int32 next
Definition blutils.c:225
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
uint64_t uint64
Definition c.h:619
uint32_t uint32
Definition c.h:618
int64 TimestampTz
Definition timestamp.h:39
#define USECS_PER_SEC
Definition timestamp.h:134
int64 TimeOffset
Definition timestamp.h:40
#define palloc0_object(type)
Definition fe_memutils.h:75
struct Latch * MyLatch
Definition globals.c:63
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static int32 maxrate
const void size_t len
static int fb(int x)
void(* begin_backup)(bbsink *sink)
TimestampTz throttled_last
TimeOffset elapsed_min_unit
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET