Refactor[mqbs::FileStore]: Encapsulate write-head cursor accessors - #1673
Conversation
Signed-off-by: Yuan Jing Vincent Yan <yyan82@bloomberg.net>
Signed-off-by: Yuan Jing Vincent Yan <yyan82@bloomberg.net>
Signed-off-by: Yuan Jing Vincent Yan <yyan82@bloomberg.net>
| { | ||
| return d_highestSeqNums[d_writeHeadLeaseId]; | ||
| d_writeHeadLeaseId = leaseId; | ||
| d_highestSeqNums[leaseId] = seqNum; |
There was a problem hiding this comment.
Should this method has any preconditions such as d_writeHeadLeaseId <= leaseId?
There was a problem hiding this comment.
Not yet, in legacy mode in FileStore::clearPrimary() there is a write head rollbacks mechanism. I am trying to eliminate rollbacks but will do it across a series of PRs to make the changes gradual.
| inline bsls::Types::Uint64& FileStore::currentSeqNumRef() | ||
| inline bsls::Types::Uint64 FileStore::incrementWriteHeadSeqNum() | ||
| { | ||
| return ++d_highestSeqNums.at(d_writeHeadLeaseId); |
There was a problem hiding this comment.
.at might throw an exception and the current design choice is not to throw exceptions in the code:
at(const LOOKUP_KEY& key) {
HashTableLink *node = d_impl.find(key);
if (!node) {
BloombergLP::bslstl::StdExceptUtil::throwOutOfRange(
"unordered_map<...>::at(LOOKUP_KEY): invalid key value");
}
return static_cast<HashTableNode *>(node)->value().second;
}
What about changing it to
auto iter = d_highestSeqNums.find(d_writeHeadLeaseId);
BSLS_ASSRT_SAFE(iter != d_highestSeqNums.end());
++iter.second;
There was a problem hiding this comment.
No auto in C++03, but I will do the assert instead.
| qlistRecTotalLength); | ||
|
|
||
| DataStoreRecordKey key(sequenceNumber(), d_writeHeadLeaseId); | ||
| DataStoreRecordKey key(writeHeadSeqNum(), d_writeHeadLeaseId); |
There was a problem hiding this comment.
Found it weird that DataStoreRecordKey has inverted order of args compared to basically everything in this file, for example:
setWriteHead(recHeader->primaryLeaseId(),
recHeader->sequenceNumber());
There was a problem hiding this comment.
I can also fix it in this PR. Still pure refactoring so won't be too much.
Signed-off-by: Yuan Jing Vincent Yan <yyan82@bloomberg.net>
Signed-off-by: Yuan Jing Vincent Yan <yyan82@bloomberg.net>
|
|
||
| unsigned int d_primaryLeaseId; | ||
|
|
||
| bsls::Types::Uint64 d_sequenceNum; |
There was a problem hiding this comment.
Now member alignment is not ideal:
d_primaryLeaseId - 4 bytes
[empty space] - 4 bytes
d_sequenceNum - 8 bytes
If only we could use 64bit type for leaseId too...
There was a problem hiding this comment.
Switched them back only in the data member declarations.
| DataStoreRecordKey(const bsls::Types::Uint64 sequenceNum, | ||
| unsigned int primaryLeaseId); | ||
| DataStoreRecordKey(unsigned int primaryLeaseId, | ||
| const bsls::Types::Uint64 sequenceNum); |
There was a problem hiding this comment.
| const bsls::Types::Uint64 sequenceNum); | |
| bsls::Types::Uint64 sequenceNum); |
This is a basic type, no need to have const
| BSLS_ASSERT_SAFE(d_highestSeqNums.find(d_writeHeadLeaseId) != | ||
| d_highestSeqNums.end()); | ||
|
|
||
| return ++d_highestSeqNums[d_writeHeadLeaseId]; |
There was a problem hiding this comment.
There are 2 lookups now when we can have 1 for better performance.
BSLS_ASSERT_SAFE(d_highestSeqNums.find(d_writeHeadLeaseId) !=
d_highestSeqNums.end());
This check searches for iterator and immediately drops it, when it can be reused to increment the value
Signed-off-by: Yuan Jing Vincent Yan <yyan82@bloomberg.net>
Summary
(leaseId, seqNum)cursor identifying the next record this store writes or applies — behind a small, intention-revealing accessor family, instead of scattered raw field/map manipulation.d_primaryLeaseId→d_writeHeadLeaseIdin Refactor[mqbs::FileStore]: Rename d_primaryLeaseId to d_writeHeadLeaseId #1660, accessorprimaryLeaseId()→writeHeadLeaseId()in Refactor[mqbs::DataStore]: Rename primaryLeaseId to writeHeadLeaseId #1666): the two halves of the cursor now have symmetric names and the leaky mutable-reference accessor is gone.