Skip to content

Commit 73c501c

Browse files
author
Jason Yellick
committed
[FAB-798] Abstract out the solo broadcast handler
As a first step of consolidating the common logic of the atomicbroadcast api between components, this changeset pulls out the logic which is not solo specific and moves it into the common/broadcast package. This begins, but does not satisfy FAB-798. Change-Id: I084ba83832c6986c5f5fb64b5f2cd16d4ab2ff68 Signed-off-by: Jason Yellick <[email protected]>
1 parent 77c7323 commit 73c501c

File tree

5 files changed

+400
-190
lines changed

5 files changed

+400
-190
lines changed

orderer/common/broadcast/broadcast.go

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package broadcast
18+
19+
import (
20+
"github.com/hyperledger/fabric/orderer/common/broadcastfilter"
21+
"github.com/hyperledger/fabric/orderer/common/configtx"
22+
cb "github.com/hyperledger/fabric/protos/common"
23+
ab "github.com/hyperledger/fabric/protos/orderer"
24+
25+
"github.com/op/go-logging"
26+
)
27+
28+
var logger = logging.MustGetLogger("orderer/common/broadcast")
29+
30+
func init() {
31+
logging.SetLevel(logging.DEBUG, "")
32+
}
33+
34+
// Target defines an interface which the broadcast handler will direct broadcasts to
35+
type Target interface {
36+
// Enqueue accepts a message and returns true on acceptance, or false on shutdown
37+
Enqueue(env *cb.Envelope) bool
38+
}
39+
40+
// Handler defines an interface which handles broadcasts
41+
type Handler interface {
42+
// Handle starts a service thread for a given gRPC connection and services the broadcast connection
43+
Handle(srv ab.AtomicBroadcast_BroadcastServer) error
44+
}
45+
46+
type handlerImpl struct {
47+
queueSize int
48+
target Target
49+
filters *broadcastfilter.RuleSet
50+
configManager configtx.Manager
51+
exitChan chan struct{}
52+
}
53+
54+
// NewHandlerImpl constructs a new implementation of the Handler interface
55+
func NewHandlerImpl(queueSize int, target Target, filters *broadcastfilter.RuleSet, configManager configtx.Manager) Handler {
56+
return &handlerImpl{
57+
queueSize: queueSize,
58+
filters: filters,
59+
configManager: configManager,
60+
target: target,
61+
exitChan: make(chan struct{}),
62+
}
63+
}
64+
65+
// Handle starts a service thread for a given gRPC connection and services the broadcast connection
66+
func (bh *handlerImpl) Handle(srv ab.AtomicBroadcast_BroadcastServer) error {
67+
b := newBroadcaster(bh)
68+
defer close(b.queue)
69+
go b.drainQueue()
70+
return b.queueEnvelopes(srv)
71+
}
72+
73+
type broadcaster struct {
74+
bs *handlerImpl
75+
queue chan *cb.Envelope
76+
}
77+
78+
func newBroadcaster(bs *handlerImpl) *broadcaster {
79+
b := &broadcaster{
80+
bs: bs,
81+
queue: make(chan *cb.Envelope, bs.queueSize),
82+
}
83+
return b
84+
}
85+
86+
func (b *broadcaster) drainQueue() {
87+
for {
88+
select {
89+
case msg, ok := <-b.queue:
90+
if ok {
91+
if !b.bs.target.Enqueue(msg) {
92+
return
93+
}
94+
} else {
95+
return
96+
}
97+
case <-b.bs.exitChan:
98+
return
99+
}
100+
}
101+
}
102+
103+
func (b *broadcaster) queueEnvelopes(srv ab.AtomicBroadcast_BroadcastServer) error {
104+
105+
for {
106+
msg, err := srv.Recv()
107+
if err != nil {
108+
return err
109+
}
110+
111+
action, _ := b.bs.filters.Apply(msg)
112+
113+
switch action {
114+
case broadcastfilter.Reconfigure:
115+
fallthrough
116+
case broadcastfilter.Accept:
117+
select {
118+
case b.queue <- msg:
119+
err = srv.Send(&ab.BroadcastResponse{Status: cb.Status_SUCCESS})
120+
default:
121+
err = srv.Send(&ab.BroadcastResponse{Status: cb.Status_SERVICE_UNAVAILABLE})
122+
}
123+
case broadcastfilter.Forward:
124+
fallthrough
125+
case broadcastfilter.Reject:
126+
err = srv.Send(&ab.BroadcastResponse{Status: cb.Status_BAD_REQUEST})
127+
default:
128+
logger.Fatalf("Unknown filter action :%v", action)
129+
}
130+
131+
if err != nil {
132+
return err
133+
}
134+
}
135+
}
+237
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package broadcast
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"testing"
23+
24+
"google.golang.org/grpc"
25+
26+
"github.com/golang/protobuf/proto"
27+
"github.com/hyperledger/fabric/orderer/common/broadcastfilter"
28+
"github.com/hyperledger/fabric/orderer/common/configtx"
29+
cb "github.com/hyperledger/fabric/protos/common"
30+
ab "github.com/hyperledger/fabric/protos/orderer"
31+
)
32+
33+
var configTx []byte
34+
35+
func init() {
36+
var err error
37+
configTx, err = proto.Marshal(&cb.ConfigurationEnvelope{})
38+
if err != nil {
39+
panic("Error marshaling empty config tx")
40+
}
41+
}
42+
43+
type mockConfigManager struct {
44+
validated bool
45+
applied bool
46+
validateErr error
47+
applyErr error
48+
}
49+
50+
func (mcm *mockConfigManager) Validate(configtx *cb.ConfigurationEnvelope) error {
51+
mcm.validated = true
52+
return mcm.validateErr
53+
}
54+
55+
func (mcm *mockConfigManager) Apply(message *cb.ConfigurationEnvelope) error {
56+
mcm.applied = true
57+
return mcm.applyErr
58+
}
59+
60+
type mockConfigFilter struct {
61+
manager configtx.Manager
62+
}
63+
64+
func (mcf *mockConfigFilter) Apply(msg *cb.Envelope) broadcastfilter.Action {
65+
if bytes.Equal(msg.Payload, configTx) {
66+
if mcf.manager == nil || mcf.manager.Validate(nil) != nil {
67+
return broadcastfilter.Reject
68+
}
69+
return broadcastfilter.Reconfigure
70+
}
71+
return broadcastfilter.Forward
72+
}
73+
74+
type mockTarget struct {
75+
queue chan *cb.Envelope
76+
done bool
77+
}
78+
79+
func (mt *mockTarget) Enqueue(env *cb.Envelope) bool {
80+
mt.queue <- env
81+
return !mt.done
82+
}
83+
84+
func (mt *mockTarget) halt() {
85+
mt.done = true
86+
select {
87+
case <-mt.queue:
88+
default:
89+
}
90+
}
91+
92+
type mockB struct {
93+
grpc.ServerStream
94+
recvChan chan *cb.Envelope
95+
sendChan chan *ab.BroadcastResponse
96+
}
97+
98+
func newMockB() *mockB {
99+
return &mockB{
100+
recvChan: make(chan *cb.Envelope),
101+
sendChan: make(chan *ab.BroadcastResponse),
102+
}
103+
}
104+
105+
func (m *mockB) Send(br *ab.BroadcastResponse) error {
106+
m.sendChan <- br
107+
return nil
108+
}
109+
110+
func (m *mockB) Recv() (*cb.Envelope, error) {
111+
msg, ok := <-m.recvChan
112+
if !ok {
113+
return msg, fmt.Errorf("Channel closed")
114+
}
115+
return msg, nil
116+
}
117+
118+
func getFiltersConfigMockTarget() (*broadcastfilter.RuleSet, *mockConfigManager, *mockTarget) {
119+
cm := &mockConfigManager{}
120+
filters := broadcastfilter.NewRuleSet([]broadcastfilter.Rule{
121+
broadcastfilter.EmptyRejectRule,
122+
&mockConfigFilter{cm},
123+
broadcastfilter.AcceptRule,
124+
})
125+
mt := &mockTarget{queue: make(chan *cb.Envelope)}
126+
return filters, cm, mt
127+
128+
}
129+
130+
func TestQueueOverflow(t *testing.T) {
131+
filters, cm, mt := getFiltersConfigMockTarget()
132+
defer mt.halt()
133+
bh := NewHandlerImpl(2, mt, filters, cm)
134+
m := newMockB()
135+
defer close(m.recvChan)
136+
b := newBroadcaster(bh.(*handlerImpl))
137+
go b.queueEnvelopes(m)
138+
139+
for i := 0; i < 2; i++ {
140+
m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")}
141+
reply := <-m.sendChan
142+
if reply.Status != cb.Status_SUCCESS {
143+
t.Fatalf("Should have successfully queued the message")
144+
}
145+
}
146+
147+
m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")}
148+
reply := <-m.sendChan
149+
if reply.Status != cb.Status_SERVICE_UNAVAILABLE {
150+
t.Fatalf("Should not have successfully queued the message")
151+
}
152+
153+
}
154+
155+
func TestMultiQueueOverflow(t *testing.T) {
156+
filters, cm, mt := getFiltersConfigMockTarget()
157+
defer mt.halt()
158+
bh := NewHandlerImpl(2, mt, filters, cm)
159+
ms := []*mockB{newMockB(), newMockB(), newMockB()}
160+
161+
for _, m := range ms {
162+
defer close(m.recvChan)
163+
b := newBroadcaster(bh.(*handlerImpl))
164+
go b.queueEnvelopes(m)
165+
}
166+
167+
for _, m := range ms {
168+
for i := 0; i < 2; i++ {
169+
m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")}
170+
reply := <-m.sendChan
171+
if reply.Status != cb.Status_SUCCESS {
172+
t.Fatalf("Should have successfully queued the message")
173+
}
174+
}
175+
}
176+
177+
for _, m := range ms {
178+
m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")}
179+
reply := <-m.sendChan
180+
if reply.Status != cb.Status_SERVICE_UNAVAILABLE {
181+
t.Fatalf("Should not have successfully queued the message")
182+
}
183+
}
184+
}
185+
186+
func TestEmptyEnvelope(t *testing.T) {
187+
filters, cm, mt := getFiltersConfigMockTarget()
188+
defer mt.halt()
189+
bh := NewHandlerImpl(2, mt, filters, cm)
190+
m := newMockB()
191+
defer close(m.recvChan)
192+
go bh.Handle(m)
193+
194+
m.recvChan <- &cb.Envelope{}
195+
reply := <-m.sendChan
196+
if reply.Status != cb.Status_BAD_REQUEST {
197+
t.Fatalf("Should have rejected the null message")
198+
}
199+
200+
}
201+
202+
func TestReconfigureAccept(t *testing.T) {
203+
filters, cm, mt := getFiltersConfigMockTarget()
204+
defer mt.halt()
205+
bh := NewHandlerImpl(2, mt, filters, cm)
206+
m := newMockB()
207+
defer close(m.recvChan)
208+
go bh.Handle(m)
209+
210+
m.recvChan <- &cb.Envelope{Payload: configTx}
211+
212+
reply := <-m.sendChan
213+
if reply.Status != cb.Status_SUCCESS {
214+
t.Fatalf("Should have successfully queued the message")
215+
}
216+
217+
if !cm.validated {
218+
t.Errorf("ConfigTx should have been validated before processing")
219+
}
220+
}
221+
222+
func TestReconfigureReject(t *testing.T) {
223+
filters, cm, mt := getFiltersConfigMockTarget()
224+
cm.validateErr = fmt.Errorf("Fail to validate")
225+
defer mt.halt()
226+
bh := NewHandlerImpl(2, mt, filters, cm)
227+
m := newMockB()
228+
defer close(m.recvChan)
229+
go bh.Handle(m)
230+
231+
m.recvChan <- &cb.Envelope{Payload: configTx}
232+
233+
reply := <-m.sendChan
234+
if reply.Status != cb.Status_BAD_REQUEST {
235+
t.Fatalf("Should have failed to queue the message because it was invalid config")
236+
}
237+
}

0 commit comments

Comments
 (0)