Esempio n. 1
0
        public void AggregatorAggregatesCorrectlyWhenNoOverlaps()
        {
            var aCounter = new BatchCounterQuery {
                CounterName = "a", UserContext = "a"
            };
            var bCounter = new BatchCounterQuery {
                CounterName = "b", UserContext = "b"
            };

            var batchRequest = new BatchQueryRequest();

            batchRequest.Queries.Add(aCounter);
            batchRequest.Queries.Add(bCounter);

            var agg = new BatchResponseAggregator(batchRequest);

            var aOnlyResponse = new BatchQueryResponse
            {
                Responses      = { this.CreateHitCountResponse("a", 100) },
                RequestDetails =
                {
                    new RequestDetails
                    {
                        Server = new ServerInfo
                        {
                            Hostname = "something",
                            Port     = 42
                        },
                        HttpResponseCode = 200
                    }
                }
            };
            var bOnlyResponse = new BatchQueryResponse
            {
                Responses      = { this.CreateHitCountResponse("b", 200) },
                RequestDetails =
                {
                    new RequestDetails
                    {
                        Server = new ServerInfo
                        {
                            Hostname = "somewhere",
                            Port     = 42
                        },
                        HttpResponseCode = 200
                    }
                }
            };

            agg.AddResponse(aOnlyResponse);
            agg.AddResponse(bOnlyResponse);

            var finalResponse = agg.GetResponse();

            Assert.IsNotNull(finalResponse);
            Assert.AreEqual(2, finalResponse.RequestDetails.Count);
            Assert.AreEqual(2, finalResponse.Responses.Count);
            Assert.AreEqual(1, finalResponse.Responses.Count(x => x.UserContext.Equals("a") && x.Samples[0].HitCount == 100));
            Assert.AreEqual(1, finalResponse.Responses.Count(x => x.UserContext.Equals("b") && x.Samples[0].HitCount == 200));
        }
Esempio n. 2
0
        public void AggregatorAggregatesOverlapCorrectly()
        {
            var aCounter = new BatchCounterQuery {
                CounterName = "a", UserContext = "a"
            };

            var batchRequest = new BatchQueryRequest();

            batchRequest.Queries.Add(aCounter);

            var agg = new BatchResponseAggregator(batchRequest);

            var oneResponse = new BatchQueryResponse
            {
                Responses      = { this.CreateHitCountResponse("a", 100) },
                RequestDetails =
                {
                    new RequestDetails
                    {
                        Server = new ServerInfo
                        {
                            Hostname = "something",
                            Port     = 42
                        },
                        HttpResponseCode = 200
                    }
                }
            };
            var twoResponse = new BatchQueryResponse
            {
                Responses      = { this.CreateHitCountResponse("a", 200) },
                RequestDetails =
                {
                    new RequestDetails
                    {
                        Server = new ServerInfo
                        {
                            Hostname = "somewhere",
                            Port     = 42
                        },
                        HttpResponseCode = 200
                    }
                }
            };

            agg.AddResponse(oneResponse);
            agg.AddResponse(twoResponse);

            var finalResponse = agg.GetResponse();

            Assert.IsNotNull(finalResponse);
            Assert.AreEqual(2, finalResponse.RequestDetails.Count);
            Assert.AreEqual(1, finalResponse.Responses.Count);
            Assert.AreEqual(300, finalResponse.Responses[0].Samples[0].HitCount);
        }
