forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
111 lines (100 loc) · 4.14 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package actionnode
// This file contains utility functions to be used with actionnode /
// topology server.
import (
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/topo"
)
var (
// DefaultLockTimeout is a good value to use as a default for
// locking a shard / keyspace.
DefaultLockTimeout = 15 * time.Second
)
// LockKeyspace will lock the keyspace in the topology server.
// UnlockKeyspace should be called if this returns no error.
func (n *ActionNode) LockKeyspace(ts topo.Server, keyspace string, lockTimeout time.Duration, interrupted chan struct{}) (lockPath string, err error) {
log.Infof("Locking keyspace %v for action %v", keyspace, n.Action)
return ts.LockKeyspaceForAction(keyspace, n.ToJson(), lockTimeout, interrupted)
}
// UnlockKeyspace unlocks a previously locked keyspace.
func (n *ActionNode) UnlockKeyspace(ts topo.Server, keyspace string, lockPath string, actionError error) error {
// first update the actionNode
if actionError != nil {
log.Infof("Unlocking keyspace %v for action %v with error %v", keyspace, n.Action, actionError)
n.Error = actionError.Error()
n.State = ACTION_STATE_FAILED
} else {
log.Infof("Unlocking keyspace %v for successful action %v", keyspace, n.Action)
n.Error = ""
n.State = ACTION_STATE_DONE
}
err := ts.UnlockKeyspaceForAction(keyspace, lockPath, n.ToJson())
if actionError != nil {
if err != nil {
// this will be masked
log.Warningf("UnlockKeyspaceForAction failed: %v", err)
}
return actionError
}
return err
}
// LockShard will lock the shard in the topology server.
// UnlockShard should be called if this returns no error.
func (n *ActionNode) LockShard(ts topo.Server, keyspace, shard string, lockTimeout time.Duration, interrupted chan struct{}) (lockPath string, err error) {
log.Infof("Locking shard %v/%v for action %v", keyspace, shard, n.Action)
return ts.LockShardForAction(keyspace, shard, n.ToJson(), lockTimeout, interrupted)
}
// UnlockShard unlocks a previously locked shard.
func (n *ActionNode) UnlockShard(ts topo.Server, keyspace, shard string, lockPath string, actionError error) error {
// first update the actionNode
if actionError != nil {
log.Infof("Unlocking shard %v/%v for action %v with error %v", keyspace, shard, n.Action, actionError)
n.Error = actionError.Error()
n.State = ACTION_STATE_FAILED
} else {
log.Infof("Unlocking shard %v/%v for successful action %v", keyspace, shard, n.Action)
n.Error = ""
n.State = ACTION_STATE_DONE
}
err := ts.UnlockShardForAction(keyspace, shard, lockPath, n.ToJson())
if actionError != nil {
if err != nil {
// this will be masked
log.Warningf("UnlockShardForAction failed: %v", err)
}
return actionError
}
return err
}
// LockSrvShard will lock the serving shard in the topology server.
// UnlockSrvShard should be called if this returns no error.
func (n *ActionNode) LockSrvShard(ts topo.Server, cell, keyspace, shard string, lockTimeout time.Duration, interrupted chan struct{}) (lockPath string, err error) {
log.Infof("Locking serving shard %v/%v/%v for action %v", cell, keyspace, shard, n.Action)
return ts.LockSrvShardForAction(cell, keyspace, shard, n.ToJson(), lockTimeout, interrupted)
}
// UnlockSrvShard unlocks a previously locked serving shard.
func (n *ActionNode) UnlockSrvShard(ts topo.Server, cell, keyspace, shard string, lockPath string, actionError error) error {
// first update the actionNode
if actionError != nil {
log.Infof("Unlocking serving shard %v/%v/%v for action %v with error %v", cell, keyspace, shard, n.Action, actionError)
n.Error = actionError.Error()
n.State = ACTION_STATE_FAILED
} else {
log.Infof("Unlocking serving shard %v/%v/%v for successful action %v", cell, keyspace, shard, n.Action)
n.Error = ""
n.State = ACTION_STATE_DONE
}
err := ts.UnlockSrvShardForAction(cell, keyspace, shard, lockPath, n.ToJson())
if actionError != nil {
if err != nil {
// this will be masked
log.Warningf("UnlockSrvShardForAction failed: %v", err)
}
return actionError
}
return err
}