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 public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrier {
24 private static final long serialVersionUID = 4724463372850048672L;
26 static final int WAIT_TIME = 60000;
28 public static final boolean PRINT = false;
30 final AsyncBarrierImpl caller;
32 private final Runnable callback;
34 private final boolean needsToBlock;
36 public AsyncBarrierImpl(AsyncBarrierImpl caller, CacheEntry<?> entry, Runnable callback, boolean needsToBlock) {
39 this.callback = callback;
40 this.needsToBlock = needsToBlock;
41 if (BarrierTracing.BOOKKEEPING) {
42 BarrierTracing.trace(this, entry);
46 public AsyncBarrier getBlockingBarrier() {
51 else return caller.getBlockingBarrier();
55 public boolean isBlocking() {
62 if(BarrierTracing.BOOKKEEPING) {
63 BarrierTracing.inc(this);
70 void inc(Object id, String info) {
73 System.err.println("inc barrier[" + get() + "] " + this);
74 StackTraceElement[] elems = new Exception().getStackTrace();
75 for(int i=0;i<4;i++) System.err.println(elems[i]);
78 if (incrementAndGet() == 1) {
80 if(BarrierTracing.BOOKKEEPING) {
81 caller.inc(this, "Child");
83 caller.inc(null, null);
94 System.err.println("dec barrier[" + get() + "] " + this);
95 StackTraceElement[] elems = new Exception().getStackTrace();
96 for(int i=0;i<3;i++) System.err.println(elems[i]);
99 int count = decrementAndGet();
101 if(BarrierTracing.BOOKKEEPING) {
102 BarrierTracing.dec(this, count);
105 if (caller != null) {
110 Logger.defaultLogError(
111 "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.",
123 public static String report(AsyncBarrierImpl barrier) {
124 CacheEntry<?> e = BarrierTracing.entryMap.get(barrier);
125 if(e != null) return e.toString();
126 else return "Barrier@" + System.identityHashCode(barrier);
129 public static void printReverse(AsyncBarrierImpl barrier, int indent) {
131 if (barrier.get() == 0)
133 for (int i = 0; i < indent; i++)
134 System.err.print(" ");
135 System.err.println("[" + barrier.get() + " requests]: " + report(barrier));
137 Collection<AsyncBarrierImpl> children = BarrierTracing.reverseLookup.get(barrier);
138 if (children != null) {
139 for (AsyncBarrierImpl child : children)
140 printReverse(child, indent + 2);
145 public void waitBarrier(Object request, ReadGraphImpl impl) {
153 boolean executed = impl.performPending();
154 if(executed) waitCount = 0;
157 if(waitCount > 100) Thread.yield();
158 if(waitCount > 1000) {
161 } catch (InterruptedException e) {
165 if(waitCount > WAIT_TIME) {
167 System.err.println("AsyncBarrierImpl.waitBarrier("
169 + ") is taking long to execute, so far "
170 + (waitCount / 1000) + " s.");
172 if (BarrierTracing.BOOKKEEPING) {
173 synchronized (BarrierTracing.reverseLookup) {
174 printReverse(this, 0);
176 BarrierTracing.printBAPS();
179 throw new RuntimeDatabaseException("Request timed out.");
189 public void restart() {
191 if(BarrierTracing.BOOKKEEPING) {
192 BarrierTracing.restart(this);
196 public void assertReady() {
199 throw new AssertionError("Barrier was not finished (pending="
203 public void report() {
204 // System.out.println("Barrier log:");
205 // for(Map.Entry<String, Integer> entry : sources.entrySet()) {
206 // System.out.println(entry.getKey() + " " + entry.getValue());
208 // System.out.println("SyncIntProcedure log:");
209 // for(Map.Entry<String, Integer> entry :
210 // SyncIntProcedure.counters.entrySet()) {
211 // System.out.println(entry.getKey() + " " + entry.getValue());
216 public String toString() {
218 // return "AsyncBarrierImpl@" + System.identityHashCode(this)
219 // + " - counter = " + get() + " - caller = " + caller;