forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
173 lines (150 loc) · 5.45 KB
/
agent.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2013, 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 zktopo
import (
"fmt"
"path"
"sort"
"strconv"
"strings"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/zk"
"launchpad.net/gozk/zookeeper"
)
/*
This file contains the code to support the local agent process for zktopo.Server
*/
func (zkts *Server) ValidateTabletActions(tabletAlias topo.TabletAlias) error {
actionPath := TabletActionPathForAlias(tabletAlias)
// Ensure that the action node is there. There is no conflict creating
// this node.
_, err := zkts.zconn.Create(actionPath, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
if err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
return err
}
return nil
}
func (zkts *Server) CreateTabletPidNode(tabletAlias topo.TabletAlias, contents string, done chan struct{}) error {
zkTabletPath := TabletPathForAlias(tabletAlias)
path := path.Join(zkTabletPath, "pid")
return zk.CreatePidNode(zkts.zconn, path, contents, done)
}
func (zkts *Server) ValidateTabletPidNode(tabletAlias topo.TabletAlias) error {
zkTabletPath := TabletPathForAlias(tabletAlias)
path := path.Join(zkTabletPath, "pid")
_, _, err := zkts.zconn.Get(path)
return err
}
func (zkts *Server) GetSubprocessFlags() []string {
return zk.GetZkSubprocessFlags()
}
// handleActionQueue will set the watch on the action queue,
// or return an error if it can't.
// It will also process all pending actions, until it can't read one
// or one fails. No error is returned for action failures.
func (zkts *Server) handleActionQueue(tabletAlias topo.TabletAlias, dispatchAction func(actionPath, data string) error) (<-chan zookeeper.Event, error) {
zkActionPath := TabletActionPathForAlias(tabletAlias)
// This read may seem a bit pedantic, but it makes it easier
// for the system to trend towards consistency if an action
// fails or somehow the action queue gets mangled by an errant
// process.
children, _, watch, err := zkts.zconn.ChildrenW(zkActionPath)
if err != nil {
return watch, err
}
if len(children) > 0 {
sort.Strings(children)
for _, child := range children {
actionPath := zkActionPath + "/" + child
if _, err := strconv.ParseUint(child, 10, 64); err != nil {
// This is handy if you want to restart a stuck queue.
// FIXME(msolomon) could listen on the queue node for a change
// generated by a "touch", but listening on two things is a bit
// more complex.
log.Warningf("remove invalid event from action queue: %v", child)
zkts.zconn.Delete(actionPath, -1)
continue
}
data, _, err := zkts.zconn.Get(actionPath)
if err != nil {
log.Errorf("cannot read action %v from zk: %v", actionPath, err)
break
}
if err := dispatchAction(actionPath, data); err != nil {
break
}
}
}
return watch, nil
}
func (zkts *Server) ActionEventLoop(tabletAlias topo.TabletAlias, dispatchAction func(actionPath, data string) error, done chan struct{}) {
for {
// Process any pending actions when we startup, before
// we start listening for events.
watch, err := zkts.handleActionQueue(tabletAlias, dispatchAction)
if err != nil {
log.Warningf("failed to set the watch on action queue, will try again in 5 seconds: %v", err)
time.Sleep(5 * time.Second)
continue
}
select {
case event := <-watch:
if !event.Ok() {
// NOTE(msolomon) The zk meta conn will
// reconnect automatically, or error out.
// At this point, there isn't much to do.
log.Warningf("zookeeper not OK: %v, will try again in 5 seconds", event)
time.Sleep(5 * time.Second)
}
// Otherwise, just handle the queue above.
case <-done:
return
}
}
}
// actionPathToTabletAlias parses an actionPath back
// zkActionPath is /zk/<cell>/vt/tablets/<uid>/action/<number>
func actionPathToTabletAlias(actionPath string) (topo.TabletAlias, error) {
pathParts := strings.Split(actionPath, "/")
if len(pathParts) != 8 || pathParts[0] != "" || pathParts[1] != "zk" || pathParts[3] != "vt" || pathParts[4] != "tablets" || pathParts[6] != "action" {
return topo.TabletAlias{}, fmt.Errorf("invalid action path: %v", actionPath)
}
return topo.ParseTabletAliasString(pathParts[2] + "-" + pathParts[5])
}
func (zkts *Server) ReadTabletActionPath(actionPath string) (topo.TabletAlias, string, int64, error) {
tabletAlias, err := actionPathToTabletAlias(actionPath)
if err != nil {
return topo.TabletAlias{}, "", 0, err
}
data, stat, err := zkts.zconn.Get(actionPath)
if err != nil {
return topo.TabletAlias{}, "", 0, err
}
return tabletAlias, data, int64(stat.Version()), nil
}
func (zkts *Server) UpdateTabletAction(actionPath, data string, version int64) error {
_, err := zkts.zconn.Set(actionPath, data, int(version))
if err != nil {
if zookeeper.IsError(err, zookeeper.ZBADVERSION) {
err = topo.ErrBadVersion
}
return err
}
return nil
}
// StoreTabletActionResponse stores the data both in action and actionlog
func (zkts *Server) StoreTabletActionResponse(actionPath, data string) error {
_, err := zkts.zconn.Set(actionPath, data, -1)
if err != nil {
return err
}
actionLogPath := strings.Replace(actionPath, "/action/", "/actionlog/", 1)
_, err = zk.CreateRecursive(zkts.zconn, actionLogPath, data, 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
return err
}
func (zkts *Server) UnblockTabletAction(actionPath string) error {
return zkts.zconn.Delete(actionPath, -1)
}