Skip to content

Commit 2fc06c7

Browse files
committed
Dispatch read-only query on read-only targets
1 parent 3c4e74a commit 2fc06c7

File tree

8 files changed

+121
-27
lines changed

8 files changed

+121
-27
lines changed

gpAux/gpdemo/demo_cluster.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ if [ "${ENABLE_COPY}" == "true" ]; then
347347
ycmdb.yc_allow_copy_to_program=on
348348
ycmdb.yc_allow_copy_from_file=on
349349
ycmdb.yc_allow_copy_to_file=on
350+
wal_level=hot_standby
351+
hot_standby=on
350352
351353
EOF
352354
fi

src/backend/access/transam/twophase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ GetOldestPreparedTransaction()
18791879
void
18801880
StandbyRecoverPreparedTransactions(bool overwriteOK)
18811881
{
1882-
elog(ERROR, "Hot Standby not supported");
1882+
// elog(ERROR, "Hot Standby not supported");
18831883
}
18841884

18851885
/*

src/backend/access/transam/xlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7200,7 +7200,7 @@ StartupXLOG(void)
72007200
oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
72017201
else
72027202
oldestActiveXID = checkPoint.oldestActiveXid;
7203-
Assert(TransactionIdIsValid(oldestActiveXID));
7203+
// Assert(TransactionIdIsValid(oldestActiveXID));
72047204

72057205
/* Tell procarray about the range of xids it has to deal with */
72067206
ProcArrayInitRecovery(ShmemVariableCache->nextXid);

src/backend/cdb/cdbutil.c

Lines changed: 105 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#endif
8383

8484
bool gp_count_host_segments_using_address = false;
85+
bool gp_dispatch_on_mirrors = false;
8586
MemoryContext CdbComponentsContext = NULL;
8687
static CdbComponentDatabases *cdb_component_dbs = NULL;
8788

@@ -92,6 +93,7 @@ static CdbComponentDatabases *getCdbComponentInfo(void);
9293
static void cleanupComponentIdleQEs(CdbComponentDatabaseInfo *cdi, bool includeWriter);
9394

9495
static int CdbComponentDatabaseInfoCompare(const void *p1, const void *p2);
96+
static int CdbComponentDatabaseInfoComparem(const void *p1, const void *p2);
9597

9698
static GpSegConfigEntry * readGpSegConfigFromCatalog(int *total_dbs);
9799
static GpSegConfigEntry * readGpSegConfigFromFTSFiles(int *total_dbs);
@@ -439,10 +441,20 @@ getCdbComponentInfo(void)
439441
pRow->numIdleQEs = 0;
440442
pRow->numActiveQEs = 0;
441443

442-
if (config->role != GP_SEGMENT_CONFIGURATION_ROLE_PRIMARY ||
443-
(gp_count_host_segments_using_address &&
444-
(config->hostip == NULL || strlen(config->hostip) == 0)))
445-
continue;
444+
if (gp_dispatch_on_mirrors)
445+
{
446+
if (config->role != GP_SEGMENT_CONFIGURATION_ROLE_MIRROR ||
447+
(gp_count_host_segments_using_address &&
448+
(config->hostip == NULL || strlen(config->hostip) == 0)))
449+
continue;
450+
}
451+
else
452+
{
453+
if (config->role != GP_SEGMENT_CONFIGURATION_ROLE_PRIMARY ||
454+
(gp_count_host_segments_using_address &&
455+
(config->hostip == NULL || strlen(config->hostip) == 0)))
456+
continue;
457+
}
446458

