summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/repo/replicated/ReplicationTrigger.java
blob: 1b88e3e3bde7bc8ca8169cbb10fed7791f5af133 (plain)
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
/*
 * 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.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.cojen.util.ThrowUnchecked;

import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.OptimisticLockException;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.PersistNoneException;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.Transaction;
import com.amazon.carbonado.Trigger;
import com.amazon.carbonado.UniqueConstraintException;

import com.amazon.carbonado.capability.ResyncCapability;

import com.amazon.carbonado.spi.RepairExecutor;
import com.amazon.carbonado.spi.TriggerManager;

/**
 * All inserts/updates/deletes are first committed to the master storage, then
 * duplicated and committed to the replica.
 *
 * @author Don Schneider
 * @author Brian S O'Neill
 */
class ReplicationTrigger<S extends Storable> extends Trigger<S> {
    private final ReplicatedRepository mRepository;
    private final Storage<S> mReplicaStorage;
    private final Storage<S> mMasterStorage;

    private final TriggerManager<S> mTriggerManager;

    ReplicationTrigger(ReplicatedRepository repository,
                       Storage<S> replicaStorage,
                       Storage<S> masterStorage)
    {
        mRepository = repository;
        mReplicaStorage = replicaStorage;
        mMasterStorage = masterStorage;

        // Use TriggerManager to locally disable trigger execution during
        // resync and repairs.
        mTriggerManager = new TriggerManager<S>();
        mTriggerManager.addTrigger(this);

        BlobReplicationTrigger<S> blobTrigger = BlobReplicationTrigger.create(masterStorage);
        if (blobTrigger != null) {
            mTriggerManager.addTrigger(blobTrigger);
        }

        ClobReplicationTrigger<S> clobTrigger = ClobReplicationTrigger.create(masterStorage);
        if (clobTrigger != null) {
            mTriggerManager.addTrigger(clobTrigger);
        }

        replicaStorage.addTrigger(mTriggerManager);
    }

    @Override
    public Object beforeInsert(S replica) throws PersistException {
        return beforeInsert(null, replica, false);
    }

    @Override
    public Object beforeTryInsert(S replica) throws PersistException {
        return beforeInsert(null, replica, true);
    }

    @Override
    public Object beforeInsert(Transaction txn, S replica) throws PersistException {
        return beforeInsert(txn, replica, false);
    }

    @Override
    public Object beforeTryInsert(Transaction txn, S replica) throws PersistException {
        return beforeInsert(txn, replica, true);
    }

    private Object beforeInsert(Transaction txn, S replica, boolean forTry) throws PersistException {
        if (txn instanceof ReadOnlyTransaction) {
            // This operation was intended to take place in a transaction, but
            // the master repository was unavailable when the transaction was
            // entered.
                throw new PersistException("Current transaction is read-only.");
            }

            final S master = mMasterStorage.prepare();
            replica.copyAllProperties(master);

            try {
                if (forTry) {
                    if (!master.tryInsert()) {
                        throw abortTry();
                    }
                } else {
                    master.insert();
                }
            } catch (UniqueConstraintException e) {
                // This may be caused by an inconsistency between replica and
                // master. Here's one scenerio: user called tryLoad and saw the
                // entry does not exist. So instead of calling update, he/she calls
                // insert. If the master entry exists, then there is an
                // inconsistency. The code below checks for this specific kind of
                // error and repairs it by inserting a record in the replica.

                // Here's another scenerio: Unique constraint was caused by an
                // inconsistency with the values of the alternate keys. User
                // expected alternate keys to have unique values, as indicated by
                // replica.

                repair(replica);

                // Throw exception since we don't know what the user's intentions
                // really are.
                throw e;
            }

            // Master may have applied sequences to unitialized primary keys, so
            // copy primary keys to replica. Mark properties as dirty to allow
            // primary key to be changed.
            replica.markPropertiesDirty();

            // Copy all properties in order to trigger constraints that
            // master should have resolved.
            master.copyAllProperties(replica);

            return null;
        }

        @Override
        public Object beforeUpdate(S replica) throws PersistException {
            return beforeUpdate(null, replica, false);
        }

        @Override
        public Object beforeTryUpdate(S replica) throws PersistException {
            return beforeUpdate(null, replica, true);
        }

        @Override
        public Object beforeUpdate(Transaction txn, S replica) throws PersistException {
            return beforeUpdate(txn, replica, false);
        }

