Пример #1
0
        protected TimeSpan ComputeNewTimeOut(double ms, TimeStats stats, double min, double stdevs)
        {
            stats.AddSample(ms);
            double timeout = stats.Average + stdevs * stats.StdDev;

            timeout = Math.Max(min, timeout);
            return(TimeSpan.FromMilliseconds(timeout));
        }
Пример #2
0
            public TimeStats Clone()
            {
                var ret = new TimeStats(_exp_moving_rtt, _exp_factor);

                ret._exp_moving_square_rtt = _exp_moving_square_rtt;
                ret._exp_moving_stdev      = _exp_moving_stdev;
                ret._max_rtt = _max_rtt;
                return(ret);
            }
Пример #3
0
    private string ToCSVString()
    {
        string csv = "Name, " + TimeStats.GetCSVHeader();

        foreach (string key in timeDictionary.Keys)
        {
            csv += key + ", " + timeDictionary[key].ToCSVString();
        }

        return(csv);
    }
Пример #4
0
    public void StartMeasuring(string name)
    {
        TimeStats stats = new TimeStats();

        if (timeDictionary.ContainsKey(name))
        {
            timeDictionary[name] = stats;
        }
        else
        {
            timeDictionary.Add(name, stats);
        }
    }
Пример #5
0
        public ReqrepManager(object info, PType prefix)
        {
            ReqrepManager existing;

            lock ( _inst_tab_sync ) {
                if (_instance_table.TryGetValue(info, out existing))
                {
                    throw new Exception("Already an existing ReqrepManager for: " + info.ToString());
                }
                else
                {
                    _instance_table[info] = this;
                }
            }
            _info = info.ToString();

            _prefix            = prefix;
            _req_handler_table = new Hashtable();
            Random r = new Random();

            //Don't use negative numbers:
            _req_state_table = new UidGenerator <RequestState>(r, true);
            //Don't use negative numbers:
            _reply_id_table    = new UidGenerator <ReplyState>(r, true);
            _rep_handler_table = new Hashtable();

            /**
             * We keep a list of the most recent 1000 replies until they
             * get too old.  If the reply gets older than reptimeout, we
             * remove it
             */
            _reply_cache = new Cache(1000);
            _reply_cache.EvictionEvent += HandleReplyCacheEviction;

            /*
             * Here we set the timeout mechanisms.  There is a default
             * value, but this is now dynamic based on the observed
             * RTT of the network
             */
            //resend the request after 5 seconds.
            _edge_reqtimeout    = new TimeSpan(0, 0, 0, 0, 5000);
            _nonedge_reqtimeout = new TimeSpan(0, 0, 0, 0, 5000);
            //Start with 50 sec timeout
            _acked_reqtimeout = new TimeSpan(0, 0, 0, 0, 50000);
            //Here we track the statistics to improve the timeouts:
            _nonedge_rtt_stats = new TimeStats(_nonedge_reqtimeout.TotalMilliseconds, 0.98);
            _edge_rtt_stats    = new TimeStats(_edge_reqtimeout.TotalMilliseconds, 0.98);
            _acked_rtt_stats   = new TimeStats(_acked_reqtimeout.TotalMilliseconds, 0.98);
            _last_check        = DateTime.UtcNow;
        }
Пример #6
0
 public TimeOutManager()
 {
     /*
      * Here we set the timeout mechanisms.  There is a default
      * value, but this is now dynamic based on the observed
      * RTT of the network
      */
     //resend the request after 5 seconds by default
     _min_timeout  = 5000;
     _global_stats = new TimeStats(_min_timeout, 0.98);
     //Start with 50 sec timeout
     _acked_rtt_stats = new TimeStats(_min_timeout * 10, 0.98);
     _last_check      = DateTime.UtcNow;
     _send_stats      = new Cache(1000);
     _type_stats      = new Cache(100);
     _sync            = new object();
 }
Пример #7
0
 public void AddReplySampleFor(RequestState reqs, ISender s, TimeSpan rtt)
 {
     /*
      * Let's look at how long it took to get this reply:
      */
     if (reqs.GotAck)
     {
         lock (_sync) {
             TimeStats ts = _acked_rtt_stats;
             ts.AddSample(rtt.TotalMilliseconds);
             _min_timeout = Math.Min(_min_timeout, ts.AvePlusKStdDev(_STD_DEVS));
         }
     }
     else
     {
         AddAckSampleFor(reqs, s, rtt);
     }
 }
Пример #8
0
            private Pair <TimeStats, TimeStats> GetStatsFor(ISender s)
            {
                System.Type st     = s.GetType();
                TimeStats   typets = (TimeStats)_type_stats[st];

                if (null == typets)
                {
                    typets          = _global_stats.Clone();
                    _type_stats[st] = typets;
                }

                TimeStats sendts = (TimeStats)_send_stats[s];

                if (null == sendts)
                {
                    sendts         = typets.Clone();
                    _send_stats[s] = sendts;
                }
                return(new Pair <TimeStats, TimeStats>(typets, sendts));
            }
Пример #9
0
 public TimeStats Clone() {
   var ret = new TimeStats(_exp_moving_rtt, _exp_factor);
   ret._exp_moving_square_rtt = _exp_moving_square_rtt;
   ret._exp_moving_stdev = _exp_moving_stdev;
   ret._max_rtt = _max_rtt;
   return ret;
 }
Пример #10
0
 public TimeOutManager() {
   /*
    * Here we set the timeout mechanisms.  There is a default
    * value, but this is now dynamic based on the observed
    * RTT of the network
    */
   //resend the request after 5 seconds by default
   _min_timeout = 5000;
   _global_stats = new TimeStats(_min_timeout, 0.98);
   //Start with 50 sec timeout
   _acked_rtt_stats = new TimeStats(_min_timeout * 10, 0.98);
   _last_check = DateTime.UtcNow;
   _send_stats = new Cache(1000);
   _type_stats = new Cache(100);
   _sync = new object();
 }
Пример #11
0
 protected TimeSpan ComputeNewTimeOut(double ms, TimeStats stats, double min, double stdevs) {
   stats.AddSample(ms);
   double timeout = stats.Average + stdevs * stats.StdDev;
   timeout = Math.Max(min, timeout);
   return TimeSpan.FromMilliseconds( timeout );
 }
Пример #12
0
  /**
   * Protected constructor, we want to control ReqrepManager instances
   * running on a node. 
   * @param node The Node we work for
   */
  public ReqrepManager(string info) {
    _info = info;

    _rand = new Random();
    _req_handler_table = new Hashtable();
    _req_state_table = new Hashtable();
    _rep_handler_table = new Hashtable();

    /**
     * We keep a list of the most recent 1000 replies until they
     * get too old.  If the reply gets older than reptimeout, we
     * remove it
     */
    _reply_cache = new Cache(1000);
    /*
     * Here we set the timeout mechanisms.  There is a default
     * value, but this is now dynamic based on the observed
     * RTT of the network
     */
    //resend the request after 5 seconds.
    _edge_reqtimeout = new TimeSpan(0,0,0,0,5000);
    _nonedge_reqtimeout = new TimeSpan(0,0,0,0,5000);
    //Start with 50 sec timeout
    _acked_reqtimeout = new TimeSpan(0,0,0,0,50000);
    //Here we track the statistics to improve the timeouts:
    _nonedge_rtt_stats = new TimeStats(_nonedge_reqtimeout.TotalMilliseconds, 0.98);
    _edge_rtt_stats = new TimeStats(_edge_reqtimeout.TotalMilliseconds, 0.98);
    _acked_rtt_stats = new TimeStats(_acked_reqtimeout.TotalMilliseconds, 0.98);
    _last_check = DateTime.UtcNow;
  }
