Skip to content

Commit

Permalink
Added SystemInfo producer
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed Jan 9, 2024
1 parent c2a49da commit 7163029
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/java/org/apache/cassandra/service/StartupChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void execute(StartupChecksOptions options) throws StartupException
@Override
public void execute(StartupChecksOptions options)
{
Optional<String> degradations = new SystemInfo().isDegraded();
Optional<String> degradations = SystemInfo.instance().isDegraded();

if (degradations.isPresent())
logger.warn("Cassandra server running in degraded mode. " + degradations.get());
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/apache/cassandra/utils/FBUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class FBUtilities

private static int availableProcessors = CASSANDRA_AVAILABLE_PROCESSORS.getInt(DatabaseDescriptor.getAvailableProcessors());

private static volatile Supplier<Semver> kernelVersionSupplier = Suppliers.memoize(() -> new SystemInfo().getKernelVersion());
private static volatile Supplier<Semver> kernelVersionSupplier = Suppliers.memoize(() -> SystemInfo.instance().getKernelVersion());

public static void setAvailableProcessors(int value)
{
Expand Down
27 changes: 26 additions & 1 deletion src/java/org/apache/cassandra/utils/SystemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ public final class SystemInfo

private static final Pattern SPACES_PATTERN = Pattern.compile("\\s+");

private static Supplier<SystemInfo> provider = () -> new SystemInfo();

/**
* Sets the Supplier of the SystemInfo.
* <p>
* If {@code null} is passed as the provider the provider will be reset to the default provider.
* </p>
* @param provider the Supplier of SystemInfo that will be used for {@code instance} calls. May be null.
*/
public static void setProvider(Supplier<SystemInfo> provider) {
SystemInfo.provider = provider == null ? () -> new SystemInfo() : provider;
}

/**
* Gets an instance of SystemInfo. Whether the SystemInfo instance is new or memoized depends upon the
* implementation of the provider specified by {@code setProvider}. By default a new version of SystemInfo
* is created on every call.
* @return
*/
public static SystemInfo instance()
{
return provider.get();
}

/**
* The oshi.SystemInfo has the following note:
* Platform-specific Hardware and Software objects are retrieved via memoized suppliers. To conserve memory at the
Expand All @@ -70,7 +94,8 @@ public final class SystemInfo
*/
private final oshi.SystemInfo si;

public SystemInfo()
// package private for testing
SystemInfo()
{
si = new oshi.SystemInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static String outputFilename(String base, String description, String extension)
*/
private static Long getProcessId()
{
long pid = new SystemInfo().getPid();
long pid = SystemInfo.instance().getPid();

if (pid >= 0)
return Long.valueOf(pid);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/org/apache/cassandra/utils/SystemInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SystemInfoTest {
public void testGetKernelVersion()
{
Assume.assumeTrue(FBUtilities.isLinux);
Semver kernelVersion = new SystemInfo().getKernelVersion();
Semver kernelVersion = SystemInfo.instance().getKernelVersion();
assertThat(kernelVersion).isGreaterThan(new Semver("0.0.0", Semver.SemverType.LOOSE));
assertThat(kernelVersion).isLessThan(new Semver("100.0.0", Semver.SemverType.LOOSE));
}
Expand Down

0 comments on commit 7163029

Please sign in to comment.