cdbfts.c 6.5 KB
Newer Older
1 2 3 4 5
/*-------------------------------------------------------------------------
 *
 * cdbfts.c
 *	  Provides fault tolerance service routines for mpp.
 *
6 7 8 9 10 11
 * Portions Copyright (c) 2003-2008, Greenplum inc
 * Portions Copyright (c) 2012-Present Pivotal Software, Inc.
 *
 *
 * IDENTIFICATION
 *	    src/backend/cdb/cdbfts.c
12 13 14 15 16 17 18
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "miscadmin.h"
19 20
#include "libpq-fe.h"
#include "libpq-int.h"
21 22 23 24
#include "utils/memutils.h"
#include "cdb/cdbvars.h"
#include "cdb/cdbconn.h"
#include "cdb/cdbutil.h"
25
#include "cdb/cdbdisp_query.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "access/xact.h"
#include "cdb/cdbfts.h"
#include "cdb/cdbtm.h"
#include "libpq/libpq-be.h"
#include "commands/dbcommands.h"
#include "storage/proc.h"

#include "executor/spi.h"

#include "postmaster/fts.h"
#include "utils/faultinjection.h"

#include "utils/fmgroids.h"
#include "catalog/pg_authid.h"

/* segment id for the master */
#define MASTER_SEGMENT_ID -1

A
Ashwin Agrawal 已提交
44 45 46
FtsProbeInfo *ftsProbeInfo = NULL;	/* Probe process updates this structure */
volatile bool *ftsShutdownMaster;
static LWLockId ftsControlLock;
47

A
Ashwin Agrawal 已提交
48 49
static volatile bool *ftsReadOnlyFlag;
static volatile bool *ftsAdminRequestedRO;
50

A
Ashwin Agrawal 已提交
51 52
static bool local_fts_status_initialized = false;
static uint64 local_fts_statusVersion;
53 54 55 56 57 58 59 60

/*
 * get fts share memory size
 */
int
FtsShmemSize(void)
{
	/*
A
Ashwin Agrawal 已提交
61
	 * this shared memory block doesn't even need to *exist* on the QEs!
62 63 64 65 66 67 68 69 70 71 72 73 74
	 */
	if ((Gp_role != GP_ROLE_DISPATCH) && (Gp_role != GP_ROLE_UTILITY))
		return 0;

	return MAXALIGN(sizeof(FtsControlBlock));
}

void
FtsShmemInit(void)
{
	bool		found;
	FtsControlBlock *shared;

A
Ashwin Agrawal 已提交
75
	shared = (FtsControlBlock *) ShmemInitStruct("Fault Tolerance manager", FtsShmemSize(), &found);
76 77 78 79 80 81 82 83 84 85
	if (!shared)
		elog(FATAL, "FTS: could not initialize fault tolerance manager share memory");

	/* Initialize locks and shared memory area */

	ftsShutdownMaster = &shared->ftsShutdownMaster;
	ftsControlLock = shared->ControlLock;

	ftsReadOnlyFlag = &shared->ftsReadOnlyFlag; /* global RO state */

A
Ashwin Agrawal 已提交
86 87 88
	ftsAdminRequestedRO = &shared->ftsAdminRequestedRO; /* Admin request --
														 * guc-controlled RO
														 * state */
89 90 91 92 93 94 95 96 97 98 99 100 101 102

	ftsProbeInfo = &shared->fts_probe_info;

	if (!IsUnderPostmaster)
	{
		shared->ControlLock = LWLockAssign();
		ftsControlLock = shared->ControlLock;

		/* initialize */
		shared->ftsReadOnlyFlag = gp_set_read_only;
		shared->ftsAdminRequestedRO = gp_set_read_only;

		shared->fts_probe_info.fts_probePid = 0;
		shared->fts_probe_info.fts_statusVersion = 0;
103
		shared->fts_probe_info.fts_status_initialized = false;
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

		shared->ftsShutdownMaster = false;
	}
}

void
ftsLock(void)
{
	LWLockAcquire(ftsControlLock, LW_EXCLUSIVE);
}

void
ftsUnlock(void)
{
	LWLockRelease(ftsControlLock);
}

void
FtsNotifyProber(void)
{
124 125 126 127
	Assert(Gp_role == GP_ROLE_DISPATCH);

	if (ftsProbeInfo->fts_probePid == 0)
		return;
A
Ashwin Agrawal 已提交
128

129
	/*
A
Ashwin Agrawal 已提交
130 131 132
	 * This is a full-scan request. We set the request-flag == to the bitmap
	 * version flag. When the version has been bumped, we know that the
	 * request has been filled.
133 134 135 136
	 */
	ftsProbeInfo->fts_probeScanRequested = ftsProbeInfo->fts_statusVersion;

	/* signal fts-probe */
137
	kill(ftsProbeInfo->fts_probePid, SIGINT);
138 139 140 141

	/* sit and spin */
	while (ftsProbeInfo->fts_probeScanRequested == ftsProbeInfo->fts_statusVersion)
	{
142
		pg_usleep(50000);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

		CHECK_FOR_INTERRUPTS();
	}
}

