cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Advocate IV
Advocate IV

SQL stored procedure on-premises data gateway timeout

I found  I can run an SQL stored procedure on the local SQL server and it is very handy when the procedures are short lived. However, when the time the procedure takes to run, exceeds 2 minutes, the Flow seems to do a couple of extra things that I do not understand.

 

When I manually run the SP that I want to launch via Flow, it takes 5 to 7 minutes to run, where it basically pulls data from a number of sources (linked servers) via a local view, and combines them into a local table. This always works as expected, there is no issue with the SP.

 

When I run the SP via Flow it does do the first part of the flow, where it drops the existing table, and then things become unclear. After 2 minutes it signals a timeout, and from then on it seems to hammer the SQL server, since my SQL studio seems to have a hard time getting any response. The flow and the sever then go in limbo for about 30 minutes and seemly do nothing but jamming.

 

The flow is running with my personal credentials, the same as I use in the SQL Studio. 

 

Is there anything I can do to prevent this timeout and retry after 2 minutes?

Is there anything I can do to prevent the explicit cancellation?

Any suggestions to make this work as expected?

 

I did look at and adjusted the timeout settings, but I think the 'Note' tells me it will not work. 

Timeout:  Limit the maximum duration an asynchronous pattern may take.
Note: this does not alter the request timeout of a single request.

 

GatewayTimeout.jpg

 

The error message: 

{
"error": {
"code": 504,
"source": "flow-apim-europe-001-northeurope-01.azure-apim.net",
"clientRequestId": "bba12345-a123-b456-c789-cf64d495e8d1",
"message": "BadGateway",
"innerError": {
"status": 504,
"message": "The operation failed due to an explicit cancellation. Exception: System.Threading.Tasks.TaskCanceledException: A task was canceled.\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.PowerBI.DataMovement.Pipeline.Common.TDFHelpers.<>c__DisplayClass7_0`1.<<GetNextResponse>b__0>d.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.PowerBI.DataMovement.Pipeline.Common.TDFHelpers.<>c__DisplayClass11_0.<<ExecuteBlockOperation>b__0>d.MoveNext()\r\n inner exception: The operation failed due to an explicit cancellation. Exception: System.Threading.Tasks.TaskCanceledException: A task was canceled.\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.PowerBI.DataMovement.Pipeline.Common.TDFHelpers.<>c__DisplayClass7_0`1.<<GetNextResponse>b__0>d.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.PowerBI.DataMovement.Pipeline.Common.TDFHelpers.<>c__DisplayClass11_0.<<ExecuteBlockOperation>b__0>d.MoveNext()\r\n inner exception: A task was canceled.\r\nclientRequestId: bba12345-a123-b456-c789-cf64d495e8d1",
"source": "sql-ne.azconn-ne.p.azurewebsites.net"
}
}
}

 

The stored procedure:

DROP TABLE IF EXISTS [LocalDB].[dbo].[MH_test_dev]

SELECT * INTO [LocalDB].[dbo].[MH_test_dev] FROM [LocalDB].[dbo].[MH_OpenSO_dev] 

  

Thanks for any feedback,

 

Michel

17 REPLIES 17
Highlighted
Resident Rockstar
Resident Rockstar

Re: SQL stored procedure on-premises data gateway timeout

Hi @MichelH

Please see here

 

