mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
proxy: initial grpc kv service proxy
This commit is contained in:
parent
deb21d3da5
commit
ec2ac72585
@ -73,6 +73,10 @@ type OpResponse struct {
|
|||||||
del *DeleteResponse
|
del *DeleteResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (op OpResponse) Put() *PutResponse { return op.put }
|
||||||
|
func (op OpResponse) Get() *GetResponse { return op.get }
|
||||||
|
func (op OpResponse) Del() *DeleteResponse { return op.del }
|
||||||
|
|
||||||
type kv struct {
|
type kv struct {
|
||||||
rc *remoteClient
|
rc *remoteClient
|
||||||
remote pb.KVClient
|
remote pb.KVClient
|
||||||
|
@ -423,6 +423,8 @@ type member struct {
|
|||||||
grpcAddr string
|
grpcAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *member) GRPCAddr() string { return m.grpcAddr }
|
||||||
|
|
||||||
type memberConfig struct {
|
type memberConfig struct {
|
||||||
name string
|
name string
|
||||||
peerTLS *transport.TLSInfo
|
peerTLS *transport.TLSInfo
|
||||||
|
130
proxy/grpcproxy/kv.go
Normal file
130
proxy/grpcproxy/kv.go
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
// Copyright 2016 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 grpcproxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/coreos/etcd/clientv3"
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type kvProxy struct {
|
||||||
|
c *clientv3.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewKvProxy(c *clientv3.Client) *kvProxy {
|
||||||
|
return &kvProxy{
|
||||||
|
c: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *kvProxy) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
|
||||||
|
resp, err := p.c.Do(ctx, RangeRequestToOp(r))
|
||||||
|
return (*pb.RangeResponse)(resp.Get()), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *kvProxy) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error) {
|
||||||
|
resp, err := p.c.Do(ctx, PutRequestToOp(r))
|
||||||
|
return (*pb.PutResponse)(resp.Put()), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *kvProxy) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
|
||||||
|
resp, err := p.c.Do(ctx, DelRequestToOp(r))
|
||||||
|
return (*pb.DeleteRangeResponse)(resp.Del()), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *kvProxy) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
|
||||||
|
txn := p.c.Txn(ctx)
|
||||||
|
cmps := make([]clientv3.Cmp, len(r.Compare))
|
||||||
|
thenops := make([]clientv3.Op, len(r.Success))
|
||||||
|
elseops := make([]clientv3.Op, len(r.Failure))
|
||||||
|
|
||||||
|
for i := range r.Compare {
|
||||||
|
cmps[i] = (clientv3.Cmp)(*r.Compare[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range r.Success {
|
||||||
|
thenops[i] = requestUnionToOp(r.Success[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range r.Failure {
|
||||||
|
elseops[i] = requestUnionToOp(r.Failure[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := txn.If(cmps...).Then(thenops...).Else(elseops...).Commit()
|
||||||
|
return (*pb.TxnResponse)(resp), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *kvProxy) Close() error {
|
||||||
|
return p.c.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func requestUnionToOp(union *pb.RequestUnion) clientv3.Op {
|
||||||
|
switch tv := union.Request.(type) {
|
||||||
|
case *pb.RequestUnion_RequestRange:
|
||||||
|
if tv.RequestRange != nil {
|
||||||
|
return RangeRequestToOp(tv.RequestRange)
|
||||||
|
}
|
||||||
|
case *pb.RequestUnion_RequestPut:
|
||||||
|
if tv.RequestPut != nil {
|
||||||
|
return PutRequestToOp(tv.RequestPut)
|
||||||
|
}
|
||||||
|
case *pb.RequestUnion_RequestDeleteRange:
|
||||||
|
if tv.RequestDeleteRange != nil {
|
||||||
|
return DelRequestToOp(tv.RequestDeleteRange)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic("unknown request")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RangeRequestToOp(r *pb.RangeRequest) clientv3.Op {
|
||||||
|
opts := []clientv3.OpOption{}
|
||||||
|
if len(r.RangeEnd) != 0 {
|
||||||
|
opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
|
||||||
|
}
|
||||||
|
opts = append(opts, clientv3.WithRev(r.Revision))
|
||||||
|
opts = append(opts, clientv3.WithLimit(r.Limit))
|
||||||
|
opts = append(opts, clientv3.WithSort(
|
||||||
|
clientv3.SortTarget(r.SortTarget),
|
||||||
|
clientv3.SortOrder(r.SortOrder)),
|
||||||
|
)
|
||||||
|
|
||||||
|
if r.Serializable {
|
||||||
|
opts = append(opts, clientv3.WithSerializable())
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientv3.OpGet(string(r.Key), opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PutRequestToOp(r *pb.PutRequest) clientv3.Op {
|
||||||
|
opts := []clientv3.OpOption{}
|
||||||
|
opts = append(opts, clientv3.WithLease(clientv3.LeaseID(r.Lease)))
|
||||||
|
|
||||||
|
return clientv3.OpPut(string(r.Key), string(r.Value), opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DelRequestToOp(r *pb.DeleteRangeRequest) clientv3.Op {
|
||||||
|
opts := []clientv3.OpOption{}
|
||||||
|
if len(r.RangeEnd) != 0 {
|
||||||
|
opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientv3.OpDelete(string(r.Key), opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *kvProxy) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
|
||||||
|
panic("unimplemented")
|
||||||
|
}
|
95
proxy/grpcproxy/kv_test.go
Normal file
95
proxy/grpcproxy/kv_test.go
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
// Copyright 2016 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 grpcproxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/clientv3"
|
||||||
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
|
"github.com/coreos/etcd/integration"
|
||||||
|
"github.com/coreos/etcd/pkg/testutil"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestKVProxyRange(t *testing.T) {
|
||||||
|
defer testutil.AfterTest(t)
|
||||||
|
|
||||||
|
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
|
||||||
|
defer clus.Terminate(t)
|
||||||
|
|
||||||
|
kvts := newKVProxyServer([]string{clus.Members[0].GRPCAddr()}, t)
|
||||||
|
defer kvts.close()
|
||||||
|
|
||||||
|
// create a client and try to get key from proxy.
|
||||||
|
cfg := clientv3.Config{
|
||||||
|
Endpoints: []string{kvts.l.Addr().String()},
|
||||||
|
DialTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
client, err := clientv3.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err = %v, want nil")
|
||||||
|
}
|
||||||
|
_, err = client.Get(context.Background(), "foo")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err = %v, want nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type kvproxyTestServer struct {
|
||||||
|
kp *kvProxy
|
||||||
|
server *grpc.Server
|
||||||
|
l net.Listener
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kts *kvproxyTestServer) close() {
|
||||||
|
kts.server.Stop()
|
||||||
|
kts.l.Close()
|
||||||
|
kts.kp.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func newKVProxyServer(endpoints []string, t *testing.T) *kvproxyTestServer {
|
||||||
|
cfg := clientv3.Config{
|
||||||
|
Endpoints: endpoints,
|
||||||
|
DialTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
client, err := clientv3.New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
kvp := NewKvProxy(client)
|
||||||
|
|
||||||
|
kvts := &kvproxyTestServer{
|
||||||
|
kp: kvp,
|
||||||
|
}
|
||||||
|
|
||||||
|
var opts []grpc.ServerOption
|
||||||
|
kvts.server = grpc.NewServer(opts...)
|
||||||
|
pb.RegisterKVServer(kvts.server, kvts.kp)
|
||||||
|
|
||||||
|
kvts.l, err = net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go kvts.server.Serve(kvts.l)
|
||||||
|
|
||||||
|
return kvts
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user