`
whitesock
  • 浏览: 478697 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

ID Generator

    博客分类:
  • SE
 
阅读更多

    关于ID Generator,想必大多数项目都有应用。跟按需生成ID相比,预生成一定数量的ID并加以缓存的方式更有助于提升性能。预生成ID的时机,通常是在发现缓存的ID用尽的时候。这种方式有个缺陷,即从调用者的角度来看每次取得ID所花费的时间可能并不均等。

    如果应用要求每次取得ID时都要尽可能的快且时间均等,那么ID Generator可以在发现缓存的ID用尽之前进行预生成,保持缓存中总是有可用的ID(例如每次预生成100个ID,在缓存中只剩下20个ID的时机再次进行预生成)。这种做法面临的主要问题就是并发控制。以及预生成时如果抛出异常, 那么该异常如何传播给请求ID的调用者(在预生成的时候,可能并没有没有线程在请求ID)。

    以下是一段笔者在项目中采用的代码片段, 首先是IdGenerator接口,以及几个跟ID生成相关的异常:

public interface IdGenerator<T> {
    T nextId();
}

public class GenerationException extends NestableRuntimeException {
    ...
}

public class GenerationInterruptedException extends GenerationException {
    ...
}

public class GenerationTimeoutException extends GenerationException {
    ...
}

 

    接下来是跟ID预生成相关的接口IdLoader

public interface IdLoader<T> {
    List<T> load() throws Exception;
}

 

    CachedIdGenerator是IdGenerator的一个实现,用于对ID缓存的管理,以及在适当的时机调用IdLoader进行ID的预生成。其最重要的一个属性是preLoadThreshold,通过调整这个属性的值,便可以控制ID预生成的时机:

  • 负值:在发现缓存中的已经没有可用的ID进行分配时生成。生成ID的过程中,调用nextId()方法的线程会被阻塞。
  • 0:在分配了缓存中的最后一个ID时生成,由于此时缓存中有最后一个可用的ID,因此调用nextId()方法的线程不会被阻塞。
  • 正值:在缓存中的ID个数小于还有该阀值时生成,调用nextId()方法的线程不会被阻塞。

    关于CachedIdGenerator的并发控制:

  • 如果多个线程同时调用nextId()方法,那么通过排他锁进行控制。
  • CachedIdGenerator内部使用一个单独的线程(以下成load线程)进行ID预生成(即调用IdLoader的load()方法的线程)。
  • 如果某次对nextId()方法的调用触发了ID预生成,那么在ID预生成结束之前该线程一直被阻塞。如果此时还有其它线程调用nextId()方法,那么这些线程也会被阻塞,即不会同时重复触发ID预生成。
  • 如果IdLoader的load()方法抛出异常,那么CachedIdGenerator对该异常的处理区分以下两个场景:1 如果此时有调用nextId()方法的线程被阻塞,那么该异常会被传播给调用nextId()方法的线程;2 如果此时没有调用nextId()方法的线程被阻塞,那么CachedIdGenerator会将该异常记录到日志中,同时将 preLoadThreshold 自动调整为-1。在最终缓存的中的ID用尽时,才会再次触发ID预生成,此时调用nextId()方法的线程一定被阻塞,如果IdLoader的load 方法再次抛出异常,那么这个异常会被传播给调用nextId()方法的线程。
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CachedIdGenerator<T> implements IdGenerator<T> {
    //
    private static final Logger LOGGER = LoggerFactory.getLogger(CachedIdGenerator.class);
    
    //
    private final AsyncIdLoader loader;
    private final ReentrantLock lock = new ReentrantLock();
    private final LinkedList<T> cache = new LinkedList<T>();
    private final AtomicBoolean verbose = new AtomicBoolean(false);
    private final AtomicInteger preLoadThreshold = new AtomicInteger(0);
    
    /**
     * 
     */
    public CachedIdGenerator(IdLoader<T> loader) {
        this(loader, 0);
    }
    
    public CachedIdGenerator(IdLoader<T> loader, int preLoadThreshold) {
        this.loader = new AsyncIdLoader(loader);
        this.preLoadThreshold.set(preLoadThreshold);
    }
    
    /**
     * 
     */
    public final T nextId() {
        try {
            while(true) {
                final Future<T> f = next();
                final T r = f.get();
                if(r != null) {
                    return r;
                }
            }
        } catch (InterruptedException e) {
            throw new GenerationInterruptedException(e);
        } catch(ExecutionException e) {
            throw new GenerationException(((ExecutionException)e).getCause());
        } catch(Exception e) {
            throw new GenerationException(e);
        }
    }
    
    public final T nextId(long timeout, TimeUnit unit) {
        try {
            while(true) {
                //
                final long now = System.nanoTime();
                final Future<T> f = next();
                final T r = f.get(timeout, unit);
                if(r != null) {
                    return r;
                }
                
                //
                timeout -= unit.convert(System.nanoTime() - now, TimeUnit.NANOSECONDS);
                if(timeout < 0) {
                    throw new TimeoutException();
                }
            }
        } catch(TimeoutException e) {
            throw new GenerationTimeoutException(e);
        } catch (InterruptedException e) {
            throw new GenerationInterruptedException(e);
        } catch(ExecutionException e) {
            throw new GenerationException(((ExecutionException)e).getCause());
        } catch(Exception e) {
            throw new GenerationException(e);
        }
    }

    public boolean isVerbose() {
        return verbose.get();
    }
    
    public void setVerbose(boolean verbose) {
        this.verbose.set(verbose);
    }

    public void disablePreLoad() {
        this.preLoadThreshold.set(-1);
    }
    
    public int getPreLoadThreshold() {
        return preLoadThreshold.get();
    }
    
    public void setPreLoadThreshold(int threshold) {
        this.preLoadThreshold.set(threshold);
    }
    
    /**
     * 
     */
    protected Future<T> next() {
        Future<T> r = null;
        this.lock.lock();
        try {
            //
            if(!this.cache.isEmpty()) {
                final DummyFuture<T> df = new DummyFuture<T>();
                df.setResult(this.cache.removeFirst());
                r = df;
            }
            
            //
            if(r == null) {
                r = this.loader.load();
            } else if(cache.size() <= this.preLoadThreshold.get()) {
                this.loader.load();
            }
        } finally {
            this.lock.unlock();
        }
        return r;
    }
    
    /**
     * 
     */
    protected class AsyncIdLoader {
        //
        private final IdLoader<T> loader;
        private final ExecutorService executor;
        private final AtomicReference<Future<T>> result = new AtomicReference<Future<T>>();
        
        /**
         * 
         */
        public AsyncIdLoader(IdLoader<T> loader) {
            this.loader = loader;
            this.executor = Executors.newFixedThreadPool(1, new XThreadFactory(getClass().getSimpleName(), true));
        }
        
        /**
         * 
         */
        public Future<T> load() {
            //
            final Future<T> current = this.result.get();
            if(current != null) { // Loading is in progress
                return current;
            }
            
            //
            final Future<T> r = this.executor.submit(new Callable<T>() {

                public T call() throws Exception {
                    List<T> ids = null;
                    try {
                        //
                        if(isVerbose() && LOGGER.isInfoEnabled()) {
                            LOGGER.info("start to load ids, loader: {}", loader);
                        }
                        
                        //
                        ids = loader.load();
                        
                        //
                        if(isVerbose() && LOGGER.isInfoEnabled()) {
                            LOGGER.info("ids were successfully loaded, count: {}, loader: {}", (ids == null ? 0 : ids.size()), loader);
                        }
                        return null;
                    } catch (Exception e) {
                        disablePreLoad();
                        LOGGER.warn("unhandled exception in id loader: " + loader + ", pre-loading was disabled", e);
                        throw e;
                    } finally {
                        //
                        lock.lock();
                        try {
                            result.set(null);
                            if(ids != null && ids.size() > 0) {
                                cache.addAll(ids);
                            }
                        } finally {
                            lock.unlock();
                        }
                    }
                }
            });
            
            //
            this.result.set(r);
            return r;
        }
    } 
}

    最后是CachedIdGenerator用到的几个工具类:

 

public class XThreadFactory implements ThreadFactory {
    //
    private static final Logger LOGGER = LoggerFactory.getLogger(XThreadFactory.class);
    
    //
    private String name;
    private boolean daemon;
    private UncaughtExceptionHandler uncaughtExceptionHandler;
    private final ConcurrentHashMap<String, AtomicLong> sequences;
    
    
    /**
     * 
     */
    public XThreadFactory() {
        this(null, false, null);
    }
    
    public XThreadFactory(String name) {
        this(name, false, null);
    }
    
    public XThreadFactory(String name, boolean daemon) {
        this(name, daemon, null);
    }
    
    public XThreadFactory(String name, boolean daemon, UncaughtExceptionHandler handler) {
        this.name = name;
        this.daemon = daemon;
        this.uncaughtExceptionHandler = handler;
        this.sequences = new ConcurrentHashMap<String, AtomicLong>();
    }

    /**
     * 
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public boolean isDaemon() {
        return daemon;
    }
    
    public void setDaemon(boolean daemon) {
        this.daemon = daemon;
    }

    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
        return uncaughtExceptionHandler;
    }
    
    public void setUncaughtExceptionHandler(UncaughtExceptionHandler handler) {
        this.uncaughtExceptionHandler = handler;
    }
    
    /**
     * 
     */
    public Thread newThread(Runnable r) {
        //
        Thread t = new Thread(r);
        t.setDaemon(this.daemon);
        
        //
        String prefix = this.name;
        if(prefix == null || prefix.equals("")) {
            prefix = getInvoker(2);
        }
        t.setName(prefix + "-" + getSequence(prefix));
        
        //
        if(this.uncaughtExceptionHandler != null) {
            t.setUncaughtExceptionHandler(this.uncaughtExceptionHandler);
        } else {
            t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                public void uncaughtException(Thread t, Throwable e) {
                    LOGGER.error("unhandled exception in thread: " + t.getId() + ":" + t.getName(), e);
                }
            });
        }
        
        //
        return t;
    }

    /**
     * 
     */
    private String getInvoker(int depth) {
        Exception e = new Exception();
        StackTraceElement[] stes = e.getStackTrace();
        if(stes.length > depth) {
            return ClassUtils.getShortClassName(stes[depth].getClassName());
        }
        return getClass().getSimpleName();
    }
    
    private long getSequence(String invoker) {
        AtomicLong r = this.sequences.get(invoker);
        if(r == null) {
            r = new AtomicLong(0);
            AtomicLong previous = this.sequences.putIfAbsent(invoker, r);
            if(previous != null) {
                r = previous;
            }
        }
        return r.incrementAndGet();
    }
}

public class DummyFuture<V> implements Future<V> {
    //
    private volatile V result;
    private volatile Throwable throwable;

    /**
     * 
     */
    public boolean isDone() {
        return true;
    }
    
    public boolean isCancelled() {
        return false;
    }
    
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    public V get() throws InterruptedException, ExecutionException {
        if(throwable != null) {
            throw new ExecutionException(throwable);
        } else {
            return result;
        }
    }

    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return get();
    }
    
    /**
     * 
     */
    public void setResult(V result) {
        this.result = result;
    }

    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
    }
}

public class XFuture<V> implements Future<V> {
    //
    private volatile V result;
    private volatile Object id;
    private volatile Throwable throwable;
    private CountDownLatch done = new CountDownLatch(1);

    /**
     * 
     */
    public XFuture() {
        this(null);
    }
    
    public XFuture(Object id) {
        this.id = id;
    }

    /**
     * 
     */
    public boolean isDone() {
        return done.getCount() != 1;
    }
    
    public boolean isCancelled() {
        return false;
    }
    
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    public V get() throws InterruptedException, ExecutionException {
        //
        done.await();
        
        //
        if(throwable != null) {
            throw new ExecutionException(throwable);
        } else {
            return result;
        }
    }

    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        //
        if(!done.await(timeout, unit)) {
            throw new TimeoutException("failed to get in timeout: " + timeout + ", unit: " + unit);
        }
        
        //
        if(throwable != null) {
            throw new ExecutionException(throwable);
        } else {
            return result;
        }
    }
    
    /**
     * 
     */
    public Object getId() {
        return id;
    }
    
    public void setResult(V result) {
        this.result = result;
        this.done.countDown();
    }
    
    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
        this.done.countDown();
    }
}
6
3
分享到:
评论
2 楼 whitesock 2010-06-15  
比如说通过数据库的某个值生成ID
1 每次对其加1,生成100个ID至少需要100次数据库访问。
2 每次对其加100, 那么生成100个ID只需要1次数据库访问。
1 楼 hellojinjie 2010-06-15  
恕我愚钝,id 的生成缓不缓存和性能有什么关系呢?

相关推荐

    idgenerator分布式主键ID生成器

    迄今为止最全面的分布式主键ID生成器。优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力...支持容器环境自动扩容(自动注册 WorkerId ),单机或分布式唯一IdGenerator。

    迄今为止最全面的分布式主键ID生成器,多语言新雪花算法(SnowFlake IdGenerator).zip

    支持容器环境自动扩容(自动注册 WorkerId ),单机或分布式唯一IdGenerator。 迄今为止最全面的分布式主键ID生成器。 优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理...

    一个简单的自定义ID 生成器IDGenerator

    一个用Java 编写简单的自定义ID 生成器IDGenerator

    idgenerator:idgenerator是基于redis的id生成器

    idgenerator是基于redis的id生成器 dgenerator是基于redis的id生成器 安装 取得 go get github.com/lbfatcgf/idgenerator 快速开始 package main import ( "fmt" "net/http" "os" "os/signal" "syscall" ...

    idGenerator

    idGenerator

    IdGenerator.java

    Java开发中中经常使用的Java工具类分享,工作中用得上,直接拿来使用,不用重复造轮子。

    idGenerator:使用golanggo的idGenerator

    #ID产生器 用go实现的id生成器,支持每秒qps:131072,超过需要等待下一秒 依赖 mysql(或zk,redis等) 需要使用mysql来保证多台机器获取到的workId不同当然,如果是单点,那随意设置workId 使用介绍 初始化程序 ...

    idgenerator-go

    核心在于扩展ID长度的同时,还能拥有极高瞬时并发处理量(保守值50W / 0.1s)。 3.本机支持C#/ Java / Go / Rust / C等语言,并由Rust提供PHP,Python,Node.js,Ruby等语言多线程安全调用库(FFI)。技术支持开源...

    Java ID Generator-开源

    Jidgen是一个易于使用但功能强大的基于Java的id生成器。 它使用模板来自动生成id和可选的冲突检测,以避免重复的id。

    JSF ID Generator Plugin-开源

    JSF ID Generator是一个eclipse插件,它为JSF(Java Server Faces)标签生成可定制且唯一的组件ID。 如果您不给JSF组件一个id,那么它将在运行时以诸如j_id_jsp_之类的前缀生成。

    id-generator-java:Java实现的ID生成器

    id-生成器-java Java实现的ID生成器 格式: T |64| * L |6| R|4| 否 |12| * S |10| * ID = TLRNS,96 位 T 时间戳,以毫秒为单位,64 位 l 逻辑区域,如分区或ISP,6bits,容量为64个区域 R 保留位,4 位,容量 ...

    id-generator:为流星集合生成 id

    安装 meteor add theara: id - generator用法 // idGenerator.gen(collection, length, [field]); // default field = _idvar id = idGenerator . gen ( CustomerCollection , 3 ) ; // 001, 002var id = id...

    人工智能-项目实践-区块链-基于区块链(以太坊)的去中心化ID生成器.zip

    ID-Generator 一款基于区块链(以太坊)的ID生成器 An ID generator based on blockchain(Ethereum) 用法 按照conf/conf.yaml中的注释提示,修改conf/conf.yaml中的配置。 在urls.go中启动程序。 访问...

    id-generator:生成带有前缀的随机ID(la Stripe)

    var IdGenerator = require ( 'auth0-id-generator' ) ; var generator = new IdGenerator ( ) ; var id = generator . new ( 'cus' ) ; console . log ( id ) ; // cus_lO1DEQWBbQAACfHO 预定义的一组允许的前缀...

    jdk与javauuidgenerator生成uuid

    jdk与javauuidgenerator生成uuid

    stripe-id-generator:生成带有前缀的随机ID(la Stripe)

    const IdGenerator = require ( 'stripe-id-generator' ) ; const generator = new IdGenerator ( ) ; const id = generator . new ( 'cus' ) ; console . log ( id ) ; // cus_lO1DEQWBbQAACfHO 预定义的一组允许...

    id-generator:整体唯一ID生成器

    将百度uid-generator组件封装成独立的springboot应用提供服务 使用方法 修改application.properties,配置好数据库 执行doc / init.sql初始化表 运行springboot项目 连接调用 1. 获取单个id: ...

    最全面的分布式主键ID生成器

    最全面的分布式主键ID生成器。 优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力(50W/0.1...支持容器环境自动扩容(自动注册 WorkerId ),单机或分布式唯一IdGenerator。

Global site tag (gtag.js) - Google Analytics