User is reporting:

  "error": {

 

    "message": "BadGateway",

    "innerError": {

      "status": 504,

      "message": "The operation failed due to an explicit cancellation

Then goes on to say: "We experienced this issue 8 times this morning. Opening a ticket with Microsoft to resolve. Seems like a Gateway code issue, or perhaps it has an issue connecting to SQL during some timeframes (Backups? DB Maint scripts?) Will post solution when Microsoft engages on the issue. We are collection the basic support info this morning on the case."

 

Looks like they are engagning Microsoft, is this of any help to you?

Maybe @SmartMeter can be asked for further assistance or information?

 

If you, the reader has found this post helpful, please help the community by clicking thumbs up.

If this post has solved your problem, please click "Accept as Solution".

Any other questions, just ask.

Thanks, Alan


Did I answer your question? Mark my post as a solution!

Proud to be a Flownaut!


Highlighted
Advocate IV
Advocate IV

Re: SQL stored procedure on-premises data gateway timeout

Thanks for your response @AlanPs1,

 

I did see that post but the issue occurred over a year ago and multiple updates to the gateway software have become available since.

Also I can say that our gateway does work for short-lived jobs.

As long as it doesn't go for the timeout, then all is fine.

 

Nevertheless it can't hurt to request feedback from @SmartMeter regarding his issue, so I just did.

 

Cheers,

Michel

Highlighted
Advocate IV
Advocate IV

Re: SQL stored procedure on-premises data gateway timeout

I'm more and more convinced the source of my problem is the hardcoded timeout (after 2 minutes) of a single request.

There are timeout settings I can use in flow but they don't apply to a single request.

 

timeout2min.jpg

 

Does anyone have an idea how to work arround this?

 

Could I download the flow package file and add something to the json to set this timeout to a longer period?

 

 

Thanks,

 

Michel

Highlighted
New Member

Re: SQL stored procedure on-premises data gateway timeout

Any recommendations on this? I am experiencing the same issue consistently.

Highlighted
Advocate IV
Advocate IV

Re: SQL stored procedure on-premises data gateway timeout

No other than just making sure the SP responds in less than 2 minutes.

 

For all the rest I made a python script of 10 lines that connects to the SQL and runs the procedures that take longer.

Highlighted
Microsoft
Microsoft

Re: SQL stored procedure on-premises data gateway timeout

As others have mentioned, there is a 120 second timeout for SQL Server connector. If you cannot decrease the time taken by the stored procedure, then I suggest using a queueing mechanism on the server. If you are willing to take on some complexity, others have found success using a "fire and forget" mechanism. It assumes that you have access to the SQL server and have permission to create tables, stored procedures, and server-side triggers. The general approach is: 1) Create a stored procedure which performs the desired query (Master) make sure this procedure also accepts an identity value from the status table. 2) Create a second stored procedure (MasterStart) which can accept the same parameters as the first (sans identity). This procedure should add these parameters to a state table (let’s call it RunState) with an additional “status” column which defaults to “pending”. This table must have an IDENTITY column AND a ROWVERSION column. 3) Create a server-side SQL trigger which executes when new rows are inserted to the RunState table. When it discovers a new row with status “pending” it should execute the stored procedure created in step 1 using the parameters in the inserted row in addition to the identity of that row, and alter the status to “running”. The stored procedure in step 1 must update the state table using the provided identity to meaningful values, but most importantly to “complete” when it has finished. 4) Your Flow should then call the stored procedure created in step 2 above (MasterStart). This will return as soon as the state table entry is created in the RunState table. 5) Your second flow step should be a trigger which observes the RunState table for Updates. This step should filter out runs and only execute for rows whose status == “complete”. If desired, you can include a “results” column in the table to communicate output values. It’s also good to subsequently update the status to “closed” – meaning, you have successfully processed the completed run. I have also seen implementations that remove the state table row on completion after logging to a second table (ala RunHistory). The advantage of this approach is that the stored procedure can take as long as necessary, and it will not impact Flows or the connector.
Highlighted
Advocate IV
Advocate IV

Re: SQL stored procedure on-premises data gateway timeout

Thanks Cameron,

 

I had an SSIS package on the SQL server allowing me to run timed, but I think also triggered stored procedures.

I no longer have this package on the new server, and was told to launch stored procedures using flow.

 

Can SQL server 'by itself' (without additiona packages) have a 'server-side SQL trigger' ?

I will check out if this is possible, or if there are any additional constraints.

 

That said, I tried a kind of fire and forget method, but flow, after the time-out, actively engages to try and stop the stored procedure (because it thinks it has failed, I asume). If it would just let the stored procedure do its thing, it would work for me too. 

Highlighted
Microsoft
Microsoft

Re: SQL stored procedure on-premises data gateway timeout

