Skip to content

Commit

Permalink
Backport of perf: Remove expensive reflection from raft/mesh hot path…
Browse files Browse the repository at this point in the history
… into release/1.15.x (#17493)

* backport of commit 04b6a90

* backport of commit 13e4057

---------

Co-authored-by: Lincoln Stoll <me@lds.li>
Co-authored-by: John Murret <john.murret@hashicorp.com>
  • Loading branch information
3 people authored May 26, 2023
1 parent b41a4e7 commit d60e96f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changelog/16552.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
raft: Remove expensive reflection from raft/mesh hot path
```
1 change: 1 addition & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ codegen-tools:
deep-copy: codegen-tools
@$(SHELL) $(CURDIR)/agent/structs/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/proxycfg/deep-copy.sh
@$(SHELL) $(CURDIR)/agent/consul/state/deep-copy.sh

version:
@echo -n "Version: "
Expand Down
19 changes: 2 additions & 17 deletions agent/consul/state/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/hashicorp/go-memdb"
"github.com/mitchellh/copystructure"

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/configentry"
Expand Down Expand Up @@ -4564,14 +4563,7 @@ func updateMeshTopology(tx WriteTxn, idx uint64, node string, svc *structs.NodeS

var mapping *upstreamDownstream
if existing, ok := obj.(*upstreamDownstream); ok {
rawCopy, err := copystructure.Copy(existing)
if err != nil {
return fmt.Errorf("failed to copy existing topology mapping: %v", err)
}
mapping, ok = rawCopy.(*upstreamDownstream)
if !ok {
return fmt.Errorf("unexpected topology type %T", rawCopy)
}
mapping := existing.DeepCopy()
mapping.Refs[uid] = struct{}{}
mapping.ModifyIndex = idx

Expand Down Expand Up @@ -4637,14 +4629,7 @@ func cleanupMeshTopology(tx WriteTxn, idx uint64, service *structs.ServiceNode)

// Do the updates in a separate loop so we don't trash the iterator.
for _, m := range mappings {
rawCopy, err := copystructure.Copy(m)
if err != nil {
return fmt.Errorf("failed to copy existing topology mapping: %v", err)
}
copy, ok := rawCopy.(*upstreamDownstream)
if !ok {
return fmt.Errorf("unexpected topology type %T", rawCopy)
}
copy := m.DeepCopy()

// Bail early if there's no reference to the proxy ID we're deleting
if _, ok := copy.Refs[uid]; !ok {
Expand Down
15 changes: 15 additions & 0 deletions agent/consul/state/catalog_schema.deepcopy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// generated by deep-copy -pointer-receiver -o ./catalog_schema.deepcopy.go -type upstreamDownstream ./; DO NOT EDIT.

package state

// DeepCopy generates a deep copy of *upstreamDownstream
func (o *upstreamDownstream) DeepCopy() *upstreamDownstream {
var cp upstreamDownstream = *o
if o.Refs != nil {
cp.Refs = make(map[string]struct{}, len(o.Refs))
for k2, v2 := range o.Refs {
cp.Refs[k2] = v2
}
}
return &cp
}
11 changes: 11 additions & 0 deletions agent/consul/state/deep-copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

readonly PACKAGE_DIR="$(dirname "${BASH_SOURCE[0]}")"
cd $PACKAGE_DIR

# Uses: https://github.com/globusdigital/deep-copy
deep-copy \
-pointer-receiver \
-o ./catalog_schema.deepcopy.go \
-type upstreamDownstream \
./

0 comments on commit d60e96f

Please sign in to comment.