From 0f066e855de65bd410da851883873f96872d989f Mon Sep 17 00:00:00 2001 From: sylwiaszunejko Date: Thu, 6 Jun 2024 08:13:27 +0200 Subject: [PATCH] Fix setting up connection to non-IP sockets --- control.go | 6 +++- control_integration_test.go | 55 +++++++++++++++++++++++++++++++++++++ docker-compose.yml | 1 + integration.sh | 2 ++ testdata/config/scylla.yaml | 1 + 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 control_integration_test.go diff --git a/control.go b/control.go index 3806f45a3..5a21baf26 100644 --- a/control.go +++ b/control.go @@ -270,7 +270,11 @@ type connHost struct { func (c *controlConn) setupConn(conn *Conn) error { // we need up-to-date host info for the filterHost call below iter := conn.querySystemLocal(context.TODO()) - host, err := c.session.hostInfoFromIter(iter, conn.host.connectAddress, conn.conn.RemoteAddr().(*net.TCPAddr).Port) + defaultPort := 9042 + if tcpAddr, ok := conn.conn.RemoteAddr().(*net.TCPAddr); ok { + defaultPort = tcpAddr.Port + } + host, err := c.session.hostInfoFromIter(iter, conn.host.connectAddress, defaultPort) if err != nil { return err } diff --git a/control_integration_test.go b/control_integration_test.go new file mode 100644 index 000000000..afb95c968 --- /dev/null +++ b/control_integration_test.go @@ -0,0 +1,55 @@ +//go:build integration && scylla +// +build integration,scylla + +package gocql + +import ( + "context" + "fmt" + "net" + "testing" +) + +// unixSocketDialer is a special dialer which connects only to the maintenance_socket. +type unixSocketDialer struct { + dialer net.Dialer + socketPath string +} + +func (d unixSocketDialer) DialContext(_ context.Context, _, _ string) (net.Conn, error) { + return d.dialer.Dial("unix", d.socketPath) +} + +func TestUnixSockets(t *testing.T) { + socketPath := "/tmp/scylla/cql.m" + + c := createCluster() + c.NumConns = 1 + c.DisableInitialHostLookup = true + c.ProtoVersion = 3 + c.ReconnectInterval = 0 + c.WriteCoalesceWaitTime = 0 + + c.Events.DisableNodeStatusEvents = true + c.Events.DisableTopologyEvents = true + c.Events.DisableSchemaEvents = true + + d := net.Dialer{ + Timeout: c.Timeout, + } + if c.SocketKeepalive > 0 { + d.KeepAlive = c.SocketKeepalive + } + + c.Dialer = unixSocketDialer{ + dialer: d, + socketPath: socketPath, + } + + sess, err := c.CreateSession() + if err != nil { + panic(fmt.Sprintf("unable to create session: %v", err)) + } + + defer sess.Close() +} diff --git a/docker-compose.yml b/docker-compose.yml index e23a1e15d..7d3a81531 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,7 @@ services: public: ipv4_address: 192.168.100.11 volumes: + - /tmp/scylla:/var/lib/scylla/ - type: bind source: ./testdata/config/scylla.yaml target: /etc/scylla/scylla.yaml diff --git a/integration.sh b/integration.sh index 51f6eeb04..07d67f64b 100755 --- a/integration.sh +++ b/integration.sh @@ -27,6 +27,8 @@ function scylla_restart() { scylla_restart +sudo chmod 0777 /tmp/scylla/cql.m + readonly clusterSize=1 readonly multiNodeClusterSize=3 readonly scylla_liveset="192.168.100.11" diff --git a/testdata/config/scylla.yaml b/testdata/config/scylla.yaml index 945d184bd..b00f15a63 100644 --- a/testdata/config/scylla.yaml +++ b/testdata/config/scylla.yaml @@ -8,5 +8,6 @@ client_encryption_options: keyfile: /etc/scylla/db.key truststore: /etc/scylla/ca.crt require_client_auth: true +maintenance_socket: workdir # when using 5.4.x we have to specify force_schema_commit_log option force_schema_commit_log: true