1package zk 2 3import ( 4 "errors" 5) 6 7const ( 8 protocolVersion = 0 9 10 DefaultPort = 2181 11) 12 13const ( 14 opNotify = 0 15 opCreate = 1 16 opDelete = 2 17 opExists = 3 18 opGetData = 4 19 opSetData = 5 20 opGetAcl = 6 21 opSetAcl = 7 22 opGetChildren = 8 23 opSync = 9 24 opPing = 11 25 opGetChildren2 = 12 26 opCheck = 13 27 opMulti = 14 28 opClose = -11 29 opSetAuth = 100 30 opSetWatches = 101 31 opError = -1 32 // Not in protocol, used internally 33 opWatcherEvent = -2 34) 35 36const ( 37 EventNodeCreated EventType = 1 38 EventNodeDeleted EventType = 2 39 EventNodeDataChanged EventType = 3 40 EventNodeChildrenChanged EventType = 4 41 42 EventSession EventType = -1 43 EventNotWatching EventType = -2 44) 45 46var ( 47 eventNames = map[EventType]string{ 48 EventNodeCreated: "EventNodeCreated", 49 EventNodeDeleted: "EventNodeDeleted", 50 EventNodeDataChanged: "EventNodeDataChanged", 51 EventNodeChildrenChanged: "EventNodeChildrenChanged", 52 EventSession: "EventSession", 53 EventNotWatching: "EventNotWatching", 54 } 55) 56 57const ( 58 StateUnknown State = -1 59 StateDisconnected State = 0 60 StateConnecting State = 1 61 StateAuthFailed State = 4 62 StateConnectedReadOnly State = 5 63 StateSaslAuthenticated State = 6 64 StateExpired State = -112 65 66 StateConnected = State(100) 67 StateHasSession = State(101) 68) 69 70const ( 71 FlagEphemeral = 1 72 FlagSequence = 2 73) 74 75var ( 76 stateNames = map[State]string{ 77 StateUnknown: "StateUnknown", 78 StateDisconnected: "StateDisconnected", 79 StateConnectedReadOnly: "StateConnectedReadOnly", 80 StateSaslAuthenticated: "StateSaslAuthenticated", 81 StateExpired: "StateExpired", 82 StateAuthFailed: "StateAuthFailed", 83 StateConnecting: "StateConnecting", 84 StateConnected: "StateConnected", 85 StateHasSession: "StateHasSession", 86 } 87) 88 89type State int32 90 91func (s State) String() string { 92 if name := stateNames[s]; name != "" { 93 return name 94 } 95 return "Unknown" 96} 97 98type ErrCode int32 99 100var ( 101 ErrConnectionClosed = errors.New("zk: connection closed") 102 ErrUnknown = errors.New("zk: unknown error") 103 ErrAPIError = errors.New("zk: api error") 104 ErrNoNode = errors.New("zk: node does not exist") 105 ErrNoAuth = errors.New("zk: not authenticated") 106 ErrBadVersion = errors.New("zk: version conflict") 107 ErrNoChildrenForEphemerals = errors.New("zk: ephemeral nodes may not have children") 108 ErrNodeExists = errors.New("zk: node already exists") 109 ErrNotEmpty = errors.New("zk: node has children") 110 ErrSessionExpired = errors.New("zk: session has been expired by the server") 111 ErrInvalidACL = errors.New("zk: invalid ACL specified") 112 ErrAuthFailed = errors.New("zk: client authentication failed") 113 ErrClosing = errors.New("zk: zookeeper is closing") 114 ErrNothing = errors.New("zk: no server responsees to process") 115 ErrSessionMoved = errors.New("zk: session moved to another server, so operation is ignored") 116 117 // ErrInvalidCallback = errors.New("zk: invalid callback specified") 118 errCodeToError = map[ErrCode]error{ 119 0: nil, 120 errAPIError: ErrAPIError, 121 errNoNode: ErrNoNode, 122 errNoAuth: ErrNoAuth, 123 errBadVersion: ErrBadVersion, 124 errNoChildrenForEphemerals: ErrNoChildrenForEphemerals, 125 errNodeExists: ErrNodeExists, 126 errNotEmpty: ErrNotEmpty, 127 errSessionExpired: ErrSessionExpired, 128 // errInvalidCallback: ErrInvalidCallback, 129 errInvalidAcl: ErrInvalidACL, 130 errAuthFailed: ErrAuthFailed, 131 errClosing: ErrClosing, 132 errNothing: ErrNothing, 133 errSessionMoved: ErrSessionMoved, 134 } 135) 136 137func (e ErrCode) toError() error { 138 if err, ok := errCodeToError[e]; ok { 139 return err 140 } 141 return ErrUnknown 142} 143 144const ( 145 errOk = 0 146 // System and server-side errors 147 errSystemError = -1 148 errRuntimeInconsistency = -2 149 errDataInconsistency = -3 150 errConnectionLoss = -4 151 errMarshallingError = -5 152 errUnimplemented = -6 153 errOperationTimeout = -7 154 errBadArguments = -8 155 errInvalidState = -9 156 // API errors 157 errAPIError ErrCode = -100 158 errNoNode ErrCode = -101 // * 159 errNoAuth ErrCode = -102 160 errBadVersion ErrCode = -103 // * 161 errNoChildrenForEphemerals ErrCode = -108 162 errNodeExists ErrCode = -110 // * 163 errNotEmpty ErrCode = -111 164 errSessionExpired ErrCode = -112 165 errInvalidCallback ErrCode = -113 166 errInvalidAcl ErrCode = -114 167 errAuthFailed ErrCode = -115 168 errClosing ErrCode = -116 169 errNothing ErrCode = -117 170 errSessionMoved ErrCode = -118 171) 172 173// Constants for ACL permissions 174const ( 175 PermRead = 1 << iota 176 PermWrite 177 PermCreate 178 PermDelete 179 PermAdmin 180 PermAll = 0x1f 181) 182 183var ( 184 emptyPassword = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 185 opNames = map[int32]string{ 186 opNotify: "notify", 187 opCreate: "create", 188 opDelete: "delete", 189 opExists: "exists", 190 opGetData: "getData", 191 opSetData: "setData", 192 opGetAcl: "getACL", 193 opSetAcl: "setACL", 194 opGetChildren: "getChildren", 195 opSync: "sync", 196 opPing: "ping", 197 opGetChildren2: "getChildren2", 198 opCheck: "check", 199 opMulti: "multi", 200 opClose: "close", 201 opSetAuth: "setAuth", 202 opSetWatches: "setWatches", 203 204 opWatcherEvent: "watcherEvent", 205 } 206) 207 208type EventType int32 209 210func (t EventType) String() string { 211 if name := eventNames[t]; name != "" { 212 return name 213 } 214 return "Unknown" 215} 216 217// Mode is used to build custom server modes (leader|follower|standalone). 218type Mode uint8 219 220func (m Mode) String() string { 221 if name := modeNames[m]; name != "" { 222 return name 223 } 224 return "unknown" 225} 226 227const ( 228 ModeUnknown Mode = iota 229 ModeLeader Mode = iota 230 ModeFollower Mode = iota 231 ModeStandalone Mode = iota 232) 233 234var ( 235 modeNames = map[Mode]string{ 236 ModeLeader: "leader", 237 ModeFollower: "follower", 238 ModeStandalone: "standalone", 239 } 240) 241