Skip to content

Commit 7dc370a

Browse files
committed
[FAB-1938]: Read orderers endpoints from CB.
Currently there is a paramter in core.yaml file which defines the ordering service endpoint, CORE_PEER_COMMITTER_LEDGER_ORDERER. This commit takes care to read configuration transaction to extract list of orderers endpoints and use it for delivery client. There is no more need in CORE_PEER_COMMITTER_LEDGER_ORDERER, hence relevant section in core.yaml files cleaned as well. Change-Id: I07a5bf19a1725194510afb90dae23c68af6ab95f Signed-off-by: Artem Barger <[email protected]>
1 parent 01cc491 commit 7dc370a

File tree

7 files changed

+39
-48
lines changed

7 files changed

+39
-48
lines changed

core/deliverservice/deliveryclient.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ package deliverclient
1818

1919
import (
2020
"errors"
21+
"fmt"
22+
"math/rand"
2123
"sync"
2224
"time"
2325

24-
"fmt"
25-
2626
"github.com/hyperledger/fabric/core/comm"
2727
"github.com/hyperledger/fabric/core/deliverservice/blocksprovider"
2828
"github.com/hyperledger/fabric/protos/orderer"
2929
"github.com/op/go-logging"
30-
"github.com/spf13/viper"
3130
"golang.org/x/net/context"
3231
"google.golang.org/grpc"
3332
)
@@ -102,26 +101,27 @@ type deliverServiceImpl struct {
102101
// delivery service instance. It tries to establish connection to
103102
// the specified in the configuration ordering service, in case it
104103
// fails to dial to it, return nil
105-
func NewDeliverService(gossip blocksprovider.GossipServiceAdapter) (DeliverService, error) {
106-
// TODO: Has to be fixed as ordering service configuration is part of the part of configuration block
107-
endpoint := viper.GetString("peer.committer.ledger.orderer")
108-
logger.Infof("Creating delivery service to get blocks from the ordering service, %s", endpoint)
104+
func NewDeliverService(gossip blocksprovider.GossipServiceAdapter, endpoints []string) (DeliverService, error) {
105+
indices := rand.Perm(len(endpoints))
106+
for _, idx := range indices {
107+
logger.Infof("Creating delivery service to get blocks from the ordering service, %s", endpoints[idx])
109108

110-
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(3 * time.Second), grpc.WithBlock()}
109+
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(3 * time.Second), grpc.WithBlock()}
111110

112-
if comm.TLSEnabled() {
113-
dialOpts = append(dialOpts, grpc.WithTransportCredentials(comm.InitTLSForPeer()))
114-
} else {
115-
dialOpts = append(dialOpts, grpc.WithInsecure())
116-
}
111+
if comm.TLSEnabled() {
112+
dialOpts = append(dialOpts, grpc.WithTransportCredentials(comm.InitTLSForPeer()))
113+
} else {
114+
dialOpts = append(dialOpts, grpc.WithInsecure())
115+
}
117116

118-
conn, err := grpc.Dial(endpoint, dialOpts...)
119-
if err != nil {
120-
logger.Errorf("Cannot dial to %s, because of %s", endpoint, err)
121-
return nil, err
117+
conn, err := grpc.Dial(endpoints[idx], dialOpts...)
118+
if err != nil {
119+
logger.Errorf("Cannot dial to %s, because of %s", endpoints[idx], err)
120+
continue
121+
}
122+
return NewFactoryDeliverService(gossip, &blocksDelivererFactoryImpl{conn}, conn), nil
122123
}
123-
124-
return NewFactoryDeliverService(gossip, &blocksDelivererFactoryImpl{conn}, conn), nil
124+
return nil, fmt.Errorf("Wasn't able to connect to any of ordering service endpoints %s", endpoints)
125125
}
126126

127127
// NewFactoryDeliverService construction function to create and initialize

core/peer/peer.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package peer
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"math"
2223
"net"
@@ -199,7 +200,11 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
199200
}
200201

