]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryThread.java
DB request scheduling scheme fails with district diagrams
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryThread.java
1 package org.simantics.db.impl.query;
2
3 import java.util.ArrayList;
4 import java.util.concurrent.Semaphore;
5 import java.util.concurrent.atomic.AtomicInteger;
6
7 import org.simantics.db.Session;
8 import org.simantics.db.common.SessionThread;
9 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
10 import org.simantics.db.impl.query.QueryProcessor.ThreadState;
11 import org.slf4j.LoggerFactory;
12
13 class QueryThread extends Thread implements SessionThread {
14
15         private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(QueryThread.class);
16
17         boolean disposed = false;
18         private Semaphore exited = new Semaphore(0);
19
20         final int index;
21
22         private Session session;
23         private QuerySupport querySupport;
24         private final QueryProcessor processor;
25         private final Semaphore requests;
26
27         final private ArrayList<SessionTask> tasks = new ArrayList<SessionTask>();
28 //      final private ArrayList<SessionTask> own;
29 //      final private ArrayList<SessionTask> ownSync;
30 //      final private ArrayList<SessionTask> queue;
31 //      final private ReentrantLock lock;
32 //      final private Condition condition;
33         final private Object querySupportLock;
34         final private int THREADS;
35         final private AtomicInteger sleepers;
36         final private ThreadState[] threadStates;
37 //      final private ArrayList<SessionTask>[] delayQueues;
38 //      final private QueryThread[] executors;
39 //      final private ReentrantLock[] threadLocks;
40 //      final private ArrayList<SessionTask>[] queues;
41 //      final private ArrayList<SessionTask>[] ownSyncTasks;
42
43         public QueryThread(Session session, QueryProcessor processor, int index, String name) {
44                 super(QueryProcessor.QueryThreadGroup, null, name);
45                 this.session = session;
46                 this.processor = processor;
47                 this.index = index;
48 //              own = processor.ownTasks[index];
49 //              ownSync = processor.ownSyncTasks[index];
50 //              queue = processor.queues[index];
51 //              lock = processor.threadLocks[index];
52 //              condition = processor.threadConditions[index];
53                 querySupportLock = processor.querySupportLock;
54                 THREADS = processor.THREADS;
55                 sleepers = processor.sleepers;
56                 querySupport = processor.querySupport;
57                 threadStates = processor.threadStates;
58                 requests = processor.requests;
59 //              delayQueues = processor.delayQueues;
60 //              executors = processor.executors;
61 //              threadLocks = processor.threadLocks;
62 //              queues = processor.queues;
63 //              ownSyncTasks = processor.ownSyncTasks;
64         }
65
66         synchronized void dispose() {
67
68 //              System.err.println("qt dispose");
69
70                 disposed = true;
71 //              lock.lock();
72 //              condition.signalAll();
73 //              lock.unlock();
74                 
75                 try {
76                         exited.acquire();
77                 } catch (InterruptedException e) {
78                         LOGGER.error("dispose was interrupted", e);
79                 }
80
81                 session = null;
82                 querySupport = null;
83
84 //              System.err.println("qt disposed");
85
86         }
87
88         boolean isDisposed() { 
89
90                 return disposed;
91
92         }
93
94         public Session getSession() {
95
96                 return session;
97
98         }
99
100         private boolean pumpTask() {
101             return processor.scheduling.pumpTask(tasks);
102         }
103         
104         ArrayList<SessionTask> newTasks(boolean doWait, ArrayList<SessionTask> tasks) {
105
106                 try {
107
108                         while(true) {
109
110                                 // Return tasks if some were found
111                                 if(!tasks.isEmpty()) return tasks;
112                                 
113                                 if(!doWait) return null;
114
115                                 synchronized (querySupportLock) {
116
117                                         if(pumpTask())
118                                                 return tasks;
119
120 //                                      lock.lock();
121
122                                         // We are the last one awake
123                                         if(sleepers.incrementAndGet() == THREADS) {
124
125                                                 // Do not indicate sleeping yet
126                                                 sleepers.decrementAndGet();
127                                                 // Ceased can create new own tasks
128                                                 if(querySupport == null) System.err.println("null qs");
129                                                 querySupport.ceased(index);
130
131                                                 if(pumpTask()) {
132 //                                                      lock.unlock();
133                                                         return tasks;
134                                                 }
135
136                                                 // OK, now we are going to sleep
137                                                 sleepers.incrementAndGet();
138
139                                         }
140
141                                 }
142
143                                 // Nope nothing. Sleep & wait
144                                 // Whoever releases this calls sleepers.decrementAndGet()
145
146                                 // We are done
147                                 if(isDisposed()) {
148                                         threadStates[index] = ThreadState.DISPOSED;
149 //                                      lock.unlock();
150                                         return null;
151                                 }
152
153                                 
154                                 threadStates[index] = ThreadState.SLEEP;
155                                 
156                                 requests.acquire();
157
158                                 sleepers.decrementAndGet();
159
160                                 // We are done
161                                 if(isDisposed()) {
162                                         threadStates[index] = ThreadState.DISPOSED;
163                                         //lock.unlock();
164                                         return null;
165                                 }
166
167                                 threadStates[index] = ThreadState.RUN;
168
169                                 //lock.unlock();
170
171                         }
172
173                 } catch (InterruptedException e) {
174
175                         LOGGER.error("Query handling (newTasks) was interrupted", e);
176                         throw new RuntimeException("Querying was interrupted.", e);
177
178                 }
179
180         }
181
182         public boolean runSynchronized() {
183
184                 boolean didExecute = false;
185
186 //              for(int performer=0;performer<THREADS;performer++) {
187 //                      if(!delayQueues[index * THREADS + performer].isEmpty()) {
188 //                              synchronized(executors[performer]) {
189 //                                      threadLocks[performer].lock();
190 //                                      queues[performer].addAll(delayQueues[index * THREADS + performer]);
191 //                                      delayQueues[index * THREADS + performer].clear();
192 //                                      executors[performer].notify();
193 //                                      threadLocks[performer].unlock();
194 //                              }
195 //                      }
196 //              }
197
198                 if(tasks.isEmpty()) {
199                         ArrayList<SessionTask> finished = newTasks(false, tasks);
200                         if(finished == null) return didExecute; 
201                 }
202
203                 while(!tasks.isEmpty()) {
204
205                         SessionTask task = tasks.remove(tasks.size() - 1);
206
207 //                      if(task.syncCaller == index) {
208 //                              ownSyncTasks[index].add(task);
209 //                      } else {
210                                 task.run(index);
211 //                              System.err.println("QT(s) " + index + " runs " + task);
212                                 didExecute = true;
213 //                      }
214
215                 }
216
217                 return didExecute;
218
219         }
220
221         @Override
222         public void run() {
223
224                 QuerySupport support = this.querySupport;
225
226                 try {
227
228                         while (true) {
229
230                                 boolean finished = newTasks(true, tasks) == null;
231                                 if(finished) {
232                                         return;
233                                 }
234                                 
235                                 while(!tasks.isEmpty()) {
236
237                                         SessionTask task = tasks.remove(tasks.size()-1);
238                                         task.run(0);
239
240                                 }
241
242                         }
243
244                 } catch (Throwable t) {
245
246                         LOGGER.error("FATAL BUG: QueryThread task processing caused unexpected exception.", t);
247                         support.exit(t);
248
249                 } finally {
250                         
251                         exited.release();
252
253                 }
254
255         }
256
257 }