2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.sli.resource.dblib.pm;
23 import java.io.Serializable;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Observable;
27 import java.util.Observer;
28 import java.util.SortedSet;
29 import java.util.Timer;
30 import java.util.TimerTask;
31 import java.util.TreeSet;
32 import java.util.concurrent.atomic.AtomicLong;
34 import org.openecomp.sdnc.sli.resource.dblib.DBResourceObserver;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class SQLExecutionMonitor extends Observable
40 private static Logger LOGGER = LoggerFactory.getLogger(SQLExecutionMonitor.class);
42 final static long MILISECOND = 1000000L;
43 final static long SECOND = 1000L*MILISECOND;
45 private final Timer timer;
47 private final SortedSet<TestObject> innerSet;
48 private SQLExecutionMonitorObserver parent = null;
49 private final AtomicLong completionCounter;
50 private boolean activeState = false;
51 private final long interval;
52 private final long initialDelay;
53 private final long EXPECTED_TIME_TO_COMPLETE;
54 private final long UNPROCESSED_FAILOVER_THRESHOLD;
56 private final class MonitoringTask extends TimerTask
62 TestObject testObj = new TestObject();
63 testObj.setStartTime(testObj.getStartTime() - EXPECTED_TIME_TO_COMPLETE);
65 // take a snapshot of the current task list
66 TestObject[] array = innerSet.toArray(new TestObject[0]);
67 SortedSet<TestObject> copyCurrent = new TreeSet<TestObject>(Arrays.asList(array));
68 // get the list of the tasks that are older than the specified
70 SortedSet<TestObject> unprocessed = copyCurrent.headSet(testObj);
72 long succesfulCount = completionCounter.get();
73 int unprocessedCount = unprocessed.size();
75 if (!unprocessed.isEmpty() && unprocessedCount > UNPROCESSED_FAILOVER_THRESHOLD && succesfulCount == 0)
77 // switch the Connection Pool to passive
79 notifyObservers("Open JDBC requests=" + unprocessedCount+" in "+SQLExecutionMonitor.this.parent.getDbConnectionName());
81 } catch (Exception exc) {
82 LOGGER.error("", exc);
84 completionCounter.set(0L);
89 public static class TestObject implements Comparable<TestObject>, Serializable
92 private static final long serialVersionUID = 1L;
93 private long starttime;
98 starttime = System.nanoTime();
101 public long getStartTime()
106 public void setStartTime(long newTime)
111 public int compareTo(TestObject o)
115 if(this.starttime > o.getStartTime())
117 if(this.starttime < o.getStartTime())
120 if(this.hashCode() > o.hashCode())
122 if(this.hashCode() < o.hashCode())
128 public String toString()
130 return Long.toString(starttime)+"#"+ this.hashCode();
133 public boolean equals(Object obj)
138 return (obj instanceof TestObject
139 && starttime == ((TestObject) obj).getStartTime()
140 && hashCode() == ((TestObject) obj).hashCode());
144 public SQLExecutionMonitor(SQLExecutionMonitorObserver parent)
146 this.parent = parent;
147 completionCounter = new AtomicLong(0L);
148 interval = parent.getInterval();
149 initialDelay = parent.getInitialDelay();
150 this.UNPROCESSED_FAILOVER_THRESHOLD = parent.getUnprocessedFailoverThreshold();
151 this.EXPECTED_TIME_TO_COMPLETE = parent.getExpectedCompletionTime()*MILISECOND;
153 innerSet = Collections.synchronizedSortedSet(new TreeSet<TestObject>());
157 public void cleanup()
163 public TestObject registerRequest()
167 TestObject test = new TestObject();
168 if(innerSet.add(test))
174 // deregisterSuccessfulReguest
175 public boolean deregisterReguest(TestObject test)
179 // remove from the collection
180 if(innerSet.remove(test) && activeState)
182 completionCounter.incrementAndGet();
188 public void terminate() {
195 public final Object getParent() {
199 public void addObserver(Observer observer)
201 if(observer instanceof DBResourceObserver)
203 DBResourceObserver dbObserver = (DBResourceObserver)observer;
204 if(dbObserver.isMonitorDbResponse())
206 if(countObservers() == 0)
208 TimerTask remindTask = new MonitoringTask();
209 timer.schedule(remindTask, initialDelay, interval);
214 super.addObserver(observer);
217 public void deleteObserver(Observer observer)
219 super.deleteObserver(observer);
220 if(observer instanceof DBResourceObserver)
222 DBResourceObserver dbObserver = (DBResourceObserver)observer;
223 if(dbObserver.isMonitorDbResponse())
225 if(countObservers() == 0)
234 public final int getPorcessedConnectionsCount() {
235 return innerSet.size();