|
| 1 | +package lock |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + log "github.com/Sirupsen/logrus" |
| 11 | + "github.com/coreos/etcd/client" |
| 12 | + "golang.org/x/net/context" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + defaultTTL = 60 |
| 17 | + defaultTry = 3 |
| 18 | +) |
| 19 | + |
| 20 | +// A Mutex is a mutual exclusion lock which is distributed across a cluster. |
| 21 | +type Mutex struct { |
| 22 | + key string |
| 23 | + id string // The identity of the caller |
| 24 | + kapi client.KeysAPI |
| 25 | + ttl time.Duration |
| 26 | + mutex sync.Mutex |
| 27 | +} |
| 28 | + |
| 29 | +// New creates a Mutex with the given key which must be the same |
| 30 | +// across the cluster nodes. |
| 31 | +// machines are the ectd cluster addresses |
| 32 | +func NewMutex(c client.KeysAPI, key string, ttl int) *Mutex { |
| 33 | + hostname, err := os.Hostname() |
| 34 | + if err != nil || key == "" { |
| 35 | + return nil |
| 36 | + } |
| 37 | + |
| 38 | + if !strings.HasPrefix(key, "/") { |
| 39 | + key = fmt.Sprintf("/%s", key) |
| 40 | + } |
| 41 | + |
| 42 | + if ttl < 1 { |
| 43 | + ttl = defaultTTL |
| 44 | + } |
| 45 | + |
| 46 | + return &Mutex{ |
| 47 | + key: key, |
| 48 | + id: fmt.Sprintf("%v-%v-%v", hostname, os.Getpid(), time.Now().Format("20060102-15:04:05.999999999")), |
| 49 | + kapi: c, |
| 50 | + ttl: time.Second * time.Duration(ttl), |
| 51 | + mutex: sync.Mutex{}, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Lock locks m. |
| 56 | +// If the lock is already in use, the calling goroutine |
| 57 | +// blocks until the mutex is available. |
| 58 | +func (m *Mutex) Lock() (err error) { |
| 59 | + m.mutex.Lock() |
| 60 | + for try := 1; try <= defaultTry; try++ { |
| 61 | + err = m.lock() |
| 62 | + if err == nil { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + log.Debugf("%s Lock node %v ERROR %v", m.id, m.key, err) |
| 67 | + |
| 68 | + if try < defaultTry { |
| 69 | + log.Debugf("%s Try to lock node %v again ERROR, %v", m.id, m.key, err) |
| 70 | + } |
| 71 | + } |
| 72 | + return err |
| 73 | +} |
| 74 | + |
| 75 | +func (m *Mutex) lock() (err error) { |
| 76 | + log.Debugf("%s Trying to create a node : key=%v", m.id, m.key) |
| 77 | + setOptions := &client.SetOptions{ |
| 78 | + PrevExist: client.PrevNoExist, |
| 79 | + TTL: m.ttl, |
| 80 | + } |
| 81 | + for { |
| 82 | + resp, err := m.kapi.Set(context.TODO(), m.key, m.id, setOptions) |
| 83 | + if err == nil { |
| 84 | + log.Debugf("%s Create node %v OK [%q]", m.id, m.key, resp) |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + log.Debugf("%s Create node %v failed [%v]", m.id, m.key, err) |
| 89 | + e, ok := err.(client.Error) |
| 90 | + if !ok { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + if e.Code != client.ErrorCodeNodeExist { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + // Get the already node's value. |
| 99 | + resp, err = m.kapi.Get(context.TODO(), m.key, nil) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + log.Debugf("%s, Get node %v OK", m.id, m.key) |
| 104 | + watcherOptions := &client.WatcherOptions{ |
| 105 | + AfterIndex: resp.Index, |
| 106 | + Recursive: false, |
| 107 | + } |
| 108 | + watcher := m.kapi.Watcher(m.key, watcherOptions) |
| 109 | + for { |
| 110 | + log.Debugf("%s Watch %v ...", m.id, m.key) |
| 111 | + resp, err = watcher.Next(context.TODO()) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + log.Debugf("%s Received an event: %q", m.id, resp) |
| 117 | + if resp.Action == "delete" || resp.Action == "expire" { |
| 118 | + // break this for-loop, and try to create the node again. |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return err |
| 124 | +} |
| 125 | + |
| 126 | +// Unlock unlocks m. |
| 127 | +// It is a run-time error if m is not locked on entry to Unlock. |
| 128 | +// |
| 129 | +// A locked Mutex is not associated with a particular goroutine. |
| 130 | +// It is allowed for one goroutine to lock a Mutex and then |
| 131 | +// arrange for another goroutine to unlock it. |
| 132 | +func (m *Mutex) Unlock() (err error) { |
| 133 | + defer m.mutex.Unlock() |
| 134 | + for i := 1; i <= defaultTry; i++ { |
| 135 | + var resp *client.Response |
| 136 | + resp, err = m.kapi.Delete(context.TODO(), m.key, nil) |
| 137 | + if err == nil { |
| 138 | + log.Debugf("%s Delete %v OK", m.id, m.key) |
| 139 | + return nil |
| 140 | + } |
| 141 | + log.Debugf("%s Delete %v failed: %q", m.id, m.key, resp) |
| 142 | + e, ok := err.(client.Error) |
| 143 | + if ok && e.Code == client.ErrorCodeKeyNotFound { |
| 144 | + return nil |
| 145 | + } |
| 146 | + } |
| 147 | + return err |
| 148 | +} |
0 commit comments