forked from citusdata/pg_shard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepair_shards.c
211 lines (173 loc) · 6.45 KB
/
repair_shards.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
/*-------------------------------------------------------------------------
*
* repair_shards.c
*
* This file contains functions to repair unhealthy shard placements using data
* from healthy ones.
*
* Copyright (c) 2014, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "c.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "postgres_ext.h"
#include "connection.h"
#include "repair_shards.h"
#include "ddl_commands.h"
#include "distribution_metadata.h"
#include <string.h>
#include "catalog/pg_class.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "storage/lock.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/errcodes.h"
#include "utils/lsyscache.h"
/* local function forward declarations */
static ShardPlacement * SearchShardPlacementInList(List *shardPlacementList,
text *nodeName, int32 nodePort);
static List * RecreateTableDDLCommandList(Oid relationId, int64 shardId);
/* declarations for dynamic loading */
PG_FUNCTION_INFO_V1(master_copy_shard_placement);
PG_FUNCTION_INFO_V1(worker_copy_shard_placement);
/*
* master_copy_shard_placement implements a user-facing UDF to copy data from
* a healthy (source) node to an inactive (target) node. To accomplish this it
* entirely recreates the table structure before copying all data. During this
* time all modifications are paused to the shard. After successful repair, the
* inactive placement is marked healthy and modifications may continue. If the
* repair fails at any point, this function throws an error, leaving the node
* in an unhealthy state.
*/
Datum
master_copy_shard_placement(PG_FUNCTION_ARGS)
{
int64 shardId = PG_GETARG_INT64(0);
text *sourceNodeName = PG_GETARG_TEXT_P(1);
int32 sourceNodePort = PG_GETARG_INT32(2);
text *targetNodeName = PG_GETARG_TEXT_P(3);
int32 targetNodePort = PG_GETARG_INT32(4);
ShardInterval *shardInterval = LoadShardInterval(shardId);
Oid distributedTableId = shardInterval->relationId;
List *shardPlacementList = NIL;
ShardPlacement *sourcePlacement PG_USED_FOR_ASSERTS_ONLY = NULL;
ShardPlacement *targetPlacement = NULL;
List *ddlCommandList = NIL;
bool recreated = false;
/*
* By taking an exclusive lock on the shard, we both stop all modifications
* (INSERT, UPDATE, or DELETE) and prevent concurrent repair operations from
* being able to operate on this shard.
*/
LockShard(shardId, ExclusiveLock);
shardPlacementList = LoadShardPlacementList(shardId);
sourcePlacement = SearchShardPlacementInList(shardPlacementList, sourceNodeName,
sourceNodePort);
targetPlacement = SearchShardPlacementInList(shardPlacementList, targetNodeName,
targetNodePort);
Assert(sourcePlacement->shardState == STATE_FINALIZED);
Assert(targetPlacement->shardState == STATE_INACTIVE);
/* retrieve the DDL commands for the table and run them */
ddlCommandList = RecreateTableDDLCommandList(distributedTableId, shardId);
recreated = ExecuteRemoteCommandList(targetPlacement->nodeName,
targetPlacement->nodePort,
ddlCommandList);
if (!recreated)
{
ereport(ERROR, (errmsg("could not recreate table to receive placement data")));
}
HOLD_INTERRUPTS();
/* TODO: implement row copying functionality */
ereport(ERROR, (errmsg("shard placement repair not fully implemented")));
/* the placement is repaired, so return to finalized state */
DeleteShardPlacementRow(targetPlacement->id);
InsertShardPlacementRow(targetPlacement->id, targetPlacement->shardId,
STATE_FINALIZED, targetPlacement->nodeName,
targetPlacement->nodePort);
RESUME_INTERRUPTS();
PG_RETURN_VOID();
}
/*
* worker_copy_shard_placement is unimplemented but will connect to a source
* shard placement in order to copy all rows from a shard table to an empty
* shard table locally.
*/
Datum
worker_copy_shard_placement(PG_FUNCTION_ARGS __attribute__((unused)))
{
ereport(ERROR, (errmsg("worker_copy_shard_placement not implemented")));
PG_RETURN_VOID();
}
/*
* SearchShardPlacementInList searches a provided list for a shard placement
* with the specified node name and port. This function throws an error if no
* such placement exists in the provided list.
*/
static ShardPlacement *
SearchShardPlacementInList(List *shardPlacementList, text *nodeNameText, int32 nodePort)
{
ListCell *shardPlacementCell = NULL;
ShardPlacement *matchingPlacement = NULL;
char *nodeName = text_to_cstring(nodeNameText);
foreach(shardPlacementCell, shardPlacementList)
{
ShardPlacement *shardPlacement = lfirst(shardPlacementCell);
if (strncmp(nodeName, shardPlacement->nodeName, MAX_NODE_LENGTH) == 0 &&
nodePort == shardPlacement->nodePort)
{
matchingPlacement = shardPlacement;
break;
}
}
if (matchingPlacement == NULL)
{
ereport(ERROR, (errmsg("could not find placement matching %s:%d", nodeName,
nodePort)));
}
return matchingPlacement;
}
/*
* RecreateTableDDLCommandList returns a list of DDL statements similar to that
* returned by ExtendedDDLCommandList except that the list begins with a "DROP
* TABLE" or "DROP FOREIGN TABLE" statement to facilitate total recreation of a
* placement.
*/
static List *
RecreateTableDDLCommandList(Oid relationId, int64 shardId)
{
char *relationName = get_rel_name(relationId);
const char *shardName = NULL;
StringInfo extendedDropCommand = makeStringInfo();
List *createCommandList = NIL;
List *extendedCreateCommandList = NIL;
List *extendedDropCommandList = NIL;
List *extendedRecreateCommandList = NIL;
char relationKind = get_rel_relkind(relationId);
AppendShardIdToName(&relationName, shardId);
shardName = quote_identifier(relationName);
/* build appropriate DROP command based on relation kind */
if (relationKind == RELKIND_RELATION)
{
appendStringInfo(extendedDropCommand, DROP_REGULAR_TABLE_COMMAND, shardName);
}
else if (relationKind == RELKIND_FOREIGN_TABLE)
{
appendStringInfo(extendedDropCommand, DROP_FOREIGN_TABLE_COMMAND, shardName);
}
else
{
ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("repair target is not a regular or foreign table")));
}
extendedDropCommandList = list_make1(extendedDropCommand->data);
createCommandList = TableDDLCommandList(relationId);
extendedCreateCommandList = ExtendedDDLCommandList(relationId, shardId,
createCommandList);
extendedRecreateCommandList = list_union(extendedDropCommandList,
extendedCreateCommandList);
return extendedRecreateCommandList;
}