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

feat: optimize route switching when freeze segment #321

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
44 changes: 24 additions & 20 deletions client/internal/vanus/eventlog/name_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ type NameService struct {
tracer *tracing.Tracer
}

func (ns *NameService) LookupWritableSegment(ctx context.Context, logID uint64) (*record.Segment, error) {
func (ns *NameService) LookupWritableSegment(ctx context.Context, logID uint64) ([]*record.Segment, error) {
ctx, span := ns.tracer.Start(ctx, "LookupWritableSegment")
defer span.End()

req := &ctrlpb.GetAppendableSegmentRequest{
EventLogId: logID,
Limited: 1,
Limited: 2,
}

resp, err := ns.client.GetAppendableSegment(ctx, req)
Expand All @@ -75,26 +75,28 @@ func (ns *NameService) LookupWritableSegment(ctx context.Context, logID uint64)
if len(segments) == 0 {
return nil, errors.ErrNotWritable
}
return segments[0], nil
return segments, nil
}

func (ns *NameService) LookupReadableSegments(ctx context.Context, logID uint64) ([]*record.Segment, error) {
ctx, span := ns.tracer.Start(ctx, "LookupReadableSegments")
defer span.End()

req := &ctrlpb.ListSegmentRequest{
EventLogId: logID,
StartOffset: 0,
EndOffset: math.MaxInt64,
Limited: math.MaxInt32,
req := &ctrlpb.GetReadableSegmentRequest{
EventLogId: logID,
Limited: math.MaxInt32,
}

resp, err := ns.client.ListSegment(ctx, req)
resp, err := ns.client.GetReadableSegment(ctx, req)
if err != nil {
return nil, err
}

segments := toSegments(resp.GetSegments())
if len(segments) == 0 {
return nil, errors.ErrNotReadable
}

return segments, nil
}

Expand All @@ -107,9 +109,9 @@ func toSegments(pbs []*metapb.Segment) []*record.Segment {
segment := toSegment(pb)
segments = append(segments, segment)
// only return first working segment
if segment.Writable {
break
}
// if segment.Writable {
// break
// }
}
return segments
}
Expand All @@ -123,13 +125,15 @@ func toSegment(segment *metapb.Segment) *record.Segment {
}
}
return &record.Segment{
ID: segment.GetId(),
StartOffset: segment.GetStartOffsetInLog(),
EndOffset: segment.GetEndOffsetInLog(),
FirstEventBornAt: time.UnixMilli(segment.FirstEventBornAtByUnixMs),
LastEventBornAt: time.UnixMilli(segment.LastEvnetBornAtByUnixMs),
Writable: segment.State == "working", // TODO: writable
Blocks: blocks,
LeaderBlockID: segment.GetLeaderBlockId(),
ID: segment.GetId(),
PreviousSegmentId: segment.GetPreviousSegmentId(),
NextSegmentId: segment.GetNextSegmentId(),
StartOffset: segment.GetStartOffsetInLog(),
EndOffset: segment.GetEndOffsetInLog(),
FirstEventBornAt: time.UnixMilli(segment.FirstEventBornAtByUnixMs),
LastEventBornAt: time.UnixMilli(segment.LastEvnetBornAtByUnixMs),
Writable: segment.State == "working", // TODO: writable
Blocks: blocks,
LeaderBlockID: segment.GetLeaderBlockId(),
}
}
4 changes: 2 additions & 2 deletions client/pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type Eventbus interface {
}

type BusWriter interface {
AppendOne(ctx context.Context, event *ce.Event, opts ...WriteOption) (eid string, err error)
AppendMany(ctx context.Context, events []*ce.Event, opts ...WriteOption) (eid string, err error)
AppendOne(ctx context.Context, event *ce.Event, opts ...WriteOption) (string, error)
AppendMany(ctx context.Context, events []*ce.Event, opts ...WriteOption) ([]string, error)
}

