-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
grpc lock service #7444
grpc lock service #7444
Changes from all commits
b025cdd
a2cdd90
d51c8bb
763a37d
47cd9d0
c737bf3
ad1a790
300323f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package v3client provides clientv3 interfaces from an etcdserver. | ||
package v3client |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v3client | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/coreos/etcd/clientv3" | ||
"github.com/coreos/etcd/etcdserver" | ||
"github.com/coreos/etcd/etcdserver/api/v3rpc" | ||
"github.com/coreos/etcd/proxy/grpcproxy/adapter" | ||
) | ||
|
||
func New(s *etcdserver.EtcdServer) *clientv3.Client { | ||
c := clientv3.NewCtxClient(context.Background()) | ||
|
||
kvc := adapter.KvServerToKvClient(v3rpc.NewQuotaKVServer(s)) | ||
c.KV = clientv3.NewKVFromKVClient(kvc) | ||
|
||
lc := adapter.LeaseServerToLeaseClient(v3rpc.NewQuotaLeaseServer(s)) | ||
c.Lease = clientv3.NewLeaseFromLeaseClient(lc, time.Second) | ||
|
||
wc := adapter.WatchServerToWatchClient(v3rpc.NewWatchServer(s)) | ||
c.Watcher = clientv3.NewWatchFromWatchClient(wc) | ||
|
||
return c | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package v3lock provides a v3 locking service from an etcdserver. | ||
package v3lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2017 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v3lock | ||
|
||
import ( | ||
"golang.org/x/net/context" | ||
|
||
"github.com/coreos/etcd/clientv3" | ||
"github.com/coreos/etcd/clientv3/concurrency" | ||
"github.com/coreos/etcd/etcdserver/api/v3lock/v3lockpb" | ||
) | ||
|
||
type lockServer struct { | ||
c *clientv3.Client | ||
} | ||
|
||
func NewLockServer(c *clientv3.Client) v3lockpb.LockServer { | ||
return &lockServer{c} | ||
} | ||
|
||
func (ls *lockServer) Lock(ctx context.Context, req *v3lockpb.LockRequest) (*v3lockpb.LockResponse, error) { | ||
s, err := concurrency.NewSession( | ||
ls.c, | ||
concurrency.WithLease(clientv3.LeaseID(req.Lease)), | ||
concurrency.WithContext(ctx), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s.Orphan() | ||
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. why do we need to call Orphan here? 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. Otherwise the server will keep the lease alive indefinitely. An alternative to putting it here is Another option is to make leases optional and have an additional TTL field so the server can grant a lease and return a lock held for at least the given TTL. |
||
m := concurrency.NewMutex(s, string(req.Name)) | ||
if err = m.Lock(ctx); err != nil { | ||
return nil, err | ||
} | ||
return &v3lockpb.LockResponse{Header: m.Header(), Key: []byte(m.Key())}, nil | ||
} | ||
|
||
func (ls *lockServer) Unlock(ctx context.Context, req *v3lockpb.UnlockRequest) (*v3lockpb.UnlockResponse, error) { | ||
resp, err := ls.c.Delete(ctx, string(req.Key)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &v3lockpb.UnlockResponse{Header: resp.Header}, nil | ||
} |
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.
Could you explain what's the rationale of for loop here? Thanks.
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.
It loops because
waitDelete
will return after sensing one delete. There is no guarantee that all keys within the revision range will be deleted by that time. This code is a little old and the API is a bit stronger now so this can possibly be optimized to be single-shot on the watcher, but I don't want to change it for this patch.