我尝试使用Set
接口作为 hazelcastIMap
实例的值,当我运行测试时,我发现该测试挂在方法ConcurrentMap#compute
。
为什么我在这段代码中使用hazelcast
IMap
时会有无限循环:
import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.IMap;
import java.io.Serializable;
import java.util.*;
public class Main {
public static void main(String[] args) {
IMap<String, HashSet<StringWrapper>> store = Hazelcast.newHazelcastInstance(
new Config().addMapConfig(new MapConfig("store"))
).getMap("store");
store.compute("user", (k, value) -> {
HashSet<StringWrapper> newValues = Objects.isNull(value) ? new HashSet<>() : new HashSet<>(value);
newValues.add(new StringWrapper("user"));
return newValues;
});
store.compute("user", (k, value) -> {
HashSet<StringWrapper> newValues = Objects.isNull(value) ? new HashSet<>() : new HashSet<>(value);
newValues.add(new StringWrapper("user"));
return newValues;
});
System.out.println(store.keySet());
}
// Data class
public static class StringWrapper implements Serializable {
String value;
public StringWrapper() {}
public StringWrapper(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
StringWrapper value = (StringWrapper) o;
return Objects.equals(this.value, value.value);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), value);
}
}
}
榛子:3.9.3
爪哇:build 1.8.0_161-b12
操作系统:macOS High Sierra 10.13.3
@Alykoff我根据上面的示例和ArrayList版本重现了该问题,该版本被报告为github问题:https://github.com/hazelcast/hazelcast/issues/12557。
有 2 个单独的问题:
1 - 使用 HashSet 时,问题在于 Java 如何反序列化 HashSet/ArrayList(集合)以及compute
方法如何工作。在方法compute
内部(因为 Hazelcast 符合 Java 6 并且没有compute
方法可以覆盖,默认实现从ConcurrentMap
调用),这个块会导致无限循环:
// replace
if (replace(key, oldValue, newValue)) {
// replaced as expected.
return newValue;
}
// some other value replaced old value. try again.
oldValue = get(key);
此replace
方法调用 IMap 替换方法。IMap 检查当前值是否等于用户提供的值。但是由于Java序列化optimization
,检查失败。请检查HashSet.readObject
方法。您将看到,在反序列化 HashSet 时,由于元素大小是已知的,它会创建具有容量的内部 HashMap:
// Set the capacity according to the size and load factor ensuring that
// the HashMap is at least 25% full but clamping to maximum capacity.
capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),
HashMap.MAXIMUM_CAPACITY);
但是您的HashSet
,在没有初始容量的情况下创建,默认容量为 16,而反序列化的初始容量为 1。这改变了序列化,索引 51 包含当前容量,并且似乎 JDK 在反序列化对象时根据大小重新计算它以最小化大小。
请参阅以下示例:
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String, Collection<String>> store = instance.getMap("store");
Collection<String> val = new HashSet<>();
val.add("a");
store.put("a", val);
Collection<String> oldVal = store.get("a");
byte[] dataOld = ((HazelcastInstanceProxy) hz).getSerializationService().toBytes(oldVal);
byte[] dataNew = ((HazelcastInstanceProxy) hz).getSerializationService().toBytes(val);
System.out.println(Arrays.equals(dataNew, dataOld));
此代码打印false
.但是,如果您创建的HashSet
初始大小为1
,则两个字节数组相等。在你的情况下,你不会得到一个无限循环。
2 - 使用ArrayList
或任何其他集合时,您上面指出的另一个问题。由于该方法在ConcurrentMap
中的实现方式compute
,当您将旧值分配给newValue
并添加新元素时,您实际上修改了oldValue
从而导致替换方法失败。但是当你将代码更改为new ArrayList(value)
时,现在你正在创建一个new ArrayList
和value
集合不会被修改。如果您不想修改原始集合,则最佳做法是在使用集合之前包装集合。由于我解释的第一个问题,如果您使用大小1
创建,则同样适用于HashSet
。
所以在你的情况下,你应该使用
Collection<String> newValues = Objects.isNull(value) ? new HashSet<>(1) : new HashSet<>(value);
或
Collection<String> newValues = Objects.isNull(value) ? new ArrayList<>() : new ArrayList<>(value);
HashSet
这种情况似乎是JDK问题,而不是优化。我不知道这些情况中的任何一个都可以在 Hazelcast 中解决/修复,除非 Hazalcast 覆盖HashXXX
集合序列化并覆盖compute
方法。