-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: support dispatch extra data
Extending the query dispatch mechanism, allowing extra data to be dispatched. To dispatch extra data, you just need to register an extra dispatch with a unique name and a pair of functions for generating and parsing the data to be dispatched.
- Loading branch information
1 parent
af54957
commit d0960ba
Showing
5 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#include "postgres.h" | ||
|
||
#include "cdb/cdbdisp_extra.h" | ||
#include "libpq/pqformat.h" | ||
#include "utils/hsearch.h" | ||
|
||
|
||
static HTAB *ExtraDispTable = NULL; | ||
|
||
typedef struct ExtraDispEntry | ||
{ | ||
char extraDispName[EXTRADISPNAME_MAX_LEN]; | ||
PackFunc packFunc; | ||
UnpackFunc unpackFunc; | ||
} ExtraDispEntry; | ||
|
||
void | ||
RegisterExtraDispatch(const char *extraDispName, PackFunc packFunc, UnpackFunc unpackFunc) | ||
{ | ||
ExtraDispEntry *entry; | ||
bool found; | ||
|
||
if (ExtraDispTable == NULL) | ||
{ | ||
HASHCTL ctl; | ||
|
||
ctl.keysize = EXTRADISPNAME_MAX_LEN; | ||
ctl.entrysize = sizeof(ExtraDispEntry); | ||
|
||
ExtraDispTable = hash_create("extra dispatch info", 8, &ctl, | ||
HASH_ELEM | HASH_STRINGS); | ||
} | ||
|
||
if (strlen(extraDispName) >= EXTRADISPNAME_MAX_LEN) | ||
elog(ERROR, "extra dispatch name is too long"); | ||
|
||
entry = (ExtraDispEntry *) hash_search(ExtraDispTable, | ||
extraDispName, | ||
HASH_ENTER, &found); | ||
if (found) | ||
ereport(ERROR, | ||
(errcode(ERRCODE_DUPLICATE_OBJECT), | ||
errmsg("extra dispatch name \"%s\" already exists", | ||
extraDispName))); | ||
|
||
entry->packFunc = packFunc; | ||
entry->unpackFunc = unpackFunc; | ||
} | ||
|
||
/* Return packaged messages. each message has the same format: | ||
* ("%d%s\0%s", totalLen, name, payload). totalLen is the message | ||
* length not including itself. name is the name of this message, | ||
* a following '\0' marks the end. payload is the main body of the | ||
* message. | ||
*/ | ||
char * | ||
PackExtraMsgs(int *len) | ||
{ | ||
HASH_SEQ_STATUS status; | ||
ExtraDispEntry *hentry; | ||
char **payloads; | ||
int *lengths; | ||
int payloadLen; | ||
char **names; | ||
int nameLen; | ||
char *total; | ||
int totalLen; | ||
char *pos; | ||
int tmp; | ||
int n; | ||
int i; | ||
|
||
if (!ExtraDispTable) | ||
{ | ||
*len = 0; | ||
return NULL; | ||
} | ||
|
||
n = hash_get_num_entries(ExtraDispTable); | ||
payloads = (char **) palloc(n * sizeof(char *)); | ||
lengths = (int *) palloc(n * sizeof(int)); | ||
names = (char **) palloc(n * sizeof(char *)); | ||
|
||
i = 0; | ||
totalLen = 0; | ||
hash_seq_init(&status, ExtraDispTable); | ||
while ((hentry = (ExtraDispEntry *) hash_seq_search(&status)) != NULL) | ||
{ | ||
payloads[i] = (*(hentry->packFunc))(lengths + i); | ||
names[i] = hentry->extraDispName; | ||
totalLen += sizeof(int) + strlen(names[i]) + 1 + *(lengths + i); | ||
i++; | ||
} | ||
Assert(i = n); | ||
|
||
total = palloc(totalLen); | ||
pos = total; | ||
|
||
for(i=0; i < n; i++) | ||
{ | ||
payloadLen = *(lengths + i); | ||
nameLen = strlen(names[i]); | ||
|
||
/* lenth */ | ||
tmp = htonl(payloadLen + nameLen + 1); | ||
memcpy(pos, &tmp, sizeof(tmp)); | ||
pos += sizeof(tmp); | ||
|
||
/* name */ | ||
memcpy(pos, names[i], nameLen + 1); | ||
pos += nameLen + 1; | ||
|
||
/* payload */ | ||
memcpy(pos, payloads[i], payloadLen); | ||
pos += payloadLen; | ||
|
||
pfree(payloads[i]); | ||
} | ||
|
||
Assert(pos - total == totalLen); | ||
|
||
pfree(names); | ||
pfree(payloads); | ||
pfree(lengths); | ||
|
||
*len = totalLen; | ||
return total; | ||
} | ||
|
||
void | ||
UnPackExtraMsgs(StringInfo inputMsgs) | ||
{ | ||
ExtraDispEntry *entry; | ||
const char *name; | ||
const char *payload; | ||
int payloadLen; | ||
int totalLen; | ||
bool found; | ||
int n; | ||
int i; | ||
|
||
if (!ExtraDispTable) | ||
return; | ||
|
||
n = hash_get_num_entries(ExtraDispTable); | ||
i = n; | ||
|
||
while (inputMsgs->cursor < inputMsgs->len) | ||
{ | ||
totalLen = pq_getmsgint(inputMsgs, 4); | ||
name = pq_getmsgstring(inputMsgs); | ||
payloadLen = totalLen - strlen(name) - 1; | ||
payload = pq_getmsgbytes(inputMsgs, payloadLen); | ||
|
||
entry = (ExtraDispEntry *) hash_search(ExtraDispTable, | ||
name, | ||
HASH_FIND, &found); | ||
if (!found) | ||
ereport(ERROR, | ||
(errcode(ERRCODE_PROTOCOL_VIOLATION), | ||
errmsg("extra dispatch %s not found", name))); | ||
|
||
(*(entry->unpackFunc))(payload, payloadLen); | ||
i--; | ||
} | ||
if (i != 0) | ||
ereport(ERROR, | ||
(errcode(ERRCODE_PROTOCOL_VIOLATION), | ||
errmsg("extra dispatch count mismatch, registered %d, get %d", n, i))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef CDBDISP_EXTRA_H | ||
#define CDBDISP_EXTRA_H | ||
|
||
#include "lib/stringinfo.h" | ||
|
||
#define EXTRADISPNAME_MAX_LEN 64 | ||
|
||
typedef char *(*PackFunc) (int *len); | ||
typedef void (*UnpackFunc) (const char *msg, int len); | ||
|
||
extern void RegisterExtraDispatch(const char *extraDispName, PackFunc packFunc, UnpackFunc unpackFunc); | ||
extern char *PackExtraMsgs(int *len); | ||
extern void UnPackExtraMsgs(StringInfo strInfo); | ||
|
||
#endif /* CDBDISP_EXTRA_H */ |