26 #ifndef _HYBRIDPUBLISHSTORE_H_ 27 #define _HYBRIDPUBLISHSTORE_H_ 57 : _store(NULL), _handler(NULL), _data(NULL)
70 volatile size_t _entries, _errorCount;
71 volatile amps_uint64_t _lastIndex;
73 SwappingOutReplayer(
PublishStore* pStore_,
size_t entries_)
74 : _pStore(pStore_), _entries(entries_)
75 , _errorCount(0), _lastIndex(0)
78 size_t getErrors() {
return _errorCount; }
80 amps_uint64_t lastIndex() {
return _lastIndex; }
84 if(_entries > 0 && _errorCount == 0)
89 _pStore->
store(message_,
false);
112 :
StoreImpl(), _memStore(maxMemoryCapacity_), _fileStore(fileName_),
113 _cap(maxMemoryCapacity_),
114 _lowWatermark((size_t)((double)maxMemoryCapacity_ * 0.5)),
115 _lowestIndexInMemory(0), _holdSwapping(false)
117 _handlerData._store =
this;
130 :
StoreImpl(), _memStore(maxMemoryCapacity_), _fileStore(fileName_),
131 _cap(maxMemoryCapacity_),
132 _lowWatermark((size_t)((double)maxMemoryCapacity_ * 0.5)),
133 _lowestIndexInMemory(0), _holdSwapping(false)
135 _handlerData._store =
this;
148 Lock<Mutex> guard(_lock);
149 _lowWatermark = lowWatermark_;
160 Lock<Mutex> guard(_lock);
161 return _lowWatermark;
169 Lock<Mutex> guard(_lock);
170 while (_holdSwapping)
172 Unlock<Mutex> u(_lock);
176 FlagFlip flip(&_holdSwapping);
177 Unlock<Mutex> u(_lock);
180 _memStore.discardUpTo(_fileStore.getLastPersisted());
183 _fileStore.discardUpTo(index_);
184 if (_lowestIndexInMemory <= index_)
186 _memStore.discardUpTo(index_);
187 _lowestIndexInMemory = index_ + 1;
198 Lock<Mutex> guard(_lock);
199 while (_holdSwapping)
201 Unlock<Mutex> u(_lock);
205 FlagFlip flip(&_holdSwapping);
206 Unlock<Mutex> u(_lock);
207 _fileStore.replay(replayer_);
208 _memStore.replay(replayer_);
216 return _fileStore.unpersistedCount() + _memStore.unpersistedCount();
229 Lock<Mutex> guard(_lock);
230 amps_uint64_t waitFor = _getHybridHighestUnpersisted();
233 if (waitFor == unset)
return;
236 bool timedOut =
false;
237 AMPS_START_TIMER(timeout_)
239 while (!timedOut && waitFor >= _getHybridLowestUnpersisted() &&
240 _getHybridLowestUnpersisted() != unset)
242 if (!_lock.wait(timeout_))
245 AMPS_RESET_TIMER(timedOut, timeout_);
249 if (timedOut && waitFor >= _getHybridLowestUnpersisted() &&
250 _getHybridLowestUnpersisted() != unset)
252 throw TimedOutException(
"Timed out waiting to flush publish store.");
257 while (waitFor >= _getHybridLowestUnpersisted() &&
258 _getHybridLowestUnpersisted() != unset)
262 Unlock<Mutex> unlck(_lock);
263 amps_invoke_waiting_function();
270 amps_uint64_t lowestIndexInMemory;
272 Lock<Mutex> guard(_lock);
273 lowestIndexInMemory = _lowestIndexInMemory;
275 if(index_ < lowestIndexInMemory)
277 return _fileStore.replaySingle(replayer_, index_);
281 return _memStore.replaySingle(replayer_, index_);
290 Lock<Mutex> guard(_lock);
292 while (_holdSwapping)
294 Unlock<Mutex> u(_lock);
297 if(_memStore.unpersistedCount() >= _cap && !_holdSwapping)
300 FlagFlip flip(&_holdSwapping);
301 SwappingOutReplayer swapper(&_fileStore,
302 _memStore.unpersistedCount() - _lowWatermark);
304 Unlock<Mutex> u(_lock);
305 _memStore.replay(swapper);
307 if(swapper.getErrors() == 0)
309 _lowestIndexInMemory = swapper.lastIndex();
310 _memStore.discardUpTo(_lowestIndexInMemory++);
313 return _memStore.store(message_);
318 _handlerData.init(handler_, data_);
319 _fileStore.setResizeHandler(HybridPublishStore::resizeHandler,
320 (
void*)&_handlerData);
325 return _handlerData._handler;
330 Lock<Mutex> guard(_lock);
331 return _getHybridLowestUnpersisted();
334 amps_uint64_t getHighestUnpersisted()
const 336 Lock<Mutex> guard(_lock);
337 return _getHybridHighestUnpersisted();
342 Lock<Mutex> guard(_lock);
343 return _getHybridLastPersisted();
348 static bool resizeHandler(
Store store_,
size_t size_,
void* data_)
350 HandlerData* handlerData = (HandlerData*)data_;
352 return handlerData->_handler(store_, size_, handlerData->_data);
356 amps_uint64_t _getHybridLowestUnpersisted()
const 358 amps_uint64_t filemin = _fileStore.getLowestUnpersisted();
359 amps_uint64_t memmin = _memStore.getLowestUnpersisted();
360 if (filemin == AMPS_UNSET_SEQUENCE)
362 if (memmin == AMPS_UNSET_SEQUENCE || memmin > filemin)
369 amps_uint64_t _getHybridHighestUnpersisted()
const 371 amps_uint64_t filemax = _fileStore.getHighestUnpersisted();
372 amps_uint64_t memmax = _memStore.getHighestUnpersisted();
373 if (filemax == AMPS_UNSET_SEQUENCE)
375 if (memmax == AMPS_UNSET_SEQUENCE || memmax < filemax)
381 amps_uint64_t _getHybridLastPersisted()
384 if (!_lowestIndexInMemory &&
385 _fileStore.unpersistedCount() == 0)
387 _fileStore.discardUpTo(_memStore.getLastPersisted());
388 return _fileStore.getLastPersisted();
390 amps_uint64_t memLast = _memStore.getLastPersisted();
391 amps_uint64_t fileLast = _fileStore.getLastPersisted();
392 return (memLast < fileLast) ? memLast : fileLast;
398 size_t _lowWatermark;
399 amps_uint64_t _lowestIndexInMemory;
401 HandlerData _handlerData;
402 volatile bool _holdSwapping;
408 #endif //_HYBRIDPUBLISHSTORE_H_ Abstract base class for storing published messages for an HA publisher client.
Definition: ampsplusplus.hpp:759
Abstract base class for replaying a publish message.
Definition: ampsplusplus.hpp:731
Message encapsulates a single message sent to or received from an AMPS server, and provides methods f...
Definition: Message.hpp:447
Provides AMPS::PublishStore, a publish store that uses memory-mapped files to provide a publish store...
void replay(StoreReplayer &replayer_)
Used internally by Client to replay messages in the store to AMPS after a successful connection...
Definition: HybridPublishStore.hpp:196
amps_uint64_t store(const Message &message_)
Used internally by Client to put messages into the Store.
Definition: HybridPublishStore.hpp:288
virtual void flush(long timeout_)
Method to wait for the Store to discard everything that has been stored up to the point in time when ...
Definition: HybridPublishStore.hpp:227
size_t unpersistedCount() const
The number of messages in the Store that have not been discarded.
Definition: HybridPublishStore.hpp:214
A StoreImpl implementation that uses a memory-mapped file to provide a publish store that persists ac...
Definition: PublishStore.hpp:46
amps_uint64_t getLowestUnpersisted() const
Get the oldest unpersisted message sequence in the store.
Definition: HybridPublishStore.hpp:328
Core type, function, and class declarations for the AMPS C++ client.
HybridPublishStore(const char *fileName_, size_t maxMemoryCapacity_)
Create a HybridPublishStore that will use fileName_ as its file storage and stores at most maxMemoryC...
Definition: HybridPublishStore.hpp:111
size_t getLowWatermark()
Get how many messags remain in memory after messages get offlined.
Definition: HybridPublishStore.hpp:158
void setLowWatermark(size_t lowWatermark_)
Set how many messags remain in memory after messages get offlined.
Definition: HybridPublishStore.hpp:146
bool(* PublishStoreResizeHandler)(Store store_, size_t size_, void *userData_)
Function type for PublishStore resize events The store_ param is store which is resizing.
Definition: ampsplusplus.hpp:753
Provides AMPS::MemoryPublishStore, a publish store that holds messages in memory. ...
void setResizeHandler(PublishStoreResizeHandler handler_, void *data_)
Set a handler to be called if the Store needs to resize in order to keep storing messages.
Definition: HybridPublishStore.hpp:316
void discardUpTo(amps_uint64_t index_)
Discard all messages in the store up to and including index_.
Definition: HybridPublishStore.hpp:167
Handle class for StoreImpl classes that track publish messages.
Definition: ampsplusplus.hpp:857
amps_uint64_t getLastPersisted(void)
Get the last persisted sequence number.
Definition: HybridPublishStore.hpp:340
virtual amps_uint64_t store(const Message &message_)
Store a given message that will be delivered to AMPS.
Definition: BlockPublishStore.hpp:247
bool replaySingle(StoreReplayer &replayer_, amps_uint64_t index_)
Called by Client to get a single message replayed by the store onto the StoreReplayer.
Definition: HybridPublishStore.hpp:268
HybridPublishStore(const std::string &fileName_, size_t maxMemoryCapacity_)
Create a HybridPublishStore that will use fileName_ as its file storage and stores at most maxMemoryC...
Definition: HybridPublishStore.hpp:129
Definition: ampsplusplus.hpp:103
static amps_uint64_t getUnsetSequence()
Method to return the value used to represent no such sequence.
Definition: ampsplusplus.hpp:815
A StoreImpl implementation that uses MemoryStoreBuffer as its buffer to hold published messages in me...
Definition: MemoryPublishStore.hpp:44
amps_uint64_t amps_message_get_field_uint64(amps_handle message, FieldId field)
Gets the unsigned 64-bit int value of a header field in an AMPS message.
An implementation of StoreImpl for publication.
Definition: HybridPublishStore.hpp:47