Skywalking-12:Skywalking SPI機制

switchvov發表於2021-10-03

SPI機制

基本概述

SPI 全稱 Service Provider Interface ,是一種服務發現機制。通過提供介面、預定義的載入器( Loader )以及約定俗稱的配置(一般在 META-INF 目錄下),可以實現動態載入服務實現類。

類圖

file

通過類圖可以分析出, ServiceLoader 實現了 Iterable 介面,提供了迭代的功能。

ServiceLoader 將迭代的實現委託給 LazyIterator

LazyIterator 提供了延時迭代的能力,當有需要的時候,才去載入。

Skywalking 模組中的使用

介面定義

org.apache.skywalking.oap.server.library.module.ModuleDefine

package org.apache.skywalking.oap.server.library.module;

import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ServiceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A module definition.
 */
public abstract class ModuleDefine implements ModuleProviderHolder {

    private static final Logger LOGGER = LoggerFactory.getLogger(ModuleDefine.class);

    private ModuleProvider loadedProvider = null;

    private final String name;

    public ModuleDefine(String name) {
        this.name = name;
    }

    /**
     * @return the module name
     *
     */
    public final String name() {
        return name;
    }

    /**
     * @return the {@link Service} provided by this module.
     */
    public abstract Class[] services();

    /**
     * Run the prepare stage for the module, including finding all potential providers, and asking them to prepare.
     *
     * @param moduleManager of this module
     * @param configuration of this module
     * @throws ProviderNotFoundException when even don't find a single one providers.
     */
    void prepare(ModuleManager moduleManager, ApplicationConfiguration.ModuleConfiguration configuration,
        ServiceLoader<ModuleProvider> moduleProviderLoader) throws ProviderNotFoundException, ServiceNotProvidedException, ModuleConfigException, ModuleStartException {
        // etc...
    }
    
    // etc...

    @Override
    public final ModuleProvider provider() throws DuplicateProviderException, ProviderNotFoundException {
        if (loadedProvider == null) {
            throw new ProviderNotFoundException("There is no module provider in " + this.name() + " module!");
        }
        return loadedProvider;
    }
}

介面實現

org.apache.skywalking.oap.server.library.module.BaseModuleA

package org.apache.skywalking.oap.server.library.module;

public class BaseModuleA extends ModuleDefine {

    public BaseModuleA() {
        super("BaseA");
    }

    // 需要提供服務的介面
    @Override
    public Class<? extends Service>[] services() {
        return new Class[] {
            ServiceABusiness1.class,
            ServiceABusiness2.class
        };
    }

    public interface ServiceABusiness1 extends Service {
        void print();
    }

    public interface ServiceABusiness2 extends Service {
    }
}

META-INF 定義

META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleDefine

org.apache.skywalking.oap.server.library.module.BaseModuleA

使用方式

org.apache.skywalking.oap.server.library.module.ModuleManager#init

    /**
     * Init the given modules
     */
    public void init(ApplicationConfiguration applicationConfiguration) /* etc... */ {
        // SPI機制載入
        ServiceLoaderModuleDefine> moduleServiceLoader = ServiceLoader.load(ModuleDefine.class);
        // 迭代器獲取
        for (ModuleDefine module : moduleServiceLoader) {
            // do something
            // etc...
        }
        // etc...
    }

原始碼解析