        @Override
        public Object beforeTryUpdate(Transaction txn, S replica) throws PersistException {
            return beforeUpdate(txn, replica, true);
        }

        private Object beforeUpdate(Transaction txn, S replica, boolean forTry) throws PersistException {
            if (txn instanceof ReadOnlyTransaction) {
                // This operation was intended to take place in a transaction, but
                // the master repository was unavailable when the transaction was
                // entered.
                throw new PersistException("Current transaction is read-only.");
            }

            final S master = mMasterStorage.prepare();
            replica.copyPrimaryKeyProperties(master);
            replica.copyVersionProperty(master);
            replica.copyDirtyProperties(master);

            try {
                if (forTry) {
                    if (!master.tryUpdate()) {
                        // Master record does not exist. To ensure consistency,
                        // delete record from replica.
                        if (tryDeleteReplica(replica)) {
                            // Replica was inconsistent, but caller might be in a
                            // transaction and rollback the repair. Run repair
                            // again in separate thread to ensure it sticks.
                            repair(replica);
                        }
                        throw abortTry();
                    }
                } else {
                    try {
                        master.update();
                    } catch (PersistNoneException e) {
                        // Master record does not exist. To ensure consistency,
                        // delete record from replica.
                        if (tryDeleteReplica(replica)) {
                            // Replica was inconsistent, but caller might be in a
                            // transaction and rollback the repair. Run repair
                            // again in separate thread to ensure it sticks.
                            repair(replica);
                        }
                    throw e;
                }
            }
        } catch (OptimisticLockException e) {
            // This may be caused by an inconsistency between replica and
            // master.

            repair(replica);

            // Throw original exception since we don't know what the user's
            // intentions really are.
            throw e;
        }

        // Copy master properties back, since its repository may have
        // altered property values as a side effect.
        master.copyUnequalProperties(replica);

        return null;
    }

    @Override
    public Object beforeDelete(S replica) throws PersistException {
        return beforeDelete(null, replica);
    }

    @Override
    public Object beforeDelete(Transaction txn, S replica) throws PersistException {
        if (txn instanceof ReadOnlyTransaction) {
            // This operation was intended to take place in a transaction, but
            // the master repository was unavailable when the transaction was
            // entered.
            throw new PersistException("Current transaction is read-only.");
        }

        S master = mMasterStorage.prepare();
        replica.copyPrimaryKeyProperties(master);

        // If this fails to delete anything, don't care. Any delete failure
        // will be detected when the replica is deleted. If there was an
        // inconsistency, it is resolved after the replica is deleted.
        master.tryDelete();

        return null;
    }

    /**
     * Re-sync the replica to the master. The primary keys of both entries are
     * assumed to match.
     *
     * @param listener optional
     * @param replicaEntry current replica entry, or null if none
     * @param masterEntry current master entry, or null if none
     * @param reload true to reload master entry
     */
    void resyncEntries(ResyncCapability.Listener<? super S> listener,
                       S replicaEntry, S masterEntry, boolean reload)
        throws FetchException, PersistException
    {
        if (replicaEntry == null && masterEntry == null) {
            return;
        }

        Log log = LogFactory.getLog(ReplicatedRepository.class);

        setReplicationDisabled();
        try {
            Transaction masterTxn = mRepository.getMasterRepository().enterTransaction();
            Transaction replicaTxn = mRepository.getReplicaRepository().enterTransaction();

            try {
                replicaTxn.setForUpdate(true);

                if (reload) {
                    if (masterEntry == null) {
                        masterEntry = mMasterStorage.prepare();
                        replicaEntry.copyAllProperties(masterEntry);
                    }
                    if (!masterEntry.tryLoad()) {
                        masterEntry = null;
                    }
                }

                final S newReplicaEntry;
                if (replicaEntry == null) {
                    newReplicaEntry = mReplicaStorage.prepare();
                    masterEntry.copyAllProperties(newReplicaEntry);
                    log.info("Inserting missing replica entry: " + newReplicaEntry);
                } else if (masterEntry != null) {
                    if (replicaEntry.equalProperties(masterEntry)) {
                        return;
                    }
                    newReplicaEntry = mReplicaStorage.prepare();
                    transferToReplicaEntry(replicaEntry, masterEntry, newReplicaEntry);
                    log.info("Updating stale replica entry with: " + newReplicaEntry);
                } else {
                    newReplicaEntry = null;
                    log.info("Deleting bogus replica entry: " + replicaEntry);
                }

                final Object state;
                if (listener == null) {
                    state = null;
                } else {
                    if (replicaEntry == null) {
                        state = listener.beforeInsert(newReplicaEntry);
                    } else if (masterEntry != null) {
                        state = listener.beforeUpdate(replicaEntry, newReplicaEntry);
                    } else {
                        state = listener.beforeDelete(replicaEntry);
                    }
                }

                try {
                    // Delete old entry.
                    if (replicaEntry != null) {
                        try {
                            replicaEntry.tryDelete();
                        } catch (PersistException e) {
                            log.error("Unable to delete replica entry: " + replicaEntry, e);
                            if (masterEntry != null) {
                                // Try to update instead.
                                log.info("Updating corrupt replica entry with: " +
                                         newReplicaEntry);
                                try {
                                    newReplicaEntry.update();
                                    // This disables the insert step, which is not needed now.
                                    masterEntry = null;
                                } catch (PersistException e2) {
                                    log.error("Unable to update replica entry: " +
                                              replicaEntry, e2);
                                    resyncFailed(listener, replicaEntry, masterEntry,
                                                 newReplicaEntry, state);
                                    return;
                                }
                            }
                        }
                    }

                    // Insert new entry.
                    if (masterEntry != null && newReplicaEntry != null) {
                        if (!newReplicaEntry.tryInsert()) {
                            /*
                            log.warn("Unable to insert: " + newReplicaEntry);
                            Storable copy = newReplicaEntry.copy();
                            if (copy.tryLoad()) {
                                log.warn("Blocked by: " + copy);
                            } else {
                                log.warn("Nothing loaded");
                                try {
                                    newReplicaEntry.insert();
                                } catch (Exception e) {
                                    log.warn("Still cannot insert", e);
                                    return;
                                }
                            }
                            */

                            // FIXME: can be caused by alt key constraint
                            // Try to correct bizarre corruption.
                            newReplicaEntry.tryDelete();
                            newReplicaEntry.tryInsert();
                        }
                    }

                    if (listener != null) {
                        if (replicaEntry == null) {
                            listener.afterInsert(newReplicaEntry, state);
                        } else if (masterEntry != null) {
                            listener.afterUpdate(newReplicaEntry, state);
                        } else {
                            listener.afterDelete(replicaEntry, state);
                        }
                    }

                    replicaTxn.commit();
                } catch (Throwable e) {
                    resyncFailed(listener, replicaEntry, masterEntry, newReplicaEntry, state);
                    ThrowUnchecked.fire(e);
                }
            } finally {
                try {
                    masterTxn.exit();
                } finally {
                    // Do second, favoring any exception thrown from it.
                    replicaTxn.exit();
                }
            }
        } finally {
            setReplicationEnabled();
        }
    }

    private void resyncFailed(ResyncCapability.Listener<? super S> listener,
                              S replicaEntry, S masterEntry,
                              S newReplicaEntry, Object state)
    {
        if (listener != null) {
            try {
                if (replicaEntry == null) {
                    listener.failedInsert(newReplicaEntry, state);
                } else if (masterEntry != null) {
                    listener.failedUpdate(newReplicaEntry, state);
                } else {
                    listener.failedDelete(replicaEntry, state);
                }
            } catch (Throwable e2) {
                Thread t = Thread.currentThread();
                t.getUncaughtExceptionHandler().uncaughtException(t, e2);
            }
        }
    }

    private void transferToReplicaEntry(S replicaEntry, S masterEntry, S newReplicaEntry) {
        // First copy from old replica to preserve values of any independent
        // properties. Be sure not to copy nulls from old replica to new
        // replica, in case new non-nullable properties have been added. This
        // is why copyUnequalProperties is called instead of copyAllProperties.
        try {
            replicaEntry.copyUnequalProperties(newReplicaEntry);
        } catch (IllegalArgumentException e) {
            // Some property cannot be copied, so copy one at a time and skip
            // the broken one.
            Map<String,Object> propertyMap = replicaEntry.propertyMap();
            for (Map.Entry<String, Object> entry : propertyMap.entrySet()) {
                String name = entry.getKey();
                Object oldValue = entry.getValue();
                try {
                    Object newValue = newReplicaEntry.getPropertyValue(name);
                    if (oldValue == null ? newValue != null : !oldValue.equals(newValue)) {
                        newReplicaEntry.setPropertyValue(name, oldValue);
                    }
                } catch (IllegalArgumentException e2) {
                    // Skip it.
                } catch (UnsupportedOperationException e2) {
                    // Skip it.
                }
            }
        }

        // Calling copyAllProperties will skip unsupported independent
        // properties in master, thus preserving old independent property values.
        masterEntry.copyAllProperties(newReplicaEntry);
    }

