Skip to content

Commit

Permalink
Add a public ServerDefaults to give access to FSServerDefaults
Browse files Browse the repository at this point in the history
Fixes #219
  • Loading branch information
colinmarc committed Feb 8, 2022
1 parent 1f6a380 commit 039ab59
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
17 changes: 0 additions & 17 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,23 +307,6 @@ func (c *Client) CopyToRemote(src string, dst string) error {
return remote.Close()
}

func (c *Client) fetchDefaults() (*hdfs.FsServerDefaultsProto, error) {
if c.defaults != nil {
return c.defaults, nil
}

req := &hdfs.GetServerDefaultsRequestProto{}
resp := &hdfs.GetServerDefaultsResponseProto{}

err := c.namenode.Execute("getServerDefaults", req, resp)
if err != nil {
return nil, err
}

c.defaults = resp.GetServerDefaults()
return c.defaults, nil
}

func (c *Client) fetchDataEncryptionKey() (*hdfs.DataEncryptionKeyProto, error) {
if c.encryptionKey != nil {
return c.encryptionKey, nil
Expand Down
57 changes: 57 additions & 0 deletions defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hdfs

import (
hdfs "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_hdfs"
)

// ServerDefaults represents the filesystem configuration stored on the
// Namenode.
type ServerDefaults struct {
BlockSize int64
BytesPerChecksum int
WritePacketSize int
Replication int
FileBufferSize int
EncryptDataTransfer bool
TrashInterval int64
KeyProviderURI string
PolicyId int
}

// ServerDefaults fetches the stored defaults from the Namenode and returns
// them and any error encountered.
func (c *Client) ServerDefaults() (ServerDefaults, error) {
resp, err := c.fetchDefaults()
if err != nil {
return ServerDefaults{}, err
}

return ServerDefaults{
BlockSize: int64(resp.GetBlockSize()),
BytesPerChecksum: int(resp.GetBytesPerChecksum()),
WritePacketSize: int(resp.GetWritePacketSize()),
Replication: int(resp.GetReplication()),
FileBufferSize: int(resp.GetFileBufferSize()),
EncryptDataTransfer: resp.GetEncryptDataTransfer(),
TrashInterval: int64(resp.GetTrashInterval()),
KeyProviderURI: resp.GetKeyProviderUri(),
PolicyId: int(resp.GetPolicyId()),
}, nil
}

func (c *Client) fetchDefaults() (*hdfs.FsServerDefaultsProto, error) {
if c.defaults != nil {
return c.defaults, nil
}

req := &hdfs.GetServerDefaultsRequestProto{}
resp := &hdfs.GetServerDefaultsResponseProto{}

err := c.namenode.Execute("getServerDefaults", req, resp)
if err != nil {
return nil, err
}

c.defaults = resp.GetServerDefaults()
return c.defaults, nil
}
16 changes: 16 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hdfs

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestServerDefaults(t *testing.T) {
client := getClient(t)

sd, err := client.ServerDefaults()
require.NoError(t, err)
assert.NotZero(t, sd.BlockSize)
}

0 comments on commit 039ab59

Please sign in to comment.