Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync recover #11

Merged
merged 5 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kafka/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (e *Error) string() string {

// NOP does not carry any information. Useful for debugging.
type NOP struct {
Topic string
Partition int32
}

Expand Down
36 changes: 19 additions & 17 deletions mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ func (_m *MockContext) EXPECT() *_MockContextRecorder {
return _m.recorder
}

func (_m *MockContext) Emit(_param0 string, _param1 string, _param2 []byte) error {
ret := _m.ctrl.Call(_m, "Emit", _param0, _param1, _param2)
ret0, _ := ret[0].(error)
return ret0
func (_m *MockContext) Emit(_param0 string, _param1 string, _param2 interface{}) {
_m.ctrl.Call(_m, "Emit", _param0, _param1, _param2)
}

func (_mr *_MockContextRecorder) Emit(arg0, arg1, arg2 interface{}) *gomock.Call {
Expand All @@ -46,11 +44,10 @@ func (_mr *_MockContextRecorder) Fail(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Fail", arg0)
}

func (_m *MockContext) Join(_param0 string) (interface{}, error) {
func (_m *MockContext) Join(_param0 string) interface{} {
ret := _m.ctrl.Call(_m, "Join", _param0)
ret0, _ := ret[0].(interface{})
ret1, _ := ret[1].(error)
return ret0, ret1
return ret0
}

func (_mr *_MockContextRecorder) Join(arg0 interface{}) *gomock.Call {
Expand All @@ -67,20 +64,26 @@ func (_mr *_MockContextRecorder) Key() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Key")
}

func (_m *MockContext) Loopback(_param0 string, _param1 interface{}) error {
ret := _m.ctrl.Call(_m, "Loopback", _param0, _param1)
ret0, _ := ret[0].(error)
func (_m *MockContext) Lookup(_param0 string, _param1 string) interface{} {
ret := _m.ctrl.Call(_m, "Lookup", _param0, _param1)
ret0, _ := ret[0].(interface{})
return ret0
}

func (_mr *_MockContextRecorder) Lookup(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Lookup", arg0, arg1)
}

func (_m *MockContext) Loopback(_param0 string, _param1 interface{}) {
_m.ctrl.Call(_m, "Loopback", _param0, _param1)
}

func (_mr *_MockContextRecorder) Loopback(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Loopback", arg0, arg1)
}

func (_m *MockContext) SetValue(_param0 interface{}) error {
ret := _m.ctrl.Call(_m, "SetValue", _param0)
ret0, _ := ret[0].(error)
return ret0
func (_m *MockContext) SetValue(_param0 interface{}) {
_m.ctrl.Call(_m, "SetValue", _param0)
}

func (_mr *_MockContextRecorder) SetValue(arg0 interface{}) *gomock.Call {
Expand All @@ -97,11 +100,10 @@ func (_mr *_MockContextRecorder) Topic() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Topic")
}

func (_m *MockContext) Value() (interface{}, error) {
func (_m *MockContext) Value() interface{} {
ret := _m.ctrl.Call(_m, "Value")
ret0, _ := ret[0].(interface{})
ret1, _ := ret[1].(error)
return ret0, ret1
return ret0
}

func (_mr *_MockContextRecorder) Value() *gomock.Call {
Expand Down
8 changes: 8 additions & 0 deletions mock/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ func (_m *MockkafkaProxy) AddGroup() {
func (_mr *_MockkafkaProxyRecorder) AddGroup() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "AddGroup")
}

func (_m *MockkafkaProxy) Stop() {
_m.ctrl.Call(_m, "Stop")
}

func (_mr *_MockkafkaProxyRecorder) Stop() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Stop")
}
9 changes: 6 additions & 3 deletions partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type kafkaProxy interface {
Add(string, int64)
Remove(string)
AddGroup()
Stop()
}

type processCallback func(msg *message, st storage.Storage, wg *sync.WaitGroup) error
Expand Down Expand Up @@ -106,6 +107,7 @@ func newPartition(topic string, cb processCallback, st *storageProxy, proxy kafk

func (p *partition) start() error {
defer close(p.done)
defer p.proxy.Stop()
defer p.mxStatus.Update(partitionClosed)

if !p.st.Stateless() {
Expand All @@ -131,6 +133,7 @@ func (p *partition) start() error {

func (p *partition) startCatchup() error {
defer close(p.done)
defer p.proxy.Stop()

err := p.st.Open()
if err != nil {
Expand Down Expand Up @@ -273,14 +276,14 @@ func (p *partition) load(catchup bool) error {

case *kafka.EOF:
p.mxPartitionLoaderHwm.Update(ev.Hwm)
if catchup {
continue
}
if atomic.LoadInt32(&p.readyFlag) == 0 {
log.Println("readyFlag was false when EOF arrived")
p.mxStatus.Update(partitionRecovered)
atomic.StoreInt32(&p.readyFlag, 1)
}
if catchup {
continue
}
return nil

case *kafka.Message:
Expand Down
13 changes: 13 additions & 0 deletions partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestPartition_startStateless(t *testing.T) {
metrics.DefaultRegistry, defaultPartitionChannelSize)

proxy.EXPECT().AddGroup().Do(func() { close(wait) })
proxy.EXPECT().Stop()
go func() {
err := p.start()
ensure.Nil(t, err)
Expand Down Expand Up @@ -91,6 +92,7 @@ func TestPartition_startStateful(t *testing.T) {
st.EXPECT().Sync(),
proxy.EXPECT().Remove(topic),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)
go func() {
err := p.start()
Expand Down Expand Up @@ -131,6 +133,7 @@ func TestPartition_runStateless(t *testing.T) {
p := newPartition(topic, consume, newNullStorageProxy(0), proxy, metrics.DefaultRegistry, defaultPartitionChannelSize)

proxy.EXPECT().AddGroup()
proxy.EXPECT().Stop()
go func() {
err := p.start()
ensure.Nil(t, err)
Expand Down Expand Up @@ -181,6 +184,7 @@ func TestPartition_runStatelessWithError(t *testing.T) {
proxy, metrics.DefaultRegistry, defaultPartitionChannelSize)

proxy.EXPECT().AddGroup()
proxy.EXPECT().Stop()
go func() {
err := p.start()
ensure.NotNil(t, err)
Expand Down Expand Up @@ -209,6 +213,7 @@ func TestPartition_runStatelessWithError(t *testing.T) {
wait = make(chan bool)

proxy.EXPECT().AddGroup()
proxy.EXPECT().Stop()
go func() {
err := p.start()
ensure.NotNil(t, err)
Expand Down Expand Up @@ -261,6 +266,7 @@ func TestPartition_runStateful(t *testing.T) {
st.EXPECT().Sync(),
st.EXPECT().Sync(),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down Expand Up @@ -343,6 +349,7 @@ func TestPartition_runStatefulWithError(t *testing.T) {
st.EXPECT().Sync(),
st.EXPECT().Sync(),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down Expand Up @@ -425,6 +432,7 @@ func TestPartition_loadStateful(t *testing.T) {
st.EXPECT().Sync(),
st.EXPECT().Sync(),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down Expand Up @@ -502,6 +510,7 @@ func TestPartition_loadStatefulWithError(t *testing.T) {
proxy.EXPECT().Add(topic, offset),
proxy.EXPECT().Remove(topic),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down Expand Up @@ -537,6 +546,7 @@ func TestPartition_loadStatefulWithError(t *testing.T) {
st.EXPECT().SetOffset(int64(offset)).Return(errors.New("some error")),
proxy.EXPECT().Remove(topic),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down Expand Up @@ -568,6 +578,7 @@ func TestPartition_loadStatefulWithError(t *testing.T) {
st.EXPECT().Open().Return(nil),
st.EXPECT().GetOffset(int64(-2)).Return(int64(0), errors.New("some error")),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down Expand Up @@ -624,6 +635,7 @@ func TestPartition_catchupStateful(t *testing.T) {
st.EXPECT().Sync(),
proxy.EXPECT().Remove(topic),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down Expand Up @@ -739,6 +751,7 @@ func TestPartition_catchupStatefulWithError(t *testing.T) {
st.EXPECT().Sync(),
proxy.EXPECT().Remove(topic),
st.EXPECT().Close().Return(nil),
proxy.EXPECT().Stop(),
)

go func() {
Expand Down
58 changes: 21 additions & 37 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ func (g *Processor) hash(key string) (int32, error) {
func (g *Processor) Start() error {
// start all views
for t, v := range g.views {
go func() {
go func(t string, v *View) {
err := v.Start()
if err != nil {
g.fail(fmt.Errorf("error in view %s: %v", t, err))
}
}()
}(t, v)
}

topics := make(map[string]int64)
Expand Down Expand Up @@ -379,7 +379,11 @@ func (g *Processor) run() {
}

case *kafka.NOP:
_ = g.pushToPartition(ev.Partition, ev)
if g.graph.joint(ev.Topic) {
_ = g.pushToPartitionView(ev.Topic, ev.Partition, ev)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure we want to ignore the error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOP is only used for testing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can only occur in unit-test. so it's fine.

} else {
_ = g.pushToPartition(ev.Partition, ev)
}

case *kafka.Error:
g.fail(ev.Err)
Expand Down Expand Up @@ -438,38 +442,6 @@ func (g *Processor) Stop() {
// partition management (rebalance)
///////////////////////////////////////////////////////////////////////////////

type proxy struct {
partition int32
consumer kafka.Consumer
}

func (p *proxy) Add(topic string, offset int64) {
p.consumer.AddPartition(topic, p.partition, offset)
}

func (p *proxy) Remove(topic string) {
p.consumer.RemovePartition(topic, p.partition)
}

func (p *proxy) AddGroup() {
p.consumer.AddGroupPartition(p.partition)
}

type storageProxy struct {
storage.Storage
partition int32
stateless bool
update UpdateCallback
}

func (s storageProxy) Update(k string, v []byte) error {
return s.update(s.Storage, s.partition, k, v)
}

func (s storageProxy) Stateless() bool {
return s.stateless
}

func (g *Processor) newStorage(topic string, id int32, codec Codec, update UpdateCallback, reg metrics.Registry) (*storageProxy, error) {
if g.isStateless() {
return &storageProxy{
Expand Down Expand Up @@ -540,7 +512,7 @@ func (g *Processor) createPartition(id int32) error {
if _, has := g.partitions[id]; has {
return nil
}
// TODO(diogo what name to use for stateless processors?
// TODO(diogo) what name to use for stateless processors?
var groupTable string
var groupCodec Codec
if gt := g.graph.GroupTable(); gt != nil {
Expand All @@ -554,9 +526,21 @@ func (g *Processor) createPartition(id int32) error {
if err != nil {
return fmt.Errorf("processor: error creating storage: %v", err)
}

// collect dependencies
var wait []func() bool
if pviews, has := g.partitionViews[id]; has {
for _, p := range pviews {
wait = append(wait, p.ready)
}
}
for _, v := range g.views {
wait = append(wait, v.Ready)
}

g.partitions[id] = newPartition(
groupTable,
g.process, st, &proxy{id, g.consumer},
g.process, st, &delayProxy{partition: id, consumer: g.consumer, wait: wait},
reg,
g.opts.partitionChannelSize,
)
Expand Down
11 changes: 11 additions & 0 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,17 @@ func TestProcessor_StartWithTable(t *testing.T) {
ch <- &kafka.EOF{
Partition: 1,
}
ensure.False(t, p.partitionViews[1][table].ready())
time.Sleep(delayProxyInterval)
ensure.False(t, p.partitionViews[1][table].ready())
ch <- &kafka.EOF{
Topic: table,
Partition: 1,
}
err = syncWith(t, ch)
ensure.Nil(t, err)
time.Sleep(delayProxyInterval)
ensure.True(t, p.partitionViews[1][table].ready())

// 5. process messages in partition 1
ch <- &kafka.Message{
Expand Down
Loading