Skip to content

Commit 380e929

Browse files
committed
Merge branch 'new_etcd' into 'master'
New etcd See merge request !145
2 parents dc2e76d + 5d3cb54 commit 380e929

File tree

6 files changed

+20
-31
lines changed

6 files changed

+20
-31
lines changed

core.yaml.sample

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ docker:
2828
local_dns: true
2929

3030
scheduler:
31-
lock_key: "_scheduler_lock"
32-
lock_ttl: 10
3331
maxshare: -1
3432
sharebase: 10
3533

lock/etcdlock/lock.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,52 @@ package etcdlock
33
import (
44
"fmt"
55
"strings"
6-
"sync"
6+
"time"
77

88
"github.com/coreos/etcd/clientv3"
99
"github.com/coreos/etcd/clientv3/concurrency"
1010
"golang.org/x/net/context"
1111
)
1212

13-
const defaultTTL = 60
13+
const defaultTTL = 30
1414

1515
type Mutex struct {
16-
mutex sync.Mutex
17-
lock *concurrency.Mutex
16+
timeout time.Duration
17+
mutex *concurrency.Mutex
1818
session *concurrency.Session
1919
}
2020

2121
func New(cli *clientv3.Client, key string, ttl int) (*Mutex, error) {
2222
if key == "" {
23-
return nil, fmt.Errorf("Where is lock key")
23+
return nil, fmt.Errorf("[etcdlock] no lock key")
2424
}
2525

2626
if !strings.HasPrefix(key, "/") {
2727
key = fmt.Sprintf("/%s", key)
2828
}
2929

30-
if ttl < 1 {
30+
if ttl <= 0 {
3131
ttl = defaultTTL
3232
}
3333

34-
s, err := concurrency.NewSession(
35-
cli,
36-
concurrency.WithTTL(ttl),
37-
concurrency.WithContext(context.Background()),
38-
)
34+
session, err := concurrency.NewSession(cli, concurrency.WithTTL(ttl))
3935
if err != nil {
4036
return nil, err
4137
}
42-
lock := concurrency.NewMutex(s, key)
43-
return &Mutex{mutex: sync.Mutex{}, lock: lock, session: s}, nil
38+
39+
mutex := &Mutex{mutex: concurrency.NewMutex(session, key), session: session}
40+
mutex.timeout = time.Duration(ttl) * time.Second
41+
return mutex, nil
4442
}
4543

4644
func (m *Mutex) Lock() error {
47-
m.mutex.Lock()
48-
return m.lock.Lock(context.TODO())
45+
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
46+
defer cancel()
47+
return m.mutex.Lock(ctx)
4948
}
5049

5150
func (m *Mutex) Unlock() error {
52-
defer m.mutex.Unlock()
5351
defer m.session.Close()
54-
return m.lock.Unlock(context.TODO())
52+
// 一定要释放
53+
return m.mutex.Unlock(context.Background())
5554
}

rpc/rpc_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ func initConfig(mStore *mockstore.MockStore) (types.Config, *vibranium) {
200200
SCMType: "gitlab",
201201
},
202202
Scheduler: types.SchedConfig{
203-
LockKey: "_scheduler_lock",
204-
LockTTL: 10,
203+
MaxShare: -1,
204+
ShareBase: 10,
205205
},
206206
Syslog: types.SyslogConfig{
207207
Address: "udp://localhost:5111",

scheduler/complex/potassium_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ func newPotassium() (*potassium, error) {
2323
EtcdMachines: []string{"http://127.0.0.1:2379"},
2424
EtcdLockPrefix: "/eru-core/_lock",
2525
Scheduler: types.SchedConfig{
26-
LockKey: "/coretest",
27-
LockTTL: 1,
2826
ShareBase: 10,
2927
MaxShare: -1,
3028
},
@@ -186,8 +184,6 @@ func TestComplexNodes(t *testing.T) {
186184
EtcdMachines: []string{"http://127.0.0.1:2379"},
187185
EtcdLockPrefix: "/eru-core/_lock",
188186
Scheduler: types.SchedConfig{
189-
LockKey: "/coretest",
190-
LockTTL: 1,
191187
ShareBase: 10,
192188
MaxShare: -1,
193189
},

scheduler/scheduler_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ func TestSchedulerInvoke(t *testing.T) {
1313
EtcdMachines: []string{"http://127.0.0.1:2379"},
1414
EtcdLockPrefix: "/eru-core/_lock",
1515
Scheduler: types.SchedConfig{
16-
LockKey: "/coretest",
17-
LockTTL: 1,
1816
ShareBase: 10,
1917
MaxShare: -1,
2018
},

types/config.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ type DockerConfig struct {
4343

4444
// SchedConfig holds scheduler config
4545
type SchedConfig struct {
46-
LockKey string `yaml:"lock_key"` // key for etcd lock
47-
LockTTL int `yaml:"lock_ttl"` // TTL for etcd lock
48-
MaxShare int64 `yaml:"maxshare"` // comlpex scheduler use maxshare
49-
ShareBase int64 `yaml:"sharebase"` // how many pieces for one core
46+
MaxShare int64 `yaml:"maxshare"` // comlpex scheduler use maxshare
47+
ShareBase int64 `yaml:"sharebase"` // how many pieces for one core
5048
}
5149

5250
// SyslogConfig 用于debug模式容器的日志收集

0 commit comments

Comments
 (0)