Michel, SQL Server side triggers allow you to kickoff a process that is not tied to a Flow request. That's really the only way to make sure the process isn't terminated after a timeout. That's the crux of "fire and forget" - it uses SQL server-side processing to execute the stored procedure. Anyone else who triggers the stored procedure would have to wait for it to complete, or terminate after some timeout. SQL Server doesn't have an async interface. If you do pursue an F-and-F approach: SQL Azure supports server-side triggers. Data Warehouse does not. See this reference: https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql the workaround I described above allows you to do something beyond the norm - namely, run extra long stored procedures that cannot otherwise be limited to 120 seconds. To do this, I've described a method which requires some complexity and nuance - and isn't for the feint of heart. If you are limited to using flows, then you probably want to simply reduce the amount of data you are processing at once, and limit the amount of time your stored procedure is taking to complete. Thanks, -Cameron
Highlighted
Frequent Visitor

Re: SQL stored procedure on-premises data gateway timeout

I say this is very amateur on Microsoft's behalf. Great thinking outside the box Cameron. Now there has to be away to do more than set and forget? Perhaps a timer to poll a table which keeps state so that you know when your state triggered stored procedure finishes.
Highlighted
Microsoft
Microsoft

Re: SQL stored procedure on-premises data gateway timeout

Yes, you will notice that in my description of Fire-And-Forget approach, you can actually set your flow to watch for changes in the results table. You can then set a Flow trigger to fire when the result shows up (and therefore the stored procedure is complete).  The most important part of the workaround is the ability to disconnect the flow that triggers the stored proc. Once complete, you can use all of the usual mechanisms you are used to (such as flow triggers).

 

Enjoy,

Cameron 

Highlighted
Frequent Visitor

Re: SQL stored procedure on-premises data gateway timeout

Hi Cameron,

 

Could you please show how you create a server-side DML trigger? The fire and forget approach sounds like a winner, but I can't figure out how to create the server-side trigger. 

 

Thanks in advance, 

Best,

Javier

 

 

Highlighted
Frequent Visitor

Re: SQL stored procedure on-premises data gateway timeout

Sorry. It didn't work for me.

Highlighted
Microsoft
Microsoft

Re: SQL stored procedure on-premises data gateway timeout

Hey there Javier - check out this link,

It has a lot of detail and some good examples.

Highlighted
Frequent Visitor

Re: SQL stored procedure on-premises data gateway timeout

Hi Cameron,

Thanks for the link. I have no issues creating the triggers. When I followed your approach, you mentioned that the "key" was to create a "server-side" trigger, and I can't find how to create a "DML server-side" triggers. In other words, how do you create a DML trigger that is "triggered" at the server level instead of the table level? Only DDL and Logon triggers are saved either in the "Stored Procedure" section or the server section. 

When I create a DML trigger (table level) to run a stored procedure that takes more than 2 min, I don't get the results back until the SP is finished. I think I'm missing something since I see others were able to follow your steps. 

Anyhow, I found a workaround where the DML trigger creates a Sql Job that calls the SP. The Job is deleted once the SP is executed. It simulates an async operation that is tracked by the status field in the table you mentioned in your approach. 

Thanks again for your reply.

Best,

Javier

 

Highlighted
Microsoft
Microsoft

Re: SQL stored procedure on-premises data gateway timeout

Hi Javier,

 

Yes, the idea here is to have a server side trigger which executes the stored procedure using parameters inserted into the state table. When the stored proc finishes, it can update the same table with results and update the status. It can also insert result set(s) into a second table, which you can trigger on from LogicApps. I think you've got the gist of it.

 

Thanks,

Cameron

 

Highlighted
Frequent Visitor

Re: SQL stored procedure on-premises data gateway timeout

I retract my statement. 

1. Using a statetable to start a storedproc. Check

2. Updating the statetable with "Complete" when finished. Check

3. Using a flow statement to monitor SQL "Modified" trigger. Trouble ahead!

 

 

Highlighted
Frequent Visitor

Re: SQL stored procedure on-premises data gateway timeout

4) Your Flow should then call the stored procedure created in step 2 above (MasterStart). This will return as soon as the state table entry is created in the RunState table.

 

Some advice here, please. When I do this the store procedure waits for the entry to be created. The entry to be created status = "Pending" waits for the Master stored procedure to finish (20 mins) and thus I am still stuck by the same timeout issue.

Helpful resources

Announcements
firstImage

Super User Program Update

Three Super User rank tiers have been launched!

firstImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

Top Solution Authors
Users online (7,481)