X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FAsyncReadEntry.java;h=72582ee605802bfe32fac38d10119f6200602586;hb=497f90316cb17b5bbd8f8b72af3ab3bb6582a902;hp=00abbebf65859e76d0e8dad481d01097cab04621;hpb=0d9b90834ce56b292c00b1a39850ed842c3e4d42;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java index 00abbebf6..72582ee60 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java @@ -11,14 +11,18 @@ *******************************************************************************/ package org.simantics.db.impl.query; +import org.simantics.databoard.Bindings; import org.simantics.db.AsyncReadGraph; +import org.simantics.db.DevelopmentKeys; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.exception.RuntimeDatabaseException; import org.simantics.db.impl.BlockingAsyncProcedure; -import org.simantics.db.impl.DebugPolicy; +import org.simantics.db.impl.graph.AsyncBarrierImpl; +import org.simantics.db.impl.graph.BarrierTracing; import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.query.QueryProcessor.SessionTask; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.request.AsyncRead; +import org.simantics.utils.Development; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,22 +30,25 @@ final public class AsyncReadEntry extends CacheEntryBase> i private static final Logger LOGGER = LoggerFactory.getLogger(AsyncReadEntry.class); - protected AsyncRead request; + protected AsyncRead id; AsyncReadEntry(AsyncRead request) { - this.request = request; - if (DebugPolicy.QUERY_STATE) - System.out.println("[QUERY STATE]: created " + this); + this.id = request; + if (Development.DEVELOPMENT) { + if(Development.getProperty(DevelopmentKeys.CACHE_ENTRY_STATE, Bindings.BOOLEAN)) { + System.err.println("[QUERY STATE]: created " + this); + } + } } @Override int makeHash() { - return request.hashCode(); + return id.hashCode(); } @Override public Object getOriginalRequest() { - return request; + return id; } @Override @@ -70,7 +77,7 @@ final public class AsyncReadEntry extends CacheEntryBase> i try { - BlockingAsyncProcedure proc = new BlockingAsyncProcedure<>(graph, new AsyncProcedure() { + BlockingAsyncProcedure proc = new BlockingAsyncProcedure<>(graph.asyncBarrier, graph, new AsyncProcedure() { @Override public void execute(AsyncReadGraph graph, T result) { @@ -83,9 +90,9 @@ final public class AsyncReadEntry extends CacheEntryBase> i except(t); } - }, request); + }, id); - request.perform(graph, proc); + id.perform(graph, proc); proc.get(); @@ -102,17 +109,17 @@ final public class AsyncReadEntry extends CacheEntryBase> i @Override public int type() { - return request.getFlags(); + return id.getFlags(); } @Override public String toString() { - if (request == null) + if (id == null) return "DISCARDED"; else if (isExcepted()) - return request.toString() + " " + getResult(); + return id.toString() + " " + getResult(); else - return request.toString() + " " + statusOrException; + return id.toString() + " " + statusOrException; } }; @@ -133,7 +140,8 @@ final public class AsyncReadEntry extends CacheEntryBase> i } else { try { - proc.execute(graph, (T) getResult()); + T result = (T) getResult(); + proc.execute(graph, result); } catch (Throwable t) { LOGGER.error("performFromCache proc.execute failed", t); } @@ -144,57 +152,101 @@ final public class AsyncReadEntry extends CacheEntryBase> i } - public static void computeForEach(ReadGraphImpl parentGraph, AsyncRead request, AsyncReadEntry entry, - AsyncProcedure procedure_) throws DatabaseException { + public static T computeForEach(ReadGraphImpl graph, AsyncRead request, AsyncReadEntry entry, + AsyncProcedure procedure_, boolean needsToBlock) throws DatabaseException { AsyncProcedure procedure = entry != null ? entry : procedure_; - ReadGraphImpl queryGraph = parentGraph.withParent(entry); + ReadGraphImpl queryGraph = graph.withParent(entry); + queryGraph.asyncBarrier.inc(); - BlockingAsyncProcedure proc = new BlockingAsyncProcedure<>(queryGraph, new AsyncProcedure() { - - @Override - public void execute(AsyncReadGraph returnGraph, T result) { - try { - procedure.execute(parentGraph, result); - } catch (Throwable t) { - LOGGER.error("computeForEach procedure.execute failed", t); - } + BlockingAsyncProcedure proc = new BlockingAsyncProcedure<>(queryGraph.asyncBarrier, graph, null, request); + + class AsyncTask extends SessionTask { + + int counter = 0; + T result; + DatabaseException exception; + + public AsyncTask(ReadGraphImpl graph) { + super(graph); } @Override - public void exception(AsyncReadGraph returnGraph, Throwable t) { - try { - procedure.exception(parentGraph, t); - } catch (Throwable t2) { - LOGGER.error("computeForEach procedure.exception failed", t2); + public void run0(int thread) { + if(needsToBlock) proc.waitBarrier(); + if(proc.isDone()) { + ReadGraphImpl executeGraph = graph.withParent(graph.parent); + executeGraph.asyncBarrier.inc(); + try { + result = (T)proc.get(); + if(procedure != null) { + procedure.execute(executeGraph, result); + } + } catch (DatabaseException e) { + if(procedure != null) procedure.exception(executeGraph, e); + exception = e; + } catch (Throwable t) { + DatabaseException dbe = new DatabaseException(t); + if(procedure != null) procedure.exception(executeGraph, dbe); + exception = dbe; + } finally { + if (entry != null) { + // This does not throw + entry.performFromCache(executeGraph, procedure_); + } + executeGraph.asyncBarrier.dec(); + executeGraph.asyncBarrier.waitBarrier(procedure, executeGraph); + } + } else { + if(counter++ > 10000) { + if(BarrierTracing.BOOKKEEPING) { + AsyncBarrierImpl.printReverse(queryGraph.asyncBarrier, 2); + AsyncBarrierImpl caller = queryGraph.asyncBarrier.caller; + while(caller != null) { + System.err.println("called by " + AsyncBarrierImpl.report(caller)); + caller = caller.caller; + } + for(AsyncBarrierImpl ab : BarrierTracing.debuggerMap.keySet()) { + AsyncBarrierImpl.printReverse(ab, 2); + } + } + throw new IllegalStateException("Eternal loop in queries."); + } + graph.processor.schedule(new AsyncTask(graph)); } } + + } - @Override - public String toString() { - return procedure.toString(); - } + try { + request.perform(queryGraph, proc); + } finally { + queryGraph.asyncBarrier.dec(); + } - }, request); + AsyncTask task = new AsyncTask(graph); - request.perform(queryGraph, proc); - - proc.get(); + if(needsToBlock) task.run(0); + else if (proc.isDone()) task.run(0); + else { + graph.processor.schedule(task); + return null; + } - if (entry != null) - entry.performFromCache(parentGraph, procedure_); + if(task.exception != null) throw task.exception; + else return task.result; } @Override public String toString() { if (isDiscarded()) - return "DISCARDED " + request.toString(); + return "DISCARDED " + id.toString(); else if (isExcepted()) - return request.toString() + " " + getResult(); + return id.toString() + " " + getResult(); else - return request.toString() + " " + statusOrException; + return id.toString() + " " + statusOrException; } @Override