| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.ServiceModel.Channels
- {
- using System.Diagnostics;
- using System.Runtime;
- using System.ServiceModel;
- using System.ServiceModel.Description;
- using System.Threading;
- class UtilityExtension : IExtension<IPeerNeighbor>
- {
- uint linkUtility;
- uint updateCount;
- IOThreadTimer ackTimer;
- const uint linkUtilityIncrement = 128;
- const uint maxLinkUtility = 4096;
- int outTotal;
- uint inTotal;
- uint inUseful;
- IPeerNeighbor owner;
- object thisLock = new object();
- object throttleLock = new object();
- public event EventHandler UtilityInfoReceived;
- public event EventHandler UtilityInfoSent;
- TypedMessageConverter messageConverter;
- public const int AcceptableMissDistance = 2;
- int pendingSends = 0;
- int checkPointPendingSends = 0;
- bool isMonitoring = false;
- int expectedClearance;
- IOThreadTimer pruneTimer;
- const int PruneIntervalMilliseconds = 10000;
- TimeSpan pruneInterval;
- const int MinimumPendingMessages = 8;
- public delegate void PruneNeighborCallback(IPeerNeighbor peer);
- PruneNeighborCallback pruneNeighbor;
- UtilityExtension()
- {
- ackTimer = new IOThreadTimer(new Action<object>(AcknowledgeLoop), null, false);
- pendingSends = 0;
- pruneTimer = new IOThreadTimer(new Action<object>(VerifyCheckPoint), null, false);
- pruneInterval = TimeSpan.FromMilliseconds(PruneIntervalMilliseconds + new Random(Process.GetCurrentProcess().Id).Next(PruneIntervalMilliseconds));
- }
- public bool IsAccurate
- {
- get { return updateCount >= 32; }
- }
- public uint LinkUtility
- {
- get
- {
- return linkUtility;
- }
- }
- internal TypedMessageConverter MessageConverter
- {
- get
- {
- if (messageConverter == null)
- {
- messageConverter = TypedMessageConverter.Create(typeof(UtilityInfo), PeerStrings.LinkUtilityAction);
- }
- return messageConverter;
- }
- }
- public void Attach(IPeerNeighbor host)
- {
- this.owner = host;
- ackTimer.Set(PeerTransportConstants.AckTimeout);
- }
- static public void OnNeighborConnected(IPeerNeighbor neighbor)
- {
- Fx.Assert(neighbor != null, "Neighbor must have a value");
- neighbor.Extensions.Add(new UtilityExtension());
- }
- static public void OnNeighborClosed(IPeerNeighbor neighbor)
- {
- Fx.Assert(neighbor != null, "Neighbor must have a value");
- UtilityExtension ext = neighbor.Extensions.Find<UtilityExtension>();
- if (ext != null) neighbor.Extensions.Remove(ext);
- }
- public void Detach(IPeerNeighbor host)
- {
- ackTimer.Cancel();
- owner = null;
- lock (throttleLock)
- {
- pruneTimer.Cancel();
- }
- }
- public object ThisLock
- {
- get
- {
- return thisLock;
- }
- }
- public static void OnMessageSent(IPeerNeighbor neighbor)
- {
- UtilityExtension ext = neighbor.Extensions.Find<UtilityExtension>();
- if (ext != null) ext.OnMessageSent();
- }
- void OnMessageSent()
- {
- lock (ThisLock)
- {
- outTotal++;
- }
- Interlocked.Increment(ref pendingSends);
- }
- public static void OnEndSend(IPeerNeighbor neighbor, FloodAsyncResult fresult)
- {
- if (neighbor.State >= PeerNeighborState.Disconnecting)
- return;
- UtilityExtension instance = neighbor.Utility;
- if (instance == null)
- return;
- instance.OnEndSend(fresult);
- }
- public void OnEndSend(FloodAsyncResult fresult)
- {
- Interlocked.Decrement(ref pendingSends);
- }
- void AcknowledgeLoop(object state)
- {
- IPeerNeighbor peer = owner;
- if (peer == null || !peer.IsConnected)
- return;
- FlushAcknowledge();
- if (owner != null)
- ackTimer.Set(PeerTransportConstants.AckTimeout);
- }
- static public void ProcessLinkUtility(IPeerNeighbor neighbor, UtilityInfo umessage)
- {
- Fx.Assert(neighbor != null, "Neighbor must have a value");
- UtilityExtension ext = neighbor.Extensions.Find<UtilityExtension>();
- if (ext != null)
- {
- ext.ProcessLinkUtility(umessage.Useful, umessage.Total);
- }
- }
- // Update link utility for the neighbor. received from the neighbor
- void ProcessLinkUtility(uint useful, uint total)
- {
- uint i = 0;
- lock (ThisLock)
- {
- if (total > PeerTransportConstants.AckWindow
- || useful > total
- || (uint)outTotal < total
- )
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.PeerLinkUtilityInvalidValues, useful, total)));
- }
- //VERIFY with in this range, we are hoping that the order of useful/useless messages doesnt matter much.
- for (i = 0; i < useful; i++)
- {
- this.linkUtility = Calculate(this.linkUtility, true);
- }
- for (; i < total; i++)
- {
- this.linkUtility = Calculate(this.linkUtility, false);
- }
- outTotal -= (int)total;
- }
- if (UtilityInfoReceived != null)
- {
- UtilityInfoReceived(this, EventArgs.Empty);
- }
- }
- uint Calculate(uint current, bool increase)
- {
- uint utility = 0;
- // Refer to graph maintenance white paper for explanation of the formula
- // used to compute utility index.
- utility = (uint)current * 31 / 32;
- if (increase)
- utility += linkUtilityIncrement;
- if (!(utility <= maxLinkUtility))
- {
- throw Fx.AssertAndThrow("Link utility should not exceed " + maxLinkUtility);
- }
- if (!IsAccurate)
- ++updateCount;
- return utility;
- }
- public static uint UpdateLinkUtility(IPeerNeighbor neighbor, bool useful)
- {
- Fx.Assert(neighbor != null, "Neighbor must have a value");
- uint linkUtility = 0;
- UtilityExtension ext = neighbor.Extensions.Find<UtilityExtension>();
- if (ext != null)
- {
- // Can happen if the neighbor has been closed for instance
- linkUtility = ext.UpdateLinkUtility(useful);
- }
- return linkUtility;
- }
- public uint UpdateLinkUtility(bool useful)
- {
- lock (ThisLock)
- {
- inTotal++;
- if (useful)
- inUseful++;
- linkUtility = Calculate(linkUtility, useful);
- if (inTotal == PeerTransportConstants.AckWindow)
- {
- FlushAcknowledge();
- }
- }
- return linkUtility;
- }
- public void FlushAcknowledge()
- {
- if (inTotal == 0)
- return;
- uint tempUseful = 0, tempTotal = 0;
- lock (ThisLock)
- {
- tempUseful = inUseful;
- tempTotal = inTotal;
- inUseful = 0;
- inTotal = 0;
- }
- SendUtilityMessage(tempUseful, tempTotal);
- }
- class AsyncUtilityState
- {
- public Message message;
- public UtilityInfo info;
- public AsyncUtilityState(Message message, UtilityInfo info)
- {
- this.message = message;
- this.info = info;
- }
- }
- void SendUtilityMessage(uint useful, uint total)
- {
- IPeerNeighbor host = owner;
- if (host == null || !PeerNeighborStateHelper.IsConnected(host.State) || total == 0)
- return;
- UtilityInfo umessage = new UtilityInfo(useful, total);
- IAsyncResult result = null;
- Message message = MessageConverter.ToMessage(umessage, MessageVersion.Soap12WSAddressing10);
- bool fatal = false;
- try
- {
- result = host.BeginSend(message, Fx.ThunkCallback(new AsyncCallback(UtilityMessageSent)), new AsyncUtilityState(message, umessage));
- if (result.CompletedSynchronously)
- {
- host.EndSend(result);
- EventHandler handler = UtilityInfoSent;
- if (handler != null)
- handler(this, EventArgs.Empty);
- }
- }
- catch (Exception e)
- {
- if (Fx.IsFatal(e))
- {
- fatal = true;
- throw;
- }
- if (null != HandleSendException(host, e, umessage))
- throw;
- DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
- }
- finally
- {
- if (!fatal && (result == null || result.CompletedSynchronously))
- message.Close();
- }
- }
- void UtilityMessageSent(IAsyncResult result)
- {
- if (result == null || result.AsyncState == null)
- return;
- IPeerNeighbor host = this.owner;
- if (host == null || !PeerNeighborStateHelper.IsConnected(host.State))
- return;
- if (result.CompletedSynchronously)
- return;
- AsyncUtilityState state = (AsyncUtilityState)result.AsyncState;
- Fx.Assert(state != null, "IAsyncResult.AsyncState does not contain AsyncUtilityState");
- Message message = state.message;
- UtilityInfo umessage = state.info;
- bool fatal = false;
- if (!(umessage != null))
- {
- throw Fx.AssertAndThrow("expecting a UtilityInfo message in the AsyncState!");
- }
- try
- {
- host.EndSend(result);
- }
- catch (Exception e)
- {
- if (Fx.IsFatal(e))
- {
- fatal = true;
- throw;
- }
- if (null != HandleSendException(host, e, umessage))
- throw;
- DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
- }
- finally
- {
- if (!fatal)
- {
- Fx.Assert(!result.CompletedSynchronously, "result.CompletedSynchronously");
- message.Close();
- }
- }
- EventHandler handler = UtilityInfoSent;
- if (handler != null)
- handler(this, EventArgs.Empty);
- }
- Exception HandleSendException(IPeerNeighbor host, Exception e, UtilityInfo umessage)
- {
- if ((e is ObjectDisposedException) ||
- (e is TimeoutException) ||
- (e is CommunicationException))
- {
- if (!(!(e.InnerException is QuotaExceededException)))
- {
- throw Fx.AssertAndThrow("insufficient quota for sending messages!");
- }
- lock (ThisLock)
- {
- this.inTotal += umessage.Total;
- this.inUseful += umessage.Useful;
- }
- return null;
- }
- else
- {
- return e;
- }
- }
- static internal void ReportCacheMiss(IPeerNeighbor neighbor, int missedBy)
- {
- Fx.Assert(missedBy > AcceptableMissDistance, "Call this method for cache misses ONLY!");
- Fx.Assert(neighbor != null, "Neighbor must have a value");
- if (!neighbor.IsConnected)
- return;
- UtilityExtension ext = neighbor.Extensions.Find<UtilityExtension>();
- if (ext != null)
- {
- ext.ReportCacheMiss(missedBy);
- }
- }
- void ReportCacheMiss(int missedBy)
- {
- lock (ThisLock)
- {
- for (int i = 0; i < missedBy; i++)
- {
- this.linkUtility = Calculate(this.linkUtility, false);
- }
- }
- }
- public int PendingMessages
- {
- get
- {
- return this.pendingSends;
- }
- }
- public void BeginCheckPoint(PruneNeighborCallback pruneCallback)
- {
- if (this.isMonitoring)
- return;
- lock (throttleLock)
- {
- if (this.isMonitoring)
- return;
- this.checkPointPendingSends = this.pendingSends;
- this.pruneNeighbor = pruneCallback;
- this.expectedClearance = this.pendingSends / 2;
- this.isMonitoring = true;
- if (owner == null)
- return;
- pruneTimer.Set(pruneInterval);
- }
- }
- void VerifyCheckPoint(object state)
- {
- int lclPendingSends;
- int lclCheckPointPendingSends;
- IPeerNeighbor peer = (IPeerNeighbor)owner;
- if (peer == null || !peer.IsConnected)
- return;
- lock (throttleLock)
- {
- lclPendingSends = this.pendingSends;
- lclCheckPointPendingSends = this.checkPointPendingSends;
- }
- if (lclPendingSends <= MinimumPendingMessages)
- {
- lock (throttleLock)
- {
- isMonitoring = false;
- }
- }
- else if (lclPendingSends + this.expectedClearance >= lclCheckPointPendingSends)
- {
- pruneNeighbor(peer);
- }
- else
- {
- lock (throttleLock)
- {
- if (owner == null)
- return;
- this.checkPointPendingSends = this.pendingSends;
- this.expectedClearance = this.expectedClearance / 2;
- pruneTimer.Set(pruneInterval);
- }
- }
- }
- }
- }
|