447459
hsEntry = (HostSegsEntry *) hash_search(hostSegsHash,
448460
gp_count_host_segments_using_address ? config->hostip : config->hostname,
@@ -474,14 +486,26 @@ getCdbComponentInfo(void)
474486
/*
475487
* Now sort the data by segindex, isprimary desc
476488
*/
477-
qsort(component_databases->segment_db_info,
478-
component_databases->total_segment_dbs, sizeof(CdbComponentDatabaseInfo),
479-
CdbComponentDatabaseInfoCompare);
489+
if (gp_dispatch_on_mirrors)
490+
{
491+
qsort(component_databases->segment_db_info,
492+
component_databases->total_segment_dbs, sizeof(CdbComponentDatabaseInfo),
493+
CdbComponentDatabaseInfoComparem);
480494

481-
qsort(component_databases->entry_db_info,
482-
component_databases->total_entry_dbs, sizeof(CdbComponentDatabaseInfo),
483-
CdbComponentDatabaseInfoCompare);
495+
qsort(component_databases->entry_db_info,
496+
component_databases->total_entry_dbs, sizeof(CdbComponentDatabaseInfo),
497+
CdbComponentDatabaseInfoComparem);
498+
}
499+
else
500+
{
501+
qsort(component_databases->segment_db_info,
502+
component_databases->total_segment_dbs, sizeof(CdbComponentDatabaseInfo),
503+
CdbComponentDatabaseInfoCompare);
484504

505+
qsort(component_databases->entry_db_info,
506+
component_databases->total_entry_dbs, sizeof(CdbComponentDatabaseInfo),
507+
CdbComponentDatabaseInfoCompare);
508+
}
485509
/*
486510
* Now count the number of distinct segindexes. Since it's sorted, this is
487511
* easy.
@@ -558,10 +582,20 @@ getCdbComponentInfo(void)
558582
{
559583
cdbInfo = &component_databases->segment_db_info[i];
560584

561-
if (cdbInfo->config->role != GP_SEGMENT_CONFIGURATION_ROLE_PRIMARY ||
562-
(gp_count_host_segments_using_address &&
563-
(cdbInfo->config->hostip == NULL || strlen(cdbInfo->config->hostip) == 0)))
564-
continue;
585+
if (gp_dispatch_on_mirrors)
586+
{
587+
if (cdbInfo->config->role != GP_SEGMENT_CONFIGURATION_ROLE_MIRROR ||
588+
(gp_count_host_segments_using_address &&
589+
(cdbInfo->config->hostip == NULL || strlen(cdbInfo->config->hostip) == 0)))
590+
continue;
591+
}
592+
else
593+
{
594+
if (cdbInfo->config->role != GP_SEGMENT_CONFIGURATION_ROLE_PRIMARY ||
595+
(gp_count_host_segments_using_address &&
596+
(cdbInfo->config->hostip == NULL || strlen(cdbInfo->config->hostip) == 0)))
597+
continue;
598+
}
565599

566600
hsEntry = (HostSegsEntry *) hash_search(hostSegsHash,
567601
gp_count_host_segments_using_address ? cdbInfo->config->hostip : cdbInfo->config->hostname,
@@ -575,10 +609,20 @@ getCdbComponentInfo(void)
575609
{
576610
cdbInfo = &component_databases->entry_db_info[i];
577611

578-
if (cdbInfo->config->role != GP_SEGMENT_CONFIGURATION_ROLE_PRIMARY ||
579-
(gp_count_host_segments_using_address &&
580-
(cdbInfo->config->hostip == NULL || strlen(cdbInfo->config->hostip) == 0)))
581-
continue;
612+
if (gp_dispatch_on_mirrors)
613+
{
614+
if (cdbInfo->config->role != GP_SEGMENT_CONFIGURATION_ROLE_MIRROR ||
615+
(gp_count_host_segments_using_address &&
616+
(cdbInfo->config->hostip == NULL || strlen(cdbInfo->config->hostip) == 0)))
617+
continue;
618+
}
619+
else
620+
{
621+
if (cdbInfo->config->role != GP_SEGMENT_CONFIGURATION_ROLE_PRIMARY ||
622+
(gp_count_host_segments_using_address &&
623+
(cdbInfo->config->hostip == NULL || strlen(cdbInfo->config->hostip) == 0)))
624+
continue;
625+
}
582626

583627
hsEntry = (HostSegsEntry *) hash_search(hostSegsHash,
584628
gp_count_host_segments_using_address ? cdbInfo->config->hostip : cdbInfo->config->hostname,
@@ -693,6 +737,9 @@ cdbcomponent_updateCdbComponents(void)
693737

694738
PG_TRY();
695739
{
740+
if (gp_dispatch_on_mirrors) {
741+
cdb_component_dbs = NULL;
742+
}
696743
if (cdb_component_dbs == NULL)
697744
{
698745
cdb_component_dbs = getCdbComponentInfo();
@@ -742,7 +789,7 @@ cdbcomponent_getCdbComponents()
742789
{
743790
PG_TRY();
744791
{
745-
if (cdb_component_dbs == NULL)
792+
if (cdb_component_dbs == NULL || gp_dispatch_on_mirrors)
746793
{
747794
cdb_component_dbs = getCdbComponentInfo();
748795
cdb_component_dbs->fts_version = getFtsVersion();
@@ -847,7 +894,9 @@ cdbcomponent_allocateIdleQE(int contentId, SegmentType segmentType)
847894
* 1. for entrydb, it's never be writer.
848895
* 2. for first QE, it must be a writer.
849896
*/
897+
/*XXX: what if gp_dispatch_on_mirrors?*/
850898
isWriter = contentId == -1 ? false: (cdbinfo->numIdleQEs == 0 && cdbinfo->numActiveQEs == 0);
899+
851900
segdbDesc = cdbconn_createSegmentDescriptor(cdbinfo, nextQEIdentifer(cdbinfo->cdbs), isWriter);
852901
}
853902

@@ -1041,11 +1090,20 @@ cdbcomponent_getComponentInfo(int contentId)
10411090
Assert(cdbs->total_segment_dbs == cdbs->total_segments * 2);
10421091
cdbInfo = &cdbs->segment_db_info[2 * contentId];
10431092

