PageRenderTime 84ms CodeModel.GetById 9ms RepoModel.GetById 8ms app.codeStats 0ms

/Katmai_October2008_Refresh1/FastDataPush/Scripts/1 - Common setup.sql

#
SQL | 228 lines | 104 code | 18 blank | 106 comment | 0 complexity | 2c024df89c1f9c28577b84c6296de9d2 MD5 | raw file
  1. --------------------------------------------------------------------
  2. -- Script for fast data push sample.
  3. --
  4. -- This file is part of the Microsoft SQL Server Code Samples.
  5. -- Copyright (C) Microsoft Corporation. All Rights reserved.
  6. -- This source code is intended only as a supplement to Microsoft
  7. -- Development Tools and/or on-line documentation. See these other
  8. -- materials for detailed information regarding Microsoft code samples.
  9. --
  10. -- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  11. -- ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  12. -- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  13. -- PARTICULAR PURPOSE.
  14. --------------------------------------------------------------------
  15. ----------------------------------------------------
  16. -- Common setup for fast data push.
  17. -- Before running, replace the configuration-dependent
  18. -- domain_name and partner_host names.
  19. ----------------------------------------------------
  20. USE master;
  21. GO
  22. ---------------------------------------------------------------------
  23. -- Create the broker endpoint using Windows authentication.
  24. -- On a secure network, encryption may be disabled to improve speed:
  25. -- (AUTHENTICATION = Windows, ENCRYPTION = DISABLED)
  26. -- This step can be skipped if services are in the same database instance.
  27. ---------------------------------------------------------------------
  28. IF EXISTS (SELECT * FROM sys.endpoints WHERE name = 'service_broker_endpoint')
  29. DROP ENDPOINT service_broker_endpoint;
  30. GO
  31. CREATE ENDPOINT service_broker_endpoint
  32. STATE = STARTED
  33. AS TCP (LISTENER_PORT = 4022)
  34. FOR SERVICE_BROKER (AUTHENTICATION = Windows);
  35. GO
  36. -- A procedure to create a Windows login and grant it endpoint connection permission.
  37. IF EXISTS (SELECT name FROM tempdb.sys.procedures WHERE name LIKE '#usp_windows_login_for_broker_endpoint%')
  38. DROP PROCEDURE #usp_windows_login_for_broker_endpoint;
  39. GO
  40. CREATE PROCEDURE #usp_windows_login_for_broker_endpoint (
  41. @domain_name VARCHAR(100),
  42. @login_name VARCHAR(50),
  43. @endpoint_name VARCHAR(50))
  44. AS
  45. BEGIN
  46. SET NOCOUNT ON;
  47. DECLARE @query VARCHAR(1000);
  48. -- Create the login.
  49. SET @query =
  50. 'IF EXISTS (SELECT * FROM sys.syslogins WHERE name = ''' + @domain_name + '\' + @login_name + ''')
  51. DROP LOGIN [' + @domain_name + '\' + @login_name + ']';
  52. EXEC (@query);
  53. SET @query = 'CREATE LOGIN [' + @domain_name + '\' + @login_name + '] FROM Windows';
  54. EXEC (@query);
  55. -- Grant the login connection access to the endpoint.
  56. SET @query = 'GRANT CONNECT ON ENDPOINT::' + @endpoint_name + ' TO [' + @domain_name + '\' + @login_name + ']';
  57. EXEC (@query);
  58. END;
  59. GO
  60. -- Create a login for the partner machine (partner_host) in the
  61. -- shared domain (domain_name) and grant it endpoint connection permission.
  62. -- This assumes the availability of Kerberos authentication.
  63. -- Note: the '$' is significant.
  64. EXEC #usp_windows_login_for_broker_endpoint 'domain_name', 'partner_host$', 'service_broker_endpoint';
  65. GO
  66. ---------------------------------------------------------------------
  67. -- Create the data push database.
  68. ---------------------------------------------------------------------
  69. IF EXISTS (SELECT * FROM sys.databases WHERE name = 'data_push_database')
  70. DROP DATABASE data_push_database;
  71. GO
  72. CREATE DATABASE data_push_database;
  73. GO
  74. USE data_push_database;
  75. GO
  76. -- Create messages and contract.
  77. CREATE MESSAGE TYPE data_push_message VALIDATION = NONE;
  78. CREATE MESSAGE TYPE end_of_stream;
  79. CREATE CONTRACT data_push_contract
  80. (
  81. data_push_message SENT BY INITIATOR,
  82. end_of_stream SENT BY INITIATOR
  83. );
  84. ----------------------------------------------------
  85. -- The data push parameters.
  86. --
  87. -- Application parameters:
  88. -- message_quantity: number of messages sent.
  89. -- message_size: size of message in bytes.
  90. -- message_processing_time: time for target to process a message.
  91. -- Format: 'hh:mm:ss:xxx' hh=hours, mm=minutes, ss=seconds, xxx=milliseconds
  92. --
  93. -- Internal parameters:
  94. -- number_initiator_transactions: number of initiator transactions used.
  95. -- Notes: 1. Fewer is more efficient since each transaction entails an overhead.
  96. -- 2. Messages are actually sent when transaction commits, so sending a large
  97. -- number of messages in a transaction can result in increased latency.
  98. -- initiator_transaction_delay: delay time between initiator transactions.
  99. -- Format: 'hh:mm:ss:xxx' hh=hours, mm=minutes, ss=seconds, xxx=milliseconds
  100. -- Notes: 1. A transaction can be thought of as a burst of message_quantity /
  101. -- number_initiator_transactions messages. This delay specifies a time
  102. -- to wait before the next transaction is run.
  103. -- 2. This parameter can be used to simulate message traffic distributed
  104. -- over time.
  105. -- number_dialogs: number of dialogs used to send messages.
  106. -- Notes: 1. Message ordering only guaranteed with a dialog.
  107. -- 2. Multiple dialogs allows concurrent processing on target.
  108. -- 3. Dialog creation is expensive; dialog reuse is employed here.
  109. -- dialog_recycle_max_messages: maximum number messages sent on a dialog before
  110. -- recycling the dialog. Recycling is defined as ending the old dialog and
  111. -- beginning a new one. A value of -1 indicates no recycling.
  112. -- Notes: 1. Larger is more efficient since is minimizes the overhead of
  113. -- creating dialogs.
  114. -- 2. Larger can complicate dialog error processing.
  115. -- number_target_procedures: number of activated target procedures to receive messages.
  116. -- Notes: 1. A target proc locks all messages in a dialog when it receives first message
  117. -- for a dialog, blocking other procs from processing these messages.
  118. -- 2. Thus more dialogs yields increased concurrent processing. However, unless
  119. -- dialog recycling is used, this should be set to number_dialogs, which
  120. -- can utilize a target proc for each dialog.
  121. -- max_messages_per_receive: maximum number of messages per target receive call.
  122. -- Notes: 1. Larger is more efficient, but can complicate transaction error processing.
  123. -- 2. The maximum value can be set to message_quantity / number_dialogs.
  124. --
  125. -- General note: for simplicity, @message_quantity should be evenly divisible
  126. -- by @number_initiator_transactions x @number_dialogs, since this allows a
  127. -- constant number of messages to be sent per dialog per transaction. "Remainder"
  128. -- messages will not be sent to the target.
  129. --
  130. -- Example:
  131. --
  132. -- I want to send 100000 messages in sets of 10000 with a delay of 10 seconds between
  133. -- each set. This calls for 10 transactions. Each message is 100 bytes and the target
  134. -- message processing time is 10 ms. The messages are independent of each other, so use
  135. -- 5 dialogs and target procedures to get some concurrent processing on the target. Allow
  136. -- each target proc to receive 2000 messages at a time. Do not recycle dialogs.
  137. --
  138. -- INSERT INTO data_push_parameters
  139. -- VALUES
  140. -- (
  141. -- 100000,
  142. -- 10000,
  143. -- '00:00:00:010',
  144. -- 10,
  145. -- '00:00:10:000',
  146. -- 5,
  147. -- -1,
  148. -- 5,
  149. -- 2000
  150. -- );
  151. --
  152. --
  153. CREATE TABLE data_push_parameters (
  154. message_quantity BIGINT NOT NULL,
  155. message_size INT NOT NULL,
  156. message_processing_time CHAR(12) NOT NULL,
  157. number_initiator_transactions INT NOT NULL,
  158. initiator_transaction_delay CHAR(12) NOT NULL,
  159. number_dialogs INT NOT NULL,
  160. dialog_recycle_max_messages BIGINT NOT NULL,
  161. number_target_procedures INT NOT NULL,
  162. max_messages_per_receive BIGINT NOT NULL);
  163. GO
  164. -- Insert parameter values.
  165. TRUNCATE TABLE data_push_parameters;
  166. INSERT INTO data_push_parameters
  167. (
  168. message_quantity,
  169. message_size,
  170. message_processing_time,
  171. number_initiator_transactions,
  172. initiator_transaction_delay,
  173. number_dialogs,
  174. dialog_recycle_max_messages,
  175. number_target_procedures,
  176. max_messages_per_receive
  177. )
  178. VALUES
  179. (
  180. 10000,
  181. 1000,
  182. '00:00:00:000',
  183. 1,
  184. '00:00:00:000',
  185. 1,
  186. -1,
  187. 1,
  188. 1000
  189. );
  190. GO
  191. -- Check parameters.
  192. DECLARE @message_quantity BIGINT;
  193. DECLARE @number_initiator_transactions INT;
  194. DECLARE @number_dialogs INT;
  195. DECLARE @i BIGINT;
  196. DECLARE @string VARCHAR(50);
  197. SET @message_quantity = (SELECT message_quantity FROM data_push_parameters);
  198. SET @number_initiator_transactions = (SELECT number_initiator_transactions FROM data_push_parameters);
  199. SET @number_dialogs = (SELECT number_dialogs FROM data_push_parameters);
  200. SET @i = @message_quantity / (@number_dialogs * @number_initiator_transactions);
  201. SET @i = @i * @number_dialogs * @number_initiator_transactions;
  202. IF @message_quantity > @i
  203. BEGIN
  204. SET @i = @message_quantity - @i;
  205. SET @string = (SELECT CAST( @i AS VARCHAR(50)));
  206. PRINT 'Warning: @message_quantity is not evenly divisible by @number_dialogs * @number_initiator_transactions';
  207. PRINT @string + ' messages will not be sent to the target';
  208. END;
  209. GO