Skip to content

Commit

Permalink
Merge pull request #3248 from xiang90/v3
Browse files Browse the repository at this point in the history
initial v3 demo
  • Loading branch information
xiang90 committed Aug 10, 2015
2 parents fb5e1ac + b0ea4ab commit a718329
Show file tree
Hide file tree
Showing 16 changed files with 863 additions and 151 deletions.
6 changes: 6 additions & 0 deletions Documentation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ Follow the instructions when using these flags.
+ Force to create a new one-member cluster. It commits configuration changes in force to remove all existing members in the cluster and add itself. It needs to be set to [restore a backup][restore].
+ default: false

### Experimental Flags

##### -experimental-v3demo
+ Enable experimental [v3 demo API](rfc/v3api.proto).
+ default: false

### Miscellaneous Flags

##### -version
Expand Down
61 changes: 61 additions & 0 deletions etcdctlv3/command/delete_range_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2015 CoreOS, Inc.
//
// 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 command

import (
"fmt"

"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
)

// NewDeleteRangeCommand returns the CLI command for "deleteRange".
func NewDeleteRangeCommand() cli.Command {
return cli.Command{
Name: "delete-range",
Action: func(c *cli.Context) {
deleteRangeCommandFunc(c)
},
}
}

// deleteRangeCommandFunc executes the "delegeRange" command.
func deleteRangeCommandFunc(c *cli.Context) {
if len(c.Args()) == 0 {
panic("bad arg")
}

var rangeEnd []byte
key := []byte(c.Args()[0])
if len(c.Args()) > 1 {
rangeEnd = []byte(c.Args()[1])
}
conn, err := grpc.Dial("127.0.0.1:12379")
if err != nil {
panic(err)
}
etcd := pb.NewEtcdClient(conn)
req := &pb.DeleteRangeRequest{Key: key, RangeEnd: rangeEnd}

etcd.DeleteRange(context.Background(), req)

if rangeEnd != nil {
fmt.Printf("range [%s, %s) is deleted\n", string(key), string(rangeEnd))
} else {
fmt.Printf("key %s is deleted\n", string(key))
}
}
53 changes: 53 additions & 0 deletions etcdctlv3/command/put_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2015 CoreOS, Inc.
//
// 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 command

import (
"fmt"

"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
)

// NewPutCommand returns the CLI command for "put".
func NewPutCommand() cli.Command {
return cli.Command{
Name: "put",
Action: func(c *cli.Context) {
putCommandFunc(c)
},
}
}

// putCommandFunc executes the "put" command.
func putCommandFunc(c *cli.Context) {
if len(c.Args()) != 2 {
panic("bad arg")
}

key := []byte(c.Args()[0])
value := []byte(c.Args()[1])
conn, err := grpc.Dial("127.0.0.1:12379")
if err != nil {
panic(err)
}
etcd := pb.NewEtcdClient(conn)
req := &pb.PutRequest{Key: key, Value: value}

etcd.Put(context.Background(), req)
fmt.Printf("%s %s\n", key, value)
}
58 changes: 58 additions & 0 deletions etcdctlv3/command/range_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2015 CoreOS, Inc.
//
// 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 command

import (
"fmt"

"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
)

// NewRangeCommand returns the CLI command for "range".
func NewRangeCommand() cli.Command {
return cli.Command{
Name: "range",
Action: func(c *cli.Context) {
rangeCommandFunc(c)
},
}
}

// rangeCommandFunc executes the "range" command.
func rangeCommandFunc(c *cli.Context) {
if len(c.Args()) == 0 {
panic("bad arg")
}

var rangeEnd []byte
key := []byte(c.Args()[0])
if len(c.Args()) > 1 {
rangeEnd = []byte(c.Args()[1])
}
conn, err := grpc.Dial("127.0.0.1:12379")
if err != nil {
panic(err)
}
etcd := pb.NewEtcdClient(conn)
req := &pb.RangeRequest{Key: key, RangeEnd: rangeEnd}

resp, err := etcd.Range(context.Background(), req)
for _, kv := range resp.Kvs {
fmt.Printf("%s %s\n", string(kv.Key), string(kv.Value))
}
}
37 changes: 37 additions & 0 deletions etcdctlv3/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2015 CoreOS, Inc.
//
// 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 main

import (
"os"

"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/coreos/etcd/etcdctlv3/command"
"github.com/coreos/etcd/version"
)

