View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.resolver.dns;
17  
18  import io.netty.channel.ChannelFuture;
19  import io.netty.handler.codec.dns.DnsQuestion;
20  import io.netty.handler.codec.dns.DnsRecordType;
21  import io.netty.handler.codec.dns.DnsResponseCode;
22  import io.netty.util.internal.UnstableApi;
23  
24  import java.net.InetSocketAddress;
25  import java.util.List;
26  
27  /**
28   * This interface provides visibility into individual DNS queries. The lifecycle of an objects is as follows:
29   * <ol>
30   *     <li>Object creation</li>
31   *     <li>{@link #queryCancelled(int)}</li>
32   * </ol>
33   * OR
34   * <ol>
35   *     <li>Object creation</li>
36   *     <li>{@link #queryWritten(InetSocketAddress, ChannelFuture)}</li>
37   *     <li>{@link #queryRedirected(List)} or {@link #queryCNAMEd(DnsQuestion)} or
38   *     {@link #queryNoAnswer(DnsResponseCode)} or {@link #queryCancelled(int)} or
39   *     {@link #queryFailed(Throwable)} or {@link #querySucceed()}</li>
40   * </ol>
41   * <p>
42   * This interface can be used to track metrics for individual DNS servers. Methods which may lead to another DNS query
43   * return an object of type {@link DnsQueryLifecycleObserver}. Implementations may use this to build a query tree to
44   * understand the "sub queries" generated by a single query.
45   */
46  @UnstableApi
47  public interface DnsQueryLifecycleObserver {
48      /**
49       * The query has been written.
50       * @param dnsServerAddress The DNS server address which the query was sent to.
51       * @param future The future which represents the status of the write operation for the DNS query.
52       */
53      void queryWritten(InetSocketAddress dnsServerAddress, ChannelFuture future);
54  
55      /**
56       * The query may have been written but it was cancelled at some point.
57       * @param queriesRemaining The number of queries remaining.
58       */
59      void queryCancelled(int queriesRemaining);
60  
61      /**
62       * The query has been redirected to another list of DNS servers.
63       * @param nameServers The name servers the query has been redirected to.
64       * @return An observer for the new query which we may issue.
65       */
66      DnsQueryLifecycleObserver queryRedirected(List<InetSocketAddress> nameServers);
67  
68      /**
69       * The query returned a CNAME which we may attempt to follow with a new query.
70       * <p>
71       * Note that multiple queries may be encountering a CNAME. For example a if both {@link DnsRecordType#AAAA} and
72       * {@link DnsRecordType#A} are supported we may query for both.
73       * @param cnameQuestion the question we would use if we issue a new query.
74       * @return An observer for the new query which we may issue.
75       */
76      DnsQueryLifecycleObserver queryCNAMEd(DnsQuestion cnameQuestion);
77  
78      /**
79       * The response to the query didn't provide the expected response code, but it didn't return
80       * {@link DnsResponseCode#NXDOMAIN} so we may try to query again.
81       * @param code the unexpected response code.
82       * @return An observer for the new query which we may issue.
83       */
84      DnsQueryLifecycleObserver queryNoAnswer(DnsResponseCode code);
85  
86      /**
87       * The following criteria are possible:
88       * <ul>
89       *     <li>IO Error</li>
90       *     <li>Server responded with an invalid DNS response</li>
91       *     <li>Server responded with a valid DNS response, but it didn't progress the resolution</li>
92       * </ul>
93       * @param cause The cause which for the failure.
94       */
95      void queryFailed(Throwable cause);
96  
97      /**
98       * The query received the expected results.
99       */
100     void querySucceed();
101 }