Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
*/
package org.apache.ratis.grpc.metrics;

import org.apache.ratis.metrics.LongCounter;
import org.apache.ratis.metrics.MetricRegistryInfo;
import org.apache.ratis.metrics.RatisMetricRegistry;
import org.apache.ratis.metrics.RatisMetrics;

import org.apache.ratis.thirdparty.com.codahale.metrics.Timer;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;

public class GrpcServerMetrics extends RatisMetrics {
Expand All @@ -44,11 +48,27 @@ public class GrpcServerMetrics extends RatisMetrics {
= "%s_pending_log_requests_count";

public static final String RATIS_GRPC_METRICS_REQUEST_RETRY_COUNT = "num_retries";
public static final String RATIS_GRPC_METRICS_REQUESTS_TOTAL = "num_requests";
public static final String RATIS_GRPC_METRICS_REQUESTS_COUNT = "num_requests";
public static final String RATIS_GRPC_INSTALL_SNAPSHOT_COUNT = "num_install_snapshot";

private final LongCounter requestRetry;
private final LongCounter requestInstallSnapshot;

private final Function<Boolean, LongCounter> requestCreate;

private final Map<String, Function<Boolean, LongCounter>> requestSuccess = new ConcurrentHashMap<>();
private final Map<String, Function<Boolean, LongCounter>> requestTimeout = new ConcurrentHashMap<>();

private final Map<String, LongCounter> requestNotLeader = new ConcurrentHashMap<>();
private final Map<String, LongCounter> requestInconsistency = new ConcurrentHashMap<>();

public GrpcServerMetrics(String serverId) {
registry = getMetricRegistryForGrpcServer(serverId);

requestRetry = registry.counter(RATIS_GRPC_METRICS_REQUEST_RETRY_COUNT);
requestInstallSnapshot = registry.counter(RATIS_GRPC_INSTALL_SNAPSHOT_COUNT);

requestCreate = newHeartbeatCounter(RATIS_GRPC_METRICS_REQUESTS_COUNT);
}

private RatisMetricRegistry getMetricRegistryForGrpcServer(String serverId) {
Expand All @@ -64,37 +84,59 @@ public Timer getGrpcLogAppenderLatencyTimer(String follower,
}

public void onRequestRetry() {
registry.counter(RATIS_GRPC_METRICS_REQUEST_RETRY_COUNT).inc();
requestRetry.inc();
}

public void onRequestCreate(boolean isHeartbeat) {
registry.counter(RATIS_GRPC_METRICS_REQUESTS_TOTAL + getHeartbeatSuffix(isHeartbeat)).inc();
requestCreate.apply(isHeartbeat).inc();
}

private Function<Boolean, LongCounter> newRequestSuccess(String follower) {
final String prefix = String.format(RATIS_GRPC_METRICS_LOG_APPENDER_SUCCESS, follower);
return newHeartbeatCounter(prefix);
}

public void onRequestSuccess(String follower, boolean isHearbeat) {
registry.counter(String.format(RATIS_GRPC_METRICS_LOG_APPENDER_SUCCESS + getHeartbeatSuffix(isHearbeat),
follower)).inc();
public void onRequestSuccess(String follower, boolean isHeartbeat) {
requestSuccess.computeIfAbsent(follower, this::newRequestSuccess).apply(isHeartbeat).inc();
}

private LongCounter newRequestNotLeader(String follower) {
return registry.counter(String.format(RATIS_GRPC_METRICS_LOG_APPENDER_NOT_LEADER, follower));
}

public void onRequestNotLeader(String follower) {
registry.counter(String.format(RATIS_GRPC_METRICS_LOG_APPENDER_NOT_LEADER, follower)).inc();
requestNotLeader.computeIfAbsent(follower, this::newRequestNotLeader).inc();
}

private LongCounter newRequestInconsistency(String follower) {
return registry.counter(String.format(RATIS_GRPC_METRICS_LOG_APPENDER_INCONSISTENCY, follower));
}

public void onRequestInconsistency(String follower) {
registry.counter(String.format(RATIS_GRPC_METRICS_LOG_APPENDER_INCONSISTENCY, follower)).inc();
requestInconsistency.computeIfAbsent(follower, this::newRequestInconsistency).inc();
}

private Function<Boolean, LongCounter> newRequestTimeout(String follower) {
final String prefix = String.format(RATIS_GRPC_METRICS_LOG_APPENDER_TIMEOUT, follower);
return newHeartbeatCounter(prefix);
}

public void onRequestTimeout(String follower, boolean isHeartbeat) {
registry.counter(String.format(RATIS_GRPC_METRICS_LOG_APPENDER_TIMEOUT + getHeartbeatSuffix(isHeartbeat),
follower)).inc();
requestTimeout.computeIfAbsent(follower, this::newRequestTimeout).apply(isHeartbeat).inc();
}

public void addPendingRequestsCount(String follower, Supplier<Integer> pendinglogQueueSize) {
registry.gauge(String.format(RATIS_GRPC_METRICS_LOG_APPENDER_PENDING_COUNT, follower), () -> pendinglogQueueSize);
}

public void onInstallSnapshot() {
registry.counter(RATIS_GRPC_INSTALL_SNAPSHOT_COUNT).inc();
requestInstallSnapshot.inc();
}

private Function<Boolean, LongCounter> newHeartbeatCounter(String prefix) {
final LongCounter trueCounter = registry.counter(prefix + getHeartbeatSuffix(true));
final LongCounter falseCounter = registry.counter(prefix + getHeartbeatSuffix(false));
return b -> b? trueCounter : falseCounter;
}

public static String getHeartbeatSuffix(boolean heartbeat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.apache.ratis.grpc.metrics.GrpcServerMetrics.RATIS_GRPC_METRICS_LOG_APPENDER_PENDING_COUNT;
import static org.apache.ratis.grpc.metrics.GrpcServerMetrics.RATIS_GRPC_METRICS_LOG_APPENDER_SUCCESS;
import static org.apache.ratis.grpc.metrics.GrpcServerMetrics.RATIS_GRPC_METRICS_LOG_APPENDER_TIMEOUT;
import static org.apache.ratis.grpc.metrics.GrpcServerMetrics.RATIS_GRPC_METRICS_REQUESTS_TOTAL;
import static org.apache.ratis.grpc.metrics.GrpcServerMetrics.RATIS_GRPC_METRICS_REQUESTS_COUNT;
import static org.apache.ratis.grpc.metrics.GrpcServerMetrics.RATIS_GRPC_METRICS_REQUEST_RETRY_COUNT;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -88,11 +88,11 @@ public void testGrpcLogAppenderLatencyTimer() throws Exception {
public void testGrpcLogRequestTotal() {
for (boolean heartbeat : new boolean[] { true, false }) {
long reqTotal = ratisMetricRegistry.counter(
RATIS_GRPC_METRICS_REQUESTS_TOTAL + GrpcServerMetrics
RATIS_GRPC_METRICS_REQUESTS_COUNT + GrpcServerMetrics
.getHeartbeatSuffix(heartbeat)).getCount();
grpcServerMetrics.onRequestCreate(heartbeat);
Assert.assertEquals(reqTotal + 1, ratisMetricRegistry.counter(
RATIS_GRPC_METRICS_REQUESTS_TOTAL + GrpcServerMetrics
RATIS_GRPC_METRICS_REQUESTS_COUNT + GrpcServerMetrics
.getHeartbeatSuffix(heartbeat)).getCount());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.ratis.metrics;

public interface LongCounter {
default void inc() {
inc(1L);
}

void inc(long n);

default void dec() {
dec(1L);
}

void dec(long n);

long getCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.ratis.metrics;

import org.apache.ratis.thirdparty.com.codahale.metrics.ConsoleReporter;
import org.apache.ratis.thirdparty.com.codahale.metrics.Counter;
import org.apache.ratis.thirdparty.com.codahale.metrics.Histogram;
import org.apache.ratis.thirdparty.com.codahale.metrics.Meter;
import org.apache.ratis.thirdparty.com.codahale.metrics.Metric;
Expand All @@ -33,16 +32,14 @@
public interface RatisMetricRegistry {
Timer timer(String name);

Counter counter(String name);
LongCounter counter(String name);

boolean remove(String name);

<T> void gauge(String name, Supplier<Supplier<T>> gaugeSupplier);

Timer timer(String name, MetricRegistry.MetricSupplier<Timer> supplier);

Counter counter(String name, MetricRegistry.MetricSupplier<Counter> supplier);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I don't find any calls in ratis code, I'm not sure if removing it directly would introduce compatibility issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codings-dan , thanks for reviewing this.

  • First of all, ratis-metrics is not yet a public API. It is neither in the ratis-server-api module nor in the org.apache.ratis.protocol package.
  • The JIRA is to remove the use Counter. There is no way to make it API compatible (e.g. Changing Counter to LongCounter is already API incompatible). The user applications (e.g. Ozone) using it must update their code.

We may consider NOT merging this to 2.4.0 for now. Once we have finished all the sub tasks in RATIS-1688, we may merge all the changes to branch-2 and require the user applications to update their code.

Copy link
Contributor

@codings-dan codings-dan Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • There is no way to make it API compatible

Got it.

We may consider NOT merging this to 2.4.0 for now

Sure, I also think 2.4.0 doesn't need to include RATIS-1688 related code.


Histogram histogram(String name);

Meter meter(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.ratis.metrics.impl;

import org.apache.ratis.metrics.LongCounter;
import org.apache.ratis.metrics.MetricRegistryInfo;
import org.apache.ratis.metrics.RatisMetricRegistry;
import org.apache.ratis.thirdparty.com.codahale.metrics.ConsoleReporter;
Expand Down Expand Up @@ -57,9 +58,28 @@ public Timer timer(String name) {
return metricRegistry.timer(getMetricName(name));
}

static LongCounter toLongCounter(Counter c) {
return new LongCounter() {
@Override
public void inc(long n) {
c.inc(n);
}

@Override
public void dec(long n) {
c.dec(n);
}

@Override
public long getCount() {
return c.getCount();
}
};
}

@Override
public Counter counter(String name) {
return metricRegistry.counter(getMetricName(name));
public LongCounter counter(String name) {
return toLongCounter(metricRegistry.counter(getMetricName(name)));
}

@Override
Expand All @@ -84,10 +104,6 @@ public SortedMap<String, Gauge> getGauges(MetricFilter filter) {
return metricRegistry.getGauges(filter);
}

@Override public Counter counter(String name, MetricRegistry.MetricSupplier<Counter> supplier) {
return metricRegistry.counter(getMetricName(name), supplier);
}

@Override public Histogram histogram(String name) {
return metricRegistry.histogram(getMetricName(name));
}
Expand Down
Loading