etcd.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcd
  15. import (
  16. "context"
  17. "crypto/tls"
  18. "fmt"
  19. "sync"
  20. "time"
  21. "go.etcd.io/etcd/api/v3/mvccpb"
  22. clientv3 "go.etcd.io/etcd/client/v3"
  23. "google.golang.org/grpc"
  24. "yunion.io/x/log"
  25. "yunion.io/x/pkg/errors"
  26. "yunion.io/x/onecloud/pkg/util/seclib2"
  27. )
  28. var (
  29. ErrNoSuchKey = errors.Error("No such key")
  30. )
  31. type SEtcdClient struct {
  32. client *clientv3.Client
  33. requestTimeout time.Duration
  34. leaseTtlTimeout int
  35. namespace string
  36. leaseId clientv3.LeaseID
  37. onKeepaliveFailure func()
  38. leaseLiving bool
  39. watchers map[string]*SEtcdWatcher
  40. watchersMu *sync.Mutex
  41. }
  42. func defaultOnKeepAliveFailed() {
  43. log.Fatalf("etcd keepalive failed")
  44. }
  45. func NewEtcdClient(opt *SEtcdOptions, onKeepaliveFailure func()) (*SEtcdClient, error) {
  46. var err error
  47. var tlsConfig *tls.Config
  48. if opt.EtcdEnabldSsl {
  49. if opt.TLSConfig == nil {
  50. if len(opt.EtcdSslCaCertfile) > 0 {
  51. tlsConfig, err = seclib2.InitTLSConfigWithCA(
  52. opt.EtcdSslCertfile, opt.EtcdSslKeyfile, opt.EtcdSslCaCertfile)
  53. } else {
  54. tlsConfig, err = seclib2.InitTLSConfig(opt.EtcdSslCertfile, opt.EtcdSslKeyfile)
  55. }
  56. if err != nil {
  57. log.Errorf("init tls config fail %s", err)
  58. return nil, err
  59. }
  60. } else {
  61. tlsConfig = opt.TLSConfig
  62. }
  63. }
  64. etcdClient := &SEtcdClient{}
  65. if onKeepaliveFailure == nil {
  66. onKeepaliveFailure = defaultOnKeepAliveFailed
  67. }
  68. etcdClient.onKeepaliveFailure = onKeepaliveFailure
  69. timeoutSeconds := opt.EtcdTimeoutSeconds
  70. if timeoutSeconds == 0 {
  71. timeoutSeconds = 5
  72. }
  73. cli, err := clientv3.New(clientv3.Config{
  74. Endpoints: opt.EtcdEndpoint,
  75. DialTimeout: time.Duration(timeoutSeconds) * time.Second,
  76. Username: opt.EtcdUsername,
  77. Password: opt.EtcdPassword,
  78. TLS: tlsConfig,
  79. DialOptions: []grpc.DialOption{
  80. grpc.WithBlock(),
  81. },
  82. })
  83. if err != nil {
  84. return nil, err
  85. }
  86. etcdClient.client = cli
  87. timeoutSeconds = opt.EtcdRequestTimeoutSeconds
  88. if timeoutSeconds == 0 {
  89. timeoutSeconds = 2
  90. }
  91. etcdClient.requestTimeout = time.Duration(timeoutSeconds) * time.Second
  92. timeoutSeconds = opt.EtcdLeaseExpireSeconds
  93. if timeoutSeconds == 0 {
  94. timeoutSeconds = 5
  95. }
  96. etcdClient.leaseTtlTimeout = timeoutSeconds
  97. etcdClient.watchers = make(map[string]*SEtcdWatcher)
  98. etcdClient.watchersMu = &sync.Mutex{}
  99. etcdClient.namespace = opt.EtcdNamspace
  100. err = etcdClient.startSession(context.TODO())
  101. if err != nil {
  102. if e := etcdClient.Close(); e != nil {
  103. log.Errorf("etcd client close failed %s", e)
  104. }
  105. return nil, err
  106. }
  107. return etcdClient, nil
  108. }
  109. func (cli *SEtcdClient) Close() error {
  110. if cli.client != nil {
  111. err := cli.client.Close()
  112. if err != nil {
  113. return err
  114. }
  115. cli.client = nil
  116. }
  117. return nil
  118. }
  119. func (cli *SEtcdClient) GetClient() *clientv3.Client {
  120. return cli.client
  121. }
  122. func (cli *SEtcdClient) SessionLiving() bool {
  123. return cli.leaseLiving
  124. }
  125. func (cli *SEtcdClient) startSession(ctx context.Context) error {
  126. resp, err := cli.client.Grant(ctx, int64(cli.leaseTtlTimeout))
  127. if err != nil {
  128. return err
  129. }
  130. cli.leaseId = resp.ID
  131. ch, err := cli.client.KeepAlive(context.Background(), cli.leaseId)
  132. if err != nil {
  133. return err
  134. }
  135. cli.leaseLiving = true
  136. go func() {
  137. for {
  138. if _, ok := <-ch; !ok {
  139. cli.leaseLiving = false
  140. log.Errorf("fail to keepalive session")
  141. if cli.onKeepaliveFailure != nil {
  142. cli.onKeepaliveFailure()
  143. }
  144. break
  145. }
  146. }
  147. }()
  148. return nil
  149. }
  150. func (cli *SEtcdClient) RestartSession() error {
  151. if cli.leaseLiving {
  152. return errors.Error("session is living, can't restart")
  153. }
  154. ctx := context.TODO()
  155. return cli.startSession(ctx)
  156. }
  157. func (cli *SEtcdClient) RestartSessionWithContext(ctx context.Context) error {
  158. if cli.leaseLiving {
  159. return errors.Error("session is living, can't restart")
  160. }
  161. return cli.startSession(ctx)
  162. }
  163. func (cli *SEtcdClient) getKey(key string) string {
  164. if len(cli.namespace) > 0 {
  165. return fmt.Sprintf("%s%s", cli.namespace, key)
  166. } else {
  167. return key
  168. }
  169. }
  170. func (cli *SEtcdClient) Put(ctx context.Context, key string, val string) error {
  171. return cli.put(ctx, key, val, false)
  172. }
  173. func (cli *SEtcdClient) PutSession(ctx context.Context, key string, val string) error {
  174. return cli.put(ctx, key, val, true)
  175. }
  176. func (cli *SEtcdClient) grantLease(ctx context.Context, ttlSeconds int64) (*clientv3.LeaseGrantResponse, error) {
  177. nctx, cancel := context.WithTimeout(ctx, cli.requestTimeout)
  178. defer cancel()
  179. resp, err := cli.client.Grant(nctx, ttlSeconds)
  180. if err != nil {
  181. return nil, errors.Wrap(err, "grant lease")
  182. }
  183. return resp, err
  184. }
  185. func (cli *SEtcdClient) PutWithLease(ctx context.Context, key string, val string, ttlSeconds int64) error {
  186. resp, err := cli.grantLease(ctx, ttlSeconds)
  187. if err != nil {
  188. return errors.Wrap(err, "put with grant lease")
  189. }
  190. nctx, cancel := context.WithTimeout(ctx, cli.requestTimeout)
  191. defer cancel()
  192. key = cli.getKey(key)
  193. leaseId := resp.ID
  194. opts := []clientv3.OpOption{
  195. clientv3.WithLease(leaseId),
  196. }
  197. _, err = cli.client.Put(nctx, key, val, opts...)
  198. return err
  199. }
  200. func (cli *SEtcdClient) put(ctx context.Context, key string, val string, session bool) error {
  201. nctx, cancel := context.WithTimeout(ctx, cli.requestTimeout)
  202. defer cancel()
  203. key = cli.getKey(key)
  204. if session {
  205. _, err := cli.client.Put(nctx, key, val, clientv3.WithLease(cli.leaseId))
  206. return err
  207. } else {
  208. _, err := cli.client.Put(nctx, key, val)
  209. return err
  210. }
  211. }
  212. func (cli *SEtcdClient) Get(ctx context.Context, key string) ([]byte, error) {
  213. nctx, cancel := context.WithTimeout(ctx, cli.requestTimeout)
  214. defer cancel()
  215. key = cli.getKey(key)
  216. resp, err := cli.client.Get(nctx, key)
  217. if err != nil {
  218. return nil, err
  219. }
  220. if len(resp.Kvs) == 0 {
  221. return nil, ErrNoSuchKey
  222. }
  223. return resp.Kvs[0].Value, nil
  224. }
  225. type SEtcdKeyValue struct {
  226. Key string
  227. Value []byte
  228. }
  229. func (cli *SEtcdClient) List(ctx context.Context, prefix string) ([]SEtcdKeyValue, error) {
  230. nctx, cancel := context.WithTimeout(ctx, cli.requestTimeout)
  231. defer cancel()
  232. prefix = cli.getKey(prefix)
  233. resp, err := cli.client.Get(nctx, prefix, clientv3.WithPrefix(),
  234. clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
  235. if err != nil {
  236. return nil, err
  237. }
  238. ret := make([]SEtcdKeyValue, len(resp.Kvs))
  239. for i := 0; i < len(resp.Kvs); i += 1 {
  240. ret[i] = SEtcdKeyValue{
  241. Key: string(resp.Kvs[i].Key[len(cli.namespace):]),
  242. Value: resp.Kvs[i].Value,
  243. }
  244. }
  245. return ret, nil
  246. }
  247. type TEtcdCreateEventFunc func(ctx context.Context, key, value []byte)
  248. type TEtcdModifyEventFunc func(ctx context.Context, key, oldvalue, value []byte)
  249. type TEtcdDeleteEventFunc func(ctx context.Context, key []byte)
  250. type SEtcdWatcher struct {
  251. watcher clientv3.Watcher
  252. cancel context.CancelFunc
  253. }
  254. func (w *SEtcdWatcher) Cancel() {
  255. w.watcher.Close()
  256. w.cancel()
  257. }
  258. func (cli *SEtcdClient) Watch(
  259. ctx context.Context, prefix string,
  260. onCreate TEtcdCreateEventFunc,
  261. onModify TEtcdModifyEventFunc,
  262. onDelete TEtcdDeleteEventFunc,
  263. ) error {
  264. cli.watchersMu.Lock()
  265. _, ok := cli.watchers[prefix]
  266. if ok {
  267. cli.watchersMu.Unlock()
  268. return errors.Errorf("watch prefix %s already registered", prefix)
  269. }
  270. watcher := clientv3.NewWatcher(cli.client)
  271. nctx, cancel := context.WithCancel(ctx)
  272. cli.watchers[prefix] = &SEtcdWatcher{
  273. watcher: watcher,
  274. cancel: cancel,
  275. }
  276. cli.watchersMu.Unlock()
  277. prefix = cli.getKey(prefix)
  278. rch := watcher.Watch(nctx, prefix, clientv3.WithPrefix(), clientv3.WithPrevKV())
  279. go func() {
  280. for wresp := range rch {
  281. for _, ev := range wresp.Events {
  282. key := ev.Kv.Key[len(cli.namespace):]
  283. if ev.PrevKv == nil {
  284. onCreate(nctx, key, ev.Kv.Value)
  285. } else {
  286. switch ev.Type {
  287. case mvccpb.PUT:
  288. onModify(nctx, key, ev.PrevKv.Value, ev.Kv.Value)
  289. case mvccpb.DELETE:
  290. if onDelete != nil {
  291. onDelete(nctx, key)
  292. }
  293. }
  294. }
  295. }
  296. }
  297. log.Infof("stop watching %s", prefix)
  298. }()
  299. return nil
  300. }
  301. func (cli *SEtcdClient) Unwatch(prefix string) {
  302. cli.watchersMu.Lock()
  303. defer cli.watchersMu.Unlock()
  304. watcher, ok := cli.watchers[prefix]
  305. if ok {
  306. log.Debugf("unwatch %s", prefix)
  307. watcher.Cancel()
  308. delete(cli.watchers, prefix)
  309. } else {
  310. log.Debugf("prefix %s not watched!!", prefix)
  311. }
  312. }
  313. func (cli *SEtcdClient) Delete(ctx context.Context, key string) ([]byte, error) {
  314. nctx, cancel := context.WithTimeout(ctx, cli.requestTimeout)
  315. defer cancel()
  316. key = cli.getKey(key)
  317. dresp, err := cli.client.Delete(nctx, key, clientv3.WithPrevKV())
  318. if err != nil {
  319. return nil, err
  320. }
  321. if dresp.Deleted == 1 {
  322. return dresp.PrevKvs[0].Value, nil
  323. } else {
  324. return nil, nil
  325. }
  326. }