/*
 * Test-Connection: This is called from the threaded context inside the
 * dispatcher: ONLY CALL THREADSAFE FUNCTIONS -- elog() is NOT threadsafe.
 */
bool
FtsTestConnection(CdbComponentDatabaseInfo *failedDBInfo, bool fullScan)
{
	/* master is always reported as alive */
	if (failedDBInfo->segindex == MASTER_SEGMENT_ID)
	{
		return true;
	}

	if (!fullScan)
	{
163 164 165 166 167 168 169 170 171
		/*
		 * if fullscan not requested, caller is just trying to optimize on
		 * cached version but if we haven't populated yet the fts_status, we
		 * don't have one and hence just return positively back. This is
		 * mainly to avoid queries incorrectly failing just after QD restarts
		 * if FTS process is yet to start and complete initializing the
		 * fts_status. We shouldn't be checking against uninitialzed variable.
		 */
		if (ftsProbeInfo->fts_status_initialized)
172
			return FTS_STATUS_IS_UP(ftsProbeInfo->fts_status[failedDBInfo->dbid]);
173 174

		return true;
175 176 177 178
	}

	FtsNotifyProber();

179 180
	Assert(ftsProbeInfo->fts_status_initialized);

181
	return FTS_STATUS_IS_UP(ftsProbeInfo->fts_status[failedDBInfo->dbid]);
182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

/*
 * Re-Configure the system: if someone has noticed that the status
 * version has been updated, they call this to verify that they've got
 * the right configuration.
 *
 * NOTE: This *always* destroys gangs. And also attempts to inform the
 * fault-prober to do a full scan.
 */
void
FtsReConfigureMPP(bool create_new_gangs)
{
	/* need to scan to pick up the latest view */
G
Gang Xiong 已提交
196
	FtsNotifyProber();
197 198 199
	local_fts_statusVersion = ftsProbeInfo->fts_statusVersion;

	ereport(LOG, (errmsg_internal("FTS: reconfiguration is in progress"),
A
Ashwin Agrawal 已提交
200
				  errSendAlert(true)));
201
	DisconnectAndDestroyAllGangs(true);
202 203 204 205 206 207

	/* Caller should throw an error. */
	return;
}

void
A
Ashwin Agrawal 已提交
208
FtsHandleNetFailure(SegmentDatabaseDescriptor **segDB, int numOfFailed)
209 210 211 212 213 214
{
	elog(LOG, "FtsHandleNetFailure: numOfFailed %d", numOfFailed);

	FtsReConfigureMPP(true);

	ereport(ERROR, (errmsg_internal("MPP detected %d segment failures, system is reconnected", numOfFailed),
A
Ashwin Agrawal 已提交
215
					errSendAlert(true)));
216 217 218
}

/*
G
Gang Xiong 已提交
219 220 221
 * Check if any segment DB is down.
 *
 * returns true if any segment DB is down.
222 223
 */
bool
A
Ashwin Agrawal 已提交
224
FtsTestSegmentDBIsDown(SegmentDatabaseDescriptor *segdbDesc, int size)
225
{
A
Ashwin Agrawal 已提交
226 227
	int			i = 0;
	bool		forceRescan = true;
228 229 230 231 232

	for (i = 0; i < size; i++)
	{
		CdbComponentDatabaseInfo *segInfo = segdbDesc[i].segment_database_info;

G
Gang Xiong 已提交
233 234 235
		elog(DEBUG2, "FtsTestSegmentDBIsDown: looking for real fault on segment dbid %d", segInfo->dbid);

		if (!FtsTestConnection(segInfo, forceRescan))
236
		{
G
Gang Xiong 已提交
237
			ereport(LOG, (errmsg_internal("FTS: found fault with segment dbid %d. "
A
Ashwin Agrawal 已提交
238
										  "Reconfiguration is in progress", segInfo->dbid)));
G
Gang Xiong 已提交
239
			return true;
240 241
		}

G
Gang Xiong 已提交
242 243
		/* only force the rescan on the first call. */
		forceRescan = false;
244 245 246 247 248
	}

	return false;
}

G
Gang Xiong 已提交
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
void
FtsCondSetTxnReadOnly(bool *XactFlag)
{
	if (*ftsReadOnlyFlag && Gp_role != GP_ROLE_UTILITY)
		*XactFlag = true;
}

bool
verifyFtsSyncCount(void)
{
	if (ftsProbeInfo->fts_probePid == 0)
		return true;

	if (!local_fts_status_initialized)
	{
		local_fts_status_initialized = true;
		local_fts_statusVersion = ftsProbeInfo->fts_statusVersion;

		return true;
	}

	return (local_fts_statusVersion == ftsProbeInfo->fts_statusVersion);
}

bool
isFtsReadOnlySet(void)
{
	return *ftsReadOnlyFlag;
}
G
Gang Xiong 已提交
279

A
Ashwin Agrawal 已提交
280 281
uint64
getFtsVersion(void)
G
Gang Xiong 已提交
282 283 284
{
	return ftsProbeInfo->fts_statusVersion;
}