]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java
AsyncBarrier.dec runs into refcounting problem
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionImplSocket.java
index 30e0dde5244de43dbd11ff9582015dad0d831f76..f8fd7d17ddab55e7274388ff259155918227486f 100644 (file)
@@ -143,6 +143,7 @@ import org.simantics.db.service.ClusteringSupport;
 import org.simantics.db.service.CollectionSupport;
 import org.simantics.db.service.DebugSupport;
 import org.simantics.db.service.DirectQuerySupport;
+import org.simantics.db.service.EventSupport;
 import org.simantics.db.service.GraphChangeListenerSupport;
 import org.simantics.db.service.InitSupport;
 import org.simantics.db.service.LifecycleSupport;
@@ -166,6 +167,7 @@ import org.simantics.utils.DataContainer;
 import org.simantics.utils.Development;
 import org.simantics.utils.threads.logger.ITask;
 import org.simantics.utils.threads.logger.ThreadLogger;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.procedure.TLongProcedure;
 import gnu.trove.set.hash.TLongHashSet;
@@ -173,6 +175,8 @@ import gnu.trove.set.hash.TLongHashSet;
 
 public abstract class SessionImplSocket implements Session, WriteRequestScheduleSupport {
 
+    private static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SessionImplSocket.class);
+
     protected static final boolean DEBUG        = false;
 
     private static final boolean DIAGNOSTICS       = false;
@@ -289,6 +293,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         serviceLocator.registerService(ExternalValueSupport.class, new ExternalValueSupportImpl(this));
         serviceLocator.registerService(RandomAccessValueSupport.class, new RandomAccessValueSupportImpl());
         serviceLocator.registerService(ServiceActivityMonitor.class, new ServiceActivityMonitorImpl());
+        serviceLocator.registerService(EventSupport.class, new EventSupportImpl());
         ServiceActivityUpdaterForWriteTransactions.register(this);
 
         this.virtualGraphServerSupport = new VirtualGraphServerSupportImpl(this, t);
@@ -414,12 +419,10 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
             }
         }
 
