# \[AIAB-02] Create the workflow

You can now create a new workflow that you will use in your optimization study.

A workflow in an optimization study is typically composed of the following tasks:

* Apply a new configuration of the selected optimization parameters to the target system: in this example, you will leverage the Akamas *FileConfigurator* operator - this operator can be used to write parameter values into a generic file, which could represent a shell script, an application configuration file, or any other file used to apply parameters to the target systems
* Restart the application (optional): in this example, the Konakart docker container needs to be restarted in order to launch the Konakart JVM for the new configuration to be effectively applied
* Launch the performance test: in this example, the JMeter performance tests are launched as described in a previous section (same as the baseline workflow)

The file `workflow-optimize.yaml` contains the pre-configured workflow, you only need to include the correct references to your environment:

```yaml
name: konakart-optimize

tasks:
  - name: Configure JVM options
    operator: FileConfigurator
    arguments:
      source:
        hostname: target_host
        username: ubuntu
        key: /home/jsmith/.ssh/akamas.key
        path: /home/ubuntu/konakart-docker/konakart/docker-compose.yml.templ
      target:
        hostname: target_host
        username: ubuntu
        key: /home/jsmith/.ssh/akamas.key
        path: /home/ubuntu/konakart-docker/konakart/docker-compose.yml

  - name: Restart konakart
    operator: Executor
    arguments:
      command: "docker stack deploy --compose-file /home/ubuntu/konakart-docker/konakart/docker-compose.yml sut"
      host:
        hostname: target_host
        username: ubuntu
        key: /home/jsmith/.ssh/akamas.key

  - name: Performance test
    operator: Executor
    arguments:
      command: "docker run --net=akamas_lab --rm --name jmeter -i -v /home/ubuntu/konakart-docker/jmeter:/tmp -w /tmp -p 9270:9270 chiabre/jmeter_plugins -t ramp_test_plan.jmx -JTARGET_HOST=target_host -JTHREADS=40 -JRAMP_SEC=300 -JRANDOM_DELAY_MAX_MS=0"
      host:
        hostname: target_host
        username: ubuntu
        key: /home/jsmith/.ssh/akamas.key
```

Please make sure to modify the `workflow-optimize.yaml` file so as to get some variables replaced with the correct references to your environment:

* *hostname* should reference your Konakart instance instead of the placeholder *target\_host*
* *username* and *key* must reflect your Konakart instance user and SSH private key file (also change the path /home/jsmith)
* *path* and *commands* should have the correct file paths to Docker Compose files
* *TARGET\_HOST* in the JMeter command-line variable should reference your Konakart instance instead of the placeholder *target\_host*
* *RAMP\_UP\_TIME* in the JMeter command-line variable should be set to the desired length of the test: you may set this value to 300 seconds (5 minutes) test to make sure everything works correctly, and then change it to 900 seconds (15 minutes), which is more appropriate for optimization purposes

Once you have edited this file, you can then run the following command to create the workflow:

```bash
akamas create workflow workflow-optimize.yaml
```

### Enable parameter configuration

In the workflow, the *FileConfigurator* operator is used to automatically apply the configuration of the JVM parameters at each experiment. In order for this to work, you need to allow Akamas to set the parameter values being tested in each experiment. This is made possible by the following Akamas templating approach:

* locate your application configuration file where the optimization parameters need to be set
* find the place where the parameter that need to be optimized is specified - for example, the heap size of the JVM: `tomcat_jvm_heapsize=1024`
* replace the hardcoded value with the Akamas parameter template string, where you specify both the component name and the name of the Akamas parameter - for example: `tomcat_jvm_heapsize=${jvm.maxHeapSize}`
* at this point, every time the FileConfiguration operator is invoked in your workflow, a new application configuration file will be created where each of the parameter templates replaced with the parameter values being tested by Akamas in the corresponding experiment (e.g. `tomcat_jvm_heapsize=537`).

Therefore, you will now prepare the Konakart configuration file (a Docker Compose file).

First of all, you want to inspect the Konakart configuration file by executing the following command:

```bash
cat konakart-docker/konakart/docker-compose.yml
```

which should return the following output, where you can see that the JAVA\_OPTS variable specifies a maximum heap size of 256 MB:

```yaml
version: "3.8"
services:
  konakart:
    image: chiabre/konakart_jmx_exporter:latest
    environment:
      JAVA_OPTS: "-Xmx1024M"
    deploy:
      resources:
# ...
```

To allow Akamas to apply this hardcoded heap size value (and any other required optimization parameter) at each experiment, you need to prepare a new Konakart Docker Compose file `docker-compose.yml.templ` where you can put the Akamas parameter template.

First, copy the Docker Compose file and rename it so as to keep the original file:

```bash
cd konakart-docker/konakart
cp docker-compose.yml docker-compose.yml.templ
mv docker-compose.yml docker-compose.yml.orig
```

Now, edit the `docker-compose.yml.templ` file and replace the hardcoded value for the JAVA\_OPTS variable with the Akamas parameter template:

```yaml
version: "3.8"
services:
  konakart:
    image: chiabre/konakart_jmx_exporter:latest
    environment:
      JAVA_OPTS: "${jvm.*}"
# ...
```

> aside positive
>
> Notice that instead of specifying one single parameter at a time, Akamas also allows you to put wildcards ('\*') and have all the JVM parameters replaced in place.

Therefore, the FileConfigurator operator in your workflow will expand all the JVM parameters and replace them with the actual values provided by Akamas AI-driven optimization engine.

At this point, you are ready to create your optimization study!
