问题
We are planning to use Infinispan in client server mode. The architecture has many clients (client 1, client 2 and so on ) and a distributed infinispan network.
We need to update the data in the cache periodically, say every5 hours . All clients could be able to update the data. If one of them (say client 1) is updating we need to prevent others doing the same job. Once the updating is complete all clients wait another 5 hour and, any of them will do the the updating again.
Infinispan providing a versioned operation for this, But during the testing this methord is giving invalid result.
String key="test";
RemoteCacheManager cacheManager = new RemoteCacheManager();
RemoteCache<String, Object> remoteCache = cacheManager.getCache("MyCache");
remoteCache.put(key, new Object());
for (int i = 1; i < 5; i++) {
System.out.println("version Before:" + remoteCache.getVersioned(key).getVersion());
System.out.println("version to put:"+(i));
System.out.println(remoteCache.replaceWithVersion(key, new Object(),i));
System.out.println("version after:" + remoteCache.getVersioned(key).getVersion());
System.out.println("---------------------");
}
This is giving correct result like,
version Before:1
version to put:1
true
version after:2
---------------------
version Before:2
version to put:2
true
version after:3
---------------------
version Before:3
version to put:3
true
version after:4
---------------------
But once I add a new different key to the same cache the version to the old key is giving incorrectly
for (int i = 1; i < 5; i++) {
remoteCache.put("Hello", new Object());
System.out.println("version Before:" + remoteCache.getVersioned(key).getVersion());
System.out.println("version to put:"+(i));
System.out.println(remoteCache.replaceWithVersion(key, new Object(),i));
System.out.println("version after:" + remoteCache.getVersioned(key).getVersion());
System.out.println("---------------------");
}
version Before:1
version to put:1
true
version after:3
---------------------
version Before:3
version to put:2
false
version after:3
---------------------
version Before:3
version to put:3
true
version after:6
---------------------
version Before:6
version to put:4
false
version after:6
---------------------
Looks like version is changing irrespective of the key but for cache. Because while inserting a different key to cache the version of existing also changes.
Update:- This was not a bug, it is the expected behavior, see the answer and its discussion.
回答1:
This is expected behaviour. When you write a new entry into the cache, it gets a new version. New versions are obtained from atomic counter which makes sure that always some new version is generated. You cannot know this new version unless you call the getWithVersion.
来源:https://stackoverflow.com/questions/21522203/infinispan-the-versioned-operation-returning-incorrect-results