summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/layout/LayoutSync.java
blob: ba03be3bf036dd67e31deabd10f10524af2f2bd7 (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
/*
 * Copyright 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.util.Arrays;

import org.apache.commons.logging.LogFactory;

import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.Query;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.Transaction;

import com.amazon.carbonado.cursor.FetchAheadCursor;
import com.amazon.carbonado.cursor.FilteredCursor;

/**
 * Synchronizes layout metadata between two repositories. Both source and
 * destination might be updated.
 *
 * @author Brian S O'Neill
 */
public class LayoutSync {
    private final Repository mSource;
    private final Repository mDestination;

    public LayoutSync(Repository source, Repository destination) {
        mSource = source;
        mDestination = destination;
    }

    /**
     * @return true if any changes to source were made
     */
    public boolean run() throws RepositoryException {
        if (doRun()) {
            // Second pass.
            doRun();
            return true;
        }
        return false;
    }

    /**
     * @return true if a second pass should be run
     */
    private boolean doRun() throws RepositoryException {
        Storage<StoredLayout> srcLayoutStorage = mSource.storageFor(StoredLayout.class);
        Storage<StoredLayout> dstLayoutStorage = mDestination.storageFor(StoredLayout.class);

        Cursor<StoredLayout> srcLayouts = srcLayoutStorage.query().orderBy("layoutID").fetch();
        try {
            Cursor<StoredLayout> dstLayouts = dstLayoutStorage.query().orderBy("layoutID").fetch();
            try {
                return doRun(srcLayouts, dstLayouts);
            } finally {
                dstLayouts.close();
            }
        } finally {
            srcLayouts.close();
        }
    }

    /**
     * @return true if another pass should be run
     */
    private boolean doRun(Cursor<StoredLayout> srcLayouts, Cursor<StoredLayout> dstLayouts) 
        throws RepositoryException
    {
        // Fetch ahead to prevent BDB cursor deadlocks.
        srcLayouts = new FetchAheadCursor<StoredLayout>(srcLayouts, 1000);
        dstLayouts = new FetchAheadCursor<StoredLayout>(dstLayouts, 1000);

        boolean doAgain = false;

        StoredLayout src = null;
        StoredLayout dst = null;

        while (true) {
            if (src == null) {
                if (!srcLayouts.hasNext()) {
                    return doAgain;
                }
                src = srcLayouts.next();
            }

            if (dst == null && dstLayouts.hasNext()) {
                dst = dstLayouts.next();
            }

            if (dst == null || src.getLayoutID() < dst.getLayoutID()) {
                // Insert missing layout. If a generation conflict, do over in
                // second pass after all source generations have been inserted.
                doAgain |= !tryInsert(src);
                src = null;
                continue;
            }

            if (src.getLayoutID() > dst.getLayoutID()) {
                // Skip layout which only exists in destination.
                dst = null;
                continue;
            }

            if (src.getGeneration() != dst.getGeneration()) {
                // Same layouts, but with different generation. Create a new
                // non-conflicting generation to replace both.
                LogFactory.getLog(LayoutSync.class).error
                    ("Unable to synchronize layouts: " + src + " != " + dst);
                /*
                createNewGen(src, dst);
                doAgain = true;
                */
            }

            src = null;
            dst = null;
        }
    }

    /**
     * @return false if generation conflict
     */
    private boolean tryInsert(StoredLayout src) throws RepositoryException {
        Storage<StoredLayout> dstLayoutStorage = mDestination.storageFor(StoredLayout.class);

        Storage<StoredLayoutProperty> srcPropStorage =
            mSource.storageFor(StoredLayoutProperty.class);
        Storage<StoredLayoutProperty> dstPropStorage =
            mDestination.storageFor(StoredLayoutProperty.class);

        Transaction txn = mDestination.enterTransaction();
        try {
            txn.setForUpdate(true);
            StoredLayout dst = dstLayoutStorage.prepare();
            src.copyAllProperties(dst);
            if (!dst.tryInsert()) {
                return false;
            }

            Cursor<StoredLayoutProperty> srcProps =
                srcPropStorage.query("layoutID = ?").with(src.getLayoutID()).fetch();
            while (srcProps.hasNext()) {
                StoredLayoutProperty srcProp = srcProps.next();
                StoredLayoutProperty dstProp = dstPropStorage.prepare();
                srcProp.copyAllProperties(dstProp);
                if (!dstProp.tryInsert()) {
                    dstProp.tryDelete();
                    dstProp.insert();
                }
            }

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

        return true;
    }

    /*
    private void createNewGen(StoredLayout src, StoredLayout dst) throws RepositoryException {
        long layoutID = src.getLayoutID();
        if (layoutID != dst.getLayoutID()) {
            throw new AssertionError();
        }

        String storableTypeName = src.getStorableTypeName();
        if (!storableTypeName.equals(dst.getStorableTypeName())) {
            // Assume that there's never any hashcode collision.
            throw new AssertionError();
        }

        Transaction dstTxn = mDestination.enterTransaction();
        try {
            dstTxn.setForUpdate(true);

            Transaction srcTxn = mSource.enterTransaction();
            try {
                srcTxn.setForUpdate(true);

                // New layout generation which is not used by src or dst.
                int newGen = nextGen(storableTypeName);

                int oldSrcGen = src.getGeneration();
                int oldDstGen = dst.getGeneration();

                long now = System.currentTimeMillis();

                // Remap source layout to new generation.
                doCreateNewGen(now, mSource, layoutID, oldSrcGen, newGen);

                // Remap destination layout to new generation.
                doCreateNewGen(now, mDestination, layoutID, oldDstGen, newGen);

                // Check if source has a layout generation matching the
                // conflicting destination layout. It cannot be used anymore.
                Cursor<StoredLayout> srcLayouts =
                    findLayouts(mSource, storableTypeName, oldDstGen);
                while (srcLayouts.hasNext()) {
                    src = srcLayouts.next();
                    newGen = nextGen(storableTypeName);
                    doCreateNewGen(now, mSource, src.getLayoutID(), oldDstGen, newGen);
                }

                // Check if destination has a layout generation matching the
                // conflicting source layout. It cannot be used anymore.
                Cursor<StoredLayout> dstLayouts =
                    findLayouts(mDestination, storableTypeName, oldSrcGen);
                while (dstLayouts.hasNext()) {
                    dst = dstLayouts.next();
                    newGen = nextGen(storableTypeName);
                    doCreateNewGen(now, mDestination, dst.getLayoutID(), oldSrcGen, newGen);
                }

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

            dstTxn.commit();
        } finally {
            dstTxn.exit();
        }
    }
    */

    /*
    private void doCreateNewGen(long now, Repository repo, long layoutID, int oldGen, int newGen)
        throws RepositoryException
    {
        Storage<StoredLayout> layoutStorage =
            repo.storageFor(StoredLayout.class);
        Storage<StoredLayoutEquivalence> equivStorage =
            repo.storageFor(StoredLayoutEquivalence.class);

        StoredLayout newLayout = layoutStorage.prepare();
        newLayout.setLayoutID(layoutID);
        newLayout.load();
        Layout.fillInCreationInfo(newLayout);
        newLayout.setGeneration(newGen);
        // Consistent timestamp for all records.
        newLayout.setCreationTimestamp(now);
        newLayout.update();

        StoredLayoutEquivalence equiv = equivStorage.prepare();
        equiv.setStorableTypeName(newLayout.getStorableTypeName());
        equiv.setGeneration(oldGen);
        equiv.setMatchedGeneration(newGen);

        equiv.insert();
    }

    private int nextGen(String storableTypeName) throws RepositoryException {
        return Math.max(nextGen(mSource, storableTypeName),
                        nextGen(mDestination, storableTypeName));
    }

    private int nextGen(Repository repo, String storableTypeName)
        throws RepositoryException
    {
        return LayoutFactory.nextGeneration(repo, storableTypeName);
    }

    static Cursor<StoredLayout> findLayouts(Repository repo,
                                            String storableTypeName, int generation)
        throws RepositoryException
    {
        return Layout.findLayouts(repo, storableTypeName, generation);
    }
    */
}