dns_resolver.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. *
  3. * Copyright 2017 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. "encoding/json"
  23. "errors"
  24. "fmt"
  25. "net"
  26. "os"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "time"
  31. "golang.org/x/net/context"
  32. "google.golang.org/grpc/grpclog"
  33. "google.golang.org/grpc/internal/grpcrand"
  34. "google.golang.org/grpc/resolver"
  35. )
  36. func init() {
  37. resolver.Register(NewBuilder())
  38. }
  39. const (
  40. defaultPort = "443"
  41. defaultFreq = time.Minute * 30
  42. golang = "GO"
  43. // In DNS, service config is encoded in a TXT record via the mechanism
  44. // described in RFC-1464 using the attribute name grpc_config.
  45. txtAttribute = "grpc_config="
  46. )
  47. var (
  48. errMissingAddr = errors.New("missing address")
  49. )
  50. // NewBuilder creates a dnsBuilder which is used to factory DNS resolvers.
  51. func NewBuilder() resolver.Builder {
  52. return &dnsBuilder{freq: defaultFreq}
  53. }
  54. type dnsBuilder struct {
  55. // frequency of polling the DNS server.
  56. freq time.Duration
  57. }
  58. // Build creates and starts a DNS resolver that watches the name resolution of the target.
  59. func (b *dnsBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) {
  60. if target.Authority != "" {
  61. return nil, fmt.Errorf("Default DNS resolver does not support custom DNS server")
  62. }
  63. host, port, err := parseTarget(target.Endpoint)
  64. if err != nil {
  65. return nil, err
  66. }
  67. // IP address.
  68. if net.ParseIP(host) != nil {
  69. host, _ = formatIP(host)
  70. addr := []resolver.Address{{Addr: host + ":" + port}}
  71. i := &ipResolver{
  72. cc: cc,
  73. ip: addr,
  74. rn: make(chan struct{}, 1),
  75. q: make(chan struct{}),
  76. }
  77. cc.NewAddress(addr)
  78. go i.watcher()
  79. return i, nil
  80. }
  81. // DNS address (non-IP).
  82. ctx, cancel := context.WithCancel(context.Background())
  83. d := &dnsResolver{
  84. freq: b.freq,
  85. host: host,
  86. port: port,
  87. ctx: ctx,
  88. cancel: cancel,
  89. cc: cc,
  90. t: time.NewTimer(0),
  91. rn: make(chan struct{}, 1),
  92. disableServiceConfig: opts.DisableServiceConfig,
  93. }
  94. d.wg.Add(1)
  95. go d.watcher()
  96. return d, nil
  97. }
  98. // Scheme returns the naming scheme of this resolver builder, which is "dns".
  99. func (b *dnsBuilder) Scheme() string {
  100. return "dns"
  101. }
  102. // ipResolver watches for the name resolution update for an IP address.
  103. type ipResolver struct {
  104. cc resolver.ClientConn
  105. ip []resolver.Address
  106. // rn channel is used by ResolveNow() to force an immediate resolution of the target.
  107. rn chan struct{}
  108. q chan struct{}
  109. }
  110. // ResolveNow resend the address it stores, no resolution is needed.
  111. func (i *ipResolver) ResolveNow(opt resolver.ResolveNowOption) {
  112. select {
  113. case i.rn <- struct{}{}:
  114. default:
  115. }
  116. }
  117. // Close closes the ipResolver.
  118. func (i *ipResolver) Close() {
  119. close(i.q)
  120. }
  121. func (i *ipResolver) watcher() {
  122. for {
  123. select {
  124. case <-i.rn:
  125. i.cc.NewAddress(i.ip)
  126. case <-i.q:
  127. return
  128. }
  129. }
  130. }
  131. // dnsResolver watches for the name resolution update for a non-IP target.
  132. type dnsResolver struct {
  133. freq time.Duration
  134. host string
  135. port string
  136. ctx context.Context
  137. cancel context.CancelFunc
  138. cc resolver.ClientConn
  139. // rn channel is used by ResolveNow() to force an immediate resolution of the target.
  140. rn chan struct{}
  141. t *time.Timer
  142. // wg is used to enforce Close() to return after the watcher() goroutine has finished.
  143. // Otherwise, data race will be possible. [Race Example] in dns_resolver_test we
  144. // replace the real lookup functions with mocked ones to facilitate testing.
  145. // If Close() doesn't wait for watcher() goroutine finishes, race detector sometimes
  146. // will warns lookup (READ the lookup function pointers) inside watcher() goroutine
  147. // has data race with replaceNetFunc (WRITE the lookup function pointers).
  148. wg sync.WaitGroup
  149. disableServiceConfig bool
  150. }
  151. // ResolveNow invoke an immediate resolution of the target that this dnsResolver watches.
  152. func (d *dnsResolver) ResolveNow(opt resolver.ResolveNowOption) {
  153. select {
  154. case d.rn <- struct{}{}:
  155. default:
  156. }
  157. }
  158. // Close closes the dnsResolver.
  159. func (d *dnsResolver) Close() {
  160. d.cancel()
  161. d.wg.Wait()
  162. d.t.Stop()
  163. }
  164. func (d *dnsResolver) watcher() {
  165. defer d.wg.Done()
  166. for {
  167. select {
  168. case <-d.ctx.Done():
  169. return
  170. case <-d.t.C:
  171. case <-d.rn:
  172. }
  173. result, sc := d.lookup()
  174. // Next lookup should happen after an interval defined by d.freq.
  175. d.t.Reset(d.freq)
  176. d.cc.NewServiceConfig(sc)
  177. d.cc.NewAddress(result)
  178. }
  179. }
  180. func (d *dnsResolver) lookupSRV() []resolver.Address {
  181. var newAddrs []resolver.Address
  182. _, srvs, err := lookupSRV(d.ctx, "grpclb", "tcp", d.host)
  183. if err != nil {
  184. grpclog.Infof("grpc: failed dns SRV record lookup due to %v.\n", err)
  185. return nil
  186. }
  187. for _, s := range srvs {
  188. lbAddrs, err := lookupHost(d.ctx, s.Target)
  189. if err != nil {
  190. grpclog.Infof("grpc: failed load balancer address dns lookup due to %v.\n", err)
  191. continue
  192. }
  193. for _, a := range lbAddrs {
  194. a, ok := formatIP(a)
  195. if !ok {
  196. grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err)
  197. continue
  198. }
  199. addr := a + ":" + strconv.Itoa(int(s.Port))
  200. newAddrs = append(newAddrs, resolver.Address{Addr: addr, Type: resolver.GRPCLB, ServerName: s.Target})
  201. }
  202. }
  203. return newAddrs
  204. }
  205. func (d *dnsResolver) lookupTXT() string {
  206. ss, err := lookupTXT(d.ctx, d.host)
  207. if err != nil {
  208. grpclog.Infof("grpc: failed dns TXT record lookup due to %v.\n", err)
  209. return ""
  210. }
  211. var res string
  212. for _, s := range ss {
  213. res += s
  214. }
  215. // TXT record must have "grpc_config=" attribute in order to be used as service config.
  216. if !strings.HasPrefix(res, txtAttribute) {
  217. grpclog.Warningf("grpc: TXT record %v missing %v attribute", res, txtAttribute)
  218. return ""
  219. }
  220. return strings.TrimPrefix(res, txtAttribute)
  221. }
  222. func (d *dnsResolver) lookupHost() []resolver.Address {
  223. var newAddrs []resolver.Address
  224. addrs, err := lookupHost(d.ctx, d.host)
  225. if err != nil {
  226. grpclog.Warningf("grpc: failed dns A record lookup due to %v.\n", err)
  227. return nil
  228. }
  229. for _, a := range addrs {
  230. a, ok := formatIP(a)
  231. if !ok {
  232. grpclog.Errorf("grpc: failed IP parsing due to %v.\n", err)
  233. continue
  234. }
  235. addr := a + ":" + d.port
  236. newAddrs = append(newAddrs, resolver.Address{Addr: addr})
  237. }
  238. return newAddrs
  239. }
  240. func (d *dnsResolver) lookup() ([]resolver.Address, string) {
  241. newAddrs := d.lookupSRV()
  242. // Support fallback to non-balancer address.
  243. newAddrs = append(newAddrs, d.lookupHost()...)
  244. if d.disableServiceConfig {
  245. return newAddrs, ""
  246. }
  247. sc := d.lookupTXT()
  248. return newAddrs, canaryingSC(sc)
  249. }
  250. // formatIP returns ok = false if addr is not a valid textual representation of an IP address.
  251. // If addr is an IPv4 address, return the addr and ok = true.
  252. // If addr is an IPv6 address, return the addr enclosed in square brackets and ok = true.
  253. func formatIP(addr string) (addrIP string, ok bool) {
  254. ip := net.ParseIP(addr)
  255. if ip == nil {
  256. return "", false
  257. }
  258. if ip.To4() != nil {
  259. return addr, true
  260. }
  261. return "[" + addr + "]", true
  262. }
  263. // parseTarget takes the user input target string, returns formatted host and port info.
  264. // If target doesn't specify a port, set the port to be the defaultPort.
  265. // If target is in IPv6 format and host-name is enclosed in sqarue brackets, brackets
  266. // are strippd when setting the host.
  267. // examples:
  268. // target: "www.google.com" returns host: "www.google.com", port: "443"
  269. // target: "ipv4-host:80" returns host: "ipv4-host", port: "80"
  270. // target: "[ipv6-host]" returns host: "ipv6-host", port: "443"
  271. // target: ":80" returns host: "localhost", port: "80"
  272. // target: ":" returns host: "localhost", port: "443"
  273. func parseTarget(target string) (host, port string, err error) {
  274. if target == "" {
  275. return "", "", errMissingAddr
  276. }
  277. if ip := net.ParseIP(target); ip != nil {
  278. // target is an IPv4 or IPv6(without brackets) address
  279. return target, defaultPort, nil
  280. }
  281. if host, port, err = net.SplitHostPort(target); err == nil {
  282. // target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port
  283. if host == "" {
  284. // Keep consistent with net.Dial(): If the host is empty, as in ":80", the local system is assumed.
  285. host = "localhost"
  286. }
  287. if port == "" {
  288. // If the port field is empty(target ends with colon), e.g. "[::1]:", defaultPort is used.
  289. port = defaultPort
  290. }
  291. return host, port, nil
  292. }
  293. if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil {
  294. // target doesn't have port
  295. return host, port, nil
  296. }
  297. return "", "", fmt.Errorf("invalid target address %v, error info: %v", target, err)
  298. }
  299. type rawChoice struct {
  300. ClientLanguage *[]string `json:"clientLanguage,omitempty"`
  301. Percentage *int `json:"percentage,omitempty"`
  302. ClientHostName *[]string `json:"clientHostName,omitempty"`
  303. ServiceConfig *json.RawMessage `json:"serviceConfig,omitempty"`
  304. }
  305. func containsString(a *[]string, b string) bool {
  306. if a == nil {
  307. return true
  308. }
  309. for _, c := range *a {
  310. if c == b {
  311. return true
  312. }
  313. }
  314. return false
  315. }
  316. func chosenByPercentage(a *int) bool {
  317. if a == nil {
  318. return true
  319. }
  320. return grpcrand.Intn(100)+1 <= *a
  321. }
  322. func canaryingSC(js string) string {
  323. if js == "" {
  324. return ""
  325. }
  326. var rcs []rawChoice
  327. err := json.Unmarshal([]byte(js), &rcs)
  328. if err != nil {
  329. grpclog.Warningf("grpc: failed to parse service config json string due to %v.\n", err)
  330. return ""
  331. }
  332. cliHostname, err := os.Hostname()
  333. if err != nil {
  334. grpclog.Warningf("grpc: failed to get client hostname due to %v.\n", err)
  335. return ""
  336. }
  337. var sc string
  338. for _, c := range rcs {
  339. if !containsString(c.ClientLanguage, golang) ||
  340. !chosenByPercentage(c.Percentage) ||
  341. !containsString(c.ClientHostName, cliHostname) ||
  342. c.ServiceConfig == nil {
  343. continue
  344. }
  345. sc = string(*c.ServiceConfig)
  346. break
  347. }
  348. return sc
  349. }