class LoadBalanceFactory extends SchedulerFactoryBase {

        protected LoadBalanceFactory(Map<String, String> config, Scheduler[] schedulers) {
            super(config, schedulers);
        }

        @Override
        public Scheduler getScheduler() {
            long minLoad = Long.MAX_VALUE;
            int index = 0;
            for (int i = 0, size = schedulers.length; i < size; i++) {
                long load = schedulers[i].getLoad();
                if (load < minLoad) {
                    index = i;
                    minLoad = load;
                }
            }
            return schedulers[index];
        }
    }

    // 变成负数时从0开始
    public static int getAndIncrementIndex(AtomicInteger index) {
        int i = index.getAndIncrement();
        if (i < 0) {
            if (index.compareAndSet(i, 1)) {
                i = 0;
            } else {
                i = index.getAndIncrement();
            }
        }
        return i;
    }

    private static SchedulerFactory defaultSchedulerFactory;

    public static void setDefaultSchedulerFactory(SchedulerFactory defaultSchedulerFactory) {
        SchedulerFactoryBase.defaultSchedulerFactory = defaultSchedulerFactory;
    }

    public static SchedulerFactory getDefaultSchedulerFactory() {
        return defaultSchedulerFactory;
    }

    public static SchedulerFactory getDefaultSchedulerFactory(String schedulerClassName,
            Map<String, String> config) {
        if (SchedulerFactoryBase.getDefaultSchedulerFactory() == null)
            initDefaultSchedulerFactory(schedulerClassName, config);
        return SchedulerFactoryBase.getDefaultSchedulerFactory();
    }

    public static synchronized SchedulerFactory initDefaultSchedulerFactory(String schedulerClassName,
            Map<String, String> config) {
        SchedulerFactory schedulerFactory = SchedulerFactoryBase.getDefaultSchedulerFactory();
        if (schedulerFactory == null) {
            schedulerFactory = createSchedulerFactory(schedulerClassName, config);
            SchedulerFactoryBase.setDefaultSchedulerFactory(schedulerFactory);
        }
        return schedulerFactory;
    }

    public static SchedulerFactory createSchedulerFactory(String schedulerClassName,
            Map<String, String> config) {<FILL_FUNCTION_BODY>

        SchedulerFactory schedulerFactory;
        String sf = MapUtils.getString(config, "scheduler_factory", null);
        if (sf != null) {
            schedulerFactory = PluginManager.getPlugin(SchedulerFactory.class, sf);
        } else {
            Scheduler[] schedulers = createSchedulers(schedulerClassName, config);
            schedulerFactory = SchedulerFactory.create(config, schedulers);
        }
        if (!schedulerFactory.isInited())
            schedulerFactory.init(config);
        return schedulerFactory;
    
<methods>public void <init>() ,public void <init>(java.lang.String) ,public synchronized void close() ,public Map<java.lang.String,java.lang.String> getConfig() ,public java.lang.String getName() ,public Class<? extends com.lealone.db.Plugin> getPluginClass() ,public com.lealone.db.Plugin.State getState() ,public synchronized void init(Map<java.lang.String,java.lang.String>) ,public boolean isInited() ,public boolean isStarted() ,public boolean isStopped() ,public void setName(java.lang.String) ,public void start() ,public void stop() <variables>protected Map<java.lang.String,java.lang.String> config,protected java.lang.String name,protected com.lealone.db.Plugin.State state