package java.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.AccessController;
import java.security.AccessControlContext;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public final class ServiceLoader<S> implements Iterable<S> {
    // 目錄字首
    private static final String PREFIX = "META-INF/services/";

    // 需要被載入物件的Class物件
    private final Class<S> service;

    // 類載入器
    private final ClassLoader loader;

    // The access control context taken when the ServiceLoader is created
    private final AccessControlContext acc;

    // 載入物件快取(按例項化順序排序)
    private LinkedHashMap<String,S> providers = new LinkedHashMap<>();

    // 當前使用的懶載入迭代器
    private LazyIterator lookupIterator;

    // 過載
    public void reload() {
        // 清除載入物件快取
        providers.clear();
        // 重置懶載入迭代器
        lookupIterator = new LazyIterator(service, loader);
    }

    // 不允許直接建立ServiceLoader物件,只能通過loadXXX獲取ServiceLoader物件
    private ServiceLoader(Class<S> svc, ClassLoader cl) {
        service = Objects.requireNonNull(svc, "Service interface cannot be null");
        loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
        acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null;
        reload();
    }

    private static void fail(Class<?> service, String msg, Throwable cause) throws ServiceConfigurationError {
        throw new ServiceConfigurationError(service.getName() + ": " + msg, cause);
    }

    private static void fail(Class<?> service, String msg) throws ServiceConfigurationError {
        throw new ServiceConfigurationError(service.getName() + ": " + msg);
    }

    private static void fail(Class<?> service, URL u, int line, String msg) throws ServiceConfigurationError {
        fail(service, u + ":" + line + ": " + msg);
    }

    // 解析配置檔案中的一行,如果沒有註釋,則加入到類名列表中
    private int parseLine(Class<?> service, URL u, BufferedReader r, int lc, List<String> names) throws IOException, ServiceConfigurationError {
        String ln = r.readLine();
        if (ln == null) {
            return -1;
        }
        int ci = ln.indexOf('#');
        if (ci >= 0) ln = ln.substring(0, ci);
        ln = ln.trim();
        int n = ln.length();
        if (n != 0) {
            if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
                fail(service, u, lc, "Illegal configuration-file syntax");
            int cp = ln.codePointAt(0);
            if (!Character.isJavaIdentifierStart(cp))
                fail(service, u, lc, "Illegal provider-class name: " + ln);
            for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
                cp = ln.codePointAt(i);
                if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
                    fail(service, u, lc, "Illegal provider-class name: " + ln);
            }
            if (!providers.containsKey(ln) && !names.contains(ln))
                names.add(ln);
        }
        return lc + 1;
    }

    // 解析配置檔案,返回實現類名列表
    private Iterator<String> parse(Class<?> service, URL u) throws ServiceConfigurationError {
        InputStream in = null;
        BufferedReader r = null;
        ArrayList<String> names = new ArrayList<>();
        try {
            in = u.openStream();
            r = new BufferedReader(new InputStreamReader(in, "utf-8"));
            int lc = 1;
            while ((lc = parseLine(service, u, r, lc, names)) >= 0);
        } catch (IOException x) {
            fail(service, "Error reading configuration file", x);
        } finally {
            try {
                if (r != null) r.close();
                if (in != null) in.close();
            } catch (IOException y) {
                fail(service, "Error closing configuration file", y);
            }
        }
        return names.iterator();
    }

    // 懶載入迭代器,提供了延時迭代的能力,當有需要的時候,才去載入
    private class LazyIterator implements Iterator<S> {
        // 需要被載入物件的Class物件
        Class<S> service;
        // 類載入器
        ClassLoader loader;
        // 配置檔案列表
        Enumeration<URL> configs = null;
        // 當前迭代的配置檔案中類名列表的迭代器
        Iterator<String> pending = null;
        // 下一個實現類名
        String nextName = null;

        private LazyIterator(Class<S> service, ClassLoader loader) {
            this.service = service;
            this.loader = loader;
        }

        // 是否有下一個Service
        private boolean hasNextService() {
            if (nextName != null) {
                return true;
            }
            // 載入所有配置檔案
            if (configs == null) {
                try {
                    String fullName = PREFIX + service.getName();
                    if (loader == null)
                        configs = ClassLoader.getSystemResources(fullName);
                    else
                        configs = loader.getResources(fullName);
                } catch (IOException x) {
                    fail(service, "Error locating configuration files", x);
                }
            }
            // 噹噹前類名列表迭代完之後,載入下一個配置檔案
            while ((pending == null) || !pending.hasNext()) {
                if (!configs.hasMoreElements()) {
                    return false;
                }
                pending = parse(service, configs.nextElement());
            }
            // 獲取下一個類名
            nextName = pending.next();
            return true;
        }

        // 獲取下一個Service
        private S nextService() {
            if (!hasNextService())
                throw new NoSuchElementException();
            String cn = nextName;
            nextName = null;
            Class<?> c = null;
            try {
                // 類名 -> 類的Class物件
                c = Class.forName(cn, false, loader);
            } catch (ClassNotFoundException x) {
                fail(service, "Provider " + cn + " not found");
            }
            if (!service.isAssignableFrom(c)) {
                fail(service, "Provider " + cn  + " not a subtype");
            }
            try {
                // 例項化
                S p = service.cast(c.newInstance());
                // 加入到快取中
                providers.put(cn, p);
                return p;
            } catch (Throwable x) {
                fail(service, "Provider " + cn + " could not be instantiated", x);
            }
            throw new Error();          // This cannot happen
        }

        // 迭代器,是否有下個元素
        public boolean hasNext() {
            if (acc == null) {
                return hasNextService();
            } else {
                // 授權資源
                PrivilegedAction<Boolean> action = new PrivilegedAction<Boolean>() {
                    public Boolean run() { return hasNextService(); }
                };
                return AccessController.doPrivileged(action, acc);
            }
        }

        // 迭代器,獲取下個元素
        public S next() {
            if (acc == null) {
                return nextService();
            } else {
                // 授權資源
                PrivilegedAction<S> action = new PrivilegedAction<S>() {
                    public S run() { return nextService(); }
                };
                return AccessController.doPrivileged(action, acc);
            }
        }

        // 不支援刪除
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

    // 迭代器實現,如果有快取從快取中獲取,沒有則從懶載入迭代器載入
    public Iterator<S> iterator() {
        return new Iterator<S>() {
            Iterator<Map.Entry<String,S>> knownProviders = providers.entrySet().iterator();

            public boolean hasNext() {
                if (knownProviders.hasNext())
                    return true;
                return lookupIterator.hasNext();
            }

            public S next() {
                if (knownProviders.hasNext())
                    return knownProviders.next().getValue();
                return lookupIterator.next();
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    // 通過類的Class物件及類載入,獲取ServiceLoader
    public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) {
        return new ServiceLoader<>(service, loader);
    }

    // 通過類的Class物件,獲取ServiceLoader
    public static <S> ServiceLoader<S> load(Class<S> service) {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        return ServiceLoader.load(service, cl);
    }

    // 通過類的Class物件和擴充套件類載入器,獲取ServiceLoader
    public static <S> ServiceLoader<S> loadInstalled(Class<S> service) {
        ClassLoader cl = ClassLoader.getSystemClassLoader();
        ClassLoader prev = null;
        while (cl != null) {
            prev = cl;
            cl = cl.getParent();
        }
        return ServiceLoader.load(service, prev);
    }

    public String toString() {
        return "java.util.ServiceLoader[" + service.getName() + "]";
    }

}

PS: JDK 提供的 SPI 機制,必須要使用迭代器遍歷獲取需要的實現,而 Dubbo SPI 可以通過 #getExtension 獲取指定實現類。

總結

通過原始碼分析,可以瞭解到 Skywalking 沒有定義自己的 SPI 機制,但深入閱讀 Skywalking 的使用場景後,發現用 JDK 提供的 SPI 機制也沒什麼問題。

個人認為,任何技術都應該根據場景選取,適合的才是最好的,如果沒有那麼複雜的需要,沒必要像 dubbo 一樣,定義自己的 SPI 機制。

參考文件

分享並記錄所學所見

相關文章