-
Notifications
You must be signed in to change notification settings - Fork 465
/
SegmentStages.java
439 lines (379 loc) · 15.4 KB
/
SegmentStages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.impl.stage.entry;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.SegmentLock;
import net.openhft.chronicle.hash.impl.*;
import net.openhft.chronicle.hash.impl.stage.hash.Chaining;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.chronicle.hash.impl.stage.query.KeySearch;
import net.openhft.chronicle.hash.locks.InterProcessLock;
import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.collection.SingleThreadedDirectBitSet;
import net.openhft.lang.io.MultiStoreBytes;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import static net.openhft.chronicle.hash.impl.LocalLockState.UNLOCKED;
@Staged
public abstract class SegmentStages implements SegmentLock {
@StageRef Chaining chaining;
@StageRef public VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation;
@Stage("TheSegmentIndex") public int segmentIndex = -1;
public void initTheSegmentIndex(int segmentIndex) {
this.segmentIndex = segmentIndex;
}
// "SegHeader" because stage-generator doesn't support stage names - one is a prefix of another
// there is a stage named "Segment"
@Stage("SegHeader") long segmentHeaderAddress;
@Stage("SegHeader") SegmentHeader segmentHeader = null;
abstract boolean segHeaderInit();
private void initSegHeader() {
segmentHeaderAddress = hh.h().ms.address() + hh.h().segmentHeaderOffset(segmentIndex);
segmentHeader = BigSegmentHeader.INSTANCE;
}
public long entries() {
return segmentHeader.size(segmentHeaderAddress);
}
public void entries(long size) {
segmentHeader.size(segmentHeaderAddress, size);
}
long nextPosToSearchFrom() {
return segmentHeader.nextPosToSearchFrom(segmentHeaderAddress);
}
public void nextPosToSearchFrom(long nextPosToSearchFrom) {
segmentHeader.nextPosToSearchFrom(segmentHeaderAddress, nextPosToSearchFrom);
}
public long deleted() {
return segmentHeader.deleted(segmentHeaderAddress);
}
public void deleted(long deleted) {
segmentHeader.deleted(segmentHeaderAddress, deleted);
}
public long size() {
return entries() - deleted();
}
@Stage("Locks") public SegmentStages rootContextOnThisSegment = null;
/**
* See the ChMap Ops spec, considerations of nested same-thread concurrent contexts.
* Once context enters the segment, and observes concurrent same-thread context,
* it sets concurrentSameThreadContexts = true for itself and that concurrent context.
* This flag is not dropped on exit of one of these contexts, because between calls of
* this context, nested one could be initialized, does some changes that break our thread-local
* assumptions _and exit_, that is why on exit concurrent context should remain "dirty".
*/
@Stage("Locks") public boolean concurrentSameThreadContexts;
@Stage("Locks") public int latestSameThreadSegmentModCount;
@Stage("Locks") public int contextModCount;
@Stage("Locks")
public void incrementModCount() {
contextModCount = ++rootContextOnThisSegment.latestSameThreadSegmentModCount;
}
// chain
@Stage("Locks") SegmentStages nextNode;
@Stage("Locks") LocalLockState localLockState;
@Stage("Locks") int totalReadLockCount;
@Stage("Locks") int totalUpdateLockCount;
@Stage("Locks") int totalWriteLockCount;
@Stage("Locks")
public boolean readZero() {
return rootContextOnThisSegment.totalReadLockCount == 0;
}
@Stage("Locks")
public boolean updateZero() {
return rootContextOnThisSegment.totalUpdateLockCount == 0;
}
@Stage("Locks")
public boolean writeZero() {
return rootContextOnThisSegment.totalWriteLockCount == 0;
}
@Stage("Locks")
public int decrementRead() {
return rootContextOnThisSegment.totalReadLockCount -= 1;
}
@Stage("Locks")
public int decrementUpdate() {
return rootContextOnThisSegment.totalUpdateLockCount -= 1;
}
@Stage("Locks")
public int decrementWrite() {
return rootContextOnThisSegment.totalWriteLockCount -= 1;
}
@Stage("Locks")
public void incrementRead() {
rootContextOnThisSegment.totalReadLockCount += 1;
}
@Stage("Locks")
public void incrementUpdate() {
rootContextOnThisSegment.totalUpdateLockCount += 1;
}
@Stage("Locks")
public void incrementWrite() {
rootContextOnThisSegment.totalWriteLockCount += 1;
}
public abstract boolean locksInit();
void initLocks() {
localLockState = UNLOCKED;
int indexOfThisContext = chaining.indexInContextChain;
for (int i = indexOfThisContext - 1; i >= 0; i--) {
if (tryFindInitLocksOfThisSegment(this, i))
return;
}
for (int i = indexOfThisContext + 1, size = chaining.contextChain.size(); i < size; i++) {
if (tryFindInitLocksOfThisSegment(this, i))
return;
}
rootContextOnThisSegment = this;
concurrentSameThreadContexts = false;
latestSameThreadSegmentModCount = 0;
contextModCount = 0;
totalReadLockCount = 0;
totalUpdateLockCount = 0;
totalWriteLockCount = 0;
}
@Stage("Locks")
boolean tryFindInitLocksOfThisSegment(Object thisContext, int index) {
SegmentStages c = chaining.contextAtIndexInChain(index);
if (c.segHeaderInit() &&
c.segmentHeaderAddress == segmentHeaderAddress &&
c.locksInit()) {
SegmentStages root = c.rootContextOnThisSegment;
this.rootContextOnThisSegment = root;
root.concurrentSameThreadContexts = true;
this.concurrentSameThreadContexts = true;
this.contextModCount = root.latestSameThreadSegmentModCount;
linkToSegmentContextsChain();
return true;
} else {
return false;
}
}
void closeLocks() {
if (rootContextOnThisSegment == this) {
closeRootLocks();
} else {
closeNestedLocks();
}
localLockState = null;
rootContextOnThisSegment = null;
}
@Stage("Locks")
private void closeNestedLocks() {
unlinkFromSegmentContextsChain();
readUnlockAndDecrementCount();
}
@Stage("Locks")
public void readUnlockAndDecrementCount() {
switch (localLockState) {
case UNLOCKED:
return;
case READ_LOCKED:
int newTotalReadLockCount = decrementRead();
if (newTotalReadLockCount == 0) {
if (updateZero() && writeZero())
segmentHeader.readUnlock(segmentHeaderAddress);
} else {
assert newTotalReadLockCount > 0 : "read underflow";
}
return;
case UPDATE_LOCKED:
int newTotalUpdateLockCount = decrementUpdate();
if (newTotalUpdateLockCount == 0) {
if (writeZero()) {
if (readZero()) {
segmentHeader.updateUnlock(segmentHeaderAddress);
} else {
segmentHeader.downgradeUpdateToReadLock(segmentHeaderAddress);
}
}
} else {
assert newTotalUpdateLockCount > 0 : "update underflow";
}
return;
case WRITE_LOCKED:
int newTotalWriteLockCount = decrementWrite();
if (newTotalWriteLockCount == 0) {
if (!updateZero()) {
segmentHeader.downgradeWriteToUpdateLock(segmentHeaderAddress);
} else {
if (!readZero()) {
segmentHeader.downgradeWriteToReadLock(segmentHeaderAddress);
} else {
segmentHeader.writeUnlock(segmentHeaderAddress);
}
}
} else {
assert newTotalWriteLockCount > 0 : "write underflow";
}
}
}
@Stage("Locks")
private void linkToSegmentContextsChain() {
SegmentStages innermostContextOnThisSegment = rootContextOnThisSegment;
while (true) {
Data key = ((KeySearch) (Object) innermostContextOnThisSegment).inputKey;
if (Objects.equals(key, ((KeySearch) (Object) this).inputKey)) {
throw new IllegalStateException("Nested same-thread contexts cannot access " +
"the same key " + key);
}
if (innermostContextOnThisSegment.nextNode == null)
break;
innermostContextOnThisSegment = innermostContextOnThisSegment.nextNode;
}
innermostContextOnThisSegment.nextNode = this;
}
@Stage("Locks")
private void unlinkFromSegmentContextsChain() {
SegmentStages prevContext = rootContextOnThisSegment;
while (true) {
assert prevContext.nextNode != null;
if (prevContext.nextNode == this)
break;
prevContext = prevContext.nextNode;
}
// i. e. structured unlocking
verifyInnermostContext();
prevContext.nextNode = null;
}
@Stage("Locks")
private void verifyInnermostContext() {
if (nextNode != null)
throw new IllegalStateException("Attempt to close contexts not structurally");
}
@Stage("Locks")
private void closeRootLocks() {
verifyInnermostContext();
switch (localLockState) {
case UNLOCKED:
return;
case READ_LOCKED:
segmentHeader.readUnlock(segmentHeaderAddress);
return;
case UPDATE_LOCKED:
segmentHeader.updateUnlock(segmentHeaderAddress);
return;
case WRITE_LOCKED:
segmentHeader.writeUnlock(segmentHeaderAddress);
}
}
@Stage("Locks")
public void setLocalLockState(LocalLockState newState) {
localLockState = newState;
}
@StageRef public ReadLock innerReadLock;
@StageRef public UpdateLock innerUpdateLock;
@StageRef public WriteLock innerWriteLock;
@NotNull
@Override
public InterProcessLock readLock() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerReadLock;
}
@NotNull
@Override
public InterProcessLock updateLock() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerUpdateLock;
}
@NotNull
@Override
public InterProcessLock writeLock() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerWriteLock;
}
@Stage("Segment") MultiStoreBytes freeListBytes = new MultiStoreBytes();
@Stage("Segment") public SingleThreadedDirectBitSet freeList = new SingleThreadedDirectBitSet();
@Stage("Segment") long entrySpaceOffset = 0;
@Stage("Segment") public long segmentBase;
boolean segmentInit() {
return entrySpaceOffset > 0;
}
void initSegment() {
VanillaChronicleHash<?, ?, ?, ?, ?, ?> h = hh.h();
long hashLookupOffset = h.segmentOffset(segmentIndex);
segmentBase = hh.h().ms.address() + hashLookupOffset;
long freeListOffset = hashLookupOffset + h.segmentHashLookupOuterSize;
freeListBytes.storePositionAndSize(h.ms, freeListOffset, h.segmentFreeListInnerSize);
freeList.reuse(freeListBytes);
entrySpaceOffset = freeListOffset + h.segmentFreeListOuterSize +
h.segmentEntrySpaceInnerOffset;
}
void closeSegment() {
entrySpaceOffset = 0;
}
//TODO refactor/optimize
public long alloc(int chunks) {
VanillaChronicleHash<?, ?, ?, ?, ?, ?> h = hh.h();
if (chunks > h.maxChunksPerEntry)
throw new IllegalArgumentException("Entry is too large: requires " + chunks +
" entry size chucks, " + h.maxChunksPerEntry + " is maximum.");
long ret = freeList.setNextNContinuousClearBits(nextPosToSearchFrom(), chunks);
if (ret == DirectBitSet.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != DirectBitSet.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment && ret < h.actualChunksPerSegment)
freeList.clear(ret, h.actualChunksPerSegment);
ret = freeList.setNextNContinuousClearBits(0L, chunks);
if (ret == DirectBitSet.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != DirectBitSet.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment &&
ret < h.actualChunksPerSegment)
freeList.clear(ret, h.actualChunksPerSegment);
if (chunks == 1) {
throw new IllegalStateException(
"Segment is full, no free entries found");
} else {
throw new IllegalStateException(
"Segment is full or has no ranges of " + chunks
+ " continuous free chunks"
);
}
}
updateNextPosToSearchFrom(ret, chunks);
} else {
// if bit at nextPosToSearchFrom is clear, it was skipped because
// more than 1 chunk was requested. Don't move nextPosToSearchFrom
// in this case. chunks == 1 clause is just a fast path.
if (chunks == 1 || freeList.isSet(nextPosToSearchFrom())) {
updateNextPosToSearchFrom(ret, chunks);
}
}
return ret;
}
public void free(long fromPos, int chunks) {
freeList.clear(fromPos, fromPos + chunks);
if (fromPos < nextPosToSearchFrom())
nextPosToSearchFrom(fromPos);
}
public void updateNextPosToSearchFrom(long allocated, int chunks) {
long nextPosToSearchFrom = allocated + chunks;
if (nextPosToSearchFrom >= hh.h().actualChunksPerSegment)
nextPosToSearchFrom = 0L;
nextPosToSearchFrom(nextPosToSearchFrom);
}
public void clearSegment() {
innerWriteLock.lock();
hh.h().hashLookup.clearHashLookup(segmentBase);
freeList.clear();
nextPosToSearchFrom(0L);
entries(0L);
}
public void clear() {
clearSegment();
}
}