Skip to content

Commit

Permalink
don't crash when service key exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jan 22, 2021
1 parent 06419bc commit fa6c4bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 16 additions & 4 deletions cluster/calcium/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
"time"

"github.com/pkg/errors"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
Expand All @@ -30,10 +31,21 @@ func (c *Calcium) RegisterService(ctx context.Context) (unregister func(), err e
return
}

expiry, unregisterService, err := c.registerService(ctx, serviceAddress)
if err != nil {
log.Errorf("[RegisterService] failed to first register service: %v", err)
return
var (
expiry <-chan struct{}
unregisterService func()
)
for {
if expiry, unregisterService, err = c.registerService(ctx, serviceAddress); err == nil {
break
}
if errors.Is(err, types.ErrKeyExists) {
log.Debugf("[RegisterService] service key exists: %v", err)
time.Sleep(time.Second)
continue
}
log.Errorf("[RegisterService] failed to first register service: %+v", err)
return nil, errors.WithStack(err)
}

wg := &sync.WaitGroup{}
Expand Down
7 changes: 4 additions & 3 deletions store/etcdv3/meta/ephemeral.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"go.etcd.io/etcd/v3/clientv3"

"github.com/pkg/errors"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/types"
)
Expand All @@ -14,17 +15,17 @@ import (
func (e *ETCD) StartEphemeral(ctx context.Context, path string, heartbeat time.Duration) (<-chan struct{}, func(), error) {
lease, err := e.cliv3.Grant(ctx, int64(heartbeat/time.Second))
if err != nil {
return nil, nil, err
return nil, nil, errors.WithStack(err)
}

switch tx, err := e.cliv3.Txn(ctx).
If(clientv3.Compare(clientv3.Version(path), "=", 0)).
Then(clientv3.OpPut(path, "", clientv3.WithLease(lease.ID))).
Commit(); {
case err != nil:
return nil, nil, err
return nil, nil, errors.WithStack(err)
case !tx.Succeeded:
return nil, nil, types.NewDetailedErr(types.ErrKeyExists, path)
return nil, nil, errors.Wrap(types.ErrKeyExists, path)
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit fa6c4bf

Please sign in to comment.