Exemplo n.º 1
0
        void UpdateSend()
        {
            if (filename == null || filename == "")
            {
                return;
            }
            string json = File.ReadAllText(filename);
            List <ReceivedMessage> messages = JsonConvert.DeserializeObject <List <ReceivedMessage> >(json);

            udp_.StartClient(address, port);
            count = 0;
            double start = MonotonicTimestamp.Now().Seconds();

            foreach (var message in messages)
            {
                double now;
                do
                {
                    now = MonotonicTimestamp.Now().Seconds() - start;
                } while (now < message.time);

                /*if (now < message.time)
                 *  System.Threading.Thread.Sleep(1000 * (int)(message.time - now));*/
                udp_.Send(message.buffer, message.buffer.Length);
                count++;
            }
            udp_.Stop();
            thread_.Stop();
        }
Exemplo n.º 2
0
 public void StopTimingExecution()
 {
     if (mStart == null)
     {
         throw new InvalidOperationException("Execution timing has not been started");
     }
     if (mEnd == null)
     {
         mEnd = MonotonicTimestamp.Now();
     }
 }
Exemplo n.º 3
0
 void UpdateMessage()
 {
     while (udp_.messageCount > 0)
     {
         if (!received)
         {
             received = true;
             baseTime = MonotonicTimestamp.Now().Seconds();
         }
         double now     = MonotonicTimestamp.Now().Seconds();
         var    buf     = udp_.Receive();
         var    message = new ReceivedMessage();
         message.buffer = buf;
         message.time   = now - baseTime;
         messages.Add(message);
         count++;
     }
 }
Exemplo n.º 4
0
        private async Task ProcessStatsBatchAsync(IEnumerable <StandardExecutionPerformanceMonitorWriteRequest> currentBatch)
        {
            MonotonicTimestamp startWrite = MonotonicTimestamp
                                            .Now();

            List <TaskPerformanceStats> executionTimeInfoBatch =
                new List <TaskPerformanceStats>();

            try
            {
                foreach (StandardExecutionPerformanceMonitorWriteRequest rq in currentBatch)
                {
                    executionTimeInfoBatch.Add(new TaskPerformanceStats(rq.PayloadType, rq.DurationMilliseconds));
                }

                await mStatsWriter.WriteAsync(executionTimeInfoBatch);

                foreach (StandardExecutionPerformanceMonitorWriteRequest rq in currentBatch)
                {
                    rq.SetCompleted(1);
                }

                IncrementPerfMonWriteCount(MonotonicTimestamp
                                           .Since(startWrite));
            }
            catch (Exception exc)
            {
                foreach (StandardExecutionPerformanceMonitorWriteRequest rq in currentBatch)
                {
                    rq.SetFailed(exc);
                    if (rq.CanBeRetried)
                    {
                        mStatsProcessingQueue.Add(rq);
                    }
                }

                mLogger.Error("Error processing performance stats batch", exc);
            }
        }
