public void CanTranscode()
        {
            using (var stream1 = new MemoryStream())
                using (var stream2 = new MemoryStream())
                    using (var writerStream1 = new WriterStream(stream1))
                        using (var writerStream2 = new WriterStream(stream2))
                        {
                            // write into compact binary protocol first
                            var writer = writerStream1.CreateCompactBinaryWriter();
                            writer.Write(this.testObject);
                            stream1.Position = 0;
                            using (var readerStream1 = ReaderStream.FromMemoryStreamBuffer(stream1, null))
                            {
                                var reader = readerStream1.CreateCompactBinaryReader();
                                // re-write as simple protocol
                                var writer2 = writerStream2.CreateSimpleBinaryWriter();
                                Transcode.FromTo(reader, writer2);
                                stream2.Position = 0;

                                using (var readerStream2 = new ReaderStream(stream2))
                                {
                                    var reader2       = readerStream2.CreateSimpleBinaryReader();
                                    var anotherObject = reader2.Read <AnotherDerivedThing>();

                                    Assert.IsTrue(Comparer.Equal(anotherObject, this.testObject));
                                }
                            }
                        }
        }
        public async Task StartExecuteReturnsOkAndAddsHandlerIfPayloadIsValid()
        {
            var message = new ServerRegistration
            {
                Hostname = "testServer",
                Port     = 867,
            };
            ByteArrayContent payload;

            using (var ms = new MemoryStream())
                using (var writerStream = new WriterStream(ms))
                {
                    writerStream.CreateCompactBinaryWriter().Write(message);
                    payload = new ByteArrayContent(ms.GetBuffer());
                }

            var response = await
                           this.httpClient.PostAsync(TestUtils.GetUri(this.server,
                                                                      RegistrationClient.RegistrationEndpoint,
                                                                      string.Empty),
                                                     payload);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsNotNull(this.server.ServerList["testServer"]);
        }
示例#3
0
        private async void StartRegistration()
        {
            IPAddress[] addresses;
            try
            {
                addresses =
                    await Task <IPAddress[]> .Factory.FromAsync(Dns.BeginGetHostAddresses, Dns.EndGetHostAddresses,
                                                                this.destinationHostname, null);
            }
            catch (SocketException e)
            {
                Events.Write.RegistrationDestinationResolutionFailed(this.destinationHostname, e.Message);
                return;
            }

            var serverRegistration = new ServerRegistration
            {
                Hostname        = this.sourceHostname,
                Port            = this.sourcePort,
                MachineFunction = this.sourceMachineFunction,
                Datacenter      = this.sourceDatacenter,
            };

            foreach (var counter in this.dataManager.Counters)
            {
                serverRegistration.Counters.Add(
                    new CounterInfo
                {
                    Name       = counter.Name,
                    Type       = counter.Type,
                    Dimensions = counter.Dimensions.ToList(),
                    StartTime  = counter.StartTime.ToMillisecondTimestamp(),
                    EndTime    = counter.EndTime.ToMillisecondTimestamp(),
                });
            }

            byte[] payload;
            using (var ms = new MemoryStream())
                using (var writerStream = new WriterStream(ms, this.dataManager.MemoryStreamManager))
                {
                    var writer = writerStream.CreateCompactBinaryWriter();
                    writer.Write(serverRegistration);
                    payload = ms.ToArray();
                }

            foreach (var address in addresses)
            {
                this.RegisterWithAddress(address, payload);
            }

            lock (this)
            {
                if (this.registrationTimer != null)
                {
                    this.registrationTimer.Change(this.registrationInterval, TimeSpan.Zero);
                }
            }
        }
