-
Notifications
You must be signed in to change notification settings - Fork 23
/
routing_policies.go
122 lines (104 loc) · 3.84 KB
/
routing_policies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package fiberapi
import (
"encoding/json"
"github.com/caraml-dev/turing/engines/experiment/runner"
"github.com/caraml-dev/turing/engines/router/missionctl/errors"
"github.com/caraml-dev/turing/engines/router/missionctl/experiment"
)
// ****************************************************************************
// Define configuration keys expected to be passed in for initialization
type routeSelPolicyCfg struct {
DefRoute string `json:"default_route_id,omitempty"`
ExperimentMappings []experimentMapping `json:"experiment_mappings"`
RouteNamePath string `json:"route_name_path"`
}
type expPolicyCfg struct {
ExpEngine string `json:"experiment_engine,omitempty"`
ExpEngineProps map[string]interface{} `json:"experiment_engine_properties,omitempty"`
LivenessPeriodSeconds int `json:"experiment_engine_liveness_period_seconds,omitempty"`
}
// ****************************************************************************
// routeSelectionPolicy captures the properties for gathering the final results
type routeSelectionPolicy struct {
defaultRoute string
experimentMappings []experimentMapping
routeNamePath string
}
// ExperimentMapping specifies the route that should be selected for a particular treatment in an experiment
type experimentMapping struct {
Experiment string // experiment name
Treatment string // treatment name
Route string // route id
}
// MarshalJSON is used to marshal the struct type with private fields into valid json,
// for testing and logging.
func (rsP routeSelectionPolicy) MarshalJSON() ([]byte, error) {
jsonVal, err := json.Marshal(struct {
DefaultRoute string
}{
DefaultRoute: rsP.defaultRoute,
})
if err != nil {
return nil, err
}
return jsonVal, nil
}
// newRouteSelectionPolicy is a creator function for routeSelectionPolicy
func newRouteSelectionPolicy(properties json.RawMessage) (*routeSelectionPolicy, error) {
var routeSelPolicy routeSelPolicyCfg
// Unmarshal the properties
err := json.Unmarshal(properties, &routeSelPolicy)
if err != nil {
return nil, errors.Newf(errors.BadConfig, "Failed to parse route selection policy")
}
// Ensure that if ExperimentMappings and RouteNamePath cannot be both set at the same time
if len(routeSelPolicy.ExperimentMappings) > 0 && routeSelPolicy.RouteNamePath != "" {
return nil, errors.Newf(
errors.BadConfig,
"Experiment mappings and route name path cannot both be configured together",
)
}
return &routeSelectionPolicy{
defaultRoute: routeSelPolicy.DefRoute,
experimentMappings: routeSelPolicy.ExperimentMappings,
routeNamePath: routeSelPolicy.RouteNamePath,
}, nil
}
// experimentationPolicy captures the common properties for Experimenting
type experimentationPolicy struct {
experimentEngine runner.ExperimentRunner
}
// MarshalJSON is used to marshal the struct type with private fields into valid json,
// for testing and logging.
func (exP experimentationPolicy) MarshalJSON() ([]byte, error) {
jsonVal, err := json.Marshal(struct {
ExperimentEngine runner.ExperimentRunner
}{
ExperimentEngine: exP.experimentEngine,
})
if err != nil {
return nil, err
}
return jsonVal, nil
}
// newExperimentationPolicy is a creator function for experimentationPolicy
func newExperimentationPolicy(properties json.RawMessage) (*experimentationPolicy, error) {
var expPolicy expPolicyCfg
// Unmarshal the properties
err := json.Unmarshal(properties, &expPolicy)
if err != nil {
return nil, errors.Newf(errors.BadConfig, "Failed to parse experimentation policy")
}
// Initialize experiment policy
engine, err := experiment.NewExperimentRunner(
expPolicy.ExpEngine,
expPolicy.ExpEngineProps,
expPolicy.LivenessPeriodSeconds,
)
if err != nil {
return nil, err
}
return &experimentationPolicy{
experimentEngine: engine,
}, nil
}