Skip to content

Commit

Permalink
Refactor synchronized block on JmxMonitorRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnelson committed Mar 23, 2016
1 parent 059a8c4 commit 0f7fbe7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Expand Up @@ -50,6 +50,7 @@ public final class JmxMonitorRegistry implements MonitorRegistry {
private final ConcurrentMap<MonitorConfig, Monitor<?>> monitors;
private final String name;
private final ObjectNameMapper mapper;
private final ConcurrentMap<ObjectName, Object> locks = new ConcurrentHashMap<>();

private final AtomicBoolean updatePending = new AtomicBoolean(false);
private final AtomicReference<Collection<Monitor<?>>> monitorList =
Expand Down Expand Up @@ -80,7 +81,7 @@ public JmxMonitorRegistry(String name, ObjectNameMapper mapper) {
}

private void register(ObjectName objectName, DynamicMBean mbean) throws Exception {
synchronized (mBeanServer) {
synchronized (getLock(objectName)) {
if (mBeanServer.isRegistered(objectName)) {
mBeanServer.unregisterMBean(objectName);
}
Expand Down Expand Up @@ -152,4 +153,11 @@ public boolean isRegistered(Monitor<?> monitor) {
}
return false;
}

private Object getLock(ObjectName objectName) {
if (locks.get(objectName) == null) {
locks.putIfAbsent(objectName, new Object());
}
return locks.get(objectName);
}
}
Expand Up @@ -15,17 +15,19 @@
*/
package com.netflix.servo.publish.graphite;


import com.netflix.servo.Metric;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import static org.testng.Assert.assertEquals;


public class GraphiteMetricObserverTest {
private String getLocalHostIp() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
Expand All @@ -45,13 +47,22 @@ public void testBadAddress2() throws Exception {
public void testBadAddress3() throws Exception {
new GraphiteMetricObserver("serverA", "socket://" + getLocalHostIp() + ":808");
}

private int getAvailablePort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

@Test
public void testSuccessfulSend() throws Exception {
SocketReceiverTester receiver = new SocketReceiverTester(8082);
final int port = getAvailablePort();
SocketReceiverTester receiver = new SocketReceiverTester(port);
receiver.start();

String host = getLocalHostIp() + ":8082";
String host = getLocalHostIp() + ":" + port;
GraphiteMetricObserver gw = new GraphiteMetricObserver("serverA", host);

try {
Expand All @@ -76,10 +87,11 @@ public void testSuccessfulSend() throws Exception {

@Test
public void testReconnection() throws Exception {
SocketReceiverTester receiver = new SocketReceiverTester(8082);
final int port = getAvailablePort();
SocketReceiverTester receiver = new SocketReceiverTester(port);
receiver.start();

String host = getLocalHostIp() + ":8082";
String host = getLocalHostIp() + ":" + port;
GraphiteMetricObserver gw = new GraphiteMetricObserver("serverA", host);

try {
Expand All @@ -98,7 +110,7 @@ public void testReconnection() throws Exception {

// restarting the receiver
receiver.stop();
receiver = new SocketReceiverTester(8082);
receiver = new SocketReceiverTester(port);
receiver.start();

// the first write does not trigger exception given how TCP works
Expand Down

0 comments on commit 0f7fbe7

Please sign in to comment.