Skip to content

Commit

Permalink
Replace generated cleaner code with concrete implementation (elastic#…
Browse files Browse the repository at this point in the history
…13430)

* Replace generated cleaner code with concrete implementation

No need to to perform codegen to create JDK specific compatible classes to
call the ByteBuffer cleaner, as we are dropping support for Java 8.

* Remove dead/redundant java 8 code from LogstashJavaCompat

Removes setupByteBuffer, specific test for Java9 and above and updates memory monitor
unit test to only test for values relevant to Java>1.8

* Renamed version utility class to reflect use after removal of methods

Also fixed some dead imports
  • Loading branch information
robbavey authored Jan 19, 2022
1 parent 93f37b9 commit 4f96a94
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 129 deletions.
45 changes: 45 additions & 0 deletions logstash-core/src/main/java/org/logstash/JavaVersionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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 org.logstash;

/**
* Helper Class for dealing with Java versions
*/
public final class JavaVersionUtils {

/**
* Identifies whether we are running on a version greater than or equal to the version parameter specified.
* @param version The version to test against. This must be the Major version of Java
* @return True if running on Java whose major version is greater than or equal to the
* specified version.
*/
public static boolean isJavaAtLeast(int version) {
final String value = System.getProperty("java.specification.version");
final int actualVersion;
// Java specification version prior to Java 9 were of the format `1.X`, and after the format `X`
// See https://openjdk.java.net/jeps/223
if (value.startsWith("1.")) {
actualVersion = Integer.parseInt(value.split("\\.")[1]);
} else {
actualVersion = Integer.parseInt(value);
}
return actualVersion >= version;
}
}
112 changes: 0 additions & 112 deletions logstash-core/src/main/java/org/logstash/LogstashJavaCompat.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.logstash.ackedqueue.io;

import java.nio.MappedByteBuffer;

import sun.misc.Unsafe;
import java.lang.reflect.Field;

public class ByteBufferCleanerImpl implements ByteBufferCleaner {

private final Unsafe unsafe;

public ByteBufferCleanerImpl() {
try {
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
unsafe = (Unsafe) unsafeField.get(null);
}catch (Exception e){
throw new IllegalStateException(e);
}
}

@Override
public void clean(MappedByteBuffer buffer) {
unsafe.invokeCleaner(buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import org.logstash.LogstashJavaCompat;

import org.logstash.ackedqueue.SequencedList;

/**
Expand All @@ -43,8 +43,7 @@ public final class MmapPageIOV1 implements PageIO {
/**
* Cleaner function for forcing unmapping of backing {@link MmapPageIOV1#buffer}.
*/
private static final ByteBufferCleaner BUFFER_CLEANER =
LogstashJavaCompat.setupBytebufferCleaner();
private static final ByteBufferCleaner BUFFER_CLEANER = new ByteBufferCleanerImpl();

private final File file;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.zip.CRC32;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.logstash.LogstashJavaCompat;
import org.logstash.ackedqueue.SequencedList;

/**
Expand All @@ -54,8 +53,7 @@ public final class MmapPageIOV2 implements PageIO {
/**
* Cleaner function for forcing unmapping of backing {@link MmapPageIOV2#buffer}.
*/
private static final ByteBufferCleaner BUFFER_CLEANER =
LogstashJavaCompat.setupBytebufferCleaner();
private static final ByteBufferCleaner BUFFER_CLEANER = new ByteBufferCleanerImpl();

private final File file;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.logstash.Logstash;
import org.logstash.LogstashJavaCompat;
import org.logstash.JavaVersionUtils;

public class ProcessMonitor {

Expand Down Expand Up @@ -119,7 +118,7 @@ private short getSystemCpuLoad() {
*/
private static Method getCpuLoadMethod(){
try{
String methodName = (LogstashJavaCompat.isJavaAtLeast(14)) ? "getCpuLoad" : "getSystemCpuLoad";
String methodName = (JavaVersionUtils.isJavaAtLeast(14)) ? "getCpuLoad" : "getSystemCpuLoad";
return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName);
} catch (ReflectiveOperationException e){
LOGGER.warn("OperatingSystemMXBean CPU load method not available, CPU load will not be measured", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package org.logstash.instruments.monitors;

import org.junit.Test;
import org.logstash.LogstashJavaCompat;
import org.logstash.instrument.monitors.MemoryMonitor;

import java.util.Map;
Expand All @@ -38,11 +37,7 @@ public class MemoryMonitorTest {
public void testEachHeapSpaceRepresented() {
Map<String, Map<String, Object>> heap = MemoryMonitor.detect(MemoryMonitor.Type.All).getHeap();
assertThat(heap, notNullValue());
if (LogstashJavaCompat.IS_JAVA_9_OR_GREATER) {
assertThat(heap.keySet(), hasItems("G1 Old Gen", "G1 Survivor Space", "G1 Eden Space"));
} else {
assertThat(heap.keySet(), hasItems("PS Survivor Space", "PS Old Gen", "PS Eden Space"));
}
assertThat(heap.keySet(), hasItems("G1 Old Gen", "G1 Survivor Space", "G1 Eden Space"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.logstash.LogstashJavaCompat;
import org.logstash.secret.SecretIdentifier;
import org.logstash.secret.store.SecretStore;
import org.logstash.secret.store.SecretStoreException;
Expand All @@ -48,7 +47,6 @@
import java.nio.file.attribute.PosixFilePermission;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down

0 comments on commit 4f96a94

Please sign in to comment.