Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | *is % allowed in string |
| 32 | */ |
| 33 | |
| 34 | #include "test/cpp/util/metrics_server.h" |
| 35 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 36 | #include <grpc++/server_builder.h> |
| 37 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame^] | 38 | #include "src/proto/grpc/testing/metrics.grpc.pb.h" |
| 39 | #include "src/proto/grpc/testing/metrics.pb.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 40 | |
| 41 | namespace grpc { |
| 42 | namespace testing { |
| 43 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 44 | Gauge::Gauge(long initial_val) : val_(initial_val) {} |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 45 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 46 | void Gauge::Set(long new_val) { |
Sree Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 47 | std::lock_guard<std::mutex> lock(val_mu_); |
| 48 | val_ = new_val; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Sree Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 51 | long Gauge::Get() { |
| 52 | std::lock_guard<std::mutex> lock(val_mu_); |
| 53 | return val_; |
| 54 | } |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 55 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 56 | grpc::Status MetricsServiceImpl::GetAllGauges( |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 57 | ServerContext* context, const EmptyMessage* request, |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 58 | ServerWriter<GaugeResponse>* writer) { |
| 59 | gpr_log(GPR_INFO, "GetAllGauges called"); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 60 | |
| 61 | std::lock_guard<std::mutex> lock(mu_); |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 62 | for (auto it = gauges_.begin(); it != gauges_.end(); it++) { |
| 63 | GaugeResponse resp; |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 64 | resp.set_name(it->first); // Gauge name |
| 65 | resp.set_long_value(it->second->Get()); // Gauge value |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 66 | writer->Write(resp); |
| 67 | } |
| 68 | |
| 69 | return Status::OK; |
| 70 | } |
| 71 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 72 | grpc::Status MetricsServiceImpl::GetGauge(ServerContext* context, |
| 73 | const GaugeRequest* request, |
| 74 | GaugeResponse* response) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 75 | std::lock_guard<std::mutex> lock(mu_); |
| 76 | |
Sree Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 77 | const auto it = gauges_.find(request->name()); |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 78 | if (it != gauges_.end()) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 79 | response->set_name(it->first); |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 80 | response->set_long_value(it->second->Get()); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return Status::OK; |
| 84 | } |
| 85 | |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 86 | std::shared_ptr<Gauge> MetricsServiceImpl::CreateGauge(const grpc::string& name, |
| 87 | bool* already_present) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 88 | std::lock_guard<std::mutex> lock(mu_); |
| 89 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 90 | std::shared_ptr<Gauge> gauge(new Gauge(0)); |
Sree Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 91 | const auto p = gauges_.emplace(name, gauge); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 92 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 93 | // p.first is an iterator pointing to <name, shared_ptr<Gauge>> pair. p.second |
Sree Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 94 | // is a boolean which is set to 'true' if the Gauge is inserted in the guages_ |
| 95 | // map and 'false' if it is already present in the map |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 96 | *already_present = !p.second; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 97 | return p.first->second; |
| 98 | } |
| 99 | |
Sree Kuchibhotla | 52a514a | 2015-11-19 10:28:47 -0800 | [diff] [blame] | 100 | // Starts the metrics server and returns the grpc::Server instance. Call Wait() |
| 101 | // on the returned server instance. |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 102 | std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) { |
| 103 | gpr_log(GPR_INFO, "Building metrics server.."); |
| 104 | |
Sree Kuchibhotla | bc3127d | 2015-11-19 10:32:59 -0800 | [diff] [blame] | 105 | const grpc::string address = "0.0.0.0:" + std::to_string(port); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 106 | |
| 107 | ServerBuilder builder; |
| 108 | builder.AddListeningPort(address, grpc::InsecureServerCredentials()); |
| 109 | builder.RegisterService(this); |
| 110 | |
| 111 | std::unique_ptr<grpc::Server> server(builder.BuildAndStart()); |
| 112 | gpr_log(GPR_INFO, "Metrics server %s started. Ready to receive requests..", |
| 113 | address.c_str()); |
| 114 | |
| 115 | return server; |
| 116 | } |
| 117 | |
| 118 | } // namespace testing |
| 119 | } // namespace grpc |