Skip to content

Commit

Permalink
Allow to build Pulsar with JDK11 and -Dmaven.compiler.release=8 (apac…
Browse files Browse the repository at this point in the history
…he#9580)

* Allow to build Pulsar with JDK11 and -Dmaven.compiler.release=8

* fix style

Co-authored-by: Enrico Olivelli <eolivelli@apache.org>
  • Loading branch information
eolivelli and eolivelli authored Feb 12, 2021
1 parent 927a270 commit 731c18f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.sun.org.apache.xml.internal.serialize.OutputFormat.Defaults.Encoding;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
Expand Down Expand Up @@ -93,11 +93,11 @@ public void testRecoveryIndex() throws Exception {
config.setManagedLedgerInterceptor(interceptor);
ManagedLedger ledger = factory.open("my_recovery_index_test_ledger", config);

ledger.addEntry("dummy-entry-1".getBytes(Encoding), MOCK_BATCH_SIZE);
ledger.addEntry("dummy-entry-1".getBytes(StandardCharsets.UTF_8), MOCK_BATCH_SIZE);

ManagedCursor cursor = ledger.openCursor("c1");

ledger.addEntry("dummy-entry-2".getBytes(Encoding), MOCK_BATCH_SIZE);
ledger.addEntry("dummy-entry-2".getBytes(StandardCharsets.UTF_8), MOCK_BATCH_SIZE);

assertEquals(((ManagedLedgerInterceptorImpl) ledger.getManagedLedgerInterceptor()).getIndex(), MOCK_BATCH_SIZE * 2 - 1);

Expand Down Expand Up @@ -141,7 +141,7 @@ public void testFindPositionByIndex() throws Exception {

long firstLedgerId = -1;
for (int i = 0; i < maxEntriesPerLedger; i++) {
firstLedgerId = ((PositionImpl) ledger.addEntry("dummy-entry".getBytes(Encoding), MOCK_BATCH_SIZE)).getLedgerId();
firstLedgerId = ((PositionImpl) ledger.addEntry("dummy-entry".getBytes(StandardCharsets.UTF_8), MOCK_BATCH_SIZE)).getLedgerId();
}

assertEquals(((ManagedLedgerInterceptorImpl) ledger.getManagedLedgerInterceptor()).getIndex(), 9);
Expand All @@ -156,7 +156,7 @@ public void testFindPositionByIndex() throws Exception {
// roll over ledger
long secondLedgerId = -1;
for (int i = 0; i < maxEntriesPerLedger; i++) {
secondLedgerId = ((PositionImpl) ledger.addEntry("dummy-entry".getBytes(Encoding), MOCK_BATCH_SIZE)).getLedgerId();
secondLedgerId = ((PositionImpl) ledger.addEntry("dummy-entry".getBytes(StandardCharsets.UTF_8), MOCK_BATCH_SIZE)).getLedgerId();
}
assertEquals(((ManagedLedgerInterceptorImpl) ledger.getManagedLedgerInterceptor()).getIndex(), 19);
assertNotEquals(firstLedgerId, secondLedgerId);
Expand All @@ -174,7 +174,7 @@ public void testFindPositionByIndex() throws Exception {

long thirdLedgerId = -1;
for (int i = 0; i < maxEntriesPerLedger; i++) {
thirdLedgerId = ((PositionImpl) ledger.addEntry("dummy-entry".getBytes(Encoding), MOCK_BATCH_SIZE)).getLedgerId();
thirdLedgerId = ((PositionImpl) ledger.addEntry("dummy-entry".getBytes(StandardCharsets.UTF_8), MOCK_BATCH_SIZE)).getLedgerId();
}
assertEquals(((ManagedLedgerInterceptorImpl) ledger.getManagedLedgerInterceptor()).getIndex(), 29);
assertNotEquals(secondLedgerId, thirdLedgerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Objects;
import lombok.Getter;
import org.apache.pulsar.client.api.MessageId;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
* A MessageId implementation that contains a map of <partitionName, MessageId>.
Expand All @@ -42,7 +41,7 @@ public class MultiMessageIdImpl implements MessageId {
// https://github.com/apache/pulsar/issues/4940
@Override
public byte[] toByteArray() {
throw new NotImplementedException();
throw new UnsupportedOperationException();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
package org.apache.pulsar.common.stats;

import com.google.common.collect.Maps;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings({"restriction", "checkstyle:JavadocType"})
@SuppressWarnings({"checkstyle:JavadocType"})
public class JvmDefaultGCMetricsLogger implements JvmGCMetricsLogger {

private static final Logger log = LoggerFactory.getLogger(JvmDefaultGCMetricsLogger.class);
Expand All @@ -36,19 +40,46 @@ public class JvmDefaultGCMetricsLogger implements JvmGCMetricsLogger {
private volatile long accumulatedFullGcTime = 0;
private volatile long currentFullGcTime = 0;

@SuppressWarnings("restriction")
private static sun.management.HotspotRuntimeMBean runtime;
private static Object /*sun.management.HotspotRuntimeMBean*/ runtime;
private static Method getTotalSafepointTimeHandle;
private static Method getSafepointCountHandle;

private Map<String, GCMetrics> gcMetricsMap = Maps.newHashMap();

static {
try {
runtime = sun.management.ManagementFactoryHelper.getHotspotRuntimeMBean();
} catch (Exception e) {
runtime = Class.forName("sun.management.ManagementFactoryHelper")
.getMethod("getHotspotRuntimeMBean")
.invoke(null);
getTotalSafepointTimeHandle = runtime.getClass().getMethod("getTotalSafepointTime");
getTotalSafepointTimeHandle.setAccessible(true);
getSafepointCountHandle = runtime.getClass().getMethod("getSafepointCount");
getSafepointCountHandle.setAccessible(true);

// try to use the methods
getTotalSafepointTimeHandle.invoke(runtime);
getSafepointCountHandle.invoke(runtime);
} catch (Throwable e) {
log.warn("Failed to get Runtime bean", e);
}
}

@SneakyThrows
static long getTotalSafepointTime() {
if (getTotalSafepointTimeHandle == null) {
return -1;
}
return (long) getTotalSafepointTimeHandle.invoke(runtime);
}

@SneakyThrows
static long getSafepointCount() {
if (getTotalSafepointTimeHandle == null) {
return -1;
}
return (long) getSafepointCountHandle.invoke(runtime);
}

/**
* Metrics for the Garbage Collector.
*/
Expand Down Expand Up @@ -92,8 +123,8 @@ public void refresh() {
* that the application has been stopped for safepoint operations.
* http://www.docjar.com/docs/api/sun/management/HotspotRuntimeMBean.html
*/
long newSafePointTime = runtime.getTotalSafepointTime();
long newSafePointCount = runtime.getSafepointCount();
long newSafePointTime = getTotalSafepointTime();
long newSafePointCount = getSafepointCount();
currentFullGcTime = newSafePointTime - accumulatedFullGcTime;
currentFullGcCount = newSafePointCount - accumulatedFullGcCount;
accumulatedFullGcTime = newSafePointTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 org.apache.pulsar.common.stats;

import lombok.extern.slf4j.Slf4j;

import org.testng.annotations.Test;

import static org.testng.Assert.assertNotEquals;


@Slf4j
public class JvmDefaultGCMetricsLoggerTest {

@Test
public void testInvokeJVMInternals() {
long safePointCount = JvmDefaultGCMetricsLogger.getSafepointCount();
long totalSafePointTime = JvmDefaultGCMetricsLogger.getTotalSafepointTime();
log.info("safePointCount {} totalSafePointTime {}", safePointCount, totalSafePointTime);
assertNotEquals(safePointCount, -1);
assertNotEquals(totalSafePointTime, -1);
}
}

0 comments on commit 731c18f

Please sign in to comment.