Skip to content

Instantly share code, notes, and snippets.

@joeplanisky-temboo
Created November 20, 2012 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeplanisky-temboo/4118374 to your computer and use it in GitHub Desktop.
Save joeplanisky-temboo/4118374 to your computer and use it in GitHub Desktop.
Hazelcast issue #359 demo: Query returns stale data.
import java.io.Serializable;
import java.util.Random;
import java.util.UUID;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.IMap;
import com.hazelcast.query.EntryObject;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.PredicateBuilder;
public class Demo {
public static void main(String[] args) throws Exception {
IMap<String, Blob> blobs = Hazelcast.getMap("blobs");
String name = null;
for (int i = 0; i < 10; i++) {
name = UUID.randomUUID().toString();
blobs.put(name, new Blob(name));
}
int errorCount = 0;
Random random = new Random();
System.out.println("*************** RUNNING ****************");
while(errorCount < 100) {
long now = System.currentTimeMillis();
Blob b = blobs.get(name);
b.setLastUpdateTime(now);
blobs.put(name,b);
Thread.sleep(random.nextInt(100) + 1);
EntryObject e = new PredicateBuilder().getEntryObject();
Predicate<?,?> predicate = e.get("name").equal(name);
long queryTime = blobs.values(predicate).iterator().next().getLastUpdateTime();
long getTime = blobs.get(name).getLastUpdateTime();
if (queryTime != getTime) {
System.out.println("ERROR! Expected " + getTime + " Got " + queryTime);
errorCount++;
}
}
System.exit(errorCount);
}
}
class Blob implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private long lastUpdateTime;
public Blob(String name) {
this.name = name;
this.lastUpdateTime = System.currentTimeMillis();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(long lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
}
<hazelcast>
<properties>
<property name="hazelcast.version.check.enabled">false</property>
<!-- Reduce the 'hang' time during network failures between nodes -->
<property name="hazelcast.icmp.enabled">true</property>
</properties>
<group>
<name>hz-index-test</name>
<password>supersecret</password>
</group>
<network>
<port auto-increment="true">5701</port>
<join>
<multicast enabled="true">
<multicast-group>224.2.2.3</multicast-group>
<multicast-port>54327</multicast-port>
</multicast>
</join>
<interfaces enabled="true">
<interface>127.0.0.*</interface>
</interfaces>
</network>
<map name="blobs">
<backup-count>1</backup-count>
<read-backup-data>false</read-backup-data>
<eviction-policy>NONE</eviction-policy>
<max-size>0</max-size>
<cache-value>false</cache-value>
<time-to-live-seconds>0</time-to-live-seconds>
<indexes>
<index ordered="false">name</index>
</indexes>
</map>
</hazelcast>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment