Repository navigation
[ISSUE #11284] fix(store): do not report plugin init failures as class-not-found #11303
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new file was committed with mode
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 143ae96 — the file is now committed with mode 100644, matching the rest of |
||
| * 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.rocketmq.store.plugin; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.apache.rocketmq.common.BrokerConfig; | ||
| import org.apache.rocketmq.store.MessageStore; | ||
| import org.apache.rocketmq.store.QueryMessageResult; | ||
| import org.apache.rocketmq.store.config.MessageStoreConfig; | ||
| import org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage; | ||
| import org.apache.rocketmq.store.timer.rocksdb.TimerMessageRocksDBStore; | ||
| import org.apache.rocketmq.store.transaction.TransMessageRocksDBStore; | ||
| import org.junit.Test; | ||
|
|
||
| public class MessageStoreFactoryTest { | ||
|
|
||
| private static final String MISSING_PLUGIN_CLASS = | ||
| "org.apache.rocketmq.store.plugin.NoSuchPluginClassSurelyMissing"; | ||
| private static final String THROWING_PLUGIN = | ||
| MessageStoreFactoryTest.class.getName() + "$ThrowingPlugin"; | ||
| private static final String OUTER_PLUGIN = | ||
| MessageStoreFactoryTest.class.getName() + "$OuterPlugin"; | ||
| private static final String INNER_PLUGIN = | ||
| MessageStoreFactoryTest.class.getName() + "$InnerPlugin"; | ||
|
|
||
| private MessageStorePluginContext buildContext(String plugin) { | ||
| BrokerConfig brokerConfig = new BrokerConfig(); | ||
| brokerConfig.setMessageStorePlugIn(plugin); | ||
| return new MessageStorePluginContext(new MessageStoreConfig(), null, null, brokerConfig, null); | ||
| } | ||
|
|
||
| @Test | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assertThatThrownBy(() -> MessageStoreFactory.build(context, null))
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("Failed to initialize message store plugin")
.hasMessageContaining("NoSuchPluginClassSurelyMissing")
.hasCauseInstanceOf(ClassNotFoundException.class);This also removes the risk of a silently passing test if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 143ae96 — both |
||
| public void testPluginClassNotExist() throws Exception { | ||
| MessageStorePluginContext context = buildContext(MISSING_PLUGIN_CLASS); | ||
| assertThatThrownBy(() -> MessageStoreFactory.build(context, null)) | ||
| .isInstanceOf(RuntimeException.class) | ||
| .hasMessageContaining("Failed to initialize message store plugin") | ||
| .hasMessageContaining("NoSuchPluginClassSurelyMissing") | ||
| .hasCauseInstanceOf(ClassNotFoundException.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPluginConstructorThrows() throws Exception { | ||
| MessageStorePluginContext context = buildContext(THROWING_PLUGIN); | ||
| assertThatThrownBy(() -> MessageStoreFactory.build(context, null)) | ||
| .isInstanceOf(RuntimeException.class) | ||
| .hasMessageContaining("Failed to initialize message store plugin") | ||
| .hasMessageContaining(THROWING_PLUGIN) | ||
| .hasMessageNotContaining("not found") | ||
| .hasCauseInstanceOf(InvocationTargetException.class) | ||
| .hasRootCauseInstanceOf(IllegalStateException.class) | ||
| .hasRootCauseMessage("boom"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBuildSinglePlugin() throws Exception { | ||
| MessageStorePluginContext context = buildContext(INNER_PLUGIN); | ||
| MessageStore store = MessageStoreFactory.build(context, null); | ||
| assertThat(store).isInstanceOf(InnerPlugin.class); | ||
| assertThat(((InnerPlugin) store).getNext()).isNull(); | ||
| assertThat(((InnerPlugin) store).context).isSameAs(context); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBuildChainsPluginsInConfigOrder() throws Exception { | ||
| // no space after the comma: build splits the config on "," without trimming entries | ||
| MessageStorePluginContext context = buildContext(OUTER_PLUGIN + "," + INNER_PLUGIN); | ||
| MessageStore store = MessageStoreFactory.build(context, null); | ||
| assertThat(store).isInstanceOf(OuterPlugin.class); | ||
| AbstractPluginMessageStore outer = (AbstractPluginMessageStore) store; | ||
| assertThat(outer.getNext()).isInstanceOf(InnerPlugin.class); | ||
| AbstractPluginMessageStore inner = (AbstractPluginMessageStore) outer.getNext(); | ||
| assertThat(inner.getNext()).isNull(); | ||
| assertThat(outer.context).isSameAs(context); | ||
| assertThat(inner.context).isSameAs(context); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Javadoc comment
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rephrased in 143ae96 exactly along those lines — the comment now says |
||
| @Test | ||
| public void testNullPluginConfigReturnsOriginalStore() throws Exception { | ||
| MessageStorePluginContext context = buildContext(null); | ||
| MessageStore delegate = new InnerPlugin(context, null); | ||
| assertThat(MessageStoreFactory.build(context, delegate)).isSameAs(delegate); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBlankPluginConfigReturnsOriginalStore() throws Exception { | ||
| MessageStorePluginContext context = buildContext(" "); | ||
| MessageStore delegate = new InnerPlugin(context, null); | ||
| assertThat(MessageStoreFactory.build(context, delegate)).isSameAs(delegate); | ||
| } | ||
|
|
||
| /** | ||
| * Test-local plugin base. AbstractPluginMessageStore does not delegate these methods, | ||
| * so concrete plugins used by the tests fill the gap here. | ||
| */ | ||
| private static class TestPlugin extends AbstractPluginMessageStore { | ||
| TestPlugin(MessageStorePluginContext context, MessageStore next) { | ||
| super(context, next); | ||
| } | ||
|
|
||
| @Override | ||
| public QueryMessageResult queryMessage(String topic, String key, int maxNum, long begin, long end, | ||
| String indexType, String lastKey) { | ||
| return next.queryMessage(topic, key, maxNum, begin, end, indexType, lastKey); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<QueryMessageResult> queryMessageAsync(String topic, String key, int maxNum, | ||
| long begin, long end, String indexType, String lastKey) { | ||
| return next.queryMessageAsync(topic, key, maxNum, begin, end, indexType, lastKey); | ||
| } | ||
|
|
||
| @Override | ||
| public MessageRocksDBStorage getMessageRocksDBStorage() { | ||
| return next == null ? null : next.getMessageRocksDBStorage(); | ||
| } | ||
|
|
||
| @Override | ||
| public TimerMessageRocksDBStore getTimerMessageRocksDBStore() { | ||
| return next == null ? null : next.getTimerMessageRocksDBStore(); | ||
| } | ||
|
|
||
| @Override | ||
| public TransMessageRocksDBStore getTransMessageRocksDBStore() { | ||
| return next == null ? null : next.getTransMessageRocksDBStore(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setTimerMessageRocksDBStore(TimerMessageRocksDBStore timerMessageRocksDBStore) { | ||
| next.setTimerMessageRocksDBStore(timerMessageRocksDBStore); | ||
| } | ||
|
|
||
| @Override | ||
| public void setTransMessageRocksDBStore(TransMessageRocksDBStore transMessageRocksDBStore) { | ||
| next.setTransMessageRocksDBStore(transMessageRocksDBStore); | ||
| } | ||
| } | ||
|
|
||
| public static class OuterPlugin extends TestPlugin { | ||
| public OuterPlugin(MessageStorePluginContext context, MessageStore next) { | ||
| super(context, next); | ||
| } | ||
| } | ||
|
|
||
| public static class InnerPlugin extends TestPlugin { | ||
| public InnerPlugin(MessageStorePluginContext context, MessageStore next) { | ||
| super(context, next); | ||
| } | ||
| } | ||
|
|
||
| public static class ThrowingPlugin extends TestPlugin { | ||
| public ThrowingPlugin(MessageStorePluginContext context, MessageStore next) { | ||
| super(context, next); | ||
| throw new IllegalStateException("boom"); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: since the original message hinted at the classpath, it may help operators to log at ERROR before throwing so that the cause chain shows up in the broker log even if the
RuntimeExceptionis later re-wrapped. A one-liner viaorg.apache.rocketmq.logging.org.slf4j.Loggerwould be consistent with other classes in this package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied in 143ae96 —
buildnow logs at ERROR via the store logger (LoggerName.STORE_LOGGER_NAME,org.apache.rocketmq.logging.org.slf4j) before rethrowing, so the cause chain lands in the broker log even when theRuntimeExceptionis re-wrapped upstream.