func main() {
app := cli.NewApp()
app.Name = "etcdctlv3"
app.Version = version.Version
app.Usage = "A simple command line client for etcd3."
app.Commands = []cli.Command{
command.NewRangeCommand(),
command.NewPutCommand(),
command.NewDeleteRangeCommand(),
}

app.Run(os.Args)
}
5 changes: 5 additions & 0 deletions etcdmain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ type config struct {

printVersion bool

v3demo bool

ignored []string
}

Expand Down Expand Up @@ -208,6 +210,9 @@ func NewConfig() *config {
// version
fs.BoolVar(&cfg.printVersion, "version", false, "Print the version and exit")

// demo flag
fs.BoolVar(&cfg.v3demo, "experimental-v3demo", false, "Enable experimental v3 demo API")

// backwards-compatibility with v0.4.6
fs.Var(&flags.IPAddressPort{}, "addr", "DEPRECATED: Use -advertise-client-urls instead.")
fs.Var(&flags.IPAddressPort{}, "bind-addr", "DEPRECATED: Use -listen-client-urls instead.")
Expand Down
21 changes: 21 additions & 0 deletions etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ import (
systemdutil "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-systemd/util"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
"github.com/coreos/etcd/discovery"
"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/etcdserver/api/v3rpc"
"github.com/coreos/etcd/etcdserver/etcdhttp"
"github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/pkg/cors"
"github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/pkg/osutil"
Expand Down Expand Up @@ -232,6 +235,15 @@ func startEtcd(cfg *config) (<-chan struct{}, error) {
clns = append(clns, l)
}

var v3l net.Listener
if cfg.v3demo {
v3l, err = net.Listen("tcp", "127.0.0.1:12379")
if err != nil {
plog.Fatal(err)
}
plog.Infof("listening for client rpc on 127.0.0.1:12379")
}

srvcfg := &etcdserver.ServerConfig{
Name: cfg.name,
ClientURLs: cfg.acurls,
Expand All @@ -249,6 +261,7 @@ func startEtcd(cfg *config) (<-chan struct{}, error) {
Transport: pt,
TickMs: cfg.TickMs,
ElectionTicks: cfg.electionTicks(),
V3demo: cfg.v3demo,
}
var s *etcdserver.EtcdServer
s, err = etcdserver.NewServer(srvcfg)
Expand Down Expand Up @@ -280,6 +293,14 @@ func startEtcd(cfg *config) (<-chan struct{}, error) {
plog.Fatal(serveHTTP(l, ch, 0))
}(l)
}

if cfg.v3demo {
// set up v3 demo rpc
grpcServer := grpc.NewServer()
etcdserverpb.RegisterEtcdServer(grpcServer, v3rpc.New(s))
go plog.Fatal(grpcServer.Serve(v3l))
}

return s.StopNotify(), nil
}

Expand Down
6 changes: 6 additions & 0 deletions etcdmain/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,11 @@ given by the consensus protocol.
--force-new-cluster 'false'
force to create a new one-member cluster.
experimental flags:
--experimental-v3demo 'false'
enable experimental v3 demo API
`
)
54 changes: 54 additions & 0 deletions etcdserver/api/v3rpc/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2015 CoreOS, Inc.
//
// 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 v3rpc

import (
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/etcdserver"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
)

type handler struct {
server etcdserver.V3DemoServer
}

func New(s etcdserver.V3DemoServer) pb.EtcdServer {
return &handler{s}
}

func (h *handler) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
resp := h.server.V3DemoDo(ctx, pb.InternalRaftRequest{Range: r})
return resp.(*pb.RangeResponse), nil
}

func (h *handler) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error) {
resp := h.server.V3DemoDo(ctx, pb.InternalRaftRequest{Put: r})
return resp.(*pb.PutResponse), nil
}

func (h *handler) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
resp := h.server.V3DemoDo(ctx, pb.InternalRaftRequest{DeleteRange: r})
return resp.(*pb.DeleteRangeResponse), nil
}

func (h *handler) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
panic("not implemented")
return nil, nil
}

func (h *handler) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
panic("not implemented")
return nil, nil
}
2 changes: 2 additions & 0 deletions etcdserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type ServerConfig struct {

TickMs uint
ElectionTicks int

V3demo bool
}

// VerifyBootstrapConfig sanity-checks the initial config for bootstrap case
Expand Down
Loading

0 comments on commit a718329

Please sign in to comment.