-        int thread = request.hashCode() & queryProvider2.THREAD_MASK;
-
-        requestManager.scheduleWrite(new SessionTask(true) {
+        requestManager.scheduleWrite(new SessionTask(null) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 if(Development.DEVELOPMENT) {
                     try {
@@ -458,7 +461,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     });
 
                     assert (null != writer);
-//                    writer.state.barrier.inc();
+                    writer.asyncBarrier.inc();
 
                     try {
                         request.perform(writer);
@@ -468,7 +471,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                             Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", t);
                         writeState.except(t);
                     } finally {
-//                        writer.state.barrier.dec();
+                        writer.asyncBarrier.dec();
 //                        writer.waitAsync(request);
                     }
 
@@ -552,12 +555,10 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
         assert (request != null);
 
-        int thread = request.hashCode() & queryProvider2.THREAD_MASK;
-
-        requestManager.scheduleWrite(new SessionTask(true) {
+        requestManager.scheduleWrite(new SessionTask(null) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
 
@@ -570,52 +571,21 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                 VirtualGraph vg = getProvider(request.getProvider());
                 WriteGraphImpl writer = WriteGraphImpl.create(getQueryProvider2(), writeSupport, vg);
 
-                try {
-                    WriteState<T> writeStateT = new WriteState<T>(writer, request, notify, procedure);
-                    writeState = writeStateT;
+                WriteState<T> writeStateT = new WriteState<T>(writer, request, notify, procedure);
+                writeState = writeStateT;
+                assert (null != writer);
 
-                    assert (null != writer);
-//                    writer.state.barrier.inc();
+                try {
+                    writer.asyncBarrier.inc();
                     writeStateT.setResult(request.perform(writer));
                     assert (null != writer);
-
-//                    writer.state.barrier.dec();
-//                    writer.waitAsync(null);
-
                 } catch (Throwable e) {
-
-//                    writer.state.barrier.dec();
-//                    writer.waitAsync(null);
-
                     writeState.except(e);
-
-//                  state.stopWriteTransaction(clusterStream);
-//
-//              } catch (Throwable e) {
-//                  // Log it first, just to be safe that the error is always logged.
-//                  Logger.defaultLogError("Write transaction caused an unexpected error, see exception.", e);
-//
-//                  try {
-//                      // Callback is client code, we have to be prepared for it to throw unexpected exceptions.
-//                      // All we can do here is to log those, can't really pass them anywhere.
-//                      if (procedure != null) {
-//                          if(e instanceof DatabaseException) procedure.exception((DatabaseException)e);
-//                          else procedure.exception(new DatabaseException(e));
-//                      }
-//                  } catch (Throwable e2) {
-//                      Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", e2);
-//                  }
-//
-//                  clientChanges = new ClientChangesImpl(SessionImplSocket.this);
-//
-//                  state.stopWriteTransaction(clusterStream);
-
                 } finally {
+                    writer.asyncBarrier.dec();
                     fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
                 }
 
-//              if(notify != null) notify.release();
-
                 task.finish();
 
             }
@@ -634,16 +604,14 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
     @Override
     public <T> void scheduleRequest(final DelayedWrite request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
 
-        final ITask total = ThreadLogger.getInstance().begin("ScheduleDelayedWrite");
+        final ITask total = ThreadLogger.task("ScheduleDelayedWrite");
 
         assert (request != null);
 
-        int thread = request.hashCode() & queryProvider2.THREAD_MASK;
-
-        requestManager.scheduleWrite(new SessionTask(true) {
+        requestManager.scheduleWrite(new SessionTask(null) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
                 fireSessionVariableChange(SessionVariables.QUEUED_READS);
 
                 Procedure<Object> stateProcedure = new Procedure<Object>() {
@@ -683,7 +651,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
                 delayedWriteState = null;
 
-                ITask task2 = ThreadLogger.getInstance().begin("DelayedWriteCommit");
+                ITask task2 = ThreadLogger.task("DelayedWriteCommit");
                 fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
 
                 flushCounter = 0;
@@ -1377,12 +1345,10 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
         assert (request != null);
 
-        int thread = request.hashCode() & queryProvider2.THREAD_MASK;
-
-        requestManager.scheduleWrite(new SessionTask(true) {
+        requestManager.scheduleWrite(new SessionTask(null) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
 
@@ -1477,12 +1443,10 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
         assert (request != null);
 
-        int thread = request.hashCode() & queryProvider2.THREAD_MASK;
-
-        requestManager.scheduleWrite(new SessionTask(true) {
+        requestManager.scheduleWrite(new SessionTask(null) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 ITask task = ThreadLogger.getInstance().begin("WriteRequest " + request);
 
@@ -1501,19 +1465,22 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         assert (request != null);
         assert (procedure != null);
 
-        //int thread = request.hashCode() & queryProvider2.THREAD_MASK;
-
         requestManager.scheduleRead(new SessionRead(throwable, notify) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 fireSessionVariableChange(SessionVariables.QUEUED_READS);
 
+                ITask task = ThreadLogger.task(request);
+
                 ListenerBase listener = getListenerBase(procedure);
 
                 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
 
+                // This is never synced but increase to prevent it from visiting 0
+                newGraph.asyncBarrier.inc();
+
                 try {
 
                     if (listener != null) {
@@ -1551,8 +1518,6 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
                         try {
 
-//                            newGraph.state.barrier.inc();
-
                             T t = request.perform(newGraph);
 
                             try {
@@ -1596,9 +1561,8 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                             }
 
                         }
-
-//                        newGraph.state.barrier.dec();
-//                        newGraph.waitAsync(request);
+                        
+                        task.finish();
 
                     }
 
@@ -1624,46 +1588,53 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         requestManager.scheduleRead(new SessionRead(null, notify) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 fireSessionVariableChange(SessionVariables.QUEUED_READS);
 
+                ITask task = ThreadLogger.task(request);
+
                 final ReadGraphImpl newGraph = ReadGraphImpl.create(getQueryProvider2());
+                newGraph.asyncBarrier.inc();
 
                 try {
 
                     if (listener != null) {
 
                         try {
-                               QueryCacheBase.resultAsyncReadEntry(newGraph, request, null, listener, procedure);
-                               //QueryCache.runnerAsyncReadEntry(newGraph, request, null, listener, procedure, true);
-                                                       //newGraph.processor.query(newGraph, request, null, procedure, listener);
-                                               } catch (DatabaseException e) {
-                                                       Logger.defaultLogError(e);
-                                               }
+                            QueryCacheBase.resultAsyncReadEntry(newGraph, request, null, listener, procedure);
+                        } catch (DatabaseException e) {
+                            LOGGER.error("Unhandled query exception", e);
+                        }
 
                     } else {
 
-//                        final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
-//                                procedure, "request");
+                        BlockingAsyncProcedure<T> wrap = new BlockingAsyncProcedure<T>(newGraph, null, procedure, request, true) {
 
-                       BlockingAsyncProcedure<T> wrap = new BlockingAsyncProcedure<T>(newGraph, procedure, request);
+                            public void execute(AsyncReadGraph graph_, T result) {
+                                task.finish();
+                                super.execute(graph_, result);
+                            }
 
-                       try {
+                            public void exception(AsyncReadGraph graph_, Throwable t) {
+                                task.finish();
+                                super.exception(graph_, t);
+                            }
 
-                            request.perform(newGraph, wrap);
-                            wrap.get();
+                        };
 
+                        try {
+                            wrap.performSync(request);
                         } catch (DatabaseException e) {
-
-                                                       Logger.defaultLogError(e);
-
+                            LOGGER.error("Unhandled query exception", e);
                         }
 
                     }
 
                 } finally {
 
+                    newGraph.asyncBarrier.dec();
+
                     fireSessionVariableChange(SessionVariables.QUEUED_READS);
 
                 }
@@ -1686,7 +1657,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         requestManager.scheduleRead(new SessionRead(null, notify) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 fireSessionVariableChange(SessionVariables.QUEUED_READS);
 
@@ -1742,7 +1713,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         requestManager.scheduleRead(new SessionRead(null, notify) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 fireSessionVariableChange(SessionVariables.QUEUED_READS);
 
@@ -1796,7 +1767,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         requestManager.scheduleRead(new SessionRead(throwable, notify) {
 
             @Override
-            public void run(int thread) {
+            public void run0(int thread) {
 
                 fireSessionVariableChange(SessionVariables.QUEUED_READS);
 
@@ -2480,6 +2451,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                 return;
 
             WriteGraphImpl reactionGraph = WriteGraphImpl.create(graph.processor, writeSupport, null);
+            reactionGraph.asyncBarrier.inc();
 
             try {
 
@@ -2488,16 +2460,18 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     try {
                         l.graphChanged(e2);
                     } catch (Throwable ex) {
-                        ex.printStackTrace();
+                        LOGGER.error("Could not invoke listener {} with event {}", l, e2, ex);
                     }
                 }
 
             } finally {
 
+                reactionGraph.asyncBarrier.dec();
+
             }
 
         } catch (Throwable t) {
-            t.printStackTrace();
+            LOGGER.error("Could not fire metadata listeners {} {}", graph, cs2, t);
         }
 
     }
@@ -3189,9 +3163,11 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         });
 
         Throwable t = exception.get();
-        if(t != null) {
-            if(t instanceof DatabaseException) throw (DatabaseException)t;
-            else throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
+        if (t != null) {
+            if (t instanceof DatabaseException)
+                throw (DatabaseException) t;
+            else
+                throw new DatabaseException("Unexpected exception in ReadGraph.syncRequest(Read)", t);
         }
 
         return result;
@@ -3397,28 +3373,6 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         asyncRequest(request, (Procedure<T>)procedure);
     }
 
-
-
-    void check(Throwable t) throws DatabaseException {
-        if(t != null) {
-            if(t instanceof DatabaseException) throw (DatabaseException)t;
-            else throw new DatabaseException("Unexpected exception", t);
-        }
-    }
-
-    void check(DataContainer<Throwable> container) throws DatabaseException {
-        Throwable t = container.get();
-        if(t != null) {
-            if(t instanceof DatabaseException) throw (DatabaseException)t;
-            else throw new DatabaseException("Unexpected exception", t);
-        }
-    }
-
-
-
-
-
-
     boolean sameProvider(Write request) {
         if(writeState.getGraph().provider != null) {
             return writeState.getGraph().provider.equals(request.getProvider());
@@ -3435,9 +3389,11 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
 
     public static final ThreadGroup SessionThreadGroup = new ThreadGroup("Session Thread Group");
+
     private void assertNotSession() throws DatabaseException {
         Thread current = Thread.currentThread();
-        if(sessionThreads.contains(current)) throw new DatabaseException("Caller is already inside a transaction.");
+        if (sessionThreads.contains(current))
+            throw new ServiceException("Caller is already inside a transaction.");
     }
 
     void assertAlive() {
@@ -3562,8 +3518,8 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
         while(dirtyPrimitives) {
             dirtyPrimitives = false;
-            getQueryProvider2().performDirtyUpdates(writer);
-            getQueryProvider2().performScheduledUpdates(writer);
+            getQueryProvider2().propagateChangesInQueryCache(writer);
+            getQueryProvider2().listening.fireListeners(writer);
         }
 
         fireMetadataListeners(writer, clientChanges);