dcapwell commented on code in PR #111: URL: https://github.com/apache/cassandra-easy-stress/pull/111#discussion_r4354240945
########## src/main/kotlin/org/apache/cassandra/easystress/PopulateLoop.kt: ########## @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.easystress + +import com.datastax.oss.driver.api.core.cql.AsyncResultSet +import com.datastax.oss.driver.api.core.cql.BatchStatement +import com.datastax.oss.driver.api.core.cql.BatchType +import com.datastax.oss.driver.api.core.cql.BoundStatement +import com.datastax.oss.driver.api.core.cql.Statement +import com.datastax.oss.driver.api.core.type.DataTypes +import com.google.common.base.Throwables +import org.apache.cassandra.easystress.PopulateRetryPolicy.Companion.POPULATE_PROFILE +import org.apache.cassandra.easystress.workloads.IStressRunner +import org.apache.cassandra.easystress.workloads.Operation +import org.apache.logging.log4j.kotlin.logger +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CompletionStage +import java.util.concurrent.Semaphore +import java.util.concurrent.ThreadLocalRandom +import java.util.concurrent.TimeUnit + +/** + * How one runner thread populates. + * + * @property rows rows to write, deletes included + * @property batchSize rows written to a partition in one batch + * @property deleteRatio chance, from 0 to 1, that a write is a delete + * @property concurrency writes allowed in flight at once + */ +data class PopulateSettings( + val rows: Long, + val batchSize: Int, + val deleteRatio: Double, + val concurrency: Int, +) + +/** + * Writes the populate rows for one runner thread. + * + * The main phase pushes work through [RequestQueue] on a schedule. That suits a load test, but the + * queue drops work when it is full, and populate has to write every row. So this loop pulls the + * next row only when a write slot is free. A semaphore holds one permit per write in flight, and a + * permit comes back only when the driver is done with the write, retries included. + * + * @param send executes a statement; the seam tests use in place of a session + * @param collect records each operation's result for the collectors + */ +class PopulateLoop( + private val runner: IStressRunner, + private val keys: Iterator<PartitionKey>, + private val settings: PopulateSettings, + private val metrics: Metrics, + private val send: (Statement<*>) -> CompletionStage<AsyncResultSet>, + private val collect: (Operation, Either<AsyncResultSet, Throwable>, Long, Long) -> Unit, +) { + companion object { + val log = logger() + } + + /** + * Writes [PopulateSettings.rows] rows and returns once every write has completed. + */ + fun run() { + val permits = Semaphore(settings.concurrency) + var queued = 0L + + while (queued < settings.rows) { + permits.acquire() + val operations = nextOperations(keys.next(), settings.rows - queued) + queued += operations.size + + val startNanos = System.nanoTime() + sendSafely(toStatement(operations)).whenComplete { result, error -> + try { + onComplete(operations, result, error, startNanos) + } finally { + permits.release() + } + } + } + + // Every write holds its permit until it completes, so holding all of them means none are in flight. + permits.acquire(settings.concurrency) + permits.release(settings.concurrency) + } + + private fun nextOperations( + key: PartitionKey, + remaining: Long, + ): List<Operation> { + // A delete goes alone. In a batch it shares the batch timestamp, and a conditional delete + // would make the whole batch conditional, so a failed condition would drop the rows with it. + if (settings.deleteRatio > 0.0 && ThreadLocalRandom.current().nextDouble() < settings.deleteRatio) { + return listOf(runner.getNextDelete(key)) + } + val count = minOf(settings.batchSize.toLong(), remaining).toInt() + return List(count) { runner.getNextPopulate(key) } + } + + private fun toStatement(operations: List<Operation>): Statement<*> { + val bound = operations.map { requireNotNull(it.bound) { "Populate only supports bound statements, got $it" } } + + // Populate statements are idempotent so the driver hands their write timeouts to PopulateRetryPolicy. + return if (bound.size == 1) { + bound.first().setExecutionProfileName(POPULATE_PROFILE).setIdempotent(true) + } else { + BatchStatement + .newInstance(batchType(bound), bound) + .setExecutionProfileName(POPULATE_PROFILE) + .setIdempotent(true) + } + } + + /** + * Cassandra only accepts counter updates in a counter batch. + */ + private fun batchType(bound: List<BoundStatement>): BatchType { Review Comment: `CountersWide` test has ```sql UPDATE counter_wide SET value = value + 1 WHERE key = ? and cluster = ? ``` so the bind variable isn't the counter, so this returns unlogged and not counter -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
