1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.graph;
14 import java.util.Collection;
15 import java.util.concurrent.atomic.AtomicInteger;
17 import org.simantics.db.common.utils.Logger;
18 import org.simantics.db.exception.RuntimeDatabaseException;
19 import org.simantics.db.impl.query.CacheEntry;
20 import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;
22 final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrier {
24 private static final long serialVersionUID = 4724463372850048672L;
26 static final int WAIT_TIME = 600;
28 public static final boolean PRINT = false;
30 final public AsyncBarrierImpl caller;
32 public AsyncBarrierImpl(AsyncBarrierImpl caller, CacheEntry<?> entry) {
35 if (BarrierTracing.BOOKKEEPING) {
36 BarrierTracing.trace(this, entry);
43 if(BarrierTracing.BOOKKEEPING) {
44 BarrierTracing.inc(this);
51 void inc(Object id, String info) {
54 System.err.println("inc barrier[" + get() + "] " + this);
55 StackTraceElement[] elems = new Exception().getStackTrace();
56 for(int i=0;i<4;i++) System.err.println(elems[i]);
59 if (incrementAndGet() == 1) {
61 if(BarrierTracing.BOOKKEEPING) {
62 caller.inc(this, "Child");
64 caller.inc(null, null);
74 System.err.println("dec barrier[" + get() + "] " + this);
75 StackTraceElement[] elems = new Exception().getStackTrace();
76 for(int i=0;i<3;i++) System.err.println(elems[i]);
79 int count = decrementAndGet();
81 if(BarrierTracing.BOOKKEEPING) {
82 BarrierTracing.dec(this, count);
90 Logger.defaultLogError(
91 "Database request processing error. The application code has performed illegal actions (probably called multiple times the execute or exception method of a single result request.",
99 public static String report(AsyncBarrierImpl barrier) {
100 CacheEntry<?> e = BarrierTracing.entryMap.get(barrier);
101 if(e != null) return e.toString();
102 else return "Barrier@" + System.identityHashCode(barrier);
105 public static void printReverse(AsyncBarrierImpl barrier, int indent) {
107 if (barrier.get() == 0)
109 for (int i = 0; i < indent; i++)
110 System.err.print(" ");
111 System.err.println("[" + barrier.get() + " requests]: " + report(barrier));
112 // if (BOOKKEEPING) {
113 // Debugger debugger = debuggerMap.get(barrier);
114 // debugger.toErr(indent + 2);
117 Collection<AsyncBarrierImpl> children = BarrierTracing.reverseLookup.get(barrier);
118 if (children != null) {
119 for (AsyncBarrierImpl child : children)
120 printReverse(child, indent + 2);
125 public void waitBarrier(Object request, ReadGraphImpl impl) {
133 boolean executed = impl.performPending();
134 if(executed) waitCount = 0;
137 if(waitCount > 100) Thread.yield();
138 if(waitCount > 1000) {
141 } catch (InterruptedException e) {
145 if(waitCount > WAIT_TIME*1000) {
147 System.err.println("AsyncBarrierImpl.waitBarrier("
149 + ") is taking long to execute, so far "
150 + (waitCount / 1000) + " s.");
152 if (BarrierTracing.BOOKKEEPING) {
153 synchronized (BarrierTracing.reverseLookup) {
154 printReverse(this, 0);
158 // if(Development.DEVELOPMENT) {
159 // impl.processor.threadLocks[0].lock();
160 // System.err.println("-queues=" + impl.processor.queues[0].size());
161 // impl.processor.threadLocks[0].unlock();
162 // System.err.println("-own=" + impl.processor.ownTasks[0].size());
163 // System.err.println("-ownSync=" + impl.processor.ownSyncTasks[0].size());
164 // for(SessionTask task : impl.processor.ownSyncTasks[0]) {
165 // System.err.println("--" + task);
169 throw new RuntimeDatabaseException("Request timed out.");
180 public void restart() {
182 if(BarrierTracing.BOOKKEEPING) {
183 BarrierTracing.restart(this);
187 public void assertReady() {
190 throw new AssertionError("Barrier was not finished (pending="
194 public void report() {
195 // System.out.println("Barrier log:");
196 // for(Map.Entry<String, Integer> entry : sources.entrySet()) {
197 // System.out.println(entry.getKey() + " " + entry.getValue());
199 // System.out.println("SyncIntProcedure log:");
200 // for(Map.Entry<String, Integer> entry :
201 // SyncIntProcedure.counters.entrySet()) {
202 // System.out.println(entry.getKey() + " " + entry.getValue());
207 public String toString() {
209 // return "AsyncBarrierImpl@" + System.identityHashCode(this)
210 // + " - counter = " + get() + " - caller = " + caller;