diff --git a/consensus/consensus.go b/consensus/consensus.go index c0b99f4b..7098984e 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -5,6 +5,7 @@ import "time" type Log struct { Id []byte `bson:"_id"` Records []Record `bson:"records"` + Err error } type Record struct { diff --git a/consensus/consensusclient/stream.go b/consensus/consensusclient/stream.go index 9c18d07a..d2f1a055 100644 --- a/consensus/consensusclient/stream.go +++ b/consensus/consensusclient/stream.go @@ -9,6 +9,7 @@ type Stream interface { WatchIds(logIds [][]byte) (err error) UnwatchIds(logIds [][]byte) (err error) WaitLogs() []*consensusproto.Log + Err() error Close() error } @@ -24,6 +25,7 @@ func runStream(rpcStream consensusproto.DRPCConsensus_WatchLogClient) Stream { type stream struct { rpcStream consensusproto.DRPCConsensus_WatchLogClient mb *mb.MB[*consensusproto.Log] + err error } func (s *stream) WatchIds(logIds [][]byte) (err error) { @@ -42,11 +44,16 @@ func (s *stream) WaitLogs() []*consensusproto.Log { return s.mb.Wait() } +func (s *stream) Err() error { + return s.err +} + func (s *stream) readStream() { defer s.Close() for { event, err := s.rpcStream.Recv() if err != nil { + s.err = err return } if err = s.mb.Add(&consensusproto.Log{ diff --git a/consensus/consensusproto/consensus.pb.go b/consensus/consensusproto/consensus.pb.go index a9aa1bb3..be7fc173 100644 --- a/consensus/consensusproto/consensus.pb.go +++ b/consensus/consensusproto/consensus.pb.go @@ -363,6 +363,7 @@ func (m *WatchLogRequest) GetUnwatchIds() [][]byte { type WatchLogEvent struct { LogId []byte `protobuf:"bytes,1,opt,name=logId,proto3" json:"logId,omitempty"` Records []*Record `protobuf:"bytes,2,rep,name=records,proto3" json:"records,omitempty"` + Error *Err `protobuf:"bytes,3,opt,name=error,proto3" json:"error,omitempty"` } func (m *WatchLogEvent) Reset() { *m = WatchLogEvent{} } @@ -412,6 +413,57 @@ func (m *WatchLogEvent) GetRecords() []*Record { return nil } +func (m *WatchLogEvent) GetError() *Err { + if m != nil { + return m.Error + } + return nil +} + +type Err struct { + Error ErrCodes `protobuf:"varint,1,opt,name=error,proto3,enum=anyConsensus.ErrCodes" json:"error,omitempty"` +} + +func (m *Err) Reset() { *m = Err{} } +func (m *Err) String() string { return proto.CompactTextString(m) } +func (*Err) ProtoMessage() {} +func (*Err) Descriptor() ([]byte, []int) { + return fileDescriptor_6b92aaf7feaf5a54, []int{7} +} +func (m *Err) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Err) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Err.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Err) XXX_Merge(src proto.Message) { + xxx_messageInfo_Err.Merge(m, src) +} +func (m *Err) XXX_Size() int { + return m.Size() +} +func (m *Err) XXX_DiscardUnknown() { + xxx_messageInfo_Err.DiscardUnknown(m) +} + +var xxx_messageInfo_Err proto.InternalMessageInfo + +func (m *Err) GetError() ErrCodes { + if m != nil { + return m.Error + } + return ErrCodes_Unexpected +} + func init() { proto.RegisterEnum("anyConsensus.ErrCodes", ErrCodes_name, ErrCodes_value) proto.RegisterType((*Log)(nil), "anyConsensus.Log") @@ -421,6 +473,7 @@ func init() { proto.RegisterType((*AddRecordRequest)(nil), "anyConsensus.AddRecordRequest") proto.RegisterType((*WatchLogRequest)(nil), "anyConsensus.WatchLogRequest") proto.RegisterType((*WatchLogEvent)(nil), "anyConsensus.WatchLogEvent") + proto.RegisterType((*Err)(nil), "anyConsensus.Err") } func init() { @@ -428,37 +481,39 @@ func init() { } var fileDescriptor_6b92aaf7feaf5a54 = []byte{ - // 475 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x53, 0x4d, 0x8b, 0xd3, 0x50, - 0x14, 0xed, 0x4b, 0xc6, 0x4e, 0x7b, 0xd3, 0x8f, 0x78, 0x19, 0x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, 0x9e, 0x59, 0x25, 0x67, 0xe7, 0xf8, 0x12, 0x9a, 0x65, - 0x4c, 0x68, 0x5f, 0xd3, 0x56, 0xf2, 0xbb, 0x41, 0xfe, 0x06, 0x1a, 0xc5, 0xf9, 0xf0, 0x7e, 0x95, - 0xfd, 0x27, 0xa5, 0xde, 0xe9, 0xcd, 0xb4, 0xba, 0xba, 0x47, 0x9e, 0x92, 0x57, 0x8f, 0x7e, 0x6e, - 0x6c, 0x72, 0xb5, 0xb1, 0xc9, 0x9f, 0x8d, 0x4d, 0xbe, 0x6f, 0xed, 0xda, 0xd5, 0xd6, 0xae, 0xfd, - 0xde, 0xda, 0xb5, 0x8f, 0xdd, 0x41, 0xf5, 0x97, 0xfb, 0x5c, 0x57, 0xaf, 0x67, 0x7f, 0x03, 0x00, - 0x00, 0xff, 0xff, 0x40, 0xd0, 0xc6, 0x8e, 0x8b, 0x03, 0x00, 0x00, + // 509 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x53, 0xcd, 0x8a, 0xd3, 0x50, + 0x14, 0xee, 0x4d, 0x3a, 0x9d, 0xf6, 0xf4, 0x2f, 0x1e, 0x86, 0x21, 0x74, 0x30, 0x94, 0x88, 0x58, + 0x65, 0x68, 0xa5, 0x23, 0xb8, 0x72, 0x31, 0x96, 0x0a, 0x95, 0x6a, 0x21, 0x30, 0x0a, 0xae, 0xac, + 0xb9, 0xb7, 0x31, 0x4c, 0xc8, 0xad, 0xf7, 0xa6, 0x63, 0x67, 0xe3, 0x33, 0xf8, 0x20, 0x3e, 0x88, + 0x3b, 0x67, 0xe9, 0x52, 0xda, 0x17, 0x91, 0xdc, 0xfc, 0xd0, 0xd8, 0x99, 0x85, 0x9b, 0x96, 0xf3, + 0x7d, 0xe7, 0xe7, 0xbb, 0xe7, 0x7c, 0x81, 0x87, 0x2e, 0x0f, 0x25, 0x0b, 0xe5, 0x4a, 0x2e,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e, 0x79, 0x4a, 0x5e, 0x3e, + 0xfe, 0xb9, 0xb1, 0xc8, 0xcd, 0xc6, 0x22, 0x7f, 0x36, 0x16, 0xf9, 0xbe, 0xb5, 0x4a, 0x37, 0x5b, + 0xab, 0xf4, 0x7b, 0x6b, 0x95, 0x3e, 0xb4, 0x07, 0xc5, 0x0f, 0xfa, 0x53, 0x45, 0xfd, 0x9d, 0xfd, + 0x0d, 0x00, 0x00, 0xff, 0xff, 0xaa, 0x39, 0x8f, 0xb0, 0xe9, 0x03, 0x00, 0x00, } func (m *Log) Marshal() (dAtA []byte, err error) { @@ -715,6 +770,18 @@ func (m *WatchLogEvent) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.Error != nil { + { + size, err := m.Error.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintConsensus(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } if len(m.Records) > 0 { for iNdEx := len(m.Records) - 1; iNdEx >= 0; iNdEx-- { { @@ -739,6 +806,34 @@ func (m *WatchLogEvent) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *Err) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Err) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Err) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Error != 0 { + i = encodeVarintConsensus(dAtA, i, uint64(m.Error)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + func encodeVarintConsensus(dAtA []byte, offset int, v uint64) int { offset -= sovConsensus(v) base := offset @@ -869,6 +964,22 @@ func (m *WatchLogEvent) Size() (n int) { n += 1 + l + sovConsensus(uint64(l)) } } + if m.Error != nil { + l = m.Error.Size() + n += 1 + l + sovConsensus(uint64(l)) + } + return n +} + +func (m *Err) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Error != 0 { + n += 1 + sovConsensus(uint64(m.Error)) + } return n } @@ -1634,6 +1745,111 @@ func (m *WatchLogEvent) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Error", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConsensus + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthConsensus + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthConsensus + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Error == nil { + m.Error = &Err{} + } + if err := m.Error.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipConsensus(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthConsensus + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *Err) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConsensus + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Err: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Err: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Error", wireType) + } + m.Error = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConsensus + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Error |= ErrCodes(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipConsensus(dAtA[iNdEx:]) diff --git a/consensus/consensusproto/protos/consensus.proto b/consensus/consensusproto/protos/consensus.proto index e15dac05..17a1bce0 100644 --- a/consensus/consensusproto/protos/consensus.proto +++ b/consensus/consensusproto/protos/consensus.proto @@ -52,4 +52,9 @@ message WatchLogRequest { message WatchLogEvent { bytes logId = 1; repeated Record records = 2; + Err error = 3; } + +message Err { + ErrCodes error = 1; +} \ No newline at end of file diff --git a/consensus/consensusrpc/consensrpc.go b/consensus/consensusrpc/consensrpc.go index f1cafe1e..bee34062 100644 --- a/consensus/consensusrpc/consensrpc.go +++ b/consensus/consensusrpc/consensrpc.go @@ -6,8 +6,10 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto/consensuserr" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/db" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/stream" + "storj.io/drpc/drpcerr" "time" ) @@ -57,11 +59,26 @@ func (c *consensusRpc) WatchLog(rpcStream consensusproto.DRPCConsensus_WatchLogS return rpcStream.Close() } for _, rec := range recs { - if err := rpcStream.Send(&consensusproto.WatchLogEvent{ - LogId: rec.Id, - Records: recordsToProto(rec.Records), - }); err != nil { - return err + if rec.Err == nil { + if err := rpcStream.Send(&consensusproto.WatchLogEvent{ + LogId: rec.Id, + Records: recordsToProto(rec.Records), + }); err != nil { + return err + } + } else { + errCode := consensusproto.ErrCodes(drpcerr.Code(rec.Err)) + if errCode == 0 { + errCode = consensusproto.ErrCodes(drpcerr.Code(consensuserr.ErrUnexpected)) + } + if err := rpcStream.Send(&consensusproto.WatchLogEvent{ + LogId: rec.Id, + Error: &consensusproto.Err{ + Error: errCode, + }, + }); err != nil { + return err + } } } } diff --git a/consensus/stream/service.go b/consensus/stream/service.go index ff4f597b..807f34d7 100644 --- a/consensus/stream/service.go +++ b/consensus/stream/service.go @@ -5,7 +5,7 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/metric" - ocache2 "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/ocache" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/db" "github.com/cheggaaa/mb/v2" @@ -42,21 +42,22 @@ type Service interface { type service struct { db db.Service - cache ocache2.OCache + cache ocache.OCache lastStreamId uint64 } func (s *service) Init(a *app.App) (err error) { s.db = a.MustComponent(db.CName).(db.Service) - cacheOpts := []ocache2.Option{ - ocache2.WithTTL(cacheTTL), - ocache2.WithLogger(log.Named("cache").Sugar()), + cacheOpts := []ocache.Option{ + ocache.WithTTL(cacheTTL), + ocache.WithRefCounter(false), + ocache.WithLogger(log.Named("cache").Sugar()), } if ms := a.Component(metric.CName); ms != nil { - cacheOpts = append(cacheOpts, ocache2.WithPrometheus(ms.(metric.Metric).Registry(), "consensus", "logcache")) + cacheOpts = append(cacheOpts, ocache.WithPrometheus(ms.(metric.Metric).Registry(), "consensus", "logcache")) } - s.cache = ocache2.New(s.loadLog, cacheOpts...) + s.cache = ocache.New(s.loadLog, cacheOpts...) return s.db.SetChangeReceiver(s.receiveChange) } @@ -98,7 +99,7 @@ func (s *service) RemoveStream(ctx context.Context, logId []byte, streamId uint6 return } -func (s *service) loadLog(ctx context.Context, id string) (value ocache2.Object, err error) { +func (s *service) loadLog(ctx context.Context, id string) (value ocache.Object, err error) { if ctxLog := ctx.Value(ctxLogKey); ctxLog != nil { return &object{ logId: ctxLog.(consensus.Log).Id, diff --git a/consensus/stream/service_test.go b/consensus/stream/service_test.go index 8b3498a9..c804b581 100644 --- a/consensus/stream/service_test.go +++ b/consensus/stream/service_test.go @@ -4,6 +4,7 @@ import ( "context" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus" + "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/consensusproto/consensuserr" "github.com/anytypeio/go-anytype-infrastructure-experiments/consensus/db" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -14,93 +15,111 @@ import ( var ctx = context.Background() func TestService_NewStream(t *testing.T) { - fx := newFixture(t) - defer fx.Finish(t) + t.Run("watch/unwatch", func(t *testing.T) { + fx := newFixture(t) + defer fx.Finish(t) - var expLogId = []byte("logId") - var preloadLogId = []byte("preloadId") + var expLogId = []byte("logId") + var preloadLogId = []byte("preloadId") - fx.mockDB.fetchLog = func(ctx context.Context, logId []byte) (log consensus.Log, err error) { - require.Equal(t, expLogId, logId) - return consensus.Log{ - Id: logId, - Records: []consensus.Record{ - { - Id: []byte{'1'}, + fx.mockDB.fetchLog = func(ctx context.Context, logId []byte) (log consensus.Log, err error) { + require.Equal(t, expLogId, logId) + return consensus.Log{ + Id: logId, + Records: []consensus.Record{ + { + Id: []byte{'1'}, + }, }, - }, - }, nil - } - - fx.mockDB.receiver(preloadLogId, []consensus.Record{ - { - Id: []byte{'2'}, - PrevId: []byte{'1'}, - }, - { - Id: []byte{'1'}, - }, - }) - - st1 := fx.NewStream() - sr1 := readStream(st1) - assert.Equal(t, uint64(1), sr1.id) - st1.WatchIds(ctx, [][]byte{expLogId, preloadLogId}) - st1.UnwatchIds(ctx, [][]byte{preloadLogId}) - assert.Equal(t, [][]byte{expLogId}, st1.LogIds()) - - st2 := fx.NewStream() - sr2 := readStream(st2) - assert.Equal(t, uint64(2), sr2.id) - st2.WatchIds(ctx, [][]byte{expLogId, preloadLogId}) - - fx.mockDB.receiver(expLogId, []consensus.Record{ - { - Id: []byte{'1'}, - }, - }) - fx.mockDB.receiver(expLogId, []consensus.Record{ - { - Id: []byte{'2'}, - PrevId: []byte{'1'}, - }, - { - Id: []byte{'1'}, - }, - }) - fx.mockDB.receiver(preloadLogId, []consensus.Record{ - { - Id: []byte{'3'}, - PrevId: []byte{'4'}, - }, - { - Id: []byte{'2'}, - PrevId: []byte{'1'}, - }, - { - Id: []byte{'1'}, - }, - }) - st1.Close() - st2.Close() - - for _, sr := range []*streamReader{sr1, sr2} { - select { - case <-time.After(time.Second / 3): - require.False(t, true, "timeout") - case <-sr.finished: + }, nil } - } - require.Len(t, sr1.logs, 2) - assert.Len(t, sr1.logs[string(expLogId)].Records, 2) - assert.Equal(t, []byte{'2'}, sr1.logs[string(expLogId)].Records[0].Id) - assert.Equal(t, []byte{'2'}, sr1.logs[string(preloadLogId)].Records[0].Id) + fx.mockDB.receiver(preloadLogId, []consensus.Record{ + { + Id: []byte{'2'}, + PrevId: []byte{'1'}, + }, + { + Id: []byte{'1'}, + }, + }) - require.Len(t, sr2.logs, 2) - assert.Len(t, sr2.logs[string(expLogId)].Records, 2) - assert.Equal(t, []byte{'2'}, sr2.logs[string(expLogId)].Records[0].Id) - assert.Equal(t, []byte{'3'}, sr2.logs[string(preloadLogId)].Records[0].Id) + st1 := fx.NewStream() + sr1 := readStream(st1) + assert.Equal(t, uint64(1), sr1.id) + st1.WatchIds(ctx, [][]byte{expLogId, preloadLogId}) + st1.UnwatchIds(ctx, [][]byte{preloadLogId}) + assert.Equal(t, [][]byte{expLogId}, st1.LogIds()) + + st2 := fx.NewStream() + sr2 := readStream(st2) + assert.Equal(t, uint64(2), sr2.id) + st2.WatchIds(ctx, [][]byte{expLogId, preloadLogId}) + + fx.mockDB.receiver(expLogId, []consensus.Record{ + { + Id: []byte{'1'}, + }, + }) + fx.mockDB.receiver(expLogId, []consensus.Record{ + { + Id: []byte{'2'}, + PrevId: []byte{'1'}, + }, + { + Id: []byte{'1'}, + }, + }) + fx.mockDB.receiver(preloadLogId, []consensus.Record{ + { + Id: []byte{'3'}, + PrevId: []byte{'4'}, + }, + { + Id: []byte{'2'}, + PrevId: []byte{'1'}, + }, + { + Id: []byte{'1'}, + }, + }) + st1.Close() + st2.Close() + + for _, sr := range []*streamReader{sr1, sr2} { + select { + case <-time.After(time.Second / 3): + require.False(t, true, "timeout") + case <-sr.finished: + } + } + + require.Len(t, sr1.logs, 2) + assert.Len(t, sr1.logs[string(expLogId)].Records, 2) + assert.Equal(t, []byte{'2'}, sr1.logs[string(expLogId)].Records[0].Id) + assert.Equal(t, []byte{'2'}, sr1.logs[string(preloadLogId)].Records[0].Id) + + require.Len(t, sr2.logs, 2) + assert.Len(t, sr2.logs[string(expLogId)].Records, 2) + assert.Equal(t, []byte{'2'}, sr2.logs[string(expLogId)].Records[0].Id) + assert.Equal(t, []byte{'3'}, sr2.logs[string(preloadLogId)].Records[0].Id) + }) + t.Run("error", func(t *testing.T) { + fx := newFixture(t) + defer fx.Finish(t) + fx.mockDB.fetchLog = func(ctx context.Context, logId []byte) (log consensus.Log, err error) { + return log, consensuserr.ErrLogNotFound + } + st1 := fx.NewStream() + sr1 := readStream(st1) + id := []byte("nonExists") + assert.Equal(t, uint64(1), sr1.id) + st1.WatchIds(ctx, [][]byte{id}) + st1.Close() + <-sr1.finished + require.Len(t, sr1.logs, 1) + assert.Equal(t, consensuserr.ErrLogNotFound, sr1.logs[string(id)].Err) + }) } func newFixture(t *testing.T) *fixture { diff --git a/consensus/stream/stream.go b/consensus/stream/stream.go index 0c686290..83684677 100644 --- a/consensus/stream/stream.go +++ b/consensus/stream/stream.go @@ -48,7 +48,11 @@ func (s *Stream) WatchIds(ctx context.Context, logIds [][]byte) { if _, ok := s.logIds[logIdKey]; !ok { s.logIds[logIdKey] = struct{}{} if addErr := s.s.AddStream(ctx, logId, s); addErr != nil { - log.Warn("can't add stream for log", zap.Binary("logId", logId), zap.Error(addErr)) + log.Info("can't add stream for log", zap.Binary("logId", logId), zap.Error(addErr)) + _ = s.mb.Add(consensus.Log{ + Id: logId, + Err: addErr, + }) } } }