Skip to content

Commit 1dbec8b

Browse files
committed
fix: address comments
Signed-off-by: Matt Ulmer <[email protected]>
1 parent 5d32d68 commit 1dbec8b

File tree

2 files changed

+55
-52
lines changed

2 files changed

+55
-52
lines changed

pkg/scalers/nsq_scaler.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,6 @@ func (m nsqMetadata) Validate() error {
6666
return fmt.Errorf("no nsqLookupdHTTPAddresses given")
6767
}
6868

69-
if m.Topic == "" {
70-
return fmt.Errorf("no topic given")
71-
}
72-
73-
if m.Channel == "" {
74-
return fmt.Errorf("no channel given")
75-
}
76-
7769
if m.DepthThreshold <= 0 {
7870
return fmt.Errorf("depthThreshold must be a positive integer")
7971
}
@@ -101,7 +93,7 @@ func (s nsqScaler) GetMetricsAndActivity(_ context.Context, metricName string) (
10193
return []external_metrics.ExternalMetricValue{}, false, err
10294
}
10395

104-
s.logger.Info("GetMetricsAndActivity", "metricName", metricName, "depth", depth)
96+
s.logger.V(1).Info("GetMetricsAndActivity", "metricName", metricName, "depth", depth)
10597

10698
metric := GenerateMetricInMili(metricName, float64(depth))
10799

@@ -115,7 +107,7 @@ func (s nsqScaler) getTopicChannelDepth() (int64, error) {
115107
}
116108

117109
if len(nsqdHosts) == 0 {
118-
s.logger.Info("no nsqd hosts found for topic", "topic", s.metadata.Topic)
110+
s.logger.V(1).Info("no nsqd hosts found for topic", "topic", s.metadata.Topic)
119111
return 0, nil
120112
}
121113

@@ -287,7 +279,7 @@ func (s *nsqScaler) aggregateDepth(nsqdHosts []string, topic string, channel str
287279

288280
if len(t.Channels) == 0 {
289281
// topic exists with no channels, but there are messages in the topic -> we should still scale to bootstrap
290-
s.logger.Info("no channels exist for topic", "topic", topic, "channel", channel, "host", result.host)
282+
s.logger.V(1).Info("no channels exist for topic", "topic", topic, "channel", channel, "host", result.host)
291283
depth += t.Depth
292284
continue
293285
}
@@ -301,14 +293,14 @@ func (s *nsqScaler) aggregateDepth(nsqdHosts []string, topic string, channel str
301293
if ch.Paused {
302294
// if it's paused on a single nsqd host, it's depth should not go into the aggregate
303295
// meaning if paused on all nsqd hosts => depth == 0
304-
s.logger.Info("channel is paused", "topic", topic, "channel", channel, "host", result.host)
296+
s.logger.V(1).Info("channel is paused", "topic", topic, "channel", channel, "host", result.host)
305297
continue
306298
}
307299
depth += ch.Depth
308300
}
309301
if !channelExists {
310302
// topic exists with channels, but not the one in question - fallback to topic depth
311-
s.logger.Info("channel does not exist for topic", "topic", topic, "channel", channel, "host", result.host)
303+
s.logger.V(1).Info("channel does not exist for topic", "topic", topic, "channel", channel, "host", result.host)
312304
depth += t.Depth
313305
}
314306
}

tests/scalers/nsq/nsq_test.go

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ const (
2121
)
2222

2323
var (
24-
testNamespace = fmt.Sprintf("%s-ns", testName)
25-
deploymentName = fmt.Sprintf("%s-consumer-deployment", testName)
26-
jobName = fmt.Sprintf("%s-producer-job", testName)
27-
scaledObjectName = fmt.Sprintf("%s-so", testName)
28-
nsqNamespace = "nsq"
29-
nsqHelmRepoURL = "https://nsqio.github.io/helm-chart"
30-
minReplicas = 1
31-
maxReplicas = 10
32-
topicName = "test_topic"
33-
channelName = "test_channel"
24+
testNamespace = fmt.Sprintf("%s-ns", testName)
25+
deploymentName = fmt.Sprintf("%s-consumer-deployment", testName)
26+
jobName = fmt.Sprintf("%s-producer-job", testName)
27+
scaledObjectName = fmt.Sprintf("%s-so", testName)
28+
nsqNamespace = "nsq"
29+
nsqHelmRepoURL = "https://nsqio.github.io/helm-chart"
30+
minReplicaCount = 0
31+
maxReplicaCount = 2
32+
depthThreshold = 10
33+
activationDepthThreshold = 5
34+
topicName = "test_topic"
35+
channelName = "test_channel"
3436
)
3537

