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