/// <summary>
        /// Receives a notification when a new inter-role communication event occurs.
        /// </summary>
        public void OnNext(InterRoleCommunicationEvent e)
        {
            DemoPayload msg = e.Payload as DemoPayload;

            if (msg != null)
            {
                msg.ReceiveTickCount = HighResolutionTimer.CurrentTickCount;
                msg.ReceiveTime      = DateTime.UtcNow;

                // Setup the 'From' & 'To' so that output from Azure storage is easier for debugging
                msg.ToInstanceID   = msg.FromInstanceID;
                msg.FromInstanceID = RoleInstanceID;

                // Remove the original payload so that ACKs are not carrying lot more than needed.
                msg.Data = null;

                var response = new InterRoleCommunicationEvent(msg);

                // Setting up so that the routing from framework is UniCast
                response.To = e.From;

                interRoleCommunicator.Publish(response);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Receives a notification when a new inter-role communication event occurs.
        /// </summary>
        public override void OnNext(TestRunStartEvent msg)
        {
            var numInstances = RoleEnvironment.CurrentRoleInstance.Role.Instances.Count - 1;

            var topicBaseCommunicator = this.interRoleCommunicator as InterRoleCommunicationExtension;

            if (topicBaseCommunicator != null)
            {
                topicBaseCommunicator.Settings.EnableAsyncPublish  = msg.EnableAsyncPublish;
                topicBaseCommunicator.Settings.EnableAsyncDispatch = msg.EnableAsyncDispatch;
            }

            if (msg.RequireTopicCleanup)
            {
                // Delete the IRC topic first so that instances will have enough time to recreate senders and receivers.
                var serviceBusSettings = ConfigurationManager.GetSection(ServiceBusConfigurationSettings.SectionName) as ServiceBusConfigurationSettings;
                var pubsubType         = ConfigurationManager.AppSettings[CommonConsts.ConfigSettingPubSubType];

                if (pubsubType.Equals(CommonConsts.ConfigSettingPubSubTypeValueTopic))
                {
                    var topicEndpoint = serviceBusSettings.Endpoints.Get(CommonConsts.TopicServiceBusEndpointName);
                    var credentials   = TokenProvider.CreateSharedSecretTokenProvider(topicEndpoint.IssuerName, topicEndpoint.IssuerSecret);
                    var address       = ServiceBusEnvironment.CreateServiceUri("sb", topicEndpoint.ServiceNamespace, String.Empty);
                    var nsManager     = new NamespaceManager(address, credentials);

                    if (nsManager.GetTopics().Where(t => String.Compare(t.Path, topicEndpoint.TopicName, true) == 0).Count() > 0)
                    {
                        nsManager.DeleteTopic(topicEndpoint.TopicName);
                    }
                }
            }

            if (msg.PurgeTraceLogTable)
            {
                // Purge WADLogsTable.
                PurgeTraceLogTable();
            }

            // Tell ACK subscriber how many messages it should expect to receive and reset the ACK counter.
            this.ackSubscriber.SetExpectedAckCount(msg.MessageCount * numInstances);
            this.ackSubscriber.ResetAckCounter();

            var testRun = new TestRunTableEntity()
            {
                TestRunID     = msg.TestRunID,
                MessageCount  = msg.MessageCount,
                MessageSize   = msg.MessageSize,
                InstanceCount = numInstances,
                StartDateTime = DateTime.UtcNow,
                EndDateTime   = new DateTime(1900, 1, 1)
            };

            var testRunsTable = new TestRunEntityTableContext(this.tableClient.BaseUri.ToString(), this.tableClient.Credentials);

            testRunsTable.TableName = this.testRunsTableName;

            try
            {
                testRunsTable.AddTestRun(testRun);
            }
            catch (DataServiceRequestException ex)
            {
                if (ex.IsEntityAlreadyExists())
                {
                    testRunsTable.UpdateTestRun(testRun);
                }
            }

            using (TraceManager.WorkerRoleComponent.TraceScope("Publishing IRC events", msg.TestRunID, msg.MessageCount, msg.MessageSize))
            {
                for (int i = 1; i <= msg.MessageCount; i++)
                {
                    DemoPayload payload = new DemoPayload(msg.TestRunID, i, numInstances, DateTime.UtcNow);

                    // Create a data portion of the payload equal to the message size specified in the test run settings.
                    payload.Data = new byte[msg.MessageSize];
                    (new Random()).NextBytes(payload.Data);

                    InterRoleCommunicationEvent ircEvent = new InterRoleCommunicationEvent(payload);

                    // Update publish time so that it accurately reflects the current time.
                    payload.PublishTickCount = HighResolutionTimer.CurrentTickCount;
                    payload.PublishTime      = DateTime.UtcNow;

                    // Publish the IRC event.
                    this.interRoleCommunicator.Publish(ircEvent);
                }
            }
        }