Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Dynamic Consistency Level

Christopher Bradford edited this page Nov 2, 2018 · 2 revisions

In some cases you may want to change the consistency level dynamically based on a value in the session. This may be accomplished with the doIf(), randomSwitch(), or doSwitch() method on a chain. See the following simulation as an example:

class BranchingStatementSimulation extends BaseCqlSimulation {

  val table_name = "test_table_simple"

  createTestKeyspace
  createTable

  val dseProtocol = dseProtocolBuilder.session(session) //Initialize Gatling DSL with your session

  val insertId = 1
  val insertStr = "one"

  val simpleStatementInsert = s"""INSERT INTO $testKeyspace.$table_name (id, str) VALUES ($insertId, '$insertStr')"""

  val insertCql = cql("Insert_Statement")
    .executeStatement(simpleStatementInsert).check(exhausted is true).check(rowCount is 0)
  val quorumGroup = group("LOCAL_QUORUM"){exec(insertCql.withConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM))}
  val oneGroup = group("LOCAL_ONE"){exec(insertCql.withConsistencyLevel(ConsistencyLevel.LOCAL_ONE))}

  val scn = scenario("Branching Statement")
    .randomSwitch(
       50 -> quorumGroup
       50 -> oneGroup
    )
    .pause(1.seconds)

  setUp(
    scn.inject(constantUsersPerSec(1500) during 1.seconds).protocols(dseProtocol)
  ).assertions(
    global.failedRequests.count.is(0)
  )

  def createTable: ResultSet = {
    val table =
      s"""
      CREATE TABLE IF NOT EXISTS $testKeyspace.$table_name (
      id int,
      str text,
      PRIMARY KEY (id)
    );"""

    session.execute(table)
  }
}

when run the following output indicates successful switching

================================================================================
2018-11-01 09:58:19                                           2s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=1500   KO=0     )
> LOCAL_QUORUM / Insert_Statement                          (OK=769    KO=0     )
> LOCAL_ONE / Insert_Statement                             (OK=731    KO=0     )

---- Branching Statement -------------------------------------------------------
[##########################################################################]100%
          waiting: 0      / active: 0      / done:1500  
================================================================================

Clone this wiki locally