public void VerifyTransportExceptionToResponseMessage()
        {
            string errorMessage         = "Test Exception!";
            DocumentClientException dce = null;
            TransportException      transportException = new TransportException(
                errorCode: TransportErrorCode.ConnectionBroken,
                innerException: null,
                activityId: Guid.NewGuid(),
                requestUri: new Uri("https://localhost"),
                sourceDescription: "The SourceDescription",
                userPayload: true,
                payloadSent: true);

            try
            {
                throw new ServiceUnavailableException(
                          message: errorMessage,
                          innerException: transportException);
            }
            catch (DocumentClientException exception)
            {
                dce = exception;
            }

            ResponseMessage responseMessage = dce.ToCosmosResponseMessage(null);

            Assert.IsFalse(responseMessage.IsSuccessStatusCode);
            Assert.AreEqual(HttpStatusCode.ServiceUnavailable, responseMessage.StatusCode);
            Assert.IsTrue(responseMessage.ErrorMessage.Contains(errorMessage));
            Assert.IsFalse(responseMessage.ErrorMessage.Contains(transportException.ToString()), "InnerException tracked in Diagnostics");
        }
        public void Failure(TransportException exception)
        {
            if (OnFailureHandler != null)
            {
                OnFailureHandler(exception);
            }

            if (FinalHandler != null)
            {
                FinalHandler();
            }
        }
Exemplo n.º 3
0
        private static void ThrowTransportExceptionOnItemOperation(
            Uri physicalAddress,
            ResourceOperation resourceOperation,
            DocumentServiceRequest request)
        {
            if (request.ResourceType == ResourceType.Document)
            {
                TransportException transportException = new TransportException(
                    errorCode: TransportErrorCode.ConnectionBroken,
                    innerException: null,
                    activityId: Guid.NewGuid(),
                    requestUri: physicalAddress,
                    sourceDescription: "SourceDescription",
                    userPayload: true,
                    payloadSent: false);

                DocumentClientException documentClientException = new DocumentClientException(
                    message: "Exception",
                    innerException: transportException,
                    statusCode: System.Net.HttpStatusCode.Gone);
                IClientSideRequestStatistics requestStatistics = request.RequestContext.ClientRequestStatistics;
                requestStatistics.RecordResponse(
                    request,
                    new StoreResult(
                        storeResponse: null,
                        exception: documentClientException,
                        partitionKeyRangeId: "PkRange",
                        lsn: 42,
                        quorumAckedLsn: 4242,
                        requestCharge: 9000.42,
                        currentReplicaSetSize: 3,
                        currentWriteQuorum: 4,
                        isValid: true,
                        storePhysicalAddress: physicalAddress,
                        globalCommittedLSN: 2,
                        numberOfReadRegions: 1,
                        itemLSN: 5,
                        sessionToken: null,
                        usingLocalLSN: true,
                        activityId: Guid.NewGuid().ToString(),
                        backendRequestDurationInMs: "0",
                        retryAfterInMs: "42",
                        transportRequestStats: new TransportRequestStats()),
                    DateTime.MinValue,
                    DateTime.MaxValue);

                throw Documents.Rntbd.TransportExceptions.GetServiceUnavailableException(physicalAddress, Guid.NewGuid(),
                                                                                         transportException);
            }
        }
Exemplo n.º 4
0
        public void Serializable()
        {
            var e = new TransportException("A message.");
            var f = new BinaryFormatter();
            TransportException e2;

            using (var s = new MemoryStream())
            {
                f.Serialize(s, e);
                s.Seek(0, SeekOrigin.Begin);
                e2 = (TransportException)f.Deserialize(s);
            }

            Assert.Equal(e.Message, e2.Message);
        }
Exemplo n.º 5
0
 public virtual void FinishAccept()
 {
     if (_fd != null)
     {
         _acceptFd = null;
         try
         {
             _acceptFd = _fd.EndAccept(_result);
         }
         catch (SocketException ex)
         {
             _acceptError = new TransportException(ex);
         }
     }
 }
Exemplo n.º 6
0
        private static void ThrowTransportExceptionOnItemOperation(
            Uri physicalAddress,
            ResourceOperation resourceOperation,
            DocumentServiceRequest request,
            Guid activityId,
            string transportExceptionSourceDescription)
        {
            if (request.ResourceType == ResourceType.Document)
            {
                TransportException transportException = new TransportException(
                    errorCode: TransportErrorCode.RequestTimeout,
                    innerException: null,
                    activityId: activityId,
                    requestUri: physicalAddress,
                    sourceDescription: transportExceptionSourceDescription,
                    userPayload: true,
                    payloadSent: false);

                throw Documents.Rntbd.TransportExceptions.GetRequestTimeoutException(physicalAddress, Guid.NewGuid(),
                                                                                     transportException);
            }
        }
