# SPDX-License-Identifier: Apache-2.0

#
# Run a joined trace for testing adaptivity, switching between a recency-biased R3 Corda trace and
# a frequency-biased LIRS loop trace.
#
# Similar to the adaptivity test for Caffeine: https://github.com/ben-manes/caffeine/wiki/Efficiency
#
# Download LIRS traces from: https://github.com/zhongch4g/LIRS2
#
# Download Corda traces from: https://github.com/ben-manes/caffeine
#
# > cluster-sharding/Test/runMain org.apache.pekko.cluster.sharding.passivation.simulator.Simulator adaptivity-trace
#
#   ╔════════════════════╤═════════╤═══════════╤═════════════╤══════════════╗
#   ║                Run │  Active │  Accesses │ Activations │ Passivations ║
#   ╠════════════════════╪═════════╪═══════════╪═════════════╪══════════════╣
#   ║            OPT 500 │ 41.89 % │ 8,565,548 │   4,977,489 │    4,976,989 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║            LRU 500 │ 15.63 % │ 8,565,548 │   7,226,870 │    7,226,370 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║           SLRU 500 │  7.31 % │ 8,565,548 │   7,939,452 │    7,938,952 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║          S4LRU 500 │ 15.63 % │ 8,565,548 │   7,226,902 │    7,226,402 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║            MRU 500 │  0.03 % │ 8,565,548 │   8,563,041 │    8,562,541 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║            LFU 500 │  0.02 % │ 8,565,548 │   8,564,051 │    8,563,551 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║          LFUDA 500 │ 15.63 % │ 8,565,548 │   7,226,892 │    7,226,392 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║    LRU/FS/SLRU 500 │ 26.14 % │ 8,565,548 │   6,326,555 │    6,326,055 ║
#   ╟────────────────────┼─────────┼───────────┼─────────────┼──────────────╢
#   ║ LRU/FS/SLRU/HC 500 │ 40.74 % │ 8,565,548 │   5,076,280 │    5,075,780 ║
#   ╚════════════════════╧═════════╧═══════════╧═════════════╧══════════════╝
#

corda-traces="corda-traces"
corda-traces=${?CORDA_TRACES}

lirs-traces="lirs-traces"
lirs-traces=${?LIRS_TRACES}

pekko.cluster.sharding {
  passivation.simulator {
    runs = [
      {
        name = "OPT 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = optimal-500
      },
      {
        name = "LRU 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = lru-500
      },
      {
        name = "SLRU 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = slru-500
      },
      {
        name = "S4LRU 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = s4lru-500
      },
      {
        name = "MRU 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = mru-500
      },
      {
        name = "LFU 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = lfu-500
      },
      {
        name = "LFUDA 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = lfuda-500
      },
      {
        name = "LRU/FS/SLRU 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = lru-fs-slru-500
      },
      {
        name = "LRU/FS/SLRU/HC 500"
        shards = 1
        regions = 1
        pattern = changing
        strategy = lru-fs-slru-hc-500
      },
    ]

    changing {
      pattern = joined
      joined = [
        corda-vaultservice,
        lirs-loop,
        lirs-loop,
        lirs-loop,
        corda-vaultservice-large,
        lirs-loop,
        lirs-loop,
        lirs-loop,
        corda-vaultservice,
        lirs-loop,
        lirs-loop,
        lirs-loop,
        corda-vaultservice-large,
      ]
    }

    # recency-biased
    corda-vaultservice {
      pattern = trace
      trace {
        format = corda
        path = ${corda-traces}"/vaultservice.trace"
      }
    }

    # recency-biased
    corda-vaultservice-large {
      pattern = trace
      trace {
        format = corda
        path = ${corda-traces}"/vaultservice-large.trace"
      }
    }

    # frequency-biased
    lirs-loop {
      pattern = trace
      trace {
        format = lirs
        path = ${lirs-traces}"/loop.trace"
      }
    }

    optimal-500 {
      strategy = optimal
      optimal {
        per-region-limit = 500
      }
    }

    lru-500 {
      strategy = least-recently-used
      least-recently-used {
        per-region-limit = 500
      }
    }

    slru-500 {
      strategy = least-recently-used
      least-recently-used {
        per-region-limit = 500
        segmented {
          levels = 2
          proportions = [0.2, 0.8]
        }
      }
    }

    s4lru-500 {
      strategy = least-recently-used
      least-recently-used {
        per-region-limit = 500
        segmented.levels = 4
      }
    }

    mru-500 {
      strategy = most-recently-used
      most-recently-used {
        per-region-limit = 500
      }
    }

    lfu-500 {
      strategy = least-frequently-used
      least-frequently-used {
        per-region-limit = 500
      }
    }

    lfuda-500 {
      strategy = least-frequently-used
      least-frequently-used {
        per-region-limit = 500
        dynamic-aging = on
      }
    }

    lru-fs-slru {
      strategy = composite
      composite {
        admission {
          window.strategy = least-recently-used
          filter = frequency-sketch
          optimizer = none
        }
        main {
          strategy = least-recently-used
          least-recently-used {
            segmented {
              levels = 2
              proportions = [0.2, 0.8]
            }
          }
        }
      }
    }

    lru-fs-slru-500 = ${pekko.cluster.sharding.passivation.simulator.lru-fs-slru} {
      composite.per-region-limit = 500
    }

    lru-fs-slru-hc = ${pekko.cluster.sharding.passivation.simulator.lru-fs-slru} {
      composite.admission.optimizer = hill-climbing
    }

    lru-fs-slru-hc-500 = ${pekko.cluster.sharding.passivation.simulator.lru-fs-slru-hc} {
      composite.per-region-limit = 500
    }
  }
}
