You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "eolivelli (via GitHub)" <gi...@apache.org> on 2023/02/01 14:04:30 UTC
[GitHub] [pulsar] eolivelli commented on a diff in pull request #19364: [fix][broker] Execute per-topic entry filters with the same classloader
eolivelli commented on code in PR #19364:
URL: https://github.com/apache/pulsar/pull/19364#discussion_r1093258954
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java:
##########
@@ -2659,7 +2659,8 @@ public CompletableFuture<Void> onPoliciesUpdate(Policies data) {
updatePublishDispatcher();
- this.updateResourceGroupLimiter(Optional.of(data));
+ final Optional<Policies> optionalData = Optional.of(data);
+ this.updateResourceGroupLimiter(optionalData);
Review Comment:
this change seems unrelated
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java:
##########
@@ -1632,9 +1592,12 @@ public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
}, executor());
return null;
});
- } catch (PulsarServerException e) {
- log.warn("Failed to create topic {}-{}", topic, e.getMessage());
+ } catch (Throwable e) {
Review Comment:
Catching Throwable is usually a code smell (and you should always deal with InterruptedException unfortunately.
Maybe we can move to RuntimeException here
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterProvider.java:
##########
@@ -177,12 +180,55 @@ private static EntryFilterWithClassLoader load(EntryFilterMetaData metadata,
}
EntryFilter pi = (EntryFilter) filter;
return new EntryFilterWithClassLoader(pi, ncl);
- } catch (Exception e) {
+ } catch (Throwable e) {
if (e instanceof IOException) {
throw (IOException) e;
}
- log.error("Failed to load class {}", def.getEntryFilterClass(), e);
+ log.error("Failed to load class {}", metadata.getDefinition().getEntryFilterClass(), e);
throw new IOException(e);
}
}
+
+ private NarClassLoader getNarClassLoader(Path archivePath) {
+ return cachedClassLoaders.get(classLoaderKey(archivePath));
+ }
+
+ private NarClassLoader loadNarClassLoader(Path archivePath) {
+ final String absolutePath = classLoaderKey(archivePath);
+ return cachedClassLoaders
+ .computeIfAbsent(absolutePath, narFilePath -> {
+ try {
+ final File narFile = archivePath.toAbsolutePath().toFile();
+ return NarClassLoaderBuilder.builder()
+ .narFile(narFile)
+ .parentClassLoader(EntryFilter.class.getClassLoader())
+ .extractionDirectory(serviceConfiguration.getNarExtractionDirectory())
+ .build();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ });
+ }
+
+ private static String classLoaderKey(Path archivePath) {
+ return archivePath.toFile().getAbsolutePath();
Review Comment:
is archivePath.toFile().getAbsolutePath() potentially performing some IO operation ? (metadata access?)
probably archivePath.toString() is enough in this context
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterProvider.java:
##########
@@ -37,74 +43,85 @@
import org.apache.pulsar.common.util.ObjectMapperFactory;
@Slf4j
-public class EntryFilterProvider {
+public class EntryFilterProvider implements AutoCloseable {
@VisibleForTesting
static final String ENTRY_FILTER_DEFINITION_FILE = "entry_filter";
- /**
- * create entry filter instance.
- */
- public static ImmutableMap<String, EntryFilterWithClassLoader> createEntryFilters(ServiceConfiguration conf,
- EntryFilters entryFilters)
+ private final ServiceConfiguration serviceConfiguration;
+ @VisibleForTesting
+ protected Map<String, EntryFilterMetaData> definitions;
+ @VisibleForTesting
+ protected Map<String, NarClassLoader> cachedClassLoaders;
Review Comment:
is it required that we refer to NarClassLoader here ? can it be simply a "ClassLoader" ?
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/testcontext/MockEntryFilterProvider.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.broker.testcontext;
+
+import lombok.SneakyThrows;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.service.plugin.EntryFilterDefinition;
+import org.apache.pulsar.broker.service.plugin.EntryFilterMetaData;
+import org.apache.pulsar.broker.service.plugin.EntryFilterProvider;
+import org.apache.pulsar.common.nar.NarClassLoader;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class MockEntryFilterProvider extends EntryFilterProvider {
+
+ public MockEntryFilterProvider(ServiceConfiguration config) throws IOException {
+ super(config);
+ }
+
+ @SneakyThrows
+ public void setMockEntryFilters(EntryFilterDefinition... defs) {
+ definitions = new HashMap<>();
+ cachedClassLoaders = new HashMap<>();
+ brokerEntryFilters = new ArrayList<>();
+
+ for (EntryFilterDefinition def : defs) {
+ final String name = def.getName();
+ final EntryFilterMetaData meta = new EntryFilterMetaData();
+ meta.setDefinition(def);
+ meta.setArchivePath(Path.of(name));
+ definitions.put(name, meta);
+ final NarClassLoader ncl = mock(NarClassLoader.class);
Review Comment:
if cachedClassLoaders is not required to old " NarClassLoader" entries that maybe this code can be simplified a lot.
and you can create a new URLClassloader with Thread.currentThread().getContextClassLoader() as parent
and so we continue to remove mocks
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterProvider.java:
##########
@@ -37,74 +43,85 @@
import org.apache.pulsar.common.util.ObjectMapperFactory;
@Slf4j
-public class EntryFilterProvider {
+public class EntryFilterProvider implements AutoCloseable {
@VisibleForTesting
static final String ENTRY_FILTER_DEFINITION_FILE = "entry_filter";
- /**
- * create entry filter instance.
- */
- public static ImmutableMap<String, EntryFilterWithClassLoader> createEntryFilters(ServiceConfiguration conf,
- EntryFilters entryFilters)
+ private final ServiceConfiguration serviceConfiguration;
+ @VisibleForTesting
+ protected Map<String, EntryFilterMetaData> definitions;
+ @VisibleForTesting
+ protected Map<String, NarClassLoader> cachedClassLoaders;
+ @VisibleForTesting
+ protected List<EntryFilter> brokerEntryFilters;
+
+ public EntryFilterProvider(ServiceConfiguration conf) throws IOException {
+ this.serviceConfiguration = conf;
+ initialize();
+ initializeBrokerEntryFilters();
+ }
+
+ protected void initializeBrokerEntryFilters() throws IOException {
+ if (!serviceConfiguration.getEntryFilterNames().isEmpty()) {
+ brokerEntryFilters = loadEntryFilters(serviceConfiguration.getEntryFilterNames());
+ } else {
+ brokerEntryFilters = Collections.emptyList();
+ }
+ }
+
+ public List<EntryFilter> loadEntryFiltersForPolicy(EntryFilters policy)
throws IOException {
- EntryFilterDefinitions definitions = searchForEntryFilters(conf.getEntryFiltersDirectory(),
- conf.getNarExtractionDirectory());
- ImmutableMap.Builder<String, EntryFilterWithClassLoader> builder = ImmutableMap.builder();
- for (String filterName : entryFilters.getEntryFilterNames().split(",")) {
- EntryFilterMetaData metaData = definitions.getFilters().get(filterName);
- if (null == metaData) {
- throw new RuntimeException("No entry filter is found for name `" + filterName
- + "`. Available entry filters are : " + definitions.getFilters());
- }
- EntryFilterWithClassLoader filter;
- filter = load(metaData, conf.getNarExtractionDirectory());
- if (filter != null) {
- builder.put(filterName, filter);
- }
- log.info("Successfully loaded entry filter for name `{}` from topic policy", filterName);
+ final String names = policy.getEntryFilterNames();
+ if (StringUtils.isBlank(names)) {
+ return Collections.emptyList();
}
- return builder.build();
+ final List<String> entryFilterList = Arrays.stream(names.split(","))
+ .filter(n -> StringUtils.isNotBlank(n))
+ .toList();
+ return loadEntryFilters(entryFilterList);
}
- public static ImmutableMap<String, EntryFilterWithClassLoader> createEntryFilters(
- ServiceConfiguration conf) throws IOException {
- EntryFilterDefinitions definitions = searchForEntryFilters(conf.getEntryFiltersDirectory(),
- conf.getNarExtractionDirectory());
- ImmutableMap.Builder<String, EntryFilterWithClassLoader> builder = ImmutableMap.builder();
- for (String filterName : conf.getEntryFilterNames()) {
- EntryFilterMetaData metaData = definitions.getFilters().get(filterName);
+ private List<EntryFilter> loadEntryFilters(Collection<String> entryFilterNames)
+ throws IOException {
+ ImmutableMap.Builder<String, EntryFilter> builder = ImmutableMap.builder();
+ for (String filterName : entryFilterNames) {
+ EntryFilterMetaData metaData = definitions.get(filterName);
if (null == metaData) {
throw new RuntimeException("No entry filter is found for name `" + filterName
- + "`. Available entry filters are : " + definitions.getFilters());
+ + "`. Available entry filters are : " + definitions.keySet());
}
- EntryFilterWithClassLoader filter;
- filter = load(metaData, conf.getNarExtractionDirectory());
- if (filter != null) {
- builder.put(filterName, filter);
- }
- log.info("Successfully loaded entry filter for name `{}`", filterName);
+ final EntryFilter filterWithClassLoader = load(metaData);
Review Comment:
nit: rename to "entryFilter"
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterWithClassLoader.java:
##########
@@ -18,12 +18,15 @@
*/
package org.apache.pulsar.broker.service.plugin;
+import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
+import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.mledger.Entry;
import org.apache.pulsar.common.nar.NarClassLoader;
@Slf4j
+@ToString
public class EntryFilterWithClassLoader implements EntryFilter {
Review Comment:
do we still need this class ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org