Skip to content

数据再平衡

1.数据再平衡

1.1.概述

当一个新节点加入集群时,部分分区会被分配至新的节点,以使整个集群的数据保持平均分布,这个过程称为数据再平衡

如果现有节点永久离开集群,并且未配置备份,则会丢失此节点上存储的分区。配置备份后,丢失分区的备份副本之一将成为主分区,并开始再平衡过程。

警告

数据再平衡由基线拓扑的变化触发。在纯内存集群中,默认行为是在节点离开或加入集群时(基线拓扑自动更改)立即开始再平衡。在开启持久化的集群中,默认必须手动更改基线拓扑,或者在启用基线自动调整后可以自动更基线拓扑。

1.2.配置再平衡模式

Ignite支持同步和异步的再平衡,在同步模式中,再平衡结束前缓存的任何操作都会被阻塞。在异步模式中,再平衡过程以异步的模式执行,也可以为某个缓存禁用再平衡。

如果要修改再平衡模式,可以在缓存配置中配置如下的值:

  • SYNC:同步再平衡模式,再平衡结束前缓存的任何操作都会被阻塞;
  • ASYNC:异步再平衡模式,缓存直接可用,然后在后台会从其它节点加载所有必要的数据;
  • NONE:该模式下不会发生再平衡,这意味着要么在访问数据时从持久化存储载入,要么数据被显式地填充。
xml
<bean class="org.apache.ignite.configuration.IgniteConfiguration" id="ignite.cfg">
    <property name="cacheConfiguration">
        <list>
            <bean class="org.apache.ignite.configuration.CacheConfiguration">
                <property name="name" value="mycache"/>
                <!-- enable synchronous rebalance mode -->
                <property name="rebalanceMode" value="SYNC"/>
            </bean>
        </list>
    </property>
</bean>
java
IgniteConfiguration cfg = new IgniteConfiguration();

CacheConfiguration cacheCfg = new CacheConfiguration("mycache");

cacheCfg.setRebalanceMode(CacheRebalanceMode.SYNC);

cfg.setCacheConfiguration(cacheCfg);

// Start a node.
Ignite ignite = Ignition.start(cfg);
csharp
IgniteConfiguration cfg = new IgniteConfiguration
{
    CacheConfiguration = new[]
    {
        new CacheConfiguration
        {
            Name = "mycache",
            RebalanceMode = CacheRebalanceMode.Sync
        }
    }
};

// Start a node.
var ignite = Ignition.Start(cfg);

1.3.配置再平衡线程池

默认的再平衡线程数量,取4或可用处理器核心数量的1/4中的较小值。例如,在8核处理器上,将使用2个线程。

可以从系统线程池中拿到更多的线程数用于再平衡。每当节点需要将一批数据发送到远端节点或需要处理来自远端节点的一批数据时,都会从池中获取系统线程,批次处理完成后,该线程会被释放。

xml
<bean class="org.apache.ignite.configuration.IgniteConfiguration" id="ignite.cfg">

    <property name="rebalanceThreadPoolSize" value="4"/>

    <property name="cacheConfiguration">
        <list>
            <bean class="org.apache.ignite.configuration.CacheConfiguration">
                <property name="name" value="mycache"/>
            </bean>
        </list>
    </property>
</bean>
java
IgniteConfiguration cfg = new IgniteConfiguration();

cfg.setRebalanceThreadPoolSize(4);

CacheConfiguration cacheCfg = new CacheConfiguration("mycache");
cfg.setCacheConfiguration(cacheCfg);

// Start a node.
Ignite ignite = Ignition.start(cfg);

警告

在内部,系统线程池广泛用于和缓存有关的所有操作(put,get等),SQL引擎和其它模块,因此将再平衡线程池设置为一个很大的值会显著提高再平衡的性能,但是会影响应用的吞吐量。

1.4.再平衡消息限流

当数据从一个节点传输到另一个节点时,整个数据集会被拆分为多个批次然后将每一个批次作为一个单独的消息进行发送,批次的大小和节点在消息之间的等待时间,都是可以配置的。

xml
<bean class="org.apache.ignite.configuration.IgniteConfiguration" id="ignite.cfg">
    <!-- Set batch size. -->
    <property name="rebalanceBatchSize" value="#{2 * 1024 * 1024}"/>
    <!-- Set batches prefetch count. -->
    <property name="rebalanceBatchesPrefetchCount" value="3"/>
    <!-- Set throttle interval. -->
    <property name="rebalanceThrottle" value="100"/>
    <property name="cacheConfiguration">
        <list>
            <bean class="org.apache.ignite.configuration.CacheConfiguration">
                <property name="name" value="mycache"/>
                <!-- enable synchronous rebalance mode -->
                <property name="rebalanceMode" value="SYNC"/>
            </bean>
        </list>
    </property>
</bean>
java
IgniteConfiguration cfg = new IgniteConfiguration();

cfg.setRebalanceBatchSize(2 * 1024 * 1024);
cfg.setRebalanceBatchesPrefetchCount(3);
cfg.setRebalanceThrottle(100);

// Start a node.
Ignite ignite = Ignition.start(cfg);
csharp
IgniteConfiguration cfg = new IgniteConfiguration
{
    RebalanceBatchSize = 2 * 1024 * 1024,
    RebalanceThrottle = new TimeSpan(0, 0, 0, 0, 100),
    RebalanceBatchesPrefetchCount = 3
};

// Start a node.
var ignite = Ignition.Start(cfg);

1.5.其他配置

下表列出了IgniteConfiguration中和再平衡有关的属性:

警告

rebalanceDelay及相关的API已被废弃,在后面的版本中会被删除。

属性描述默认值
rebalanceThreadPoolSize再平衡线程池大小,限制用于再平衡的线程数min(4, max(1, AVAILABLE_PROC_CNT / 4))
rebalanceBatchSize单个再平衡消息的大小(字节),在每个节点再平衡算法会将数据拆分为多个批次,然后再将其发送给其他节点。512KB
rebalanceBatchesPrefetchCount再平衡批次预取数量3
rebalanceThrottle请参见再平衡消息限流0(限流禁用)
rebalanceOrder完成再平衡的顺序,只有SYNCASYNC再平衡模式的缓存才可以将再平衡顺序设置为非0值,具有更小值的缓存再平衡会被首先完成,再平衡默认是无序的。0
rebalanceTimeout节点间交换再平衡消息的挂起超时。10秒
rebalanceDelay(废弃)当节点加入或者离开集群时,再平衡过程启动的延迟时间(毫秒)。如果打算同时/一个个重启或者启动多个节点的过程中,所有节点全部启动完成之前不进行重新分区或者再平衡,再平衡延迟会有用。0,无延迟

1.6.再平衡过程监控

通过JMX可以监控缓存的再平衡过程

18624049226