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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
|
/*
* Copyright 2006-2012 Amazon Technologies, Inc. or its affiliates.
* Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
* of Amazon Technologies, Inc. or its affiliates. All rights reserved.
*
* 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.amazon.carbonado.repo.replicated;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazon.carbonado.CorruptEncodingException;
import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.FetchInterruptedException;
import com.amazon.carbonado.IsolationLevel;
import com.amazon.carbonado.MalformedTypeException;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.Query;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.SupportException;
import com.amazon.carbonado.Transaction;
import com.amazon.carbonado.UnsupportedTypeException;
import com.amazon.carbonado.capability.Capability;
import com.amazon.carbonado.capability.IndexInfo;
import com.amazon.carbonado.capability.IndexInfoCapability;
import com.amazon.carbonado.capability.ResyncCapability;
import com.amazon.carbonado.capability.ShutdownCapability;
import com.amazon.carbonado.capability.StorableInfoCapability;
import com.amazon.carbonado.cursor.SortedCursor;
import com.amazon.carbonado.filter.Filter;
import com.amazon.carbonado.info.Direction;
import com.amazon.carbonado.info.StorableIndex;
import com.amazon.carbonado.info.StorableInfo;
import com.amazon.carbonado.info.StorableIntrospector;
import com.amazon.carbonado.qe.IndexedQueryAnalyzer;
import com.amazon.carbonado.qe.OrderingList;
import com.amazon.carbonado.qe.QueryHint;
import com.amazon.carbonado.qe.QueryHints;
import com.amazon.carbonado.qe.RepositoryAccess;
import com.amazon.carbonado.qe.UnionQueryAnalyzer;
import com.amazon.carbonado.repo.indexed.IndexEntryAccessCapability;
import com.amazon.carbonado.spi.StoragePool;
import com.amazon.carbonado.txn.TransactionPair;
import com.amazon.carbonado.util.BelatedCreationException;
import com.amazon.carbonado.util.Throttle;
/**
* A ReplicatedRepository binds two repositories together. One will be used
* for reading (the replica), and the other will be used for writing; changes
* to the master repository will be copied to the replica.
*
* @author Don Schneider
*/
class ReplicatedRepository
implements Repository,
ResyncCapability,
ShutdownCapability,
StorableInfoCapability
{
// Maximum number of resync updates to replica per transaction.
private static final int RESYNC_BATCH_SIZE = 10;
// Commit resync replica transaction early if this many records
// scanned. Otherwise, write locks may be held for a very long time.
private static final int RESYNC_WATERMARK = 100;
/**
* Utility method to select the natural ordering of a storage, by looking for a clustered
* index on the primary key. Returns null if no clustered index was found. If a filter is
* provided, the ordering which utilizes the best index is used.
*/
private static <S extends Storable> String[] selectNaturalOrder(Repository repo,
Class<S> type,
Filter<S> filter)
throws RepositoryException
{
if (!filter.isOpen() && repo instanceof RepositoryAccess) {
UnionQueryAnalyzer.Result result =
new UnionQueryAnalyzer(type, (RepositoryAccess) repo)
.analyze(filter, null, QueryHints.emptyHints().with(QueryHint.CONSUME_SLICE));
OrderingList<S> ordering = result.getTotalOrdering();
if (ordering == null) {
List<IndexedQueryAnalyzer.Result> list = result.getSubResults();
if (!list.isEmpty()) {
StorableIndex<S> index = list.get(0).getLocalIndex();
if (index != null) {
ordering = OrderingList.get(index.getOrderedProperties());
}
}
}
if (ordering != null) {
String[] props = new String[ordering.size()];
for (int i=0; i<props.length; i++) {
props[i] = ordering.get(i).toString();
}
return props;
}
}
IndexInfoCapability capability = repo.getCapability(IndexInfoCapability.class);
if (capability == null) {
return null;
}
IndexInfo info = null;
for (IndexInfo candidate : capability.getIndexInfo(type)) {
if (candidate.isClustered()) {
info = candidate;
break;
}
}
if (info == null) {
return null;
}
// Verify index is part of primary key.
Set<String> pkSet = StorableIntrospector.examine(type).getPrimaryKeyProperties().keySet();
String[] propNames = info.getPropertyNames();
for (String prop : propNames) {
if (!pkSet.contains(prop)) {
return null;
}
}
String[] orderBy = new String[pkSet.size()];
Direction[] directions = info.getPropertyDirections();
// Clone to remove elements.
pkSet = new LinkedHashSet<String>(pkSet);
int i;
for (i=0; i<propNames.length; i++) {
orderBy[i] = ((directions[i] == Direction.DESCENDING) ? "-" : "+") + propNames[i];
pkSet.remove(propNames[i]);
}
// Append any remaining pk properties, to ensure complete ordering.
if (pkSet.size() > 0) {
for (String prop : pkSet) {
orderBy[i++] = prop;
}
}
return orderBy;
}
private String mName;
private Repository mReplicaRepository;
private Repository mMasterRepository;
private final StoragePool mStoragePool;
ReplicatedRepository(String aName,
Repository aReplicaRepository,
Repository aMasterRepository) {
mName = aName;
mReplicaRepository = aReplicaRepository;
mMasterRepository = aMasterRepository;
mStoragePool = new StoragePool() {
@Override
protected <S extends Storable> Storage<S> createStorage(Class<S> type)
throws SupportException, RepositoryException
{
StorableInfo<S> info = StorableIntrospector.examine(type);
if (info.isAuthoritative()) {
// Cannot rely on replica to be up-to-date, so always go to master.
try {
return mMasterRepository.storageFor(type);
} catch (UnsupportedTypeException e) {
if (info.isIndependent()) {
// Okay, no master. A standalone replica is
// considered to be authoritative.
return mReplicaRepository.storageFor(type);
}
throw e;
}
} else {
Storage<S> replicaStorage = mReplicaRepository.storageFor(type);
try {
return new ReplicatedStorage<S>(ReplicatedRepository.this, replicaStorage);
} catch (UnsupportedTypeException e) {
if (info.isIndependent()) {
// Okay, no master.
return replicaStorage;
}
throw e;
}
}
}
};
}
public String getName() {
return mName;
}
// Used by test suite.
Repository getReplicaRepository() {
return mReplicaRepository;
}
public Repository getMasterRepository() {
return mMasterRepository;
}
public <S extends Storable> Storage<S> storageFor(Class<S> type)
throws MalformedTypeException, SupportException, RepositoryException
{
return mStoragePool.get(type);
}
public Transaction enterTransaction() {
Transaction master;
try {
master = mMasterRepository.enterTransaction();
} catch (BelatedCreationException e) {
return new ReadOnlyTransaction(mReplicaRepository.enterTransaction());
}
return new TransactionPair(master, mReplicaRepository.enterTransaction());
}
public Transaction enterTransaction(IsolationLevel level) {
Transaction master;
try {
master = mMasterRepository.enterTransaction(level);
} catch (BelatedCreationException e) {
return new ReadOnlyTransaction(mReplicaRepository.enterTransaction(level));
}
return new TransactionPair(master, mReplicaRepository.enterTransaction(level));
}
public Transaction enterTopTransaction(IsolationLevel level) {
Transaction master;
try {
master = mMasterRepository.enterTopTransaction(level);
} catch (BelatedCreationException e) {
return new ReadOnlyTransaction(mReplicaRepository.enterTopTransaction(level));
}
return new TransactionPair(master, mReplicaRepository.enterTopTransaction(level));
}
public IsolationLevel getTransactionIsolationLevel() {
IsolationLevel replicaLevel = mReplicaRepository.getTransactionIsolationLevel();
if (replicaLevel == null) {
return null;
}
IsolationLevel masterLevel = mMasterRepository.getTransactionIsolationLevel();
if (masterLevel == null) {
return null;
}
return replicaLevel.lowestCommon(masterLevel);
}
@SuppressWarnings("unchecked")
public <C extends Capability> C getCapability(Class<C> capabilityType) {
if (capabilityType.isInstance(this)) {
if (ShutdownCapability.class.isAssignableFrom(capabilityType)) {
if (mReplicaRepository.getCapability(capabilityType) == null &&
mMasterRepository.getCapability(capabilityType) == null) {
return null;
}
}
return (C) this;
}
// Favor replica's indexing capabilities, since they are used for queries.
boolean favorReplica =
IndexInfoCapability.class.isAssignableFrom(capabilityType) ||
IndexEntryAccessCapability.class.isAssignableFrom(capabilityType);
C masterCap = mMasterRepository.getCapability(capabilityType);
C replicaCap = mReplicaRepository.getCapability(capabilityType);
if (favorReplica) {
return replicaCap != null ? replicaCap : masterCap;
} else {
return masterCap != null ? masterCap : replicaCap;
}
}
public void close() {
mReplicaRepository.close();
mMasterRepository.close();
}
/*
public boolean isClosed() {
return mReplicaRepository.isClosed() || mMasterRepository.isClosed();
}
*/
public String[] getUserStorableTypeNames() throws RepositoryException {
StorableInfoCapability replicaCap =
mReplicaRepository.getCapability(StorableInfoCapability.class);
StorableInfoCapability masterCap =
mMasterRepository.getCapability(StorableInfoCapability.class);
if (replicaCap == null) {
if (masterCap == null) {
return new String[0];
}
return masterCap.getUserStorableTypeNames();
} else if (masterCap == null) {
return replicaCap.getUserStorableTypeNames();
}
// Merge the two sets together.
Set<String> names = new LinkedHashSet<String>();
for (String name : replicaCap.getUserStorableTypeNames()) {
names.add(name);
}
for (String name : masterCap.getUserStorableTypeNames()) {
names.add(name);
}
return names.toArray(new String[names.size()]);
}
public boolean isSupported(Class<Storable> type) {
StorableInfoCapability replicaCap =
mReplicaRepository.getCapability(StorableInfoCapability.class);
StorableInfoCapability masterCap =
mMasterRepository.getCapability(StorableInfoCapability.class);
return (masterCap == null || masterCap.isSupported(type))
&& (replicaCap == null || replicaCap.isSupported(type));
}
public boolean isPropertySupported(Class<Storable> type, String name) {
StorableInfoCapability replicaCap =
mReplicaRepository.getCapability(StorableInfoCapability.class);
StorableInfoCapability masterCap =
mMasterRepository.getCapability(StorableInfoCapability.class);
return (masterCap == null || masterCap.isPropertySupported(type, name))
&& (replicaCap == null || replicaCap.isPropertySupported(type, name));
}
public boolean isAutoShutdownEnabled() {
ShutdownCapability cap = mReplicaRepository.getCapability(ShutdownCapability.class);
if (cap != null && cap.isAutoShutdownEnabled()) {
return true;
}
cap = mMasterRepository.getCapability(ShutdownCapability.class);
if (cap != null && cap.isAutoShutdownEnabled()) {
return true;
}
return false;
}
public void setAutoShutdownEnabled(boolean enabled) {
ShutdownCapability cap = mReplicaRepository.getCapability(ShutdownCapability.class);
if (cap != null) {
cap.setAutoShutdownEnabled(enabled);
}
cap = mMasterRepository.getCapability(ShutdownCapability.class);
if (cap != null) {
cap.setAutoShutdownEnabled(enabled);
}
}
public void shutdown() {
ShutdownCapability cap = mReplicaRepository.getCapability(ShutdownCapability.class);
if (cap != null) {
cap.shutdown();
} else {
mReplicaRepository.close();
}
cap = mMasterRepository.getCapability(ShutdownCapability.class);
if (cap != null) {
cap.shutdown();
} else {
mMasterRepository.close();
}
}
/**
* Repairs replicated storables by synchronizing the replica repository
* against the master repository.
*
* @param type type of storable to re-sync
* @param desiredSpeed throttling parameter - 1.0 = full speed, 0.5 = half
* speed, 0.1 = one-tenth speed, etc
* @param filter optional query filter to limit which objects get re-sync'ed
* @param filterValues filter values for optional filter
*/
public <S extends Storable> void resync(Class<S> type,
double desiredSpeed,
String filter,
Object... filterValues)
throws RepositoryException
{
resync(type, null, desiredSpeed, filter, filterValues);
}
/**
* Repairs replicated storables by synchronizing the replica repository
* against the master repository.
*
* @param type type of storable to re-sync
* @param listener optional listener which gets notified as storables are re-sync'd
* @param desiredSpeed throttling parameter - 1.0 = full speed, 0.5 = half
* speed, 0.1 = one-tenth speed, etc
* @param filter optional query filter to limit which objects get re-sync'ed
* @param filterValues filter values for optional filter
* @since 1.2
*/
public <S extends Storable> void resync(Class<S> type,
ResyncCapability.Listener<? super S> listener,
double desiredSpeed,
String filter,
Object... filterValues)
throws RepositoryException
{
ReplicationTrigger<S> replicationTrigger;
if (storageFor(type) instanceof ReplicatedStorage) {
replicationTrigger = ((ReplicatedStorage) storageFor(type)).getReplicationTrigger();
} else {
throw new UnsupportedTypeException("Storable type is not replicated", type);
}
Storage<S> replicaStorage, masterStorage;
replicaStorage = mReplicaRepository.storageFor(type);
masterStorage = mMasterRepository.storageFor(type);
Query<S> replicaQuery, masterQuery;
if (filter == null) {
replicaQuery = replicaStorage.query();
masterQuery = masterStorage.query();
} else {
replicaQuery = replicaStorage.query(filter).withValues(filterValues);
masterQuery = masterStorage.query(filter).withValues(filterValues);
}
// Order both queries the same so that they can be run in parallel.
// Favor natural order of replica, since cursors may be opened and
// re-opened on it.
String[] orderBy = selectNaturalOrder(mReplicaRepository, type, replicaQuery.getFilter());
if (orderBy == null) {
orderBy = selectNaturalOrder(mMasterRepository, type, masterQuery.getFilter());
if (orderBy == null) {
Set<String> pkSet =
StorableIntrospector.examine(type).getPrimaryKeyProperties().keySet();
orderBy = pkSet.toArray(new String[pkSet.size()]);
}
}
Comparator comparator = SortedCursor.createComparator(type, orderBy);
replicaQuery = replicaQuery.orderBy(orderBy);
masterQuery = masterQuery.orderBy(orderBy);
Throttle throttle;
if (desiredSpeed >= 1.0) {
throttle = null;
} else {
if (desiredSpeed < 0.0) {
desiredSpeed = 0.0;
}
// 50 samples
throttle = new Throttle(50);
}
Transaction replicaTxn = mReplicaRepository.enterTransaction();
try {
replicaTxn.setForUpdate(true);
resync(replicationTrigger,
replicaStorage, replicaQuery,
masterStorage, masterQuery,
listener,
throttle, desiredSpeed,
comparator, replicaTxn);
replicaTxn.commit();
} finally {
replicaTxn.exit();
}
}
@SuppressWarnings("unchecked")
private <S extends Storable> void resync(ReplicationTrigger<S> replicationTrigger,
Storage<S> replicaStorage, Query<S> replicaQuery,
Storage<S> masterStorage, Query<S> masterQuery,
ResyncCapability.Listener<? super S> listener,
Throttle throttle, double desiredSpeed,
Comparator comparator, Transaction replicaTxn)
throws RepositoryException
{
final Log log = LogFactory.getLog(ReplicatedRepository.class);
Cursor<S> replicaCursor = null;
Cursor<S> masterCursor = null;
try {
while (replicaCursor == null) {
try {
replicaCursor = replicaQuery.fetch();
} catch (CorruptEncodingException e) {
S replicaWithKeyOnly = recoverReplicaKey(replicaStorage, e);
if (!deleteCorruptEntry(replicationTrigger, replicaWithKeyOnly, e)) {
// Cannot delete it, so just give up.
throw e;
}
}
}
masterCursor = masterQuery.fetch();
S lastReplicaEntry = null;
S replicaEntry = null;
S masterEntry = null;
int count = 0, txnCount = 0;
while (true) {
if (throttle != null) {
try {
// 100 millisecond clock precision
throttle.throttle(desiredSpeed, 100);
} catch (InterruptedException e) {
throw new FetchInterruptedException(e);
}
}
if (replicaEntry == null && replicaCursor != null) {
int skippedCount = 0;
while (replicaCursor.hasNext()) {
try {
replicaEntry = replicaCursor.next();
if (listener != null) {
listener.afterLoad(replicaEntry);
}
if (skippedCount > 0) {
if (skippedCount == 1) {
log.warn("Skipped corrupt replica entry before this one: " +
replicaEntry);
} else {
log.warn("Skipped " + skippedCount +
" corrupt replica entries before this one: " +
replicaEntry);
}
}
break;
} catch (CorruptEncodingException e) {
// Exception forces cursor to close. Close again to be sure.
replicaCursor.close();
replicaCursor = null;
S replicaWithKeyOnly = recoverReplicaKey(replicaStorage, e);
boolean deleted = deleteCorruptEntry
(replicationTrigger, replicaWithKeyOnly, e);
// Re-open (if we can)
if (lastReplicaEntry == null) {
break;
}
replicaCursor = replicaQuery.fetchAfter(lastReplicaEntry);
if (deleted) {
// Skip if it cannot be deleted.
try {
skippedCount = replicaCursor.skipNext(++skippedCount);
log.info("Skipped corrupt replica entry", e);
if (replicaWithKeyOnly != null) {
// Try to update entry which could not be deleted.
replicaEntry = replicaWithKeyOnly;
if (listener != null) {
listener.afterLoad(replicaEntry);
}
break;
}
} catch (FetchException e2) {
log.error("Unable to skip past corrupt replica entry", e2);
throw e;
}
}
}
}
}
if (count++ >= RESYNC_WATERMARK || txnCount >= RESYNC_BATCH_SIZE) {
replicaTxn.commit();
if (replicaCursor != null) {
// Cursor should auto-close after txn commit, but force
// a close anyhow. Cursor is re-opened when it is
// allowed to advance.
replicaCursor.close();
replicaCursor = null;
}
count = 0;
txnCount = 0;
}
if (masterEntry == null && masterCursor.hasNext()) {
masterEntry = masterCursor.next();
}
Runnable resyncTask = null;
// Comparator should treat null as high.
int compare = comparator.compare(replicaEntry, masterEntry);
if (compare < 0) {
// Bogus record exists only in replica so delete it.
resyncTask = prepareResyncTask
(replicationTrigger, listener, replicaEntry, null);
// Allow replica to advance.
if (replicaCursor == null) {
replicaCursor = replicaQuery.fetchAfter(replicaEntry);
}
lastReplicaEntry = replicaEntry;
replicaEntry = null;
} else if (compare > 0) {
// Replica cursor is missing an entry so copy it.
resyncTask = prepareResyncTask
(replicationTrigger, listener, null, masterEntry);
// Allow master to advance.
masterEntry = null;
} else {
// If compare is zero, replicaEntry and masterEntry are
// either both null or both non-null.
if (replicaEntry == null && masterEntry == null) {
// Both cursors exhausted -- resync is complete.
break;
}
// Both replicaEntry and masterEntry are non-null.
if (!replicaEntry.equalProperties(masterEntry)) {
// Replica is stale.
resyncTask = prepareResyncTask
(replicationTrigger, listener, replicaEntry, masterEntry);
}
// Entries are synchronized so allow both cursors to advance.
if (replicaCursor == null) {
replicaCursor = replicaQuery.fetchAfter(replicaEntry);
}
lastReplicaEntry = replicaEntry;
replicaEntry = null;
masterEntry = null;
}
if (resyncTask != null) {
txnCount++;
resyncTask.run();
}
}
} finally {
try {
if (masterCursor != null) {
masterCursor.close();
}
} finally {
if (replicaCursor != null) {
replicaCursor.close();
}
}
}
}
private <S extends Storable> S recoverReplicaKey(Storage<S> replicaStorage,
CorruptEncodingException e)
{
Storable withKey = e.getStorableWithPrimaryKey();
if (withKey != null && withKey.storableType() == replicaStorage.getStorableType()) {
return (S) withKey;
}
return null;
}
private <S extends Storable> boolean deleteCorruptEntry
(ReplicationTrigger<S> replicationTrigger,
S replicaWithKeyOnly,
CorruptEncodingException e)
throws RepositoryException
{
if (replicaWithKeyOnly == null) {
return false;
}
final Log log = LogFactory.getLog(ReplicatedRepository.class);
// Delete corrupt replica entry.
try {
replicationTrigger.deleteReplica(replicaWithKeyOnly);
log.info("Deleted corrupt replica entry: " +
replicaWithKeyOnly.toStringKeyOnly(), e);
return true;
} catch (PersistException e2) {
log.warn("Unable to delete corrupt replica entry: " +
replicaWithKeyOnly.toStringKeyOnly(), e2);
return false;
}
}
private <S extends Storable> Runnable prepareResyncTask
(final ReplicationTrigger<S> replicationTrigger,
final ResyncCapability.Listener<? super S> listener,
final S replicaEntry,
final S masterEntry)
throws RepositoryException
{
if (replicaEntry == null && masterEntry == null) {
// If both are null, then there's nothing to do, is there?
// Note: Caller shouldn't have passed double nulls to
// prepareResyncTask in the first place.
return null;
}
Runnable task = new Runnable() {
public void run() {
// Reload deletes and updates. Don't reload inserts, because it
// makes a resync from an empty replica way too expensive.
boolean reload = masterEntry == null || replicaEntry != null;
try {
replicationTrigger.resyncEntries(listener, replicaEntry, masterEntry, reload);
} catch (Exception e) {
LogFactory.getLog(ReplicatedRepository.class).error(null, e);
}
}
};
return task;
}
}
|