Elasticsearch 设计模式—命令模式

本贴最后更新于 2332 天前,其中的信息可能已经斗转星移

Elasticsearch 设计模式—命令模式

命令模式

不同的书籍不同的文章对于命令模式的解释不尽相同,但是总结起来,思想上缺失大同小异,这里用我自己的理解来
描述一下命令模式:

命令模式一般用来解决客户端/服务端的解耦问题,即将客户端的请求封装成一个抽象的对象,便于服务端将该请求进行参数话处理,从而实现“行为请求者”与“行为实现者”解耦

关于设计模式的详细介绍,请参考: 命令模式

一般来说, 命令模式会涉及到五个角色:

  • 客户端角色(Client):创建一个具体命令(ConcreteCommand)对象并确定其接收者
  • 命令角色(Command):声明了一个给所有具体命令类的抽象接口或抽象类
  • 具体命令角色(ConcreteCommand):定义一个接收者和行为之间的弱耦合
  • 请求者角色(Invoker):负责调用命令对象执行请求,相关的方法叫做行动方法
  • 接收者角色(Receiver):负责具体实施和执行一个请求。任何一个类都可以成为接收者,实施和执行请求的方法叫做行动方法

其 UML 类图如下:

UML 类图

Elasticsearch 对于命令模式的应用

我们都知道 Elasticsearch 在启动的时候能接收许多不同的参数,以此来控制 Elasticsearch 的启动后的行为,Elasticsearch 是如何理解和处理这些参数呢,其实 Elasticsearch
内部就是采用了设计模式—命令模式来解决这些不同参数的问题.

下面我们来分析一下 Elasticsearch 是如何使用命名模式的.

命令角色(Command)

Elasticsearch 有一个抽象类 org.elasticsearch.cli.Command,是用来充当命令模式中的命令角色,其源码如下:

/**
 * An action to execute within a cli.
 */
public abstract class Command implements Closeable {

    /** A description of the command, used in the help output. */
    protected final String description;

    /** The option parser for this command. */
    protected final OptionParser parser = new OptionParser();

    private final OptionSpec<Void> helpOption = parser.acceptsAll(Arrays.asList("h", "help"), "show help").forHelp();
    private final OptionSpec<Void> silentOption = parser.acceptsAll(Arrays.asList("s", "silent"), "show minimal output");
    private final OptionSpec<Void> verboseOption =
        parser.acceptsAll(Arrays.asList("v", "verbose"), "show verbose output").availableUnless(silentOption);

    public Command(String description) {
        this.description = description;
    }

    final SetOnce<Thread> shutdownHookThread = new SetOnce<>();

    /** Parses options for this command from args and executes it. */
    public final int main(String[] args, Terminal terminal) throws Exception {
        if (addShutdownHook()) {
            shutdownHookThread.set(new Thread(() -> {
                try {
                    this.close();
                } catch (final IOException e) {
                    try (
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw)) {
                        e.printStackTrace(pw);
                        terminal.println(sw.toString());
                    } catch (final IOException impossible) {
                        // StringWriter#close declares a checked IOException from the Closeable interface but the Javadocs for StringWriter
                        // say that an exception here is impossible
                        throw new AssertionError(impossible);
                    }
                }
            }));
            Runtime.getRuntime().addShutdownHook(shutdownHookThread.get());
        }

        if (shouldConfigureLoggingWithoutConfig()) {
            // initialize default for es.logger.level because we will not read the log4j2.properties
            final String loggerLevel = System.getProperty("es.logger.level", Level.INFO.name());
            final Settings settings = Settings.builder().put("logger.level", loggerLevel).build();
            LogConfigurator.configureWithoutConfig(settings);
        }

        try {
            mainWithoutErrorHandling(args, terminal);
        } catch (OptionException e) {
            printHelp(terminal);
            terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
            return ExitCodes.USAGE;
        } catch (UserException e) {
            if (e.exitCode == ExitCodes.USAGE) {
                printHelp(terminal);
            }
            terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
            return e.exitCode;
        }
        return ExitCodes.OK;
    }

    /**
     * Indicate whether or not logging should be configured without reading a log4j2.properties. Most commands should do this because we do
     * not configure logging for CLI tools. Only commands that configure logging on their own should not do this.
     *
     * @return true if logging should be configured without reading a log4j2.properties file
     */
    protected boolean shouldConfigureLoggingWithoutConfig() {
        return true;
    }

    /**
     * Executes the command, but all errors are thrown.
     */
    void mainWithoutErrorHandling(String[] args, Terminal terminal) throws Exception {
        final OptionSet options = parser.parse(args);

        if (options.has(helpOption)) {
            printHelp(terminal);
            return;
        }

        if (options.has(silentOption)) {
            terminal.setVerbosity(Terminal.Verbosity.SILENT);
        } else if (options.has(verboseOption)) {
            terminal.setVerbosity(Terminal.Verbosity.VERBOSE);
        } else {
            terminal.setVerbosity(Terminal.Verbosity.NORMAL);
        }

        execute(terminal, options);
    }

    /** Prints a help message for the command to the terminal. */
    private void printHelp(Terminal terminal) throws IOException {
        terminal.println(description);
        terminal.println("");
        printAdditionalHelp(terminal);
        parser.printHelpOn(terminal.getWriter());
    }

    /** Prints additional help information, specific to the command */
    protected void printAdditionalHelp(Terminal terminal) {}

    @SuppressForbidden(reason = "Allowed to exit explicitly from #main()")
    protected static void exit(int status) {
        System.exit(status);
    }

    /**
     * Executes this command.
     *
     * Any runtime user errors (like an input file that does not exist), should throw a {@link UserException}. */
    protected abstract void execute(Terminal terminal, OptionSet options) throws Exception;

    /**
     * Return whether or not to install the shutdown hook to cleanup resources on exit. This method should only be overridden in test
     * classes.
     *
     * @return whether or not to install the shutdown hook
     */
    protected boolean addShutdownHook() {
        return true;
    }

    @Override
    public void close() throws IOException {

    }

}

该类(命令角色)的作用有如下几个:

  • 声明该命令的描述信息,以方便在打印命令参数的时候格式化输出

  • 定义一个 execute()抽象方法,用于其具体子类,即具体的命令的执行

具体命令角色(ConcreteCommand)

Elasticsearch 中有一个 EnvironmentAwareCommand 抽象类,是 Command 的子类,并具体实现了 Command 的 execute()抽象方法方法,具体实现如下:

@Override
    protected void execute(Terminal terminal, OptionSet options) throws Exception {
        final Map<String, String> settings = new HashMap<>();
        for (final KeyValuePair kvp : settingOption.values(options)) {
            if (kvp.value.isEmpty()) {
                throw new UserException(ExitCodes.USAGE, "setting [" + kvp.key + "] must not be empty");
            }
            if (settings.containsKey(kvp.key)) {
                final String message = String.format(
                        Locale.ROOT,
                        "setting [%s] already set, saw [%s] and [%s]",
                        kvp.key,
                        settings.get(kvp.key),
                        kvp.value);
                throw new UserException(ExitCodes.USAGE, message);
            }
            settings.put(kvp.key, kvp.value);
        }

        putSystemPropertyIfSettingIsMissing(settings, "path.conf", "es.path.conf");
        putSystemPropertyIfSettingIsMissing(settings, "path.data", "es.path.data");
        putSystemPropertyIfSettingIsMissing(settings, "path.home", "es.path.home");
        putSystemPropertyIfSettingIsMissing(settings, "path.logs", "es.path.logs");

        execute(terminal, options, createEnv(terminal, settings));
    }

其中 execute 接收两个参数,这里解释一下:

  • Terminal terminal 代表一个终端操作,可读可写

  • OptionSet options 一系列命令的集合