201202
c := committer.NewLedgerCommitter(ledger, txvalidator.NewTxValidator(cs))
202-
service.GetGossipService().InitializeChannel(cs.ChainID(), c)
203+
ordererAddresses := configtxManager.ChannelConfig().OrdererAddresses()
204+
if len(ordererAddresses) == 0 {
205+
return errors.New("No orderering service endpoint provided in configuration block")
206+
}
207+
service.GetGossipService().InitializeChannel(cs.ChainID(), c, ordererAddresses)
203208

204209
chains.Lock()
205210
defer chains.Unlock()

core/peer/peer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (*mockDeliveryClient) Stop() {
6060
type mockDeliveryClientFactory struct {
6161
}
6262

63-
func (*mockDeliveryClientFactory) Service(g service.GossipService) (deliverclient.DeliverService, error) {
63+
func (*mockDeliveryClientFactory) Service(g service.GossipService, endpoints []string) (deliverclient.DeliverService, error) {
6464
return &mockDeliveryClient{}, nil
6565
}
6666

core/scc/cscc/configure_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (*mockDeliveryClient) Stop() {
6666
type mockDeliveryClientFactory struct {
6767
}
6868

69-
func (*mockDeliveryClientFactory) Service(g service.GossipService) (deliverclient.DeliverService, error) {
69+
func (*mockDeliveryClientFactory) Service(g service.GossipService, endpoints []string) (deliverclient.DeliverService, error) {
7070
return &mockDeliveryClient{}, nil
7171
}
7272

gossip/service/gossip_service.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type GossipService interface {
5151
// NewConfigEventer creates a ConfigProcessor which the configtx.Manager can ultimately route config updates to
5252
NewConfigEventer() ConfigProcessor
5353
// InitializeChannel allocates the state provider and should be invoked once per channel per execution
54-
InitializeChannel(chainID string, committer committer.Committer)
54+
InitializeChannel(chainID string, committer committer.Committer, endpoints []string)
5555
// GetBlock returns block for given chain
5656
GetBlock(chainID string, index uint64) *common.Block
5757
// AddPayload appends message payload to for given chain
@@ -61,15 +61,15 @@ type GossipService interface {
6161
// DeliveryServiceFactory factory to create and initialize delivery service instance
6262
type DeliveryServiceFactory interface {
6363
// Returns an instance of delivery client
64-
Service(g GossipService) (deliverclient.DeliverService, error)
64+
Service(g GossipService, endpoints []string) (deliverclient.DeliverService, error)
6565
}
6666

6767
type deliveryFactoryImpl struct {
6868
}
6969

7070
// Returns an instance of delivery client
71-
func (*deliveryFactoryImpl) Service(g GossipService) (deliverclient.DeliverService, error) {
72-
return deliverclient.NewDeliverService(g)
71+
func (*deliveryFactoryImpl) Service(g GossipService, endpoints []string) (deliverclient.DeliverService, error) {
72+
return deliverclient.NewDeliverService(g, endpoints)
7373
}
7474

7575
type gossipServiceImpl struct {
@@ -159,15 +159,15 @@ func (g *gossipServiceImpl) NewConfigEventer() ConfigProcessor {
159159
}
160160

161161
// InitializeChannel allocates the state provider and should be invoked once per channel per execution
162-
func (g *gossipServiceImpl) InitializeChannel(chainID string, committer committer.Committer) {
162+
func (g *gossipServiceImpl) InitializeChannel(chainID string, committer committer.Committer, endpoints []string) {
163163
g.lock.Lock()
164164
defer g.lock.Unlock()
165165
// Initialize new state provider for given committer
166166
logger.Debug("Creating state provider for chainID", chainID)
167167
g.chains[chainID] = state.NewGossipStateProvider(chainID, g, committer, g.mcs)
168168
if g.deliveryService == nil {
169169
var err error
170-
g.deliveryService, err = g.deliveryFactory.Service(gossipServiceInstance)
170+
g.deliveryService, err = g.deliveryFactory.Service(gossipServiceInstance, endpoints)
171171
if err != nil {
172172
logger.Warning("Cannot create delivery client, due to", err)
173173
}

gossip/service/gossip_service_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestLeaderElectionWithDeliverClient(t *testing.T) {
118118
gossips[i].(*gossipServiceImpl).deliveryFactory = deliverServiceFactory
119119
deliverServiceFactory.service.running[channelName] = false
120120

121-
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0})
121+
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0}, []string{"localhost:5005"})
122122
service, exist := gossips[i].(*gossipServiceImpl).leaderElection[channelName]
123123
assert.True(t, exist, "Leader election service should be created for peer %d and channel %s", i, channelName)
124124
services[i] = &electionService{nil, false, 0}
@@ -169,7 +169,7 @@ func TestWithStaticDeliverClientLeader(t *testing.T) {
169169
for i := 0; i < n; i++ {
170170
gossips[i].(*gossipServiceImpl).deliveryFactory = deliverServiceFactory
171171
deliverServiceFactory.service.running[channelName] = false
172-
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0})
172+
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0}, []string{"localhost:5005"})
173173
}
174174

175175
for i := 0; i < n; i++ {
@@ -180,7 +180,7 @@ func TestWithStaticDeliverClientLeader(t *testing.T) {
180180
channelName = "chanB"
181181
for i := 0; i < n; i++ {
182182
deliverServiceFactory.service.running[channelName] = false
183-
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0})
183+
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0}, []string{"localhost:5005"})
184184
}
185185

186186
for i := 0; i < n; i++ {
@@ -217,7 +217,7 @@ func TestWithStaticDeliverClientNotLeader(t *testing.T) {
217217
for i := 0; i < n; i++ {
218218
gossips[i].(*gossipServiceImpl).deliveryFactory = deliverServiceFactory
219219
deliverServiceFactory.service.running[channelName] = false
220-
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0})
220+
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0}, []string{"localhost:5005"})
221221
}
222222

223223
for i := 0; i < n; i++ {
@@ -254,7 +254,7 @@ func TestWithStaticDeliverClientBothStaticAndLeaderElection(t *testing.T) {
254254
for i := 0; i < n; i++ {
255255
gossips[i].(*gossipServiceImpl).deliveryFactory = deliverServiceFactory
256256
assert.Panics(t, func() {
257-
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0})
257+
gossips[i].InitializeChannel(channelName, &mockLedgerInfo{0}, []string{"localhost:5005"})
258258
}, "Dynamic leader lection based and static connection to ordering service can't exist simultaniosly")
259259
}
260260

@@ -265,7 +265,7 @@ type mockDeliverServiceFactory struct {
265265
service *mockDeliverService
266266
}
267267

268-
func (mf *mockDeliverServiceFactory) Service(g GossipService) (deliverclient.DeliverService, error) {
268+
func (mf *mockDeliverServiceFactory) Service(g GossipService, endpoints []string) (deliverclient.DeliverService, error) {
269269
return mf.service, nil
270270
}
271271

peer/core.yaml

-14
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,6 @@ peer:
171171
# if > 0, if buffer full, blocks till timeout
172172
timeout: 10
173173

174-
# ----!!!!IMPORTANT!!!-!!!IMPORTANT!!!-!!!IMPORTANT!!!!----
175-
# THIS HAS TO BE DONE IN THE CONTEXT OF BOOTSTRAP. TILL THAT
176-
# IS DESIGNED AND FINALIZED, THE FOLLOWING COMMITTER/ORDERER
177-
# DEFINITIONS HAVE TO SERVE AS THE MEANS TO DRIVE A SIMPLE
178-
# SKELETON.
179-
#
180-
# All "chaincode" commands from CLI (except "query") will
181-
# send response from the endorser to the Committer defined below.
182-
committer:
183-
enabled: true
184-
ledger:
185-
# orderer to talk to
186-
orderer: 0.0.0.0:7050
187-
188174
# TLS Settings for p2p communications
189175
tls:
190176
enabled: false

0 commit comments

Comments
 (0)