Hazelcast C++ Client
Hazelcast C++ Client Library
hazelcast::client::ringbuffer Class Reference

A Ringbuffer is a data-structure where the content is stored in a ring like structure. More...

#include <ringbuffer.h>

+ Inheritance diagram for hazelcast::client::ringbuffer:

Public Member Functions

template<typename E >
boost::future< int64_t > add (const E &item)
 Adds an item to the tail of the Ringbuffer. More...
 
template<typename E >
boost::future< boost::optional< E > > read_one (int64_t sequence)
 Reads one item from the Ringbuffer. More...
 
template<typename E >
boost::future< int64_t > add (const E &item, rb::overflow_policy overflow_policy)
 Asynchronously writes an item with a configurable rb::OverflowPolicy. More...
 
template<typename E >
boost::future< int64_t > add_all (const std::vector< E > &items, rb::overflow_policy overflow_policy)
 Adds all the items of a collection to the tail of the Ringbuffer. More...
 
template<typename IFUNCTION >
boost::future< rb::read_result_setread_many (int64_t start_sequence, int32_t min_count, int32_t max_count, const IFUNCTION *filter=nullptr)
 Reads a batch of items from the Ringbuffer. More...
 
rb::read_result_set get_result_set (boost::future< protocol::ClientMessage > f)
 
boost::future< rb::read_result_setread_many (int64_t start_sequence, int32_t min_count, int32_t max_count)
 

Friends

class spi::ProxyManager
 

Detailed Description

A Ringbuffer is a data-structure where the content is stored in a ring like structure.

A ringbuffer has a capacity so it won't grow beyond that capacity and endanger the stability of the system. If that capacity is exceeded, than the oldest item in the ringbuffer is overwritten.

The ringbuffer has 2 always incrementing sequences:

  1. tailSequence: this is the side where the youngest item is found. So the tail is the side of the ringbuffer where items are added to.
  2. headSequence: this is the side where the oldest items are found. So the head is the side where items gets discarded.

The items in the ringbuffer can be found by a sequence that is in between (inclusive) the head and tail sequence.

If data is read from a ringbuffer with a sequence that is smaller than the headSequence, it means that the data is not available anymore and a stale_sequence is thrown.

A Ringbuffer currently is not a distributed data-structure. So all data is stored in a single partition; comparable to the IQueue implementation. But we'll provide an option to partition the data in the near future.

A Ringbuffer can be used in a similar way as a queue, but one of the key differences is that a queue.take is destructive, meaning that only 1 thread is able to take an item. A ringbuffer.read is not destructive, so you can have multiple threads reading the same item multiple times.

The Ringbuffer is the backing data-structure for the reliable com.hazelcast.core.ITopic implementation. See com.hazelcast.config.ReliableTopicConfig.

Definition at line 59 of file ringbuffer.h.

Member Function Documentation

◆ add() [1/2]

template<typename E >
boost::future<int64_t> hazelcast::client::ringbuffer::add ( const E &  item)
inline

Adds an item to the tail of the Ringbuffer.

If there is no space in the Ringbuffer, the add will overwrite the oldest item in the ringbuffer no matter what the ttl is. For more control on this behavior.

The returned value is the sequence of the added item. Using this sequence you can read the added item.

Using the sequence as id

This sequence will always be unique for this Ringbuffer instance so it can be used as a unique id generator if you are publishing items on this Ringbuffer. However you need to take care of correctly determining an initial id when any node uses the ringbuffer for the first time. The most reliable way to do that is to write a dummy item into the ringbuffer and use the returned sequence as initial id. On the reading side, this dummy item should be discard. Please keep in mind that this id is not the sequence of the item you are about to publish but from a previously published item. So it can't be used to find that item.

Parameters
itemthe item to add.
Returns
the sequence of the added item.

Definition at line 80 of file ringbuffer.h.

80  {
81  return add_data(to_data(item));
82  }

◆ add() [2/2]

template<typename E >
boost::future<int64_t> hazelcast::client::ringbuffer::add ( const E &  item,
rb::overflow_policy  overflow_policy 
)
inline

Asynchronously writes an item with a configurable rb::OverflowPolicy.

If there is space in the Ringbuffer, the call will return the sequence of the written item. If there is no space, it depends on the overflow policy what happens:

  1. rb::OverflowPolicy#OVERWRITE: we just overwrite the oldest item in the Ringbuffer and we violate the ttl
  2. rb::OverflowPolicy#FAIL: we return -1

The reason that FAIL exist is to give the opportunity to obey the ttl. If blocking behavior is required, this can be implemented using retrying in combination with an exponential backoff. Example:

int64_t sleepMs = 100;
for (; ; ) {
int64_t result = *(ringbuffer.addAsync(item, FAIL)->get());
if (result != -1) {
break;
}
util::sleepMillis(sleepMs);
sleepMs = min(5000, sleepMs * 2);
}

<p<blockquote>

