* instead, they may wait on their process latch, which sleeps as
* necessary, but is awakened if postmaster dies. That way the
* background process goes away immediately in an emergency.
+ *
+ * We wake up everytime our latch gets set or if 180 seconds have
+ * passed without events. That's a stopgap for the case a backend
+ * committed sequencer changes but died before setting the latch.
*/
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
- 10000L);
+ 180000L);
ResetLatch(&MyProc->procLatch);
int64 end_value;
} BdrSequenceValues;
+static void bdr_schedule_eoxact_sequencer_wakeup(void);
+
/* cached relids */
Oid BdrSequenceValuesRelid; /* bdr_sequence_values */
Oid BdrSequenceElectionsRelid; /* bdr_sequence_elections */
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
+static bool bdr_seq_pending_wakeup = false;
+
/* vote */
const char* vote_sql =
"INSERT INTO bdr_votes (\n"
}
}
+
+static void
+bdr_sequence_xact_callback(XactEvent event, void *arg)
+{
+ if (event != XACT_EVENT_COMMIT)
+ return;
+
+ if (bdr_seq_pending_wakeup)
+ {
+ bdr_sequencer_wakeup();
+ bdr_seq_pending_wakeup = false;
+ }
+}
+
+/*
+ * Schedule a wakeup of all sequencer workers, as soon as this transaction
+ * commits.
+ *
+ * This is e.g. useful when a new sequnece is created, and the voting process
+ * should start immediately.
+ *
+ * NB: There's a window between the commit and this callback in which this
+ * backend could die without causing a cluster wide restart. So we need to
+ * periodically check whether we've missed wakeups.
+ */
+static void bdr_schedule_eoxact_sequencer_wakeup(void)
+{
+ static bool registered = false;
+
+ if (!registered)
+ {
+ RegisterXactCallback(bdr_sequence_xact_callback, NULL);
+ registered = true;
+ }
+ bdr_seq_pending_wakeup = true;
+}
+
void
bdr_sequencer_init(void)
{
result = elm->last;
END_CRIT_SECTION();
+
+ /* schedule wakeup as soon as other xacts can see the seuqence */
+ bdr_schedule_eoxact_sequencer_wakeup();
}
PG_FUNCTION_INFO_V1(bdr_sequence_setval);
log_sequence_tuple(seqrel, seqtuple, page);
END_CRIT_SECTION();
+
+ /* schedule wakeup as soon as other xacts can see the seuqence */
+ bdr_schedule_eoxact_sequencer_wakeup();
}
PG_FUNCTION_INFO_V1(bdr_sequence_options);
PG_RETURN_BYTEA_P(result);
PG_RETURN_NULL();
+
+ /* schedule wakeup as soon as other xacts can see the seuqence */
+ bdr_schedule_eoxact_sequencer_wakeup();
}