Skip to content
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

support removing table_id/store_id from namespace. #776

Merged
merged 10 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions pdctl/command/namespace_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ import (
)

const (
namespacePrefix = "pd/api/v1/namespaces"
namespaceAddPrefix = "pd/api/v1/namespaces/add"
namespacePrefix = "pd/api/v1/namespaces"
namespaceTablePrefix = "pd/api/v1/namespaces/table"
)

// NewNamespaceCommand return a namespace sub-command of rootCmd
func NewNamespaceCommand() *cobra.Command {
s := &cobra.Command{
Use: "namespace [create|append]",
Use: "namespace [create|add|remove]",
Short: "show the namespace information",
Run: showNamespaceCommandFunc,
}
s.AddCommand(NewCreateNamespaceCommand())
s.AddCommand(NewAppendTableIDCommand())
s.AddCommand(NewAddTableIDCommand())
s.AddCommand(NewRemoveTableIDCommand())
return s
}

Expand All @@ -48,12 +49,22 @@ func NewCreateNamespaceCommand() *cobra.Command {
return d
}

// NewAppendTableIDCommand returns a add sub-command of namespaceCmd
func NewAppendTableIDCommand() *cobra.Command {
// NewAddTableIDCommand returns a add sub-command of namespaceCmd
func NewAddTableIDCommand() *cobra.Command {
c := &cobra.Command{
Use: "append <name> <table_id>",
Use: "add <name> <table_id>",
Short: "add table id to namespace",
Run: appendTableCommandFunc,
Run: addTableCommandFunc,
}
return c
}

// NewRemoveTableIDCommand returns a remove sub-command of namespaceCmd
func NewRemoveTableIDCommand() *cobra.Command {
c := &cobra.Command{
Use: "remove <name> <table_id>",
Short: "remove table id from namespace",
Run: removeTableCommandFunc,
}
return c
}
Expand All @@ -73,24 +84,47 @@ func createNamespaceCommandFunc(cmd *cobra.Command, args []string) {
return
}

input := make(map[string]interface{})
input["namespace"] = args[0]
input := map[string]interface{}{
"namespace": args[0],
}

postJSON(cmd, namespacePrefix, input)
}

func appendTableCommandFunc(cmd *cobra.Command, args []string) {
func addTableCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("Usage: namespace add <name> <table_id>")
return
}
if _, err := strconv.Atoi(args[1]); err != nil {
fmt.Println("table_id shoud be a number")
return
}

input := map[string]interface{}{
"namespace": args[0],
"table_id": args[1],
"action": "add",
}

postJSON(cmd, namespaceTablePrefix, input)
}

func removeTableCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("Usage: namespace append <name> <table_id>")
fmt.Println("Usage: namespace remove <name> <table_id>")
return
}
if _, err := strconv.Atoi(args[1]); err != nil {
fmt.Println("table_id shoud be a number")
return
}

input := make(map[string]interface{})
input["namespace"] = args[0]
input["table_id"] = args[1]
input := map[string]interface{}{
"namespace": args[0],
"table_id": args[1],
"action": "remove",
}

postJSON(cmd, namespaceAddPrefix, input)
postJSON(cmd, namespaceTablePrefix, input)
}
28 changes: 0 additions & 28 deletions pdctl/command/store_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewStoreCommand() *cobra.Command {
s.AddCommand(NewDeleteStoreCommand())
s.AddCommand(NewLabelStoreCommand())
s.AddCommand(NewSetStoreWeightCommand())
s.AddCommand(NewNamespaceStoreCommand())
return s
}

Expand Down Expand Up @@ -70,17 +69,6 @@ func NewSetStoreWeightCommand() *cobra.Command {
}
}

// NewNamespaceStoreCommand returns a namespace subcommand of storeCmd.
func NewNamespaceStoreCommand() *cobra.Command {
n := &cobra.Command{
Use: "namespace <store_id> <namespace>",
Short: "set a store's namespace",
Run: namespaceStoreCommandFunc,
}

return n
}

func showStoreCommandFunc(cmd *cobra.Command, args []string) {
var prefix string
prefix = storesPrefix
Expand Down Expand Up @@ -151,19 +139,3 @@ func setStoreWeightCommandFunc(cmd *cobra.Command, args []string) {
"region": region,
})
}

func namespaceStoreCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("Usage: store namespace <store_id> <namespace>")
return
}
_, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("store_id should be a number")
return
}
prefix := fmt.Sprintf(path.Join(storePrefix, "namespace"), args[0])
postJSON(cmd, prefix, map[string]interface{}{
"namespace": args[1],
})
}
90 changes: 90 additions & 0 deletions pdctl/command/store_ns_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2017 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package command

import (
"fmt"
"strconv"

"github.com/spf13/cobra"
)

const storeNsPrefix = "pd/api/v1/store_ns/%s"

// NewStoreNsCommand return a store_ns subcommand of rootCmd
func NewStoreNsCommand() *cobra.Command {
s := &cobra.Command{
Use: "store_ns [set|rm] <store_id> <namespace>",
Short: "show the store status",
}
s.AddCommand(NewSetNamespaceStoreCommand())
s.AddCommand(NewRemoveNamespaceStoreCommand())
return s
}