我们来看下具体的命令角色是如何完成调用接收者相应的操作的,源码如下:

@Override
    protected void execute(Terminal terminal, OptionSet options, Environment env) throws UserException {
        if (options.nonOptionArguments().isEmpty() == false) {
            throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
        }
        if (options.has(versionOption)) {
            if (options.has(daemonizeOption) || options.has(pidfileOption)) {
                throw new UserException(ExitCodes.USAGE, "Elasticsearch version option is mutually exclusive with any other option");
            }
            terminal.println("Version: " + org.elasticsearch.Version.CURRENT
                    + ", Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date()
                    + ", JVM: " + JvmInfo.jvmInfo().version());
            return;
        }

        final boolean daemonize = options.has(daemonizeOption);
        final Path pidFile = pidfileOption.value(options);
        final boolean quiet = options.has(quietOption);

        try {
            init(daemonize, pidFile, quiet, env);
        } catch (NodeValidationException e) {
            throw new UserException(ExitCodes.CONFIG, e.getMessage());
        }
    }

该方法中有一个 init(daemonize, pidFile, quiet, env)方法,该方法则是由命令角色中的接收者角色来完成,那这个接收者角色是谁呢?

接收者角色

我们继续看 init(daemonize, pidFile, quiet, env)的方法是谁在负责调度呢? 源码如下:

void init(final boolean daemonize, final Path pidFile, final boolean quiet, Environment initialEnv)
        throws NodeValidationException, UserException {
        try {
            Bootstrap.init(!daemonize, pidFile, quiet, initialEnv);
        } catch (BootstrapException | RuntimeException e) {
            // format exceptions to the console in a special way
            // to avoid 2MB stacktraces from guice, etc.
            throw new StartupException(e);
        }
    }

可以看到,真正处理 init()方法参数的核心方法是:Bootstrap.init(!daemonize, pidFile, quiet, initialEnv);
也就是说,真正的接收者角色是 Bootstrap,它在接收到具体的命令对象的时候,负责来完成 Elasticsearch 环境的初始化工作.

请求者(Invoker)角色

我们再回顾下请求者(Invoker)角色的作用:负责调用命令对象执行请求

那么在 Elasticsearch 中,谁负责调用命令对象呢?

答案是:Elasticsearch 类.

Elasticsearch 既然是请求者(Invoker)角色,那它是如何持有命令角色呢?这里 Elasticsearch 的处理方式是继承具体的命令角色,这样其自身
就可以有充当具体命令的角色,又充当请求者角色,而且 Elasticsearch 还完成了组装命令对象和接收者.因此这里
Elasticsearch 既是请求者角色又是客户端角色.

解决问题

如果细看 Elasticsearch 来解析和使用这些参数的整个过程的话,会发现整个调用链比较长,处理过程比较复杂,而且会对某些参数进行处理
比如有些参数是帮助参数(-help),则这些参数需要打印其其他具体命令参数的使用描述信息,有些参数是控制启动行为的参数,比如-d,则需要
对其进行处理,而且在整个调用过程中,会有一些内部日志的记录以及缓存.使用命令模式则很好的解决了这些问题.

由此可见命令模式在下面这些场景中可以按需使用:

  • 整个调用过程比较繁杂,或者存在多处这种调用。这时,使用 Command 类对该调用加以封装,便于功能的再利用
  • 调用前后需要对调用参数进行某些处理。
  • 调用前后需要进行某些额外处理,比如日志,缓存,记录历史操作等

命令模式的使用效果

  • 将调用操作的对象和知道如何实现该操作的对象解耦
  • Command 是头等对象。他们可以像其他对象一样被操作和扩展
  • 你可将多个命令装配成一个复合命令
  • 增加新的 Command 很容易,因为这无需改变现有的类

参考

  • 设计模式

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

    198 引用 • 120 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...