|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | + |
| 23 | +package persistence |
| 24 | + |
| 25 | +import ( |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/golang/mock/gomock" |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | + "go.temporal.io/server/common/log" |
| 32 | + "go.temporal.io/server/common/membership" |
| 33 | + "go.temporal.io/server/common/metrics" |
| 34 | + "go.temporal.io/server/common/metrics/metricstest" |
| 35 | + "go.temporal.io/server/service/history/tasks" |
| 36 | +) |
| 37 | + |
| 38 | +func TestDLQMetricsEmitter_EmitMetrics_WhenInstanceHostsShardOne(t *testing.T) { |
| 39 | + ctrl := gomock.NewController(t) |
| 40 | + defer ctrl.Finish() |
| 41 | + |
| 42 | + metricsHandler := metricstest.NewCaptureHandler() |
| 43 | + logger := log.NewMockLogger(ctrl) |
| 44 | + manager := NewMockHistoryTaskQueueManager(ctrl) |
| 45 | + manager.EXPECT().ListQueues(gomock.Any(), &ListQueuesRequest{ |
| 46 | + QueueType: QueueTypeHistoryDLQ, |
| 47 | + PageSize: 100, |
| 48 | + NextPageToken: nil, |
| 49 | + }).Return(&ListQueuesResponse{ |
| 50 | + Queues: []QueueInfo{ |
| 51 | + {QueueName: GetHistoryTaskQueueName(tasks.CategoryIDTransfer, "source", "target"), MessageCount: 1}, |
| 52 | + {QueueName: GetHistoryTaskQueueName(tasks.CategoryIDTimer, "source", "target"), MessageCount: 2}, |
| 53 | + {QueueName: GetHistoryTaskQueueName(tasks.CategoryIDReplication, "source", "target"), MessageCount: 3}, |
| 54 | + }, |
| 55 | + NextPageToken: []byte("test_page_token"), |
| 56 | + }, nil).Times(1) |
| 57 | + manager.EXPECT().ListQueues(gomock.Any(), &ListQueuesRequest{ |
| 58 | + QueueType: QueueTypeHistoryDLQ, |
| 59 | + PageSize: 100, |
| 60 | + NextPageToken: []byte("test_page_token"), |
| 61 | + }).Return(&ListQueuesResponse{ |
| 62 | + Queues: []QueueInfo{ |
| 63 | + {QueueName: GetHistoryTaskQueueName(tasks.CategoryIDTransfer, "source", "target"), MessageCount: 8}, |
| 64 | + {QueueName: GetHistoryTaskQueueName(tasks.CategoryIDTimer, "source", "target"), MessageCount: 9}, |
| 65 | + {QueueName: GetHistoryTaskQueueName(tasks.CategoryIDReplication, "source", "target"), MessageCount: 10}, |
| 66 | + }, |
| 67 | + NextPageToken: nil, |
| 68 | + }, nil).Times(1) |
| 69 | + resolver := membership.NewMockServiceResolver(ctrl) |
| 70 | + resolver.EXPECT().Lookup("1").Return(membership.NewHostInfoFromAddress("testAddress"), nil).Times(1) |
| 71 | + hostInfoProvider := membership.NewMockHostInfoProvider(ctrl) |
| 72 | + hostInfoProvider.EXPECT().HostInfo().Return(membership.NewHostInfoFromAddress("testAddress")).Times(1) |
| 73 | + categoryRegistry := tasks.NewDefaultTaskCategoryRegistry() |
| 74 | + emitter := NewDLQMetricsEmitter(metricsHandler, logger, manager, resolver, hostInfoProvider, categoryRegistry) |
| 75 | + emitter.emitMetricsTimer = time.NewTicker(60 * time.Millisecond) |
| 76 | + logger.EXPECT().Info(gomock.Any()).AnyTimes() |
| 77 | + logger.EXPECT().Error(gomock.Any()).AnyTimes() |
| 78 | + |
| 79 | + capture := metricsHandler.StartCapture() |
| 80 | + snapshot := capture.Snapshot() |
| 81 | + emitter.Start() |
| 82 | + assert.Eventually(t, func() bool { |
| 83 | + snapshot = capture.Snapshot() |
| 84 | + return len(snapshot[metrics.DLQMessageCount.Name()]) == len(categoryRegistry.GetCategories()) |
| 85 | + }, 5*time.Second, 100*time.Millisecond) |
| 86 | + |
| 87 | + emitter.Stop() |
| 88 | + <-emitter.shutdownCh |
| 89 | + |
| 90 | + messageCount := make(map[string]float64) |
| 91 | + for _, recording := range snapshot[metrics.DLQMessageCount.Name()] { |
| 92 | + value, ok := recording.Value.(float64) |
| 93 | + assert.True(t, ok) |
| 94 | + messageCount[recording.Tags[metrics.TaskCategoryTagName]] = value |
| 95 | + } |
| 96 | + assert.Equal(t, float64(9), messageCount["transfer"]) |
| 97 | + assert.Equal(t, float64(11), messageCount["timer"]) |
| 98 | + assert.Equal(t, float64(13), messageCount["replication"]) |
| 99 | +} |
| 100 | + |
| 101 | +func TestDLQMetricsEmitter_DoesNotEmitMetrics_WhenInstanceDoesNotHostShardOne(t *testing.T) { |
| 102 | + ctrl := gomock.NewController(t) |
| 103 | + defer ctrl.Finish() |
| 104 | + |
| 105 | + metricsHandler := metricstest.NewCaptureHandler() |
| 106 | + capture := metricsHandler.StartCapture() |
| 107 | + logger := log.NewMockLogger(ctrl) |
| 108 | + manager := NewMockHistoryTaskQueueManager(ctrl) |
| 109 | + |
| 110 | + resolver := membership.NewMockServiceResolver(ctrl) |
| 111 | + resolver.EXPECT().Lookup("1").Return(membership.NewHostInfoFromAddress("testAddress1"), nil).MinTimes(1) |
| 112 | + hostInfoProvider := membership.NewMockHostInfoProvider(ctrl) |
| 113 | + hostInfoProvider.EXPECT().HostInfo().Return(membership.NewHostInfoFromAddress("testAddress2")).MinTimes(1) |
| 114 | + |
| 115 | + emitter := NewDLQMetricsEmitter(metricsHandler, logger, manager, resolver, hostInfoProvider, tasks.NewDefaultTaskCategoryRegistry()) |
| 116 | + emitter.emitMetricsTimer = time.NewTicker(time.Millisecond) |
| 117 | + logger.EXPECT().Info(gomock.Any()).AnyTimes() |
| 118 | + logger.EXPECT().Error(gomock.Any()).AnyTimes() |
| 119 | + |
| 120 | + emitter.Start() |
| 121 | + time.Sleep(100 * time.Millisecond) //nolint |
| 122 | + emitter.Stop() |
| 123 | + <-emitter.shutdownCh |
| 124 | + |
| 125 | + snapshot := capture.Snapshot() |
| 126 | + assert.Empty(t, snapshot[metrics.DLQMessageCount.Name()]) |
| 127 | +} |
0 commit comments