Skip to content

Commit 12487c8

Browse files
purnesh42Heaswarscoxley
authored
Cherry pick #7571 and #7579 to v1.66.x release branch (#7616)
* estats: remove dependency on testing package (#7579) * grpc: fix regression by freeing request bufferslice after processing unary (#7571) --------- Co-authored-by: Easwar Swaminathan <[email protected]> Co-authored-by: Codey Oxley <[email protected]>
1 parent 7185cf4 commit 12487c8

File tree

7 files changed

+32
-19
lines changed

7 files changed

+32
-19
lines changed

experimental/stats/metricregistry.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package stats
2020

2121
import (
2222
"maps"
23-
"testing"
2423

2524
"google.golang.org/grpc/grpclog"
2625
"google.golang.org/grpc/internal"
@@ -250,9 +249,9 @@ func RegisterInt64Gauge(descriptor MetricDescriptor) *Int64GaugeHandle {
250249
}
251250

252251
// snapshotMetricsRegistryForTesting snapshots the global data of the metrics
253-
// registry. Registers a cleanup function on the provided testing.T that sets
254-
// the metrics registry to its original state. Only called in testing functions.
255-
func snapshotMetricsRegistryForTesting(t *testing.T) {
252+
// registry. Returns a cleanup function that sets the metrics registry to its
253+
// original state.
254+
func snapshotMetricsRegistryForTesting() func() {
256255
oldDefaultMetrics := DefaultMetrics
257256
oldRegisteredMetrics := registeredMetrics
258257
oldMetricsRegistry := metricsRegistry
@@ -262,9 +261,9 @@ func snapshotMetricsRegistryForTesting(t *testing.T) {
262261
maps.Copy(registeredMetrics, registeredMetrics)
263262
maps.Copy(metricsRegistry, metricsRegistry)
264263

265-
t.Cleanup(func() {
264+
return func() {
266265
DefaultMetrics = oldDefaultMetrics
267266
registeredMetrics = oldRegisteredMetrics
268267
metricsRegistry = oldMetricsRegistry
269-
})
268+
}
270269
}

experimental/stats/metricregistry_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func Test(t *testing.T) {
3838
// TestPanic tests that registering two metrics with the same name across any
3939
// type of metric triggers a panic.
4040
func (s) TestPanic(t *testing.T) {
41-
snapshotMetricsRegistryForTesting(t)
41+
cleanup := snapshotMetricsRegistryForTesting()
42+
defer cleanup()
43+
4244
want := "metric simple counter already registered"
4345
defer func() {
4446
if r := recover(); !strings.Contains(fmt.Sprint(r), want) {
@@ -64,7 +66,9 @@ func (s) TestPanic(t *testing.T) {
6466
// this tests the interactions between the metrics recorder and the metrics
6567
// registry.
6668
func (s) TestMetricRegistry(t *testing.T) {
67-
snapshotMetricsRegistryForTesting(t)
69+
cleanup := snapshotMetricsRegistryForTesting()
70+
defer cleanup()
71+
6872
intCountHandle1 := RegisterInt64Count(MetricDescriptor{
6973
Name: "simple counter",
7074
Description: "sum of all emissions from tests",
@@ -141,7 +145,9 @@ func (s) TestMetricRegistry(t *testing.T) {
141145
// metric registry. A component (simulated by test) should be able to record on
142146
// the different registered int count metrics.
143147
func TestNumerousIntCounts(t *testing.T) {
144-
snapshotMetricsRegistryForTesting(t)
148+
cleanup := snapshotMetricsRegistryForTesting()
149+
defer cleanup()
150+
145151
intCountHandle1 := RegisterInt64Count(MetricDescriptor{
146152
Name: "int counter",
147153
Description: "sum of all emissions from tests",

internal/internal.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,9 @@ var (
217217
SetConnectedAddress any // func(scs *SubConnState, addr resolver.Address)
218218

219219
// SnapshotMetricRegistryForTesting snapshots the global data of the metric
220-
// registry. Registers a cleanup function on the provided testing.T that
221-
// sets the metric registry to its original state. Only called in testing
222-
// functions.
223-
SnapshotMetricRegistryForTesting any // func(t *testing.T)
220+
// registry. Returns a cleanup function that sets the metric registry to its
221+
// original state. Only called in testing functions.
222+
SnapshotMetricRegistryForTesting func() func()
224223

225224
// SetDefaultBufferPoolForTesting updates the default buffer pool, for
226225
// testing purposes.

internal/stats/metrics_recorder_list_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ type recordingLoadBalancer struct {
132132
// test then asserts that the recorded metrics show up on both configured stats
133133
// handlers.
134134
func (s) TestMetricsRecorderList(t *testing.T) {
135-
internal.SnapshotMetricRegistryForTesting.(func(t *testing.T))(t)
135+
cleanup := internal.SnapshotMetricRegistryForTesting()
136+
defer cleanup()
137+
136138
mr := manual.NewBuilderWithScheme("test-metrics-recorder-list")
137139
defer mr.Close()
138140

@@ -212,7 +214,9 @@ func (s) TestMetricsRecorderList(t *testing.T) {
212214
// TestMetricRecorderListPanic tests that the metrics recorder list panics if
213215
// received the wrong number of labels for a particular metric.
214216
func (s) TestMetricRecorderListPanic(t *testing.T) {
215-
internal.SnapshotMetricRegistryForTesting.(func(t *testing.T))(t)
217+
cleanup := internal.SnapshotMetricRegistryForTesting()
218+
defer cleanup()
219+
216220
intCountHandle := estats.RegisterInt64Count(estats.MetricDescriptor{
217221
Name: "simple counter",
218222
Description: "sum of all emissions from tests",

mem/buffer_slice.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ func (s BufferSlice) Materialize() []byte {
9292
}
9393

9494
// MaterializeToBuffer functions like Materialize except that it writes the data
95-
// to a single Buffer pulled from the given BufferPool. As a special case, if the
96-
// input BufferSlice only actually has one Buffer, this function has nothing to
97-
// do and simply returns said Buffer.
95+
// to a single Buffer pulled from the given BufferPool.
96+
//
97+
// As a special case, if the input BufferSlice only actually has one Buffer, this
98+
// function simply increases the refcount before returning said Buffer. Freeing this
99+
// buffer won't release it until the BufferSlice is itself released.
98100
func (s BufferSlice) MaterializeToBuffer(pool BufferPool) Buffer {
99101
if len(s) == 1 {
100102
s[0].Ref()

server.go

+1
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,7 @@ func (s *Server) processUnaryRPC(ctx context.Context, t transport.ServerTranspor
13591359
}
13601360
return err
13611361
}
1362+
defer d.Free()
13621363
if channelz.IsOn() {
13631364
t.IncrMsgRecv()
13641365
}

stats/opentelemetry/metricsregistry_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func newServerStatsHandler(options MetricsOptions) metricsRecorderForTest {
6161
// the expected metrics emissions, which includes default metrics and optional
6262
// label assertions.
6363
func (s) TestMetricsRegistryMetrics(t *testing.T) {
64-
internal.SnapshotMetricRegistryForTesting.(func(t *testing.T))(t)
64+
cleanup := internal.SnapshotMetricRegistryForTesting()
65+
defer cleanup()
66+
6567
intCountHandle1 := estats.RegisterInt64Count(estats.MetricDescriptor{
6668
Name: "int-counter-1",
6769
Description: "Sum of calls from test",

0 commit comments

Comments
 (0)