type BusReader interface {
Expand Down
4 changes: 2 additions & 2 deletions client/pkg/api/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 30 additions & 30 deletions client/pkg/eventbus/eventbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package eventbus
import (
// standard libraries.
"context"
"encoding/base64"
"encoding/binary"
stderrors "errors"
"io"
"sync"
Expand All @@ -36,7 +34,7 @@ import (
"github.com/linkall-labs/vanus/client/pkg/api"
"github.com/linkall-labs/vanus/client/pkg/eventlog"
"github.com/linkall-labs/vanus/client/pkg/policy"
vlog "github.com/linkall-labs/vanus/observability/log"
"github.com/linkall-labs/vanus/observability/log"
"github.com/linkall-labs/vanus/pkg/errors"

eb "github.com/linkall-labs/vanus/client/internal/vanus/eventbus"
Expand Down Expand Up @@ -66,7 +64,7 @@ func NewEventbus(cfg *eb.Config) *eventbus {
for {
re, ok := <-ch
if !ok {
vlog.Debug(context.Background(), "eventbus quits writable watcher", map[string]interface{}{
log.Debug(context.Background(), "eventbus quits writable watcher", map[string]interface{}{
"eventbus": bus.cfg.Name,
})
break
Expand All @@ -88,7 +86,7 @@ func NewEventbus(cfg *eb.Config) *eventbus {
for {
re, ok := <-ch
if !ok {
vlog.Debug(context.Background(), "eventbus quits readable watcher", map[string]interface{}{
log.Debug(context.Background(), "eventbus quits readable watcher", map[string]interface{}{
"eventbus": bus.cfg.Name,
})
break
Expand Down Expand Up @@ -187,16 +185,20 @@ func (b *eventbus) GetLog(ctx context.Context, logID uint64, opts ...api.LogOpti
if len(b.readableLogs) == 0 {
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
b.refreshReadableLogs(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

unlock when refresh route info.

}
if log, ok := b.readableLogs[logID]; ok {
return log, nil
b.readableMu.RLock()
defer b.readableMu.RUnlock()
if l, ok := b.readableLogs[logID]; ok {
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
return l, nil
}
return nil, errors.ErrResourceNotFound.WithMessage("eventlog not found")
} else if op.Policy.AccessMode() == api.ReadWrite {
if len(b.writableLogs) == 0 {
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
b.refreshWritableLogs(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

unlock when refresh route info.

}
if log, ok := b.writableLogs[logID]; ok {
return log, nil
b.writableMu.RLock()
defer b.writableMu.RUnlock()
if l, ok := b.writableLogs[logID]; ok {
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
return l, nil
}
return nil, errors.ErrResourceNotFound.WithMessage("eventlog not found")
} else {
Expand All @@ -219,6 +221,8 @@ func (b *eventbus) ListLog(ctx context.Context, opts ...api.LogOption) ([]api.Ev
b.refreshReadableLogs(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

unlock when refresh route info.

}
eventlogs := make([]api.Eventlog, 0)
b.readableMu.RLock()
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
defer b.readableMu.RUnlock()
for _, el := range b.readableLogs {
eventlogs = append(eventlogs, el)
}
Expand All @@ -228,6 +232,8 @@ func (b *eventbus) ListLog(ctx context.Context, opts ...api.LogOption) ([]api.Ev
b.refreshWritableLogs(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

unlock when refresh route info

}
eventlogs := make([]api.Eventlog, 0)
b.writableMu.RLock()
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
defer b.writableMu.RUnlock()
for _, el := range b.writableLogs {
eventlogs = append(eventlogs, el)
}
Expand Down Expand Up @@ -310,8 +316,8 @@ func (b *eventbus) updateWritableLogs(ctx context.Context, re *WritableLogsResul
Endpoints: b.cfg.Endpoints,
ID: logID,
}
log := eventlog.NewEventLog(cfg)
lws[logID] = log
l := eventlog.NewEventLog(cfg)
lws[logID] = l
return true
})
b.setWritableLogs(s, lws)
Expand Down Expand Up @@ -403,8 +409,8 @@ func (b *eventbus) updateReadableLogs(ctx context.Context, re *ReadableLogsResul
Endpoints: b.cfg.Endpoints,
ID: logID,
}
log := eventlog.NewEventLog(cfg)
lws[logID] = log
l := eventlog.NewEventLog(cfg)
lws[logID] = l
return true
})
b.setReadableLogs(s, lws)
Expand Down Expand Up @@ -447,7 +453,7 @@ type busWriter struct {

var _ api.BusWriter = (*busWriter)(nil)

func (w *busWriter) AppendOne(ctx context.Context, event *ce.Event, opts ...api.WriteOption) (eid string, err error) {
func (w *busWriter) AppendOne(ctx context.Context, event *ce.Event, opts ...api.WriteOption) (string, error) {
_ctx, span := w.tracer.Start(ctx, "AppendOne")
defer span.End()

Expand All @@ -466,23 +472,17 @@ func (w *busWriter) AppendOne(ctx context.Context, event *ce.Event, opts ...api.
}

// 2. append the event to the eventlog
off, err := lw.Append(_ctx, event)
eid, err := lw.Append(_ctx, event)
if err != nil {
return "", err
}

// 3. generate event ID
var buf [16]byte
binary.BigEndian.PutUint64(buf[0:8], lw.Log().ID())
binary.BigEndian.PutUint64(buf[8:16], uint64(off))
encoded := base64.StdEncoding.EncodeToString(buf[:])

return encoded, nil
return eid, nil
}

func (w *busWriter) AppendMany(ctx context.Context, events []*ce.Event, opts ...api.WriteOption) (eid string, err error) {
func (w *busWriter) AppendMany(ctx context.Context, events []*ce.Event, opts ...api.WriteOption) ([]string, error) {
// TODO(jiangkai): implement this method, by jiangkai, 2022.10.24
return "", nil
return nil, nil
}

func (w *busWriter) Bus() api.Eventbus {
Expand All @@ -493,17 +493,17 @@ func (w *busWriter) pickWritableLog(ctx context.Context, opts *api.WriteOptions)
_ctx, span := w.tracer.Start(ctx, "pickWritableLog")
defer span.End()

log, err := opts.Policy.NextLog(ctx)
l, err := opts.Policy.NextLog(ctx)
if err != nil {
return nil, err
}

l := w.ebus.getWritableLog(_ctx, log.ID())
if l == nil {
lw := w.ebus.getWritableLog(_ctx, l.ID())
if lw == nil {
return nil, stderrors.New("can not pick writable log")
}

return l.Writer(), nil
return lw.Writer(), nil
}

type busReader struct {
Expand Down Expand Up @@ -554,11 +554,11 @@ func (r *busReader) pickReadableLog(ctx context.Context, opts *api.ReadOptions)
_ctx, span := r.tracer.Start(ctx, "pickReadableLog")
defer span.End()

log, err := opts.Policy.NextLog(ctx)
l, err := opts.Policy.NextLog(ctx)
if err != nil {
return nil, err
}
lr := r.ebus.getReadableLog(_ctx, log.ID())
lr := r.ebus.getReadableLog(_ctx, l.ID())
if lr == nil {
return nil, stderrors.New("can not pick readable log")
}
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/eventlog/eventlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type LogWriter interface {

Close(ctx context.Context)

Append(ctx context.Context, event *ce.Event) (off int64, err error)
Append(ctx context.Context, event *ce.Event) (eid string, err error)
}

type LogReader interface {
Expand Down
Loading