vitess.io/vitess@v0.16.2/go/vt/topo/zk2topo/file.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  	"bytes"
    21  	"fmt"
    22  	"path"
    23  
    24  	"context"
    25  
    26  	"github.com/z-division/go-zookeeper/zk"
    27  
    28  	"vitess.io/vitess/go/vt/topo"
    29  )
    30  
    31  // Create is part of the topo.Conn interface.
    32  func (zs *Server) Create(ctx context.Context, filePath string, contents []byte) (topo.Version, error) {
    33  	zkPath := path.Join(zs.root, filePath)
    34  
    35  	pathCreated, err := CreateRecursive(ctx, zs.conn, zkPath, contents, 0, zk.WorldACL(PermFile), -1)
    36  	if err != nil {
    37  		return nil, convertError(err, zkPath)
    38  	}
    39  
    40  	// Now do a Get to get the version. If the content doesn't
    41  	// match, it means someone else already changed the file,
    42  	// between our Create and Get. It is safer to return an error here,
    43  	// and let the calling process recover if it can.
    44  	data, stat, err := zs.conn.Get(ctx, pathCreated)
    45  	if err != nil {
    46  		return nil, convertError(err, zkPath)
    47  	}
    48  	if !bytes.Equal(data, contents) {
    49  		return nil, fmt.Errorf("file contents changed between zk.Create and zk.Get")
    50  	}
    51  
    52  	return ZKVersion(stat.Version), nil
    53  }
    54  
    55  // Update is part of the topo.Conn interface.
    56  func (zs *Server) Update(ctx context.Context, filePath string, contents []byte, version topo.Version) (topo.Version, error) {
    57  	zkPath := path.Join(zs.root, filePath)
    58  
    59  	// Interpret the version
    60  	var zkVersion int32
    61  	if version != nil {
    62  		zkVersion = int32(version.(ZKVersion))
    63  	} else {
    64  		zkVersion = -1
    65  	}
    66  
    67  	stat, err := zs.conn.Set(ctx, zkPath, contents, zkVersion)
    68  	if zkVersion == -1 && err == zk.ErrNoNode {
    69  		// In zookeeper, an unconditional set of a nonexisting
    70  		// node will return ErrNoNode. In that case, we want
    71  		// to Create.
    72  		return zs.Create(ctx, filePath, contents)
    73  	}
    74  	if err != nil {
    75  		return nil, convertError(err, zkPath)
    76  	}
    77  	return ZKVersion(stat.Version), nil
    78  }
    79  
    80  // Get is part of the topo.Conn interface.
    81  func (zs *Server) Get(ctx context.Context, filePath string) ([]byte, topo.Version, error) {
    82  	zkPath := path.Join(zs.root, filePath)
    83  
    84  	contents, stat, err := zs.conn.Get(ctx, zkPath)
    85  	if err != nil {
    86  		return nil, nil, convertError(err, zkPath)
    87  	}
    88  	return contents, ZKVersion(stat.Version), nil
    89  }
    90  
    91  // List is part of the topo.Conn interface.
    92  func (zs *Server) List(ctx context.Context, filePathPrefix string) ([]topo.KVInfo, error) {
    93  	return nil, topo.NewError(topo.NoImplementation, "List not supported in ZK2 topo")
    94  }
    95  
    96  // Delete is part of the topo.Conn interface.
    97  func (zs *Server) Delete(ctx context.Context, filePath string, version topo.Version) error {
    98  	zkPath := path.Join(zs.root, filePath)
    99  
   100  	// Interpret the version
   101  	var zkVersion int32
   102  	if version != nil {
   103  		zkVersion = int32(version.(ZKVersion))
   104  	} else {
   105  		zkVersion = -1
   106  	}
   107  
   108  	if err := zs.conn.Delete(ctx, zkPath, zkVersion); err != nil {
   109  		return convertError(err, zkPath)
   110  	}
   111  	return zs.recursiveDeleteParentIfEmpty(ctx, filePath)
   112  }
   113  
   114  func (zs *Server) recursiveDeleteParentIfEmpty(ctx context.Context, filePath string) error {
   115  	dir := path.Dir(filePath)
   116  	if dir == "" || dir == "/" || dir == "." {
   117  		// we reached the top
   118  		return nil
   119  	}
   120  	zkPath := path.Join(zs.root, dir)
   121  	err := zs.conn.Delete(ctx, zkPath, -1)
   122  	switch err {
   123  	case nil:
   124  		// we keep going up
   125  		return zs.recursiveDeleteParentIfEmpty(ctx, dir)
   126  	case zk.ErrNotEmpty, zk.ErrNoNode:
   127  		// we're done (not empty, or someone beat us to deletion)
   128  		return nil
   129  	default:
   130  		return err
   131  	}
   132  }