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

chore: improve logging with additional test coverage #1937

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 23 additions & 9 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,19 +510,25 @@ func NewClient(ctx context.Context, d cloudsql.Dialer, l cloudsql.Logger, conf *
for _, inst := range conf.Instances {
m, err := c.newSocketMount(ctx, conf, pc, inst)
if err != nil {
for _, m := range mnts {
mErr := m.Close()
annafang-google marked this conversation as resolved.
Show resolved Hide resolved
if mErr != nil {
l.Errorf("failed to close mount: %v", mErr)
}
c.logger.Errorf("[%v] Unable to mount socket: %v", inst.Name, err)
enocom marked this conversation as resolved.
Show resolved Hide resolved
if networkType(conf, inst) == "unix" {
continue
}
switch err.(type) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of switching on error type, could we just continue if the socket type is a Unix socket (like what we had before)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will miss the case when the type is TCP socket and there is no active instance. Correct me if I'm wrong, my understanding from the discussion offline:

  • Inactive instance with unix socket should fail
  • Inactive instance with TCP socket with no port number configured should fail.

// this is not the inactive instance case. Fail the proxy
// so that the user could fix the proxy command error.
case *net.OpError:
Copy link
Member

Choose a reason for hiding this comment

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

In what cases does this error get returned? Could we add a comment clarifying that?

Copy link
Member

Choose a reason for hiding this comment

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

Since this isn't Unix related, I think we shouldn't add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the comments. This is to handle the case where listen call could fail.

return nil, fmt.Errorf("[%v] Unable to mount socket: %v", inst.Name, err)
}
return nil, fmt.Errorf("[%v] Unable to mount socket: %v", inst.Name, err)
}

l.Infof("[%s] Listening on %s", inst.Name, m.Addr())
mnts = append(mnts, m)
}
c.mnts = mnts
if len(mnts) == 0 {
return nil, fmt.Errorf("No active instance to mount")
}
return c, nil
}

Expand Down Expand Up @@ -747,6 +753,14 @@ type socketMount struct {
dialOpts []cloudsqlconn.DialOption
}

func networkType(conf *Config, inst InstanceConnConfig) string {
if (conf.UnixSocket == "" && inst.UnixSocket == "" && inst.UnixSocketPath == "") ||
(inst.Addr != "" || inst.Port != 0) {
return "tcp"
}
return "unix"
}

