Exemplo n.º 1
0
        private async Task <Stream> GetStreamAsync(HttpClient client, Uri requestUri)
        {
            var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                if (response.Content != null)
                {
                    throw new StreamChannelException(JsonSerializationHelper.DeserializeObject <ErrorDetails>(await response.Content.ReadAsByteArrayAsync()));
                }
            }

            response.EnsureSuccessStatusCode();
            var c = response.Content;

            return(c != null ? await c.ReadAsStreamAsync().ConfigureAwait(false) : Stream.Null);
        }
        private async Task PerformKvsOperation(Func <List <UpgradeServiceNodeState>, bool> operation, TimeSpan timeout, CancellationToken cancellationToken)
        {
            Trace.WriteNoise(TraceType, "PerformKvsOperation: Begin.");
            using (var tx = this.kvsStore.CreateTransaction())
            {
                bool shouldAdd = false;
                List <UpgradeServiceNodeState> upgradeServiceNodeStates = new List <UpgradeServiceNodeState>();

                var item = this.kvsStore.TryGet(tx, StoreKey);
                if (item == null)
                {
                    shouldAdd = true;
                }
                else
                {
                    Trace.WriteNoise(TraceType, "PerformKvsOperation: Invoking JsonSerializationHelper.DeserializeObject<List<UpgradeServiceNodeState>>");
                    upgradeServiceNodeStates = JsonSerializationHelper.DeserializeObject <List <UpgradeServiceNodeState> >(item.Value);
                    Trace.WriteNoise(TraceType, "PerformKvsOperation: Completed JsonSerializationHelper.DeserializeObject<List<UpgradeServiceNodeState>>");
                }

                bool shouldUpdate = operation(upgradeServiceNodeStates);

                if (shouldAdd || shouldUpdate)
                {
                    var serializedState = JsonSerializationHelper.SerializeObject(upgradeServiceNodeStates);

                    if (shouldAdd)
                    {
                        Trace.WriteNoise(TraceType, "PerformKvsOperation: Invoking this.kvsStore.Add");
                        this.kvsStore.Add(tx, StoreKey, serializedState);
                        Trace.WriteNoise(TraceType, "PerformKvsOperation: Completed this.kvsStore.Add");
                    }
                    else
                    {
                        Trace.WriteNoise(TraceType, "PerformKvsOperation: Invoking this.kvsStore.Update");
                        this.kvsStore.Update(tx, StoreKey, serializedState, item.Metadata.SequenceNumber);
                        Trace.WriteNoise(TraceType, "PerformKvsOperation: Completed this.kvsStore.Update");
                    }
                    Trace.WriteNoise(TraceType, "PerformKvsOperation: Invoking tx.CommitAsync");
                    await tx.CommitAsync(timeout);

                    Trace.WriteNoise(TraceType, "PerformKvsOperation: Completed tx.CommitAsync");
                }
            }
            Trace.WriteNoise(TraceType, "PerformKvsOperation: End.");
        }
        internal async Task FilterNodeTypesAsync(IEnumerable <string> primaryNodeTypes, TimeSpan timeout, CancellationToken cancellationToken)
        {
            using (var tx = this.kvsStore.CreateTransaction())
            {
                var item = this.kvsStore.TryGet(tx, StoreKey);
                if (item == null)
                {
                    return;
                }

                var nodeStates = JsonSerializationHelper.DeserializeObject <List <UpgradeServiceNodeState> >(item.Value);
                if (nodeStates == null || !nodeStates.Any())
                {
                    return;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var filtered = new List <UpgradeServiceNodeState>();
                foreach (var nodeState in nodeStates)
                {
                    if (primaryNodeTypes.Contains(nodeState.Node.NodeType))
                    {
                        filtered.Add(nodeState);
                    }
                }

                var serializedState = JsonSerializationHelper.SerializeObject(filtered);

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                this.kvsStore.Update(tx, StoreKey, serializedState);

                await tx.CommitAsync(timeout);
            }
        }
Exemplo n.º 4
0
        private async Task ProcessStreamChannelRequestAsync(Stream stream, CancellationToken cancellationToken)
        {
            try
            {
                using (var reader = new WrpStreamReader(stream, cancellationToken))
                {
                    reader.SetReadTimeout((int)this.defaultTimeout.TotalMilliseconds);

                    while (!await reader.IsEndOfStream() && !cancellationToken.IsCancellationRequested)
                    {
                        var lengthArrary = new char[4];
                        await reader.ReadAsync(lengthArrary, 0, 4);

                        var length     = (lengthArrary[3] << 24) + (lengthArrary[2] << 16) + (lengthArrary[1] << 8) + lengthArrary[0];
                        var readBuffer = new char[length];
                        var readLength = 0;
                        if ((readLength = await reader.ReadAsync(readBuffer, 0, readBuffer.Length)) == length)
                        {
                            var streamRequestString = Encoding.UTF8.GetBytes(readBuffer);
                            var streamRequest       = JsonSerializationHelper.DeserializeObject <StreamRequest>(
                                streamRequestString,
                                JsonSerializationHelper.DefaultJsonSerializerSettings);

                            if (streamRequest.PingRequest)
                            {
                                Trace.WriteNoise(TraceType, "WrpStreamChannel: Received a ping request");
                                continue;
                            }

                            this.ProcessEachStreamRequestAsync(streamRequest, cancellationToken);
                        }
                        else
                        {
                            Trace.WriteError(TraceType, "Expected read length is {0}, but actually read length is {1}, close the stream now .", length, readLength);

                            // something corrupt there, need to exit to avoid further corruption
                            return;
                        }
                    }

                    Trace.WriteInfo(TraceType, "WrpStreamChannel: Reached the end of the stream");
                }
            }
            catch (Exception e)
            {
                Trace.WriteWarning(TraceType, "WrpStreamChannel: ProcessStreamChannelRequest thrown {0}", e);

                while (e.InnerException != null)
                {
                    e = e.InnerException;
                }

                if (e is WebException || e is HttpRequestException)
                {
                    var webException = (WebException)e;
                    if (webException.Status == WebExceptionStatus.Timeout)
                    {
                        Trace.WriteWarning(TraceType, "WrpStreamChannel:Client timeout");
                    }
                }
                else
                {
                    throw;
                }
            }
        }