Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 55abc0c

Browse files
mbutrovichtli2
authored andcommitted
TileGroupHeader refactor (#1423)
* TileGroupHeader refactor to get rid of hardcoded pointer arithmetic and casts. * Don't repeatedly look up the SpinLatch field. * clang-format. * Don't replace the SpinLatch on resetting of tuple. * Fix in TileGroupHeader assignment operator. * Update Field Descriptions based on refactor.
1 parent bf7ff62 commit 55abc0c

File tree

5 files changed

+112
-214
lines changed

5 files changed

+112
-214
lines changed

src/concurrency/timestamp_ordering_transaction_manager.cpp

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,60 +27,36 @@
2727
namespace peloton {
2828
namespace concurrency {
2929

30-
common::synchronization::SpinLatch *
31-
TimestampOrderingTransactionManager::GetSpinLatchField(
32-
const storage::TileGroupHeader *const tile_group_header,
33-
const oid_t &tuple_id) {
34-
return (
35-
common::synchronization::SpinLatch
36-
*)(tile_group_header->GetReservedFieldRef(tuple_id) + LOCK_OFFSET);
37-
}
38-
39-
cid_t TimestampOrderingTransactionManager::GetLastReaderCommitId(
40-
const storage::TileGroupHeader *const tile_group_header,
41-
const oid_t &tuple_id) {
42-
return *(cid_t *)(tile_group_header->GetReservedFieldRef(tuple_id) +
43-
LAST_READER_OFFSET);
44-
}
45-
4630
bool TimestampOrderingTransactionManager::SetLastReaderCommitId(
4731
const storage::TileGroupHeader *const tile_group_header,
4832
const oid_t &tuple_id, const cid_t &current_cid, const bool is_owner) {
4933
// get the pointer to the last_reader_cid field.
50-
cid_t *ts_ptr = (cid_t *)(tile_group_header->GetReservedFieldRef(tuple_id) +
51-
LAST_READER_OFFSET);
34+
cid_t read_ts = tile_group_header->GetLastReaderCommitId(tuple_id);
35+
36+
auto latch = tile_group_header->GetSpinLatch(tuple_id);
5237

53-
GetSpinLatchField(tile_group_header, tuple_id)->Lock();
38+
latch->Lock();
5439

5540
txn_id_t tuple_txn_id = tile_group_header->GetTransactionId(tuple_id);
5641

5742
if (is_owner == false && tuple_txn_id != INITIAL_TXN_ID) {
5843
// if the write lock has already been acquired by some concurrent
5944
// transactions,
6045
// then return without setting the last_reader_cid.
61-
GetSpinLatchField(tile_group_header, tuple_id)->Unlock();
46+
latch->Unlock();
6247
return false;
6348
} else {
6449
// if current_cid is larger than the current value of last_reader_cid field,
6550
// then set last_reader_cid to current_cid.
66-
if (*ts_ptr < current_cid) {
67-
*ts_ptr = current_cid;
51+
if (read_ts < current_cid) {
52+
tile_group_header->SetLastReaderCommitId(tuple_id, current_cid);
6853
}
6954

70-
GetSpinLatchField(tile_group_header, tuple_id)->Unlock();
55+
latch->Unlock();
7156
return true;
7257
}
7358
}
7459

75-
void TimestampOrderingTransactionManager::InitTupleReserved(
76-
const storage::TileGroupHeader *const tile_group_header,
77-
const oid_t tuple_id) {
78-
auto reserved_area = tile_group_header->GetReservedFieldRef(tuple_id);
79-
80-
new ((reserved_area + LOCK_OFFSET)) common::synchronization::SpinLatch();
81-
*(cid_t *)(reserved_area + LAST_READER_OFFSET) = 0;
82-
}
83-
8460
TimestampOrderingTransactionManager &
8561
TimestampOrderingTransactionManager::GetInstance(
8662
const ProtocolType protocol, const IsolationLevelType isolation,
@@ -138,25 +114,26 @@ bool TimestampOrderingTransactionManager::AcquireOwnership(
138114
// to acquire the ownership,
139115
// we must guarantee that no transaction that has read
140116
// the tuple has a larger timestamp than the current transaction.
141-
GetSpinLatchField(tile_group_header, tuple_id)->Lock();
117+
auto latch = tile_group_header->GetSpinLatch(tuple_id);
118+
latch->Lock();
142119
// change timestamp
143-
cid_t last_reader_cid = GetLastReaderCommitId(tile_group_header, tuple_id);
120+
cid_t last_reader_cid = tile_group_header->GetLastReaderCommitId(tuple_id);
144121

145122
// must compare last_reader_cid with a transaction's commit_id
146123
// (rather than read_id).
147124
// consider a transaction that is executed under snapshot isolation.
148125
// in this case, commit_id is not equal to read_id.
149126
if (last_reader_cid > current_txn->GetCommitId()) {
150-
GetSpinLatchField(tile_group_header, tuple_id)->Unlock();
127+
tile_group_header->GetSpinLatch(tuple_id)->Unlock();
151128

152129
return false;
153130
} else {
154131
if (tile_group_header->SetAtomicTransactionId(tuple_id, txn_id) == false) {
155-
GetSpinLatchField(tile_group_header, tuple_id)->Unlock();
132+
latch->Unlock();
156133

157134
return false;
158135
} else {
159-
GetSpinLatchField(tile_group_header, tuple_id)->Unlock();
136+
latch->Unlock();
160137

161138
return true;
162139
}
@@ -328,9 +305,9 @@ bool TimestampOrderingTransactionManager::PerformRead(TransactionContext *const
328305

329306
// if we have already owned the version.
330307
PELOTON_ASSERT(IsOwner(current_txn, tile_group_header, tuple_id) == true);
331-
PELOTON_ASSERT(GetLastReaderCommitId(tile_group_header, tuple_id) ==
308+
PELOTON_ASSERT(tile_group_header->GetLastReaderCommitId(tuple_id) ==
332309
current_txn->GetCommitId() ||
333-
GetLastReaderCommitId(tile_group_header, tuple_id) == 0);
310+
tile_group_header->GetLastReaderCommitId(tuple_id) == 0);
334311
return true;
335312

336313
} else {
@@ -352,9 +329,9 @@ bool TimestampOrderingTransactionManager::PerformRead(TransactionContext *const
352329
} else {
353330
// if the current transaction has already owned this tuple,
354331
// then perform read directly.
355-
PELOTON_ASSERT(GetLastReaderCommitId(tile_group_header, tuple_id) ==
332+
PELOTON_ASSERT(tile_group_header->GetLastReaderCommitId(tuple_id) ==
356333
current_txn->GetCommitId() ||
357-
GetLastReaderCommitId(tile_group_header, tuple_id) == 0);
334+
tile_group_header->GetLastReaderCommitId(tuple_id) == 0);
358335

359336
// this version must already be in the read/write set.
360337
// so no need to update read set.
@@ -391,8 +368,6 @@ void TimestampOrderingTransactionManager::PerformInsert(
391368
// Add the new tuple into the insert set
392369
current_txn->RecordInsert(location);
393370

394-
InitTupleReserved(tile_group_header, tuple_id);
395-
396371
// Write down the head pointer's address in tile group header
397372
tile_group_header->SetIndirection(tuple_id, index_entry_ptr);
398373
}
@@ -446,8 +421,6 @@ void TimestampOrderingTransactionManager::PerformUpdate(
446421
// newer version to older version.
447422
COMPILER_MEMORY_FENCE;
448423

449-
InitTupleReserved(new_tile_group_header, new_location.offset);
450-
451424
// we must be updating the latest version.
452425
// Set the header information for the new version
453426
ItemPointer *index_entry_ptr =
@@ -520,9 +493,8 @@ void TimestampOrderingTransactionManager::PerformDelete(
520493

521494
auto transaction_id = current_txn->GetTransactionId();
522495

523-
PELOTON_ASSERT(
524-
GetLastReaderCommitId(tile_group_header, old_location.offset) ==
525-
current_txn->GetCommitId());
496+
PELOTON_ASSERT(tile_group_header->GetLastReaderCommitId(
497+
old_location.offset) == current_txn->GetCommitId());
526498

527499
// if we can perform delete, then we must have already locked the older
528500
// version.
@@ -554,8 +526,6 @@ void TimestampOrderingTransactionManager::PerformDelete(
554526
// newer version to older version.
555527
COMPILER_MEMORY_FENCE;
556528

557-
InitTupleReserved(new_tile_group_header, new_location.offset);
558-
559529
// we must be deleting the latest version.
560530
// Set the header information for the new version
561531
ItemPointer *index_entry_ptr =

src/gc/transaction_level_gc_manager.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ bool TransactionLevelGCManager::ResetTuple(const ItemPointer &location) {
3636

3737
// Reset the header
3838
tile_group_header->SetTransactionId(location.offset, INVALID_TXN_ID);
39+
tile_group_header->SetLastReaderCommitId(location.offset, INVALID_CID);
3940
tile_group_header->SetBeginCommitId(location.offset, MAX_CID);
4041
tile_group_header->SetEndCommitId(location.offset, MAX_CID);
41-
tile_group_header->SetPrevItemPointer(location.offset, INVALID_ITEMPOINTER);
4242
tile_group_header->SetNextItemPointer(location.offset, INVALID_ITEMPOINTER);
43-
44-
PELOTON_MEMSET(tile_group_header->GetReservedFieldRef(location.offset), 0,
45-
storage::TileGroupHeader::GetReservedSize());
43+
tile_group_header->SetPrevItemPointer(location.offset, INVALID_ITEMPOINTER);
44+
tile_group_header->SetIndirection(location.offset, nullptr);
4645

4746
// Reclaim the varlen pool
4847
CheckAndReclaimVarlenColumns(tile_group, location.offset);

src/include/concurrency/timestamp_ordering_transaction_manager.h

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -238,41 +238,7 @@ class TimestampOrderingTransactionManager : public TransactionManager {
238238
*/
239239
virtual ResultType AbortTransaction(TransactionContext *const current_txn);
240240

241-
242-
private:
243-
static const int LOCK_OFFSET = 0;
244-
static const int LAST_READER_OFFSET = (LOCK_OFFSET + 8);
245-
246-
/**
247-
* @brief Gets the spin latch field.
248-
*
249-
* Timestamp ordering requires a spinlock field for protecting the atomic access
250-
* to txn_id field and last_reader_cid field.
251-
*
252-
* @param[in] tile_group_header The tile group header
253-
* @param[in] tuple_id The tuple identifier
254-
*
255-
* @return The spin latch field.
256-
*/
257-
common::synchronization::SpinLatch *GetSpinLatchField(
258-
const storage::TileGroupHeader *const tile_group_header,
259-
const oid_t &tuple_id);
260-
261-
/**
262-
* @brief Gets the last reader commit identifier.
263-
*
264-
* In timestamp ordering, the last_reader_cid records the timestamp of the last
265-
* transaction that reads the tuple.
266-
*
267-
* @param[in] tile_group_header The tile group header
268-
* @param[in] tuple_id The tuple identifier
269-
*
270-
* @return The last reader commit identifier.
271-
*/
272-
cid_t GetLastReaderCommitId(
273-
const storage::TileGroupHeader *const tile_group_header,
274-
const oid_t &tuple_id);
275-
241+
private:
276242
/**
277243
* @brief Sets the last reader commit identifier.
278244
*
@@ -285,19 +251,7 @@ class TimestampOrderingTransactionManager : public TransactionManager {
285251
*/
286252
bool SetLastReaderCommitId(
287253
const storage::TileGroupHeader *const tile_group_header,
288-
const oid_t &tuple_id,
289-
const cid_t &current_cid,
290-
const bool is_owner);
291-
292-
/**
293-
* Initialize reserved area of a tuple.
294-
*
295-
* @param[in] tile_group_header The tile group header
296-
* @param[in] tuple_id The tuple identifier
297-
*/
298-
void InitTupleReserved(
299-
const storage::TileGroupHeader *const tile_group_header,
300-
const oid_t tuple_id);
254+
const oid_t &tuple_id, const cid_t &current_cid, const bool is_owner);
301255
};
302256
}
303257
}

0 commit comments

Comments
 (0)