summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java
blob: 978873946b16ac65292c5695bddb8fa4b186c779 (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
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
/*
 * 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.layout;

import java.io.InputStream;
import java.io.IOException;

import java.lang.annotation.Annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.logging.LogFactory;

import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.FetchDeadlockException;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.FetchInterruptedException;
import com.amazon.carbonado.FetchNoneException;
import com.amazon.carbonado.FetchTimeoutException;
import com.amazon.carbonado.IsolationLevel;
import com.amazon.carbonado.PersistDeadlockException;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.PersistTimeoutException;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.Transaction;
import com.amazon.carbonado.UniqueConstraintException;

import com.amazon.carbonado.capability.ResyncCapability;

import com.amazon.carbonado.info.StorableInfo;
import com.amazon.carbonado.info.StorableIntrospector;
import com.amazon.carbonado.info.StorableProperty;
import com.amazon.carbonado.info.StorablePropertyAdapter;
import com.amazon.carbonado.info.StorablePropertyAnnotation;

import com.amazon.carbonado.util.SoftValuedCache;

/**
 * Factory for obtaining references to storable layouts.
 *
 * @author Brian S O'Neill
 */
public class LayoutFactory implements LayoutCapability {
    // The first entry is the primary hash multiplier. Subsequent ones are
    // rehash multipliers.
    private static final int[] HASH_MULTIPLIERS = {31, 63};

    final Repository mRepository;
    final Storage<StoredLayout> mLayoutStorage;
    final Storage<StoredLayoutProperty> mPropertyStorage;

    private SoftValuedCache<Class<? extends Storable>, Layout> mReconstructed;

    // Added this to allow consumers of the the main Carbonado to deal which
    // changes to how LayoutFactories are reconstructed from streams.
    public static final int VERSION = 2;

    /**
     * @throws com.amazon.carbonado.SupportException if underlying repository
     * does not support the storables for persisting storable layouts
     */
    public LayoutFactory(Repository repo) throws RepositoryException {
        mRepository = repo;
        mLayoutStorage = repo.storageFor(StoredLayout.class);
        mPropertyStorage = repo.storageFor(StoredLayoutProperty.class);
    }

    /**
     * Returns the layout matching the current definition of the given type.
     *
     * @throws PersistException if type represents a new generation, but
     * persisting this information failed
     */
    public Layout layoutFor(Class<? extends Storable> type)
        throws FetchException, PersistException
    {
        return layoutFor(type, null);
    }

    /**
     * Returns the layout matching the current definition of the given type.
     *
     * @throws PersistException if type represents a new generation, but
     * persisting this information failed
     */
    public Layout layoutFor(Class<? extends Storable> type, LayoutOptions options)
        throws FetchException, PersistException
    {
        return layoutFor(false, type, options);
    }

    /**
     * Returns the layout matching the current definition of the given type.
     *
     * @param readOnly if true, don't attempt to persist new generation because
     * underlying repository is read-only
     * @throws PersistException if type represents a new generation, but
     * persisting this information failed
     */
    public Layout layoutFor(final boolean readOnly,
                            final Class<? extends Storable> type, final LayoutOptions options)
        throws FetchException, PersistException
    {
        if (options != null) {
            // Make side-effect consistently applied.
            options.readOnly();
        }

        synchronized (this) {
            if (mReconstructed != null) {
                Layout layout = mReconstructed.get(type);
                if (layout != null) {
                    return layout;
                }
            }
        }

        final StorableInfo<?> info = StorableIntrospector.examine(type);

        // Launch layout request in a new separate thread to ensure that
        // transaction is top-level.

        class LayoutRequest extends Thread {
            private boolean done;
            private Layout layout;
            private FetchException fetchEx;
            private PersistException persistEx;
            private RuntimeException runtimeEx;

            public synchronized void run() {
                try {
                    try {
                        layout = layoutFor(readOnly, info, options);
                    } catch (FetchException e) {
                        fetchEx = e;
                    } catch (PersistException e) {
                        persistEx = e;
                    } catch (RuntimeException e) {
                        // This is a catchall for any other exception which
                        // might happen so that it doesn't get absorbed by
                        // this thread.
                        runtimeEx = e;
                    }
                } finally {
                    done = true;
                    notifyAll();
                }
            }

            synchronized Layout getLayout()
                throws FetchException, PersistException, InterruptedException
            {
                while (!done) {
                    wait();
                }
                if (fetchEx != null) {
                    // Wrap to get complete stack trace.
                    throw new FetchException(fetchEx);
                }
                if (persistEx != null) {
                    // Wrap to get complete stack trace.
                    throw new PersistException(persistEx);
                }
                if (runtimeEx != null) {
                    // Wrap to get complete stack trace.
                    throw new RuntimeException(runtimeEx);
                }
                return layout;
            }
        }

        LayoutRequest request = new LayoutRequest();
        request.setDaemon(true);
        request.start();

        try {
            return request.getLayout();
        } catch (InterruptedException e) {
            throw new FetchInterruptedException();
        }
    }

    private Layout layoutFor(boolean readOnly, StorableInfo<?> info, LayoutOptions options)
        throws FetchException, PersistException
    {
        Layout layout;
        ResyncCapability resyncCap = null;

        // Try to insert metadata up to three times.
        boolean top = true;
        loadLayout: for (int retryCount = 3;;) {
            try {
                Transaction txn;
                if (top) {
                    txn = mRepository.enterTopTransaction(IsolationLevel.READ_COMMITTED);
                } else {
                    txn = mRepository.enterTransaction(IsolationLevel.READ_COMMITTED);
                }

                txn.setForUpdate(!readOnly);
                try {
                    // If type represents a new generation, then a new layout needs to
                    // be inserted.
                    Layout newLayout = null;

                    for (int i=0; i<HASH_MULTIPLIERS.length; i++) {
                        // Generate an identifier which has a high likelyhood of being unique.
                        long layoutID = mixInHash(0L, info, options, HASH_MULTIPLIERS[i]);

                        // Initially use for comparison purposes.
                        newLayout = new Layout(this, info, options, layoutID);

                        StoredLayout storedLayout = mLayoutStorage.prepare();
                        storedLayout.setLayoutID(layoutID);

                        if (!storedLayout.tryLoad()) {
                            // Not found, so break out and insert.
                            break;
                        }

                        Layout knownLayout = new Layout(this, storedLayout);
                        if (knownLayout.equalLayouts(newLayout)) {
                            // Type does not represent a new generation. Return
                            // existing layout.
                            layout = knownLayout;
                            break loadLayout;
                        }

                        if (knownLayout.getAllProperties().size() == 0) {
                            // This is clearly wrong. All Storables must have
                            // at least one property. Assume that layout record
                            // is corrupt so rebuild it.
                            break;
                        }

                        // If this point is reached, then there was a hash collision in
                        // the generated layout ID. This should be extremely rare.
                        // Rehash and try again.

                        if (i >= HASH_MULTIPLIERS.length - 1) {
                            // No more rehashes to attempt. This should be extremely,
                            // extremely rare, unless there is a bug somewhere.
                            throw new FetchException
                                ("Unable to generate unique layout identifier for " +
                                 info.getName());
                        }
                    }

                    // If this point is reached, then type represents a new
                    // generation. Calculate next generation value and insert.

                    // Note: The following query might find a record that
                    // didn't exist just a moment ago. This will cause a new
                    // generation value to be calculated, which is incorrect.
                    // Inserting the layout causes a unique constraint
                    // exception, which prevents the mistake from persisting.

                    assert(newLayout != null);
                    int generation = nextGeneration(mRepository, info.getStorableType().getName());

                    newLayout.insert(readOnly, generation);
                    layout = newLayout;

                    if (generation == 0) {
                        LogFactory.getLog(getClass())
                            .debug("New schema layout inserted: " + layout);
                    }

                    txn.commit();
                } finally {
                    txn.exit();
                }

                break;
            } catch (UniqueConstraintException e) {
                // This might be caused by a transient replication error. Retry
                // a few times before throwing exception. Wait up to a second
                // before each retry.
                retryCount = e.backoff(e, retryCount, 1000);
                resyncCap = mRepository.getCapability(ResyncCapability.class);
            } catch (FetchException e) {
                if (e instanceof FetchDeadlockException || e instanceof FetchTimeoutException) {
                    // Might be caused by coarse locks. Switch to nested
                    // transaction to share the locks.
                    if (top) {
                        top = false;
                        retryCount = e.backoff(e, retryCount, 100);
                        continue;
                    }
                }
                throw e;
            } catch (PersistException e) {
                if (e instanceof PersistDeadlockException || e instanceof PersistTimeoutException) {
                    // Might be caused by coarse locks. Switch to nested
                    // transaction to share the locks.
                    if (top) {
                        top = false;
                        retryCount = e.backoff(e, retryCount, 100);
                        continue;
                    }
                }
                throw e;
            }
        }

        if (!readOnly && resyncCap != null) {
            // Make sure that all layout records are sync'd.
            try {
                resyncCap.resync(StoredLayoutProperty.class, 1.0, null);
            } catch (RepositoryException e) {
                throw e.toPersistException();
            }
        }

        return layout;
    }

    /**
     * Returns the layout for a particular generation of the given type.
     *
     * @param generation desired generation
     * @throws FetchNoneException if generation not found
     */
    public Layout layoutFor(Class<? extends Storable> type, int generation)
        throws FetchException, FetchNoneException
    {
        StoredLayout storedLayout =
            mLayoutStorage.query("storableTypeName = ? & generation = ?")
            .with(type.getName()).with(generation)
            .loadOne();
        return new Layout(this, storedLayout);
    }

    /**
     * Read a layout as written by {@link Layout#writeTo}.
     *
     * @since 1.2.2
     */
    public Layout readLayoutFrom(InputStream in) throws IOException, RepositoryException {
        Transaction txn = mRepository.enterTransaction();
        try {
            txn.setForUpdate(true);

            StoredLayout storedLayout = mLayoutStorage.prepare();
            storedLayout.readFrom(in);
            try {
                storedLayout.insert();
            } catch (UniqueConstraintException e) {
                StoredLayout existing = mLayoutStorage.prepare();
                storedLayout.copyPrimaryKeyProperties(existing);
                if (existing.tryLoad()) {
                    // Only check subset of primary and alternate keys. The check
                    // of layout properties is more important.
                    if (!existing.getStorableTypeName().equals(storedLayout.getStorableTypeName()))
                    {
                        throw e;
                    }
                    storedLayout = existing;
                } else {
                    // Assume alternate key constraint, so increment the generation.
                    storedLayout.setGeneration
                        (nextGeneration(mRepository, storedLayout.getStorableTypeName()));
                    storedLayout.insert();
                }
            }

            int op;
            while ((op = in.read()) != 0) {
                StoredLayoutProperty storedProperty = mPropertyStorage.prepare();
                storedProperty.readFrom(in);
                try {
                    storedProperty.insert();
                } catch (UniqueConstraintException e) {
                    StoredLayoutProperty existing = mPropertyStorage.prepare();
                    storedProperty.copyPrimaryKeyProperties(existing);
                    if (!existing.tryLoad()) {
                        throw e;
                    }
                    storedProperty.copyVersionProperty(existing);
                    if (!existing.equalProperties(storedProperty)) {
                        throw e;
                    }
                }
            }

            txn.commit();

            return new Layout(this, storedLayout);
        } finally {
            txn.exit();
        }
    }

    synchronized void registerReconstructed
        (Class<? extends Storable> reconstructed, Layout layout)
    {
        if (mReconstructed == null) {
            mReconstructed = SoftValuedCache.newCache(7);
        }
        mReconstructed.put(reconstructed, layout);
    }

    static int nextGeneration(Repository repo, String typeName) throws FetchException {
        int highestGen = -1;
        {
            Cursor<StoredLayout> cursor;
            try {
                cursor = repo.storageFor(StoredLayout.class).query("storableTypeName = ?")
                    .with(typeName).orderBy("-generation").fetchSlice(0, 1L);
            } catch (RepositoryException e) {
                throw e.toFetchException();
            }

            try {
                if (cursor.hasNext()) {
                    highestGen = cursor.next().getGeneration();
                }
            } finally {
                cursor.close();
            }
        }

        Storage<StoredLayoutEquivalence> es;
        try {
            es = repo.storageFor(StoredLayoutEquivalence.class);
        } catch (RepositoryException e) {
            throw e.toFetchException();
        }

        Cursor<StoredLayoutEquivalence> cursor = es.query("storableTypeName = ?")
            .with(typeName).orderBy("-generation").fetchSlice(0, 1L);

        try {
            if (cursor.hasNext()) {
                highestGen = Math.max(highestGen, cursor.next().getGeneration());
            }
        } finally {
            cursor.close();
        }

        cursor = es.query("storableTypeName = ?")
            .with(typeName).orderBy("-matchedGeneration").fetchSlice(0, 1L);

        try {
            if (cursor.hasNext()) {
                highestGen = Math.max(highestGen, cursor.next().getMatchedGeneration());
            }
        } finally {
            cursor.close();
        }

        return highestGen + 1;
    }

    /**
     * Creates a long hash code that attempts to mix in all relevant layout
     * elements.
     */
    private long mixInHash(long hash, StorableInfo<?> info, LayoutOptions options, int multiplier)
    {
        hash = mixInHash(hash, info.getStorableType().getName(), multiplier);
        hash = mixInHash(hash, options, multiplier);

        for (StorableProperty<?> property : info.getAllProperties().values()) {
            if (!property.isJoin()) {
                hash = mixInHash(hash, property, multiplier);
            }
        }

        return hash;
    }

    /**
     * Creates a long hash code that attempts to mix in all relevant layout
     * elements.
     */
    private long mixInHash(long hash, StorableProperty<?> property, int multiplier) {
        hash = mixInHash(hash, property.getName(), multiplier);
        hash = mixInHash(hash, property.getType().getName(), multiplier);
        hash = hash * multiplier + (property.isNullable() ? 1 : 2);
        hash = hash * multiplier + (property.isPrimaryKeyMember() ? 1 : 2);

        // Keep this in for compatibility with prior versions of hash code.
        hash = hash * multiplier + 1;

        if (property.getAdapter() != null) {
            // Keep this in for compatibility with prior versions of hash code.
            hash += 1;

            StorablePropertyAdapter adapter = property.getAdapter();
            StorablePropertyAnnotation annotation = adapter.getAnnotation();

            hash = mixInHash(hash, annotation.getAnnotationType().getName(), multiplier);

            // Annotation may contain parameters which affect how property
            // value is stored. So mix that in too.
            Annotation ann = annotation.getAnnotation();
            if (ann != null) {
                hash = hash * multiplier + annHashCode(ann);
            }
        }

        return hash;
    }

    private long mixInHash(long hash, CharSequence value, int multiplier) {
        for (int i=value.length(); --i>=0; ) {
            hash = hash * multiplier + value.charAt(i);
        }
        return hash;
    }

    private long mixInHash(long hash, LayoutOptions options, int multiplier) {
        if (options != null) {
            byte[] data = options.encode();
            if (data != null) {
                for (int b : data) {
                    hash = hash * multiplier + (b & 0xff);
                }
            }
        }
        return hash;
    }

    /**
     * Returns an annotation hash code using a algorithm similar to the
     * default. The difference is in the handling of class and enum values. The
     * name is chosen for the hash code component instead of the instance
     * because it is stable between invocations of the JVM.
     */
    private static int annHashCode(Annotation ann) {
        int hash = 0;

        Method[] methods = ann.getClass().getDeclaredMethods();
        for (Method m : methods) {
            if (m.getReturnType() == null || m.getReturnType() == void.class) {
                continue;
            }
            if (m.getParameterTypes().length != 0) {
                continue;
            }

            String name = m.getName();
            if (name.equals("hashCode") ||
                name.equals("toString") ||
                name.equals("annotationType"))
            {
                continue;
            }

            Object value;
            try {
                value = m.invoke(ann);
            } catch (InvocationTargetException e) {
                continue;
            } catch (IllegalAccessException e) {
                continue;
            }

            hash += (127 * name.hashCode()) ^ annValueHashCode(value);
        }

        return hash;
    }

    private static int annValueHashCode(Object value) {
        Class type = value.getClass();
        if (!type.isArray()) {
            if (value instanceof String || type.isPrimitive()) {
                return value.hashCode();
            } else if (value instanceof Class) {
                // Use name for stable hash code.
                return ((Class) value).getName().hashCode();
            } else if (value instanceof Enum) {
                // Use name for stable hash code.
                return ((Enum) value).name().hashCode();
            } else if (value instanceof Annotation) {
                return annHashCode((Annotation) value);
            } else {
                return value.hashCode();
            }
        } else if (type == byte[].class) {
            return Arrays.hashCode((byte[]) value);
        } else if (type == char[].class) {
            return Arrays.hashCode((char[]) value);
        } else if (type == double[].class) {
            return Arrays.hashCode((double[]) value);
        } else if (type == float[].class) {
            return Arrays.hashCode((float[]) value);
        } else if (type == int[].class) {
            return Arrays.hashCode((int[]) value);
        } else if (type == long[].class) {
            return Arrays.hashCode((long[]) value);
        } else if (type == short[].class) {
            return Arrays.hashCode((short[]) value);
        } else if (type == boolean[].class) {
            return Arrays.hashCode((boolean[]) value);
        } else {
            return Arrays.hashCode((Object[]) value);
        }
    }
}