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

query scheduler: optimize queueBroker.makeQueuePath to remove double slice allocation #9925

Merged

Conversation

francoposa
Copy link
Member

@francoposa francoposa commented Nov 15, 2024

What this PR does

Fixes unneeded allocation seen during recent query-scheduler issues - this line was responsible for 1.3% of all allocations profiled.

This is a minor optimization compared to the main fix, but the extra allocations contribute to main thread CPU usage as well as garbage collection pressure.

Which issue(s) this PR fixes or relates to

Fixes #

Checklist

  • Tests updated.
  • Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • about-versioning.md updated with experimental features.

@francoposa francoposa requested a review from a team as a code owner November 15, 2024 21:50
@francoposa
Copy link
Member Author

francoposa commented Nov 15, 2024

Benchmark Code & Outputs
Benchmark comparison shows >50% speedup, ~50% reduction in memory allocations, and ~50% reduction in CPU usage.

goos: linux
goarch: amd64
pkg: github.com/grafana/mimir/pkg/scheduler/queue/tree
cpu: AMD Ryzen 9 PRO 6950H with Radeon Graphics     
BenchmarkMakeQueuePath
BenchmarkMakeQueuePath/queue_path_func_baselineMakeQueuePath
BenchmarkMakeQueuePath/queue_path_func_baselineMakeQueuePath-16         	   10000	    616530 ns/op
BenchmarkMakeQueuePath/queue_path_func_noAppendMakeQueuePath
BenchmarkMakeQueuePath/queue_path_func_noAppendMakeQueuePath-16         	   10000	    304396 ns/op
PASS
type makeQueuePathFunc func(component, tenant string) QueuePath

func baselineMakeQueuePath(component, tenant string) QueuePath {
	return append([]string{component}, tenant)
}

func noAppendMakeQueuePath(component, tenant string) QueuePath {
	return QueuePath{component, tenant}
}

func BenchmarkMakeQueuePath(b *testing.B) {
	tenantCount := 10000

	tenants := make([]string, tenantCount)
	queueComponents := make([]string, tenantCount)
	for i := range tenantCount {
		tenants[i] = strconv.Itoa(i)
		queueComponents[i] = randAdditionalQueueDimension()
	}

	var testCases = []struct {
		name     string
		pathFunc makeQueuePathFunc
	}{
		{"baselineMakeQueuePath", baselineMakeQueuePath},
		{"noAppendMakeQueuePath", noAppendMakeQueuePath},
	}

	for _, testCase := range testCases {
		b.Run(fmt.Sprintf("queue_path_func_%s", testCase.name), func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				for tenantIdx := range tenantCount {
					_ = testCase.pathFunc(
						queueComponents[tenantIdx], tenants[tenantIdx],
					)
				}
			}
		})
	}
}

const ingesterQueueDimension = "ingester"
const storeGatewayQueueDimension = "store-gateway"
const ingesterAndStoreGatewayQueueDimension = "ingester-and-store-gateway"
const unknownQueueDimension = "unknown"

var secondQueueDimensionOptions = []string{
	ingesterQueueDimension,
	storeGatewayQueueDimension,
	ingesterAndStoreGatewayQueueDimension,
	unknownQueueDimension,
}

func randAdditionalQueueDimension() string {
	maxIdx := len(secondQueueDimensionOptions) - 1

	idx := rand.Intn(maxIdx)
	return secondQueueDimensionOptions[idx]
}

Copy link
Contributor

@56quarters 56quarters left a comment

Choose a reason for hiding this comment

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

Nice find!

@francoposa francoposa merged commit 810a7d9 into main Nov 15, 2024
29 checks passed
@francoposa francoposa deleted the francoposa/query-scheduler-optimization-queue-path-no-append branch November 15, 2024 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants