vitess.io/vitess@v0.16.2/go/vt/topo/zk2topo/watch.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package zk2topo
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"path"
    23  
    24  	"vitess.io/vitess/go/vt/vterrors"
    25  
    26  	"vitess.io/vitess/go/vt/topo"
    27  )
    28  
    29  // Watch is part of the topo.Conn interface.
    30  func (zs *Server) Watch(ctx context.Context, filePath string) (*topo.WatchData, <-chan *topo.WatchData, error) {
    31  	zkPath := path.Join(zs.root, filePath)
    32  
    33  	// Get the initial value, set the initial watch
    34  	initialCtx, initialCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
    35  	defer initialCancel()
    36  
    37  	data, stats, watch, err := zs.conn.GetW(initialCtx, zkPath)
    38  	if err != nil {
    39  		return nil, nil, convertError(err, zkPath)
    40  	}
    41  	if stats == nil {
    42  		// No stats --> node doesn't exist.
    43  		return nil, nil, topo.NewError(topo.NoNode, zkPath)
    44  	}
    45  	wd := &topo.WatchData{
    46  		Contents: data,
    47  		Version:  ZKVersion(stats.Version),
    48  	}
    49  
    50  	c := make(chan *topo.WatchData, 10)
    51  	go func() {
    52  		defer close(c)
    53  
    54  		for {
    55  			// Act on the watch, or on 'stop' close.
    56  			select {
    57  			case event, ok := <-watch:
    58  				if !ok {
    59  					c <- &topo.WatchData{Err: fmt.Errorf("watch on %v was closed", zkPath)}
    60  					return
    61  				}
    62  
    63  				if event.Err != nil {
    64  					c <- &topo.WatchData{Err: vterrors.Wrapf(event.Err, "received a non-OK event for %v", zkPath)}
    65  					return
    66  				}
    67  
    68  			case <-ctx.Done():
    69  				// user is not interested any more
    70  				c <- &topo.WatchData{Err: topo.NewError(topo.Interrupted, "watch")}
    71  				return
    72  			}
    73  
    74  			// Get the value again, and send it, or error.
    75  			data, stats, watch, err = zs.conn.GetW(ctx, zkPath)
    76  			if err != nil {
    77  				c <- &topo.WatchData{Err: convertError(err, zkPath)}
    78  				return
    79  			}
    80  			if stats == nil {
    81  				// No data --> node doesn't exist
    82  				c <- &topo.WatchData{Err: topo.NewError(topo.NoNode, zkPath)}
    83  				return
    84  			}
    85  			wd := &topo.WatchData{
    86  				Contents: data,
    87  				Version:  ZKVersion(stats.Version),
    88  			}
    89  			c <- wd
    90  			if wd.Err != nil {
    91  				return
    92  			}
    93  		}
    94  	}()
    95  
    96  	return wd, c, nil
    97  }
    98  
    99  // WatchRecursive is part of the topo.Conn interface.
   100  func (zs *Server) WatchRecursive(_ context.Context, path string) ([]*topo.WatchDataRecursive, <-chan *topo.WatchDataRecursive, error) {
   101  	// This isn't implemented yet, but potentially can be implemented if we want
   102  	// to update the minimum ZooKeeper requirement to 3.6.0 and use recursive watches.
   103  	// Also see https://zookeeper.apache.org/doc/r3.6.3/zookeeperProgrammers.html#sc_WatchPersistentRecursive
   104  	return nil, nil, topo.NewError(topo.NoImplementation, path)
   105  }