dns_resolver.go 9.8 KB

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