dcapwell commented on code in PR #4556:
URL: https://github.com/apache/cassandra/pull/4556#discussion_r2723172281


##########
src/java/org/apache/cassandra/service/writes/thresholds/CoordinatorWriteWarnings.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.service.writes.thresholds;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.db.ColumnFamilyStore;
+import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.db.IMutation;
+import org.apache.cassandra.db.partitions.PartitionUpdate;
+import org.apache.cassandra.schema.Schema;
+import org.apache.cassandra.schema.TableId;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.service.ClientWarn;
+import org.apache.cassandra.service.thresholds.CoordinatorWarningsState;
+import org.apache.cassandra.utils.Pair;
+
+/**
+ * ThreadLocal manager for write warnings at the coordinator.
+ * Accumulates warnings from multiple write operations in a single client 
request,
+ * then sends them to the client and updates metrics.
+ *
+ * REFACTORED VERSION: Uses CoordinatorWarningsState for state management.
+ */
+public class CoordinatorWriteWarnings
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(CoordinatorWriteWarnings.class);
+
+    private static final Warnings INIT = new Warnings();
+    private static final Warnings EMPTY = new Warnings();
+
+    private static final CoordinatorWarningsState<Warnings> STATE =
+    new CoordinatorWarningsState<>("CoordinatorWriteWarnings",
+                                   INIT,
+                                   EMPTY,
+                                   Warnings::new,
+                                   logger,
+                                   false);
+
+    /**
+     * Initialize coordinator write warnings for this thread.
+     * Must be called at the start of a client request.
+     */
+    public static void init()
+    {
+        STATE.init();
+    }
+
+    /**
+     * Update warnings for a partition after receiving responses from replicas.
+     *
+     * @param mutation the mutation that was written
+     * @param snapshot the aggregated warnings from replicas
+     */
+    public static void update(IMutation mutation, WriteWarningsSnapshot 
snapshot)
+    {
+        if (snapshot.isEmpty())
+        {
+            return;
+        }
+
+        Warnings warnings = STATE.getMutableState(() -> EMPTY);
+        if (warnings == EMPTY)
+        {
+            return;
+        }
+
+        for (PartitionUpdate update : mutation.getPartitionUpdates())
+        {
+            Pair<TableId, DecoratedKey> key = 
Pair.create(update.metadata().id, update.partitionKey());
+            warnings.merge(key, snapshot);

Review Comment:
   ```suggestion
               warnings.merge(update.metadata().id, update.partitionKey(), 
snapshot);
   ```
   
   code is easier to read if you push creating the `Pair` into the `merge` 
function.  This is also a leaky detail as your API exposes how the internal 
data is structured.



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