github.com/portworx/kvdb@v0.0.0-20241107215734-a185a966f535/wrappers/kv_no_quorum.go (about)

     1  package wrappers
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	"github.com/portworx/kvdb"
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  type noKvdbQuorumWrapper struct {
    12  	kvBaseWrapper
    13  	randGen *rand.Rand
    14  }
    15  
    16  // NewNoKvdbQuorumWrapper constructs a new kvdb.Kvdb.
    17  func NewNoKvdbQuorumWrapper(
    18  	kv kvdb.Kvdb,
    19  	options map[string]string,
    20  ) (kvdb.Kvdb, error) {
    21  	logrus.Infof("creating quorum check wrapper")
    22  	return &noKvdbQuorumWrapper{
    23  		kvBaseWrapper: kvBaseWrapper{
    24  			name:        kvdb.Wrapper_NoQuorum,
    25  			wrappedKvdb: kv,
    26  		},
    27  		randGen: rand.New(rand.NewSource(time.Now().UnixNano())),
    28  	}, nil
    29  }
    30  
    31  // Version returns the supported version of the mem implementation
    32  func Version(url string, kvdbOptions map[string]string) (string, error) {
    33  	return kvdb.MemVersion1, nil
    34  }
    35  
    36  func (k *noKvdbQuorumWrapper) String() string {
    37  	return k.wrappedKvdb.String()
    38  }
    39  
    40  func (k *noKvdbQuorumWrapper) Capabilities() int {
    41  	return k.wrappedKvdb.Capabilities()
    42  }
    43  
    44  func (k *noKvdbQuorumWrapper) Get(key string) (*kvdb.KVPair, error) {
    45  	return nil, kvdb.ErrNoQuorum
    46  }
    47  
    48  func (k *noKvdbQuorumWrapper) Snapshot(prefixes []string, consistent bool) (kvdb.Kvdb, uint64, error) {
    49  	return nil, 0, kvdb.ErrNoQuorum
    50  }
    51  
    52  func (k *noKvdbQuorumWrapper) Put(
    53  	key string,
    54  	value interface{},
    55  	ttl uint64,
    56  ) (*kvdb.KVPair, error) {
    57  	return nil, kvdb.ErrNoQuorum
    58  }
    59  
    60  func (k *noKvdbQuorumWrapper) GetVal(key string, v interface{}) (*kvdb.KVPair, error) {
    61  	return nil, kvdb.ErrNoQuorum
    62  }
    63  
    64  func (k *noKvdbQuorumWrapper) Create(
    65  	key string,
    66  	value interface{},
    67  	ttl uint64,
    68  ) (*kvdb.KVPair, error) {
    69  	return nil, kvdb.ErrNoQuorum
    70  }
    71  
    72  func (k *noKvdbQuorumWrapper) Update(
    73  	key string,
    74  	value interface{},
    75  	ttl uint64,
    76  ) (*kvdb.KVPair, error) {
    77  	return nil, kvdb.ErrNoQuorum
    78  }
    79  
    80  func (k *noKvdbQuorumWrapper) GetLockTryDuration() time.Duration {
    81  	return k.wrappedKvdb.GetLockTryDuration()
    82  }
    83  
    84  func (k *noKvdbQuorumWrapper) Enumerate(prefix string) (kvdb.KVPairs, error) {
    85  	return nil, kvdb.ErrNoQuorum
    86  }
    87  
    88  func (k *noKvdbQuorumWrapper) Delete(key string) (*kvdb.KVPair, error) {
    89  	return nil, kvdb.ErrNoQuorum
    90  }
    91  
    92  func (k *noKvdbQuorumWrapper) DeleteTree(prefix string) error {
    93  	return kvdb.ErrNoQuorum
    94  }
    95  
    96  func (k *noKvdbQuorumWrapper) Keys(prefix, sep string) ([]string, error) {
    97  	return nil, kvdb.ErrNoQuorum
    98  }
    99  
   100  func (k *noKvdbQuorumWrapper) CompareAndSet(
   101  	kvp *kvdb.KVPair,
   102  	flags kvdb.KVFlags,
   103  	prevValue []byte,
   104  ) (*kvdb.KVPair, error) {
   105  	return nil, kvdb.ErrNoQuorum
   106  }
   107  
   108  func (k *noKvdbQuorumWrapper) CompareAndDelete(
   109  	kvp *kvdb.KVPair,
   110  	flags kvdb.KVFlags,
   111  ) (*kvdb.KVPair, error) {
   112  	return nil, kvdb.ErrNoQuorum
   113  }
   114  
   115  func (k *noKvdbQuorumWrapper) delayWatch() {
   116  	time.Sleep(time.Duration(20+k.randGen.Intn(20)) * time.Second)
   117  }
   118  
   119  func (k *noKvdbQuorumWrapper) WatchKey(
   120  	key string,
   121  	waitIndex uint64,
   122  	opaque interface{},
   123  	cb kvdb.WatchCB,
   124  ) error {
   125  	// slow-down retrying watch since we don't know if kvdb is still up
   126  	k.delayWatch()
   127  	return k.wrappedKvdb.WatchKey(key, waitIndex, opaque, cb)
   128  }
   129  
   130  func (k *noKvdbQuorumWrapper) WatchTree(
   131  	prefix string,
   132  	waitIndex uint64,
   133  	opaque interface{},
   134  	cb kvdb.WatchCB,
   135  ) error {
   136  	// slow-down retrying watch since we don't know if kvdb is still up
   137  	k.delayWatch()
   138  	return k.wrappedKvdb.WatchTree(prefix, waitIndex, opaque, cb)
   139  }
   140  
   141  func (k *noKvdbQuorumWrapper) Compact(
   142  	index uint64,
   143  ) error {
   144  	return kvdb.ErrNoQuorum
   145  }
   146  
   147  func (k *noKvdbQuorumWrapper) Lock(key string) (*kvdb.KVPair, error) {
   148  	return nil, kvdb.ErrNoQuorum
   149  }
   150  
   151  func (k *noKvdbQuorumWrapper) IsKeyLocked(key string) (bool, string, error) {
   152  	return false, "", kvdb.ErrNoQuorum
   153  }
   154  
   155  func (k *noKvdbQuorumWrapper) LockWithID(
   156  	key string,
   157  	lockerID string,
   158  ) (*kvdb.KVPair, error) {
   159  	return nil, kvdb.ErrNoQuorum
   160  }
   161  
   162  func (k *noKvdbQuorumWrapper) LockWithTimeout(
   163  	key string,
   164  	lockerID string,
   165  	lockTryDuration time.Duration,
   166  	lockHoldDuration time.Duration,
   167  ) (*kvdb.KVPair, error) {
   168  	return nil, kvdb.ErrNoQuorum
   169  }
   170  
   171  func (k *noKvdbQuorumWrapper) Unlock(kvp *kvdb.KVPair) error {
   172  	return k.wrappedKvdb.Unlock(kvp)
   173  }
   174  
   175  func (k *noKvdbQuorumWrapper) EnumerateWithSelect(
   176  	prefix string,
   177  	enumerateSelect kvdb.EnumerateSelect,
   178  	copySelect kvdb.CopySelect,
   179  ) ([]interface{}, error) {
   180  	return nil, kvdb.ErrNoQuorum
   181  }
   182  
   183  func (k *noKvdbQuorumWrapper) EnumerateKVPWithSelect(
   184  	prefix string,
   185  	enumerateSelect kvdb.EnumerateKVPSelect,
   186  	copySelect kvdb.CopyKVPSelect,
   187  ) (kvdb.KVPairs, error) {
   188  	return nil, kvdb.ErrNoQuorum
   189  }
   190  
   191  func (k *noKvdbQuorumWrapper) GetWithCopy(
   192  	key string,
   193  	copySelect kvdb.CopySelect,
   194  ) (interface{}, error) {
   195  	return nil, kvdb.ErrNoQuorum
   196  }
   197  
   198  func (k *noKvdbQuorumWrapper) TxNew() (kvdb.Tx, error) {
   199  	return nil, kvdb.ErrNoQuorum
   200  }
   201  
   202  func (k *noKvdbQuorumWrapper) SnapPut(snapKvp *kvdb.KVPair) (*kvdb.KVPair, error) {
   203  	return nil, kvdb.ErrNoQuorum
   204  }
   205  
   206  func (k *noKvdbQuorumWrapper) AddUser(username string, password string) error {
   207  	return kvdb.ErrNoQuorum
   208  }
   209  
   210  func (k *noKvdbQuorumWrapper) RemoveUser(username string) error {
   211  	return kvdb.ErrNoQuorum
   212  }
   213  
   214  func (k *noKvdbQuorumWrapper) GrantUserAccess(
   215  	username string,
   216  	permType kvdb.PermissionType,
   217  	subtree string,
   218  ) error {
   219  	return kvdb.ErrNoQuorum
   220  }
   221  
   222  func (k *noKvdbQuorumWrapper) RevokeUsersAccess(
   223  	username string,
   224  	permType kvdb.PermissionType,
   225  	subtree string,
   226  ) error {
   227  	return kvdb.ErrNoQuorum
   228  }
   229  
   230  func (k *noKvdbQuorumWrapper) SetFatalCb(f kvdb.FatalErrorCB) {
   231  	k.wrappedKvdb.SetFatalCb(f)
   232  }
   233  
   234  func (k *noKvdbQuorumWrapper) SetLockHoldDuration(timeout time.Duration) {
   235  	k.wrappedKvdb.SetLockHoldDuration(timeout)
   236  }
   237  
   238  func (k *noKvdbQuorumWrapper) GetLockHoldDuration() time.Duration {
   239  	return k.wrappedKvdb.GetLockHoldDuration()
   240  }
   241  
   242  func (k *noKvdbQuorumWrapper) Serialize() ([]byte, error) {
   243  	return nil, kvdb.ErrNoQuorum
   244  }
   245  
   246  func (k *noKvdbQuorumWrapper) Deserialize(b []byte) (kvdb.KVPairs, error) {
   247  	return nil, kvdb.ErrNoQuorum
   248  }
   249  
   250  func (k *noKvdbQuorumWrapper) AddMember(nodeIP, nodePeerPort, nodeName string) (map[string][]string, error) {
   251  	return nil, kvdb.ErrNoQuorum
   252  }
   253  
   254  func (k *noKvdbQuorumWrapper) AddLearner(nodeIP, nodePeerPort, nodeName string) (map[string][]string, error) {
   255  	return nil, kvdb.ErrNoQuorum
   256  }
   257  
   258  func (k *noKvdbQuorumWrapper) RemoveMember(nodeName, nodeIP string) error {
   259  	return kvdb.ErrNoQuorum
   260  }
   261  
   262  func (k *noKvdbQuorumWrapper) RemoveMemberByID(removeMemberID uint64) error {
   263  	return kvdb.ErrNoQuorum
   264  }
   265  
   266  func (k *noKvdbQuorumWrapper) UpdateMember(nodeIP, nodePeerPort, nodeName string) (map[string][]string, error) {
   267  	return k.wrappedKvdb.UpdateMember(nodeIP, nodePeerPort, nodeName)
   268  }
   269  
   270  func (k *noKvdbQuorumWrapper) ListMembers() (map[uint64]*kvdb.MemberInfo, error) {
   271  	return k.wrappedKvdb.ListMembers()
   272  }
   273  
   274  func (k *noKvdbQuorumWrapper) SetEndpoints(endpoints []string) error {
   275  	return k.wrappedKvdb.SetEndpoints(endpoints)
   276  }
   277  
   278  func (k *noKvdbQuorumWrapper) GetEndpoints() []string {
   279  	return k.wrappedKvdb.GetEndpoints()
   280  }
   281  
   282  func (k *noKvdbQuorumWrapper) Defragment(endpoint string, timeout int) error {
   283  	return k.wrappedKvdb.Defragment(endpoint, timeout)
   284  }
   285  
   286  func init() {
   287  	if err := kvdb.RegisterWrapper(kvdb.Wrapper_NoQuorum, NewNoKvdbQuorumWrapper); err != nil {
   288  		panic(err.Error())
   289  	}
   290  	if err := kvdb.RegisterWrapper(kvdb.Wrapper_Log, NewLogWrapper); err != nil {
   291  		panic(err.Error())
   292  	}
   293  }