Exemplo n.º 5
0
        private async Task ProcessResultBatchAsync(Queue <PostgreSqlTaskResultQueueProcessRequest> currentBatch)
        {
            MonotonicTimestamp startWrite = MonotonicTimestamp
                                            .Now();

            //An explicit choice has been made not to use transactions
            //	since failing to update a result MUST NOT
            //	cause the other successful updates to be rolled back.
            using (NpgsqlConnection conn = await OpenConnectionAsync(CancellationToken.None))
                using (NpgsqlCommand updateCmd = new NpgsqlCommand(mUpdateSql, conn))
                {
                    NpgsqlParameter pStatus = updateCmd.Parameters
                                              .Add("t_status", NpgsqlDbType.Integer);
                    NpgsqlParameter pLastError = updateCmd.Parameters
                                                 .Add("t_last_error", NpgsqlDbType.Text);
                    NpgsqlParameter pErrorCount = updateCmd.Parameters
                                                  .Add("t_error_count", NpgsqlDbType.Integer);
                    NpgsqlParameter pLastErrorIsRecoverable = updateCmd.Parameters
                                                              .Add("t_last_error_recoverable", NpgsqlDbType.Boolean);
                    NpgsqlParameter pProcessingTime = updateCmd.Parameters
                                                      .Add("t_processing_time_milliseconds", NpgsqlDbType.Bigint);
                    NpgsqlParameter pFinalizedAt = updateCmd.Parameters
                                                   .Add("t_processing_finalized_at_ts", NpgsqlDbType.TimestampTz);
                    NpgsqlParameter pId = updateCmd.Parameters
                                          .Add("t_id", NpgsqlDbType.Uuid);

                    await updateCmd.PrepareAsync();

                    while (currentBatch.Count > 0)
                    {
                        PostgreSqlTaskResultQueueProcessRequest processRq =
                            currentBatch.Dequeue();

                        try
                        {
                            pStatus.Value = ( int )processRq.ResultToUpdate.Status;

                            string strLastError = processRq.ResultToUpdate.LastError.ToJson();
                            if (strLastError != null)
                            {
                                pLastError.Value = strLastError;
                            }
                            else
                            {
                                pLastError.Value = DBNull.Value;
                            }

                            pErrorCount.Value             = processRq.ResultToUpdate.ErrorCount;
                            pLastErrorIsRecoverable.Value = processRq.ResultToUpdate.LastErrorIsRecoverable;
                            pProcessingTime.Value         = processRq.ResultToUpdate.ProcessingTimeMilliseconds;

                            if (processRq.ResultToUpdate.ProcessingFinalizedAtTs.HasValue)
                            {
                                pFinalizedAt.Value = processRq.ResultToUpdate.ProcessingFinalizedAtTs;
                            }
                            else
                            {
                                pFinalizedAt.Value = DBNull.Value;
                            }

                            pId.Value = processRq.ResultToUpdate.Id;

                            int affectedRows = await updateCmd.ExecuteNonQueryAsync();

                            processRq.SetCompleted(affectedRows);

                            IncrementResultWriteCount(MonotonicTimestamp
                                                      .Since(startWrite));
                        }
                        catch (OperationCanceledException)
                        {
                            processRq.SetCancelled();
                            throw;
                        }
                        catch (Exception exc)
                        {
                            processRq.SetFailed(exc);
                            if (processRq.CanBeRetried)
                            {
                                mResultProcessingQueue.Add(processRq);
                            }

                            mLogger.Error("Error processing result", exc);
                        }
                    }

                    await conn.CloseAsync();
                }
        }
        public async Task <IQueuedTaskToken> DequeueAsync(params string[] selectTaskTypes)
        {
            NpgsqlConnection          conn               = null;
            QueuedTask                dequeuedTask       = null;
            QueuedTaskResult          dequeuedTaskResult = null;
            PostgreSqlQueuedTaskToken dequeuedTaskToken  = null;

            MonotonicTimestamp startDequeue;
            DateTimeOffset     refNow = mTimestampProvider.GetNow();

            CheckNotDisposedOrThrow();

            try
            {
                mLogger.DebugFormat("Begin dequeue task. Looking for types: {0}.",
                                    string.Join <string>(",", selectTaskTypes));

                startDequeue = MonotonicTimestamp
                               .Now();

                conn = await OpenQueueConnectionAsync();

                if (conn == null)
                {
                    return(null);
                }

                using (NpgsqlTransaction tx = conn.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    //1. Dequeue means that we acquire lock on a task in the queue
                    //	with the guarantee that nobody else did, and respecting
                    //	the priority and static locks (basically the task_locked_until which says
                    //	that it should not be pulled out of the queue until the
                    //	current abstract time reaches that tick value)
                    dequeuedTask = await TryDequeueTaskAsync(selectTaskTypes, refNow, conn, tx);

                    if (dequeuedTask != null)
                    {
                        //2. Mark the task as being "Processing" and pull result info
                        //	The result is stored separately and it's what allows us to remove
                        //	the task from the queue at step #2,
                        //	whils also tracking it's processing status and previous results
                        dequeuedTaskResult = await TryUpdateTaskResultAsync(dequeuedTask, conn, tx);

                        if (dequeuedTaskResult != null)
                        {
                            await tx.CommitAsync();

                            dequeuedTaskToken = new PostgreSqlQueuedTaskToken(dequeuedTask,
                                                                              dequeuedTaskResult,
                                                                              refNow);
                        }
                    }

                    if (dequeuedTaskToken != null)
                    {
                        IncrementDequeueCount(MonotonicTimestamp.Since(startDequeue));
                    }
                    else
                    {
                        await tx.RollbackAsync();
                    }
                }
            }
            finally
            {
                if (conn != null)
                {
                    await conn.CloseAsync();

                    conn.Dispose();
                }
            }

            return(dequeuedTaskToken);
        }
Exemplo n.º 7
0
 public void StartTimingExecution()
 {
     mStart = MonotonicTimestamp.Now();
     mEnd   = null;
 }