Merge branch 'cassandra-3.11' into trunk

This commit is contained in:
Alex Petrov 2020-12-14 14:34:35 +01:00
commit d41fe2aa8e
5 changed files with 248 additions and 1 deletions

View File

@ -592,7 +592,7 @@
<dependency groupId="org.mockito" artifactId="mockito-core" version="3.2.4" />
<dependency groupId="org.quicktheories" artifactId="quicktheories" version="0.25" />
<dependency groupId="com.google.code.java-allocation-instrumenter" artifactId="java-allocation-instrumenter" version="${allocation-instrumenter.version}" />
<dependency groupId="org.apache.cassandra" artifactId="dtest-api" version="0.0.6" />
<dependency groupId="org.apache.cassandra" artifactId="dtest-api" version="0.0.7" />
<dependency groupId="org.reflections" artifactId="reflections" version="0.9.12" />
<dependency groupId="org.apache.rat" artifactId="apache-rat" version="0.10">
<exclusion groupId="commons-lang" artifactId="commons-lang"/>

View File

@ -67,6 +67,7 @@ import org.apache.cassandra.distributed.api.NodeToolResult;
import org.apache.cassandra.distributed.api.TokenSupplier;
import org.apache.cassandra.distributed.shared.InstanceClassLoader;
import org.apache.cassandra.distributed.shared.MessageFilters;
import org.apache.cassandra.distributed.shared.Metrics;
import org.apache.cassandra.distributed.shared.NetworkTopology;
import org.apache.cassandra.distributed.shared.Shared;
import org.apache.cassandra.distributed.shared.ShutdownException;
@ -256,6 +257,14 @@ public abstract class AbstractCluster<I extends IInstance> implements ICluster<I
throw new IllegalStateException("Cannot get live member count on shutdown instance: " + config.num());
}
public Metrics metrics()
{
if (isShutdown)
throw new IllegalStateException();
return delegate.metrics();
}
public NodeToolResult nodetoolResult(boolean withNotifications, String... commandAndArgs)
{
return delegate().nodetoolResult(withNotifications, commandAndArgs);

View File

@ -81,6 +81,7 @@ import org.apache.cassandra.distributed.api.NodeToolResult;
import org.apache.cassandra.distributed.api.SimpleQueryResult;
import org.apache.cassandra.distributed.mock.nodetool.InternalNodeProbe;
import org.apache.cassandra.distributed.mock.nodetool.InternalNodeProbeFactory;
import org.apache.cassandra.distributed.shared.Metrics;
import org.apache.cassandra.gms.ApplicationState;
import org.apache.cassandra.gms.Gossiper;
import org.apache.cassandra.gms.VersionedValue;
@ -93,6 +94,7 @@ import org.apache.cassandra.io.util.DataInputBuffer;
import org.apache.cassandra.io.util.DataOutputBuffer;
import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.locator.InetAddressAndPort;
import org.apache.cassandra.metrics.CassandraMetricsRegistry;
import org.apache.cassandra.net.Message;
import org.apache.cassandra.net.MessagingService;
import org.apache.cassandra.net.NoPayload;
@ -692,6 +694,11 @@ public class Instance extends IsolatedExecutor implements IInvokableInstance
}).call();
}
public Metrics metrics()
{
return callOnInstance(() -> new InstanceMetrics(CassandraMetricsRegistry.Metrics));
}
public NodeToolResult nodetoolResult(boolean withNotifications, String... commandAndArgs)
{
return sync(() -> {

View File

@ -0,0 +1,183 @@
/*
* 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.distributed.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import org.apache.cassandra.distributed.shared.Metrics;
import org.apache.cassandra.metrics.CassandraMetricsRegistry;
/**
* Pulls metrics out of in-JVM dtest cluster instance.
*/
class InstanceMetrics implements Metrics
{
private final CassandraMetricsRegistry metricsRegistry;
InstanceMetrics(CassandraMetricsRegistry metricsRegistry)
{
this.metricsRegistry = metricsRegistry;
}
public List<String> getNames()
{
return new ArrayList<>(metricsRegistry.getNames());
}
public long getCounter(String name)
{
return metricsRegistry.getCounters().get(name).getCount();
}
public Map<String, Long> getCounters(Predicate<String> filter)
{
Map<String, Long> values = new HashMap<>();
for (Map.Entry<String, Counter> e : metricsRegistry.getCounters().entrySet())
{
if (filter.test(e.getKey()))
values.put(e.getKey(), e.getValue().getCount());
}
return values;
}
public double getHistogram(String name, MetricValue value)
{
Histogram histogram = metricsRegistry.getHistograms().get(name);
if (value == MetricValue.COUNT)
return histogram.getCount();
return getValue(histogram.getSnapshot(), value);
}
public Map<String, Double> getHistograms(Predicate<String> filter, MetricValue value)
{
Map<String, Double> values = new HashMap<>();
for (Map.Entry<String, Histogram> e : metricsRegistry.getHistograms().entrySet())
{
if (filter.test(e.getKey()))
values.put(e.getKey(), getValue(e.getValue().getSnapshot(), value));
}
return values;
}
public Object getGauge(String name)
{
return metricsRegistry.getGauges().get(name).getValue();
}
public Map<String, Object> getGauges(Predicate<String> filter)
{
Map<String, Object> values = new HashMap<>();
for (Map.Entry<String, Gauge> e : metricsRegistry.getGauges().entrySet())
{
if (filter.test(e.getKey()))
values.put(e.getKey(), e.getValue().getValue());
}
return values;
}
public double getMeter(String name, Rate value)
{
return getRate(metricsRegistry.getMeters().get(name), value);
}
public Map<String, Double> getMeters(Predicate<String> filter, Rate rate)
{
Map<String, Double> values = new HashMap<>();
for (Map.Entry<String, Meter> e : metricsRegistry.getMeters().entrySet())
{
if (filter.test(e.getKey()))
values.put(e.getKey(), getRate(e.getValue(), rate));
}
return values;
}
public double getTimer(String name, MetricValue value)
{
return getValue(metricsRegistry.getTimers().get(name).getSnapshot(), value);
}
public Map<String, Double> getTimers(Predicate<String> filter, MetricValue value)
{
Map<String, Double> values = new HashMap<>();
for (Map.Entry<String, Timer> e : metricsRegistry.getTimers().entrySet())
{
if (filter.test(e.getKey()))
values.put(e.getKey(), getValue(e.getValue().getSnapshot(), value));
}
return values;
}
static double getValue(Snapshot snapshot, MetricValue value)
{
switch (value)
{
case MEDIAN:
return snapshot.getMedian();
case P75:
return snapshot.get75thPercentile();
case P95:
return snapshot.get95thPercentile();
case P98:
return snapshot.get98thPercentile();
case P99:
return snapshot.get99thPercentile();
case P999:
return snapshot.get999thPercentile();
case MAX:
return snapshot.getMax();
case MEAN:
return snapshot.getMean();
case MIN:
return snapshot.getMin();
case STDDEV:
return snapshot.getStdDev();
default:
throw new RuntimeException("Shouldn't happen");
}
}
static double getRate(Meter meter, Rate rate)
{
switch (rate)
{
case RATE15_MIN:
return meter.getFifteenMinuteRate();
case RATE5_MIN:
return meter.getFiveMinuteRate();
case RATE1_MIN:
return meter.getOneMinuteRate();
case RATE_MEAN:
return meter.getMeanRate();
default:
throw new RuntimeException("Shouldn't happen");
}
}
}

View File

@ -0,0 +1,48 @@
/*
* 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.distributed.test;
import com.google.common.collect.ImmutableMap;
import org.junit.Assert;
import org.junit.Test;
import org.apache.cassandra.distributed.api.ICluster;
import org.apache.cassandra.distributed.api.IInvokableInstance;
import org.apache.cassandra.metrics.CassandraMetricsRegistry;
public class MetricsTest extends TestBaseImpl
{
@Test
public void testMetrics() throws Throwable
{
try (ICluster<IInvokableInstance> cluster = init(builder().withNodes(1)
.start()))
{
cluster.get(1).runOnInstance(() -> {
CassandraMetricsRegistry.Metrics.counter("test_counter").inc(100);
CassandraMetricsRegistry.Metrics.counter("test_counter_2").inc(101);
});
Assert.assertEquals(100, cluster.get(1).metrics().getCounter("test_counter"));
Assert.assertEquals(ImmutableMap.of("test_counter", 100L,
"test_counter_2", 101L),
cluster.get(1).metrics().getCounters(s -> s.startsWith("test_counter")));
}
}
}