summaryrefslogtreecommitdiff
path: root/java/src/Freeze/TransactionalEvictorContext.java
blob: 2de368abeb0d90b86a6ce05a369d96175b55bc55 (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
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package Freeze;

//
// A Freeze transactional evictor context holds a Berkeley DB transaction
//  and a number of servants loaded using this transaction.
//

class TransactionalEvictorContext implements Ice.DispatchInterceptorAsyncCallback, PostCompletionCallback
{
    public void 
    postCompletion(boolean committed, boolean deadlock)
    {    
        try
        {
            if(committed)
            {
                //
                // Remove updated & removed objects from cache
                //
                java.util.Iterator p = _invalidateList.iterator();
                while(p.hasNext())
                {
                    ToInvalidate ti = (ToInvalidate)p.next();
                    ti.invalidate();
                }
                _invalidateList.clear();
            }
        }
        finally
        {
            synchronized(this)
            {
                if(_tx != null)
                {
                    if(deadlock)
                    {
                        _deadlockExceptionDetected = true;
                    }
                    _tx = null;
                    notifyAll();
                }
            }
        }
    }

    public boolean
    response(boolean ok)
    {
        if(Thread.currentThread().equals(_owner))
        {
            if(!ok)
            {
                _userExceptionDetected = true;
            }

            return true;
        }
        else
        {
            synchronized(this)
            {
                if(_deadlockExceptionDetected)
                {
                    return false;
                }
                if(_tx == null)
                {
                    return true;
                }
                try
                {
                    wait();
                }
                catch(InterruptedException ex)
                {
                }
                return !_deadlockExceptionDetected;
            }
        }
    }

    public boolean
    exception(java.lang.Exception ex)
    {
        if(ex instanceof DeadlockException && Thread.currentThread().equals(_owner))
        {
            _deadlockException = (DeadlockException)ex;
            return false;
        }

        if(ex instanceof TransactionalEvictorDeadlockException && Thread.currentThread().equals(_owner))
        {
            _nestedCallDeadlockException = (TransactionalEvictorDeadlockException)ex;
            return false;
        }

        return true;
    }

   
    class ServantHolder
    {
        ServantHolder(Ice.Current current, ObjectStore store)
        {
            _current = current;
            _store = store;
            
            ServantHolder sh = findServantHolder(_current.id, _store);
            if(sh != null)
            {
                if(!sh._removed)
                {
                    _rec = sh._rec;
                    _readOnly = sh._readOnly;

                    if(_trace >= 3)
                    {
                        _communicator.getLogger().trace("Freeze.Evictor", "found \"" 
                                                        + _communicator.identityToString(_current.id) +"\" with facet \"" + _store.facet() 
                                                        + "\" in current context");
                    }
                }
            }
            else
            {
                //
                // Let's load this servant
                //
                _rec = store.load(current.id, _tx);
                if(_rec != null)
                {
                    if(_trace >= 3)
                    {
                        _communicator.getLogger().trace("Freeze.Evictor", "loaded \"" 
                                                        + _communicator.identityToString(_current.id) +"\" with facet \"" + _store.facet() 
                                                        + "\" into current context");
                    }

                    _stack.push(this);
                    _ownServant = true;
                }
            }
        }

        void 
        markReadWrite()
        {
            if(_ownServant)
            {
                _readOnly = false;
            }
            else
            {
                if(_readOnly)
                {
                    throw new DatabaseException("freeze:write operation called from freeze:read operation");
                }
            }
        }

        void 
        release()
        {
            if(_ownServant)
            {
                if(_tx != null)
                {
                    if(!_readOnly && !_removed)
                    {
                        EvictorI.updateStats(_rec.stats, IceInternal.Time.currentMonotonicTimeMillis());
                        _store.update(_current.id, _rec, _tx);

                        if(_trace >= 3)
                        {
                            _communicator.getLogger().trace("Freeze.Evictor", "updated \"" 
                                                            + _communicator.identityToString(_current.id) +"\" with facet \"" + _store.facet() 
                                                            + "\" within transaction");
                        }
                    }
                    
                    if(!_readOnly || _removed)
                    {
                        _invalidateList.add(new ToInvalidate(_current.id, _store));
                    }
                }
                _stack.pop();
            }
        }
        
        boolean
        matches(Ice.Identity ident, ObjectStore store)
        {
            return ident.equals(_current.id) && store == _store;
        }

        Ice.Object 
        servant() 
        {
            if(_rec == null)
            {
                return null;
            }
            else
            {
                return _rec.servant;
            }
        }

        void 
        removed()
        {
            _removed = true;
        }
     
        private boolean _ownServant = false;
        private boolean _removed = false;
        private boolean _readOnly = true;

        private final Ice.Current _current;
        private final ObjectStore _store;
        private ObjectRecord _rec;
    };


    TransactionalEvictorContext(SharedDbEnv dbEnv)
    {
        _communicator = dbEnv.getCommunicator();
        _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Evictor");
        _tx = (TransactionI)(new ConnectionI(dbEnv).beginTransaction());
        _owner = Thread.currentThread();
        _tx.adoptConnection();

        _tx.setPostCompletionCallback(this);
    }

    TransactionalEvictorContext(TransactionI tx, Ice.Communicator communicator)
    {
        _communicator = communicator;
        _trace = _communicator.getProperties().getPropertyAsInt("Freeze.Trace.Evictor");
        _tx = tx;
        _owner = Thread.currentThread();

        _tx.setPostCompletionCallback(this);
    }

    Ice.Object
    findServant(Ice.Identity ident, ObjectStore store)
    {
        ServantHolder sh = findServantHolder(ident, store);
        return sh != null ? sh.servant() : null;
    }

    void
    rollback()
    {
        if(_tx != null)
        {
            _tx.rollback();
        }
    }

    void
    commit()
    {
        if(_tx != null)
        {
            _tx.commit();
        }
    }


    void
    checkDeadlockException()
    {
        if(_deadlockException != null)
        {
            throw _deadlockException;
        }

        if(_nestedCallDeadlockException != null)
        {
            throw _nestedCallDeadlockException;
        }
    }

    boolean
    clearUserException()
    {
        //
        // No need to synchronize; _userExceptionDetected is only read/written by the 
        // dispatch thread
        //
        boolean result = _userExceptionDetected;
        _userExceptionDetected = false;
        return result;
    }
    
  
    TransactionI 
    transaction()
    {
        return _tx;
    }

    ServantHolder
    createServantHolder(Ice.Current current, ObjectStore store)
    {
        return new ServantHolder(current, store);
    }
    
    void
    deadlockException()
    {
        synchronized(this)
        {
            _deadlockExceptionDetected = true;
            notifyAll();
        }

        rollback();
    }
    
    Ice.Object
    servantRemoved(Ice.Identity ident, ObjectStore store)
    {
        if(_tx != null)
        {
            //
            // Lookup servant holder on stack
            //
            ServantHolder sh = findServantHolder(ident, store);
            if(sh != null)
            {
                sh.removed();
                return sh.servant();
            }
            else
            {
                _invalidateList.add(new ToInvalidate(ident, store));
                return null;
            }
        }
        return null;
    }
    
 
    protected void 
    finalize()
    {
        if(_tx != null)
        {
            _tx.getConnectionI().communicator().getLogger().warning
                ("Finalizing incomplete TransactionalEvictorContext on DbEnv '" +  _tx.getConnectionI().dbEnv().getEnvName() + "'");
        }
    }

    private ServantHolder
    findServantHolder(Ice.Identity ident, ObjectStore store)
    {
        java.util.Iterator p = _stack.iterator();
        while(p.hasNext())
        {
            ServantHolder sh = (ServantHolder)p.next();
            if(sh.matches(ident, store))
            {
                return sh;
            }
        }
        return null;
    }
    
    private static class ToInvalidate
    {
        ToInvalidate(Ice.Identity ident, ObjectStore store)
        {
            _ident = ident;
            _store = store;
        }

        void 
        invalidate()
        {
            ((TransactionalEvictorI)_store.evictor()).evict(_ident, _store);
        }

        private final Ice.Identity _ident;
        private final ObjectStore _store;
    }


    //
    // Stack of ServantHolder
    //
    private final java.util.Stack _stack = new java.util.Stack();
    
    //
    // List of objects to invalidate from the caches upon commit
    //
    private final java.util.List _invalidateList = new java.util.LinkedList();

    private TransactionI _tx;
    private final Thread _owner;

    private DeadlockException _deadlockException;
    private TransactionalEvictorDeadlockException _nestedCallDeadlockException;

    //
    // Protected by this
    //
    private boolean _deadlockExceptionDetected = false;

    
    //
    // Only updated by the dispatch thread
    //
    private boolean _userExceptionDetected = false;

    private final int _trace;
    private final Ice.Communicator _communicator;
}