| 1 |
// |
| 2 |
// Copyright 2015 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#ifndef GRPC_CORE_LIB_RESOLVER_RESOLVER_H |
| 18 |
#define GRPC_CORE_LIB_RESOLVER_RESOLVER_H |
| 19 |
|
| 20 |
#include <grpc/support/port_platform.h> |
| 21 |
|
| 22 |
#include <functional> |
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "absl/status/status.h" |
| 26 |
#include "absl/status/statusor.h" |
| 27 |
|
| 28 |
#include "src/core/lib/channel/channel_args.h" |
| 29 |
#include "src/core/lib/debug/trace.h" |
| 30 |
#include "src/core/lib/gprpp/orphanable.h" |
| 31 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 32 |
#include "src/core/lib/resolver/server_address.h" |
| 33 |
#include "src/core/lib/service_config/service_config.h" |
| 34 |
|
| 35 |
extern grpc_core::DebugOnlyTraceFlag grpc_trace_resolver_refcount; |
| 36 |
|
| 37 |
// Name associated with individual address, if available. |
| 38 |
#define GRPC_ARG_ADDRESS_NAME "grpc.address_name" |
| 39 |
|
| 40 |
namespace grpc_core { |
| 41 |
|
| 42 |
/// Interface for name resolution. |
| 43 |
/// |
| 44 |
/// This interface is designed to support both push-based and pull-based |
| 45 |
/// mechanisms. A push-based mechanism is one where the resolver will |
| 46 |
/// subscribe to updates for a given name, and the name service will |
| 47 |
/// proactively send new data to the resolver whenever the data associated |
| 48 |
/// with the name changes. A pull-based mechanism is one where the resolver |
| 49 |
/// needs to query the name service again to get updated information (e.g., |
| 50 |
/// DNS). |
| 51 |
/// |
| 52 |
/// Note: All methods with a "Locked" suffix must be called from the |
| 53 |
/// work_serializer passed to the constructor. |
| 54 |
class Resolver : public InternallyRefCounted<Resolver> { |
| 55 |
public: |
| 56 |
/// Results returned by the resolver. |
| 57 |
struct Result { |
| 58 |
/// A list of addresses, or an error. |
| 59 |
absl::StatusOr<ServerAddressList> addresses; |
| 60 |
/// A service config, or an error. |
| 61 |
absl::StatusOr<RefCountedPtr<ServiceConfig>> service_config = nullptr; |
| 62 |
/// An optional human-readable note describing context about the resolution, |
| 63 |
/// to be passed along to the LB policy for inclusion in RPC failure status |
| 64 |
/// messages in cases where neither \a addresses nor \a service_config |
| 65 |
/// has a non-OK status. For example, a resolver that returns an empty |
| 66 |
/// address list but a valid service config may set to this to something |
| 67 |
/// like "no DNS entries found for <name>". |
| 68 |
std::string resolution_note; |
| 69 |
// TODO(roth): Before making this a public API, figure out a way to |
| 70 |
// avoid exposing channel args this way. |
| 71 |
ChannelArgs args; |
| 72 |
// If non-null, this callback will be invoked when the LB policy has |
| 73 |
// processed the result. The status value passed to the callback |
| 74 |
// indicates whether the LB policy accepted the update. For polling |
| 75 |
// resolvers, if the reported status is non-OK, then the resolver |
| 76 |
// should put itself into backoff to retry the resolution later. |
| 77 |
// The resolver impl must not call ResultHandler::ReportResult() |
| 78 |
// again until after this callback has been invoked. |
| 79 |
// The callback will be invoked within the channel's WorkSerializer. |
| 80 |
// It may or may not be invoked before ResultHandler::ReportResult() |
| 81 |
// returns, which is why it's a separate callback. |
| 82 |
std::function<void(absl::Status)> result_health_callback; |
| 83 |
}; |
| 84 |
|
| 85 |
/// A proxy object used by the resolver to return results to the |
| 86 |
/// client channel. |
| 87 |
class ResultHandler { |
| 88 |
public: |
| 89 |
virtual ~ResultHandler() {} |
| 90 |
|
| 91 |
/// Reports a result to the channel. |
| 92 |
virtual void ReportResult(Result result) = 0; // NOLINT |
| 93 |
}; |
| 94 |
|
| 95 |
// Not copyable nor movable. |
| 96 |
Resolver(const Resolver&) = delete; |
| 97 |
Resolver& operator=(const Resolver&) = delete; |
| 98 |
~Resolver() override = default; |
| 99 |
|
| 100 |
/// Starts resolving. |
| 101 |
virtual void StartLocked() = 0; |
| 102 |
|
| 103 |
/// Asks the resolver to obtain an updated resolver result, if |
| 104 |
/// applicable. |
| 105 |
/// |
| 106 |
/// This is useful for pull-based implementations to decide when to |
| 107 |
/// re-resolve. However, the implementation is not required to |
| 108 |
/// re-resolve immediately upon receiving this call; it may instead |
| 109 |
/// elect to delay based on some configured minimum time between |
| 110 |
/// queries, to avoid hammering the name service with queries. |
| 111 |
/// |
| 112 |
/// For push-based implementations, this may be a no-op. |
| 113 |
/// |
| 114 |
/// Note: Implementations must not invoke any method on the |
| 115 |
/// ResultHandler from within this call. |
| 116 |
virtual void RequestReresolutionLocked() {} |
| 117 |
|
| 118 |
/// Resets the re-resolution backoff, if any. |
| 119 |
/// This needs to be implemented only by pull-based implementations; |
| 120 |
/// for push-based implementations, it will be a no-op. |
| 121 |
virtual void ResetBackoffLocked() {} |
| 122 |
|
| 123 |
// Note: This must be invoked while holding the work_serializer. |
| 124 |
void Orphan() override { |
| 125 |
ShutdownLocked(); |
| 126 |
Unref(); |
| 127 |
} |
| 128 |
|
| 129 |
protected: |
| 130 |
Resolver(); |
| 131 |
|
| 132 |
/// Shuts down the resolver. |
| 133 |
virtual void ShutdownLocked() = 0; |
| 134 |
}; |
| 135 |
|
| 136 |
} // namespace grpc_core |
| 137 |
|
| 138 |
#endif // GRPC_CORE_LIB_RESOLVER_RESOLVER_H |