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
|
/*-------------------------------------------------------------------------
*
* pglogical_manager.c
* pglogical worker for managing apply workers in a database
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglogical_manager.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "miscadmin.h"
#include "access/xact.h"
#include "commands/dbcommands.h"
#include "commands/extension.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
#include "utils/timestamp.h"
#include "pgstat.h"
#include "pglogical_node.h"
#include "pglogical_worker.h"
#include "pglogical.h"
#define INITIAL_SLEEP 10000L
#define MAX_SLEEP 180000L
#define MIN_SLEEP 5000L
void PGDLLEXPORT pglogical_manager_main(Datum main_arg);
/*
* Manage the apply workers - start new ones, kill old ones.
*/
static bool
manage_apply_workers(void)
{
PGLogicalLocalNode *node;
List *subscriptions;
List *workers;
List *subs_to_start = NIL;
ListCell *slc,
*wlc;
bool ret = true;
/* Get list of existing workers. */
LWLockAcquire(PGLogicalCtx->lock, LW_EXCLUSIVE);
workers = pglogical_apply_find_all(MyPGLogicalWorker->dboid);
LWLockRelease(PGLogicalCtx->lock);
StartTransactionCommand();
/* Get local node, exit if no found. */
node = get_local_node(true, true);
if (!node)
proc_exit(0);
/* Get list of subscribers. */
subscriptions = get_node_subscriptions(node->node->id, false);
/* Check for active workers for each subscription. */
foreach (slc, subscriptions)
{
PGLogicalSubscription *sub = (PGLogicalSubscription *) lfirst(slc);
PGLogicalWorker *apply = NULL;
#if PG_VERSION_NUM < 130000
ListCell *next;
ListCell *prev = NULL;
#endif
/*
* Skip if subscriber not enabled.
* This must be called before the following search loop because
* we want to kill any workers for disabled subscribers.
*/
if (!sub->enabled)
continue;
/* Check if the subscriber already has registered worker. */
#if PG_VERSION_NUM >= 130000
foreach(wlc, workers)
#else
for (wlc = list_head(workers); wlc; wlc = next)
#endif
{
apply = (PGLogicalWorker *) lfirst(wlc);
#if PG_VERSION_NUM < 130000
/* We might delete the cell so advance it now. */
next = lnext(wlc);
#endif
if (apply->worker.apply.subid == sub->id)
{
#if PG_VERSION_NUM >= 130000
workers = foreach_delete_current(workers, wlc);
#else
workers = list_delete_cell(workers, wlc, prev);
#endif
break;
}
else
{
#if PG_VERSION_NUM < 130000
prev = wlc;
#endif
}
}
/* If the subscriber does not have a registered worker. */
if (!wlc)
apply = NULL;
/* Skip if the worker was alrady registered. */
if (pglogical_worker_running(apply))
continue;
/* Check if this is crashed worker and if we want to restart it now. */
if (apply)
{
if (apply->crashed_at != 0)
{
TimestampTz restart_time;
restart_time = TimestampTzPlusMilliseconds(apply->crashed_at,
MIN_SLEEP);
if (restart_time > GetCurrentTimestamp())
{
ret = false;
continue;
}
}
else
{
ret = false;
continue;
}
}
subs_to_start = lappend(subs_to_start, sub);
}
foreach (slc, subs_to_start)
{
PGLogicalSubscription *sub = (PGLogicalSubscription *) lfirst(slc);
PGLogicalWorker apply;
memset(&apply, 0, sizeof(PGLogicalWorker));
apply.worker_type = PGLOGICAL_WORKER_APPLY;
apply.dboid = MyPGLogicalWorker->dboid;
apply.worker.apply.subid = sub->id;
apply.worker.apply.sync_pending = true;
apply.worker.apply.replay_stop_lsn = InvalidXLogRecPtr;
pglogical_worker_register(&apply);
}
CommitTransactionCommand();
/* Kill any remaining running workers that should not be running. */
LWLockAcquire(PGLogicalCtx->lock, LW_EXCLUSIVE);
foreach (wlc, workers)
{
PGLogicalWorker *worker = (PGLogicalWorker *) lfirst(wlc);
pglogical_worker_kill(worker);
/* Cleanup old info about crashed apply workers. */
if (worker && worker->crashed_at != 0)
{
elog(DEBUG2, "cleaning pglogical worker slot %zu",
(worker - &PGLogicalCtx->workers[0]));
worker->worker_type = PGLOGICAL_WORKER_NONE;
worker->crashed_at = 0;
}
}
LWLockRelease(PGLogicalCtx->lock);
return ret;
}
/*
* Entry point for manager worker.
*/
void
pglogical_manager_main(Datum main_arg)
{
int slot = DatumGetInt32(main_arg);
Oid extoid;
int sleep_timer = INITIAL_SLEEP;
/* Setup shmem. */
pglogical_worker_attach(slot, PGLOGICAL_WORKER_MANAGER);
CurrentResourceOwner = ResourceOwnerCreate(NULL, "pglogical manager");
StartTransactionCommand();
/* If the extension is not installed in this DB, exit. */
extoid = get_extension_oid(EXTENSION_NAME, true);
if (!OidIsValid(extoid))
proc_exit(0);
elog(LOG, "starting pglogical database manager for database %s",
get_database_name(MyDatabaseId));
CommitTransactionCommand();
/* Use separate transaction to avoid lock escalation. */
StartTransactionCommand();
pglogical_manage_extension();
CommitTransactionCommand();
/* Main wait loop. */
while (!got_SIGTERM)
{
int rc;
bool processed_all;
/* Launch the apply workers. */
processed_all = manage_apply_workers();
/* Handle sequences and update our sleep timer as necessary. */
if (synchronize_sequences())
sleep_timer = Min(sleep_timer * 2, MAX_SLEEP);
else
sleep_timer = Max(sleep_timer / 2, MIN_SLEEP);
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
processed_all ? sleep_timer : MIN_SLEEP);
ResetLatch(&MyProc->procLatch);
/* emergency bailout if postmaster has died */
if (rc & WL_POSTMASTER_DEATH)
proc_exit(1);
CHECK_FOR_INTERRUPTS();
}
proc_exit(0);
}
|