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

Add Refactoring #4060

Closed
wants to merge 8 commits into from
Closed
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 @@ -15,15 +15,18 @@
@State(Scope.Benchmark)
public class CachedGaugeBenchmark {

private static final long LOAD_VALUE_DELAY_MILLIS = 10; // Loading delay for 10 milliseconds
private static final int DEFAULT_VALUE = 12345; // Placeholder value

private CachedGauge<Integer> cachedGauge = new CachedGauge<Integer>(100, TimeUnit.MILLISECONDS) {
@Override
protected Integer loadValue() {
try {
Thread.sleep(10);
Thread.sleep(LOAD_VALUE_DELAY_MILLIS);
} catch (InterruptedException e) {
throw new RuntimeException("Thread was interrupted", e);
}
return 12345;
return DEFAULT_VALUE;
}
};

Expand Down
6 changes: 6 additions & 0 deletions metrics-collectd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,11 @@
<version>1.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.codahale.metrics.collectd;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class SecurityConfigurationTest {

private byte[] username;
private byte[] password;
private SecurityLevel securityLevel;

@BeforeEach
public void setUp() {
username = "admin".getBytes();
password = "password".getBytes();
securityLevel = SecurityLevel.SIGN;
}

/**
* Test the constructor and getters of the SecurityConfiguration class.
*/
@Test
public void testConstructorAndGetters() {
// When
SecurityConfiguration config = new SecurityConfiguration(username, password, securityLevel);

// Then
assertArrayEquals(username, config.getUsername(), "Username matched");
assertArrayEquals(password, config.getPassword(), "Password matched");
assertEquals(securityLevel, config.getSecurityLevel(), "Security level matched");
}

/**
* Test the none() factory method of SecurityConfiguration to ensure it creates
* a configuration with no security settings.
*/
@Test
public void testNoneSecurityConfiguration() {
// When
SecurityConfiguration config = SecurityConfiguration.none();

// Then
assertNull(config.getUsername(), "Username is null");
assertNull(config.getPassword(), "Password is null");
assertEquals(SecurityLevel.NONE, config.getSecurityLevel(), "Security level is NONE");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,41 @@ private void freeChunk(Chunk chunk) {

synchronized boolean put(long key, long value) {
Chunk activeChunk = chunks.peekLast();
if (activeChunk != null && activeChunk.cursor != 0 && activeChunk.keys[activeChunk.cursor - 1] > key) {
// key should be the same as last inserted or bigger
return false;
}
if (activeChunk == null || activeChunk.cursor - activeChunk.startIndex == activeChunk.chunkSize) {
// The last chunk doesn't exist or full
activeChunk = allocateChunk();
chunks.add(activeChunk);

// Validate key order
if (hasValidActiveChunkForAppend(activeChunk) && isKeyOutOfOrder(activeChunk, key)) {
return false; // Key should be the same as last inserted or bigger
}

// Ensure chunk availability
activeChunk = ensureChunkAvailable(activeChunk);

// Append key-value pair
activeChunk.append(key, value);
return true;
}

private boolean hasValidActiveChunkForAppend(Chunk activeChunk) {
return activeChunk != null && activeChunk.cursor != 0;
}

private boolean isKeyOutOfOrder(Chunk activeChunk, long key) {
return activeChunk.keys[activeChunk.cursor - 1] > key;
}

private Chunk ensureChunkAvailable(Chunk activeChunk) {
if (activeChunk == null || isChunkFull(activeChunk)) {
return allocateChunk();
} else {
return activeChunk;
}
}

private boolean isChunkFull(Chunk activeChunk) {
return activeChunk.cursor - activeChunk.startIndex == activeChunk.chunkSize;
}


synchronized long[] values() {
final int valuesSize = size();
if (valuesSize == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,119 +19,100 @@
* @since 4.1.17
*/
public final class NoopMetricRegistry extends MetricRegistry {

private static final EmptyConcurrentMap<String, Metric> EMPTY_CONCURRENT_MAP = new EmptyConcurrentMap<>();

/**
* {@inheritDoc}
*/
@Override
protected ConcurrentMap<String, Metric> buildMap() {
return EMPTY_CONCURRENT_MAP;
}
/**
* {@inheritDoc}
*/
@Override
protected ConcurrentMap<String, Metric> buildMap() {
return new EmptyConcurrentMap<>();
}

/**
* {@inheritDoc}
*/
@Override
public <T extends Metric> T register(String name, T metric) throws IllegalArgumentException {
if (metric == null) {
throw new NullPointerException("metric == null");
/**
* Factory method to create instances of Noop metrics.
*/
public static NoopMetricFactory metricFactory() {
return new NoopMetricFactory();
}
return metric;
}

/**
* {@inheritDoc}
*/
@Override
public void registerAll(MetricSet metrics) throws IllegalArgumentException {
// NOP
}
/**
* {@inheritDoc}
*/
@Override
public <T extends Metric> T register(String name, T metric) throws IllegalArgumentException {
if (metric == null) {
throw new NullPointerException("metric == null");
}
return metric;
}

/**
* {@inheritDoc}
*/
@Override
public Counter counter(String name) {
return NoopCounter.INSTANCE;
}

/**
* {@inheritDoc}
* Factory class to create instances of Noop metrics.
*/
@Override
public Counter counter(String name, MetricSupplier<Counter> supplier) {
return NoopCounter.INSTANCE;
}
static class NoopMetricFactory {

/**
* {@inheritDoc}
*/
@Override
public Histogram histogram(String name) {
return NoopHistogram.INSTANCE;
}
public Counter counter(String name, MetricSupplier<Counter> supplier) {
return NoopCounter.INSTANCE;
}

/**
* {@inheritDoc}
*/
@Override
public Histogram histogram(String name, MetricSupplier<Histogram> supplier) {
return NoopHistogram.INSTANCE;
}
public Histogram histogram(String name, MetricSupplier<Histogram> supplier) {
return NoopHistogram.INSTANCE;
}

/**
* {@inheritDoc}
*/
@Override
public Meter meter(String name) {
return NoopMeter.INSTANCE;
}
public Histogram histogram(String name) {
return NoopHistogram.INSTANCE;
}

/**
* {@inheritDoc}
*/
@Override
public Meter meter(String name, MetricSupplier<Meter> supplier) {
return NoopMeter.INSTANCE;
}
public Meter meter(String name) {
return NoopMeter.INSTANCE;
}

/**
* {@inheritDoc}
*/
@Override
public Timer timer(String name) {
return NoopTimer.INSTANCE;
}
public Meter meter(String name, MetricSupplier<Meter> supplier) {
return NoopMeter.INSTANCE;
}

/**
* {@inheritDoc}
*/
@Override
public Timer timer(String name, MetricSupplier<Timer> supplier) {
return NoopTimer.INSTANCE;
}
public Timer timer(String name) {
return NoopTimer.INSTANCE;
}

/**
* {@inheritDoc}
*
* @since 4.2
*/
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public <T extends Gauge> T gauge(String name) {
return (T) NoopGauge.INSTANCE;
public Timer timer(String name, MetricSupplier<Timer> supplier) {
return NoopTimer.INSTANCE;
}

@SuppressWarnings({"rawtypes", "unchecked"})
public <T extends Gauge> T gauge(String name) {
return (T) NoopGauge.INSTANCE;
}

@SuppressWarnings({"rawtypes", "unchecked"})
public <T extends Gauge> T gauge(String name, MetricSupplier<T> supplier) {
return (T) NoopGauge.INSTANCE;
}

protected ConcurrentMap<String, Metric> buildMap() {
return EMPTY_CONCURRENT_MAP;
}

public <T extends Metric> T register(String name, T metric) throws IllegalArgumentException {
if (metric == null) {
throw new NullPointerException("metric == null");
}
return metric;
}
}

/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public <T extends Gauge> T gauge(String name, MetricSupplier<T> supplier) {
return (T) NoopGauge.INSTANCE;
public void registerAll(MetricSet metrics) throws IllegalArgumentException {
// NOP
}


/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.codahale.metrics;

import java.io.Closeable;
import java.util.concurrent.TimeUnit;

/*
* A tag interface to indicate that a class is a Reporter.
*/
public interface Reporter extends Closeable {

void start(long period, TimeUnit unit);

void start(long initialDelay, long period, TimeUnit unit);

void stop();

void report();

}
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ public Result execute() {
} catch (Exception e) {
result = Result.unhealthy(e);
}
result.setDuration(TimeUnit.MILLISECONDS.convert(clock().getTick() - start, TimeUnit.NANOSECONDS));
result.setDuration(TimeUnit.MILLISECONDS.convert(
clock().getTick() - start, TimeUnit.NANOSECONDS));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.codahale.metrics.health;

/**
* Constants for HealthCheck.Result class.
*/
public final class HealthCheckResultConstants {
public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
public static final int PRIME = 31;

private HealthCheckResultConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public HttpHost startServerWithGlobalRequestHandler(HttpRequestHandler handler)
return new HttpHost("localhost", address.getPort(), "http");
}



@After
public void tearDown() {
if (server != null) {
Expand Down
Loading