聊聊mybatis-plus的DefaultIdentifierGenerator

2024-05-11 17:48:45 浏览数 (1)

本文主要研究一下mybatis-plus的DefaultIdentifierGenerator

MybatisSqlSessionFactoryBuilder

com/baomidou/mybatisplus/core/MybatisSqlSessionFactoryBuilder.java

代码语言:javascript复制
    @Override
    public SqlSessionFactory build(Configuration configuration) {
        GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);

        final IdentifierGenerator identifierGenerator;
        if (null == globalConfig.getIdentifierGenerator()) {
            identifierGenerator = new DefaultIdentifierGenerator();
            globalConfig.setIdentifierGenerator(identifierGenerator);
        } else {
            identifierGenerator = globalConfig.getIdentifierGenerator();
        }
        IdWorker.setIdentifierGenerator(identifierGenerator);

        if (globalConfig.isEnableSqlRunner()) {
            new SqlRunnerInjector().inject(configuration);
        }

        SqlSessionFactory sqlSessionFactory = super.build(configuration);

        // 缓存 sqlSessionFactory
        globalConfig.setSqlSessionFactory(sqlSessionFactory);

        return sqlSessionFactory;
    }

MybatisSqlSessionFactoryBuilder的build方法,在globalConfig.getIdentifierGenerator()为null的时候创建并使用DefaultIdentifierGenerator

IdentifierGenerator

com/baomidou/mybatisplus/core/incrementer/IdentifierGenerator.java

代码语言:javascript复制
public interface IdentifierGenerator {

    /**
     * 判断是否分配 ID
     *
     * @param idValue 主键值
     * @return true 分配 false 无需分配
     */
    default boolean assignId(Object idValue) {
        return StringUtils.checkValNull(idValue);
    }

    /**
     * 生成Id
     *
     * @param entity 实体
     * @return id
     */
    Number nextId(Object entity);

    /**
     * 生成uuid
     *
     * @param entity 实体
     * @return uuid
     */
    default String nextUUID(Object entity) {
        return IdWorker.get32UUID();
    }
}

IdentifierGenerator接口定义了nextId方法,同时提供了assignId、nextUUID默认方法

DefaultIdentifierGenerator

com/baomidou/mybatisplus/core/incrementer/DefaultIdentifierGenerator.java

代码语言:javascript复制
public class DefaultIdentifierGenerator implements IdentifierGenerator {
    private final Sequence sequence;

    public DefaultIdentifierGenerator() {
        this.sequence = new Sequence(null);
    }

    public DefaultIdentifierGenerator(InetAddress inetAddress) {
        this.sequence = new Sequence(inetAddress);
    }

    public DefaultIdentifierGenerator(long workerId, long dataCenterId) {
        this.sequence = new Sequence(workerId, dataCenterId);
    }

    public DefaultIdentifierGenerator(Sequence sequence) {
        this.sequence = sequence;
    }

    @Override
    public Long nextId(Object entity) {
        return sequence.nextId();
    }
}

DefaultIdentifierGenerator实现了IdentifierGenerator接口,其nextId方法委托给了Sequence

Sequence

com/baomidou/mybatisplus/core/toolkit/Sequence.java

代码语言:javascript复制
public class Sequence {

    private static final Log logger = LogFactory.getLog(Sequence.class);
    /**
     * 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
     */
    private static final long twepoch = 1288834974657L;
    /**
     * 机器标识位数
     */
    private final long workerIdBits = 5L;
    private final long datacenterIdBits = 5L;
    private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
    private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
    /**
     * 毫秒内自增位
     */
    private final long sequenceBits = 12L;
    private final long workerIdShift = sequenceBits;
    private final long datacenterIdShift = sequenceBits   workerIdBits;
    /**
     * 时间戳左移动位
     */
    private final long timestampLeftShift = sequenceBits   workerIdBits   datacenterIdBits;
    private final long sequenceMask = -1L ^ (-1L << sequenceBits);

    private final long workerId;

    /**
     * 数据标识 ID 部分
     */
    private final long datacenterId;
    /**
     * 并发控制
     */
    private long sequence = 0L;
    /**
     * 上次生产 ID 时间戳
     */
    private long lastTimestamp = -1L;
    /**
     * IP 地址
     */
    private InetAddress inetAddress;

    public Sequence(InetAddress inetAddress) {
        this.inetAddress = inetAddress;
        this.datacenterId = getDatacenterId(maxDatacenterId);
        this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
    }

