Skip to content

Commit

Permalink
SPC-36: Handle unexpected async responses better (#5)
Browse files Browse the repository at this point in the history
* Update script.csx

- Fix async detection based on response code instead of request params, b/c apparently snowflake API can decide to return an async response if a synchronous response takes too long to return.

* Fix typo in script.csx

"BeginFetch" misspelled

---------

Co-authored-by: Joseph Brinkman <[email protected]>
  • Loading branch information
TobinWritesCode and jbrinkman authored Sep 10, 2024
1 parent 8eeef21 commit 3f2502e
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions certified-connectors/Snowflake v2/script.csx
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public class Script : ScriptBase
HttpResponseMessage response = await Context.SendAsync(Context.Request, CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
if (response.IsSuccessStatusCode)
{
if(IsFullResponseWithData())
if(IsFullResponseWithData(response))
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var converted = ConvertToObjects_FullReponseWithData(responseContent, Context.OperationId, originalContent);

return converted.GetAsResponse();
}
else if(IsAsyncResponse())
else if(IsAsyncResponse(response))
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var converted = ConvertToObjects_AsyncResponse(responseContent, Context.OperationId);
Expand Down Expand Up @@ -154,16 +154,16 @@ public class Script : ScriptBase
return (nullable == "true");
}

private bool IsFullResponseWithData()
private bool IsFullResponseWithData(HttpResponseMessage response)
{
return (Context.OperationId == OP_EXECUTE_SQL || Context.OperationId == OP_GET_RESULTS)
&& GetQueryStringParam(QueryString_Async) != "true";
return (Context.OperationId == OP_EXECUTE_SQL || Context.OperationId == OP_GET_RESULTS) &&
response.StatusCode == HttpStatusCode.OK;
}

private bool IsAsyncResponse()
private bool IsAsyncResponse(HttpResponseMessage response)
{
return Context.OperationId == OP_EXECUTE_SQL &&
GetQueryStringParam(QueryString_Async) == "true";
return (Context.OperationId == OP_EXECUTE_SQL || Context.OperationId == OP_GET_RESULTS) &&
response.StatusCode == HttpStatusCode.Accepted;
}

private ConvertObjectResult ConvertToObjects_AsyncResponse(string content, string operationId)
Expand Down Expand Up @@ -449,6 +449,17 @@ public class Script : ScriptBase
}
}

public class PerformanceData
{
public DateTimeOffset BeginFetch {get;set;}
public DateTimeOffset EndFetch {get;set;}
public int FetchDurationSeconds {get;set;}

public DateTimeOffset? BeginConvert {get;set;}
public DateTimeOffset? EndConvert {get;set;}
public int? ConvertDurationSeconds {get;set;}
}

public class SnowflakeResponseMetadata
{
public long Rows { get; set; }
Expand Down Expand Up @@ -515,4 +526,4 @@ public class Script : ScriptBase
}

#endregion
}
}

0 comments on commit 3f2502e

Please sign in to comment.