Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hazelcast Ringbuffer readManyAsync returns Empty Results

Tags:

java

hazelcast

I'm trying to read N items from a RingBuffer using readManyAsync but It's always returns an empty resultSet. If I use readOne I get data.

I'm using the readManyAsync as the documentation specify. There is another way to do that?

Enviroment:

  • Java 8
  • Hazelcast 3.5.3

Example:

Ringbuffer<String> buffer = this.hazelcastInstance.getRingbuffer("testBuffer");
buffer.add("a");
buffer.add("b");
buffer.add("c");
Long sequence = buffer.headSequence();
ICompletableFuture<ReadResultSet<String>> resultSetFuture = buffer.readManyAsync(sequence, 0, 3, null);
ReadResultSet<String> resultSet = resultSetFuture.get();

System.out.println("*** readManyAsync *** readCount: " + resultSet.readCount());
int count = 0;
for (String s : resultSet) {
    System.out.println(count + " - " + s);
    count++;
}

System.out.println("*** readOne ***");
for (int i = 0; i < 3; i++) {
    System.out.println(i + " - " + buffer.readOne(i));
}

Output:

*** readManyAsync *** readCount: 0
*** readOne ***
0 - a
1 - b
2 - c
like image 876
Diego D Avatar asked Oct 13 '15 18:10

Diego D


2 Answers

You are happy with receiving zero results:

buffer.readManyAsync(sequence, 0, 3, null);

Try changing 0 to 1.

buffer.readManyAsync(sequence, 1, 3, null);

Now the call will block till there is at least 1 result.

Probably you can make things more efficient by asking for more than 3 items. In most cases, retrieving data is cheap, but the io/operation scheduling is expensive. So try to batch as much as possible. So try to get as many results as possible.. e.g. 100... or 1000 (which is the max).

like image 156
pveentjer Avatar answered Dec 16 '22 23:12

pveentjer


Ok, but how do you use readManyAsync in a non-blocking way, with minCount to 0 ?

I made a minimal test case, and I really can't figure it out. I posted a support topic here :

https://groups.google.com/forum/#!topic/hazelcast/FGnLWDGrzb8

As an answer : I use readManyAsync with a timeout, like so :

try{
  buffer.readManyAsync(sequence, 1, 3, null).get(500, TimeUnit.MILLISECONDS);
} catch (TimeoutException e){
  // We timed out, shame, let's move on
}

That seems the only way to make a graceful non-blocking thread, but by reading the doc, I really thought a minCount=0 would do the trick.

like image 25
Brice Avatar answered Dec 17 '22 00:12

Brice