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

[patch] change gateway vald's mutex lock #765

Merged
merged 5 commits into from
Oct 13, 2020
Merged
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
80 changes: 36 additions & 44 deletions pkg/gateway/vald/handler/grpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func (s *server) search(ctx context.Context, cfg *payload.Search_Config,
span.End()
}
}()
maxDist := uint32(math.MaxUint32)
var maxDist uint32
atomic.StoreUint32(&maxDist, math.Float32bits(math.MaxFloat32))
kpango marked this conversation as resolved.
Show resolved Hide resolved
num := int(cfg.GetNum())
res = new(payload.Search_Response)
res.Results = make([]*payload.Object_Distance, 0, s.gateway.GetAgentCount(ctx)*num)
Expand All @@ -147,32 +148,23 @@ func (s *server) search(ctx context.Context, cfg *payload.Search_Config,

eg.Go(safety.RecoverFunc(func() error {
defer cancel()
// cl := new(checkList)
visited := make(map[string]bool, len(res.Results))
mu := sync.RWMutex{}
visited := new(sync.Map)
return s.gateway.BroadCast(ectx, func(ctx context.Context, target string, ac agent.AgentClient, copts ...grpc.CallOption) error {
r, err := f(ctx, ac, copts...)
if err != nil {
log.Debug("ignoring error:", err)
return nil
}
for _, dist := range r.GetResults() {
if dist.GetDistance() > math.Float32frombits(atomic.LoadUint32(&maxDist)) {
if dist.GetDistance() >= math.Float32frombits(atomic.LoadUint32(&maxDist)) {
return nil
}
id := dist.GetId()
mu.Lock()
if !visited[id] {
visited[id] = true
mu.Unlock()
if dist == nil{
continue
}
if _, already := visited.LoadOrStore(dist.GetId(), struct{}{}); !already {
dch <- dist
} else {
mu.Unlock()
}
// if !cl.Exists(id) {
// dch <- dist
// cl.Check(id)
// }
}
return nil
})
Expand Down Expand Up @@ -217,44 +209,44 @@ func (s *server) search(ctx context.Context, cfg *payload.Search_Config,
}
return res, nil
case dist := <-dch:
if len(res.GetResults()) >= num {
if dist.GetDistance() < math.Float32frombits(atomic.LoadUint32(&maxDist)) {
atomic.StoreUint32(&maxDist, math.Float32bits(dist.GetDistance()))
} else {
continue
}
pos := len(res.GetResults())
kpango marked this conversation as resolved.
Show resolved Hide resolved
if pos >= num && dist.GetDistance() >= math.Float32frombits(atomic.LoadUint32(&maxDist)) {
continue
}
switch len(res.GetResults()) {
switch pos {
case 0:
res.Results = append(res.Results, dist)
continue
case 1:
if res.GetResults()[0].GetDistance() <= dist.GetDistance() {
res.Results = append(res.Results, dist)
} else {
res.Results = append([]*payload.Object_Distance{dist}, res.Results[0])
}
continue
}

pos := len(res.GetResults())
for idx := pos; idx >= 1; idx-- {
if res.GetResults()[idx-1].GetDistance() <= dist.GetDistance() {
pos = idx - 1
break
default:
for idx := pos; idx >= 1; idx-- {
kpango marked this conversation as resolved.
Show resolved Hide resolved
if res.GetResults()[idx-1].GetDistance() <= dist.GetDistance() {
pos = idx - 1
break
Copy link
Contributor

Choose a reason for hiding this comment

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

[golangci] reported by reviewdog 🐶
break with no blank line before (nlreturn)

}
}
switch {
Copy link
Contributor

Choose a reason for hiding this comment

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

[golangci] reported by reviewdog 🐶
anonymous switch statements should never be cuddled (wsl)

case pos == len(res.GetResults()):
res.Results = append([]*payload.Object_Distance{dist}, res.Results...)
case pos == len(res.GetResults())-1:
res.Results = append(res.GetResults(), dist)
case pos >= 0:
res.Results = append(res.GetResults()[:pos+1], res.GetResults()[pos:]...)
Copy link
Contributor

Choose a reason for hiding this comment

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

[golangci] reported by reviewdog 🐶
appendAssign: append result not assigned to the same slice (gocritic)

res.Results[pos+1] = dist
}
pos = len(res.GetResults())
kpango marked this conversation as resolved.
Show resolved Hide resolved
if pos > num && num != 0 {
res.Results = res.GetResults()[:num]
pos = len(res.GetResults())
}
if distEnd := res.GetResults()[pos].GetDistance(); pos >= num &&
distEnd < math.Float32frombits(atomic.LoadUint32(&maxDist)) {
atomic.StoreUint32(&maxDist, math.Float32bits(distEnd))
}
}
switch {
case pos == len(res.GetResults()):
res.Results = append([]*payload.Object_Distance{dist}, res.Results...)
case pos == len(res.GetResults())-1:
res.Results = append(res.GetResults(), dist)
case pos >= 0:
res.Results = append(res.GetResults()[:pos+1], res.GetResults()[pos:]...)
res.Results[pos+1] = dist
}
if len(res.GetResults()) > num && num != 0 {
res.Results = res.GetResults()[:num]
}
}
}
Expand Down