Last active
January 2, 2016 09:19
-
-
Save neilellis/8282537 to your computer and use it in GitHub Desktop.
A little Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.cazcade.snapito.control.impl | |
| import com.cazcade.snapito.control.api.Infrastructure | |
| import com.cazcade.snapito.control.api.MachineState.* | |
| import com.cazcade.snapito.control.api.MachineGroupState.* | |
| import com.cazcade.snapito.control.api.Action.* | |
| import com.cazcade.snapito.control.api.GroupAction.* | |
| import com.cazcade.snapito.control.api.Controller | |
| import com.cazcade.snapito.control.api.MachineGroup | |
| /** | |
| * @author <a href="http://uk.linkedin.com/in/neilellis">Neil Ellis</a> | |
| */ | |
| public fun defaultTranstitions(group: MachineGroup) { | |
| group allowMachine (MACHINE_START to MACHINE_OK); | |
| group allowMachine (MACHINE_OK to MACHINE_STOPPING); | |
| group allowMachine (MACHINE_OK to BROKEN_MACHINE); | |
| group allowMachine (MACHINE_OK to STALE_MACHINE); | |
| group allowMachine (MACHINE_STOPPING to MACHINE_STOPPED); | |
| group allowMachine (MACHINE_STOPPING to MACHINE_START); | |
| group allowMachine (BROKEN_MACHINE to MACHINE_STOPPING); | |
| group allowMachine (STALE_MACHINE to MACHINE_STOPPING); | |
| group allow (UNDERLOADED to OVERLOADED); | |
| group allow (UNDERLOADED to GROUP_OK); | |
| group allow (UNDERLOADED to GROUP_BROKEN); | |
| group allow (OVERLOADED to GROUP_OK); | |
| group allow (OVERLOADED to UNDERLOADED); | |
| group allow (OVERLOADED to GROUP_BROKEN); | |
| group allow (GROUP_OK to GROUP_BROKEN); | |
| group allow (GROUP_OK to UNDERLOADED); | |
| group allow (GROUP_OK to OVERLOADED); | |
| } | |
| public fun snapitoPolicy(infra: Infrastructure, controller: Controller) { | |
| infra.topology().each { | |
| defaultTranstitions(it); | |
| when(it.name()) { | |
| "lb" -> { | |
| val balancers = it; | |
| balancers have BROKEN_MACHINE ask controller to RESTART_MACHINE; | |
| balancers have STALE_MACHINE ask controller to REIMAGE_MACHINE; | |
| } | |
| "gateway" -> { | |
| val gateways = it; | |
| gateways have BROKEN_MACHINE ask controller to RESTART_MACHINE; | |
| gateways have STALE_MACHINE ask controller to REIMAGE_MACHINE; | |
| } | |
| "worker" -> { | |
| val workers = it; | |
| workers have BROKEN_MACHINE ask controller to DESTROY_MACHINE; | |
| workers become OVERLOADED use controller to EXPAND; | |
| workers have STALE_MACHINE ask controller to REIMAGE_MACHINE; | |
| workers become UNDERLOADED use controller to CONTRACT; | |
| } | |
| } | |
| } | |
| } | |
| public fun snapitoStrategy(ctrl: Infrastructure, controller: Controller) { | |
| ctrl.topology().each { | |
| when(it.name()) { | |
| "lb" -> { | |
| val balancers = it; | |
| controller uses { balancers.failover(it).reImage(it).configure().failback(it) } to REIMAGE_MACHINE within balancers; | |
| controller uses { balancers.failover(it).destroy(it) } to DESTROY_MACHINE within balancers; | |
| controller uses { balancers.failover(it).restart(it).failback(it) } to RESTART_MACHINE within balancers; | |
| } | |
| "gateway" -> { | |
| val gateways = it; | |
| controller uses { | |
| gateways.failover(it); | |
| ctrl.topology().group("lb").configure(); | |
| gateways.reImage(it).failback(it); | |
| ctrl.topology().group("lb").configure() | |
| } to REIMAGE_MACHINE within gateways; | |
| controller uses { | |
| gateways.failover(it).destroy(it); | |
| ctrl.topology().group("lb").configure(); | |
| } to DESTROY_MACHINE within gateways; | |
| controller uses { | |
| gateways.failover(it); | |
| ctrl.topology().group("lb").configure(); | |
| gateways.restart(it).failback(it); | |
| ctrl.topology().group("lb").configure() | |
| } to RESTART_MACHINE within gateways; | |
| } | |
| "worker" -> { | |
| val workers = it; | |
| controller uses { workers.failover(it).reImage(it).configure().failback(it) } to REIMAGE_MACHINE within workers; | |
| controller uses { workers.failover(it).destroy(it) } to DESTROY_MACHINE within workers; | |
| controller uses { workers.failover(it).restart(it).failback(it) } to RESTART_MACHINE within workers; | |
| controller will { workers.expand() } to EXPAND the workers ; | |
| controller will { workers.contract() } to CONTRACT the workers; | |
| } | |
| } | |
| }; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment