本文整理匯總了Golang中github.com/samuel/go-zookeeper/zk.NewLock函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewLock函數的具體用法?Golang NewLock怎麽用?Golang NewLock使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewLock函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateConnection
func (mgr *MetadataManager) CreateConnection() {
if mgr.zkConn != nil {
// Return if the connection is good already
if mgr.zkConn.State() == zk.StateConnected ||
mgr.zkConn.State() == zk.StateHasSession ||
mgr.zkConn.State() == zk.StateConnecting {
return
}
// Close the connection because it probably expired
mgr.zkConn.Close()
}
conn, _, err := zk.Connect(mgr.zookeepers, time.Second)
if err != nil {
conn.Close()
log.Panic(err)
}
bns := baseNamespace{}
ns := makeSubSpace(makeSubSpace(makeSubSpace(bns, "riak"), "frameworks"), mgr.frameworkID)
lockPath := makeSubSpace(ns, "lock")
zkLock := zk.NewLock(conn, lockPath.GetZKPath(), zk.WorldACL(zk.PermAll))
mgr.zkConn = conn
mgr.namespace = ns
mgr.zkLock = *zkLock
}
示例2: AddQueue
//Add a queue by name
func (m *Metadata) AddQueue(queue string) error {
mutex := zk.NewLock(m.zkClient.Conn, m.operationPath, zk.WorldACL(zk.PermAll))
if err := mutex.Lock(); err != nil {
return errors.Trace(err)
}
defer mutex.Unlock()
if err := m.RefreshMetadata(); err != nil {
return errors.Trace(err)
}
if exist := m.ExistQueue(queue); exist {
return errors.AlreadyExistsf("CreateQueue queue:%s ", queue)
}
if err := m.manager.CreateTopic(queue, int32(m.config.KafkaReplications),
int32(m.config.KafkaPartitions)); err != nil {
return errors.Trace(err)
}
path := m.buildQueuePath(queue)
data := ""
log.Debugf("add queue, zk path:%s, data:%s", path, data)
if err := m.zkClient.CreateRec(path, data, 0); err != nil {
return errors.Trace(err)
}
return nil
}
示例3: DelQueue
//Delete a queue by name
func (m *Metadata) DelQueue(queue string) error {
mutex := zk.NewLock(m.zkClient.Conn, m.operationPath, zk.WorldACL(zk.PermAll))
if err := mutex.Lock(); err != nil {
return errors.Trace(err)
}
defer mutex.Unlock()
if err := m.RefreshMetadata(); err != nil {
return errors.Trace(err)
}
can, err := m.canDeleteQueue(queue)
if err != nil {
return errors.Trace(err)
}
if !can {
return errors.NotValidf("DeleteQueue queue:%s has one or more group", queue)
}
path := m.buildQueuePath(queue)
log.Debugf("del queue, zk path:%s", path)
if err := m.zkClient.DeleteRec(path); err != nil {
return errors.Trace(err)
}
delete(m.queueConfigs, queue)
if err := m.manager.DeleteTopic(queue); err != nil {
return errors.Trace(err)
}
return nil
}
示例4: attemptLock
func (i *ZookeeperHALock) attemptLock(lockpath string, didLock chan struct{}, failLock chan error, releaseCh chan bool) {
// Wait to acquire the lock in ZK
lock := zk.NewLock(i.in.client, lockpath, i.in.acl)
err := lock.Lock()
if err != nil {
failLock <- err
return
}
// Set node value
data := []byte(i.value)
err = i.in.ensurePath(lockpath, data)
if err != nil {
failLock <- err
lock.Unlock()
return
}
i.zkLock = lock
// Signal that lock is held
close(didLock)
// Handle an early abort
release := <-releaseCh
if release {
lock.Unlock()
}
}
示例5: UpdateGroupConfig
func (m *Metadata) UpdateGroupConfig(group string, queue string,
write bool, read bool, url string, ips []string) error {
mutex := zk.NewLock(m.zkClient.Conn, m.operationPath, zk.WorldACL(zk.PermAll))
if err := mutex.Lock(); err != nil {
return errors.Trace(err)
}
defer mutex.Unlock()
if err := m.RefreshMetadata(); err != nil {
return errors.Trace(err)
}
if exist := m.ExistGroup(queue, group); !exist {
return errors.NotFoundf("queue : %q, group: %q", queue, group)
}
path := m.buildConfigPath(group, queue)
groupConfig := GroupConfig{
Group: group,
Queue: queue,
Write: write,
Read: read,
Url: url,
Ips: ips,
}
data := groupConfig.String()
log.Debugf("update group config, zk path:%s, data:%s", path, data)
if err := m.zkClient.Set(path, data); err != nil {
return errors.Trace(err)
}
return nil
}
示例6: campaign
func (c *Candidate) campaign() {
defer close(c.electedChn)
defer close(c.errChn)
for {
c.setLeader(false)
if c.zkLock == nil {
c.zkLock = zk.NewLock(c.store.conn, c.key, zk.WorldACL(zk.PermAll))
}
err := c.zkLock.Lock()
if err != nil {
log.Info("Get Lock error :", err)
c.errChn <- err
return
}
//good, we are now the leader
log.Info("Get the leader lock success")
c.setLeader(true)
select {
case <-c.resignChn:
c.zkLock.Unlock()
case <-c.stopChn:
if c.IsLeader() {
c.zkLock.Lock()
}
return
}
}
}
示例7: NewStore
func NewStore(s *Server) *Storage {
conn, connEvent, err := zk.Connect([]string{"127.0.0.1"}, defaultLeaderTime)
if err != nil {
panic(err)
log.Error("Zk連接創建失敗...", err)
}
log.Info("Zk連接創建成功...")
execIdLock := zk.NewLock(conn, "/swiss/ids/execid", WorldACLPermAll)
jobIdLock := zk.NewLock(conn, "/swiss/ids/jobid", WorldACLPermAll)
store := &Storage{conn: conn, connEvent: connEvent, execIdLock: execIdLock, jobIdLock: jobIdLock, server: s}
store.initBasePath()
return store
}
示例8: unbinding
func unbinding(portUuid, hostUuid string) error {
log.Println("unbinding port " + portUuid)
conn, _, err := connect()
if err != nil {
return err
}
defer conn.Close()
lock := zk.NewLock(conn, GetLockPath(lockKey), zk.WorldACL(zk.PermAll))
if err = lock.Lock(); err != nil {
return err
}
defer lock.Unlock()
portPath := GetPortPath(portUuid)
var data []byte
if data, _, err = conn.Get(portPath); err != nil {
fmt.Fprintf(os.Stderr, "Error on getting port: %s\n", err.Error())
return err
}
port := &WrappedPort{}
if err = json.Unmarshal(data, port); err != nil {
fmt.Fprintf(os.Stderr, "Error on deserializing port: %s\n", err.Error())
return err
}
if port.Data.HostId != hostUuid {
return errors.New("The given host ID didn't match with one in NSDB")
}
port.Data.InterfaceName = ""
updatedPort, err := json.Marshal(port)
if err != nil {
fmt.Fprintf(os.Stderr, "Error on serializing port: %s\n", err.Error())
return err
}
if _, err = conn.Set(portPath, updatedPort, -1); err != nil {
return err
}
vrnMappingPath := GetVrnMappingPath(hostUuid, portUuid)
var exists bool
if exists, _, err = conn.Exists(vrnMappingPath); err != nil {
fmt.Fprintf(os.Stderr, "Error on examining vrnMapping %s\n", err.Error())
return err
}
if exists {
if err = conn.Delete(vrnMappingPath, -1); err != nil {
fmt.Fprintf(os.Stderr, "Error on deleging vrnMapping %s\n", err.Error())
return err
}
}
log.Println("Succeded to unbind the port")
return nil
}
示例9: acquireLock
func acquireLock(zookeepers []string, sessionTimeout time.Duration, lockPath string) (*lock, error) {
conn, _, err := zk.Connect(zookeepers, sessionTimeout)
if err != nil {
return nil, err
}
zkLock := zk.NewLock(conn, lockPath, zk.WorldACL(zk.PermAll))
err = zkLock.Lock()
if err != nil {
return nil, err
} else {
return &lock{zkLock}, nil
}
}
示例10: NewLock
// NewLock returns a handle to a lock struct which can be used to acquire and
// release the mutex.
func (s *Zookeeper) NewLock(key string, options *LockOptions) (Locker, error) {
value := []byte("")
// Apply options
if options != nil {
if options.Value != nil {
value = options.Value
}
}
return &zookeeperLock{
client: s.client,
key: normalize(key),
value: value,
lock: zk.NewLock(s.client, normalize(key), zk.WorldACL(zk.PermAll)),
}, nil
}
示例11: Select
func (c *Client) Select(queue, msgID string) (string, error) {
if err := c.assertConnected(); err != nil {
return "", err
}
lock := zk.NewLock(c.zk, zRoot+zLock, acl)
if err := lock.Lock(); err != nil {
return "", err
}
defer lock.Unlock()
txnZNode := fmt.Sprintf("%s%s/%s", zRoot, zTxn, msgID)
if _, err := c.zk.Sync(txnZNode); err != nil {
return "", err
}
exists, _, err := c.zk.Exists(txnZNode)
if err != nil {
return "", err
}
defer c.mark(msgID)
if exists {
return c.getSelected(msgID)
}
if err := c.createTxn(queue, msgID); err != nil {
return "", err
}
selected, err := c.selectCandidate(queue)
if err != nil {
return "", err
}
if _, err := c.zk.Create(fmt.Sprintf("%s%s/%s", txnZNode, zSelected, selected), nil, 0, acl); err != nil {
return "", err
}
clientAndID := strings.Split(selected, "-")
if clientAndID[0] != c.clientID {
return "", ErrNoSelection
}
return clientAndID[1], nil
}
示例12: NewLock
// NewLock returns a handle to a lock struct which can
// be used to provide mutual exclusion on a key
func (s *Zookeeper) NewLock(key string, options *store.LockOptions) (lock store.Locker, err error) {
value := []byte("")
// Apply options
if options != nil {
if options.Value != nil {
value = options.Value
}
}
lock = &zookeeperLock{
client: s.client,
key: store.Normalize(key),
value: value,
lock: zk.NewLock(s.client, store.Normalize(key), zk.WorldACL(zk.PermAll)),
}
return lock, err
}
示例13: NewMetadataManager
func NewMetadataManager(frameworkID string, zookeepers []string) *MetadataManager {
conn, _, err := zk.Connect(zookeepers, time.Second)
if err != nil {
log.Panic(err)
}
bns := baseNamespace{}
ns := makeSubSpace(makeSubSpace(makeSubSpace(bns, "riak"), "frameworks"), frameworkID)
lockPath := makeSubSpace(ns, "lock")
zkLock := zk.NewLock(conn, lockPath.GetZKPath(), zk.WorldACL(zk.PermAll))
manager := &MetadataManager{
lock: &sync.Mutex{},
frameworkID: frameworkID,
zkConn: conn,
namespace: ns,
zkLock: *zkLock,
}
manager.setup()
return manager
}
示例14: removeId
func removeId(appId, taskId string) {
zkPath := filepath.Join(zkBase, appId)
conn := connect()
defer conn.Close()
exists, _, err := conn.Exists(zkPath)
check(err)
if !exists {
return
}
lock := zk.NewLock(conn, zkPath, acl)
lock.Lock()
defer lock.Unlock()
contents, stat, err := conn.Get(zkPath)
if len(contents) == 0 {
return
}
newContents := strings.Replace(string(contents), taskId, "NA", 1)
_, err = conn.Set(zkPath, []byte(newContents), stat.Version)
check(err)
return
}
示例15: Subscribe
func (c *Client) Subscribe(id, queue string) error {
if err := c.assertConnected(); err != nil {
return err
}
if _, err := c.createIfNotExists(fmt.Sprintf("%s%s/%s", zRoot, zQueue, queue), nil, 0, acl); err != nil {
return err
}
lock := zk.NewLock(c.zk, zRoot+zLock, acl)
if err := lock.Lock(); err != nil {
return err
}
defer lock.Unlock()
zNode := fmt.Sprintf("%s%s/%s/%s-%s", zRoot, zQueue, queue, c.clientID, id)
if _, err := c.createIfNotExists(zNode, nil, zk.FlagEphemeral, acl); err != nil {
return err
}
return nil
}