Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/raft stat describer #208

Merged
merged 11 commits into from
Jul 29, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alipay.sofa.jraft.entity.LeaderChangeContext;
import com.alipay.sofa.jraft.error.RaftException;
import com.alipay.sofa.jraft.option.FSMCallerOptions;
import com.alipay.sofa.jraft.util.Describer;

/**
* Finite state machine caller.
Expand All @@ -29,7 +30,7 @@
*
* 2018-Apr-03 11:07:52 AM
*/
public interface FSMCaller extends Lifecycle<FSMCallerOptions> {
public interface FSMCaller extends Lifecycle<FSMCallerOptions>, Describer {

/**
* Listen on lastAppliedLogIndex update events.
Expand Down
3 changes: 2 additions & 1 deletion jraft-core/src/main/java/com/alipay/sofa/jraft/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.alipay.sofa.jraft.error.LogNotFoundException;
import com.alipay.sofa.jraft.option.NodeOptions;
import com.alipay.sofa.jraft.option.RaftOptions;
import com.alipay.sofa.jraft.util.Describer;

/**
* A raft replica node.
Expand All @@ -37,7 +38,7 @@
*
* 2018-Apr-03 4:06:55 PM
*/
public interface Node extends Lifecycle<NodeOptions> {
public interface Node extends Lifecycle<NodeOptions>, Describer {

/**
* Get the leader peer id for redirect, null if absent.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 com.alipay.sofa.jraft;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alipay.sofa.jraft.util.Describer;
import com.alipay.sofa.jraft.util.FileOutputSignalHandler;
import com.alipay.sofa.jraft.util.SystemPropertyUtil;

/**
*
* @author jiachun.fjc
*/
public class NodeDescribeSignalHandler extends FileOutputSignalHandler {

private static Logger LOG = LoggerFactory.getLogger(NodeDescribeSignalHandler.class);

private static final String DIR = SystemPropertyUtil.get("jraft.signal.node.describe.dir", "");
private static final String BASE_NAME = "node_describe.log";
fengjiachun marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void handle(final String signalName) {
final List<Node> nodes = NodeManager.getInstance().getAllNodes();
if (nodes.isEmpty()) {
return;
}

try {
final File file = getOutputFile(DIR, BASE_NAME);

LOG.info("Describing raft nodes with signal: {} to file: {}.", signalName, file);

try (final PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true),
StandardCharsets.UTF_8))) {
final Describer.Printer printer = new DefaultPrinter(out);
for (final Node node : nodes) {
node.describe(printer);
}
}
} catch (final IOException e) {
LOG.error("Fail to describe nodes: {}.", nodes, e);
}
}

private static class DefaultPrinter implements Describer.Printer {

private final PrintWriter out;

private DefaultPrinter(PrintWriter out) {
this.out = out;
}

@Override
public Describer.Printer print(final Object x) {
this.out.print(x);
return this;
}

@Override
public Describer.Printer println(final Object x) {
this.out.println(x);
return this;
}
}
}
22 changes: 11 additions & 11 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/NodeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static NodeManager getInstance() {
/**
* Return true when RPC service is registered.
*/
public boolean serverExists(Endpoint addr) {
public boolean serverExists(final Endpoint addr) {
if (addr.getIp().equals(Utils.IP_ANY)) {
return this.addrSet.contains(new Endpoint(Utils.IP_ANY, addr.getPort()));
}
Expand All @@ -63,28 +63,28 @@ public boolean serverExists(Endpoint addr) {
/**
* Remove a RPC service address.
*/
public boolean removeAddress(Endpoint addr) {
public boolean removeAddress(final Endpoint addr) {
return this.addrSet.remove(addr);
}

/**
* Adds a RPC service address.
*/
public void addAddress(Endpoint addr) {
public void addAddress(final Endpoint addr) {
this.addrSet.add(addr);
}

/**
* Adds a node.
*/
public boolean add(Node node) {
public boolean add(final Node node) {
// check address ok?
if (!this.serverExists(node.getNodeId().getPeerId().getEndpoint())) {
if (!serverExists(node.getNodeId().getPeerId().getEndpoint())) {
return false;
}
NodeId nodeId = node.getNodeId();
final NodeId nodeId = node.getNodeId();
if (this.nodeMap.putIfAbsent(nodeId, node) == null) {
String groupId = node.getGroupId();
final String groupId = node.getGroupId();
List<Node> nodes = this.groupMap.get(groupId);
if (nodes == null) {
nodes = Collections.synchronizedList(new ArrayList<>());
Expand Down Expand Up @@ -112,9 +112,9 @@ public void clear() {
/**
* Remove a node.
*/
public boolean remove(Node node) {
public boolean remove(final Node node) {
if (this.nodeMap.remove(node.getNodeId(), node)) {
List<Node> nodes = this.groupMap.get(node.getGroupId());
final List<Node> nodes = this.groupMap.get(node.getGroupId());
if (nodes != null) {
return nodes.remove(node);
}
Expand All @@ -125,14 +125,14 @@ public boolean remove(Node node) {
/**
* Get node by groupId and peer.
*/
public Node get(String groupId, PeerId peerId) {
public Node get(final String groupId, final PeerId peerId) {
return this.nodeMap.get(new NodeId(groupId, peerId));
}

/**
* Get all nodes in a raft group.
*/
public List<Node> getNodesByGroupId(String groupId) {
public List<Node> getNodesByGroupId(final String groupId) {
return this.groupMap.get(groupId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 com.alipay.sofa.jraft;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alipay.sofa.jraft.core.NodeMetrics;
import com.alipay.sofa.jraft.util.FileOutputSignalHandler;
import com.alipay.sofa.jraft.util.MetricReporter;
import com.alipay.sofa.jraft.util.SystemPropertyUtil;
import com.codahale.metrics.MetricRegistry;

/**
*
* @author jiachun.fjc
*/
public class NodeMetricsSignalHandler extends FileOutputSignalHandler {

private static Logger LOG = LoggerFactory.getLogger(NodeMetricsSignalHandler.class);

private static final String DIR = SystemPropertyUtil.get("jraft.signal.node.metrics.dir", "");
private static final String BASE_NAME = "node_metrics.log";

@Override
public void handle(final String signalName) {
final List<Node> nodes = NodeManager.getInstance().getAllNodes();
if (nodes.isEmpty()) {
return;
}

try {
final File file = getOutputFile(DIR, BASE_NAME);

LOG.info("Printing raft nodes metrics with signal: {} to file: {}.", signalName, file);

try (final PrintStream out = new PrintStream(new FileOutputStream(file, true))) {
for (final Node node : nodes) {
final NodeMetrics nodeMetrics = node.getNodeMetrics();
final MetricRegistry registry = nodeMetrics.getMetricRegistry();
if (registry == null) {
LOG.warn("Node: {} received a signal to print metric, but it does not have metric enabled.",
node);
continue;
Copy link
Contributor

Choose a reason for hiding this comment

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

建议加个 warn log,当没启用统计的时候

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
final MetricReporter reporter = MetricReporter.forRegistry(registry) //
.outputTo(out) //
.prefixedWith("-- " + node.getNodeId()) //
.build();
reporter.report();
}
}
} catch (final IOException e) {
LOG.error("Fail to print nodes metrics: {}.", nodes, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alipay.sofa.jraft.option.ReplicatorGroupOptions;
import com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesResponse;
import com.alipay.sofa.jraft.rpc.RpcResponseClosure;
import com.alipay.sofa.jraft.util.Describer;
import com.alipay.sofa.jraft.util.ThreadId;

/**
Expand All @@ -34,7 +35,7 @@
*
* 2018-Apr-08 5:35:26 PM
*/
public interface ReplicatorGroup {
public interface ReplicatorGroup extends Describer {
/**
* Init the replicator group.
*
Expand Down
Loading