vitess.io/vitess@v0.16.2/go/vt/topo/zk2topo/lock.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  	"github.com/z-division/go-zookeeper/zk"
    25  
    26  	"vitess.io/vitess/go/vt/vterrors"
    27  
    28  	"vitess.io/vitess/go/vt/log"
    29  	"vitess.io/vitess/go/vt/topo"
    30  )
    31  
    32  // This file contains the lock management code for zktopo.Server.
    33  
    34  // zkLockDescriptor implements topo.LockDescriptor.
    35  type zkLockDescriptor struct {
    36  	zs       *Server
    37  	nodePath string
    38  }
    39  
    40  // Lock is part of the topo.Conn interface.
    41  func (zs *Server) Lock(ctx context.Context, dirPath, contents string) (topo.LockDescriptor, error) {
    42  	return zs.lock(ctx, dirPath, contents)
    43  }
    44  
    45  // TryLock is part of the topo.Conn interface.
    46  func (zs *Server) TryLock(ctx context.Context, dirPath, contents string) (topo.LockDescriptor, error) {
    47  	// We list all the entries under dirPath
    48  	entries, err := zs.ListDir(ctx, dirPath, true)
    49  	if err != nil {
    50  		// We need to return the right error codes, like
    51  		// topo.ErrNoNode and topo.ErrInterrupted, and the
    52  		// easiest way to do this is to return convertError(err).
    53  		// It may lose some of the context, if this is an issue,
    54  		// maybe logging the error would work here.
    55  		return nil, convertError(err, dirPath)
    56  	}
    57  
    58  	// If there is a folder '/locks' with some entries in it then we can assume that someone else already has a lock.
    59  	// Throw error in this case
    60  	for _, e := range entries {
    61  		// there is a bug where ListDir return ephemeral = false for locks. It is due
    62  		// https://github.com/vitessio/vitess/blob/main/go/vt/topo/zk2topo/utils.go#L55
    63  		// TODO: Fix/send ephemeral flag value recursively while creating ephemeral file
    64  		if e.Name == locksPath && e.Type == topo.TypeDirectory {
    65  			return nil, topo.NewError(topo.NodeExists, fmt.Sprintf("lock already exists at path %s", dirPath))
    66  		}
    67  	}
    68  
    69  	// everything is good let's acquire the lock.
    70  	return zs.lock(ctx, dirPath, contents)
    71  }
    72  
    73  // Lock is part of the topo.Conn interface.
    74  func (zs *Server) lock(ctx context.Context, dirPath, contents string) (topo.LockDescriptor, error) {
    75  	// Lock paths end in a trailing slash so that when we create
    76  	// sequential nodes, they are created as children, not siblings.
    77  	locksDir := path.Join(zs.root, dirPath, locksPath) + "/"
    78  
    79  	// Create the locks path, possibly creating the parent.
    80  	nodePath, err := CreateRecursive(ctx, zs.conn, locksDir, []byte(contents), zk.FlagSequence|zk.FlagEphemeral, zk.WorldACL(PermFile), 1)
    81  	if err != nil {
    82  		return nil, convertError(err, locksDir)
    83  	}
    84  
    85  	err = obtainQueueLock(ctx, zs.conn, nodePath)
    86  	if err != nil {
    87  		var errToReturn error
    88  		switch err {
    89  		case context.DeadlineExceeded:
    90  			errToReturn = topo.NewError(topo.Timeout, nodePath)
    91  		case context.Canceled:
    92  			errToReturn = topo.NewError(topo.Interrupted, nodePath)
    93  		default:
    94  			errToReturn = vterrors.Wrapf(err, "failed to obtain action lock: %v", nodePath)
    95  		}
    96  
    97  		// Regardless of the reason, try to cleanup.
    98  		log.Warningf("Failed to obtain action lock: %v", err)
    99  
   100  		if err := zs.conn.Delete(ctx, nodePath, -1); err != nil {
   101  			log.Warningf("Failed to close connection :%v", err)
   102  		}
   103  
   104  		// Show the other locks in the directory
   105  		dir := path.Dir(nodePath)
   106  		children, _, err := zs.conn.Children(ctx, dir)
   107  		if err != nil {
   108  			log.Warningf("Failed to get children of %v: %v", dir, err)
   109  			return nil, errToReturn
   110  		}
   111  
   112  		if len(children) == 0 {
   113  			log.Warningf("No other locks present, you may just try again now.")
   114  			return nil, errToReturn
   115  		}
   116  
   117  		childPath := path.Join(dir, children[0])
   118  		data, _, err := zs.conn.Get(ctx, childPath)
   119  		if err != nil {
   120  			log.Warningf("Failed to get first locks node %v (may have just ended): %v", childPath, err)
   121  			return nil, errToReturn
   122  		}
   123  
   124  		log.Warningf("------ Most likely blocking lock: %v\n%v", childPath, string(data))
   125  		return nil, errToReturn
   126  	}
   127  
   128  	// Remove the root prefix from the file. So when we delete it,
   129  	// it's a relative file.
   130  	nodePath = nodePath[len(zs.root):]
   131  	return &zkLockDescriptor{
   132  		zs:       zs,
   133  		nodePath: nodePath,
   134  	}, nil
   135  }
   136  
   137  // Check is part of the topo.LockDescriptor interface.
   138  func (ld *zkLockDescriptor) Check(ctx context.Context) error {
   139  	// TODO(alainjobart): check the connection has not been interrupted.
   140  	// We'd lose the ephemeral node in case of a session loss.
   141  	return nil
   142  }
   143  
   144  // Unlock is part of the topo.LockDescriptor interface.
   145  func (ld *zkLockDescriptor) Unlock(ctx context.Context) error {
   146  	return ld.zs.Delete(ctx, ld.nodePath, nil)
   147  }