-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make pserver able to get server index without etcd (decouple pserver with etcd) #2634
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package pserver | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/PaddlePaddle/Paddle/go/utils/networkhelper" | ||
"github.com/coreos/etcd/clientv3" | ||
"github.com/coreos/etcd/clientv3/concurrency" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// EtcdClient is the etcd client that the pserver uses for fault | ||
// tolerance, service registry and coordination. | ||
type EtcdClient struct { | ||
numPservers int | ||
etcdEndpoints string | ||
etcdClient *clientv3.Client | ||
// etcdTimeout is also used as retry intervals. | ||
etcdTimeout time.Duration | ||
// FIXME: ensure GetExternalIP gets the correct ip for trainers to connect. | ||
externalIP string | ||
// desired number of pservers in the job. | ||
// assume desired will not change during one training job. | ||
desired int | ||
} | ||
|
||
// NewEtcdClient creates an EtcdClient | ||
func NewEtcdClient(endpoints string, numPservers int, timeout time.Duration) *EtcdClient { | ||
return &EtcdClient{ | ||
etcdTimeout: timeout, | ||
numPservers: numPservers, | ||
etcdEndpoints: endpoints, | ||
} | ||
} | ||
|
||
// Register registers the pserver on etcd | ||
// | ||
// Register returns the index of the current pserver. | ||
func (e *EtcdClient) Register() (int, error) { | ||
|
||
var err error | ||
e.externalIP, err = networkhelper.GetExternalIP() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// initialize connection to etcd. | ||
ep := strings.Split(e.etcdEndpoints, ",") | ||
for { | ||
cli, err := clientv3.New(clientv3.Config{ | ||
Endpoints: ep, | ||
DialTimeout: e.etcdTimeout, | ||
}) | ||
if err != nil { | ||
log.Errorf("connect to etcd error: %v", err) | ||
time.Sleep(e.etcdTimeout) | ||
continue | ||
} | ||
e.etcdClient = cli | ||
log.Debugf("inited client to %s", e.etcdEndpoints) | ||
break | ||
} | ||
// init /ps_desired using transaction, for multiple pservers may want to write | ||
// it at the same time. | ||
for { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
_, err := e.initDesiredPsercers(ctx, e.numPservers) | ||
cancel() | ||
if err != nil { | ||
log.Warn(err) | ||
time.Sleep(e.etcdTimeout) | ||
continue | ||
} | ||
break | ||
} | ||
// TODO: when implementing extending or reducing pservers, /ps_desired is | ||
// changed, then we need to watch /ps_desired node for events. For now, just | ||
// write once when init and read from it. | ||
// wait and set s.desired init value | ||
for { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
resp, err := e.etcdClient.Get(ctx, PsDesired) | ||
cancel() | ||
if err != nil { | ||
log.Errorf("getting %s error: %v", PsDesired, err) | ||
time.Sleep(e.etcdTimeout) | ||
continue | ||
} | ||
if len(resp.Kvs) != 0 { | ||
e.desired, err = strconv.Atoi(string(resp.Kvs[0].Value)) | ||
if err != nil { | ||
log.Errorf("value of %s invalid %v\n", PsDesired, err) | ||
time.Sleep(e.etcdTimeout) | ||
// NOTE: wait util ps_desired value change | ||
continue | ||
} | ||
break | ||
} | ||
} | ||
|
||
var pserverIdx int | ||
// try register pserver node on etcd | ||
for { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
var err error | ||
pserverIdx, err = e.registerPserverEtcd(ctx) | ||
cancel() | ||
if err != nil { | ||
log.Warn(err) | ||
time.Sleep(e.etcdTimeout) | ||
continue | ||
} | ||
break | ||
} | ||
|
||
return pserverIdx, nil | ||
} | ||
|
||
func (e *EtcdClient) initDesiredPsercers(ctx context.Context, numPservers int) (*clientv3.TxnResponse, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is a typo here |
||
return concurrency.NewSTM(e.etcdClient, func(c concurrency.STM) error { | ||
dsStr := c.Get(PsDesired) | ||
if dsStr == "" { | ||
c.Put(PsDesired, strconv.Itoa(numPservers)) | ||
} | ||
return nil | ||
}, concurrency.WithAbortContext(ctx), concurrency.WithIsolation(concurrency.RepeatableReads)) | ||
} | ||
|
||
// registerPserverEtcd registers pserver node on etcd using transaction. | ||
func (e *EtcdClient) registerPserverEtcd(ctx context.Context) (int, error) { | ||
var idx int | ||
_, err := concurrency.NewSTM(e.etcdClient, func(c concurrency.STM) error { | ||
registered := false | ||
for i := 0; i < e.desired; i++ { | ||
psKey := "/ps/" + strconv.Itoa(i) | ||
log.Debugf("checking %s", psKey) | ||
ps := c.Get(psKey) | ||
log.Debugf("got value (%s) for key: %s", ps, psKey) | ||
|
||
if ps == "" { | ||
resp, err := e.etcdClient.Grant(context.TODO(), 5) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// find the first id and write info | ||
c.Put(psKey, e.externalIP, clientv3.WithLease(resp.ID)) | ||
log.Debugf("set pserver node %s with value %s", psKey, e.externalIP) | ||
ch, kaerr := e.etcdClient.KeepAlive(context.TODO(), resp.ID) | ||
if kaerr != nil { | ||
log.Errorf("keepalive etcd node error: %v", kaerr) | ||
return kaerr | ||
} | ||
|
||
// Eat the keep alive message so etcd | ||
// will not expire the lease. | ||
go func(ch <-chan *clientv3.LeaseKeepAliveResponse) { | ||
ka := <-ch | ||
log.Debugf("keepalive: %d\n", ka.TTL) | ||
}(ch) | ||
log.Debug("register finished") | ||
idx = i | ||
registered = true | ||
break | ||
} | ||
} | ||
if registered == true { | ||
return nil | ||
} | ||
return errors.New("not registerd, may due to already have enough pservers") | ||
}, concurrency.WithAbortContext(ctx), concurrency.WithIsolation(concurrency.RepeatableReads)) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return idx, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't catch up what 0 meaning here, pserver index?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad, I found it in the bottom. maybe add some comment here is better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if develop is confused, he can check the definition of
pserver.NewService
:) Please let me know if you think otherwise.