Пример #13
0
  public ReqrepManager(string info, PType prefix) {
    lock( _inst_tab_sync ) {
      _instance_table.Replace(info, this);
    }
    _info = info;

    _prefix = prefix;
    _req_handler_table = new Hashtable();
    Random r = new Random();
    //Don't use negative numbers:
    _req_state_table = new UidGenerator<RequestState>(r, true);
    //Don't use negative numbers:
    _reply_id_table = new UidGenerator<ReplyState>(r, true);
    _rep_handler_table = new Hashtable();

    /**
     * We keep a list of the most recent 1000 replies until they
     * get too old.  If the reply gets older than reptimeout, we
     * remove it
     */
    _reply_cache = new Cache(1000);
    _reply_cache.EvictionEvent += HandleReplyCacheEviction;
    /*
     * Here we set the timeout mechanisms.  There is a default
     * value, but this is now dynamic based on the observed
     * RTT of the network
     */
    //resend the request after 5 seconds.
    _edge_reqtimeout = new TimeSpan(0,0,0,0,5000);
    _nonedge_reqtimeout = new TimeSpan(0,0,0,0,5000);
    //Start with 50 sec timeout
    _acked_reqtimeout = new TimeSpan(0,0,0,0,50000);
    //Here we track the statistics to improve the timeouts:
    _nonedge_rtt_stats = new TimeStats(_nonedge_reqtimeout.TotalMilliseconds, 0.98);
    _edge_rtt_stats = new TimeStats(_edge_reqtimeout.TotalMilliseconds, 0.98);
    _acked_rtt_stats = new TimeStats(_acked_reqtimeout.TotalMilliseconds, 0.98);
    _last_check = DateTime.UtcNow;
  }
Пример #14
0
        void RunLoop()
        {
            Log.Info(Strings.Device_Polling_Loop_Started);

            var stream = DeviceStream;
            var cancel = Cancellation.Token;

            var responseWaitInterval = TimeSpan.FromMilliseconds(PollingInterval.TotalMilliseconds * 0.9);

            var responseBuffer = new byte[stream.MaxResponseSize];

            var  timeStats           = new TimeStats();
            uint failedAttemptsCount = 0;

            while (!cancel.IsCancellationRequested && failedAttemptsCount < MaxReadAttempts)
            {
                try
                {
                    timeStats.BeginSample();

                    // Send request
                    stream.Write(RequestBytes, 0, RequestBytes.Length);

                    // Receive response
                    bool succeed = stream.Read(responseBuffer, 0, responseBuffer.Length, responseWaitInterval, cancel);

                    var timeElapsed = timeStats.EndSample();

                    // Log failed attempts
                    if (!succeed)
                    {
                        ++failedAttemptsCount;
                        continue;
                    }

                    // Dispatch response
                    EnqueueEvent(DataReceived, new DataSampleArgs(SampleParse(DateTimeOffset.Now, responseBuffer)));
                    failedAttemptsCount = 0;

                    // Wait before next request if needed
                    if (timeElapsed < PollingInterval)
                    {
                        cancel.WaitHandle.WaitOne(PollingInterval - timeElapsed);
                        Debug.WriteLine("Device polling waited {0:c}", PollingInterval - timeElapsed);
                    }
                }
                catch (OperationCanceledException)
                {
                    // This must be caused by StopPolling() and/or Dispose() call
                    Contract.Assert(cancel.IsCancellationRequested);
                    break;
                }
                catch (ObjectDisposedException)
                {
                    // This must be caused by StopPolling() and/or Dispose() call
                    Contract.Assert(cancel.IsCancellationRequested);
                    break;
                }
                catch (Exception exception)
                {
                    Log.ExceptionError(exception, Errors.Data_Request_Failed);
                    break;
                }
            }

            // Stopped after MaxReadAttempts consequent failed requests
            if (MaxReadAttempts == failedAttemptsCount)
            {
                Log.WarningFormat(Errors.Too_Many_Failed_Attempts_1, failedAttemptsCount, responseWaitInterval);
            }

            // Notify of this Stop event
            Log.Info(Strings.Device_Polling_Loop_Stopped);

            // Log time stats if needed
            if (timeStats.Avg > PollingInterval)
            {
                Log.Warning(
                    string.Concat(
                        string.Format(Strings.Device_Too_Slow_0, PollingInterval),
                        Environment.NewLine,
                        timeStats.ToString(Environment.NewLine, "g")
                        )
                    );
            }

            // Final cleanup
            Dispose();
        }