You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.9 KiB
145 lines
3.9 KiB
8 years ago
|
package master
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
8 years ago
|
"time"
|
||
8 years ago
|
|
||
|
"github.com/coreos/etcd/clientv3"
|
||
|
"github.com/coreos/etcd/clientv3/concurrency"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
// DefaultLockPath is the default etcd master lock path.
|
||
|
DefaultLockPath = "/master/lock"
|
||
|
// DefaultStatePath is the default etcd key for master state.
|
||
|
DefaultStatePath = "/master/state"
|
||
8 years ago
|
// DefaultAddrPath is the default etcd key for master address.
|
||
|
DefaultAddrPath = "/master/addr"
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
// EtcdClient is the etcd client that master uses for fault tolerance
|
||
8 years ago
|
// and service registry.
|
||
8 years ago
|
type EtcdClient struct {
|
||
8 years ago
|
lockPath string
|
||
|
statePath string
|
||
|
client *clientv3.Client
|
||
8 years ago
|
lock *concurrency.Mutex
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// NewEtcdClient creates a new EtcdClient.
|
||
|
func NewEtcdClient(endpoints []string, addr string, lockPath, addrPath, statePath string, ttlSec int) (*EtcdClient, error) {
|
||
|
log.Debugf("Connecting to etcd at %v", endpoints)
|
||
8 years ago
|
// TODO(helin): gracefully shutdown etcd store. Becuase etcd
|
||
|
// store holds a etcd lock, even though the lock will expire
|
||
|
// when the lease timeout, we need to implement graceful
|
||
|
// shutdown to release the lock.
|
||
8 years ago
|
cli, err := clientv3.New(clientv3.Config{
|
||
|
Endpoints: endpoints,
|
||
|
DialTimeout: dialTimeout,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
sess, err := concurrency.NewSession(cli, concurrency.WithTTL(ttlSec))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
lock := concurrency.NewMutex(sess, lockPath)
|
||
|
// It's fine for the lock to get stuck, in this case we have
|
||
|
// multiple master servers running (only configured to have
|
||
|
// one master running, but split-brain problem may cuase
|
||
|
// multiple master servers running), and the cluster management
|
||
|
// software will kill one of them.
|
||
8 years ago
|
log.Debugf("Trying to acquire lock at %s.", lockPath)
|
||
8 years ago
|
err = lock.Lock(context.TODO())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
8 years ago
|
log.Debugf("Successfully acquired lock at %s.", lockPath)
|
||
|
|
||
|
put := clientv3.OpPut(addrPath, string(addr))
|
||
|
resp, err := cli.Txn(context.Background()).If(lock.IsOwner()).Then(put).Commit()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if !resp.Succeeded {
|
||
|
log.Fatal("No longer owns the master lock. Exiting.")
|
||
|
}
|
||
|
|
||
|
e := &EtcdClient{
|
||
|
lockPath: lockPath,
|
||
|
statePath: statePath,
|
||
|
client: cli,
|
||
|
lock: lock,
|
||
|
}
|
||
|
|
||
8 years ago
|
return e, nil
|
||
|
}
|
||
|
|
||
|
// Save saves the state into the etcd.
|
||
8 years ago
|
func (e *EtcdClient) Save(state []byte) error {
|
||
8 years ago
|
ctx := context.TODO()
|
||
|
put := clientv3.OpPut(e.statePath, string(state))
|
||
|
resp, err := e.client.Txn(ctx).If(e.lock.IsOwner()).Then(put).Commit()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if !resp.Succeeded {
|
||
8 years ago
|
log.Errorln("No longer owns the lock, trying to lock again")
|
||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
|
err := e.lock.Lock(ctx)
|
||
|
cancel()
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
// We lost the master lock and can not acquire
|
||
|
// it back, it means some other master is
|
||
|
// already started. We don't want cluster
|
||
|
// managment system to kill the master server
|
||
|
// who is holding the lock and running
|
||
|
// correctly. So the most feasible solution is
|
||
|
// to kill current master server. The current
|
||
|
// state is not saved, but the trainer's RPC
|
||
|
// call will fail, so the trainer will retry.
|
||
|
log.Fatalf("Could not acquire the lock at %s: %v. Exiting.", e.lockPath, err)
|
||
8 years ago
|
}
|
||
|
log.Infof("Successfully acquired lock at %s.", e.lockPath)
|
||
|
return e.Save(state)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Load loads the state from etcd.
|
||
8 years ago
|
func (e *EtcdClient) Load() ([]byte, error) {
|
||
8 years ago
|
ctx := context.TODO()
|
||
|
get := clientv3.OpGet(e.statePath)
|
||
|
|
||
|
resp, err := e.client.Txn(ctx).If(e.lock.IsOwner()).Then(get).Commit()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if !resp.Succeeded {
|
||
|
log.Errorln("No longer owns the lock, trying to lock and load again.")
|
||
8 years ago
|
err = e.lock.Lock(context.Background())
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
8 years ago
|
return e.Load()
|
||
|
}
|
||
|
|
||
|
kvs := resp.Responses[0].GetResponseRange().Kvs
|
||
|
if len(kvs) == 0 {
|
||
|
// No state exists
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
state := kvs[0].Value
|
||
|
return state, nil
|
||
|
}
|