You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2022/04/03 11:05:55 UTC

[GitHub] [cassandra] smiklosovic commented on a change in pull request #1546: CASSANDRA-17150 trunk: Guardrails for disk usage

smiklosovic commented on a change in pull request #1546:
URL: https://github.com/apache/cassandra/pull/1546#discussion_r841205166



##########
File path: src/java/org/apache/cassandra/service/disk/usage/DiskUsageBroadcaster.java
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.disk.usage;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.gms.ApplicationState;
+import org.apache.cassandra.gms.EndpointState;
+import org.apache.cassandra.gms.Gossiper;
+import org.apache.cassandra.gms.IEndpointStateChangeSubscriber;
+import org.apache.cassandra.gms.VersionedValue;
+import org.apache.cassandra.locator.InetAddressAndPort;
+import org.apache.cassandra.service.StorageService;
+import org.apache.cassandra.utils.NoSpamLogger;
+
+/**
+ * Starts {@link DiskUsageMonitor} to monitor local disk usage state and broadcast new state via Gossip.
+ * At the same time, it caches cluster's disk usage state received via Gossip.
+ */
+public class DiskUsageBroadcaster implements IEndpointStateChangeSubscriber
+{
+    private static final Logger logger = LoggerFactory.getLogger(DiskUsageBroadcaster.class);
+    private static final NoSpamLogger noSpamLogger = NoSpamLogger.getLogger(logger, 10, TimeUnit.MINUTES);
+
+    public static final DiskUsageBroadcaster instance = new DiskUsageBroadcaster(DiskUsageMonitor.instance);
+
+    private final DiskUsageMonitor monitor;
+    private final ConcurrentMap<InetAddressAndPort, DiskUsageState> usageInfo = new ConcurrentHashMap<>();
+    private volatile boolean hasStuffedOrFullNode = false;
+
+    @VisibleForTesting
+    public DiskUsageBroadcaster(DiskUsageMonitor monitor)
+    {
+        this.monitor = monitor;
+        Gossiper.instance.register(this);
+    }
+
+    /**
+     * @return {@code true} if any node in the cluster is STUFFED OR FULL
+     */
+    public boolean hasStuffedOrFullNode()
+    {
+        return hasStuffedOrFullNode;
+    }
+
+    /**
+     * @return {@code true} if given node's disk usage is FULL
+     */
+    public boolean isFull(InetAddressAndPort endpoint)
+    {
+        return state(endpoint).isFull();
+    }
+
+    /**
+     * @return {@code true} if given node's disk usage is STUFFED
+     */
+    public boolean isStuffed(InetAddressAndPort endpoint)
+    {
+        return state(endpoint).isStuffed();
+    }
+
+    @VisibleForTesting
+    public DiskUsageState state(InetAddressAndPort endpoint)
+    {
+        return usageInfo.getOrDefault(endpoint, DiskUsageState.NOT_AVAILABLE);
+    }
+
+    public void startBroadcasting()
+    {
+        monitor.start(newState -> {
+
+            if (logger.isTraceEnabled())
+                logger.trace("Disseminating disk usage info: {}", newState);
+
+            Gossiper.instance.addLocalApplicationState(ApplicationState.DISK_USAGE,
+                                                       StorageService.instance.valueFactory.diskUsage(newState.name()));
+        });
+    }
+
+    @Override
+    public void onChange(InetAddressAndPort endpoint, ApplicationState state, VersionedValue value)
+    {
+        if (state != ApplicationState.DISK_USAGE)
+            return;
+
+        DiskUsageState usageState = DiskUsageState.NOT_AVAILABLE;
+        try
+        {
+            usageState = DiskUsageState.valueOf(value.value);
+        }
+        catch (IllegalArgumentException e)
+        {
+            noSpamLogger.warn(String.format("Found unknown DiskUsageState: %s. Using default state %s instead.",
+                                            value.value, usageState));
+        }
+        usageInfo.put(endpoint, usageState);
+
+        hasStuffedOrFullNode = usageState.isStuffedOrFull() || usageInfo.values().stream().anyMatch(DiskUsageState::isStuffedOrFull);

Review comment:
       @adelapena do you think using stream() here is performance friendly / without any penalty when hundreds of nodes are used? Would not be simple for loop better?




-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org