]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/SessionGarbageCollection.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / SessionGarbageCollection.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.util;\r
13 \r
14 import java.util.function.Consumer;\r
15 \r
16 import org.eclipse.core.runtime.IProgressMonitor;\r
17 import org.eclipse.core.runtime.NullProgressMonitor;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Session;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.request.WriteRequest;\r
22 import org.simantics.db.common.utils.Logger;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.service.ClusterControl;\r
25 import org.simantics.db.service.LifecycleSupport;\r
26 import org.simantics.db.service.QueryControl;\r
27 import org.simantics.utils.DataContainer;\r
28 import org.simantics.utils.datastructures.Callback;\r
29 \r
30 /**\r
31  * @author Tuukka Lehtonen\r
32  */\r
33 public class SessionGarbageCollection {\r
34     /**\r
35      * collect everything possible, default target is 0 queries.\r
36      */\r
37     private static int DEFAULT_ALLOWED_TIME = 300;\r
38     public static int getDefaultQueryTarget() {\r
39         return DEFAULT_ALLOWED_TIME;\r
40     }\r
41     public static void setDefaultAllowedTime(int a) {\r
42         DEFAULT_ALLOWED_TIME = a;\r
43     }\r
44     /**\r
45      * Default maximum cluster memory consumption target is 32MB.\r
46      */\r
47     private static int DEFAULT_CLUSTER_TARGET = -1;\r
48     public static int getDefaultClusterTarget() {\r
49         return DEFAULT_CLUSTER_TARGET;\r
50     }\r
51     public static void setDefaultClusterTarget(int a) {\r
52         DEFAULT_CLUSTER_TARGET = a;\r
53     }\r
54     /**\r
55      * @param monitor <code>null</code> if progress monitor not available\r
56      * @param session the session to GC\r
57      * @param sync <code>true</code> to block until gc is completed\r
58      * @param errorCallback callback for any errors occuring during the\r
59      *        operation or <code>null</code> to ignore errors\r
60      */\r
61     public static boolean gc(IProgressMonitor monitor, Session session, boolean sync,\r
62             final Consumer<DatabaseException> errorCallback) {\r
63         long took = gc(monitor, session, sync, errorCallback, DEFAULT_ALLOWED_TIME, DEFAULT_CLUSTER_TARGET);\r
64         return ((16*took) / DEFAULT_ALLOWED_TIME) > 15;\r
65     }\r
66     \r
67     public static void gc(ReadGraph graph, final int allowedTimeInMs, final int clusterTarget) throws DatabaseException {\r
68         doIt(null, graph, allowedTimeInMs, clusterTarget);\r
69     }\r
70 \r
71     private static void doIt(IProgressMonitor _monitor, ReadGraph graph, final int allowedTimeInMs, final int clusterTarget) {\r
72         if(_monitor == null) _monitor = new NullProgressMonitor();\r
73         QueryControl qc = graph.getService(QueryControl.class);\r
74         ClusterControl cc = graph.getService(ClusterControl.class);\r
75         _monitor.beginTask("Collect clusters", IProgressMonitor.UNKNOWN);\r
76         cc.gc(graph, clusterTarget);\r
77         _monitor.beginTask("Collect queries", IProgressMonitor.UNKNOWN);\r
78         qc.gc(graph, allowedTimeInMs);\r
79     }\r
80     \r
81     /**\r
82      * @param monitor\r
83      * @param session\r
84      * @param sync\r
85      * @param errorCallback\r
86      */\r
87     /**\r
88      * @param monitor <code>null</code> if progress monitor not available\r
89      * @param session the session to GC\r
90      * @param sync <code>true</code> to block until gc is completed\r
91      * @param errorCallback callback for any errors occuring during the\r
92      *        operation or <code>null</code> to ignore errors\r
93      * @param allowedTimeInMs the time allowed for query collection in ms\r
94      * @param clusterTarget target for the maximum amount of memory in bytes to\r
95      *        be consumed by cluster caches after collection.\r
96      */\r
97     public static long gc(IProgressMonitor monitor, Session session, boolean sync,\r
98             final Consumer<DatabaseException> errorCallback, final int allowedTimeInMs, final int clusterTarget) {\r
99         if (monitor == null)\r
100             monitor = new NullProgressMonitor();\r
101         final IProgressMonitor _monitor = monitor;\r
102         if (session == null)\r
103             throw new NullPointerException("null session");\r
104 \r
105         final DataContainer<Long> took = new DataContainer<Long>(0L);\r
106         \r
107         WriteRequest request = new WriteRequest() {\r
108             @Override\r
109             public void perform(WriteGraph graph) throws DatabaseException {\r
110                 long start = System.nanoTime();\r
111                 doIt(_monitor, graph, allowedTimeInMs, clusterTarget);\r
112                 long duration = System.nanoTime()-start;\r
113                 took.set((long)(duration*1e-6));\r
114             }\r
115         };\r
116         \r
117         LifecycleSupport lfs = session.peekService(LifecycleSupport.class);\r
118         if (lfs == null || lfs.isClosed() || lfs.isClosing())\r
119             return 0L;\r
120         \r
121         if (sync) {\r
122             try {\r
123                 session.syncRequest(request);\r
124             } catch (DatabaseException e) {\r
125                 if (errorCallback != null)\r
126                     errorCallback.accept(e);\r
127                 else\r
128                     Logger.defaultLogError(e);\r
129             }\r
130         } else {\r
131             session.asyncRequest(request, new Callback<DatabaseException>() {\r
132                 @Override\r
133                 public void run(DatabaseException e) {\r
134                     if (e != null) {\r
135                         if (errorCallback != null)\r
136                             errorCallback.accept(e);\r
137                         else\r
138                             Logger.defaultLogError(e);\r
139                     }\r
140                 }\r
141             });\r
142         }\r
143         \r
144         return took.get();\r
145         \r
146     }\r
147 \r
148 }\r