Skip to content

Commit

Permalink
[FAB-11170] Refactor discovery client cache
Browse files Browse the repository at this point in the history
changed name and type field maxMemorySize in signer.go,
changed code of signer.go to skip putting in memory if maxSize is 0,
changed parameter in CLI from 10 to 0 in stub.go,
updated tests

Change-Id: Ib3c81dafe09a3e267973399d53846aaae9c07689
Signed-off-by: Inbar Badian <Inbar.Badian@ibm.com>
  • Loading branch information
Inbar Badian authored and Inbar Badian committed Jul 22, 2018
1 parent 7d5b728 commit fd76467
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion discovery/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ type endorsementDescriptor struct {
}

// NewClient creates a new Client instance
func NewClient(createConnection Dialer, s Signer, signerCacheSize int) *Client {
func NewClient(createConnection Dialer, s Signer, signerCacheSize uint) *Client {
return &Client{
createConnection: createConnection,
signRequest: NewMemoizeSigner(s, signerCacheSize).Sign,
Expand Down
2 changes: 1 addition & 1 deletion discovery/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
)

const (
signerCacheSize = 1
signerCacheSize uint = 1
)

var (
Expand Down
21 changes: 12 additions & 9 deletions discovery/client/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import (
// MemoizeSigner signs messages with the same signature
// if the message was signed recently
type MemoizeSigner struct {
maxMemorySize int
maxEntries uint
sync.RWMutex
memory map[string][]byte
sign Signer
}

// NewMemoizeSigner creates a new MemoizeSigner that signs
// message with the given sign function
func NewMemoizeSigner(signFunc Signer, maxMemorySize int) *MemoizeSigner {
func NewMemoizeSigner(signFunc Signer, maxEntries uint) *MemoizeSigner {
return &MemoizeSigner{
maxMemorySize: maxMemorySize,
memory: make(map[string][]byte),
sign: signFunc,
maxEntries: maxEntries,
memory: make(map[string][]byte),
sign: signFunc,
}
}

Expand Down Expand Up @@ -57,25 +57,28 @@ func (ms *MemoizeSigner) lookup(msg []byte) ([]byte, bool) {
}

func (ms *MemoizeSigner) memorize(msg, signature []byte) {
if ms.maxEntries == 0 {
return
}
ms.RLock()
shouldShrink := len(ms.memory) >= ms.maxMemorySize
shouldShrink := len(ms.memory) >= (int)(ms.maxEntries)
ms.RUnlock()

if shouldShrink {
ms.shrinkMemory()
}

ms.Lock()
defer ms.Unlock()
ms.memory[msgDigest(msg)] = signature

}

// evict evicts random messages from memory
// until its size is smaller than maxMemorySize
// until its size is smaller than maxEntries
func (ms *MemoizeSigner) shrinkMemory() {
ms.Lock()
defer ms.Unlock()
for len(ms.memory) > ms.maxMemorySize {
for len(ms.memory) > (int)(ms.maxEntries) {
ms.evictFromMemory()
}
}
Expand Down
22 changes: 19 additions & 3 deletions discovery/client/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package discovery

import (
"crypto/rand"
"io"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -37,17 +39,17 @@ func TestSameMessage(t *testing.T) {
}

func TestDifferentMessages(t *testing.T) {
n := 5
var n uint = 5
var signedInvokedCount uint32
sign := func(msg []byte) ([]byte, error) {
atomic.AddUint32(&signedInvokedCount, 1)
return msg, nil
}

ms := NewMemoizeSigner(sign, n)
parallelSignRange := func(start, end int) {
parallelSignRange := func(start, end uint) {
var wg sync.WaitGroup
wg.Add(end - start)
wg.Add((int)(end - start))
for i := start; i < end; i++ {
i := i
go func() {
Expand Down Expand Up @@ -86,3 +88,17 @@ func TestFailure(t *testing.T) {
_, err := ms.Sign([]byte{1, 2, 3})
assert.Equal(t, "something went wrong", err.Error())
}

func TestNotSavingInMem(t *testing.T) {
sign := func(_ []byte) ([]byte, error) {
b := make([]byte, 30)
_, err := io.ReadFull(rand.Reader, b)
assert.NoError(t, err)
return b, nil
}
ms := NewMemoizeSigner(sign, 0)
sig1, _ := ms.sign(([]byte)("aa"))
sig2, _ := ms.sign(([]byte)("aa"))
assert.NotEqual(t, sig1, sig2)

}
2 changes: 1 addition & 1 deletion discovery/cmd/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (stub *ClientStub) Send(server string, conf common.Config, req *discovery.R
timeout, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

disc := discovery.NewClient(comm.NewDialer(server), signer.Sign, 10)
disc := discovery.NewClient(comm.NewDialer(server), signer.Sign, 0)

resp, err := disc.Send(timeout, req, &AuthInfo{
ClientIdentity: signer.Creator,
Expand Down
2 changes: 1 addition & 1 deletion discovery/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func createClientAndService(t *testing.T, testdir string) (*client, *service) {
assert.NoError(t, err)

wrapperClient := &client{AuthInfo: authInfo, conn: conn}
signerCacheSize := 10
var signerCacheSize uint = 10
c := disc.NewClient(wrapperClient.newConnection, signer.Sign, signerCacheSize)
wrapperClient.Client = c
service := &service{Server: gRPCServer.Server(), lc: lc, sup: sup}
Expand Down

0 comments on commit fd76467

Please sign in to comment.