Exemplo n.º 7
0
            public void Visit(StoreResult storeResult)
            {
                this.jsonWriter.WriteObjectStart();

                this.jsonWriter.WriteFieldName(nameof(storeResult.ActivityId));
                this.WriteStringValueOrNull(storeResult.ActivityId);

                this.jsonWriter.WriteFieldName(nameof(storeResult.StatusCode));
                this.jsonWriter.WriteStringValue(storeResult.StatusCode.ToString());

                this.jsonWriter.WriteFieldName(nameof(storeResult.SubStatusCode));
                this.jsonWriter.WriteStringValue(storeResult.SubStatusCode.ToString());

                this.jsonWriter.WriteFieldName(nameof(storeResult.LSN));
                this.jsonWriter.WriteNumber64Value(storeResult.LSN);

                this.jsonWriter.WriteFieldName(nameof(storeResult.PartitionKeyRangeId));
                this.WriteStringValueOrNull(storeResult.PartitionKeyRangeId);

                this.jsonWriter.WriteFieldName(nameof(storeResult.GlobalCommittedLSN));
                this.jsonWriter.WriteNumber64Value(storeResult.GlobalCommittedLSN);

                this.jsonWriter.WriteFieldName(nameof(storeResult.ItemLSN));
                this.jsonWriter.WriteNumber64Value(storeResult.ItemLSN);

                this.jsonWriter.WriteFieldName(nameof(storeResult.UsingLocalLSN));
                this.jsonWriter.WriteBoolValue(storeResult.UsingLocalLSN);

                this.jsonWriter.WriteFieldName(nameof(storeResult.QuorumAckedLSN));
                this.jsonWriter.WriteNumber64Value(storeResult.QuorumAckedLSN);

                this.jsonWriter.WriteFieldName(nameof(storeResult.SessionToken));
                this.WriteStringValueOrNull(storeResult.SessionToken?.ConvertToString());

                this.jsonWriter.WriteFieldName(nameof(storeResult.CurrentWriteQuorum));
                this.jsonWriter.WriteNumber64Value(storeResult.CurrentWriteQuorum);

                this.jsonWriter.WriteFieldName(nameof(storeResult.CurrentReplicaSetSize));
                this.jsonWriter.WriteNumber64Value(storeResult.CurrentReplicaSetSize);

                this.jsonWriter.WriteFieldName(nameof(storeResult.NumberOfReadRegions));
                this.jsonWriter.WriteNumber64Value(storeResult.NumberOfReadRegions);

                this.jsonWriter.WriteFieldName(nameof(storeResult.IsClientCpuOverloaded));
                this.jsonWriter.WriteBoolValue(storeResult.IsClientCpuOverloaded);

                this.jsonWriter.WriteFieldName(nameof(storeResult.IsValid));
                this.jsonWriter.WriteBoolValue(storeResult.IsValid);

                this.jsonWriter.WriteFieldName(nameof(storeResult.StorePhysicalAddress));
                this.WriteStringValueOrNull(storeResult.StorePhysicalAddress?.ToString());

                this.jsonWriter.WriteFieldName(nameof(storeResult.RequestCharge));
                this.jsonWriter.WriteNumber64Value(storeResult.RequestCharge);

                this.jsonWriter.WriteFieldName("BELatencyInMs");
                this.WriteStringValueOrNull(storeResult.BackendRequestDurationInMs);

                this.jsonWriter.WriteFieldName("TransportException");
                TransportException transportException = storeResult.Exception?.InnerException as TransportException;

                this.WriteStringValueOrNull(transportException?.Message);

                this.jsonWriter.WriteObjectEnd();
            }
        private void DownloadObject(ProgressMonitor pm, AnyObjectId id)
        {
            if (_local.HasObject(id))
            {
                return;
            }

            while (true)
            {
                // Try a pack file we know about, but don't have yet. Odds are
                // that if it has this object, it has others related to it so
                // getting the pack is a good bet.
                //
                if (DownloadPackedObject(pm, id))
                {
                    return;
                }

                // Search for a loose object over all alternates, starting
                // from the one we last successfully located an object through.
                //
                string idStr     = id.Name;
                string subdir    = idStr.Slice(0, 2);
                string file      = idStr.Substring(2);
                string looseName = subdir + "/" + file;

                for (int i = _lastRemoteIdx; i < _remotes.Count; i++)
                {
                    if (DownloadLooseObject(id, looseName, _remotes[i]))
                    {
                        _lastRemoteIdx = i;
                        return;
                    }
                }

                for (int i = 0; i < _lastRemoteIdx; i++)
                {
                    if (DownloadLooseObject(id, looseName, _remotes[i]))
                    {
                        _lastRemoteIdx = i;
                        return;
                    }
                }

                // Try to obtain more pack information and search those.
                //
                while (_noPacksYet.Count > 0)
                {
                    WalkRemoteObjectDatabase wrr = _noPacksYet.First.Value;
                    _noPacksYet.RemoveFirst();
                    ICollection <string> packNameList;
                    try
                    {
                        pm.BeginTask("Listing packs", ProgressMonitor.UNKNOWN);
                        packNameList = wrr.getPackNames();
                    }
                    catch (IOException e)
                    {
                        // Try another repository.
                        //
                        RecordError(id, e);
                        continue;
                    }
                    finally
                    {
                        pm.EndTask();
                    }

                    if (packNameList == null || packNameList.Count == 0)
                    {
                        continue;
                    }
                    foreach (string packName in packNameList)
                    {
                        bool contains = _packsConsidered.Contains(packName);
                        _packsConsidered.Add(packName);
                        if (!contains)
                        {
                            _unfetchedPacks.AddLast(new RemotePack(_lockMessage, _packLocks, _objCheck, _local, wrr, packName));
                        }
                    }
                    if (DownloadPackedObject(pm, id))
                    {
                        return;
                    }
                }

                // Try to expand the first alternate we haven't expanded yet.
                //
                ICollection <WalkRemoteObjectDatabase> al = ExpandOneAlternate(id, pm);
                if (al != null && al.Count > 0)
                {
                    foreach (WalkRemoteObjectDatabase alt in al)
                    {
                        _remotes.Add(alt);
                        _noPacksYet.AddLast(alt);
                        _noAlternatesYet.AddLast(alt);
                    }
                    continue;
                }

                // We could not obtain the object. There may be reasons why.
                //
                List <Exception> failures = _fetchErrors.get(id.Copy());

                var te = new TransportException("Cannot get " + id.Name + ".");

                if (failures != null && failures.Count > 0)
                {
                    te = failures.Count == 1 ?
                         new TransportException("Cannot get " + id.Name + ".", failures[0]) :
                         new TransportException("Cannot get " + id.Name + ".", new CompoundException(failures));
                }

                throw te;
            }
        }
