]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/BarrierTracing.java
Wrong graph was used when performing async query from session
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / BarrierTracing.java
1 package org.simantics.db.impl.graph;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.simantics.db.impl.BlockingAsyncProcedure;
9 import org.simantics.db.impl.query.CacheEntry;
10 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
11
12 public class BarrierTracing {
13
14     public static final boolean BOOKKEEPING = false;
15     static final boolean RESTART_GUARD = BOOKKEEPING && false;
16
17     public static Map<SessionTask,Exception> tasks = new HashMap<>();
18     public static final HashMap<AsyncBarrierImpl, Collection<AsyncBarrierImpl>> reverseLookup = new HashMap<>();
19     public static final HashMap<AsyncBarrierImpl, Debugger> debuggerMap = new HashMap<>();
20     public static final HashMap<AsyncBarrierImpl, CacheEntry<?>> entryMap = new HashMap<>();
21     public static final HashMap<AsyncBarrierImpl, Throwable> restartMap = new HashMap<>();
22     public static final HashMap<AsyncBarrierImpl, Throwable> startMap = new HashMap<>();
23     public static final HashMap<BlockingAsyncProcedure, Throwable> baps = new HashMap<>();
24
25     synchronized public static void registerBAP(BlockingAsyncProcedure bap) {
26         baps.put(bap, new Exception());
27     }
28     
29     synchronized public static void unregisterBAP(BlockingAsyncProcedure bap) {
30         baps.remove(bap);
31     }
32
33     synchronized public static void printBAPS() {
34         for(BlockingAsyncProcedure bap : baps.keySet()) {
35             bap.print();
36             Throwable t = baps.get(bap);
37             if(t != null)
38                 t.printStackTrace();
39         }
40     }
41     
42     public static void trace(AsyncBarrierImpl barrier, CacheEntry<?> entry) {
43
44         if (RESTART_GUARD) {
45             synchronized (startMap) {
46                 startMap.put(barrier, new Exception());
47             }
48         }
49         synchronized (entryMap) {
50             entryMap.put(barrier, entry);
51         }
52         synchronized (debuggerMap) {
53             debuggerMap.put(barrier, new Debugger());
54         }
55         synchronized (reverseLookup) {
56             Collection<AsyncBarrierImpl> barriers = reverseLookup
57                     .get(barrier.caller);
58             if (barriers == null) {
59                 barriers = new ArrayList<AsyncBarrierImpl>();
60                 reverseLookup.put(barrier.caller, barriers);
61             }
62             barriers.add(barrier);
63         }
64
65     }
66
67     public static void inc(AsyncBarrierImpl barrier) {
68
69         barrier.inc(barrier, new Exception().getStackTrace()[2].toString());
70
71         if (RESTART_GUARD)
72             if(restartMap.containsKey(barrier)) {
73                 startMap.get(barrier).printStackTrace();
74                 restartMap.get(barrier).printStackTrace();
75                 new Exception().printStackTrace();
76                 throw new IllegalStateException("Unplanned restart");
77             }
78
79
80     }
81
82     public static void restart(AsyncBarrierImpl barrier) {
83         if (RESTART_GUARD)
84             BarrierTracing.restartMap.remove(barrier);
85         if (BOOKKEEPING)
86             BarrierTracing.debuggerMap.put(barrier, new Debugger());
87     }
88
89     public static void dec(AsyncBarrierImpl barrier, int count) {
90         if (count == 0) {
91             if (RESTART_GUARD) {
92                 restartMap.put(barrier, new Exception());
93             }
94             debuggerMap.remove(barrier);
95         }
96         else if (count < 0) {
97             BarrierTracing.startMap.get(barrier).printStackTrace();
98             BarrierTracing.restartMap.get(barrier).printStackTrace();
99             new Exception().printStackTrace();
100         }
101     }
102
103     public static class Debugger {
104
105         public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
106
107         public synchronized void inc(AsyncBarrierImpl id, String info) {
108             if (id == null)
109                 return;
110             String exist = infos.get(id);
111             if (exist != null)
112                 throw new IllegalStateException("Already existing info " + id + " " + info);
113             infos.put(id, exist);
114         }
115
116         public synchronized void dec(AsyncBarrierImpl id) {
117             if (id == null)
118                 return;
119             String exist = infos.get(id);
120             if (exist == null) {
121                 System.err.println("No data for " + id);
122             } else {
123                 infos.remove(id);
124             }
125         }
126
127         @Override
128         public synchronized String toString() {
129             StringBuilder b = new StringBuilder();
130             for (String s : infos.values()) {
131                 b.append("info " + s + "\r\n");
132             }
133             return b.toString();
134         }
135
136         public boolean isEmpty() {
137             return infos.isEmpty();
138         }
139
140     }
141
142 }