示例#4
0
 private static ByteArrayContent GetRequestPayload(BatchQueryRequest data)
 {
     using (var ms = new MemoryStream())
         using (var writerStream = new WriterStream(ms))
         {
             writerStream.CreateCompactBinaryWriter().Write(data);
             return(new ByteArrayContent(ms.GetBuffer()));
         }
 }
        public void VerifyEmbeddedPolymorphicTypes <TStream>(Func <TStream> createWriter, Func <TStream, TStream> createReaderFromWriter) where TStream : Stream
        {
            const int secretValToUse = 0x12345;

            var listOfThings = new ListOfThings();

            this.testObject.secretVal = secretValToUse;
            listOfThings.list.Add(new Bonded <AnotherDerivedThing>(this.testObject));
            listOfThings.list.Add(new Bonded <DerivedThing>(new DerivedThing
            {
                anotherVal = 100,
                secretVal  = secretValToUse
            }));

            using (var writeStream = createWriter())
                using (var writerStream = new WriterStream(writeStream))
                {
                    var writer = writerStream.CreateCompactBinaryWriter();
                    writer.Write(listOfThings);

                    using (var readStream = createReaderFromWriter(writeStream))
                        using (var reader = new ReaderStream(readStream, null, false))
                        {
                            var cbReader = reader.CreateBondedCompactBinaryReader <ListOfThings>();
                            var newThing = cbReader.Deserialize <ListOfThings>();

                            Assert.IsNotNull(newThing);

                            var matchesFound = 0;

                            foreach (var thing in newThing.list)
                            {
                                var baseThing = thing.Deserialize();

                                var another = thing.TryDeserialize <Base, AnotherDerivedThing>();
                                var derived = thing.TryDeserialize <Base, DerivedThing>();

                                Assert.IsNotNull(baseThing);

                                if (another != null)
                                {
                                    Assert.IsNull(derived);
                                    Assert.IsTrue(Comparer.Equal <AnotherDerivedThing>(another, this.testObject));
                                    matchesFound++;
                                }

                                if (derived != null)
                                {
                                    Assert.IsNull(another);
                                    Assert.AreEqual(100, derived.anotherVal);
                                    matchesFound++;
                                }
                            }
                            Assert.AreEqual(2, matchesFound);
                        }
                }
        }
示例#6
0
        internal static HttpResponseMessage CreateResponse <T>(T response, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            var result = new HttpResponseMessage(statusCode);

            using (var tempStream = new MemoryStream())
            {
                using (var writerStream = new WriterStream(tempStream))
                {
                    writerStream.CreateCompactBinaryWriter().Write(response);
                    result.Content = new ByteArrayContent(tempStream.GetBuffer());
                }
            }
            return(result);
        }
        public void CanReadAndWriteCompactBinary()
        {
            using (var stream = new MemoryStream())
                using (var writerStream = new WriterStream(stream))
                {
                    var writer = writerStream.CreateCompactBinaryWriter();
                    writer.Write(this.testObject);

                    stream.Position = 0;
                    using (var reader = ReaderStream.FromMemoryStreamBuffer(stream, this.mgr))
                    {
                        var cbReader      = new CompactBinaryReader <ReaderStream>(reader);
                        var anotherObject = cbReader.Read <AnotherDerivedThing>();

                        Assert.IsTrue(Comparer.Equal(anotherObject, this.testObject));
                    }
                }
        }
        /// <summary>
        /// Clone the original request and replace the fanout information to be specific to this block
        /// </summary>
        private MemoryStream CreatePostBody <TRequestType>(TRequestType originalRequest, List <ServerInfo> blockSources)
            where TRequestType : TieredRequest, new()
        {
            var newRequest = (TRequestType)originalRequest.Clone();

            newRequest.FanoutTimeoutInMilliseconds = (originalRequest.FanoutTimeoutInMilliseconds * 9) / 10;
            //each  block gets 90% of the remaining timeout (arbitrarily)
            newRequest.Sources = blockSources;

            var requestStream = this.MemoryStreamManager.GetStream();

            using (var writer = new WriterStream(requestStream, this.MemoryStreamManager))
            {
                writer.CreateCompactBinaryWriter().Write(newRequest);
                requestStream.Position = 0;

                return(requestStream);
            }
        }