3638
const (
@@ -58,6 +60,7 @@ spec:
5860
- "--mode=consumer"
5961
- "--topic={{.TopicName}}"
6062
- "--channel={{.ChannelName}}"
63+
- "--sleep-duration=1s"
6164
- "--nsqlookupd-http-address=nsq-nsqlookupd.{{.NSQNamespace}}.svc.cluster.local:4161"
6265
imagePullPolicy: Always
6366
`
@@ -73,9 +76,8 @@ metadata:
7376
spec:
7477
pollingInterval: 5
7578
cooldownPeriod: 10
76-
idleReplicaCount: 0
77-
maxReplicaCount: {{.MaxReplicas}}
78-
minReplicaCount: {{.MinReplicas}}
79+
maxReplicaCount: {{.MaxReplicaCount}}
80+
minReplicaCount: {{.MinReplicaCount}}
7981
scaleTargetRef:
8082
apiVersion: "apps/v1"
8183
kind: "Deployment"
@@ -87,8 +89,8 @@ spec:
8789
nsqLookupdHTTPAddresses: "nsq-nsqlookupd.{{.NSQNamespace}}.svc.cluster.local:4161"
8890
topic: "{{.TopicName}}"
8991
channel: "{{.ChannelName}}"
90-
depthThreshold: "10"
91-
activationDepthThreshold: "5"
92+
depthThreshold: "{{.DepthThreshold}}"
93+
activationDepthThreshold: "{{.ActivationDepthThreshold}}"
9294
`
9395

9496
jobTemplate = `
@@ -114,16 +116,18 @@ spec:
114116
)
115117

116118
type templateData struct {
117-
TestNamespace string
118-
NSQNamespace string
119-
DeploymentName string
120-
ScaledObjectName string
121-
JobName string
122-
MinReplicas int
123-
MaxReplicas int
124-
TopicName string
125-
ChannelName string
126-
MessageCount int
119+
TestNamespace string
120+
NSQNamespace string
121+
DeploymentName string
122+
ScaledObjectName string
123+
JobName string
124+
MinReplicaCount int
125+
MaxReplicaCount int
126+
DepthThreshold int
127+
ActivationDepthThreshold int
128+
TopicName string
129+
ChannelName string
130+
MessageCount int
127131
}
128132

129133
func TestNSQScaler(t *testing.T) {
@@ -172,15 +176,17 @@ func uninstallNSQ(t *testing.T) {
172176

173177
func getTemplateData() (templateData, []Template) {
174178
return templateData{
175-
TestNamespace: testNamespace,
176-
NSQNamespace: nsqNamespace,
177-
DeploymentName: deploymentName,
178-
JobName: jobName,
179-
ScaledObjectName: scaledObjectName,
180-
MinReplicas: minReplicas,
181-
MaxReplicas: maxReplicas,
182-
TopicName: topicName,
183-
ChannelName: channelName,
179+
TestNamespace: testNamespace,
180+
NSQNamespace: nsqNamespace,
181+
DeploymentName: deploymentName,
182+
JobName: jobName,
183+
ScaledObjectName: scaledObjectName,
184+
MinReplicaCount: minReplicaCount,
185+
MaxReplicaCount: maxReplicaCount,
186+
TopicName: topicName,
187+
ChannelName: channelName,
188+
DepthThreshold: depthThreshold,
189+
ActivationDepthThreshold: activationDepthThreshold,
184190
}, []Template{
185191
{Name: "deploymentTemplate", Config: deploymentTemplate},
186192
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate},
@@ -190,20 +196,25 @@ func getTemplateData() (templateData, []Template) {
190196
func testActivation(t *testing.T, kc *kubernetes.Clientset, data templateData) {
191197
t.Log("--- testing activation ---")
192198

193-
data.MessageCount = 5
199+
data.MessageCount = activationDepthThreshold
194200
KubectlReplaceWithTemplate(t, data, "jobTemplate", jobTemplate)
195-
196201
AssertReplicaCountNotChangeDuringTimePeriod(t, kc, deploymentName, testNamespace, 0, 60)
202+
203+
data.MessageCount = 1 // total message count > activationDepthThreshold
204+
KubectlReplaceWithTemplate(t, data, "jobTemplate", jobTemplate)
205+
require.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1),
206+
"replica count should reach 1 in under 1 minute")
197207
}
198208

199209
func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) {
200210
t.Log("--- testing scale out ---")
201211

202-
data.MessageCount = 1 // 5 already published + 1 > activationDepthThreshold
212+
// can handle depthThreshold messages per replica - using maxReplicaCount + 1 to ensure scaling to maxReplicaCount
213+
data.MessageCount = depthThreshold * (maxReplicaCount + 1)
203214
KubectlReplaceWithTemplate(t, data, "jobTemplate", jobTemplate)
204215

205-
require.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1),
206-
"replica count should be 1 after 1 minute")
216+
require.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 60, 1),
217+
"replica count should reach 2 in under 1 minute")
207218
}
208219

209220
func testScaleIn(t *testing.T, kc *kubernetes.Clientset) {

0 commit comments

Comments
 (0)