/
ThrottlingDocumentStoreWrapper.java
307 lines (268 loc) · 11.2 KB
/
ThrottlingDocumentStoreWrapper.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.jackrabbit.oak.plugins.document.util;
import org.apache.jackrabbit.oak.cache.CacheStats;
import org.apache.jackrabbit.oak.plugins.document.Collection;
import org.apache.jackrabbit.oak.plugins.document.Document;
import org.apache.jackrabbit.oak.plugins.document.DocumentStore;
import org.apache.jackrabbit.oak.plugins.document.DocumentStoreException;
import org.apache.jackrabbit.oak.plugins.document.Throttler;
import org.apache.jackrabbit.oak.plugins.document.ThrottlingStatsCollector;
import org.apache.jackrabbit.oak.plugins.document.UpdateOp;
import org.apache.jackrabbit.oak.plugins.document.cache.CacheInvalidationStats;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import java.util.List;
import java.util.Map;
import static java.lang.System.currentTimeMillis;
import static java.lang.Thread.sleep;
import static java.util.Objects.isNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.stream.Collectors.toList;
import static org.apache.jackrabbit.oak.plugins.document.Collection.CLUSTER_NODES;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Wrapper of another DocumentStore that does a throttling check on any method
* invocation (create, update or delete) and throttled the system if under high load.
*/
public class ThrottlingDocumentStoreWrapper implements DocumentStore {
private static final Logger LOG = getLogger(ThrottlingDocumentStoreWrapper.class);
private volatile int lastLogTime = 0;
@NotNull
private final DocumentStore store;
@NotNull
private final ThrottlingStatsCollector throttlingStatsCollector;
public ThrottlingDocumentStoreWrapper(final @NotNull DocumentStore store,
final @NotNull ThrottlingStatsCollector throttlingStatsCollector) {
this.store = store;
this.throttlingStatsCollector = throttlingStatsCollector;
}
@Override
public <T extends Document> T find(final Collection<T> collection, final String key) {
return store.find(collection, key);
}
@Override
public <T extends Document> T find(final Collection<T> collection, final String key,
final int maxCacheAge) {
return store.find(collection, key, maxCacheAge);
}
@NotNull
@Override
public <T extends Document> List<T> query(final Collection<T> collection, final String fromKey,
final String toKey, final int limit) {
return store.query(collection, fromKey, toKey, limit);
}
@Override
@NotNull
public <T extends Document> List<T> query(final Collection<T> collection, final String fromKey,
final String toKey, final String indexedProperty,
final long startValue, final int limit) {
return store.query(collection, fromKey, toKey, indexedProperty, startValue, limit);
}
@Override
@NotNull
public <T extends Document> List<T> query(final Collection<T> collection, final String fromKey, final String toKey,
final String indexedProperty, final long startValue, final int limit,
final List<String> projection) throws DocumentStoreException {
return store.query(collection, fromKey, toKey, indexedProperty, startValue, limit, projection);
}
@Override
public <T extends Document> void remove(Collection<T> collection, String key) {
long throttlingTime = performThrottling(collection);
try {
store.remove(collection, key);
} finally {
throttlingStatsCollector.doneRemove(MILLISECONDS.toNanos(throttlingTime), collection);
}
}
@Override
public <T extends Document> void remove(Collection<T> collection, List<String> keys) {
long throttlingTime = performThrottling(collection);
try {
store.remove(collection, keys);
} finally {
throttlingStatsCollector.doneRemove(MILLISECONDS.toNanos(throttlingTime), collection);
}
}
@Override
public <T extends Document> int remove(final Collection<T> collection, final Map<String, Long> toRemove) {
long throttlingTime = performThrottling(collection);
int count = 0;
try {
count = store.remove(collection, toRemove);
} finally {
throttlingStatsCollector.doneRemove(MILLISECONDS.toNanos(throttlingTime), collection);
}
return count;
}
@Override
public <T extends Document> int remove(final Collection<T> collection, final String indexedProperty,
final long startValue, final long endValue) throws DocumentStoreException {
long throttlingTime = performThrottling(collection);
int count = 0;
try {
count = store.remove(collection, indexedProperty, startValue, endValue);
} finally {
throttlingStatsCollector.doneRemove(MILLISECONDS.toNanos(throttlingTime), collection);
}
return count;
}
@Override
public <T extends Document> boolean create(final Collection<T> collection, final List<UpdateOp> updateOps) {
long throttlingTime = performThrottling(collection);
boolean isSuccess = false;
try {
isSuccess = store.create(collection, updateOps);
} finally {
throttlingStatsCollector.doneCreate(MILLISECONDS.toNanos(throttlingTime), collection,
updateOps.stream().map(UpdateOp::getId).collect(toList()), isSuccess);
}
return isSuccess;
}
@Override
public <T extends Document> T createOrUpdate(final Collection<T> collection, final UpdateOp update) {
long throttlingTime = performThrottling(collection);
T oldDoc = null;
try {
oldDoc = store.createOrUpdate(collection, update);
} finally {
throttlingStatsCollector.doneFindAndModify(MILLISECONDS.toNanos(throttlingTime), collection, update.getId(),
(isNull(oldDoc) && update.isNew()), true, 0);
}
return oldDoc;
}
@Override
public <T extends Document> List<T> createOrUpdate(final Collection<T> collection, final List<UpdateOp> updateOps) {
long throttlingTime = performThrottling(collection);
List<T> results = null;
try {
results = store.createOrUpdate(collection, updateOps);
} finally {
throttlingStatsCollector.doneCreateOrUpdate(MILLISECONDS.toNanos(throttlingTime), collection,
updateOps.stream().map(UpdateOp::getId).collect(toList()));
}
return results;
}
@Override
public <T extends Document> T findAndUpdate(final Collection<T> collection, final UpdateOp update) {
long throttlingTime = performThrottling(collection);
T oldDoc = null;
try {
oldDoc = store.findAndUpdate(collection, update);
} finally {
throttlingStatsCollector.doneFindAndModify(MILLISECONDS.toNanos(throttlingTime), collection, update.getId(),
false, true, 0);
}
return oldDoc;
}
@Override
@NotNull
public <T extends Document> List<T> findAndUpdate(@NotNull Collection<T> collection, @NotNull List<UpdateOp> updateOps) {
final long throttlingTime = performThrottling(collection);
try {
return store.findAndUpdate(collection, updateOps);
} finally {
throttlingStatsCollector.doneFindAndModify(MILLISECONDS.toNanos(throttlingTime), collection,
updateOps.stream().map(UpdateOp::getId).collect(toList()), true, 0);
}
}
@Override
public CacheInvalidationStats invalidateCache() {
return store.invalidateCache();
}
@Override
public CacheInvalidationStats invalidateCache(Iterable<String> keys) {
return store.invalidateCache(keys);
}
@Override
public <T extends Document> void invalidateCache(Collection<T> collection, String key) {
store.invalidateCache(collection, key);
}
@Override
public void dispose() {
store.dispose();
}
@Override
public <T extends Document> T getIfCached(final Collection<T> collection, final String key) {
return store.getIfCached(collection, key);
}
@Override
public void setReadWriteMode(String readWriteMode) {
store.setReadWriteMode(readWriteMode);
}
@Override
public Iterable<CacheStats> getCacheStats() {
return store.getCacheStats();
}
@Override
public Map<String, String> getMetadata() {
return store.getMetadata();
}
@NotNull
@Override
public Map<String, String> getStats() {
return store.getStats();
}
@Override
public long determineServerTimeDifferenceMillis() {
return store.determineServerTimeDifferenceMillis();
}
/**
* Return the size limit for node name based on the document store implementation
*
* @return node name size limit
*/
@Override
public int getNodeNameLimit() {
return store.getNodeNameLimit();
}
/**
* Return the {@link Throttler} for the underlying store
* Default is no throttling
*
* @return throttler for document store
*/
@Override
public Throttler throttler() {
return store.throttler();
}
// helper methods
private <T extends Document> long performThrottling(final Collection<T> collection) {
if (CLUSTER_NODES == collection) {
return 0L;
}
final Throttler throttler = throttler();
long throttleTime = throttler.throttlingTime();
if (throttleTime == 0) {
return throttleTime; // no throttling
}
try {
// log message every 10 secs once to reduce noise
// (decaseconds since 1970 - overflows roughly in year 2650)
final int currentDecaSecond = (int) (currentTimeMillis() / 10_000);
if (currentDecaSecond > lastLogTime) {
lastLogTime = currentDecaSecond;
LOG.warn("Throttling the system for {} ms for {} collection", throttleTime, collection);
}
sleep(throttleTime);
} catch (InterruptedException e) {
// swallow the exception and log it
LOG.error("Error while throttling", e);
}
return throttleTime;
}
}