    /**
     * Runs a repair in a background thread. This is done for two reasons: It
     * allows repair to not be hindered by locks acquired by transactions and
     * repairs don't get rolled back when culprit exception is thrown. Culprit
     * may be UniqueConstraintException or OptimisticLockException.
     */
    private void repair(S replica) throws PersistException {
        replica = (S) replica.copy();
        S master = mMasterStorage.prepare();
        // Must copy more than just primary key properties to master since
        // replica object might only have alternate keys.
        replica.copyAllProperties(master);

        try {
            if (replica.tryLoad()) {
                if (master.tryLoad()) {
                    if (replica.equalProperties(master)) {
                        // Both are equal -- no repair needed.
                        return;
                    }
                }
            } else {
                if (!master.tryLoad()) {
                    // Both are missing -- no repair needed.
                    return;
                }
            }
        } catch (IllegalStateException e) {
            // Can be caused by not fully defining the primary key on the
            // replica, but an alternate key is. The insert will fail anyhow,
            // so don't try to repair.
            return;
        } catch (FetchException e) {
            throw e.toPersistException();
        }

        final S finalReplica = replica;
        final S finalMaster = master;

        RepairExecutor.execute(new Runnable() {
            public void run() {
                try {
                    Transaction txn = mRepository.enterTransaction();
                    try {
                        txn.setForUpdate(true);
                        if (finalReplica.tryLoad()) {
                            if (finalMaster.tryLoad()) {
                                resyncEntries(null, finalReplica, finalMaster, false);
                            } else {
                                resyncEntries(null, finalReplica, null, false);
                            }
                        } else if (finalMaster.tryLoad()) {
                            resyncEntries(null, null, finalMaster, false);
                        }
                        txn.commit();
                    } finally {
                        txn.exit();
                    }
                } catch (FetchException fe) {
                    Log log = LogFactory.getLog(ReplicatedRepository.class);
                    log.warn("Unable to check if repair is required for " +
                             finalReplica.toStringKeyOnly(), fe);
                } catch (PersistException pe) {
                    Log log = LogFactory.getLog(ReplicatedRepository.class);
                    log.error("Unable to repair entry " +
                              finalReplica.toStringKeyOnly(), pe);
                }
            }
        });
    }

    boolean addTrigger(Trigger<? super S> trigger) {
        return mTriggerManager.addTrigger(trigger);
    }

    boolean removeTrigger(Trigger<? super S> trigger) {
        return mTriggerManager.removeTrigger(trigger);
    }

    /**
     * Deletes the replica entry with replication disabled.
     */
    boolean tryDeleteReplica(Storable replica) throws PersistException {
        // Prevent trigger from being invoked by deleting replica.
        TriggerManager tm = mTriggerManager;
        tm.locallyDisableDelete();
        try {
            return replica.tryDelete();
        } finally {
            tm.locallyEnableDelete();
        }
    }

    /**
     * Deletes the replica entry with replication disabled.
     */
    void deleteReplica(Storable replica) throws PersistException {
        // Prevent trigger from being invoked by deleting replica.
        TriggerManager tm = mTriggerManager;
        tm.locallyDisableDelete();
        try {
            replica.delete();
        } finally {
            tm.locallyEnableDelete();
        }
    }

    void setReplicationDisabled() {
        // This method disables not only this trigger, but all triggers added
        // to manager.
        TriggerManager tm = mTriggerManager;
        tm.locallyDisableInsert();
        tm.locallyDisableUpdate();
        tm.locallyDisableDelete();
        tm.locallyDisableLoad();
    }

    void setReplicationEnabled() {
        TriggerManager tm = mTriggerManager;
        tm.locallyEnableInsert();
        tm.locallyEnableUpdate();
        tm.locallyEnableDelete();
        tm.locallyEnableLoad();
    }
}