-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgsampler.c
309 lines (259 loc) · 10.5 KB
/
pgsampler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "pgsampler.h"
/* Essential for shared libs! */
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pgsampler_launch);
PG_FUNCTION_INFO_V1(predict);
/* Signal handling */
volatile sig_atomic_t got_sigterm = false;
volatile sig_atomic_t got_sighup = false;
/* This is the main execution loop for the pgsampler module */
void pgsampler_main(Datum main_arg) {
bool found;
int res;
int cycle = 0;
/* global defaults */
sockfd = 0;
restart_cycle = 0;
pqsignal(SIGTERM, pgsampler_sigterm);
pqsignal(SIGHUP, pgsampler_sighup);
BackgroundWorkerUnblockSignals();
/* Check and set shared memory state for info passing between process cycles */
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
pgsampler_state = ShmemInitStruct("pgsampler_state", sizeof(pgsampler_shared_state), &found);
LWLockRelease(AddinShmemInitLock); //TODO consider putting this later after setting db
if (found) {
if (pgsampler_state->next_db != NULL && strlen(pgsampler_state->next_db) > 0) {
target_db = pstrdup(pgsampler_state->next_db);
}
restart_cycle = ++pgsampler_state->restart_cycle;
if (restart_cycle > 32000) {
pgsampler_state->restart_cycle = 0;
}
} else {
pgsampler_state->restart_cycle = 0; // initialize restart cycle since database was restarted
}
/* Connect to target database */
BackgroundWorkerInitializeConnection(target_db, NULL);
/* Set next target db */
set_next_db_target();
/*
* Ensure network is sane
*/
res = ensure_valid_environment();
if (res == 1) {
elog(LOG, "Failed to find appropriate environment. Shutting down pgsampler monitoring for Safety.");
proc_exit(1);
}
/* Set database count to drive fast restarts */
pgsampler_database_count = get_database_count();
/* Main loop, periodically check if should rebuild models */
while (!got_sigterm) {
int rc;
pgstat_report_activity(STATE_RUNNING, "idling in polling loop");
rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, 1000L);
ResetLatch(&MyProc->procLatch);
if (rc & WL_POSTMASTER_DEATH) {
elog(LOG, "pgsampler terminating!");
proc_exit(1);
}
collect_and_send_metrics(cycle);
cycle++;
if (cycle_db_seconds > 0) { // GUC controlling whether to disconnect and reconnect
if (cycle > cycle_db_seconds || (restart_cycle < pgsampler_database_count) ) {
elog(LOG, "pgsampler restarting to connect to different database: normal exit.");
proc_exit(0); // RESET so new database is connected to.
}
}
}
proc_exit(0);
}
/* Signal Handlers*/
void pgsampler_sigterm(SIGNAL_ARGS) { //TODO send a packet to trigger an alarm if possible!
int save_errno = errno;
got_sigterm = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
errno = save_errno;
}
void pgsampler_sighup(SIGNAL_ARGS) {
got_sighup = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
}
/* This is callback execute when creating the extension. It registers the background worker
In the future may make sense to start the background worker at this point, so as not to require
a server restart.
*/
void _PG_init(void) {
BackgroundWorker worker;
/* Main database to connect to. */
DefineCustomStringVariable("pgsampler.target_db",
"Database to connect to first",
NULL,
&target_db,
"postgres",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomStringVariable("pgsampler.token",
"token used to identify cluster to antenna",
NULL,
&pgsampler_token,
"",
PGC_USERSET,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.cycle_db_seconds",
"Duration between each heartbeat sample.",
NULL,
&cycle_db_seconds,
500,
-1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
/* The mode to run pgsampler in, accepted values include 'csv', 'dbtables', 'network'*/
DefineCustomStringVariable("pgsampler.output_mode",
"The mode to write pgsampler data (csv or network).",
NULL,
&output_mode,
"network",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
/* The hostname to send sample data to for network mode */
DefineCustomStringVariable("pgsampler.output_network_host",
"pgsampler send data to this host.",
NULL,
&output_network_host,
"localhost",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
/* The hostname to send sample data to for network mode */
DefineCustomStringVariable("pgsampler.output_csv_dir",
"pgsampler output directory for csv data.",
NULL,
&output_csv_dir,
"/tmp/",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
//Individual timers
DefineCustomIntVariable("pgsampler.heartbeat_seconds",
"Duration between each heartbeat sample.",
NULL,
&heartbeat_seconds,
5,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.system_seconds",
"Duration between stats for filesystems and memory/cpu",
NULL,
&system_seconds,
10,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.relation_seconds",
"Duration between stats for index/table",
NULL,
&relation_seconds,
150,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.bgwriter_seconds",
"Duration between stats for bgwriter",
NULL,
&bgwriter_seconds,
30,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.guc_seconds",
"Duration between recording gucs",
NULL,
&guc_seconds,
120,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.activity_seconds",
"Duration between sampling pgstatactivity",
NULL,
&activity_seconds,
5,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.replication_seconds",
"Duration between recording replication stats",
NULL,
&replication_seconds,
60,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pgsampler.statements_seconds",
"Duration between recording stat_statements data",
NULL,
&statements_seconds,
86400, //24 hours
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
worker.bgw_main = pgsampler_main;
snprintf(worker.bgw_name, BGW_MAXLEN, "pgsampler");
worker.bgw_restart_time = BGW_NEVER_RESTART;
worker.bgw_main_arg = (Datum) 0;
RegisterBackgroundWorker(&worker);
}