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