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 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
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 @@ -47,13 +47,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 @@ -65,26 +65,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 @@ -97,9 +99,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 @@ -113,13 +115,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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

must return ([]string, error).

}

type BusReader interface {
Expand Down
50 changes: 21 additions & 29 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"
"strings"
Expand All @@ -37,7 +35,7 @@ import (
"github.com/linkall-labs/vanus/client/pkg/errors"
"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"

eb "github.com/linkall-labs/vanus/client/internal/vanus/eventbus"
el "github.com/linkall-labs/vanus/client/internal/vanus/eventlog"
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,16 @@ 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
if l, ok := b.readableLogs[logID]; ok {
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
return l, nil
}
return nil, errors.ErrNotFound
} 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
if l, ok := b.writableLogs[logID]; ok {
ifplusor marked this conversation as resolved.
Show resolved Hide resolved
return l, nil
}
return nil, errors.ErrNotFound
} else {
Expand Down Expand Up @@ -312,8 +310,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 @@ -407,8 +405,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 @@ -451,7 +449,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 @@ -470,21 +468,15 @@ 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
}
Expand All @@ -497,17 +489,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 @@ -558,11 +550,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