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

fix(event_history) fix a bug in event queue #410

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion server/v2/get_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetHandler(w http.ResponseWriter, req *http.Request, s Server) error {
// Start the watcher on the store.
eventChan, err := s.Store().Watch(key, recursive, sinceIndex)
if err != nil {
return etcdErr.NewError(500, key, s.Store().Index())
return err
}

cn, _ := w.(http.CloseNotifier)
Expand Down
10 changes: 6 additions & 4 deletions store/event_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (eh *EventHistory) scan(key string, recursive bool, index uint64) (*Event,
defer eh.rwl.RUnlock()

// the index should locate after the event history's StartIndex
if index-eh.StartIndex < 0 {
if index < eh.StartIndex {
return nil,
etcdErr.NewError(etcdErr.EcodeEventIndexCleared,
fmt.Sprintf("the requested history has been cleared [%v/%v]",
Expand All @@ -58,7 +58,8 @@ func (eh *EventHistory) scan(key string, recursive bool, index uint64) (*Event,
return nil, nil
}

i := eh.Queue.Front
offset := index - eh.StartIndex
i := (eh.Queue.Front + int(offset)) % eh.Queue.Capacity

for {
e := eh.Queue.Events[i]
Expand All @@ -75,13 +76,13 @@ func (eh *EventHistory) scan(key string, recursive bool, index uint64) (*Event,
ok = ok || strings.HasPrefix(e.Node.Key, key)
}

if ok && index <= e.Index() { // make sure we bypass the smaller one
if ok {
return e, nil
}

i = (i + 1) % eh.Queue.Capacity

if i > eh.Queue.back() {
if i == eh.Queue.Back {
return nil, nil
}
}
Expand All @@ -95,6 +96,7 @@ func (eh *EventHistory) clone() *EventHistory {
Events: make([]*Event, eh.Queue.Capacity),
Size: eh.Queue.Size,
Front: eh.Queue.Front,
Back: eh.Queue.Back,
}

for i, e := range eh.Queue.Events {
Expand Down
13 changes: 4 additions & 9 deletions store/event_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@ type eventQueue struct {
Events []*Event
Size int
Front int
Back int
Capacity int
}

func (eq *eventQueue) back() int {
return (eq.Front + eq.Size - 1 + eq.Capacity) % eq.Capacity
}

func (eq *eventQueue) insert(e *Event) {
index := (eq.back() + 1) % eq.Capacity

eq.Events[index] = e
eq.Events[eq.Back] = e
eq.Back = (eq.Back + 1) % eq.Capacity

if eq.Size == eq.Capacity { //dequeue
eq.Front = (index + 1) % eq.Capacity
eq.Front = (eq.Front + 1) % eq.Capacity
} else {
eq.Size++
}

}