| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package informer
- import (
- "context"
- "fmt"
- "path/filepath"
- "strings"
- "yunion.io/x/jsonutils"
- "yunion.io/x/log"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/onecloud/pkg/cloudcommon/etcd"
- "yunion.io/x/onecloud/pkg/util/ctx"
- )
- const (
- EtcdInformerPrefix = "/onecloud/informer"
- EtcdInformerClientsKey = "@clients"
- EventTypeCreate = "CREATE"
- EventTypeUpdate = "UPDATE"
- EventTypeDelete = "DELETE"
- )
- type TEventType string
- type modelObject struct {
- EventType TEventType `json:"event_type"`
- Object *jsonutils.JSONDict `json:"object"`
- OldObject *jsonutils.JSONDict `json:"old_object"`
- }
- func (obj modelObject) ToKey() string {
- return jsonutils.Marshal(obj).String()
- }
- func newModelObjectFromValue(val []byte) (*modelObject, error) {
- jObj, err := jsonutils.Parse(val)
- if err != nil {
- return nil, errors.Wrapf(err, "parse key %s", val)
- }
- ret := new(modelObject)
- if err := jObj.Unmarshal(ret); err != nil {
- return nil, errors.Wrap(err, "unmarshal to model object")
- }
- return ret, nil
- }
- type EtcdBackend struct {
- client *etcd.SEtcdClient
- leaseTTL int64
- }
- func newEtcdBackend(opt *etcd.SEtcdOptions, onKeepaliveFailure func()) (*EtcdBackend, error) {
- opt.EtcdNamspace = EtcdInformerPrefix
- be := new(EtcdBackend)
- be.leaseTTL = int64(opt.EtcdLeaseExpireSeconds)
- if onKeepaliveFailure == nil {
- onKeepaliveFailure = be.onKeepaliveFailure
- }
- cli, err := etcd.NewEtcdClient(opt, onKeepaliveFailure)
- if err != nil {
- return nil, errors.Wrap(err, "new etcd client")
- }
- be.client = cli
- return be, nil
- }
- func NewEtcdBackend(opt *etcd.SEtcdOptions, onKeepaliveFailure func()) (*EtcdBackend, error) {
- be, err := newEtcdBackend(opt, onKeepaliveFailure)
- if err != nil {
- return nil, err
- }
- ctx := ctx.CtxWithTime()
- be.initClientResources(ctx)
- be.StartClientWatch(ctx)
- return be, nil
- }
- func (b *EtcdBackend) initClientResources(ctx context.Context) error {
- pairs, err := b.client.List(ctx, "/")
- if err != nil {
- return errors.Wrap(err, "list all client resources")
- }
- for _, pair := range pairs {
- keywordPlural, err := b.getClientWatchResource([]byte(pair.Key))
- if err == nil && len(keywordPlural) != 0 {
- AddWatchedResources(keywordPlural)
- }
- }
- return nil
- }
- func (b *EtcdBackend) getObjectKey(obj *ModelObject) string {
- if obj.IsJoint {
- return fmt.Sprintf("/%s/%s/%s", obj.KeywordPlural, obj.MasterId, obj.SlaveId)
- }
- return fmt.Sprintf("/%s/%s", obj.KeywordPlural, obj.Id)
- }
- func (b *EtcdBackend) getValue(eventType TEventType, obj *ModelObject) string {
- modelObject := modelObject{
- EventType: eventType,
- Object: obj.Object,
- }
- return modelObject.ToKey()
- }
- func (b *EtcdBackend) getUpdateValue(obj *ModelObject, oldObj *jsonutils.JSONDict) string {
- modelObject := modelObject{
- EventType: EventTypeUpdate,
- Object: obj.Object,
- OldObject: oldObj,
- }
- return modelObject.ToKey()
- }
- func (b *EtcdBackend) GetType() string {
- return "etcd"
- }
- func (b *EtcdBackend) Create(ctx context.Context, obj *ModelObject) error {
- key := b.getObjectKey(obj)
- val := b.getValue(EventTypeCreate, obj)
- return b.put(ctx, key, val)
- }
- func (b *EtcdBackend) Update(ctx context.Context, obj *ModelObject, oldObj *jsonutils.JSONDict) error {
- key := b.getObjectKey(obj)
- val := b.getUpdateValue(obj, oldObj)
- return b.put(ctx, key, val)
- }
- func (b *EtcdBackend) Delete(ctx context.Context, obj *ModelObject) error {
- key := b.getObjectKey(obj)
- val := b.getValue(EventTypeDelete, obj)
- err := b.put(ctx, key, val)
- return err
- }
- func (b *EtcdBackend) put(ctx context.Context, key, val string) error {
- return b.PutWithLease(ctx, key, val, b.leaseTTL)
- }
- func (b *EtcdBackend) PutSession(ctx context.Context, key, val string) error {
- return b.client.PutSession(ctx, key, val)
- }
- func (b *EtcdBackend) PutWithLease(ctx context.Context, key, val string, ttlSeconds int64) error {
- return b.client.PutWithLease(ctx, key, val, ttlSeconds)
- }
- func (b *EtcdBackend) onKeepaliveFailure() {
- if err := b.client.RestartSession(); err != nil {
- log.Errorf("restart etcd session error: %v", err)
- return
- }
- b.StartClientWatch(context.Background())
- }
- func (b *EtcdBackend) StartClientWatch(ctx context.Context) {
- b.client.Unwatch("/")
- b.client.Watch(ctx, "/", b.onClientResourceCreate, b.onClientResourceUpdate, b.onClientResourceDelete)
- }
- func (b *EtcdBackend) isClientsKey(key []byte) bool {
- return strings.Contains(string(key), EtcdInformerClientsKey)
- }
- func (b *EtcdBackend) getClientWatchResource(key []byte) (string, error) {
- // key is like: /servers/@clients/default-climc-5d4c8d49f6-p6l68
- keyPath := string(key)
- parts := strings.Split(keyPath, "/")
- if len(parts) != 4 {
- return "", errors.Errorf("invalid client resource key: %v", parts)
- }
- if parts[2] != EtcdInformerClientsKey {
- return "", errors.Errorf("key %s not contains %s", keyPath, EtcdInformerClientsKey)
- }
- return parts[1], nil
- }
- func (b *EtcdBackend) onClientResourceAdd(key []byte) {
- if !b.isClientsKey(key) {
- return
- }
- keywordPlural, err := b.getClientWatchResource(key)
- if err != nil {
- log.Errorf("get client watch resource error: %v", err)
- return
- }
- AddWatchedResources(keywordPlural)
- }
- func (b *EtcdBackend) onClientResourceCreate(ctx context.Context, key, value []byte) {
- b.onClientResourceAdd(key)
- }
- func (b *EtcdBackend) onClientResourceUpdate(ctx context.Context, key, oldvalue, value []byte) {
- b.onClientResourceAdd(key)
- }
- func (b *EtcdBackend) shouldDeleteWatchedResource(ctx context.Context, keywordPlural string) bool {
- // clientsKey is like: /servers/@clients
- clientsKey := fmt.Sprintf("/%s/%s", keywordPlural, EtcdInformerClientsKey)
- pairs, err := b.client.List(ctx, clientsKey)
- if err != nil {
- log.Errorf("list clientsKey %s error: %v", clientsKey, err)
- return false
- }
- return len(pairs) == 0
- }
- func (b *EtcdBackend) onClientResourceDelete(ctx context.Context, key []byte) {
- if !b.isClientsKey(key) {
- return
- }
- keywordPlural, err := b.getClientWatchResource(key)
- if err != nil {
- log.Errorf("get client watch resource keywordPlural error: %v", err)
- return
- }
- if b.shouldDeleteWatchedResource(ctx, keywordPlural) {
- DeleteWatchedResources(keywordPlural)
- }
- }
- func (b *EtcdBackend) getWatchKey(key string) string {
- return filepath.Join("/", key)
- }
|