    /**
     * 有参构造器
     *
     * @param workerId     工作机器 ID
     * @param datacenterId 序列号
     */
    public Sequence(long workerId, long datacenterId) {
        Assert.isFalse(workerId > maxWorkerId || workerId < 0,
            String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
        Assert.isFalse(datacenterId > maxDatacenterId || datacenterId < 0,
            String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }

    //......

}    

Sequence的twepoch为1288834974657(2010-11-04 09:42:54 657),默认工作机器ID为5bit和数据中心ID均为5bit(与雪花算法一致),默认maxWorkerId与maxDatacenterId均为2^5-1;毫秒内序列号为12bit;时间戳为41bit

getDatacenterId

代码语言:javascript复制
    protected long getDatacenterId(long maxDatacenterId) {
        long id = 0L;
        try {
            if (null == this.inetAddress) {
                this.inetAddress = InetAddress.getLocalHost();
            }
            NetworkInterface network = NetworkInterface.getByInetAddress(this.inetAddress);
            if (null == network) {
                id = 1L;
            } else {
                byte[] mac = network.getHardwareAddress();
                if (null != mac) {
                    id = ((0x000000FF & (long) mac[mac.length - 2]) | (0x0000FF00 & (((long) mac[mac.length - 1]) << 8))) >> 6;
                    id = id % (maxDatacenterId   1);
                }
            }
        } catch (Exception e) {
            logger.warn(" getDatacenterId: "   e.getMessage());
        }
        return id;
    }

getDatacenterId会通过NetworkInterface.getByInetAddress获取NetworkInterface,若为null则返回1,否则获取NetworkInterface的getHardwareAddress,之后根据mac地址(00:16:3e:0e:36:ac)的倒数第2部分的低8位,将倒数第1部分左移8位再取低16位,再将这两部分取或,最后右移6位,再将结果对(maxDatacenterId 1)(即32)取余作为datacenterId

getMaxWorkerId

代码语言:javascript复制
    protected long getMaxWorkerId(long datacenterId, long maxWorkerId) {
        StringBuilder mpid = new StringBuilder();
        mpid.append(datacenterId);
        String name = ManagementFactory.getRuntimeMXBean().getName();
        if (StringUtils.isNotBlank(name)) {
            /*
             * GET jvmPid
             */
            mpid.append(name.split(StringPool.AT)[0]);
        }
        /*
         * MAC   PID 的 hashcode 获取16个低位
         */
        return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId   1);
    }

getMaxWorkerId实际是在没有指定workerId的时候自动计算一个workerId,它是基于datacenterId及pid的哈希值取低16位,最后对(maxWorkerId 1)(即32)取余

nextId

代码语言:javascript复制
    public synchronized long nextId() {
        long timestamp = timeGen();
        //闰秒
        if (timestamp < lastTimestamp) {
            long offset = lastTimestamp - timestamp;
            if (offset <= 5) {
                try {
                    wait(offset << 1);
                    timestamp = timeGen();
                    if (timestamp < lastTimestamp) {
                        throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", offset));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", offset));
            }
        }

        if (lastTimestamp == timestamp) {
            // 相同毫秒内,序列号自增
            sequence = (sequence   1) & sequenceMask;
            if (sequence == 0) {
                // 同一毫秒的序列数已经达到最大
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            // 不同毫秒内,序列号置为 1 - 2 随机数
            sequence = ThreadLocalRandom.current().nextLong(1, 3);
        }

        lastTimestamp = timestamp;

        // 时间戳部分 | 数据中心部分 | 机器标识部分 | 序列号部分
        return ((timestamp - twepoch) << timestampLeftShift)
            | (datacenterId << datacenterIdShift)
            | (workerId << workerIdShift)
            | sequence;
    }

    protected long timeGen() {
        return SystemClock.now();
    }

    protected long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }       

nextId就是雪花算法生成id的部分,它先通过SystemClock.now()获取当前时间戳,判断是同毫秒的则递增sequence,不同毫秒则随机设置为1或者2;最开始会对时间回拨进行判断,若差异大于5则直接抛出异常,在小于等于5的时候会等待offset的两倍,若时间还是小于lastTimestamp则抛出异常;sequence设置完之后更新lastTimestamp,然后按照时间戳部分 | 数据中心部分 | 机器标识部分 | 序列号部分构造id

SystemClock

com/baomidou/mybatisplus/core/toolkit/SystemClock.java

代码语言:javascript复制
public class SystemClock {

    private final long period;
    private final AtomicLong now;

    private SystemClock(long period) {
        this.period = period;
        this.now = new AtomicLong(System.currentTimeMillis());
        scheduleClockUpdating();
    }

    private static SystemClock instance() {
        return InstanceHolder.INSTANCE;
    }

    public static long now() {
        return instance().currentTimeMillis();
    }

    public static String nowDate() {
        return new Timestamp(instance().currentTimeMillis()).toString();
    }

    private void scheduleClockUpdating() {
        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
            Thread thread = new Thread(runnable, "System Clock");
            thread.setDaemon(true);
            return thread;
        });
        scheduler.scheduleAtFixedRate(() -> now.set(System.currentTimeMillis()), period, period, TimeUnit.MILLISECONDS);
    }

    private long currentTimeMillis() {
        return now.get();
    }

    private static class InstanceHolder {
        public static final SystemClock INSTANCE = new SystemClock(1);
    }
}

SystemClock是针对高并发场景下System. currentTimeMillis()的性能问题的优化,其构造器在创建的时候将System.currentTimeMillis()赋值给AtomicLong,其now方法返回的是instance().currentTimeMillis(),即now.get(),它有个定时任务,每隔1毫秒更新一下now为当前System.currentTimeMillis()

小结

mybatis-plus的MybatisSqlSessionFactoryBuilder的build方法,在globalConfig.getIdentifierGenerator()为null的时候创建并使用DefaultIdentifierGenerator,它内部使用的是Sequence来生成id的,Sequence使用的是雪花算法,默认的datacenterId(5bit)是基于mac地址计算而来(取倒数第2部分的低8位,将倒数第1部分左移8位再取低16位,再将这两部分取或,最后右移6位,再将结果对(maxDatacenterId 1)(即32)取余作为datacenterId),默认的workerId(5bit)是基于datacenterId及pid的哈希值取低16位,最后对(maxWorkerId 1)(即32)取余;nextId就是雪花算法生成id的部分,它先通过SystemClock.now()获取当前时间戳,判断是同毫秒的则递增sequence,不同毫秒则随机设置为1或者2。

doc

  • snowflake

0 人点赞