Skip to content

Commit 242f879

Browse files
authored
[System benchmark] Start service in SetUp process to update placeholders in config (#3138)
In order to be able to update the placeholders like Hostname and Port with the expected values in system benchmarks, the service must be started in the SetUp method so it can update the variables in the configuration using those placeholders accordingly. This PR ensures that the service is setup when the configuration is read and therefore policy variables are also set.
1 parent c9dcd6b commit 242f879

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

‎internal/benchrunner/runners/system/runner.go‎

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type runner struct {
5858
mcollector *collector
5959
corporaFile string
6060

61+
service servicedeployer.DeployedService
62+
6163
// Execution order of following handlers is defined in runner.TearDown() method.
6264
deletePolicyHandler func(context.Context) error
6365
resetAgentPolicyHandler func(context.Context) error
@@ -152,7 +154,26 @@ func (r *runner) setUp(ctx context.Context) error {
152154
}
153155
r.svcInfo.OutputDir = outputDir
154156

155-
scenario, err := readConfig(r.options.BenchPath, r.options.BenchName, r.svcInfo)
157+
serviceName, err := r.serviceDefinedInConfig()
158+
if err != nil {
159+
return fmt.Errorf("failed to determine if service is defined in config: %w", err)
160+
}
161+
162+
if serviceName != "" {
163+
// Just in the case service deployer is needed (input_service field), setup the service now so all the
164+
// required information is available in r.svcInfo (e.g. hostname, port, etc).
165+
// This info may be needed to render the variables in the configuration.
166+
s, err := r.setupService(ctx, serviceName)
167+
if errors.Is(err, os.ErrNotExist) {
168+
logger.Debugf("No service deployer defined for this benchmark")
169+
} else if err != nil {
170+
return err
171+
}
172+
r.service = s
173+
}
174+
175+
// Read the configuration again to have any possible service-related variable rendered.
176+
scenario, err := readConfig(r.options.BenchPath, r.options.BenchName, &r.svcInfo)
156177
if err != nil {
157178
return err
158179
}
@@ -242,18 +263,22 @@ func (r *runner) setUp(ctx context.Context) error {
242263
return nil
243264
}
244265

245-
func (r *runner) run(ctx context.Context) (report reporters.Reportable, err error) {
246-
var service servicedeployer.DeployedService
247-
if r.scenario.Corpora.InputService != nil {
248-
s, err := r.setupService(ctx)
249-
if errors.Is(err, os.ErrNotExist) {
250-
logger.Debugf("No service deployer defined for this benchmark")
251-
} else if err != nil {
252-
return nil, err
253-
}
254-
service = s
266+
func (r *runner) serviceDefinedInConfig() (string, error) {
267+
// Read of the configuration to know if a service deployer is needed.
268+
// No need to render any template at this point.
269+
scenario, err := readRawConfig(r.options.BenchPath, r.options.BenchName)
270+
if err != nil {
271+
return "", err
255272
}
256273

274+
if scenario.Corpora.InputService == nil {
275+
return "", nil
276+
}
277+
278+
return scenario.Corpora.InputService.Name, nil
279+
}
280+
281+
func (r *runner) run(ctx context.Context) (report reporters.Reportable, err error) {
257282
r.startMetricsColletion(ctx)
258283
defer r.mcollector.stop()
259284

@@ -271,8 +296,8 @@ func (r *runner) run(ctx context.Context) (report reporters.Reportable, err erro
271296
}
272297

273298
// Signal to the service that the agent is ready (policy is assigned).
274-
if service != nil && r.scenario.Corpora.InputService != nil && r.scenario.Corpora.InputService.Signal != "" {
275-
if err = service.Signal(ctx, r.scenario.Corpora.InputService.Signal); err != nil {
299+
if r.service != nil && r.scenario.Corpora.InputService != nil && r.scenario.Corpora.InputService.Signal != "" {
300+
if err = r.service.Signal(ctx, r.scenario.Corpora.InputService.Signal); err != nil {
276301
return nil, fmt.Errorf("failed to notify benchmark service: %w", err)
277302
}
278303
}
@@ -297,7 +322,7 @@ func (r *runner) run(ctx context.Context) (report reporters.Reportable, err erro
297322
return createReport(r.options.BenchName, r.corporaFile, r.scenario, msum)
298323
}
299324

300-
func (r *runner) setupService(ctx context.Context) (servicedeployer.DeployedService, error) {
325+
func (r *runner) setupService(ctx context.Context, serviceName string) (servicedeployer.DeployedService, error) {
301326
stackVersion, err := r.options.KibanaClient.Version()
302327
if err != nil {
303328
return nil, fmt.Errorf("cannot request Kibana version: %w", err)
@@ -320,7 +345,7 @@ func (r *runner) setupService(ctx context.Context) (servicedeployer.DeployedServ
320345
return nil, fmt.Errorf("could not create service runner: %w", err)
321346
}
322347

323-
r.svcInfo.Name = r.scenario.Corpora.InputService.Name
348+
r.svcInfo.Name = serviceName
324349
service, err := serviceDeployer.SetUp(ctx, r.svcInfo)
325350
if err != nil {
326351
return nil, fmt.Errorf("could not setup service: %w", err)

‎internal/benchrunner/runners/system/scenario.go‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ func defaultConfig() *scenario {
7777
}
7878
}
7979

80-
func readConfig(benchPath string, scenario string, svcInfo servicedeployer.ServiceInfo) (*scenario, error) {
80+
// readRawConfig reads the configuration without applying any template
81+
func readRawConfig(benchPath string, scenario string) (*scenario, error) {
82+
return readConfig(benchPath, scenario, nil)
83+
}
84+
85+
func readConfig(benchPath string, scenario string, svcInfo *servicedeployer.ServiceInfo) (*scenario, error) {
8186
configPath := filepath.Clean(filepath.Join(benchPath, fmt.Sprintf("%s.yml", scenario)))
8287
data, err := os.ReadFile(configPath)
8388
if err != nil {
@@ -87,9 +92,11 @@ func readConfig(benchPath string, scenario string, svcInfo servicedeployer.Servi
8792
return nil, fmt.Errorf("could not load system benchmark configuration file: %s: %w", configPath, err)
8893
}
8994

90-
data, err = applyServiceInfo(data, svcInfo)
91-
if err != nil {
92-
return nil, fmt.Errorf("could not apply context to benchmark configuration file: %s: %w", configPath, err)
95+
if svcInfo != nil {
96+
data, err = applyServiceInfo(data, *svcInfo)
97+
if err != nil {
98+
return nil, fmt.Errorf("could not apply context to benchmark configuration file: %s: %w", configPath, err)
99+
}
93100
}
94101

95102
cfg, err := yaml.NewConfig(data, ucfg.PathSep("."))

0 commit comments

Comments
 (0)