Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@

import java.io.IOException;
import java.lang.reflect.Constructor;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
import org.apache.rocketmq.store.MessageStore;

public final class MessageStoreFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggerName.STORE_LOGGER_NAME);

public static MessageStore build(MessageStorePluginContext context,
MessageStore messageStore) throws IOException {
String plugin = context.getBrokerConfig().getMessageStorePlugIn();
Expand All @@ -36,7 +41,8 @@ public static MessageStore build(MessageStorePluginContext context,
AbstractPluginMessageStore pluginMessageStore = construct.newInstance(context, messageStore);
messageStore = pluginMessageStore;
} catch (Throwable e) {
throw new RuntimeException("Initialize plugin's class: " + pluginClass + " not found!", e);
LOGGER.error("Failed to initialize message store plugin: {}", pluginClass, e);
throw new RuntimeException("Failed to initialize message store plugin: " + pluginClass, e);

Copy link
Copy Markdown

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 RuntimeException is later re-wrapped. A one-liner via org.apache.rocketmq.logging.org.slf4j.Logger would be consistent with other classes in this package.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 143ae96 — build now 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 the RuntimeException is re-wrapped upstream.

}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new file was committed with mode 100755 (executable). Java source files in this repo are 100644 — please chmod 644 and re-commit so the file mode matches the rest of store/src/test/java.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 store/src/test/java. Good catch, thank you!

* 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try { build(...); fail(...); } catch (RuntimeException e) { assertThat(...) } pattern can be replaced by AssertJ's assertThatThrownBy for a cleaner and less error-prone form, e.g.:

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 fail(...) ever gets refactored out. Same applies to testPluginConstructorThrows.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 143ae96 — both testPluginClassNotExist and testPluginConstructorThrows now use assertThatThrownBy chains. For the throwing-constructor case the chain also carries the former root-cause assertions via hasCauseInstanceOf(InvocationTargetException.class) + hasRootCauseInstanceOf(IllegalStateException.class) + hasRootCauseMessage("boom"). All 6 tests pass locally (mvn -pl store -am -Dtest=MessageStoreFactoryTest test).

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);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Javadoc comment "AbstractPluginMessageStore leaves a few MessageStore methods unimplemented in this branch" is ambiguous — "this branch" reads like the PR branch, but the intent is presumably "in the current develop state of AbstractPluginMessageStore". Consider rephrasing to something like "AbstractPluginMessageStore does not delegate these methods; concrete plugins used by the tests fill the gap here." so future readers aren't confused.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rephrased in 143ae96 exactly along those lines — the comment now says AbstractPluginMessageStore does not delegate these methods, so concrete plugins used by the tests fill the gap here.

@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");
}
}
}
Loading