-
Notifications
You must be signed in to change notification settings - Fork 615
/
EntryFactoryImpl.java
284 lines (264 loc) · 11.8 KB
/
EntryFactoryImpl.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
package org.infinispan.container.impl;
import static org.infinispan.commons.util.Util.toStr;
import org.infinispan.commons.time.TimeService;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.Configurations;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.container.entries.MVCCEntry;
import org.infinispan.container.entries.NullCacheEntry;
import org.infinispan.container.entries.ReadCommittedEntry;
import org.infinispan.container.entries.RepeatableReadEntry;
import org.infinispan.container.entries.VersionedRepeatableReadEntry;
import org.infinispan.container.versioning.VersionGenerator;
import org.infinispan.context.InvocationContext;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.expiration.impl.InternalExpirationManager;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.metadata.EmbeddedMetadata;
import org.infinispan.metadata.Metadata;
import org.infinispan.util.concurrent.IsolationLevel;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
/**
* {@link EntryFactory} implementation to be used for optimistic locking scheme.
*
* @author Mircea Markus
* @since 5.1
*/
public class EntryFactoryImpl implements EntryFactory {
private static final Log log = LogFactory.getLog(EntryFactoryImpl.class);
private final static boolean trace = log.isTraceEnabled();
@Inject private InternalDataContainer container;
@Inject private Configuration configuration;
@Inject private TimeService timeService;
@Inject private VersionGenerator versionGenerator;
@Inject private DistributionManager distributionManager;
@Inject private InternalExpirationManager expirationManager;
private boolean isL1Enabled;
private boolean useRepeatableRead;
private boolean useVersioning;
@Start (priority = 8)
public void init() {
// Scattered mode needs repeatable-read entries to properly retry half-committed multi-key operations
// (see RetryingEntryWrappingInterceptor for details).
useRepeatableRead = configuration.transaction().transactionMode().isTransactional()
&& configuration.locking().isolationLevel() == IsolationLevel.REPEATABLE_READ
|| configuration.clustering().cacheMode().isScattered();
isL1Enabled = configuration.clustering().l1().enabled();
// Write-skew check implies isolation level = REPEATABLE_READ && locking mode = OPTIMISTIC
useVersioning = Configurations.isTxVersioned(configuration);
}
@Override
public final void wrapEntryForReading(InvocationContext ctx, Object key, int segment, boolean isOwner) {
if (!isOwner && !isL1Enabled) {
return;
}
CacheEntry cacheEntry = getFromContext(ctx, key);
if (cacheEntry == null) {
cacheEntry = getFromContainerForRead(key, segment, isOwner);
if (cacheEntry != null) {
// With repeatable read, we need to create a RepeatableReadEntry as internal cache entries are mutable
// Otherwise we can store the InternalCacheEntry directly in the context
if (useRepeatableRead) {
MVCCEntry mvccEntry = createWrappedEntry(key, cacheEntry);
mvccEntry.setRead();
cacheEntry = mvccEntry;
}
ctx.putLookedUpEntry(key, cacheEntry);
}
}
if (trace) {
log.tracef("Wrap %s for read. Entry=%s", toStr(key), cacheEntry);
}
}
@Override
public void wrapEntryForWriting(InvocationContext ctx, Object key, int segment, boolean isOwner, boolean isRead) {
CacheEntry contextEntry = getFromContext(ctx, key);
if (contextEntry instanceof MVCCEntry) {
// Nothing to do, already wrapped.
} else if (contextEntry != null) {
// Already in the context as an InternalCacheEntry
// Need to wrap it in a MVCCEntry.
MVCCEntry mvccEntry = createWrappedEntry(key, contextEntry);
ctx.putLookedUpEntry(key, mvccEntry);
if (trace)
log.tracef("Updated context entry %s -> %s", contextEntry, mvccEntry);
} else {
// Not in the context yet.
CacheEntry cacheEntry = getFromContainerForWrite(key, segment, isOwner);
if (cacheEntry == null) {
return;
}
MVCCEntry mvccEntry = createWrappedEntry(key, cacheEntry);
if (cacheEntry.isNull()) {
mvccEntry.setCreated(true);
}
if (isRead) {
mvccEntry.setRead();
}
ctx.putLookedUpEntry(key, mvccEntry);
if (trace)
log.tracef("Updated context entry %s -> %s", contextEntry, mvccEntry);
}
}
@Override
public void wrapEntryForExpired(InvocationContext ctx, Object key, int segment, boolean isOwner) {
CacheEntry contextEntry = getFromContext(ctx, key);
if (contextEntry instanceof MVCCEntry) {
// Nothing to do, already wrapped.
} else if (contextEntry != null) {
// Already in the context as an InternalCacheEntry
// Need to wrap it in a MVCCEntry.
MVCCEntry mvccEntry = createWrappedEntry(key, contextEntry);
ctx.putLookedUpEntry(key, mvccEntry);
if (trace)
log.tracef("Updated context entry %s -> %s", contextEntry, mvccEntry);
} else {
// Not in the context yet.
CacheEntry cacheEntry = innerGetFromContainer(key, segment, true, false);
if (cacheEntry == null) {
cacheEntry = NullCacheEntry.getInstance();
}
MVCCEntry mvccEntry = createWrappedEntry(key, cacheEntry);
// Make sure to set the created date so we can verify if the entry actually expired
mvccEntry.setCreated(cacheEntry.getCreated());
if (cacheEntry.isNull()) {
mvccEntry.setCreated(true);
}
mvccEntry.setRead();
ctx.putLookedUpEntry(key, mvccEntry);
if (trace)
log.tracef("Updated context entry %s -> %s", contextEntry, mvccEntry);
}
}
@Override
public void wrapExternalEntry(InvocationContext ctx, Object key, CacheEntry externalEntry, boolean isRead,
boolean isWrite) {
// For a write operation, the entry is always already wrapped. For a read operation, the entry may be
// in the context as an InternalCacheEntry, as null, or missing altogether.
CacheEntry contextEntry = getFromContext(ctx, key);
if (contextEntry instanceof MVCCEntry) {
MVCCEntry mvccEntry = (MVCCEntry) contextEntry;
// Already wrapped for a write. Update the value and the metadata.
if (mvccEntry.skipLookup()) {
// This can happen during getGroup() invocations, which request the whole group from remote nodes
// even if some keys are already in the context.
if (trace)
log.tracef("Ignored update for context entry %s", contextEntry);
return;
}
// Without updating initial value a local write skew check would fail when the entry is loaded
// from the cache store. This shouldn't be called more than once since afterwards we set skipLookup
mvccEntry.setValue(externalEntry.getValue());
mvccEntry.setCreated(externalEntry.getCreated());
mvccEntry.setLastUsed(externalEntry.getLastUsed());
mvccEntry.setMetadata(externalEntry.getMetadata());
mvccEntry.updatePreviousValue();
if (trace) log.tracef("Updated context entry %s", contextEntry);
} else if (contextEntry == null || contextEntry.isNull()) {
if (isWrite || useRepeatableRead) {
MVCCEntry mvccEntry = createWrappedEntry(key, externalEntry);
if (isRead) {
mvccEntry.setRead();
}
ctx.putLookedUpEntry(key, mvccEntry);
if (trace)
log.tracef("Updated context entry %s -> %s", contextEntry, mvccEntry);
} else {
// This is a read operation, store the external entry in the context directly.
ctx.putLookedUpEntry(key, externalEntry);
if (trace)
log.tracef("Updated context entry %s -> %s", contextEntry, externalEntry);
}
} else {
if (useRepeatableRead) {
if (trace) log.tracef("Ignored update %s -> %s as we do repeatable reads", contextEntry, externalEntry);
} else {
ctx.putLookedUpEntry(key, externalEntry);
if (trace) log.tracef("Updated context entry %s -> %s", contextEntry, externalEntry);
}
}
}
private CacheEntry getFromContext(InvocationContext ctx, Object key) {
final CacheEntry cacheEntry = ctx.lookupEntry(key);
if (trace) log.tracef("Exists in context? %s ", cacheEntry);
return cacheEntry;
}
private boolean isPrimaryOwner(boolean isOwner, int segment) {
if (distributionManager != null) {
return distributionManager.getCacheTopology().getSegmentDistribution(segment).isPrimary();
} else {
return isOwner;
}
}
private CacheEntry getFromContainerForWrite(Object key, int segment, boolean isOwner) {
if (isOwner) {
final InternalCacheEntry ice = innerGetFromContainer(key, segment, false, isPrimaryOwner(isOwner, segment));
if (trace)
log.tracef("Retrieved from container %s", ice);
if (ice == null) {
return NullCacheEntry.getInstance();
}
return ice;
} else if (isL1Enabled) {
final InternalCacheEntry ice = innerGetFromContainer(key, segment, false, isPrimaryOwner(isOwner, segment));
if (trace)
log.tracef("Retrieved from container %s", ice);
if (ice == null || !ice.isL1Entry()) return null;
return ice;
}
return null;
}
private CacheEntry getFromContainerForRead(Object key, int segment, boolean isOwner) {
InternalCacheEntry ice = container.get(segment, key);
if (trace) {
log.tracef("Retrieved from container %s", ice);
}
if (isOwner) {
return ice == null ? NullCacheEntry.getInstance() : ice;
} else {
return ice == null || !ice.isL1Entry() ? null : ice;
}
}
private InternalCacheEntry innerGetFromContainer(Object key, int segment, boolean returnExpired, boolean isPrimaryOwner) {
InternalCacheEntry ice = container.peek(segment, key);
if (ice != null && !returnExpired) {
long currentTime = timeService.wallClockTime();
if (ice.isExpired(currentTime)) {
// This means it is a write operation that isn't expiration and we are the owner, thus we should
// actually expire the entry from memory
if (isPrimaryOwner) {
// This method is always called from a write operation
expirationManager.entryExpiredInMemory(ice, currentTime, true).join();
}
return null;
}
}
return ice;
}
protected MVCCEntry createWrappedEntry(Object key, CacheEntry cacheEntry) {
Object value = null;
Metadata metadata = null;
if (cacheEntry != null) {
value = cacheEntry.getValue();
metadata = cacheEntry.getMetadata();
}
if (trace) log.tracef("Creating new entry for key %s", toStr(key));
if (useRepeatableRead) {
MVCCEntry mvccEntry;
if (useVersioning) {
if (metadata == null) {
metadata = new EmbeddedMetadata.Builder().version(versionGenerator.nonExistingVersion()).build();
}
mvccEntry = new VersionedRepeatableReadEntry(key, value, metadata);
} else {
mvccEntry = new RepeatableReadEntry(key, value, metadata);
}
return mvccEntry;
} else {
return new ReadCommittedEntry(key, value, metadata);
}
}
}