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

*: reimplement dynamic config #2349

Merged
merged 6 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.2.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.12.1
github.com/json-iterator/go v1.1.9 // indirect
github.com/juju/ratelimit v1.0.1
github.com/kevinburke/go-bindata v3.18.0+incompatible
Expand Down
124 changes: 124 additions & 0 deletions pkg/component/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2020 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 component

import (
"fmt"
"sync"

"github.com/pingcap/log"
"go.uber.org/zap"
)

// Manager is used to manage components.
type Manager struct {
sync.RWMutex
// component -> addresses
Addresses map[string][]string
}

// NewManager creates a new component manager.
func NewManager() *Manager {
return &Manager{
Addresses: make(map[string][]string),
}
}

// GetComponentAddrs returns component addresses for a given component.
func (c *Manager) GetComponentAddrs(component string) []string {
c.RLock()
defer c.RUnlock()
addresses := []string{}
if ca, ok := c.Addresses[component]; ok {
addresses = append(addresses, ca...)
}
return addresses
}

// GetAllComponentAddrs returns all components' addresses.
func (c *Manager) GetAllComponentAddrs() map[string][]string {
c.RLock()
defer c.RUnlock()
n := make(map[string][]string)
for k, v := range c.Addresses {
b := make([]string, len(v))
copy(b, v)
n[k] = b
}
return n
}

// GetComponent returns the component from a given component ID.
func (c *Manager) GetComponent(addr string) string {
c.RLock()
defer c.RUnlock()
for component, ca := range c.Addresses {
if exist, _ := contains(ca, addr); exist {
return component
}
}
return ""
}

// Register is used for registering a component with an address to PD.
func (c *Manager) Register(component, addr string) error {
rleungx marked this conversation as resolved.
Show resolved Hide resolved
c.Lock()
defer c.Unlock()

ca, ok := c.Addresses[component]
if exist, _ := contains(ca, addr); ok && exist {
log.Info("address has already been registered", zap.String("component", component), zap.String("address", addr))
return fmt.Errorf("component %s address %s has already been registered", component, addr)
}

ca = append(ca, addr)
c.Addresses[component] = ca
log.Info("address registers successfully", zap.String("component", component), zap.String("address", addr))
return nil
}

// UnRegister is used for unregistering a component with an address from PD.
func (c *Manager) UnRegister(component, addr string) error {
c.Lock()
defer c.Unlock()

ca, ok := c.Addresses[component]
if !ok {
return fmt.Errorf("component %s not found", component)
}

if exist, idx := contains(ca, addr); exist {
ca = append(ca[:idx], ca[idx+1:]...)
log.Info("address has successfully been unregistered", zap.String("component", component), zap.String("address", addr))
if len(ca) == 0 {
delete(c.Addresses, component)
return nil
}

c.Addresses[component] = ca
return nil
}

return fmt.Errorf("address %s not found", addr)
}

func contains(slice []string, item string) (bool, int) {
for i, s := range slice {
if s == item {
return true, i
}
}

return false, 0
}
63 changes: 63 additions & 0 deletions pkg/component/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2020 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 component

import (
"strings"
"testing"

. "github.com/pingcap/check"
)

func Test(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testManagerSuite{})

type testManagerSuite struct{}

