-
Notifications
You must be signed in to change notification settings - Fork 209
/
HollowDataHolder.java
267 lines (227 loc) · 11.6 KB
/
HollowDataHolder.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
/*
* Copyright 2016-2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.hollow.api.client;
import com.netflix.hollow.api.consumer.HollowConsumer;
import com.netflix.hollow.api.consumer.HollowConsumer.TransitionAwareRefreshListener;
import com.netflix.hollow.api.custom.HollowAPI;
import com.netflix.hollow.core.HollowConstants;
import com.netflix.hollow.core.memory.MemoryMode;
import com.netflix.hollow.core.read.HollowBlobInput;
import com.netflix.hollow.core.read.OptionalBlobPartInput;
import com.netflix.hollow.core.read.dataaccess.HollowDataAccess;
import com.netflix.hollow.core.read.dataaccess.proxy.HollowProxyDataAccess;
import com.netflix.hollow.core.read.engine.HollowBlobReader;
import com.netflix.hollow.core.read.engine.HollowReadStateEngine;
import com.netflix.hollow.core.read.filter.HollowFilterConfig;
import com.netflix.hollow.core.read.filter.TypeFilter;
import com.netflix.hollow.tools.history.HollowHistoricalStateCreator;
import com.netflix.hollow.tools.history.HollowHistoricalStateDataAccess;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.logging.Logger;
/**
* A class comprising much of the internal state of a {@link HollowConsumer}. Not intended for external consumption.
*/
class HollowDataHolder {
private static final Logger LOG = Logger.getLogger(HollowDataHolder.class.getName());
private final HollowReadStateEngine stateEngine;
private final HollowAPIFactory apiFactory;
private final MemoryMode memoryMode;
private final HollowBlobReader reader;
private final HollowConsumer.DoubleSnapshotConfig doubleSnapshotConfig;
private final FailedTransitionTracker failedTransitionTracker;
private final StaleHollowReferenceDetector staleReferenceDetector;
private final HollowConsumer.ObjectLongevityConfig objLongevityConfig;
private TypeFilter filter;
private HollowAPI currentAPI;
private WeakReference<HollowHistoricalStateDataAccess> priorHistoricalDataAccess;
private long currentVersion = HollowConstants.VERSION_NONE;
HollowDataHolder(HollowReadStateEngine stateEngine,
HollowAPIFactory apiFactory,
MemoryMode memoryMode,
HollowConsumer.DoubleSnapshotConfig doubleSnapshotConfig,
FailedTransitionTracker failedTransitionTracker,
StaleHollowReferenceDetector staleReferenceDetector,
HollowConsumer.ObjectLongevityConfig objLongevityConfig) {
this.stateEngine = stateEngine;
this.apiFactory = apiFactory;
this.memoryMode = memoryMode;
this.reader = new HollowBlobReader(stateEngine, memoryMode);
this.doubleSnapshotConfig = doubleSnapshotConfig;
this.failedTransitionTracker = failedTransitionTracker;
this.staleReferenceDetector = staleReferenceDetector;
this.objLongevityConfig = objLongevityConfig;
}
HollowReadStateEngine getStateEngine() {
return stateEngine;
}
HollowAPI getAPI() {
return currentAPI;
}
long getCurrentVersion() {
return currentVersion;
}
HollowDataHolder setFilter(HollowFilterConfig filter) {
/*
* This method is preserved for binary compat from before TypeFilter was introduced.
*/
return setFilter((TypeFilter)filter);
}
HollowDataHolder setFilter(TypeFilter filter) {
this.filter = filter;
return this;
}
HollowDataHolder setSkipTypeShardUpdateWithNoAdditions(boolean skipTypeShardUpdateWithNoAdditions) {
this.stateEngine.setSkipTypeShardUpdateWithNoAdditions(skipTypeShardUpdateWithNoAdditions);
return this;
}
void update(HollowUpdatePlan updatePlan, HollowConsumer.RefreshListener[] refreshListeners,
Runnable apiInitCallback) throws Throwable {
// Only fail if double snapshot is configured.
// This is a short term solution until it is decided to either remove this feature
// or refine it.
// If the consumer is configured to only follow deltas (no double snapshot) then
// any failure to transition will cause the consumer to become "stuck" on stale data
// unless the failed transitions are explicitly cleared or a new consumer is created.
// A transition failure is very broad encompassing many forms of transitory failure,
// such as network failures when accessing a blob, where the consumer might recover,
// such as when a new delta is published.
// Note that a refresh listener may also induce a failed transition, likely unknowingly,
// by throwing an exception.
if (doubleSnapshotConfig.allowDoubleSnapshot() && failedTransitionTracker.anyTransitionWasFailed(updatePlan)) {
throw new RuntimeException("Update plan contains known failing transition!");
}
if (updatePlan.isSnapshotPlan()) {
applySnapshotPlan(updatePlan, refreshListeners, apiInitCallback);
} else {
applyDeltaOnlyPlan(updatePlan, refreshListeners);
}
}
private void applySnapshotPlan(HollowUpdatePlan updatePlan,
HollowConsumer.RefreshListener[] refreshListeners,
Runnable apiInitCallback) throws Throwable {
applySnapshotTransition(updatePlan.getSnapshotTransition(), refreshListeners, apiInitCallback);
for(HollowConsumer.Blob blob : updatePlan.getDeltaTransitions()) {
applyDeltaTransition(blob, true, refreshListeners);
}
try {
for(HollowConsumer.RefreshListener refreshListener : refreshListeners)
refreshListener.snapshotUpdateOccurred(currentAPI, stateEngine, updatePlan.destinationVersion());
} catch(Throwable t) {
failedTransitionTracker.markAllTransitionsAsFailed(updatePlan);
throw t;
}
}
private void applySnapshotTransition(HollowConsumer.Blob snapshotBlob,
HollowConsumer.RefreshListener[] refreshListeners,
Runnable apiInitCallback) throws Throwable {
try (HollowBlobInput in = HollowBlobInput.modeBasedSelector(memoryMode, snapshotBlob);
OptionalBlobPartInput optionalPartIn = snapshotBlob.getOptionalBlobPartInputs()) {
applyStateEngineTransition(in, optionalPartIn, snapshotBlob, refreshListeners);
initializeAPI(apiInitCallback);
for (HollowConsumer.RefreshListener refreshListener : refreshListeners) {
if (refreshListener instanceof TransitionAwareRefreshListener)
((TransitionAwareRefreshListener)refreshListener).snapshotApplied(currentAPI, stateEngine, snapshotBlob.getToVersion());
}
} catch (Throwable t) {
failedTransitionTracker.markFailedTransition(snapshotBlob);
throw t;
}
}
private void applyStateEngineTransition(HollowBlobInput in, OptionalBlobPartInput optionalPartIn, HollowConsumer.Blob transition, HollowConsumer.RefreshListener[] refreshListeners) throws IOException {
if(transition.isSnapshot()) {
if(filter == null) {
reader.readSnapshot(in, optionalPartIn);
}
else {
reader.readSnapshot(in, optionalPartIn, filter);
}
} else {
reader.applyDelta(in, optionalPartIn);
}
setVersion(transition.getToVersion());
for(HollowConsumer.RefreshListener refreshListener : refreshListeners)
refreshListener.blobLoaded(transition);
}
private void initializeAPI(Runnable r) {
if (objLongevityConfig.enableLongLivedObjectSupport()) {
HollowProxyDataAccess dataAccess = new HollowProxyDataAccess();
dataAccess.setDataAccess(stateEngine);
currentAPI = apiFactory.createAPI(dataAccess);
} else {
currentAPI = apiFactory.createAPI(stateEngine);
}
staleReferenceDetector.newAPIHandle(currentAPI);
try {
r.run();
} catch (Throwable t) {
LOG.warning("Failed to execute API init callback: " + t);
}
}
private void applyDeltaOnlyPlan(HollowUpdatePlan updatePlan, HollowConsumer.RefreshListener[] refreshListeners) throws Throwable {
for(HollowConsumer.Blob blob : updatePlan) {
applyDeltaTransition(blob, false, refreshListeners);
}
}
private void applyDeltaTransition(HollowConsumer.Blob blob, boolean isSnapshotPlan, HollowConsumer.RefreshListener[] refreshListeners) throws Throwable {
if (!memoryMode.equals(MemoryMode.ON_HEAP)) {
LOG.warning("Skipping delta transition in shared-memory mode");
return;
}
try (HollowBlobInput in = HollowBlobInput.modeBasedSelector(memoryMode, blob);
OptionalBlobPartInput optionalPartIn = blob.getOptionalBlobPartInputs()) {
applyStateEngineTransition(in, optionalPartIn, blob, refreshListeners);
if(objLongevityConfig.enableLongLivedObjectSupport()) {
HollowDataAccess previousDataAccess = currentAPI.getDataAccess();
HollowHistoricalStateDataAccess priorState = new HollowHistoricalStateCreator(null).createBasedOnNewDelta(currentVersion, stateEngine);
HollowProxyDataAccess newDataAccess = new HollowProxyDataAccess();
newDataAccess.setDataAccess(stateEngine);
currentAPI = apiFactory.createAPI(newDataAccess, currentAPI);
if(previousDataAccess instanceof HollowProxyDataAccess)
((HollowProxyDataAccess)previousDataAccess).setDataAccess(priorState);
wireHistoricalStateChain(priorState);
} else {
if(currentAPI.getDataAccess() != stateEngine)
currentAPI = apiFactory.createAPI(stateEngine);
priorHistoricalDataAccess = null;
}
if(!staleReferenceDetector.isKnownAPIHandle(currentAPI))
staleReferenceDetector.newAPIHandle(currentAPI);
for(HollowConsumer.RefreshListener refreshListener : refreshListeners) {
if(!isSnapshotPlan)
refreshListener.deltaUpdateOccurred(currentAPI, stateEngine, blob.getToVersion());
if (refreshListener instanceof TransitionAwareRefreshListener)
((TransitionAwareRefreshListener)refreshListener).deltaApplied(currentAPI, stateEngine, blob.getToVersion());
}
} catch(Throwable t) {
failedTransitionTracker.markFailedTransition(blob);
throw t;
}
}
private void wireHistoricalStateChain(HollowHistoricalStateDataAccess nextPriorState) {
if(priorHistoricalDataAccess != null) {
HollowHistoricalStateDataAccess dataAccess = priorHistoricalDataAccess.get();
if(dataAccess != null) {
dataAccess.setNextState(nextPriorState);
}
}
priorHistoricalDataAccess = new WeakReference<HollowHistoricalStateDataAccess>(nextPriorState);
}
private void setVersion(long version) {
currentVersion = version;
}
}