|
| 1 | +package lock |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/url" |
| 9 | + "time" |
| 10 | + |
| 11 | + transport "go.etcd.io/etcd/client/pkg/v3/transport" |
| 12 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + keyTemplate = "com.coreos.airlock/groups/%s/v1/semaphore" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + // ErrNilEtcdManager is returned on nil manager |
| 21 | + ErrNilEtcdManager = errors.New("nil EtcdManager") |
| 22 | +) |
| 23 | + |
| 24 | +// EtcdManager takes care of locking for clients |
| 25 | +type EtcdManager struct { |
| 26 | + client *clientv3.Client |
| 27 | + keyPath string |
| 28 | +} |
| 29 | + |
| 30 | +// NewEtcdManager returns a new lock manager, ensuring the underlying semaphore is initialized. |
| 31 | +func NewEtcdManager(ctx context.Context, etcdURLs []string, certPubPath string, certKeyPath string, txnTimeoutMs time.Duration, group string, slots uint64) (*EtcdManager, error) { |
| 32 | + tlsInfo := transport.TLSInfo{ |
| 33 | + CertFile: certPubPath, |
| 34 | + KeyFile: certKeyPath, |
| 35 | + } |
| 36 | + |
| 37 | + tlsConfig, err := tlsInfo.ClientConfig() |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + client, err := clientv3.New(clientv3.Config{ |
| 43 | + Endpoints: etcdURLs, |
| 44 | + DialTimeout: time.Duration(txnTimeoutMs) * time.Millisecond, |
| 45 | + TLS: tlsConfig, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + keyPath := fmt.Sprintf(keyTemplate, url.QueryEscape(group)) |
| 52 | + manager := EtcdManager{client, keyPath} |
| 53 | + |
| 54 | + if err := manager.ensureInit(ctx, slots); err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + return &manager, nil |
| 59 | +} |
| 60 | + |
| 61 | +// RecursiveLock adds this lock `id` as a holder of the semaphore |
| 62 | +// |
| 63 | +// It will return an error if there is a problem getting or setting the |
| 64 | +// semaphore, or if the maximum number of holders has been reached. |
| 65 | +func (m *EtcdManager) RecursiveLock(ctx context.Context, id string) (*Semaphore, error) { |
| 66 | + sem, version, err := m.get(ctx) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + held, err := sem.RecursiveLock(id) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + if held { |
| 76 | + return sem, nil |
| 77 | + } |
| 78 | + |
| 79 | + if err := m.set(ctx, sem, version); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + return sem, nil |
| 84 | +} |
| 85 | + |
| 86 | +// UnlockIfHeld removes this lock `id` as a holder of the semaphore |
| 87 | +// |
| 88 | +// It returns an error if there is a problem getting or setting the semaphore. |
| 89 | +func (m *EtcdManager) UnlockIfHeld(ctx context.Context, id string) (*Semaphore, error) { |
| 90 | + sem, version, err := m.get(ctx) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + if err := sem.UnlockIfHeld(id); err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + if err := m.set(ctx, sem, version); err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + return sem, nil |
| 104 | +} |
| 105 | + |
| 106 | +// FetchSemaphore fetches current semaphore version |
| 107 | +func (m *EtcdManager) FetchSemaphore(ctx context.Context) (*Semaphore, error) { |
| 108 | + semaphore, _, err := m.get(ctx) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + return semaphore, nil |
| 114 | +} |
| 115 | + |
| 116 | +// Close reaps all running goroutines |
| 117 | +func (m *EtcdManager) Close() { |
| 118 | + if m == nil { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + m.client.Close() |
| 123 | +} |
| 124 | + |
| 125 | +// ensureInit initialize the semaphore in etcd, if it does not exist yet |
| 126 | +func (m *EtcdManager) ensureInit(ctx context.Context, slots uint64) error { |
| 127 | + if m == nil { |
| 128 | + return ErrNilEtcdManager |
| 129 | + } |
| 130 | + |
| 131 | + sem := NewSemaphore(slots) |
| 132 | + semValue, err := sem.String() |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + _, err = m.client.Txn(ctx).If( |
| 138 | + // version=0 means that the key does not exist. |
| 139 | + clientv3.Compare(clientv3.Version(m.keyPath), "=", 0), |
| 140 | + ).Then( |
| 141 | + clientv3.OpPut(m.keyPath, semValue), |
| 142 | + ).Commit() |
| 143 | + |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +// get returns the current semaphore value and version, or an error |
| 151 | +func (m *EtcdManager) get(ctx context.Context) (*Semaphore, int64, error) { |
| 152 | + resp, err := m.client.Get(ctx, m.keyPath) |
| 153 | + if err != nil { |
| 154 | + return nil, 0, err |
| 155 | + } |
| 156 | + if resp.Count != 1 { |
| 157 | + return nil, 0, fmt.Errorf("unexpected number of results: %d", resp.Count) |
| 158 | + } |
| 159 | + |
| 160 | + var data []byte |
| 161 | + var version int64 |
| 162 | + for _, kv := range resp.Kvs { |
| 163 | + data = kv.Value |
| 164 | + version = kv.Version |
| 165 | + break |
| 166 | + } |
| 167 | + if version == 0 { |
| 168 | + return nil, 0, errors.New("key at version 0") |
| 169 | + } |
| 170 | + if len(data) == 0 { |
| 171 | + return nil, 0, errors.New("empty semaphore value") |
| 172 | + } |
| 173 | + |
| 174 | + sem := &Semaphore{} |
| 175 | + err = json.Unmarshal(data, sem) |
| 176 | + if err != nil { |
| 177 | + return nil, 0, err |
| 178 | + } |
| 179 | + |
| 180 | + return sem, version, nil |
| 181 | +} |
| 182 | + |
| 183 | +// set updates the semaphore in etcd, if `version` matches the one previously observed |
| 184 | +func (m *EtcdManager) set(ctx context.Context, sem *Semaphore, version int64) error { |
| 185 | + if m == nil { |
| 186 | + return ErrNilEtcdManager |
| 187 | + } |
| 188 | + if sem == nil { |
| 189 | + return ErrNilSemaphore |
| 190 | + } |
| 191 | + |
| 192 | + data, err := json.Marshal(sem) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + // Conditionally Put if version in etcd is still the same we observed. |
| 198 | + // If the condition is not met, the transaction will return as "not succeeding". |
| 199 | + resp, err := m.client.Txn(ctx).If( |
| 200 | + clientv3.Compare(clientv3.Version(m.keyPath), "=", version), |
| 201 | + ).Then( |
| 202 | + clientv3.OpPut(m.keyPath, string(data)), |
| 203 | + ).Commit() |
| 204 | + |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + if !resp.Succeeded { |
| 209 | + return errors.New("conflict on semaphore detected, aborting") |
| 210 | + } |
| 211 | + |
| 212 | + return nil |
| 213 | +} |
0 commit comments