Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openfaas-faas
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Team Jaz CS 598 CCC Final Project
openfaas-faas
Commits
c211ba0d
Commit
c211ba0d
authored
8 years ago
by
Alex
Browse files
Options
Downloads
Patches
Plain Diff
Cover replica scaling with tests.
parent
52266a67
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
gateway/handlers/alerthandler.go
+27
-22
27 additions, 22 deletions
gateway/handlers/alerthandler.go
gateway/tests/alerthandler_test.go
+47
-0
47 additions, 0 deletions
gateway/tests/alerthandler_test.go
gateway/tests/isalexa_test.go
+0
-27
0 additions, 27 deletions
gateway/tests/isalexa_test.go
with
74 additions
and
49 deletions
gateway/handlers/alerthandler.go
+
27
−
22
View file @
c211ba0d
...
...
@@ -12,38 +12,43 @@ import (
"github.com/docker/docker/client"
)
// CalculateReplicas decides what replica count to set depending on a Prometheus alert
func
CalculateReplicas
(
status
string
,
currentReplicas
uint64
)
uint64
{
newReplicas
:=
currentReplicas
if
status
==
"firing"
{
if
currentReplicas
==
1
{
newReplicas
=
5
}
else
{
if
currentReplicas
+
5
>
20
{
newReplicas
=
20
}
else
{
newReplicas
=
currentReplicas
+
5
}
}
}
else
{
// Resolved event.
newReplicas
=
1
}
return
newReplicas
}
func
scaleService
(
req
requests
.
PrometheusAlert
,
c
*
client
.
Client
)
error
{
var
err
error
//Todo: convert to loop / handler.
serviceName
:=
req
.
Alerts
[
0
]
.
Labels
.
FunctionName
service
,
_
,
inspectErr
:=
c
.
ServiceInspectWithRaw
(
context
.
Background
(),
serviceName
)
if
inspectErr
==
nil
{
var
replicas
uint64
if
req
.
Status
==
"firing"
{
if
*
service
.
Spec
.
Mode
.
Replicated
.
Replicas
<
20
{
replicas
=
*
service
.
Spec
.
Mode
.
Replicated
.
Replicas
+
uint64
(
5
)
}
else
{
return
err
}
}
else
{
// Resolved event.
// Previously decremented by 5, but event only fires once, so set to 1/1.
if
*
service
.
Spec
.
Mode
.
Replicated
.
Replicas
>
1
{
// replicas = *service.Spec.Mode.Replicated.Replicas - uint64(5)
// if replicas < 1 {
// replicas = 1
// }
// return nil
currentReplicas
:=
*
service
.
Spec
.
Mode
.
Replicated
.
Replicas
status
:=
req
.
Status
newReplicas
:=
CalculateReplicas
(
status
,
currentReplicas
)
replicas
=
1
}
else
{
return
nil
}
if
newReplicas
==
currentReplicas
{
return
nil
}
log
.
Printf
(
"Scaling %s to %d replicas.
\n
"
,
serviceName
,
replicas
)
service
.
Spec
.
Mode
.
Replicated
.
Replicas
=
&
replicas
log
.
Printf
(
"Scaling %s to %d replicas.
\n
"
,
serviceName
,
newReplicas
)
service
.
Spec
.
Mode
.
Replicated
.
Replicas
=
&
newReplicas
updateOpts
:=
types
.
ServiceUpdateOptions
{}
updateOpts
.
RegistryAuthFrom
=
types
.
RegistryAuthFromSpec
...
...
This diff is collapsed.
Click to expand it.
gateway/tests/alerthandler_test.go
0 → 100644
+
47
−
0
View file @
c211ba0d
package
tests
import
(
"testing"
"github.com/alexellis/faas/gateway/handlers"
)
func
TestScale1to5
(
t
*
testing
.
T
)
{
newReplicas
:=
handlers
.
CalculateReplicas
(
"firing"
,
1
)
if
newReplicas
!=
5
{
t
.
Log
(
"Expected increment in blocks of 5 from 1 to 5"
)
t
.
Fail
()
}
}
func
TestScale5to10
(
t
*
testing
.
T
)
{
newReplicas
:=
handlers
.
CalculateReplicas
(
"firing"
,
5
)
if
newReplicas
!=
10
{
t
.
Log
(
"Expected increment in blocks of 5 from 5 to 10"
)
t
.
Fail
()
}
}
func
TestScaleCeilingOf20Replicas_Noaction
(
t
*
testing
.
T
)
{
newReplicas
:=
handlers
.
CalculateReplicas
(
"firing"
,
20
)
if
newReplicas
!=
20
{
t
.
Log
(
"Expected ceiling of 20 replicas"
)
t
.
Fail
()
}
}
func
TestScaleCeilingOf20Replicas
(
t
*
testing
.
T
)
{
newReplicas
:=
handlers
.
CalculateReplicas
(
"firing"
,
19
)
if
newReplicas
!=
20
{
t
.
Log
(
"Expected ceiling of 20 replicas"
)
t
.
Fail
()
}
}
func
TestBackingOff10to1
(
t
*
testing
.
T
)
{
newReplicas
:=
handlers
.
CalculateReplicas
(
"resolved"
,
10
)
if
newReplicas
!=
1
{
t
.
Log
(
"Expected backing off to 1 replica"
)
t
.
Fail
()
}
}
This diff is collapsed.
Click to expand it.
gateway/tests/isalexa_test.go
deleted
100644 → 0
+
0
−
27
View file @
52266a67
package
tests
import
(
"testing"
"io/ioutil"
"github.com/alexellis/faas/gateway/handlers"
"github.com/alexellis/faas/gateway/requests"
)
func
TestIsAlexa
(
t
*
testing
.
T
)
{
requestBody
,
_
:=
ioutil
.
ReadFile
(
"./alexhostname_request.json"
)
var
result
requests
.
AlexaRequestBody
result
=
handlers
.
IsAlexa
(
requestBody
)
if
len
(
result
.
Session
.
Application
.
ApplicationId
)
==
0
{
t
.
Fail
()
}
if
len
(
result
.
Session
.
SessionId
)
==
0
{
t
.
Fail
()
}
if
len
(
result
.
Request
.
Intent
.
Name
)
==
0
{
t
.
Fail
()
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment