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


##########
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(

Review Comment:
    Severity: Major / Should Fix
    Location: 
src/main/kotlin/org/apache/cassandra/easystress/WorkloadRunner.kt:134, 181,
    src/main/kotlin/org/apache/cassandra/easystress/PopulateLoop.kt:171-173
   
    #### Problem
   
    PopulateLoop.onComplete calls runner.onSuccess(op, result) so workloads 
like LWT can record populated
    keys into their per-runner state map. However, WorkloadRunner.populate() 
creates a local val runner =
    profile.getRunner(context) (line 181) and discards it when populate() 
returns;
    WorkloadRunner.executeOperations() then creates a new val runner = 
profile.getRunner(context) (line 134)
    with an empty state map.
   
    For LWT:
    1. During populate, LWT inserts rows with INSERT INTO lwt (id, value) 
VALUES (?, 0) IF NOT EXISTS and
       records state[id] = 0 on the populate runner instance.
    2. In the main phase, the new runner instance has an empty state map, so 
getNextMutation assumes the keys
       do not exist and issues INSERT ... IF NOT EXISTS again instead of UPDATE 
... IF value = ?.
    3. Additionally, in LWT.kt, val state = mutableMapOf<String, Int>() is a 
non-thread-safe LinkedHashMap:
       PopulateLoop reads state on the populate-N thread (getNextPopulate) 
while onComplete writes to state
       (onSuccess) concurrently from multiple Netty I/O threads when 
populate.concurrency > 1.
   
    Similarly, for CreateDrop, calling profile.getRunner(context) in both 
populate() and executeOperations()
    starts two separate non-daemon DDL Timer threads per runner thread.
   
    #### Recommendation
   
    Reuse a single IStressRunner instance per WorkloadRunner across populate() 
and executeOperations() (and
    ensure LWT's state map uses ConcurrentHashMap since onSuccess is invoked on 
driver I/O threads).



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