dcapwell commented on code in PR #111:
URL: 
https://github.com/apache/cassandra-easy-stress/pull/111#discussion_r4354345205


##########
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()
+                }
+            }

Review Comment:
   inside onComplete:
   
    ```kotlin
      if (op is Operation.Mutation) {
          runner.onSuccess(op, result)
      }
    ```
   
    - With --populate.concurrency writes in flight (default 100), completions 
execute asynchronously on
      Netty/driver worker threads. Multiple completions can fire concurrently 
for the same runner instance.
    - Workloads such as LWT track mutable state inside runner:
      ```kotlin
        val state = mutableMapOf<String, Int>()
        ...
        override fun onSuccess(op: Operation.Mutation, result: AsyncResultSet?) 
{
            state[payload.id] = payload.value
        }
      ```
      state is an unsynchronized HashMap. While nextOperations() reads state on 
the generator thread,
      multiple completion threads concurrently call onSuccess() and mutate 
state.



-- 
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]

Reply via email to