示例#9
0
        private void Write <TData>(TData data)
        {
            var acceptTypes = this.HttpRequest.AcceptTypes;

            if (acceptTypes != null && acceptTypes.Contains(Protocol.BondCompactBinaryMimeType, StringComparer.Ordinal))
            {
                this.ContentType = Protocol.BondCompactBinaryMimeType;
                using (var writerStream = new WriterStream(this.responseBody))
                {
                    var writer = writerStream.CreateCompactBinaryWriter();
                    writer.Write(data);
                }
            }
            else
            {
                this.ContentType = Protocol.ApplicationJsonMimeType;
                var writer = new SimpleJsonWriter(this.responseBody);
                writer.Write(data);
            }
        }
        public void CanReadWritePolymorphicTypes()
        {
            using (var stream = new MemoryStream())
                using (var writerStream = new WriterStream(stream))
                {
                    var writer = writerStream.CreateCompactBinaryWriter();
                    writer.Write(this.testObject);

                    stream.Position = 0;
                    using (var readerStream = new ReaderStream(stream.GetBuffer(), 0, stream.Length))
                    {
                        var reader = readerStream.CreateBondedCompactBinaryReader <Base>();

                        var baseThing    = reader.Deserialize <Base>();
                        var derivedThing = reader.Deserialize <AnotherDerivedThing>();
                        Assert.AreEqual(baseThing.secretVal, derivedThing.secretVal);
                        Assert.IsTrue(Comparer.Equal <AnotherDerivedThing>(derivedThing, this.testObject));
                    }
                }
        }
        public void CanReadAndWriteToFile()
        {
            var tempFile = Path.GetTempFileName();

            using (var createFileStream = File.Create(tempFile))
                using (var writerStream = new WriterStream(createFileStream))
                {
                    var writer = writerStream.CreateCompactBinaryWriter();
                    writer.Write(this.testObject);
                    createFileStream.Close();
                }

            using (var openFileStream = File.OpenRead(tempFile))
                using (var readerStream = new ReaderStream(openFileStream))
                {
                    var reader        = readerStream.CreateCompactBinaryReader();
                    var anotherObject = reader.Read <AnotherDerivedThing>();

                    Assert.IsTrue(Comparer.Equal(anotherObject, this.testObject));
                }

            File.Delete(tempFile);
        }
        private async Task <bool> QuerySources(IList <PersistedDataSource> sources)
        {
            Events.Write.BeginQuerySources(sources);

            var success         = false;
            var transferRequest =
                new TransferRequest
            {
                DataType  = PersistedDataProtocol.GetPersistedTypeCodeFromType(typeof(TInternal)),
                Timeout   = (this.Timeout.Seconds * 9) / 10,   // 90% of timeout goes to the child
                MaxFanout = this.MaxFanout,
                Sources   = new List <string>()
            };

            foreach (var source in sources)
            {
                source.Status = PersistedDataSourceStatus.Unknown;
                transferRequest.Sources.Add(source.Name);
            }

            var serverOffset   = this.randomNumberGenerator.Next(sources.Count);
            var selectedSource = sources[serverOffset];

            var request = new HttpRequestMessage(HttpMethod.Post, this.CreateRequestUri(selectedSource.Name));

            using (var ms = (this.memoryStreamManager.GetStream()))
                using (var writeStream = new WriterStream(ms, this.memoryStreamManager))
                {
                    var writer = writeStream.CreateCompactBinaryWriter();
                    writer.Write(transferRequest);
                    request.Content = new ByteArrayContent(ms.ToArray());
                }

            try
            {
                Events.Write.BeginSendSourceQuery(selectedSource, request.RequestUri);
                var response =
                    await this.httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

                Events.Write.EndSendSourceQuery(selectedSource, (int)response.StatusCode, response.ReasonPhrase);

                if (!response.IsSuccessStatusCode)
                {
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.NotFound:
                        foreach (var source in sources)
                        {
                            source.Status = PersistedDataSourceStatus.Unavailable;
                        }
                        return(true);

                    default:
                        return(false);
                    }
                }

                Events.Write.BeginReceiveSourceResponseBody(selectedSource);
                var length = response.Content.Headers.ContentLength ?? 0;
                using (var dataStream = this.memoryStreamManager.GetStream("PersistedDataAggregator/Http/Read",
                                                                           (int)length, true))
                    using (var responseStream = await response.Content.ReadAsStreamAsync())
                    {
                        responseStream.CopyTo(dataStream);
                        dataStream.Position = 0;
                        using (var dataReader =
                                   new PersistedDataReader(dataStream, this.memoryStreamManager, this.DimensionSet))
                        {
                            Events.Write.EndReceiveSourceResponseBody(selectedSource, (int)dataStream.Length);

                            success = true;
                            if (dataStream.Length == 0)
                            {
                                Events.Write.EmptyResponseReceivedFromSource(selectedSource);
                                // Over the line. Mark it zero. Or just available, but empty (which is okay!)
                                foreach (var source in sources)
                                {
                                    source.Status = PersistedDataSourceStatus.Available;
                                }
                            }
                            else
                            {
                                try
                                {
                                    this.UnpackResponse(dataReader);
                                }
                                catch (PersistedDataException ex)
                                {
                                    Events.Write.PersistedDataExceptionFromSource(selectedSource, ex);
                                    success = false;
                                }
                            }
                        }
                    }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                if (ex is HttpRequestException || ex is IOException || ex is WebException || ex is ObjectDisposedException)
                {
                    Events.Write.HttpExceptionFromSource(selectedSource, ex);
                }
                else
                {
                    throw;
                }
            }

            Events.Write.EndQuerySources(success, sources);
            return(success);
        }