func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfig, inst InstanceConnConfig) (*socketMount, error) {
var (
// network is one of "tcp" or "unix"
Expand All @@ -765,8 +779,7 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi
// instance)
// use a TCP listener.
// Otherwise, use a Unix socket.
if (conf.UnixSocket == "" && inst.UnixSocket == "" && inst.UnixSocketPath == "") ||
(inst.Addr != "" || inst.Port != 0) {
if networkType(conf, inst) == "tcp" {
network = "tcp"

a := conf.Addr
Expand All @@ -784,7 +797,6 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi
version, err := c.dialer.EngineVersion(ctx, inst.Name)
if err != nil {
c.logger.Errorf("could not resolve version for %q: %v", inst.Name, err)
return nil, err
enocom marked this conversation as resolved.
Show resolved Hide resolved
enocom marked this conversation as resolved.
Show resolved Hide resolved
}
np = pc.nextDBPort(version)
}
Expand All @@ -801,6 +813,8 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi

address, err = newUnixSocketMount(inst, conf.UnixSocket, strings.HasPrefix(version, "POSTGRES"))
if err != nil {
c.logger.Errorf("could not mount unix socket %q for %q: %v",
Copy link
Member

Choose a reason for hiding this comment

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

Let's consolidate all logging up above in NewClient.

Copy link
Member

Choose a reason for hiding this comment

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

I see that we're not doing that consistently above. We should confirm that we're not logging this error twice, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, removed the NewClient logs and kept logs at the newSocketMount level.

conf.UnixSocket, inst.Name, err)
return nil, err
enocom marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
97 changes: 97 additions & 0 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package proxy_test
import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
Expand Down Expand Up @@ -83,6 +84,8 @@ func (f *fakeDialer) EngineVersion(_ context.Context, inst string) (string, erro
return "MYSQL_8_0", nil
case strings.Contains(inst, "sqlserver"):
return "SQLSERVER_2019_STANDARD", nil
case strings.Contains(inst, "fakeserver"):
return "", fmt.Errorf("non existing server")
default:
return "POSTGRES_14", nil
}
Expand Down Expand Up @@ -306,6 +309,25 @@ func TestClientInitialization(t *testing.T) {
filepath.Join(testUnixSocketPathPg),
},
},
{
desc: "without TCP port or unix socket for non functional instance",
in: &proxy.Config{
Copy link
Member

Choose a reason for hiding this comment

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

The test below is build around wantTCPAddrs or wantUnixAddrs. So as written, this test will never fail. To ensure we have the behavior we want here, we should add a functioning socket with a non functioning one and ensure we get what we expect with wantTCPAddrs or wantUnixAddrs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fake dialer's EngineVersion() added a fakeserver case to mimic the failed instance case. EngineVersion() call will fail in the NewClient() call. What the test does is to make sure that NewClient() will proceed upon the failure of EngineVersion().

Instances: []proxy.InstanceConnConfig{
{Name: "proj:region:fakeserver"},
},
},
},
{
desc: "with TCP port for non functional instance",
enocom marked this conversation as resolved.
Show resolved Hide resolved
in: &proxy.Config{
Instances: []proxy.InstanceConnConfig{
{Name: "proj:region:fakeserver", Port: 50000},
},
},
wantTCPAddrs: []string{
"127.0.0.1:50000",
},
},
}

for _, tc := range tcs {
Expand Down Expand Up @@ -711,3 +733,78 @@ func TestRunConnectionCheck(t *testing.T) {
}

}

func TestProxyInitializationWithFailedUnixSocket(t *testing.T) {
ctx := context.Background()
testDir, _ := createTempDir(t)
testUnixSocketPath := path.Join(testDir, "db")

tcs := []struct {
desc string
in *proxy.Config
}{
{
desc: "with unix socket for non functional instance",
in: &proxy.Config{
Instances: []proxy.InstanceConnConfig{
{
Name: "proj:region:fakeserver",
UnixSocketPath: testUnixSocketPath,
},
},
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
_, err := proxy.NewClient(ctx, &fakeDialer{}, testLogger, tc.in)
if err == nil {
t.Fatalf("want non nil error, got = %v", err)
}
})
}
}

func TestProxyMultiInstances(t *testing.T) {
ctx := context.Background()
testDir, _ := createTempDir(t)
testUnixSocketPath := path.Join(testDir, "db")

tcs := []struct {
desc string
in *proxy.Config
wantSuccess bool
}{
{
desc: "with tcp socket and unix for non functional instance",
in: &proxy.Config{
Instances: []proxy.InstanceConnConfig{
{
Name: "proj:region:fakeserver",
UnixSocketPath: testUnixSocketPath,
},
{Name: mysql, Port: 3306},
},
},
wantSuccess: true,
},
{
desc: "with two tcp socket instances",
in: &proxy.Config{
Instances: []proxy.InstanceConnConfig{
{Name: "proj:region:fakeserver", Port: 60000},
{Name: mysql, Port: 60000},
},
},
wantSuccess: false,
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
_, err := proxy.NewClient(ctx, &fakeDialer{}, testLogger, tc.in)
if tc.wantSuccess != (err == nil) {
Copy link
Member

Choose a reason for hiding this comment

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

Could this be:

if tc.wantSuccess && err != nil {
// ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the proposed change, if tc.wantSuccess is false (we want NewClient() to fail), the branch will not be executed regardless what the err is. This will miss reporting the failing case when err is nil and we want NewClient() to fail.

t.Fatalf("want return = %v, got = %v", tc.wantSuccess, err == nil)
}
})
}
}
Loading