Skip to content

Commit

Permalink
Randomize endorsement layouts
Browse files Browse the repository at this point in the history
For improved load balancing and fairer workload distribution across organizations, then gateway should randomize the endorsement layouts

Signed-off-by: andrew-coleman <[email protected]>
  • Loading branch information
andrew-coleman authored and denyeart committed Dec 2, 2021
1 parent 29d1e21 commit 2d8d7f4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
18 changes: 1 addition & 17 deletions internal/pkg/gateway/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,30 +573,14 @@ func TestEndorse(t *testing.T) {
errString: "failed to find any endorsing peers for org(s): msp4, msp5",
},
{
name: "endorse with multiple layouts - default choice first layout",
name: "endorse retry - localhost and peer3 fail - retry on peer1 and peer2",
plan: endorsementPlan{
"g1": {{endorser: localhostMock, height: 4}, {endorser: peer1Mock, height: 4}}, // msp1
"g2": {{endorser: peer2Mock, height: 3}, {endorser: peer3Mock, height: 4}}, // msp2
"g3": {{endorser: peer4Mock, height: 5}}, // msp3
},
layouts: []endorsementLayout{
{"g1": 1, "g2": 1},
{"g1": 1, "g3": 1},
{"g2": 1, "g3": 1},
},
expectedEndorsers: []string{"localhost:7051", "peer3:10051"},
},
{
name: "endorse retry - localhost and peer2 fail - retry on peer1 and peer2",
plan: endorsementPlan{
"g1": {{endorser: localhostMock, height: 4}, {endorser: peer1Mock, height: 4}}, // msp1
"g2": {{endorser: peer2Mock, height: 3}, {endorser: peer3Mock, height: 4}}, // msp2
"g3": {{endorser: peer4Mock, height: 5}}, // msp3
},
layouts: []endorsementLayout{
{"g1": 1, "g2": 1},
{"g1": 1, "g3": 1},
{"g2": 1, "g3": 1},
},
postSetup: func(t *testing.T, def *preparedTest) {
def.localEndorser.ProcessProposalReturns(nil, status.Error(codes.Aborted, "bad local endorser"))
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/gateway/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package gateway
import (
"bytes"
"fmt"
"math/rand"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -139,6 +140,9 @@ layout:
}
}

// shuffle the layouts - keep the preferred ones first
rand.Shuffle(len(preferredLayouts), func(i, j int) { preferredLayouts[i], preferredLayouts[j] = preferredLayouts[j], preferredLayouts[i] })
rand.Shuffle(len(otherLayouts), func(i, j int) { otherLayouts[i], otherLayouts[j] = otherLayouts[j], otherLayouts[i] })
layouts := append(preferredLayouts, otherLayouts...)

if len(layouts) == 0 {
Expand Down

0 comments on commit 2d8d7f4

Please sign in to comment.