Exemplo n.º 9
0
 /// <exception cref="NGit.Errors.TransportException"></exception>
 private void DownloadObject(ProgressMonitor pm, AnyObjectId id)
 {
     if (AlreadyHave(id))
     {
         return;
     }
     for (; ;)
     {
         // Try a pack file we know about, but don't have yet. Odds are
         // that if it has this object, it has others related to it so
         // getting the pack is a good bet.
         //
         if (DownloadPackedObject(pm, id))
         {
             return;
         }
         // Search for a loose object over all alternates, starting
         // from the one we last successfully located an object through.
         //
         string idStr     = id.Name;
         string subdir    = Sharpen.Runtime.Substring(idStr, 0, 2);
         string file      = Sharpen.Runtime.Substring(idStr, 2);
         string looseName = subdir + "/" + file;
         for (int i = lastRemoteIdx; i < remotes.Count; i++)
         {
             if (DownloadLooseObject(id, looseName, remotes[i]))
             {
                 lastRemoteIdx = i;
                 return;
             }
         }
         for (int i_1 = 0; i_1 < lastRemoteIdx; i_1++)
         {
             if (DownloadLooseObject(id, looseName, remotes[i_1]))
             {
                 lastRemoteIdx = i_1;
                 return;
             }
         }
         // Try to obtain more pack information and search those.
         //
         while (!noPacksYet.IsEmpty())
         {
             WalkRemoteObjectDatabase wrr = noPacksYet.RemoveFirst();
             ICollection <string>     packNameList;
             try
             {
                 pm.BeginTask("Listing packs", ProgressMonitor.UNKNOWN);
                 packNameList = wrr.GetPackNames();
             }
             catch (IOException e)
             {
                 // Try another repository.
                 //
                 RecordError(id, e);
                 continue;
             }
             finally
             {
                 pm.EndTask();
             }
             if (packNameList == null || packNameList.IsEmpty())
             {
                 continue;
             }
             foreach (string packName in packNameList)
             {
                 if (packsConsidered.AddItem(packName))
                 {
                     unfetchedPacks.AddItem(new WalkFetchConnection.RemotePack(this, wrr, packName));
                 }
             }
             if (DownloadPackedObject(pm, id))
             {
                 return;
             }
         }
         // Try to expand the first alternate we haven't expanded yet.
         //
         ICollection <WalkRemoteObjectDatabase> al = ExpandOneAlternate(id, pm);
         if (al != null && !al.IsEmpty())
         {
             foreach (WalkRemoteObjectDatabase alt in al)
             {
                 remotes.AddItem(alt);
                 noPacksYet.AddItem(alt);
                 noAlternatesYet.AddItem(alt);
             }
             continue;
         }
         // We could not obtain the object. There may be reasons why.
         //
         IList <Exception>  failures = fetchErrors.Get((ObjectId)id);
         TransportException te;
         te = new TransportException(MessageFormat.Format(JGitText.Get().cannotGet, id.Name
                                                          ));
         if (failures != null && !failures.IsEmpty())
         {
             if (failures.Count == 1)
             {
                 Sharpen.Extensions.InitCause(te, failures[0]);
             }
             else
             {
                 Sharpen.Extensions.InitCause(te, new CompoundException(failures));
             }
         }
         throw te;
     }
 }