// NewSetNamespaceStoreCommand returns a set subcommand of storeNsCmd.
func NewSetNamespaceStoreCommand() *cobra.Command {
n := &cobra.Command{
Use: "set <store_id> <namespace>",
Short: "set namespace to store",
Run: setNamespaceStoreCommandFunc,
}

return n
}

// NewRemoveNamespaceStoreCommand returns a rm subcommand of storeNsCmd.
func NewRemoveNamespaceStoreCommand() *cobra.Command {
n := &cobra.Command{
Use: "rm <store_id> <namespace>",
Short: "remove namespace from store",
Run: removeNamespaceStoreCommandFunc,
}

return n
}

func setNamespaceStoreCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("Usage: store_ns set <store_id> <namespace>")
return
}
_, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("store_id should be a number")
return
}
prefix := fmt.Sprintf(storeNsPrefix, args[0])
postJSON(cmd, prefix, map[string]interface{}{
"namespace": args[1],
"action": "add",
})
}

func removeNamespaceStoreCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("Usage: store_ns rm <store_id> <namespace>")
return
}
_, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("store_id should be a number")
return
}
prefix := fmt.Sprintf(storeNsPrefix, args[0])
postJSON(cmd, prefix, map[string]interface{}{
"namespace": args[1],
"action": "remove",
})
}
1 change: 1 addition & 0 deletions pdctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {
command.NewConfigCommand(),
command.NewRegionCommand(),
command.NewStoreCommand(),
command.NewStoreNsCommand(),
command.NewMemberCommand(),
command.NewExitCommand(),
command.NewLabelCommand(),
Expand Down
26 changes: 22 additions & 4 deletions server/api/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/http"
"strconv"

"github.com/juju/errors"
"github.com/pingcap/pd/server"
"github.com/unrolled/render"
)
Expand Down Expand Up @@ -78,7 +79,7 @@ func (h *namespaceHandler) Post(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusOK, nil)
}

func (h *namespaceHandler) Append(w http.ResponseWriter, r *http.Request) {
func (h *namespaceHandler) Update(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
Expand All @@ -97,10 +98,27 @@ func (h *namespaceHandler) Append(w http.ResponseWriter, r *http.Request) {
return
}
ns := input["namespace"]
action, ok := input["action"]
if !ok {
h.rd.JSON(w, http.StatusBadRequest, errors.New("missing parameters"))
return
}

// append table id to namespace
if err := cluster.AddNamespaceTableID(ns, tableID); err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
switch action {
case "add":
// append table id to namespace
if err := cluster.AddNamespaceTableID(ns, tableID); err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
case "remove":
// remove table id from namespace
if err := cluster.RemoveNamespaceTableID(ns, tableID); err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
default:
h.rd.JSON(w, http.StatusBadRequest, errors.New("unknown action"))
return
}

Expand Down
6 changes: 4 additions & 2 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
router.HandleFunc("/api/v1/store/{id}", storeHandler.Get).Methods("GET")
router.HandleFunc("/api/v1/store/{id}", storeHandler.Delete).Methods("DELETE")
router.HandleFunc("/api/v1/store/{id}/label", storeHandler.SetLabels).Methods("POST")
router.HandleFunc("/api/v1/store/{id}/namespace", storeHandler.SetNamespace).Methods("POST")
router.HandleFunc("/api/v1/store/{id}/weight", storeHandler.SetWeight).Methods("POST")
router.Handle("/api/v1/stores", newStoresHandler(svr, rd)).Methods("GET")

storeNsHandler := newStoreNsHandler(svr, rd)
router.HandleFunc("/api/v1/store_ns/{id}", storeNsHandler.SetNamespace).Methods("POST")

labelsHandler := newLabelsHandler(svr, rd)
router.HandleFunc("/api/v1/labels", labelsHandler.Get).Methods("GET")
router.HandleFunc("/api/v1/labels/stores", labelsHandler.GetStores).Methods("GET")
Expand Down Expand Up @@ -92,7 +94,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
namespaceHandler := newNamespaceHandler(svr, rd)
router.HandleFunc("/api/v1/namespaces", namespaceHandler.Get).Methods("GET")
router.HandleFunc("/api/v1/namespaces", namespaceHandler.Post).Methods("POST")
router.HandleFunc("/api/v1/namespaces/add", namespaceHandler.Append).Methods("POST")
router.HandleFunc("/api/v1/namespaces/table", namespaceHandler.Update).Methods("POST")

router.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {}).Methods("GET")
return router
Expand Down
31 changes: 0 additions & 31 deletions server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,37 +206,6 @@ func (h *storeHandler) SetLabels(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusOK, nil)
}

func (h *storeHandler) SetNamespace(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
}

vars := mux.Vars(r)
storeIDStr := vars["id"]
storeID, err := strconv.ParseUint(storeIDStr, 10, 64)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}

var input map[string]string
if err := readJSON(r.Body, &input); err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
ns := input["namespace"]

// append store id to namespace
if err := cluster.AddNamespaceStoreID(ns, storeID); err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}

h.rd.JSON(w, http.StatusOK, nil)

}

func (h *storeHandler) SetWeight(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
Expand Down
Loading