Skip to content

Commit

Permalink
fix: wrong calculation of network stats and add some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
buraksezer committed Jul 11, 2021
1 parent b28d783 commit 5e134fc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const (
// Olric project.
DefaultStorageEngine = "kvstore"

// DefaultRoutingTablePushInterval is interval between routing table push events.
DefaultRoutingTablePushInterval = time.Minute
)

Expand Down Expand Up @@ -168,6 +169,7 @@ type Config struct {
// bootstrapping status without blocking indefinitely.
BootstrapTimeout time.Duration

// RoutingTablePushInterval is interval between routing table push events.
RoutingTablePushInterval time.Duration

// The list of host:port which are used by memberlist for discovery.
Expand Down
6 changes: 3 additions & 3 deletions internal/kvstore/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ func (e *Entry) Encode() []byte {
func (e *Entry) Decode(buf []byte) {
var offset int

klen := int(uint8(buf[offset]))
keyLength := int(buf[offset])
offset++

e.key = string(buf[offset : offset+klen])
offset += klen
e.key = string(buf[offset : offset+keyLength])
offset += keyLength

e.ttl = int64(binary.BigEndian.Uint64(buf[offset : offset+8]))
offset += 8
Expand Down
4 changes: 2 additions & 2 deletions internal/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type EncodeDecoder interface {
Response(*bytes.Buffer) EncodeDecoder
}

const headerSize int64 = 6
const HeaderLength int64 = 6

// Header is a shared message header for all the message types in Olric Binary Protocol.
type Header struct {
Expand All @@ -98,7 +98,7 @@ func readHeader(conn io.ReadWriteCloser) (*Header, error) {

// Read the header section. The first 6 bytes.
var header Header
_, err := io.CopyN(buf, conn, headerSize)
_, err := io.CopyN(buf, conn, HeaderLength)
if err != nil {
return nil, filterNetworkErrors(err)
}
Expand Down
20 changes: 16 additions & 4 deletions internal/transport/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *Client) Close() {
}

// ClosePool closes the underlying connections in a pool,
// deletes from Olric's pools map and frees resources.
// deletes from the pools map and frees resources.
func (c *Client) ClosePool(addr string) {
c.mu.Lock()
defer c.mu.Unlock()
Expand All @@ -93,7 +93,14 @@ func (c *Client) pool(addr string) (connpool.Pool, error) {
}

factory := func() (net.Conn, error) {
return c.dialer.Dial("tcp", addr)
conn, err := c.dialer.Dial("tcp", addr)
if err != nil {
return nil, err
}

ConnectionsTotal.Increase(1)
CurrentConnections.Increase(1)
return conn, nil
}

p, err := connpool.NewChannelPool(c.config.MinConn, c.config.MaxConn, factory)
Expand Down Expand Up @@ -148,6 +155,7 @@ func (c *Client) teardownConn(rawConn net.Conn, dead bool) {

pc, _ := rawConn.(*connpool.PoolConn)
if dead {
CurrentConnections.Decrease(1)
pc.MarkUnusable()
}
err := pc.Close()
Expand Down Expand Up @@ -176,21 +184,25 @@ func (c *Client) RequestTo(addr string, req protocol.EncodeDecoder) (protocol.En
if err != nil {
return nil, err
}
_, err = req.Buffer().WriteTo(conn)

nr, err := req.Buffer().WriteTo(conn)
if err != nil {
dead = true
return nil, err
}
WrittenBytesTotal.Increase(nr)

// Await for the response
buf.Reset()
_, err = protocol.ReadMessage(conn, buf)
h, err := protocol.ReadMessage(conn, buf)
if err != nil {
// Failed to read message from the TCP socket. Close it.
dead = true
return nil, err
}

ReadBytesTotal.Increase(protocol.HeaderLength + int64(h.MessageLength))

// Response is a shortcut to create a response message for the request.
resp := req.Response(buf)
err = resp.Decode()
Expand Down
8 changes: 4 additions & 4 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ type (
MemberID uint64
)

// SlabInfo denotes memory usage of the storage engine(a hash indexed append only log file).
// SlabInfo denotes memory usage of the storage engine(a hash indexed, append only byte slice).
type SlabInfo struct {
// Total allocated space by the append-only log files.
// Total allocated space by the append-only byte slice.
Allocated int

// Total inuse memory space in the append-only log files.
// Total inuse memory space in the append-only byte slice.
Inuse int

// Total garbage(deleted key/value pairs) space in the append-only log files.
// Total garbage(deleted key/value pairs) space in the append-only byte slice.
Garbage int
}

Expand Down

0 comments on commit 5e134fc

Please sign in to comment.