Skip to content

Commit

Permalink
Close connection to PXF when query to external table is canceled (#42)
Browse files Browse the repository at this point in the history
The C-part of the PXF releases the context (cleanup_context) only on
the last call in the pxfprotocol_export and pxfprotocol_import functions.
That is, on errors, the context is not released, including
curl-connections to the Java-part are not closed.
Therefore, I added a callback to release resources on errors, which
releases the context, including closing curl-connections.
  • Loading branch information
RekGRpth authored Jul 10, 2023
1 parent bd6a7fc commit 21efde5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion external-table/src/pxfbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ gpbridge_cleanup(gphadoop_context *context)
if (context == NULL)
return;

churl_cleanup(context->churl_handle, false);
churl_cleanup(context->churl_handle, context->after_error);
context->churl_handle = NULL;

churl_headers_cleanup(context->churl_headers);
Expand Down
1 change: 1 addition & 0 deletions external-table/src/pxfbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef struct
ProjectionInfo *proj_info;
List *quals;
bool completed;
bool after_error;
} gphadoop_context;

/*
Expand Down
19 changes: 19 additions & 0 deletions external-table/src/pxfprotocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "access/fileam.h"
#endif
#include "utils/elog.h"
#include "utils/resowner.h"

/* define magic module unless run as a part of test cases */
#ifndef UNIT_TESTING
Expand Down Expand Up @@ -154,6 +155,21 @@ pxfprotocol_import(PG_FUNCTION_ARGS)
PG_RETURN_INT32(bytes_read);
}

static void
url_curl_abort_callback(ResourceReleasePhase phase,
bool isCommit,
bool isTopLevel,
void *arg)
{
gphadoop_context *context = arg;

if (phase != RESOURCE_RELEASE_AFTER_LOCKS || isCommit || !isTopLevel)
return;

context->after_error = true;
cleanup_context(context);
}

/*
* Allocates context and sets values for the segment
*/
Expand Down Expand Up @@ -190,6 +206,8 @@ create_context(PG_FUNCTION_ARGS, bool is_import)
context->proj_info = proj_info;
context->quals = filter_quals;
context->completed = false;
context->after_error = false;
RegisterResourceReleaseCallback(url_curl_abort_callback, context);
return context;
}

Expand All @@ -201,6 +219,7 @@ cleanup_context(gphadoop_context *context)
{
if (context != NULL)
{
UnregisterResourceReleaseCallback(url_curl_abort_callback, context);
gpbridge_cleanup(context);
pfree(context->uri.data);
pfree(context);
Expand Down

0 comments on commit 21efde5

Please sign in to comment.