github.com/m3db/m3@v1.5.0/src/dbnode/x/m3em/node/node.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package m3db 22 23 import ( 24 "fmt" 25 "sync" 26 27 m3dbrpc "github.com/m3db/m3/src/dbnode/generated/thrift/rpc" 28 m3dbchannel "github.com/m3db/m3/src/dbnode/network/server/tchannelthrift/node/channel" 29 "github.com/m3db/m3/src/m3em/node" 30 31 tchannel "github.com/uber/tchannel-go" 32 "github.com/uber/tchannel-go/thrift" 33 ) 34 35 type m3emNode struct { 36 sync.Mutex 37 node.ServiceNode 38 39 opts Options 40 rpcClient m3dbrpc.TChanNode 41 } 42 43 // New constructs a new m3emNode 44 func New(svcNode node.ServiceNode, opts Options) (Node, error) { 45 if err := opts.Validate(); err != nil { 46 return nil, err 47 } 48 return &m3emNode{ 49 ServiceNode: svcNode, 50 opts: opts, 51 }, nil 52 } 53 54 func (n *m3emNode) client() (m3dbrpc.TChanNode, error) { 55 n.Lock() 56 defer n.Unlock() 57 if n.rpcClient != nil { 58 return n.rpcClient, nil 59 } 60 channel, err := tchannel.NewChannel("Client", nil) 61 if err != nil { 62 return nil, fmt.Errorf("could not create new tchannel channel: %v", err) 63 } 64 endpoint := &thrift.ClientOptions{HostPort: n.Endpoint()} 65 thriftClient := thrift.NewClient(channel, m3dbchannel.ChannelName, endpoint) 66 client := m3dbrpc.NewTChanNodeClient(thriftClient) 67 n.rpcClient = client 68 return n.rpcClient, nil 69 } 70 71 func (n *m3emNode) Health() (NodeHealth, error) { 72 healthResult := NodeHealth{} 73 74 client, err := n.client() 75 if err != nil { 76 return healthResult, err 77 } 78 79 attemptFn := func() error { 80 tctx, _ := thrift.NewContext(n.opts.NodeOptions().OperationTimeout()) 81 result, err := client.Health(tctx) 82 if err != nil { 83 return err 84 } 85 healthResult.Bootstrapped = result.GetBootstrapped() 86 healthResult.OK = result.GetOk() 87 healthResult.Status = result.GetStatus() 88 return nil 89 } 90 91 retrier := n.opts.NodeOptions().Retrier() 92 err = retrier.Attempt(attemptFn) 93 return healthResult, err 94 } 95 96 func (n *m3emNode) Bootstrapped() bool { 97 health, err := n.Health() 98 if err != nil { 99 return false 100 } 101 return health.Bootstrapped 102 }