Parameters
itemthe item to add
overflowPolicythe rb::OverflowPolicy to use.
Returns
the sequenceId of the added item, or -1 if the add failed.

Definition at line 155 of file ringbuffer.h.

155  {
156  return addData(to_data(item), overflow_policy);
157  }

◆ add_all()

template<typename E >
boost::future<int64_t> hazelcast::client::ringbuffer::add_all ( const std::vector< E > &  items,
rb::overflow_policy  overflow_policy 
)
inline

Adds all the items of a collection to the tail of the Ringbuffer.

An add_all is likely to outperform multiple calls to add(const E&) due to better io utilization and a reduced number of executed operations. If the batch is empty, the call is ignored.

If the collection is larger than the capacity of the Ringbuffer, then the items that were written first will be overwritten. Therefore this call will not block.

The items are inserted in the order of the Iterator of the collection. If an add_all is executed concurrently with an add or add_all, no guarantee is given that items are contiguous.

The result of the future contains the sequenceId of the last written item.

Parameters
collectionthe batch of items to add.
Returns
the future to synchronize on completion. The result of the future contains the sequenceId of the last written item.
Exceptions
illegal_argumentif items is empty

Definition at line 186 of file ringbuffer.h.

186  {
187  return add_all_data(to_data_collection(items), overflow_policy);
188  }

◆ read_many()

template<typename IFUNCTION >
boost::future<rb::read_result_set> hazelcast::client::ringbuffer::read_many ( int64_t  start_sequence,
int32_t  min_count,
int32_t  max_count,
const IFUNCTION *  filter = nullptr 
)
inline

Reads a batch of items from the Ringbuffer.

If the number of available items after the first read item is smaller than the

maxCount

, these items are returned. So it could be the number of items read is smaller than the

maxCount

.

If there are less items available than

minCount

, then this call blocks.

Reading a batch of items is likely to perform better because less overhead is involved.

A filter can be provided to only select items that need to be read. If the filter is null, all items are read. If the filter is not null, only items where the filter function returns true are returned. Using filters is a good way to prevent getting items that are of no value to the receiver. This reduces the amount of IO and the number of operations being executed, and can result in a significant performance improvement.

For each item not available in the Ringbuffer an attempt is made to read it from the underlying {com.hazelcast.core.RingbufferStore} via multiple invocations of {com.hazelcast.core.RingbufferStore::load(long)}, if store is configured for the Ringbuffer. These cases may increase the execution time significantly depending on the implementation of the store. Note that exceptions thrown by the store are propagated to the caller.

Parameters
startSequencethe startSequence of the first item to read.
minCountthe minimum number of items to read.
maxCountthe maximum number of items to read.
filterthe filter. Filter is allowed to be null, indicating there is no filter.
Returns
a future containing the items read.
Exceptions
illegal_argumentif startSequence is smaller than 0 or if startSequence larger than tailSequence() or if minCount smaller than 0 or if minCount larger than maxCount, or if maxCount larger than the capacity of the ringbuffer or if maxCount larger than 1000 (to prevent overload)

Definition at line 231 of file ringbuffer.h.

231  {
232  auto filter_data = to_data<IFUNCTION>(filter);
233  return read_many_data(start_sequence, min_count, max_count, &filter_data).then(boost::launch::sync, [=] (boost::future<protocol::ClientMessage> f) {
234  return get_result_set(std::move(f));
235  });
236  }

◆ read_one()

template<typename E >
boost::future<boost::optional<E> > hazelcast::client::ringbuffer::read_one ( int64_t  sequence)
inline

Reads one item from the Ringbuffer.

If the sequence is one beyond the current tail, this call blocks until an item is added.

This means that the ringbuffer can be processed using the following idiom: Ringbuffer<String> ringbuffer = client.getRingbuffer<E>("rb"); long seq = ringbuffer.headSequence(); while(true){ String item = ringbuffer.readOne(seq); seq++; ... process item }

This method is not destructive unlike e.g. a queue.take. So the same item can be read by multiple readers or it can be read multiple times by the same reader.

Currently it isn't possible to control how long this call is going to block. In the future we could add e.g. tryReadOne(long sequence, long timeout, TimeUnit unit).

Parameters
sequencethe sequence of the item to read.
Returns
the read item
Exceptions
stale_sequenceif the sequence is smaller then headSequence(). Because a Ringbuffer won't store all event indefinitely, it can be that the data for the given sequence doesn't exist anymore and the stale_sequence is thrown. It is up to the caller to deal with this particular situation, e.g. throw an Exception or restart from the last known head. That is why the stale_sequence contains the last known head.
illegal_argumentif sequence is smaller than 0 or larger than tailSequence()+1.
interruptedif the call is interrupted while blocking.

Definition at line 118 of file ringbuffer.h.

118  {
119  return to_object<E>(read_one_data(sequence));
120  }

The documentation for this class was generated from the following file: