001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.service;
019    
020    import java.sql.Connection;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.apache.hadoop.conf.Configuration;
025    import org.apache.oozie.ErrorCode;
026    import org.apache.oozie.client.WorkflowJob;
027    import org.apache.oozie.service.SchemaService.SchemaName;
028    import org.apache.oozie.store.Store;
029    import org.apache.oozie.store.StoreException;
030    import org.apache.oozie.store.WorkflowStore;
031    import org.apache.oozie.util.Instrumentable;
032    import org.apache.oozie.util.Instrumentation;
033    import org.apache.oozie.util.XLog;
034    import org.apache.oozie.workflow.WorkflowLib;
035    import org.apache.oozie.workflow.lite.DBLiteWorkflowLib;
036    
037    public class DBLiteWorkflowStoreService extends LiteWorkflowStoreService implements Instrumentable {
038        private boolean selectForUpdate;
039        private XLog log;
040        private int statusWindow;
041    
042        public static final String CONF_PREFIX = Service.CONF_PREFIX + "DBLiteWorkflowStoreService.";
043        public static final String CONF_METRICS_INTERVAL_MINS = CONF_PREFIX + "status.metrics.collection.interval";
044        public static final String CONF_METRICS_INTERVAL_WINDOW = CONF_PREFIX + "status.metrics.window";
045    
046        private static final String INSTRUMENTATION_GROUP = "jobstatus";
047        private static final String INSTRUMENTATION_GROUP_WINDOW = "windowjobstatus";
048    
049        private Map<String, Integer> statusCounts = new HashMap<String, Integer>();
050        private Map<String, Integer> statusWindowCounts = new HashMap<String, Integer>();
051    
052        /**
053         * Gets the number of workflows for each status and populates the hash.
054         */
055        class JobStatusCountCallable implements Runnable {
056            @Override
057            public void run() {
058                WorkflowStore store = null;
059                try {
060                    store = Services.get().get(WorkflowStoreService.class).create();
061                    store.beginTrx();
062                    WorkflowJob.Status[] wfStatusArr = WorkflowJob.Status.values();
063                    for (WorkflowJob.Status aWfStatusArr : wfStatusArr) {
064                        statusCounts.put(aWfStatusArr.name(), store.getWorkflowCountWithStatus(aWfStatusArr.name()));
065                        statusWindowCounts.put(aWfStatusArr.name(), store.getWorkflowCountWithStatusInLastNSeconds(
066                                aWfStatusArr.name(), statusWindow));
067                    }
068                    store.commitTrx();
069                }
070                catch (StoreException e) {
071                    if (store != null) {
072                        store.rollbackTrx();
073                    }
074                    log.warn("Exception while accessing the store", e);
075                }
076                catch (Exception ex) {
077                    log.error("Exception, {0}", ex.getMessage(), ex);
078                    if (store != null && store.isActive()) {
079                        try {
080                            store.rollbackTrx();
081                        }
082                        catch (RuntimeException rex) {
083                            log.warn("openjpa error, {0}", rex.getMessage(), rex);
084                        }
085                    }
086                }
087                finally {
088                    if (store != null) {
089                        if (!store.isActive()) {
090                            try {
091                                store.closeTrx();
092                            }
093                            catch (RuntimeException rex) {
094                                log.warn("Exception while attempting to close store", rex);
095                            }
096                        }
097                        else {
098                            log.warn("transaction is not committed or rolled back before closing entitymanager.");
099                        }
100                    }
101                }
102            }
103        }
104    
105        public void init(Services services) throws ServiceException {
106            Configuration conf = services.getConf();
107            statusWindow = conf.getInt(CONF_METRICS_INTERVAL_WINDOW, 3600);
108            int statusMetricsCollectionInterval = conf.getInt(CONF_METRICS_INTERVAL_MINS, 5);
109            log = XLog.getLog(getClass());
110            selectForUpdate = false;
111    
112            WorkflowJob.Status[] wfStatusArr = WorkflowJob.Status.values();
113            for (WorkflowJob.Status aWfStatusArr : wfStatusArr) {
114                statusCounts.put(aWfStatusArr.name(), 0);
115                statusWindowCounts.put(aWfStatusArr.name(), 0);
116            }
117            Runnable jobStatusCountCallable = new JobStatusCountCallable();
118            services.get(SchedulerService.class).schedule(jobStatusCountCallable, 1, statusMetricsCollectionInterval,
119                                                          SchedulerService.Unit.MIN);
120        }
121    
122        public void destroy() {
123        }
124    
125        /**
126         * Return the workflow lib without DB connection. Will be used for parsing purpose.
127         *
128         * @return Workflow Library
129         */
130        @Override
131        public WorkflowLib getWorkflowLibWithNoDB() {
132            return getWorkflowLib(null);
133        }
134    
135        private WorkflowLib getWorkflowLib(Connection conn) {
136            javax.xml.validation.Schema schema = Services.get().get(SchemaService.class).getSchema(SchemaName.WORKFLOW);
137            return new DBLiteWorkflowLib(schema, LiteDecisionHandler.class, LiteActionHandler.class, conn);
138        }
139    
140        @Override
141        public WorkflowStore create() throws StoreException {
142            try {
143                return new WorkflowStore(selectForUpdate);
144            }
145            catch (Exception ex) {
146                throw new StoreException(ErrorCode.E0600, ex.getMessage(), ex);
147            }
148        }
149    
150        @Override
151        public <S extends Store> WorkflowStore create(S store) throws StoreException {
152            try {
153                return new WorkflowStore(store, selectForUpdate);
154            }
155            catch (Exception ex) {
156                throw new StoreException(ErrorCode.E0600, ex.getMessage(), ex);
157            }
158        }
159    
160    
161        @Override
162        public void instrument(Instrumentation instr) {
163            final WorkflowJob.Status[] wfStatusArr = WorkflowJob.Status.values();
164            for (WorkflowJob.Status aWfStatusArr : wfStatusArr) {
165                final String statusName = aWfStatusArr.name();
166                instr.addVariable(INSTRUMENTATION_GROUP, statusName, new Instrumentation.Variable<Long>() {
167                    public Long getValue() {
168                        return statusCounts.get(statusName).longValue();
169                    }
170                });
171                instr.addVariable(INSTRUMENTATION_GROUP_WINDOW, statusName, new Instrumentation.Variable<Long>() {
172                    public Long getValue() {
173                        return statusWindowCounts.get(statusName).longValue();
174                    }
175                });
176            }
177        }
178    }