dns_resolver.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package dns implements a dns resolver to be installed as the default resolver
  19. // in grpc.
  20. package dns
  21. import (
  22. "context"
  23. "encoding/json"
  24. "errors"
  25. "fmt"
  26. "net"
  27. "os"
  28. "strconv"
  29. "strings"
  30. "sync"
  31. "time"
  32. "google.golang.org/grpc/grpclog"
  33. "google.golang.org/grpc/internal/backoff"
  34. "google.golang.org/grpc/internal/grpcrand"
  35. "google.golang.org/grpc/resolver"
  36. )
  37. func init() {
  38. resolver.Register(NewBuilder())
  39. }
  40. const (
  41. defaultPort = "443"
  42. defaultFreq = time.Minute * 30
  43. defaultDNSSvrPort = "53"
  44. golang = "GO"
  45. // txtPrefix is the prefix string to be prepended to the host name for txt record lookup.
  46. txtPrefix = "_grpc_config."
  47. // In DNS, service config is encoded in a TXT record via the mechanism
  48. // described in RFC-1464 using the attribute name grpc_config.
  49. txtAttribute = "grpc_config="
  50. )
  51. var (
  52. errMissingAddr = errors.New("dns resolver: missing address")
  53. // Addresses ending with a colon that is supposed to be the separator
  54. // between host and port is not allowed. E.g. "::" is a valid address as
  55. // it is an IPv6 address (host only) and "[::]:" is invalid as it ends with
  56. // a colon as the host and port separator
  57. errEndsWithColon = errors.New("dns resolver: missing port after port-separator colon")
  58. )
  59. var (
  60. defaultResolver netResolver = net.DefaultResolver
  61. // To prevent excessive re-resolution, we enforce a rate limit on DNS
  62. // resolution requests.
  63. minDNSResRate = 30 * time.Second
  64. )
  65. var customAuthorityDialler = func(authority string) func(ctx context.Context, network, address string) (net.Conn, error) {
  66. return func(ctx context.Context, network, address string) (net.Conn, error) {
  67. var dialer net.Dialer
  68. return dialer.DialContext(ctx, network, authority)
  69. }
  70. }
  71. var customAuthorityResolver = func(authority string) (netResolver, error) {
  72. host, port, err := parseTarget(authority, defaultDNSSvrPort)
  73. if err != nil {
  74. return nil, err
  75. }
  76. authorityWithPort := net.JoinHostPort(host, port)
  77. return &net.Resolver{
  78. PreferGo: true,
  79. Dial: customAuthorityDialler(authorityWithPort),
  80. }, nil
  81. }
  82. // NewBuilder creates a dnsBuilder which is used to factory DNS resolvers.
  83. func NewBuilder() resolver.Builder {
  84. return &dnsBuilder{minFreq: defaultFreq}
  85. }
  86. type dnsBuilder struct {
  87. // minimum frequency of polling the DNS server.
  88. minFreq time.Duration
  89. }
  90. // Build creates and starts a DNS resolver that watches the name resolution of the target.
  91. func (b *dnsBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) {
  92. host, port, err := parseTarget(target.Endpoint, defaultPort)
  93. if err != nil {
  94. return nil, err
  95. }
  96. // IP address.
  97. if net.ParseIP(host) != nil {
  98. host, _ = formatIP(host)
  99. addr := []resolver.Address{{Addr: host + ":" + port}}
  100. i := &ipResolver{
  101. cc: cc,
  102. ip: addr,
  103. rn: make(chan struct{}, 1),
  104. q: make(chan struct{}),
  105. }
  106. cc.NewAddress(addr)
  107. go i.watcher()
  108. return i, nil
  109. }
  110. // DNS address (non-IP).
  111. ctx, cancel := context.WithCancel(context.Background())
  112. d := &dnsResolver{
  113. freq: b.minFreq,
  114. backoff: backoff.Exponential{MaxDelay: b.minFreq},
  115. host: host,
  116. port: port,
  117. ctx: ctx,
  118. cancel: cancel,
  119. cc: cc,
  120. t: time.NewTimer(0),
  121. rn: make(chan struct{}, 1),
  122. disableServiceConfig: opts.DisableServiceConfig,
  123. }
  124. if target.Authority == "" {
  125. d.resolver = defaultResolver
  126. } else {
  127. d.resolver, err = customAuthorityResolver(target.Authority)
  128. if err != nil {
  129. return nil, err
  130. }
  131. }
  132. d.wg.Add(1)
  133. go d.watcher()
  134. return d, nil
  135. }
  136. // Scheme returns the naming scheme of this resolver builder, which is "dns".
  137. func (b *dnsBuilder) Scheme() string {
  138. return "dns"
  139. }
  140. type netResolver interface {
  141. LookupHost(ctx context.Context, host string) (addrs []string, err error)
  142. LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error)
  143. LookupTXT(ctx context.Context, name string) (txts []string, err error)
  144. }
  145. // ipResolver watches for the name resolution update for an IP address.
  146. type ipResolver struct {
  147. cc resolver.ClientConn
  148. ip []resolver.Address
  149. // rn channel is used by ResolveNow() to force an immediate resolution of the target.
  150. rn chan struct{}
  151. q chan struct{}
  152. }
  153. // ResolveNow resend the address it stores, no resolution is needed.
  154. func (i *ipResolver) ResolveNow(opt resolver.ResolveNowOption) {
  155. select {
  156. case i.rn <- struct{}{}:
  157. default:
  158. }
  159. }
  160. // Close closes the ipResolver.
  161. func (i *ipResolver) Close() {
  162. close(i.q)
  163. }
  164. func (i *ipResolver) watcher() {
  165. for {
  166. select {
  167. case <-i.rn:
  168. i.cc.NewAddress(i.ip)
  169. case <-i.q:
  170. return
  171. }
  172. }
  173. }
  174. // dnsResolver watches for the name resolution update for a non-IP target.
  175. type dnsResolver struct {
  176. freq time.Duration
  177. backoff backoff.Exponential
  178. retryCount int
  179. host string
  180. port string
  181. resolver netResolver
  182. ctx context.Context
  183. cancel context.CancelFunc
  184. cc resolver.ClientConn
  185. // rn channel is used by ResolveNow() to force an immediate resolution of the target.
  186. rn chan struct{}
  187. t *time.Timer
  188. // wg is used to enforce Close() to return after the watcher() goroutine has finished.
  189. // Otherwise, data race will be possible. [Race Example] in dns_resolver_test we
  190. // replace the real lookup functions with mocked ones to facilitate testing.
  191. // If Close() doesn't wait for watcher() goroutine finishes, race detector sometimes
  192. // will warns lookup (READ the lookup function pointers) inside watcher() goroutine
  193. // has data race with replaceNetFunc (WRITE the lookup function pointers).
  194. wg sync.WaitGroup
  195. disableServiceConfig bool
  196. }
  197. // ResolveNow invoke an immediate resolution of the target that this dnsResolver watches.
  198. func (d *dnsResolver) ResolveNow(opt resolver.ResolveNowOption) {
  199. select {
  200. case d.rn <- struct{}{}:
  201. default:
  202. }
  203. }
  204. // Close closes the dnsResolver.
  205. func (d *dnsResolver) Close() {
  206. d.cancel()
  207. d.wg.Wait()
  208. d.t.Stop()
  209. }
  210. func (d *dnsResolver) watcher() {
  211. defer d.wg.Done()
  212. for {
  213. select {
  214. case <-d.ctx.Done():
  215. return
  216. case <-d.t.C:
  217. case <-d.rn:
  218. if !d.t.Stop() {
  219. // Before resetting a timer, it should be stopped to prevent racing with
  220. // reads on it's channel.
  221. <-d.t.C
  222. }
  223. }
  224. result, sc := d.lookup()
  225. // Next lookup should happen within an interval defined by d.freq. It may be
  226. // more often due to exponential retry on empty address list.
  227. if len(result) == 0 {
  228. d.retryCount++
  229. d.t.Reset(d.backoff.Backoff(d.retryCount))
  230. } else {
  231. d.retryCount = 0
  232. d.t.Reset(d.freq)
  233. }
  234. d.cc.NewServiceConfig(sc)
  235. d.cc.NewAddress(result)
  236. // Sleep to prevent excessive re-resolutions. Incoming resolution requests
  237. // will be queued in d.rn.
  238. t := time.NewTimer(minDNSResRate)
  239. select {
  240. case <-t.C:
  241. case <-d.ctx.Done():
  242. t.Stop()
  243. return
  244. }
  245. }
  246. }
  247. func (d *dnsResolver) lookupSRV() []resolver.Address {
  248. var newAddrs []resolver.Address
  249. _, srvs, err := d.resolver.LookupSRV(d.ctx, "grpclb", "tcp", d.host)
  250. if err != nil {
  251. grpclog.Infof("grpc: failed dns SRV record lookup due to %v.\n", err)
  252. return nil
  253. }
  254. for _, s := range srvs {
  255. lbAddrs, err := d.resolver.LookupHost(d.ctx, s.Target)
  256. if err != nil {
  257. grpclog.Infof("grpc: failed load balancer address dns lookup due to %v.\n", err)
  258. continue
  259. }
  260. for _, a := range lbAddrs {
  261. a, ok := formatIP(a)
  262. if !ok {
  263. grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err)
  264. continue
  265. }
  266. addr := a + ":" + strconv.Itoa(int(s.Port))
  267. newAddrs = append(newAddrs, resolver.Address{Addr: addr, Type: resolver.GRPCLB, ServerName: s.Target})
  268. }
  269. }
  270. return newAddrs
  271. }
  272. func (d *dnsResolver) lookupTXT() string {
  273. ss, err := d.resolver.LookupTXT(d.ctx, txtPrefix+d.host)
  274. if err != nil {
  275. grpclog.Infof("grpc: failed dns TXT record lookup due to %v.\n", err)
  276. return ""
  277. }
  278. var res string
  279. for _, s := range ss {
  280. res += s
  281. }
  282. // TXT record must have "grpc_config=" attribute in order to be used as service config.
  283. if !strings.HasPrefix(res, txtAttribute) {
  284. grpclog.Warningf("grpc: TXT record %v missing %v attribute", res, txtAttribute)
  285. return ""
  286. }
  287. return strings.TrimPrefix(res, txtAttribute)
  288. }
  289. func (d *dnsResolver) lookupHost() []resolver.Address {
  290. var newAddrs []resolver.Address
  291. addrs, err := d.resolver.LookupHost(d.ctx, d.host)
  292. if err != nil {
  293. grpclog.Warningf("grpc: failed dns A record lookup due to %v.\n", err)
  294. return nil
  295. }
  296. for _, a := range addrs {
  297. a, ok := formatIP(a)
  298. if !ok {
  299. grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err)
  300. continue
  301. }
  302. addr := a + ":" + d.port
  303. newAddrs = append(newAddrs, resolver.Address{Addr: addr})
  304. }
  305. return newAddrs
  306. }
  307. func (d *dnsResolver) lookup() ([]resolver.Address, string) {
  308. newAddrs := d.lookupSRV()
  309. // Support fallback to non-balancer address.
  310. newAddrs = append(newAddrs, d.lookupHost()...)
  311. if d.disableServiceConfig {
  312. return newAddrs, ""
  313. }
  314. sc := d.lookupTXT()
  315. return newAddrs, canaryingSC(sc)
  316. }
  317. // formatIP returns ok = false if addr is not a valid textual representation of an IP address.
  318. // If addr is an IPv4 address, return the addr and ok = true.
  319. // If addr is an IPv6 address, return the addr enclosed in square brackets and ok = true.
  320. func formatIP(addr string) (addrIP string, ok bool) {
  321. ip := net.ParseIP(addr)
  322. if ip == nil {
  323. return "", false
  324. }
  325. if ip.To4() != nil {
  326. return addr, true
  327. }
  328. return "[" + addr + "]", true
  329. }
  330. // parseTarget takes the user input target string and default port, returns formatted host and port info.
  331. // If target doesn't specify a port, set the port to be the defaultPort.
  332. // If target is in IPv6 format and host-name is enclosed in square brackets, brackets
  333. // are stripped when setting the host.
  334. // examples:
  335. // target: "www.google.com" defaultPort: "443" returns host: "www.google.com", port: "443"
  336. // target: "ipv4-host:80" defaultPort: "443" returns host: "ipv4-host", port: "80"
  337. // target: "[ipv6-host]" defaultPort: "443" returns host: "ipv6-host", port: "443"
  338. // target: ":80" defaultPort: "443" returns host: "localhost", port: "80"
  339. func parseTarget(target, defaultPort string) (host, port string, err error) {
  340. if target == "" {
  341. return "", "", errMissingAddr
  342. }
  343. if ip := net.ParseIP(target); ip != nil {
  344. // target is an IPv4 or IPv6(without brackets) address
  345. return target, defaultPort, nil
  346. }
  347. if host, port, err = net.SplitHostPort(target); err == nil {
  348. if port == "" {
  349. // If the port field is empty (target ends with colon), e.g. "[::1]:", this is an error.
  350. return "", "", errEndsWithColon
  351. }
  352. // target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port
  353. if host == "" {
  354. // Keep consistent with net.Dial(): If the host is empty, as in ":80", the local system is assumed.
  355. host = "localhost"
  356. }
  357. return host, port, nil
  358. }
  359. if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil {
  360. // target doesn't have port
  361. return host, port, nil
  362. }
  363. return "", "", fmt.Errorf("invalid target address %v, error info: %v", target, err)
  364. }
  365. type rawChoice struct {
  366. ClientLanguage *[]string `json:"clientLanguage,omitempty"`
  367. Percentage *int `json:"percentage,omitempty"`
  368. ClientHostName *[]string `json:"clientHostName,omitempty"`
  369. ServiceConfig *json.RawMessage `json:"serviceConfig,omitempty"`
  370. }
  371. func containsString(a *[]string, b string) bool {
  372. if a == nil {
  373. return true
  374. }
  375. for _, c := range *a {
  376. if c == b {
  377. return true
  378. }
  379. }
  380. return false
  381. }
  382. func chosenByPercentage(a *int) bool {
  383. if a == nil {
  384. return true
  385. }
  386. return grpcrand.Intn(100)+1 <= *a
  387. }
  388. func canaryingSC(js string) string {
  389. if js == "" {
  390. return ""
  391. }
  392. var rcs []rawChoice
  393. err := json.Unmarshal([]byte(js), &rcs)
  394. if err != nil {
  395. grpclog.Warningf("grpc: failed to parse service config json string due to %v.\n", err)
  396. return ""
  397. }
  398. cliHostname, err := os.Hostname()
  399. if err != nil {
  400. grpclog.Warningf("grpc: failed to get client hostname due to %v.\n", err)
  401. return ""
  402. }
  403. var sc string
  404. for _, c := range rcs {
  405. if !containsString(c.ClientLanguage, golang) ||
  406. !chosenByPercentage(c.Percentage) ||
  407. !containsString(c.ClientHostName, cliHostname) ||
  408. c.ServiceConfig == nil {
  409. continue
  410. }
  411. sc = string(*c.ServiceConfig)
  412. break
  413. }
  414. return sc
  415. }