浅谈软件工程中的Shim

什么是Shim

Shim一词的原本含义是“垫片”或者“楔子”,而首先将这个词应用到软件工程领域的似乎是微软。根据Wikipedia的总结:

A shim is a library that transparently intercepts API calls and changes the arguments passed, handles the operation itself or redirects the operation elsewhere. Shims can be used to support an old API in a newer environment, or a new API in an older environment. Shims can also be used for running programs on different software platforms than they were developed for.

按照这个释义,Shim是一种(小型的)库,负责透明地拦截API调用,并更改其参数,或将其转发至其他组件,或者自行处理。它用于在新环境中支持旧API(或反过来),以及使程序能够在非特定支持的平台上运行。

熟悉Docker的看官可能会立即想起该体系中的containerd-shim组件,它作为containerd与容器运行时交互的中间层发挥作用,并且符合上文的释义。

具体到不同API版本这一方面的话,我们可以参考一下Flink(当然其他很多开源组件也同理)的设计思路。

Flink Hive Catalog中的Shims

我们知道,Flink 1.14支持从1.0.0~3.1.2各版本的Hive,而横跨这么多版本的Hive API底层逻辑势必不会完全一致,如果将全部版本的Hive依赖都引入进来,也一定会造成冲突,这里就需要Shims发挥作用。在代码中,HiveShim是一个接口,定义了所有需要做兼容性支持的方法,如下图所示。

该接口的实现类就从HiveShimV100一直命名至HiveShimV312,每个类都会override对应版本出现变更的方法。例如,alterPartition()方法对应1.0.0版本的实现是:

    @Override
    public void alterPartition(
            IMetaStoreClient client, String databaseName, String tableName, Partition partition)
            throws InvalidOperationException, MetaException, TException {
        String errorMsg = "Failed to alter partition for table %s in database %s";
        try {
            Method method =
                    client.getClass()
                            .getMethod(
                                    "alter_partition", String.class, String.class, Partition.class);
            method.invoke(client, databaseName, tableName, partition);
        } catch (InvocationTargetException ite) {
            // ...
        } catch (NoSuchMethodException | IllegalAccessException e) {
            // ...
        }
    }

而由于Hive Metastore提供的API alter_partition() 方法的签名发生了变化,对应2.1.0版本的实现是:

    @Override
    public void alterPartition(
            IMetaStoreClient client, String databaseName, String tableName, Partition partition)
            throws InvalidOperationException, MetaException, TException {
        String errorMsg = "Failed to alter partition for table %s in database %s";
        try {
            Method method =
                    client.getClass()
                            .getMethod(
                                    "alter_partition",
                                    String.class,
                                    String.class,
                                    Partition.class,
                                    EnvironmentContext.class);
            method.invoke(client, databaseName, tableName, partition, null);
        } catch (InvocationTargetException ite) {
            // ...
        } catch (NoSuchMethodException | IllegalAccessException e) {
            // ...
        }
    }

显然,由于Flink环境并不能事先确定外部Hive的版本,所以全部的Shim方法都需要依赖反射调用。另外,为了保持向后兼容性,Shim实现类从低版本到高版本会自然形成链式继承关系,如下图所示。

在Flink App启动时,HiveShimLoader组件会根据Catalog定义时传入的hive-version参数或者自动探测到的Hive版本加载特定的HiveShim,不再赘述。

不用Shim的场景?

在Flink Connector体系内也能找到这类场景,如ES Connector就使用了3个不同的module来实现:flink-connector-elasticsearch5flink-connector-elasticsearch6flink-connector-elasticsearch7

造成这种不同选择的原因有二:一是ES版本并不像Hive版本那么细分,即使6.x和7.x之间有大量的重复代码也在可接受的范围内;二是5.x采用Transport Client进行通信,而6.x和7.x采用REST High Level Client进行通信,相当于失去了统一的接口规约(如Hive的IMetaStoreClient那样),再使用反射调用会更加复杂,得不偿失。

Iceberg基于同样的考虑,在0.13版本之后也对Spark和Flink做了非Shim化的支持,看官可以去GitHub看看。

vs 适配器模式?

在笔者看来,Shim是适配器模式的一种(另辟蹊径的)实现方式。参考GoF对适配器模式的解说:

The adapter design pattern solves problems like:

  • How can a class be reused that does not have an interface that a client requires?
  • How can classes that have incompatible interfaces work together?
  • How can an alternative interface be provided for a class?

The adapter design pattern describes how to solve such problems:

  • Define a separate adapter class that converts the (incompatible) interface of a class (adaptee) into another interface (target) clients require.
  • Work through an adapter to work with (reuse) classes that do not have the required interface.

用上文的例子套用这个定义,HiveShim就是适配器本体,而Hive的原生API就是被适配者(adaptee),各个不同版本的HiveShim实现的方法就是目标接口(target)。一家之言,仅供参考。

The End

晚安晚安。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章