| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.impl; |
| 9 |
|
| 10 |
/** |
| 11 |
* Thrown and handled internally when metadata must be refreshed on a Replica. |
| 12 |
* |
| 13 |
* There are several scenarios for refreshing DPL metadata: |
| 14 |
* |
| 15 |
* 1. Read entity record on Replica that has stale in-memory metadata. |
| 16 |
* |
| 17 |
* When an entity record that references new metadata (for example, a never |
| 18 |
* before encountered class) is written on the Master, the metadata is |
| 19 |
* written and replicated prior to writing and replicating the entity |
| 20 |
* record. However, the Replica's in-memory cache of metadata (the |
| 21 |
* PersistCatalog object) is not synchronously updated when the metadata is |
| 22 |
* replicated. When the entity record that references the newly replicated |
| 23 |
* metadata is read on the Replica, the DPL must refresh the in-memory |
| 24 |
* metadata cache by reading it from the catalog database. |
| 25 |
* |
| 26 |
* Note that this scenario occurs even without class evolution/upgrade, for |
| 27 |
* two reasons. First, the Master does not write all metadata at once; |
| 28 |
* metadata is added to the catalog incrementally as new persistent classes |
| 29 |
* are encountered. Second, even when all metadata is written intially by |
| 30 |
* the Master, the Replica may read the catalog before the Master has |
| 31 |
* completed metadata updates. |
| 32 |
* |
| 33 |
* Implementation: |
| 34 |
* + PersistCatalog.getFormat(int) throws RefreshException when the given |
| 35 |
* format ID is not in the in-memory catalog. |
| 36 |
* + The binding method that is calling getFormat catches RefreshException, |
| 37 |
* calls RefreshException.refresh to read the updated metadata, and |
| 38 |
* retries the operation. |
| 39 |
* |
| 40 |
* Tests: |
| 41 |
* c.s.je.rep.persist.test.UpgradeTest.testIncrementalMetadataChanges |
| 42 |
* c.s.je.rep.persist.test.UpgradeTest.testUpgrade |
| 43 |
* |
| 44 |
* 2. Write entity record on Master that is in Replica Upgrade Mode. |
| 45 |
* |
| 46 |
* When a Replica is upgraded with new persistent classes (see |
| 47 |
* evolve/package.html doc) the DPL will evolve the existing metadata and |
| 48 |
* update the in-memory metadata cache (PersistCatalog object), but will not |
| 49 |
* write the metadata; instead, it will enter Replica Upgrade Mode. In this |
| 50 |
* mode, the Replica will convert old format data to new format data as |
| 51 |
* records are read, using the in-memory evolved metadata. This allows the |
| 52 |
* Replica application to perform entity read operations with the new |
| 53 |
* persistent classes, during the upgrade process. |
| 54 |
* |
| 55 |
* When this Replica is elected Master, the application will begin writing |
| 56 |
* entity records. Note that the new metadata has not yet been written to |
| 57 |
* the catalog database. In Replica Upgrade Mode, the current in-memory |
| 58 |
* metadata cannot be written to disk, since the catalog database may be |
| 59 |
* stale, i.e., it have been updated by the Master after the Replica's |
| 60 |
* in-memory metadata was evolved. Therefore, before the first entity |
| 61 |
* record is written, the newly elected Master must read the latest |
| 62 |
* metadata, perform evolution of the metadata again, write the metadata, |
| 63 |
* and then write the entity record. |
| 64 |
* |
| 65 |
* Implementation: |
| 66 |
* + The catalog enters Replica Upgrade Mode when a new or evolved format is |
| 67 |
* added to the catalog, and a ReplicaWriteException occurs when |
| 68 |
* attempting to write the metadata. Replica Upgrade Mode is defined as |
| 69 |
* when the number of in-memory formats is greater than the number of |
| 70 |
* stored formats. |
| 71 |
* + Before an entity record is written, PersistEntityBinding.objectToData |
| 72 |
* is called to convert the entity object to record data. |
| 73 |
* + objectToData calls PersistCatalog.checkWriteInReplicaUpgradeMode, |
| 74 |
* which throws RefreshExeption in Replica Upgrade Mode. |
| 75 |
* + objectToData catches RefreshException, calls RefreshException.refresh |
| 76 |
* to read the updated metadata, and retries the operation. |
| 77 |
* |
| 78 |
* Tests: |
| 79 |
* c.s.je.rep.persist.test.UpgradeTest.testElectedMasterWithStaleMetadata |
| 80 |
* c.s.je.rep.persist.test.UpgradeTest.testRefreshAfterFirstWrite |
| 81 |
* c.s.je.rep.persist.test.UpgradeTest.testUpgrade |
| 82 |
* |
| 83 |
* 3. Write metadata on Master that is in Replica Upgrade Mode. |
| 84 |
* |
| 85 |
* This third scenario is more unusual than the first two. It occurs when |
| 86 |
* a Replica with stale metadata is elected Master, but is not in Replica |
| 87 |
* Upgrade Mode. The new Master must refresh metadata before writing |
| 88 |
* metadata. See the test case for more information. |
| 89 |
* |
| 90 |
* Implementation: |
| 91 |
* + On a Master with stale metadata, the application tries to write an |
| 92 |
* entity record that refers to a class that has not been encountered |
| 93 |
* before. |
| 94 |
* + Before the entity record is written, PersistEntityBinding.objectToData |
| 95 |
* is called to convert the entity object to record data. |
| 96 |
* + objectToData calls PersistCatalog.addNewFormat during serialization, |
| 97 |
* which attempts to write metadata by by calling writeDataCheckStale. |
| 98 |
* + writeDataCheckStale reads the existing metadata and detects that it |
| 99 |
* has changed since metadata was last read, and throws RefreshExeption. |
| 100 |
* + objectToData catches RefreshException, calls RefreshException.refresh |
| 101 |
* to read the updated metadata, and retries the operation. |
| 102 |
* |
| 103 |
* Tests: |
| 104 |
* c.s.je.rep.persist.test.UpgradeTest.testRefreshBeforeWrite |
| 105 |
*/ |
| 106 |
public class RefreshException extends Exception { |
| 107 |
|
| 108 |
private final Store store; |
| 109 |
private final PersistCatalog catalog; |
| 110 |
private final int formatId; |
| 111 |
|
| 112 |
RefreshException(final Store store, |
| 113 |
final PersistCatalog catalog, |
| 114 |
final int formatId) { |
| 115 |
this.store = store; |
| 116 |
this.catalog = catalog; |
| 117 |
this.formatId = formatId; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* This method must be called to handle this exception in the binding |
| 122 |
* methods, after the stack has unwound. The binding methods should retry |
| 123 |
* the operation once after calling this method. If the operation fails |
| 124 |
* again, then corruption rather than stale metadata is the likely cause |
| 125 |
* of the problem, and an exception will be thrown to that effect. |
| 126 |
* [#16655] |
| 127 |
*/ |
| 128 |
public PersistCatalog refresh() { |
| 129 |
return store.refresh(catalog, formatId, this); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public String getMessage() { |
| 134 |
return "formatId=" + formatId; |
| 135 |
} |
| 136 |
} |