380 likes | 391 Views
Learn about event notifications and service broker, implement a client/repository setup, and troubleshoot and monitor database events.
E N D
Event Notifications with Service Broker Phil Brammer
Phil Brammer • Over 10 years’ experience in database technologies, data warehousing, ETL, on-call… • Started on Red Brick Data Warehouse. Ralph Kimball’s product. Inmon Who? • Worked with Teradata at PayPal • Operationally manages multi-terabyte instances • Dabbles a bit in SSIS – ssistalk.com • Microsoft MVP, SQL Server – 5th year
Denali • Code name • Yukon, Katmai, Kilimanjaro • CTP 3 • microsoft.com/sql/
Agenda • We will • learn about Event Notifications • go over Service Broker concepts • implement a client/repository setup • ask questions as we go through the session
Background at BCBSNE • Say hi to Kevin Klein of Quest Software • BCBSNE uses Quest Spotlight for monitoring • Over 1,000 databases • Two databases over two terabytes in size • ETL very popular • 15-50 database change controls per week • Microsoft shop
Challenges at BCBSNE • Compliance • When things go wrong, who changed what? • What kind of permissions changes happened? • Autogrowths are bad. • Shrinks are worse.
Event Notifications • Similar to a server-side trace • User-configurable • Interfaces with Service Broker • Asynchronous
Using Event Notifications • Compliance monitoring • Operational monitoring • Troubleshooting support
Compliance Monitoring • Archive logins and logouts • Query permissions changes on an object • Capture impersonation activity
Operational Monitoring • Near real-time data/log file auto growths • Shrinks too • Alert on blocking • Alert on deadlocks • Capture backup/restores • Log reindexing activity
Troubleshooting Support • Determine if indexes were dropped recently
Service Broker • Queuing model • Guaranteed message delivery • Asynchronous • Conversation based
Setting up Event Notifications • Setup repository server first • We will need: • To ensure Service Broker is enabled in target database • Setup required tables to hold our data • Setup Service Broker components • Setup client next • Setup Event Notifications • Setup Service Broker components to talk to repository
Setup Repository • Walk through setup
Messages • Sent on a conversation • Bound to a message type
Conversations • Belong to a conversation group • Dialog between two services • Provides exactly-once-in-order delivery • Watch out for fire and forget designs • Ended via two-way acknowledgement
Message Types • Enforces message type validation • Well formed XML • No validation • Empty • Valid XML with Schema • CREATE MESSAGE TYPE [AuditData] AUTHORIZATION [dbo] VALIDATION = NONE
Contracts • Defines which message types are allowed in a conversation • Message types in a contract must be included on both client and repository sides • Enforces who can send a message type on a contract • Initiator, Target, Any • CREATE CONTRACT [AuditDataContract] AUTHORIZATION [dbo] ([AuditData] SENT BY INITIATOR, [EndOfTimer] SENT BY INITIATOR)
Queues • Destination for messages • Transactionally consistent • Survives restarts • Can activate a stored procedure one or many times • CREATE QUEUE [dbo].[TargetAuditQueue] WITH STATUS = ON , RETENTION = OFF , ACTIVATION ( STATUS = ON , PROCEDURE_NAME = [Audit].[prcWriteAuditData] , MAX_QUEUE_READERS = 50 , EXECUTE AS N'dbo' ) ON [PRIMARY]
Activation Procedure • Initiated by a message landing on a queue • Activation can be turned on and off • Activation can spawn many iterations in parallel as load increases • Demo
Services • Hooked up to a queue • Talks to another service • Bound to one or more contracts • CREATE SERVICE [AuditDataWriter] AUTHORIZATION dbo ON QUEUE dbo.TargetAuditQueue ([AuditContract]);
Routes • Sets up a route for messages to understand where to go. • Can set it up many different ways. • We’ll use a transport route • Uses service name to tell message where to go • CREATE ROUTE [AuditRoute] WITH ADDRESS = N'TRANSPORT';
Endpoints • A physical end point (i.e. TCP/IP port) • Authenticates connections • Belong to master database • CREATE ENDPOINT AuditEndPoint STATE = STARTED AS TCP (LISTENER_PORT = 10050) FOR SERVICE_BROKER ( AUTHENTICATION = WINDOWS NTLM );
Volunteer Time • 10 volunteers
Setup Clients • Walk through setup
Client Event Notifications • First we need to set up Event Notifications (EN) • Four steps. • Create a queue • Create a service • Create a route • Create an Event Notification
EN Queue • Same as in the previous setup. Just a plain ol’ queue. • CREATE QUEUE [dbo].[NotificationsQueue] WITH STATUS = ON , RETENTION = OFF ON [PRIMARY] GO
EN Service • CREATE SERVICE [NotificationsService] AUTHORIZATION [dbo] ON QUEUE [dbo].[NotificationsQueue] ([http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]) GO • Note the system contract
EN Route • CREATE ROUTE [NotificationsRoute] AUTHORIZATION [dbo] WITH SERVICE_NAME = N'NotificationsService' , ADDRESS = N'LOCAL' GO • Note how this is different from the last route we created
Event Notification • This is where we setup the events to monitor • Points to a service (which points to a queue) • CREATE EVENT NOTIFICATION NotificationEvent ON SERVER FOR BLOCKED_PROCESS_REPORT, DEADLOCK_GRAPH, DDL_TABLE_EVENTS TO SERVICE 'NotificationsService', 'current database'; GO
Event Notification Demo • Let’s see it work • How does it get to the repository?
Getting data to repository • What will we need? • Stored procedure to process items in EN queue • Stored procedure to send data • Message Types to match repository • Contract to match repository • Another queue to receive messages back from repository • Another stored procedure to process items in received queue • A service • A route • Some back-end tables
Client-Side Tables • Two tables required • AuditErrors to capture any errors • SessionConversations to keep track of current conversations that are in use
Client-Side Stored Procedures • Review and implement stored procedures • prcProcessNotification • prcSendAuditData • prcProcessSenderAuditQueue
Client-Side Service Broker • Similar to previous setups. • Review client-side Service Broker components
Client-Side Implementation • Implement the changes to direct data to repository
Questions • Any further questions?
Resources • Event Notifications - http://msdn.microsoft.com/en-us/library/ms190427.aspx • Service Broker Architecture - http://technet.microsoft.com/en-us/library/ms166125.aspx • DDL Events for Event Notifications - http://msdn.microsoft.com/en-us/library/bb510452.aspx • Trace Events for Event Notifications - http://msdn.microsoft.com/en-us/library/ms190655.aspx • Fire and forget - http://rusanu.com/2006/04/06/fire-and-forget-good-for-the-military-but-not-for-service-broker-conversations/