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

config: fix url config #43

Merged
merged 2 commits into from
Aug 18, 2022
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
14 changes: 11 additions & 3 deletions cmd/weirproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pingcap/TiProxy/pkg/config"
"github.com/pingcap/TiProxy/pkg/server"
"github.com/pingcap/TiProxy/pkg/util/cmd"
"github.com/pingcap/TiProxy/pkg/util/waitgroup"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -71,14 +72,21 @@ func main() {
return err
}

<-cmd.Context().Done()
var wg waitgroup.WaitGroup
wg.Run(func() {
if err := srv.Run(cmd.Context()); err != nil {
logger.Error("shutdown with error", zap.Error(err))
}
})
Comment on lines +75 to +80
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Run(the listener) is blocking, we must run it in a separate go routine.


if err = srv.Close(); err != nil {
logger.Error("shutdown with errors", zap.Error(err))
<-cmd.Context().Done()
if e := srv.Close(); e != nil {
logger.Error("shutdown with errors", zap.Error(e))
} else {
logger.Info("gracefully shutdown")
}

wg.Wait()
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions conf/namespace/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace: ""
frontend:
security:
backend:
instances:
- "127.0.0.1:4000"
selector_type: "random"
security:
8 changes: 7 additions & 1 deletion conf/weirproxy.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
workdir: "./work"
listen-urls:
- "0.0.0.0:2379"
- "http://0.0.0.0:3080"
advertise-urls:
- "http://127.0.0.1:3080"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why some of them are 0.0.0.0 while others 127.0.0.1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

advertise-xxx is used by etcd server to tell other etcd servers what address they should use to connect. 0.0.0.0 is correct for Listen() but wrong for Dial(). We could change both to 127.0.0.1.

listen/advertise-urls are used for etcd client. xxx-peer-urls are for inter-etcd-servers communication.

listen-peer-urls:
- "http://0.0.0.0:3081"
advertise-peer-urls:
- "http://127.0.0.1:3081"
config:
ignore_wrong_namespace: true
proxy:
Expand Down
56 changes: 51 additions & 5 deletions pkg/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ const (
)

type Config struct {
Workdir string `yaml:"workdir"`
LCUrls []url.URL `yaml:"listen-urls"`
ACUrls []url.URL `yaml:"advertise-urls"`
LPUrls []url.URL `yaml:"listen-peer-urls"`
APUrls []url.URL `yaml:"advertise-peer-urls"`
Workdir string `yaml:"workdir"`

LCUrlsI []string `yaml:"listen-urls"`
ACUrlsI []string `yaml:"advertise-urls"`
LPUrlsI []string `yaml:"listen-peer-urls"`
APUrlsI []string `yaml:"advertise-peer-urls"`
LCUrls []url.URL `yaml:"-"`
ACUrls []url.URL `yaml:"-"`
LPUrls []url.URL `yaml:"-"`
APUrls []url.URL `yaml:"-"`

Config ConfigManager `yaml:"config"`
Proxy ProxyServer `yaml:"proxy"`
Expand Down Expand Up @@ -108,6 +113,23 @@ func NewConfig(data []byte) (*Config, error) {
}

func (cfg *Config) Check() error {
var err error
cfg.LCUrls, err = str2url(cfg.LCUrlsI)
if err != nil {
return err
}
cfg.ACUrls, err = str2url(cfg.ACUrlsI)
if err != nil {
return err
}
cfg.LPUrls, err = str2url(cfg.LPUrlsI)
if err != nil {
return err
}
cfg.APUrls, err = str2url(cfg.APUrlsI)
if err != nil {
return err
}
if cfg.Metrics.PromCluster == "" {
cfg.Metrics.PromCluster = DefaultClusterName
}
Expand All @@ -122,5 +144,29 @@ func (cfg *Config) Check() error {
}

func (cfg *Config) ToBytes() ([]byte, error) {
cfg.LCUrlsI = url2str(cfg.LCUrls)
cfg.ACUrlsI = url2str(cfg.ACUrls)
cfg.LPUrlsI = url2str(cfg.LPUrls)
cfg.APUrlsI = url2str(cfg.APUrls)
xhebox marked this conversation as resolved.
Show resolved Hide resolved
return yaml.Marshal(cfg)
}

func str2url(us []string) ([]url.URL, error) {
r := make([]url.URL, len(us))
for i, ustr := range us {
url, err := url.Parse(ustr)
if err != nil {
return r, err
}
r[i] = *url
}
return r, nil
}

func url2str(us []url.URL) []string {
r := make([]string, len(us))
for i, u := range us {
r[i] = u.String()
}
return r
}
12 changes: 9 additions & 3 deletions pkg/config/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (

var testProxyConfig = Config{
Workdir: "./wd",
LCUrlsI: []string{"http://0.0.0.0:3080"},
ACUrlsI: []string{},
LPUrlsI: []string{"http://0.0.0.0:3081"},
APUrlsI: []string{},
LCUrls: []url.URL{},
ACUrls: []url.URL{},
LPUrls: []url.URL{},
Expand Down Expand Up @@ -55,9 +59,11 @@ var testProxyConfig = Config{
}

func TestProxyConfig(t *testing.T) {
data, err := testProxyConfig.ToBytes()
data1, err := testProxyConfig.ToBytes()
require.NoError(t, err)
cfg, err := NewConfig(data)
cfg, err := NewConfig(data1)
require.NoError(t, err)
require.Equal(t, testProxyConfig, *cfg)
data2, err := cfg.ToBytes()
require.NoError(t, err)
require.Equal(t, data1, data2)
}
6 changes: 4 additions & 2 deletions pkg/manager/namespace/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func (mgr *NamespaceManager) buildNamespace(cfg *config.Namespace, client *clien
r.frontendTLS = &tls.Config{}

if !cfg.Frontend.Security.HasCert() {
return nil, errors.Errorf("require certificates to secure frontend tls connections")
// TODO: require certs here
logger.Warn("require certificates to secure frontend tls connections")
} else {
cert, err := tls.LoadX509KeyPair(cfg.Frontend.Security.Cert, cfg.Frontend.Security.Key)
if err != nil {
Expand Down Expand Up @@ -83,7 +84,8 @@ func (mgr *NamespaceManager) buildNamespace(cfg *config.Namespace, client *clien
r.backendTLS = &tls.Config{}
// backend tls configuration
if !cfg.Backend.Security.HasCA() {
return nil, errors.Errorf("require signed certs to verify backend tls connections")
// TODO: require certs here
logger.Error("require signed certs to verify backend tls connections")
} else {
r.backendTLS.RootCAs = x509.NewCertPool()
certBytes, err := ioutil.ReadFile(cfg.Backend.Security.CA)
Expand Down
12 changes: 7 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net/http"
"path/filepath"
"time"

ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -93,6 +94,8 @@ func NewServer(ctx context.Context, cfg *config.Config, logger *zap.Logger, name
etcd_cfg.ACUrls = cfg.ACUrls
etcd_cfg.LPUrls = cfg.LPUrls
etcd_cfg.APUrls = cfg.APUrls
etcd_cfg.Name = "proxy-" + fmt.Sprint(time.Now().UnixMicro())
etcd_cfg.InitialCluster = etcd_cfg.InitialClusterFromName(etcd_cfg.Name)
etcd_cfg.Dir = filepath.Join(cfg.Workdir, "etcd")
etcd_cfg.ZapLoggerBuilder = embed.NewZapLoggerBuilder(logger.Named("etcd"))
etcd_cfg.UserHandlers = map[string]http.Handler{
Expand Down Expand Up @@ -162,17 +165,16 @@ func NewServer(ctx context.Context, cfg *config.Config, logger *zap.Logger, name
err = errors.WithStack(err)
return
}

go func() {
}()
}

ready.Toggle()

err = srv.Proxy.Run(ctx)
return
}

func (s *Server) Run(ctx context.Context) error {
return s.Proxy.Run(ctx)
}

func (s *Server) Close() error {
var errs []error
if s.Proxy != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/util/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func RunRootCommand(rootCmd *cobra.Command) {

// wait for quit signals
<-sc

cancel()
}()

Expand Down