Exemplo n.º 10
0
        private void DownloadObject(ProgressMonitor pm, AnyObjectId id)
        {
            if (_local.HasObject(id))
            {
                return;
            }

            while (true)
            {
                if (DownloadPackedObject(pm, id))
                {
                    return;
                }

                string idStr     = id.Name;
                string subdir    = idStr.Slice(0, 2);
                string file      = idStr.Substring(2);
                string looseName = subdir + "/" + file;

                for (int i = _lastRemoteIdx; i < _remotes.Count; i++)
                {
                    if (DownloadLooseObject(id, looseName, _remotes[i]))
                    {
                        _lastRemoteIdx = i;
                        return;
                    }
                }

                for (int i = 0; i < _lastRemoteIdx; i++)
                {
                    if (DownloadLooseObject(id, looseName, _remotes[i]))
                    {
                        _lastRemoteIdx = i;
                        return;
                    }
                }

                while (_noPacksYet.Count > 0)
                {
                    WalkRemoteObjectDatabase wrr = _noPacksYet.First.Value;
                    _noPacksYet.RemoveFirst();
                    List <string> packNameList;
                    try
                    {
                        pm.BeginTask("Listing packs", ProgressMonitor.UNKNOWN);
                        packNameList = wrr.getPackNames();
                    }
                    catch (IOException e)
                    {
                        RecordError(id, e);
                        continue;
                    }
                    finally
                    {
                        pm.EndTask();
                    }

                    if (packNameList == null || packNameList.Count == 0)
                    {
                        continue;
                    }
                    foreach (string packName in packNameList)
                    {
                        if (!_packsConsidered.Contains(packName))
                        {
                            _packsConsidered.Add(packName);
                            _unfetchedPacks.AddLast(new RemotePack(_lockMessage, _packLocks, _objCheck, _local, wrr, packName));
                        }
                    }
                    if (DownloadPackedObject(pm, id))
                    {
                        return;
                    }
                }

                List <WalkRemoteObjectDatabase> al = ExpandOneAlternate(id, pm);
                if (al != null && al.Count > 0)
                {
                    foreach (WalkRemoteObjectDatabase alt in al)
                    {
                        _remotes.Add(alt);
                        _noPacksYet.AddLast(alt);
                        _noAlternatesYet.AddLast(alt);
                    }
                    continue;
                }

                List <Exception> failures = null;
                if (_fetchErrors.ContainsKey(id.Copy()))
                {
                    failures = _fetchErrors[id.Copy()];
                }

                TransportException te = null;
                if (failures != null && failures.Count > 0)
                {
                    te = failures.Count == 1 ?
                         new TransportException("Cannot get " + id.Name + ".", failures[0]) :
                         new TransportException("Cannot get " + id.Name + ".", new CompoundException(failures));
                }

                if (te == null)
                {
                    te = new TransportException("Cannot get " + id.Name + ".");
                }

                throw te;
            }
        }