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 unix socket communication #614

Merged
merged 3 commits into from
Apr 12, 2024
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
9 changes: 9 additions & 0 deletions cmd/router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,15 @@ var runCmd = &cobra.Command{
wg.Done()
}(wg)

wg.Add(1)
go func(wg *sync.WaitGroup) {
err := app.ServceUnixSocket(ctx)
if err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
}
wg.Done()
}(wg)

wg.Wait()

return nil
Expand Down
48 changes: 47 additions & 1 deletion coordinator/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package app

import (
"context"
"fmt"
"net"
"os"
"path"
"sync"

"github.com/pg-sharding/spqr/pkg/spqrlog"
"github.com/pg-sharding/spqr/router/port"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
Expand Down Expand Up @@ -55,6 +59,12 @@ func (app *App) Run(withPsql bool) error {
}
}(wg)
}
wg.Add(1)
go func(wg *sync.WaitGroup) {
if err := app.ServeUnixSocket(wg); err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
}
}(wg)

wg.Wait()

Expand Down Expand Up @@ -104,7 +114,7 @@ func (app *App) ServeCoordinator(wg *sync.WaitGroup) error {
go func() {
defer app.sem.Release(1)

err := app.coordinator.ProcClient(context.TODO(), conn)
err := app.coordinator.ProcClient(context.TODO(), conn, port.DefaultRouterPortType)
if err != nil {
spqrlog.Zero.Error().Err(err).Msg("failed to serve client")
}
Expand Down Expand Up @@ -150,3 +160,39 @@ func (app *App) ServeGrpcApi(wg *sync.WaitGroup) error {

return serv.Serve(listener)
}

func (app *App) ServeUnixSocket(wg *sync.WaitGroup) error {
defer wg.Done()

if err := os.MkdirAll(config.UnixSocketDirectory, 0777); err != nil {
return err
}
socketPath := path.Join(config.UnixSocketDirectory, fmt.Sprintf(".s.PGSQL.%s", config.CoordinatorConfig().CoordinatorPort))
lAddr := &net.UnixAddr{Name: socketPath, Net: "unix"}
listener, err := net.ListenUnix("unix", lAddr)
if err != nil {
return err
}

for {
conn, err := listener.Accept()
if err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
continue
}

if err := app.sem.Acquire(context.Background(), 1); err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
continue
}

go func() {
defer app.sem.Release(1)

err := app.coordinator.ProcClient(context.TODO(), conn, port.UnixSocketPortType)
if err != nil {
spqrlog.Zero.Error().Err(err).Msg("failed to serve client")
}
}()
}
}
14 changes: 9 additions & 5 deletions coordinator/provider/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,14 @@ func (qc *qdbCoordinator) RemoveTaskGroup(ctx context.Context) error {
}

