Your SlideShare is downloading. ×
Asynchronous t sql
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Asynchronous t sql

2,412
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,412
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
44
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Asynchronous Procedures Reliable T-SQL invocation of procedures using Service Broker Activation Remus Rusanu
  • 2. About Me
    • Consulting on SQL Server problems
    • Founder of bugcollect.com
    • Former engineer with SQL Server Engine
    • SQL Server blog at http://rusanu.com/blog
    • Service Broker Expert
  • 3. Agenda
    • Why asynchronous procedures
    • Service Broker Activation
    • Using Activation for code execution
    • Demo 1
    • Pass parameters to procedures
    • Demo 2
    • Activation context
    • Resources Q&A
  • 4. Why Asynchronous Procedures
    • Allow client to return before processing completes
    • Read operations are suitable for the client stack asynchronous methods: BeginExecuteXXX, EndExecuteXXX
    • Write operations require reliability: must complete even if client disconnects or crashes
  • 5. Reliable Asynchronous Calls
    • The client must trust that once the call is submitted, it is guaranteed to occur
    • No retry logic on client code
    • The call has to be transactional consistent
      • Backup/restore
      • Mirroring/Clustering failover
    • The call must execute even if client disconnects, exits or crashes
  • 6. Service Broker Activation
    • Service Broker: message based distributed application platform
    • Asynchronous Event Driven
    • Activation: start a procedure when there are messages to process
    • CREATE QUEUE [queuename] WITH ACTIVATION ( PROCEDURE_NAME = usp_ActivatedProcedure, STATUS = ON, MAX_QUEUE_READERS = 5, EXECUTE AS Owner);
  • 7. Activation internals
    • Each queue is monitored by the system
      • sys.dm_broker_queue_monitors
    • Each time a message is enqueued the queue id is added to the in memory transaction context
    • After commit the transaction notifies all relevant queue monitors that messages have been added
    • The queue monitor will determine if it needs to launch a new activated procedure
    • http://rusanu.com/2008/08/03/understanding-queue-monitors/
  • 8. Activation Triggers After transaction commits During transaction Background connection Client connection EXECUTE AS context Login context When messages available On INSERT/DELETE/UPDATE Batch calls for set processing Each call is standalone Runs in a loop New instance every 5 sec Up to MAX_QUEUE_READERS Runs in each session that invokes it
  • 9. Activation SQL Agent Jobs Internal to SQL Engine External process Agent Runs on demand Runs on timer Consistent backup/restore Backup split with msdb Mirroring failover No failover Available in every edition Not in Express editions For for many small jobs Better fit for few large jobs
  • 10. Using Activation for code execution
    • Recipe:
    • Create a queue with an activated procedure
    • Create a service on the queue
    • SEND a message to the service. Procedure name to be invoked is passed in as the message body.
    • After SEND is committed, the queue procedure will activate
    • Execute the desired procedure from the activated procedure
  • 11. Required Infrastructure
    • Procedure usp_AsyncExecInvoke
      • Enqueues an asynchronous invocation
      • Accepts the procedure to be invoked as parameter
      • Returns an opaque token used to check the invocation result
    • Procedure usp_AsyncExecActivated
      • The activated procedure
      • Must RECEIVE from the queue and execute the desired code
    • Service Broker objects
      • Queue: AsyncExecQueue
      • Service: AsyncExecService
      • Contract, Message Type: use the [DEFAULT] ones
    • Table AsyncExecResults
      • A table to store the execution times and results
  • 12. Asynchronous Execution Workflow
    • Client Session
    • exec usp_AsyncExecInvoke ‘usp_myProcedure’;
    • SEND ‘usp_myProcedure’ to AsyncExecService;
    • INSERT into AsyncExecResults
    • COMMIT
    • SELECT .. FROM AsyncExecResults
    • Background Session
    • Activate usp_AsyncExecActivated
    • RECEIVE from AsyncExecQueue
    • EXEC usp_myProcedure
    • UPDATE AsyncExecResults
    • COMMIT
  • 13. Demo 1
  • 14. Pass Parameters to procedures
    • Pass the parameters with the activation request message as part of the message body
    • Use XML to encode the parameter names an values
    • A generic mechanism is challenging
      • Arbitrary parameter types
      • Arbitrary parameter names
      • Execute the procedure passing in the appropriate parameters requires dynamic SQL
  • 15. Invoke Arbitrary Procedure
    • Original code:
      • exec usp_myProcedure @id=1, @name=‘foo’;
    • Asynchronous Invoke:
      • exec usp_AsyncExecInvoke @procedureName=N’usp_myProcedure’, @p1=1, @n1=N‘@id’, @p2=‘foo’, @n2=N‘@name;
    • @p1, @n1 for first parameter value and name, @p2, @n2 for second parameter value and name etc.
    • @p1, @p2, @p3 etc are of arbitrary types. The formal defintion of usp_AsyncExecInvoke declares them as sql_variant .
  • 16. The Invocation Message
    • < procedure > < name > usp_myProcedure </ name > < parameters > < parameter Name = &quot; @id &quot; BaseType = &quot; numeric &quot; > 1.0 </ parameter > < parameter Name = &quot; @name &quot; BaseType = &quot; nvarchar &quot; > foo </ parameter >
    • … </ parameters > </ procedure >
    • The actual XML also include the numeric precision and varchar/nvarchar length
  • 17. Dynamic SQL for execution
    • declare @pt1 numeric ( 2 , 1 ) declare @pt2 nvarchar ( 6 ) declare @pt3 numeric( 3,1 ) select @pt1 = @x . value ( N'(//procedure/parameters/parameter)[1]' , N'numeric(2,1)' ); select @pt2 = @x . value ( N'(//procedure/parameters/parameter)[2]' , N'nvarchar(6)' ); select @pt3 = @x . value ( N'(//procedure/parameters/parameter)[3]' , N‘numeric(3,1)' ); exec [usp_myProcedure] @id = @pt1 , @name = @pt2 , @amount = @pt3 ;
    • This batch is executed using sp_executesql
    • The message received by the activated procedure is passed as the XML type @x parameter
  • 18. Demo 2
  • 19. Activation context
    • Background Session
      • No connection
      • No Login
    • EXECUTE AS clause is required
    • It is a database impersonation context, not a server impersonation context:
      • Same as EXECUTE AS USER = ‘<username>’;
      • Cannot access any other database
      • Cannot access server level views
    • ALTER DATABASE <dbname> SET TRUSTWORTHY ON;
      • Extends the database impersonation context to server
      • Same as EXECUTE AS LOGIN = ‘<loginname>’;
    • More secure solution: code signing
      • http://rusanu.com/2006/03/07/call-a-procedure-in-another-database-from-an-activated-procedure/
  • 20. Resources and Q&A
    • http://rusanu.com/2008/08/03/understanding-queue-monitors/
    • Extending Database Impersonation by Using EXECUTE AS: http://msdn.microsoft.com/en-us/library/ms188304.aspx
    • contact@ rusanu. com
    • @rusanu