|
|
|
@ -1,20 +1,18 @@ |
|
|
|
|
/*
|
|
|
|
|
* |
|
|
|
|
* Copyright 2015 gRPC authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
* You may obtain a copy of the License at |
|
|
|
|
* |
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
* |
|
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
|
* limitations under the License. |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
//
|
|
|
|
|
// Copyright 2015 gRPC authors.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H |
|
|
|
|
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H |
|
|
|
@ -81,26 +79,6 @@ extern DebugOnlyTraceFlag grpc_trace_lb_policy_refcount; |
|
|
|
|
// interested_parties() hooks from the API.
|
|
|
|
|
class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> { |
|
|
|
|
public: |
|
|
|
|
// Represents backend metrics reported by the backend to the client.
|
|
|
|
|
struct BackendMetricData { |
|
|
|
|
/// CPU utilization expressed as a fraction of available CPU resources.
|
|
|
|
|
double cpu_utilization; |
|
|
|
|
/// Memory utilization expressed as a fraction of available memory
|
|
|
|
|
/// resources.
|
|
|
|
|
double mem_utilization; |
|
|
|
|
/// Total requests per second being served by the backend. This
|
|
|
|
|
/// should include all services that a backend is responsible for.
|
|
|
|
|
uint64_t requests_per_second; |
|
|
|
|
/// Application-specific requests cost metrics. Metric names are
|
|
|
|
|
/// determined by the application. Each value is an absolute cost
|
|
|
|
|
/// (e.g. 3487 bytes of storage) associated with the request.
|
|
|
|
|
std::map<absl::string_view, double> request_cost; |
|
|
|
|
/// Application-specific resource utilization metrics. Metric names
|
|
|
|
|
/// are determined by the application. Each value is expressed as a
|
|
|
|
|
/// fraction of total resources available.
|
|
|
|
|
std::map<absl::string_view, double> utilization; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// Interface for accessing per-call state.
|
|
|
|
|
/// Implemented by the client channel and used by the SubchannelPicker.
|
|
|
|
|
class CallState { |
|
|
|
@ -114,13 +92,6 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> { |
|
|
|
|
/// for allocations that need to be made on a per-call basis.
|
|
|
|
|
virtual void* Alloc(size_t size) = 0; |
|
|
|
|
|
|
|
|
|
/// Returns the backend metric data returned by the server for the call,
|
|
|
|
|
/// or null if no backend metric data was returned.
|
|
|
|
|
// TODO(roth): Move this out of CallState, since it should not be
|
|
|
|
|
// accessible to the picker, only to the recv_trailing_metadata_ready
|
|
|
|
|
// callback. It should instead be in its own interface.
|
|
|
|
|
virtual const BackendMetricData* GetBackendMetricData() = 0; |
|
|
|
|
|
|
|
|
|
/// EXPERIMENTAL API.
|
|
|
|
|
/// Returns the value of the call attribute \a key.
|
|
|
|
|
/// Keys are static strings, so an attribute can be accessed by an LB
|
|
|
|
@ -172,6 +143,59 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> { |
|
|
|
|
CallState* call_state; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// Interface for accessing backend metric data.
|
|
|
|
|
/// Implemented by the client channel and used by
|
|
|
|
|
/// SubchannelCallTrackerInterface.
|
|
|
|
|
class BackendMetricAccessor { |
|
|
|
|
public: |
|
|
|
|
// Represents backend metrics reported by the backend to the client.
|
|
|
|
|
struct BackendMetricData { |
|
|
|
|
/// CPU utilization expressed as a fraction of available CPU resources.
|
|
|
|
|
double cpu_utilization; |
|
|
|
|
/// Memory utilization expressed as a fraction of available memory
|
|
|
|
|
/// resources.
|
|
|
|
|
double mem_utilization; |
|
|
|
|
/// Total requests per second being served by the backend. This
|
|
|
|
|
/// should include all services that a backend is responsible for.
|
|
|
|
|
uint64_t requests_per_second; |
|
|
|
|
/// Application-specific requests cost metrics. Metric names are
|
|
|
|
|
/// determined by the application. Each value is an absolute cost
|
|
|
|
|
/// (e.g. 3487 bytes of storage) associated with the request.
|
|
|
|
|
std::map<absl::string_view, double> request_cost; |
|
|
|
|
/// Application-specific resource utilization metrics. Metric names
|
|
|
|
|
/// are determined by the application. Each value is expressed as a
|
|
|
|
|
/// fraction of total resources available.
|
|
|
|
|
std::map<absl::string_view, double> utilization; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
virtual ~BackendMetricAccessor() = default; |
|
|
|
|
|
|
|
|
|
/// Returns the backend metric data returned by the server for the call,
|
|
|
|
|
/// or null if no backend metric data was returned.
|
|
|
|
|
virtual const BackendMetricData* GetBackendMetricData() = 0; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// Interface for tracking subchannel calls.
|
|
|
|
|
/// Implemented by LB policy and used by the channel.
|
|
|
|
|
class SubchannelCallTrackerInterface { |
|
|
|
|
public: |
|
|
|
|
virtual ~SubchannelCallTrackerInterface() = default; |
|
|
|
|
|
|
|
|
|
/// Called when a subchannel call is started after an LB pick.
|
|
|
|
|
virtual void Start() = 0; |
|
|
|
|
|
|
|
|
|
/// Called when a subchannel call is completed.
|
|
|
|
|
/// The metadata may be modified by the implementation. However, the
|
|
|
|
|
/// implementation does not take ownership, so any data that needs to be
|
|
|
|
|
/// used after returning must be copied.
|
|
|
|
|
struct FinishArgs { |
|
|
|
|
absl::Status status; |
|
|
|
|
MetadataInterface* trailing_metadata; |
|
|
|
|
BackendMetricAccessor* backend_metric_accessor; |
|
|
|
|
}; |
|
|
|
|
virtual void Finish(FinishArgs args) = 0; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// The result of picking a subchannel for a call.
|
|
|
|
|
struct PickResult { |
|
|
|
|
/// A successful pick.
|
|
|
|
@ -179,25 +203,17 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> { |
|
|
|
|
/// The subchannel to be used for the call. Must be non-null.
|
|
|
|
|
RefCountedPtr<SubchannelInterface> subchannel; |
|
|
|
|
|
|
|
|
|
/// Callback set by LB policy to be notified of trailing metadata.
|
|
|
|
|
/// If non-null, the client channel will invoke the callback
|
|
|
|
|
/// when trailing metadata is returned.
|
|
|
|
|
/// The metadata may be modified by the callback. However, the callback
|
|
|
|
|
/// does not take ownership, so any data that needs to be used after
|
|
|
|
|
/// returning must be copied.
|
|
|
|
|
/// The call state can be used to obtain backend metric data.
|
|
|
|
|
// TODO(roth): The arguments to this callback should be moved into a
|
|
|
|
|
// struct, so that we can later add new fields without breaking
|
|
|
|
|
// existing implementations.
|
|
|
|
|
std::function<void(absl::Status, MetadataInterface*, CallState*)> |
|
|
|
|
recv_trailing_metadata_ready; |
|
|
|
|
/// Optionally set by the LB policy when it wishes to be notified
|
|
|
|
|
/// about the resulting subchannel call.
|
|
|
|
|
/// Note that if the pick is abandoned by the channel, this may never
|
|
|
|
|
/// be used.
|
|
|
|
|
std::unique_ptr<SubchannelCallTrackerInterface> subchannel_call_tracker; |
|
|
|
|
|
|
|
|
|
explicit Complete( |
|
|
|
|
RefCountedPtr<SubchannelInterface> sc, |
|
|
|
|
std::function<void(absl::Status, MetadataInterface*, CallState*)> cb = |
|
|
|
|
nullptr) |
|
|
|
|
std::unique_ptr<SubchannelCallTrackerInterface> tracker = nullptr) |
|
|
|
|
: subchannel(std::move(sc)), |
|
|
|
|
recv_trailing_metadata_ready(std::move(cb)) {} |
|
|
|
|
subchannel_call_tracker(std::move(tracker)) {} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// Pick cannot be completed until something changes on the control
|
|
|
|
|