-
Notifications
You must be signed in to change notification settings - Fork 212
Support dispatch session state #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.