fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

indexserver: do not bump unknown repos in queue (#210)

queue.Bump contains a bug since queue.get will create an item if it
doesn't exist. This means we would try index empty IndexOptions. We fix
this by directly checking the items map rather than using the get
helper.

+43 -5
+14 -5
cmd/zoekt-sourcegraph-indexserver/queue.go
··· 93 93 // re-insert it with the last known IndexOptions. 94 94 func (q *Queue) Bump(ids []uint32) { 95 95 q.mu.Lock() 96 + defer q.mu.Unlock() 97 + 98 + if q.items == nil { 99 + q.init() 100 + } 101 + 96 102 for _, id := range ids { 97 - item := q.get(id) 98 - if item.heapIdx < 0 { 103 + item, ok := q.items[id] 104 + if ok && item.heapIdx < 0 { 99 105 q.seq++ 100 106 item.seq = q.seq 101 107 heap.Push(&q.pq, item) ··· 103 109 metricQueueCap.Set(float64(len(q.items))) 104 110 } 105 111 } 106 - q.mu.Unlock() 107 112 } 108 113 109 114 // Iterate will call f on each item known to the queue, including items that ··· 185 190 // Note: get requires that q.mu is held. 186 191 func (q *Queue) get(repoID uint32) *queueItem { 187 192 if q.items == nil { 188 - q.items = map[uint32]*queueItem{} 189 - q.pq = make(pqueue, 0) 193 + q.init() 190 194 } 191 195 192 196 item, ok := q.items[repoID] ··· 199 203 } 200 204 201 205 return item 206 + } 207 + 208 + func (q *Queue) init() { 209 + q.items = map[uint32]*queueItem{} 210 + q.pq = make(pqueue, 0) 202 211 } 203 212 204 213 // setIndexedState will set indexedState and update the corresponding metrics
+29
cmd/zoekt-sourcegraph-indexserver/queue_test.go
··· 5 5 "strconv" 6 6 "testing" 7 7 8 + "github.com/google/go-cmp/cmp" 8 9 "github.com/google/zoekt" 9 10 ) 10 11 ··· 85 86 _, ok := queue.Pop() 86 87 if ok { 87 88 t.Fatal("queue should be empty") 89 + } 90 + } 91 + 92 + func TestQueue_Bump(t *testing.T) { 93 + queue := &Queue{} 94 + 95 + queue.AddOrUpdate(IndexOptions{RepoID: 1, Name: "foo"}) 96 + queue.AddOrUpdate(IndexOptions{RepoID: 2, Name: "bar"}) 97 + 98 + // Empty queue 99 + for ok := true; ok; _, ok = queue.Pop() { 100 + } 101 + 102 + // Bump 2 and 3. 3 doesn't exist, so only 2 should exist. 103 + queue.Bump([]uint32{2, 3}) 104 + 105 + want := []IndexOptions{{RepoID: 2, Name: "bar"}} 106 + var got []IndexOptions 107 + for { 108 + opts, ok := queue.Pop() 109 + if !ok { 110 + break 111 + } 112 + got = append(got, opts) 113 + } 114 + 115 + if d := cmp.Diff(want, got); d != "" { 116 + t.Fatalf("(-want, +got):\n%s", d) 88 117 } 89 118 } 90 119