1044-
if (!SEGMENT_IS_ACTIVE_PRIMARY(cdbInfo))
1093+
if (gp_dispatch_on_mirrors)
10451094
{
1046-
cdbInfo = &cdbs->segment_db_info[2 * contentId + 1];
1095+
if (!SEGMENT_IS_ACTIVE_MIRROR(cdbInfo))
1096+
{
1097+
cdbInfo = &cdbs->segment_db_info[2 * contentId + 1];
1098+
}
1099+
}
1100+
else
1101+
{
1102+
if (!SEGMENT_IS_ACTIVE_PRIMARY(cdbInfo))
1103+
{
1104+
cdbInfo = &cdbs->segment_db_info[2 * contentId + 1];
1105+
}
10471106
}
1048-
10491107
return cdbInfo;
10501108
}
10511109

@@ -1228,6 +1286,31 @@ CdbComponentDatabaseInfoCompare(const void *p1, const void *p2)
12281286
return cmp;
12291287
}
12301288

1289+
static int
1290+
CdbComponentDatabaseInfoComparem(const void *p1, const void *p2)
1291+
{
1292+
const CdbComponentDatabaseInfo *obj1 = (CdbComponentDatabaseInfo *) p1;
1293+
const CdbComponentDatabaseInfo *obj2 = (CdbComponentDatabaseInfo *) p2;
1294+
1295+
int cmp = obj1->config->segindex - obj2->config->segindex;
1296+
1297+
if (cmp == 0)
1298+
{
1299+
int obj2cmp = 0;
1300+
int obj1cmp = 0;
1301+
1302+
if (SEGMENT_IS_ACTIVE_MIRROR(obj2))
1303+
obj2cmp = 1;
1304+
1305+
if (SEGMENT_IS_ACTIVE_MIRROR(obj1))
1306+
obj1cmp = 1;
1307+
1308+
cmp = obj2cmp - obj1cmp;
1309+
}
1310+
1311+
return cmp;
1312+
}
1313+
12311314
/*
12321315
* Maintain a cache of names.
12331316
*

src/backend/storage/lmgr/lock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "access/twophase.h"
3737
#include "access/twophase_rmgr.h"
3838
#include "cdb/cdbvars.h"
39+
#include "cdb/cdbutil.h"
3940
#include "miscadmin.h"
4041
#include "pg_trace.h"
4142
#include "pgstat.h"
@@ -837,7 +838,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
837838

838839
if (lockmethodid == DEFAULT_LOCKMETHOD && locktag->locktag_type != LOCKTAG_TRANSACTION)
839840
{
840-
if (Gp_role == GP_ROLE_EXECUTE && !Gp_is_writer)
841+
if (Gp_role == GP_ROLE_EXECUTE && !Gp_is_writer && !gp_dispatch_on_mirrors)
841842
{
842843
if (lockHolderProcPtr == NULL || lockHolderProcPtr == MyProc)
843844
{
@@ -857,7 +858,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
857858
lockHolderProcPtr = proc;
858859
}
859860
else
860-
ereport(FATAL,
861+
ereport(WARNING,
861862
(errcode(ERRCODE_GP_INTERCONNECTION_ERROR),
862863
errmsg(WRITER_IS_MISSING_MSG),
863864
errdetail("lock [%u,%u] %s %d. "

src/backend/utils/misc/guc_gp.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3197,7 +3197,13 @@ struct config_bool ConfigureNamesBool_gp[] =
31973197
&gp_use_fastanalyze,
31983198
false, NULL, NULL
31993199
},
3200-
3200+
{
3201+
{"gp_dispatch_on_mirrors", PGC_USERSET, RESOURCES,
3202+
},
3203+
&gp_dispatch_on_mirrors,
3204+
false, NULL, NULL
3205+
},
3206+
32013207
{
32023208
{"stats_queue_level", PGC_SUSET, STATS_COLLECTOR,
32033209
gettext_noop("Collects resource queue-level statistics on database activity."),

src/include/cdb/cdbutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ extern int16 contentid_get_dbid(int16 contentid, char role, bool getPreferredRol
206206
extern int numsegmentsFromQD;
207207

208208
extern bool gp_count_host_segments_using_address;
209+
extern bool gp_dispatch_on_mirrors;
209210

210211
/*
211212
* Returns the number of segments

src/include/utils/unsync_guc_name.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
"gp_dispatch_keepalives_idle",
161161
"gp_dispatch_keepalives_interval",
162162
"gp_dispatch_keepalives_count",
163+
"gp_dispatch_on_mirrors",
163164
"gp_distinct_grouping_sets_threshold",
164165
"gp_dtx_recovery_interval",
165166
"gp_dtx_recovery_prepared_period",

0 commit comments

Comments
 (0)