Esempio n. 3
0
        /// <summary>
        /// Process a /batchQuery query sent with possible fanout
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private async Task <Response> ProcessQuery(Request request)
        {
            var query = await request.ReadInputBody <BatchQueryRequest>();

            if (query == null || query.Queries.Count == 0)
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid or empty request body."));
            }

            try
            {
                Task <BatchQueryResponse> distributedResponse;
                // kick off the distributed request if needed
                if (query.Sources != null && query.Sources.Count > 0)
                {
                    distributedResponse = this.server.CreateQueryClient(query).BatchQuery(query);
                }
                else if (this.server.EnableQueryAggregation && this.server.ServerList != null && this.server.ServerList.Any())
                {
                    // if we are an aggregation server, fanout to all servers
                    var fanoutRequest = (BatchQueryRequest)query.Clone();
                    fanoutRequest.Sources = this.server.ServerList.Servers;

                    distributedResponse = this.server.CreateQueryClient(fanoutRequest).BatchQuery(fanoutRequest);
                }
                else
                {
                    distributedResponse = null;
                }

                var localResponses = this.ProcessBatchQueryRequest(query);

                if (distributedResponse == null)
                {
                    await localResponses;
                    return(Response.Create(request, HttpStatusCode.OK, CreateQueryResponse(query, localResponses.Result)));
                }

                await Task.WhenAll(new Task[] { distributedResponse, localResponses });

                var merger = new BatchResponseAggregator(query);
                merger.AddResponse(distributedResponse.Result);
                merger.AddResponse(CreateQueryResponse(query, localResponses.Result));

                return(Response.Create(request, HttpStatusCode.OK, merger.GetResponse()));
            }
            catch (ArgumentException ex)
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
Esempio n. 4
0
        public void AggregatorIgnoresUnknownCounterInResponse()
        {
            var aCounter = new BatchCounterQuery {
                CounterName = "a", UserContext = "a"
            };

            var batchRequest = new BatchQueryRequest();

            batchRequest.Queries.Add(aCounter);

            var agg = new BatchResponseAggregator(batchRequest);

            var oneResponse = new BatchQueryResponse
            {
                Responses      = { this.CreateHitCountResponse("i am a key tree", 100) },
                RequestDetails =
                {
                    new RequestDetails
                    {
                        Server = new ServerInfo
                        {
                            Hostname = "something",
                        },
                        HttpResponseCode = 200
                    }
                }
            };

            agg.AddResponse(oneResponse);

            var finalResponse = agg.GetResponse();

            Assert.IsNotNull(finalResponse);
            Assert.AreEqual(1, finalResponse.Responses.Count);
            Assert.AreEqual(0, finalResponse.Responses[0].Samples.Count);
        }
        public void AggregatorAggregatesCorrectlyWhenNoOverlaps()
        {
            var aCounter = new BatchCounterQuery { CounterName = "a", UserContext = "a" };
            var bCounter = new BatchCounterQuery { CounterName = "b", UserContext = "b" };

            var batchRequest = new BatchQueryRequest();
            batchRequest.Queries.Add(aCounter);
            batchRequest.Queries.Add(bCounter);

            var agg = new BatchResponseAggregator(batchRequest);

            var aOnlyResponse = new BatchQueryResponse
                                {
                                    Responses = {this.CreateHitCountResponse("a", 100)},
                                    RequestDetails =
                                    {
                                        new RequestDetails
                                        {
                                            Server = new ServerInfo
                                            {
                                                Hostname = "something",
                                                Port = 42
                                            },
                                            HttpResponseCode = 200
                                        }
                                    }
                                };
            var bOnlyResponse = new BatchQueryResponse
                                {
                                    Responses = {this.CreateHitCountResponse("b", 200)},
                                    RequestDetails =
                                    {
                                        new RequestDetails
                                        {
                                            Server = new ServerInfo
                                            {
                                                Hostname = "somewhere",
                                                Port = 42
                                            },
                                            HttpResponseCode = 200
                                        }
                                    }
                                };

            agg.AddResponse(aOnlyResponse);
            agg.AddResponse(bOnlyResponse);

            var finalResponse = agg.GetResponse();
            Assert.IsNotNull(finalResponse);
            Assert.AreEqual(2, finalResponse.RequestDetails.Count);
            Assert.AreEqual(2, finalResponse.Responses.Count);
            Assert.AreEqual(1, finalResponse.Responses.Count(x => x.UserContext.Equals("a") && x.Samples[0].HitCount == 100));
            Assert.AreEqual(1, finalResponse.Responses.Count(x => x.UserContext.Equals("b") && x.Samples[0].HitCount == 200));
        }
        public void AggregatorIgnoresUnknownCounterInResponse()
        {
            var aCounter = new BatchCounterQuery { CounterName = "a", UserContext = "a" };

            var batchRequest = new BatchQueryRequest();
            batchRequest.Queries.Add(aCounter);

            var agg = new BatchResponseAggregator(batchRequest);

            var oneResponse = new BatchQueryResponse
                              {
                                  Responses = {this.CreateHitCountResponse("i am a key tree", 100)},
                                  RequestDetails =
                                  {
                                      new RequestDetails
                                      {
                                          Server = new ServerInfo
                                                   {
                                                       Hostname = "something",
                                                   },
                                          HttpResponseCode = 200
                                      }
                                  }
                              };

            agg.AddResponse(oneResponse);

            var finalResponse = agg.GetResponse();
            Assert.IsNotNull(finalResponse);
            Assert.AreEqual(1, finalResponse.Responses.Count);
            Assert.AreEqual(0, finalResponse.Responses[0].Samples.Count);
        }
        public void AggregatorAggregatesOverlapCorrectly()
        {
            var aCounter = new BatchCounterQuery { CounterName = "a", UserContext = "a" };

            var batchRequest = new BatchQueryRequest();
            batchRequest.Queries.Add(aCounter);

            var agg = new BatchResponseAggregator(batchRequest);

            var oneResponse = new BatchQueryResponse
                              {
                                  Responses = {this.CreateHitCountResponse("a", 100)},
                                  RequestDetails =
                                  {
                                      new RequestDetails
                                      {
                                          Server = new ServerInfo
                                          {
                                              Hostname = "something",
                                              Port = 42
                                          },
                                          HttpResponseCode = 200
                                      }
                                  }
                              };
            var twoResponse = new BatchQueryResponse
                              {
                                  Responses = {this.CreateHitCountResponse("a", 200)},
                                  RequestDetails =
                                  {
                                      new RequestDetails
                                      {
                                          Server = new ServerInfo
                                          {
                                              Hostname = "somewhere",
                                              Port = 42
                                          },
                                          HttpResponseCode = 200
                                      }
                                  }
                              };

            agg.AddResponse(oneResponse);
            agg.AddResponse(twoResponse);

            var finalResponse = agg.GetResponse();
            Assert.IsNotNull(finalResponse);
            Assert.AreEqual(2, finalResponse.RequestDetails.Count);
            Assert.AreEqual(1, finalResponse.Responses.Count);
            Assert.AreEqual(300, finalResponse.Responses[0].Samples[0].HitCount);
        }