zoorest/rest/zoo.go

254 lines
5.0 KiB
Go
Raw Normal View History

2017-02-15 17:09:28 +03:00
package rest
import (
"github.com/samuel/go-zookeeper/zk"
"log"
"strings"
"time"
)
//ZooNode zookeeper node
type ZooNode struct {
Path string
Conn *zk.Conn
Zoo Zk
2017-06-22 17:06:39 +03:00
MC MC
2017-02-15 17:09:28 +03:00
}
//Zk Zookeeper connection settings
type Zk struct {
Hosts []string
Path string
Tick int
}
//InitConnection Initialize Zookeeper connection
func (conf Zk) InitConnection() (*zk.Conn, error) {
conn, _, err := zk.Connect(conf.Hosts, time.Second)
if err != nil {
log.Panic("[ERROR] ", err)
}
return conn, err
}
// GetChildrens get Zookeeper node childrens
2017-02-15 17:09:28 +03:00
func (z ZooNode) GetChildrens(path string) Ls {
var lsPath string
lsPath = strings.Join([]string{z.Path, path}, "")
if path == "/" {
lsPath = z.Path
}
if strings.Contains(lsPath, "//") {
lsPath = strings.Replace(lsPath, "//", "/", 1)
}
2017-06-22 17:06:39 +03:00
log.Print("V1 LS: ", lsPath)
2017-02-15 17:09:28 +03:00
var l Ls
l.State = "OK"
2017-06-22 17:06:39 +03:00
l.Path = path
2017-06-23 10:22:49 +03:00
// if z.MC.Enabled {
// data, err := z.MC.GetFromCache(lsPath)
// if err != nil {
// log.Print("V1 LS ERROR: ", err.Error())
// } else {
// log.Print("We are get it from memecache!")
// childrens := strings.Split(string(data), ",")
// l.Childrens = childrens
// return l
// }
// }
2017-02-15 17:09:28 +03:00
childrens, zkStat, err := z.Conn.Children(lsPath)
2017-02-15 17:09:28 +03:00
if err != nil {
l.State = "ERROR"
2017-02-15 17:24:58 +03:00
l.Error = err.Error()
2017-02-15 17:09:28 +03:00
return l
}
2017-06-23 10:22:49 +03:00
// // Store to cache
// if z.MC.Enabled {
// err := z.MC.StoreToCache(lsPath, []byte(strings.Join(childrens, ",")))
// if err != nil {
// log.Print("V1 LS: ", err.Error())
// }
// }
2017-06-22 17:06:39 +03:00
2017-02-15 17:24:58 +03:00
l.Error = ""
2017-02-15 17:09:28 +03:00
l.Childrens = childrens
l.ZkStat = zkStat
2017-02-15 17:09:28 +03:00
return l
}
2017-06-22 17:06:39 +03:00
// GetNode data
2017-02-15 17:09:28 +03:00
func (z ZooNode) GetNode(path string) Get {
var getPath string
getPath = strings.Join([]string{z.Path, path}, "")
if path == "/" {
getPath = z.Path
}
if strings.Contains(getPath, "//") {
getPath = strings.Replace(getPath, "//", "/", 1)
}
2017-06-22 17:06:39 +03:00
log.Print("V1 GET: ", getPath)
2017-02-15 17:09:28 +03:00
var g Get
g.State = "OK"
2017-06-22 17:06:39 +03:00
g.Path = path
// Get data from memcached
if z.MC.Enabled {
if data, err := z.MC.GetFromCache(getPath); err != nil {
2017-06-23 10:22:49 +03:00
log.Print("[mc ERROR] ", err.Error())
2017-06-22 17:06:39 +03:00
} else {
g.Data = data
return g
}
}
2017-02-15 17:09:28 +03:00
data, zkStat, err := z.Conn.Get(getPath)
2017-02-15 17:09:28 +03:00
if err != nil {
g.State = "ERROR"
2017-02-15 17:24:58 +03:00
g.Error = err.Error()
2017-02-15 17:09:28 +03:00
return g
}
2017-06-22 17:06:39 +03:00
// Store to cache
if z.MC.Enabled {
err := z.MC.StoreToCache(getPath, data)
if err != nil {
2017-06-23 10:22:49 +03:00
log.Print("[mc ERROR] ", err.Error())
2017-06-22 17:06:39 +03:00
}
}
2017-02-15 17:24:58 +03:00
g.Error = ""
2017-02-15 17:09:28 +03:00
g.Data = data
g.ZkStat = zkStat
2017-02-15 17:09:28 +03:00
return g
}
2017-02-15 19:13:12 +03:00
//RMR remove Zk node recursive
func (z ZooNode) RMR(path string) {
2017-02-17 12:34:43 +03:00
log.Print("rm: ", path)
c, _, err := z.Conn.Children(path)
2017-02-15 19:13:12 +03:00
if err != nil {
log.Print("[zk ERROR] ", err)
}
log.Print("[WARNING] Trying delete ", path)
if len(c) > 0 {
for _, child := range c {
2017-02-17 12:34:43 +03:00
childPath := strings.Join([]string{path, child}, "/")
2017-02-15 19:13:12 +03:00
z.RMR(childPath)
}
}
err = z.Conn.Delete(path, -1)
if err != nil {
log.Print("[zk ERROR] ", err)
2017-06-23 10:22:49 +03:00
} else {
if z.MC.Enabled {
err := z.MC.DeleteFromCache(path)
if err != nil {
log.Print("[mc ERROR] ", err.Error())
}
}
log.Print("[WARNING] ", path, " deleted")
2017-02-15 19:13:12 +03:00
}
}
2017-02-17 12:22:50 +03:00
// CreateNode ...
func (z ZooNode) CreateNode(path string, content []byte) string {
createPath := strings.Join([]string{z.Path, path}, "")
if strings.Contains(createPath, "//") {
createPath = strings.Replace(createPath, "//", "/", 1)
}
if path == "/" {
return "ERROR: Not creating root path\n"
}
log.Print("Creating ", createPath)
_, err := z.EnsureZooPath(createPath)
if err != nil {
return err.Error()
}
return z.UpdateNode(path, content)
}
// UpdateNode update existing node
2017-02-17 12:22:50 +03:00
func (z ZooNode) UpdateNode(path string, content []byte) string {
upPath := strings.Join([]string{z.Path, path}, "")
if strings.Contains(upPath, "//") {
upPath = strings.Replace(upPath, "//", "/", 1)
}
if upPath == "/" {
return "Not updating root path"
}
_, err := z.Conn.Set(upPath, content, -1)
if err != nil {
return err.Error()
}
2017-06-22 17:06:39 +03:00
if z.MC.Enabled {
if err := z.MC.StoreToCache(upPath, content); err != nil {
2017-06-23 10:22:49 +03:00
log.Print("[mc ERROR] ", err.Error())
2017-06-22 17:06:39 +03:00
}
}
2017-02-25 16:00:43 +03:00
return path
2017-02-17 12:22:50 +03:00
}
// CreateChild create child in /node/path
func (z ZooNode) CreateChild(path string, content []byte) string {
crPath := strings.Join([]string{z.Path, path}, "")
if strings.Contains(crPath, "//") {
crPath = strings.Replace(crPath, "//", "/", 1)
}
if crPath == "/" {
return "Not updating root path"
}
_, err := z.Conn.Create(crPath, content, 0, zk.WorldACL(zk.PermAll))
if err != nil {
return err.Error()
}
2017-06-22 17:06:39 +03:00
if z.MC.Enabled {
if err := z.MC.StoreToCache(crPath, content); err != nil {
2017-06-23 10:22:49 +03:00
log.Print("[mc ERROR] ", err.Error())
2017-06-22 17:06:39 +03:00
}
}
return path
}
2017-02-17 12:22:50 +03:00
//EnsureZooPath create zookeeper path
func (z ZooNode) EnsureZooPath(path string) (string, error) {
flag := int32(0)
acl := zk.WorldACL(zk.PermAll)
s := strings.Split(path, "/")
var p []string
var fullnodepath string
for i := 1; i < len(s); i++ {
p = append(p, strings.Join([]string{"/", s[i]}, ""))
}
for i := 0; i < len(p); i++ {
fullnodepath = strings.Join([]string{fullnodepath, p[i]}, "")
exists, _, _ := z.Conn.Exists(fullnodepath)
if !exists {
z.Conn.Create(fullnodepath, []byte(""), flag, acl)
}
}
2017-02-25 16:00:43 +03:00
return path, nil
2017-02-17 12:22:50 +03:00
}