Example #1
0
        public void Run(ICompiledApplication application)
        {
            TelemetryListenerArray telemetry = null;

            if (application.Configurations.TryGetValue(typeof(ReactiveMachine.TelemetryBlobWriter.Configuration), out var c))
            {
                telemetry = new TelemetryListenerArray((ReactiveMachine.TelemetryBlobWriter.Configuration)c, application, this.GetType(), deploymentId, deploymentTimestamp);
                application.HostServices.RegisterTelemetryListener(telemetry);
            }

            application.HostServices.RegisterSend(Send);
            application.HostServices.RegisterGlobalExceptionHandler(HandleGlobalException);

            _serializer = new DataContractSerializer(typeof(IMessage), application.SerializableTypes);

            processes = new ProcessInfo[application.NumberProcesses];
            for (uint i = 0; i < application.NumberProcesses; i++)
            {
                processes[i] = new ProcessInfo()
                {
                    Inbox = new List <IMessage>(),
                };
            }
            for (uint i = 0; i < application.NumberProcesses; i++)
            {
                processes[i].Process = application.MakeProcess(i);
                processes[i].Process.FirstStart();
                processes[i].Process.BecomePrimary();
            }

            Console.WriteLine($"=========================== START SIMULATION ===========================");
            var stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();

            while (processes[0].Process.RequestsOutstanding())
            {
                for (uint i = 0; i < application.NumberProcesses; i++)
                {
                    var             info    = processes[i];
                    List <IMessage> deliver = empty;
                    lock (sgl)
                    {
                        if (info.Inbox.Count > 0)
                        {
                            deliver    = info.Inbox;
                            info.Inbox = new List <IMessage>();
                        }
                    }
                    if (deliver.Count > 0)
                    {
                        foreach (var m in deliver)
                        {
                            //// lose 1/2 of activity responses that originated on older instances
                            //if (configuration.DeliverStaleExternalsOneOutOf != 1
                            //    && m is ReactiveMachine.Implementation.RespondToActivity rte
                            //    && rte.InstanceId != info.Process.InstanceId
                            //    && (random.Next(configuration.DeliverStaleExternalsOneOutOf) != 0))
                            //{
                            //    continue;
                            //}

                            info.Process.ProcessMessage(m);

                            if (configuration.RoundTripProcessStateEvery < int.MaxValue &&
                                random.Next(configuration.RoundTripProcessStateEvery) == 0)    // for debugging process state serialization
                            {
                                info.Process.SaveState(out var bytes, out var label1);
                                info.Process.Restore(bytes, out var label2);
                                info.Process.BecomePrimary();
                            }
                        }
                    }
                }
            }

            Console.WriteLine($"=========================== END SIMULATION ===========================");
            stopwatch.Stop();
            Console.WriteLine($"elapsed = {stopwatch.Elapsed.TotalSeconds:f2}s  #messages={messageCount}");

            telemetry?.Shutdown().Wait();
        }
Example #2
0
        public void Run(ICompiledApplication application)
        {
            TelemetryListenerArray telemetry = null;

            if (application.Configurations.TryGetValue(typeof(ReactiveMachine.TelemetryBlobWriter.Configuration), out var c))
            {
                telemetry = new TelemetryListenerArray((ReactiveMachine.TelemetryBlobWriter.Configuration)c, application, this.GetType(), deploymentId, deploymentTimestamp);
                application.HostServices.RegisterTelemetryListener(telemetry);
            }

            application.HostServices.RegisterSend(Send);
            application.HostServices.RegisterGlobalExceptionHandler(HandleGlobalException);
            application.HostServices.RegisterGlobalShutdown(() => shutdown.Cancel());
            _serializer = new DataContractSerializer(typeof(IMessage), application.SerializableTypes);


            processes = new ProcessInfo[application.NumberProcesses];
            for (uint i = 0; i < application.NumberProcesses; i++)
            {
                processes[i] = new ProcessInfo()
                {
                    Inbox = new List <IMessage>(),
                };
            }
            for (uint i = 0; i < application.NumberProcesses; i++)
            {
                var info = processes[i];

                logger.LogDebug($"Recovering process {i}.");

                info.Process = application.MakeProcess(i);
                info.Process.FirstStart();
            }
            for (uint i = 0; i < application.NumberProcesses; i++)
            {
                uint processId = i;
                new Thread(() => RunProcess(processId, processes[processId], application)).Start();
            }

            bool someoneBusy = true;

            while (!shutdown.IsCancellationRequested)
            {
                someoneBusy = false;
                for (uint i = 0; i < application.NumberProcesses; i++)
                {
                    var  info = processes[i];
                    long received;
                    bool busy;
                    lock (info)
                    {
                        received = info.Received;
                        busy     = (info.Inbox.Count > 0) || (info.Process?.RequestsOutstanding() ?? true);
                    }
                    someoneBusy = busy || someoneBusy;
                    logger.LogInformation($"Process {i}: Received={received:D12} busy={busy}");
                }
                if (!someoneBusy)
                {
                    break;
                }

                emulator.FlushLog();

                shutdown.Token.WaitHandle.WaitOne(TimeSpan.FromSeconds(10));
            }

            telemetry?.Shutdown().Wait();
        }