// TODO : unit tests
func (qc *qdbCoordinator) PrepareClient(nconn net.Conn) (CoordinatorClient, error) {
cl := psqlclient.NewPsqlClient(nconn, port.DefaultRouterPortType, "")
func (qc *qdbCoordinator) PrepareClient(nconn net.Conn, pt port.RouterPortType) (CoordinatorClient, error) {
cl := psqlclient.NewPsqlClient(nconn, pt, "")

if err := cl.Init(qc.tlsconfig); err != nil {
tlsconfig := qc.tlsconfig
if pt == port.UnixSocketPortType {
tlsconfig = nil
}
if err := cl.Init(tlsconfig); err != nil {
return nil, err
}

Expand Down Expand Up @@ -1094,8 +1098,8 @@ func (qc *qdbCoordinator) PrepareClient(nconn net.Conn) (CoordinatorClient, erro
}

// TODO : unit tests
func (qc *qdbCoordinator) ProcClient(ctx context.Context, nconn net.Conn) error {
cl, err := qc.PrepareClient(nconn)
func (qc *qdbCoordinator) ProcClient(ctx context.Context, nconn net.Conn, pt port.RouterPortType) error {
cl, err := qc.PrepareClient(nconn, pt)
if err != nil {
spqrlog.Zero.Error().Err(err).Msg("")
return err
Expand Down
2 changes: 2 additions & 0 deletions docker/coordinator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
FROM spqr-base-image

RUN apt-get update && apt-get install -y postgresql-client

ENTRYPOINT /spqr/spqr-coordinator -c ${COORDINATOR_CONFIG=/spqr/docker/coordinator/cfg.yaml}
3 changes: 2 additions & 1 deletion pkg/clientinteractor/interactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pg-sharding/spqr/pkg/pool"
"github.com/pg-sharding/spqr/pkg/shard"
"github.com/pg-sharding/spqr/pkg/txstatus"
"github.com/pg-sharding/spqr/router/port"
"github.com/pg-sharding/spqr/router/statistics"
spqrparser "github.com/pg-sharding/spqr/yacc/console"

Expand All @@ -31,7 +32,7 @@ import (
)

type Interactor interface {
ProcClient(ctx context.Context, nconn net.Conn) error
ProcClient(ctx context.Context, nconn net.Conn, pt port.RouterPortType) error
}

type PSQLInteractor struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/auth.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package config

const (
UnixSocketDirectory = "/var/run/postgresql"
)

type AuthMethod string

const (
Expand Down
28 changes: 28 additions & 0 deletions router/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package app

import (
"context"
"fmt"
"net"
"os"
"path"
"sync"

reuse "github.com/libp2p/go-reuseport"
"github.com/pg-sharding/spqr/pkg/config"
"github.com/pg-sharding/spqr/pkg/spqrlog"
rgrpc "github.com/pg-sharding/spqr/router/grpc"
"github.com/pg-sharding/spqr/router/instance"
Expand Down Expand Up @@ -108,3 +112,27 @@ func (app *App) ServeGrpcApi(ctx context.Context) error {
server.GracefulStop()
return nil
}

func (app *App) ServceUnixSocket(ctx context.Context) error {
if err := os.MkdirAll(config.UnixSocketDirectory, 0777); err != nil {
return err
}
socketPath := path.Join(config.UnixSocketDirectory, fmt.Sprintf(".s.PGSQL.%s", app.spqr.Config().RouterPort))
lAddr := &net.UnixAddr{Name: socketPath, Net: "unix"}
listener, err := net.ListenUnix("unix", lAddr)
if err != nil {
return err
}
defer func(listener net.Listener) {
_ = listener.Close()
}(listener)

spqrlog.Zero.Info().
Msg("SPQR Router is ready by unix socket")
go func() {
_ = app.spqr.Run(ctx, listener, port.UnixSocketPortType)
}()

<-ctx.Done()
return nil
}
2 changes: 2 additions & 0 deletions router/port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ const (
RORouterPortType = RouterPortType(1)

ADMRouterPortType = RouterPortType(2)

UnixSocketPortType = RouterPortType(3)
)
9 changes: 7 additions & 2 deletions router/rulerouter/rulerouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package rulerouter
import (
"crypto/tls"
"fmt"
"github.com/pg-sharding/spqr/pkg/models/spqrerror"
"net"
"sync"

"github.com/pg-sharding/spqr/pkg/models/spqrerror"

"github.com/jackc/pgx/v5/pgproto3"
"github.com/pg-sharding/spqr/pkg/auth"
"github.com/pg-sharding/spqr/pkg/client"
Expand Down Expand Up @@ -171,7 +172,11 @@ func NewRouter(tlsconfig *tls.Config, rcfg *config.Router, notifier *notifier.No
func (r *RuleRouterImpl) PreRoute(conn net.Conn, pt port.RouterPortType) (rclient.RouterClient, error) {
cl := rclient.NewPsqlClient(conn, pt, r.Config().Qr.DefaultRouteBehaviour)

if err := cl.Init(r.tlsconfig); err != nil {
tlsConfig := r.tlsconfig
if pt == port.UnixSocketPortType {
tlsConfig = nil
}
if err := cl.Init(tlsConfig); err != nil {
return cl, err
}

Expand Down
Loading