3

我们计划使用 Hazelcast 对 Apache Camel 消息进行排队,并作为二级 Hibernate 缓存。

假设我们将队列配置为使用 Integer.MAX_VALUE(可能不适合内存)而没有后备映射:如果所有内存都被使用,会发生什么?我在文档中真的找不到任何关于这种情况的信息。

<hazelcast>
...
<queue name="tasks">
    <!--
        Maximum size of the queue. When a JVM's local queue size reaches the maximum,
        all put/offer operations will get blocked until the queue size
        of the JVM goes down below the maximum.
        Any integer between 0 and Integer.MAX_VALUE. 0 means Integer.MAX_VALUE. Default is 0.
    -->
    <max-size-per-jvm>0</max-size-per-jvm>

    <!--
        Name of the map configuration that will be used for the backing distributed
        map for this queue.
    -->
    <backing-map-ref></backing-map-ref>
</queue>

地图也一样:如果没有 TTL 并且驱逐策略设置为 NONE,那么整个地图迟早不会适合内存会怎样?

<hazelcast>
...
<map name="default">
    <!--
        Number of backups. If 1 is set as the backup-count for example,
        then all entries of the map will be copied to another JVM for
        fail-safety. Valid numbers are 0 (no backup), 1, 2, 3.
    -->
    <backup-count>1</backup-count>

    <!--
        Maximum number of seconds for each entry to stay in the map. Entries that are
        older than <time-to-live-seconds> and not updated for <time-to-live-seconds>
        will get automatically evicted from the map.
        Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.
    -->
    <time-to-live-seconds>0</time-to-live-seconds>

    <!--
        Maximum number of seconds for each entry to stay idle in the map. Entries that are
        idle(not touched) for more than <max-idle-seconds> will get
        automatically evicted from the map.
        Entry is touched if get, put or containsKey is called.
        Any integer between 0 and Integer.MAX_VALUE.
        0 means infinite. Default is 0.
    -->
    <max-idle-seconds>0</max-idle-seconds>

    <!--
        Valid values are:
        NONE (no extra eviction, <time-to-live-seconds> may still apply),
        LRU  (Least Recently Used),
        LFU  (Least Frequently Used).
        NONE is the default.
        Regardless of the eviction policy used, <time-to-live-seconds> will still apply. 
    -->
    <eviction-policy>NONE</eviction-policy>

    <!--
        Maximum size of the map. When max size is reached,
        map is evicted based on the policy defined.
        Any integer between 0 and Integer.MAX_VALUE. 0 means
        Integer.MAX_VALUE. Default is 0.
    -->
    <max-size policy="cluster_wide_map_size">0</max-size>

    <!--
        When max. size is reached, specified percentage of
        the map will be evicted. Any integer between 0 and 100.
        If 25 is set for example, 25% of the entries will
        get evicted.
    -->
    <eviction-percentage>25</eviction-percentage>
   <!--
        Specifies when eviction will be started. Default value is 3. 
       So every 3 (+up to 5 for performance reasons) seconds 
       eviction will be kicked of. Eviction is costly operation, setting 
       this number too low, can decrease the performance. 
   -->
  <eviction-delay-seconds>3</eviction-delay-seconds>
</map>
</hazelcast>

虽然这是一个相当愚蠢的配置,但这只是内存不足情况的一个示例。

4

2 回答 2

10

默认情况下,Hazelcast 会立即终止内存不足的节点。请参阅DefaultOutOfMemoryHandler

如果您希望更改默认行为,您可以实现OutOfMemoryHandler

public class MyOutOfMemoryHandler extends OutOfMemoryHandler {

     public void onOutOfMemory(OutOfMemoryError oom, HazelcastInstance[] instances) {
        // handle oom
    }
}

并通过注册

Hazelcast.setOutOfMemoryHandler(new MyOutOfMemoryHandler());

于 2013-06-07T06:59:45.010 回答
6

在队列的情况下,如果你不能像生产一样快地消费,那么最终你可能会耗尽内存,这意味着 JVM 会在你尝试创建新对象时抛出 OutOfMemoryException。如果可能有积压,那么设置队列的最大大小通常是一个好习惯。

如果您尝试在地图中插入太多条目,则可能会出现内存不足的地图。

问题类似于“我有一个本地 java.util.HashMap,如果我尝试向其中插入 10 亿个条目会发生什么?如果所有内存都被使用会发生什么”。因此,Hazelcast 并没有那么具体。让我知道我是否遗漏了问题中的任何内容。

-塔利普

于 2013-06-07T05:52:52.583 回答