func (s *testManagerSuite) TestManager(c *C) {
HunDunDM marked this conversation as resolved.
Show resolved Hide resolved
m := NewManager()
// register legal address
c.Assert(m.Register("c1", "127.0.0.1:1"), IsNil)
c.Assert(m.Register("c1", "127.0.0.1:2"), IsNil)
// register repeatedly
c.Assert(strings.Contains(m.Register("c1", "127.0.0.1:2").Error(), "already"), IsTrue)
c.Assert(m.Register("c2", "127.0.0.1:3"), IsNil)

// get all addresses
all := map[string][]string{
"c1": {"127.0.0.1:1", "127.0.0.1:2"},
"c2": {"127.0.0.1:3"},
}
c.Assert(m.GetAllComponentAddrs(), DeepEquals, all)

// get the specific component addresses
c.Assert(m.GetComponentAddrs("c1"), DeepEquals, all["c1"])
c.Assert(m.GetComponentAddrs("c2"), DeepEquals, all["c2"])

// get the component from the address
c.Assert(m.GetComponent("127.0.0.1:1"), Equals, "c1")
c.Assert(m.GetComponent("127.0.0.1:2"), Equals, "c1")
c.Assert(m.GetComponent("127.0.0.1:3"), Equals, "c2")

// unregister address
c.Assert(m.UnRegister("c1", "127.0.0.1:1"), IsNil)
c.Assert(m.GetComponentAddrs("c1"), DeepEquals, []string{"127.0.0.1:2"})
c.Assert(m.UnRegister("c1", "127.0.0.1:2"), IsNil)
c.Assert(m.GetComponentAddrs("c1"), DeepEquals, []string{})
all = map[string][]string{"c2": {"127.0.0.1:3"}}
c.Assert(m.GetAllComponentAddrs(), DeepEquals, all)
}
24 changes: 6 additions & 18 deletions pkg/dashboard/adapter/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ type Manager struct {
service *apiserver.Service
redirector *Redirector

enableDynamic bool

isLeader bool
members []*pdpb.Member
}
Expand All @@ -55,12 +53,11 @@ type Manager struct {
func NewManager(srv *server.Server, s *apiserver.Service, redirector *Redirector) *Manager {
ctx, cancel := context.WithCancel(srv.Context())
return &Manager{
ctx: ctx,
cancel: cancel,
srv: srv,
service: s,
redirector: redirector,
enableDynamic: srv.GetConfig().EnableDynamicConfig,
ctx: ctx,
cancel: cancel,
srv: srv,
service: s,
redirector: redirector,
}
}

Expand Down Expand Up @@ -101,9 +98,7 @@ func (m *Manager) updateInfo() {
if !m.srv.GetMember().IsLeader() {
m.isLeader = false
m.members = nil
if !m.enableDynamic {
m.srv.GetPersistOptions().Reload(m.srv.GetStorage())
}
m.srv.GetPersistOptions().Reload(m.srv.GetStorage())
return
}

Expand Down Expand Up @@ -195,13 +190,6 @@ func (m *Manager) setNewAddress() {
}
}
}
// set new dashboard address
rleungx marked this conversation as resolved.
Show resolved Hide resolved
if m.enableDynamic {
if err := m.srv.UpdateConfigManager("pd-server.dashboard-address", addr); err != nil {
log.Error("failed to update the dashboard address in config manager", zap.Error(err))
}
return
}
cfg := m.srv.GetPersistOptions().GetPDServerConfig().Clone()
cfg.DashboardAddress = addr
m.srv.SetPDServerConfig(*cfg)
Expand Down
120 changes: 120 additions & 0 deletions server/api/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2020 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 api

import (
"net/http"

"github.com/gorilla/mux"
"github.com/pingcap/errcode"
"github.com/pingcap/pd/v4/pkg/apiutil"
"github.com/pingcap/pd/v4/server"
"github.com/pkg/errors"
"github.com/unrolled/render"
)

// Addresses is mapping from component to addresses.
type Addresses map[string][]string

type componentHandler struct {
svr *server.Server
rd *render.Render
}

func newComponentHandler(svr *server.Server, rd *render.Render) *componentHandler {
return &componentHandler{
svr: svr,
rd: rd,
}
}

// @Tags component
// @Summary Register component address.
// @Produce json
// @Success 200 {string} string
// @Failure 400 {string} string "The input is invalid."
// @Failure 500 {string} string "PD server failed to proceed the request."
// @Router /component [post]
func (h *componentHandler) Register(w http.ResponseWriter, r *http.Request) {
input := make(map[string]string)
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil {
return
}
component, ok := input["component"]
if !ok {
apiutil.ErrorResp(h.rd, w, errcode.NewInvalidInputErr(errors.New("not set component")))
return
}
addr, ok := input["addr"]
if !ok {
apiutil.ErrorResp(h.rd, w, errcode.NewInvalidInputErr(errors.New("not set addr")))
return
}
m := h.svr.GetComponentManager()
err := m.Register(component, addr)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, nil)
}

// @Tags component
// @Summary Unregister component address.
// @Produce json
// @Success 200 {string} string
// @Failure 400 {string} string "The input is invalid."
// @Router /component [delete]
func (h *componentHandler) UnRegister(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
component := vars["component"]
addr := vars["addr"]
m := h.svr.GetComponentManager()
err := m.UnRegister(component, addr)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, nil)
}

// @Tags component
// @Summary List all component addresses
// @Produce json
// @Success 200 {object} Addresses
// @Router /component [get]
func (h *componentHandler) GetAllAddress(w http.ResponseWriter, r *http.Request) {
m := h.svr.GetComponentManager()
addrs := m.GetAllComponentAddrs()
h.rd.JSON(w, http.StatusOK, addrs)
}

// @Tags component
// @Summary List component addresses
// @Produce json
// @Success 200 {array} string
// @Failure 404 {string} string "The component does not exist."
// @Router /component/{type} [get]
func (h *componentHandler) GetAddress(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
component := vars["type"]
m := h.svr.GetComponentManager()
addrs := m.GetComponentAddrs(component)

if len(addrs) == 0 {
h.rd.JSON(w, http.StatusNotFound, "component not found")
return
}
h.rd.JSON(w, http.StatusOK, addrs)
}
Loading