001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.usage;
018
019import java.util.List;
020import java.util.concurrent.CopyOnWriteArrayList;
021import java.util.concurrent.ThreadPoolExecutor;
022
023import org.apache.activemq.Service;
024import org.apache.activemq.broker.scheduler.JobSchedulerStore;
025import org.apache.activemq.store.PListStore;
026import org.apache.activemq.store.PersistenceAdapter;
027
028/**
029 * Holder for Usage instances for memory, store and temp files Main use case is
030 * manage memory usage.
031 *
032 * @org.apache.xbean.XBean
033 *
034 */
035public class SystemUsage implements Service {
036
037    private SystemUsage parent;
038    private String name;
039    private MemoryUsage memoryUsage;
040    private StoreUsage storeUsage;
041    private TempUsage tempUsage;
042    private ThreadPoolExecutor executor;
043    private JobSchedulerUsage jobSchedulerUsage;
044    private String checkLimitsLogLevel = "warn";
045
046    /**
047     * True if someone called setSendFailIfNoSpace() on this particular usage
048     * manager
049     */
050    private boolean sendFailIfNoSpaceExplicitySet;
051    private boolean sendFailIfNoSpace;
052    private boolean sendFailIfNoSpaceAfterTimeoutExplicitySet;
053    private long sendFailIfNoSpaceAfterTimeout = 0;
054
055    private final List<SystemUsage> children = new CopyOnWriteArrayList<SystemUsage>();
056
057    public SystemUsage() {
058        this("default", null, null, null);
059    }
060
061    public SystemUsage(String name, PersistenceAdapter adapter, PListStore tempStore, JobSchedulerStore jobSchedulerStore) {
062        this.parent = null;
063        this.name = name;
064        this.memoryUsage = new MemoryUsage(name + ":memory");
065        this.storeUsage = new StoreUsage(name + ":store", adapter);
066        this.tempUsage = new TempUsage(name + ":temp", tempStore);
067        this.jobSchedulerUsage = new JobSchedulerUsage(name + ":jobScheduler", jobSchedulerStore);
068        this.memoryUsage.setExecutor(getExecutor());
069        this.storeUsage.setExecutor(getExecutor());
070        this.tempUsage.setExecutor(getExecutor());
071    }
072
073    public SystemUsage(SystemUsage parent, String name) {
074        this.parent = parent;
075        this.executor = parent.getExecutor();
076        this.name = name;
077        this.memoryUsage = new MemoryUsage(parent.memoryUsage, name + ":memory");
078        this.storeUsage = new StoreUsage(parent.storeUsage, name + ":store");
079        this.tempUsage = new TempUsage(parent.tempUsage, name + ":temp");
080        this.jobSchedulerUsage = new JobSchedulerUsage(parent.jobSchedulerUsage, name + ":jobScheduler");
081        this.memoryUsage.setExecutor(getExecutor());
082        this.storeUsage.setExecutor(getExecutor());
083        this.tempUsage.setExecutor(getExecutor());
084    }
085
086    public String getName() {
087        return name;
088    }
089
090    /**
091     * @return the memoryUsage
092     */
093    public MemoryUsage getMemoryUsage() {
094        return this.memoryUsage;
095    }
096
097    /**
098     * @return the storeUsage
099     */
100    public StoreUsage getStoreUsage() {
101        return this.storeUsage;
102    }
103
104    /**
105     * @return the tempDiskUsage
106     */
107    public TempUsage getTempUsage() {
108        return this.tempUsage;
109    }
110
111    /**
112     * @return the schedulerUsage
113     */
114    public JobSchedulerUsage getJobSchedulerUsage() {
115        return this.jobSchedulerUsage;
116    }
117
118    @Override
119    public String toString() {
120        return "UsageManager(" + getName() + ")";
121    }
122
123    @Override
124    public void start() {
125        if (parent != null) {
126            parent.addChild(this);
127        }
128        this.memoryUsage.start();
129        this.storeUsage.start();
130        this.tempUsage.start();
131        this.jobSchedulerUsage.start();
132    }
133
134    @Override
135    public void stop() {
136        if (parent != null) {
137            parent.removeChild(this);
138        }
139        this.memoryUsage.stop();
140        this.storeUsage.stop();
141        this.tempUsage.stop();
142        this.jobSchedulerUsage.stop();
143    }
144
145    /**
146     * Sets whether or not a send() should fail if there is no space free. The
147     * default value is false which means to block the send() method until space
148     * becomes available
149     */
150    public void setSendFailIfNoSpace(boolean failProducerIfNoSpace) {
151        sendFailIfNoSpaceExplicitySet = true;
152        this.sendFailIfNoSpace = failProducerIfNoSpace;
153    }
154
155    public boolean isSendFailIfNoSpace() {
156        if (sendFailIfNoSpaceExplicitySet || parent == null) {
157            return sendFailIfNoSpace;
158        } else {
159            return parent.isSendFailIfNoSpace();
160        }
161    }
162
163    private void addChild(SystemUsage child) {
164        children.add(child);
165    }
166
167    private void removeChild(SystemUsage child) {
168        children.remove(child);
169    }
170
171    public SystemUsage getParent() {
172        return parent;
173    }
174
175    public void setParent(SystemUsage parent) {
176        this.parent = parent;
177    }
178
179    public boolean isSendFailIfNoSpaceExplicitySet() {
180        return sendFailIfNoSpaceExplicitySet;
181    }
182
183    public void setSendFailIfNoSpaceExplicitySet(boolean sendFailIfNoSpaceExplicitySet) {
184        this.sendFailIfNoSpaceExplicitySet = sendFailIfNoSpaceExplicitySet;
185    }
186
187    public long getSendFailIfNoSpaceAfterTimeout() {
188        if (sendFailIfNoSpaceAfterTimeoutExplicitySet || parent == null) {
189            return sendFailIfNoSpaceAfterTimeout;
190        } else {
191            return parent.getSendFailIfNoSpaceAfterTimeout();
192        }
193    }
194
195    public void setSendFailIfNoSpaceAfterTimeout(long sendFailIfNoSpaceAfterTimeout) {
196        this.sendFailIfNoSpaceAfterTimeoutExplicitySet = true;
197        this.sendFailIfNoSpaceAfterTimeout = sendFailIfNoSpaceAfterTimeout;
198    }
199
200    public void setName(String name) {
201        this.name = name;
202        this.memoryUsage.setName(name + ":memory");
203        this.storeUsage.setName(name + ":store");
204        this.tempUsage.setName(name + ":temp");
205        this.jobSchedulerUsage.setName(name + ":jobScheduler");
206    }
207
208    public void setMemoryUsage(MemoryUsage memoryUsage) {
209        if (memoryUsage.getName() == null) {
210            memoryUsage.setName(this.memoryUsage.getName());
211        }
212        if (parent != null) {
213            memoryUsage.setParent(parent.memoryUsage);
214        }
215        this.memoryUsage = memoryUsage;
216        this.memoryUsage.setExecutor(getExecutor());
217    }
218
219    public void setStoreUsage(StoreUsage storeUsage) {
220        if (storeUsage.getStore() == null) {
221            storeUsage.setStore(this.storeUsage.getStore());
222        }
223        if (storeUsage.getName() == null) {
224            storeUsage.setName(this.storeUsage.getName());
225        }
226        if (parent != null) {
227            storeUsage.setParent(parent.storeUsage);
228        }
229        this.storeUsage = storeUsage;
230        this.storeUsage.setExecutor(executor);
231    }
232
233    public void setTempUsage(TempUsage tempDiskUsage) {
234        if (tempDiskUsage.getStore() == null) {
235            tempDiskUsage.setStore(this.tempUsage.getStore());
236        }
237        if (tempDiskUsage.getName() == null) {
238            tempDiskUsage.setName(this.tempUsage.getName());
239        }
240        if (parent != null) {
241            tempDiskUsage.setParent(parent.tempUsage);
242        }
243        this.tempUsage = tempDiskUsage;
244        this.tempUsage.setExecutor(getExecutor());
245    }
246
247    public void setJobSchedulerUsage(JobSchedulerUsage jobSchedulerUsage) {
248        if (jobSchedulerUsage.getStore() == null) {
249            jobSchedulerUsage.setStore(this.jobSchedulerUsage.getStore());
250        }
251        if (jobSchedulerUsage.getName() == null) {
252            jobSchedulerUsage.setName(this.jobSchedulerUsage.getName());
253        }
254        if (parent != null) {
255            jobSchedulerUsage.setParent(parent.jobSchedulerUsage);
256        }
257        this.jobSchedulerUsage = jobSchedulerUsage;
258        this.jobSchedulerUsage.setExecutor(getExecutor());
259    }
260
261    /**
262     * @return the executor
263     */
264    public ThreadPoolExecutor getExecutor() {
265        return this.executor;
266    }
267
268    /**
269     * @param executor
270     *            the executor to set
271     */
272    public void setExecutor(ThreadPoolExecutor executor) {
273        this.executor = executor;
274        if (this.memoryUsage != null) {
275            this.memoryUsage.setExecutor(this.executor);
276        }
277        if (this.storeUsage != null) {
278            this.storeUsage.setExecutor(this.executor);
279        }
280        if (this.tempUsage != null) {
281            this.tempUsage.setExecutor(this.executor);
282        }
283        if(this.jobSchedulerUsage != null) {
284            this.jobSchedulerUsage.setExecutor(this.executor);
285        }
286    }
287
288   public String getCheckLimitsLogLevel() {
289       return checkLimitsLogLevel;
290   }
291
292   public void setCheckLimitsLogLevel(String checkLimitsLogLevel) {
293       this.checkLimitsLogLevel = checkLimitsLogLevel;
294   }
295}