1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.dao;
\r
25 import java.util.Date;
\r
26 import java.util.List;
\r
27 import java.util.Map;
\r
28 import java.util.Timer;
\r
29 import java.util.TimerTask;
\r
31 import org.onap.aaf.authz.env.AuthzEnv;
\r
32 import org.onap.aaf.authz.env.AuthzTrans;
\r
33 import org.onap.aaf.authz.layer.Result;
\r
34 import org.onap.aaf.cache.Cache;
\r
35 import org.onap.aaf.dao.aaf.cass.Status;
\r
37 import org.onap.aaf.inno.env.Env;
\r
38 import org.onap.aaf.inno.env.Trans;
\r
40 public class Cached<TRANS extends Trans, DATA extends Cacheable> extends Cache<TRANS,DATA> {
\r
41 // Java does not allow creation of Arrays with Generics in them...
\r
42 // private Map<String,Dated> cache[];
\r
43 protected final CIDAO<TRANS> info;
\r
45 private static Timer infoTimer;
\r
46 private Object cache[];
\r
47 public final int segSize;
\r
49 protected final String name;
\r
53 // Taken from String Hash, but coded, to ensure consistent across Java versions. Also covers negative case;
\r
54 public int cacheIdx(String key) {
\r
56 for (int i = 0; i < key.length(); i++) {
\r
57 h = 31*h + key.charAt(i);
\r
63 public Cached(CIDAO<TRANS> info, String name, int segSize) {
\r
65 this.segSize = segSize;
\r
67 cache = new Object[segSize];
\r
68 // Create a new Map for each Segment, and store locally
\r
69 for(int i=0;i<segSize;++i) {
\r
70 cache[i]=obtain(name+i);
\r
74 public void add(String key, List<DATA> data) {
\r
75 @SuppressWarnings("unchecked")
\r
76 Map<String,Dated> map = ((Map<String,Dated>)cache[cacheIdx(key)]);
\r
77 map.put(key, new Dated(data));
\r
81 public int invalidate(String key) {
\r
82 int cacheIdx = cacheIdx(key);
\r
83 @SuppressWarnings("unchecked")
\r
84 Map<String,Dated> map = ((Map<String,Dated>)cache[cacheIdx]);
\r
85 // if(map.remove(key)!=null) // Not seeming to remove all the time
\r
86 if(map!=null)map.clear();
\r
87 // System.err.println("Remove " + name + " " + key);
\r
91 public Result<Void> invalidate(int segment) {
\r
92 if(segment<0 || segment>=cache.length) return Result.err(Status.ERR_BadData,"Cache Segment %s is out of range",Integer.toString(segment));
\r
93 @SuppressWarnings("unchecked")
\r
94 Map<String,Dated> map = ((Map<String,Dated>)cache[segment]);
\r
101 protected interface Getter<D> {
\r
102 public abstract Result<List<D>> get();
\r
105 // TODO utilize Segmented Caches, and fold "get" into "reads"
\r
106 @SuppressWarnings("unchecked")
\r
107 public Result<List<DATA>> get(TRANS trans, String key, Getter<DATA> getter) {
\r
108 List<DATA> ld = null;
\r
109 Result<List<DATA>> rld = null;
\r
111 int cacheIdx = cacheIdx(key);
\r
112 Map<String, Dated> map = ((Map<String,Dated>)cache[cacheIdx]);
\r
114 // Check for saved element in cache
\r
115 Dated cached = map.get(key);
\r
116 // Note: These Segment Timestamps are kept up to date with DB
\r
117 Date dbStamp = info.get(trans, name,cacheIdx);
\r
119 // Check for cache Entry and whether it is still good (a good Cache Entry is same or after DBEntry, so we use "before" syntax)
\r
120 if(cached!=null && dbStamp.before(cached.timestamp)) {
\r
121 ld = (List<DATA>)cached.data;
\r
122 rld = Result.ok(ld);
\r
124 rld = getter.get();
\r
125 if(rld.isOK()) { // only store valid lists
\r
126 map.put(key, new Dated(rld.value)); // successful item found gets put in cache
\r
127 // } else if(rld.status == Result.ERR_Backend){
\r
128 // map.remove(key);
\r
135 * Each Cached object has multiple Segments that need cleaning. Derive each, and add to Cleansing Thread
\r
139 public static void startCleansing(AuthzEnv env, CachedDAO<?,?,?> ... dao) {
\r
140 for(CachedDAO<?,?,?> d : dao) {
\r
141 for(int i=0;i<d.segSize;++i) {
\r
142 startCleansing(env, d.table()+i);
\r
148 public static<T extends Trans> void startRefresh(AuthzEnv env, CIDAO<AuthzTrans> cidao) {
\r
149 if(infoTimer==null) {
\r
150 infoTimer = new Timer("CachedDAO Info Refresh Timer");
\r
151 int minRefresh = 10*1000*60; // 10 mins Integer.parseInt(env.getProperty(CACHE_MIN_REFRESH_INTERVAL,"2000")); // 2 second minimum refresh
\r
152 infoTimer.schedule(new Refresh(env,cidao, minRefresh), 1000, minRefresh); // note: Refresh from DB immediately
\r
156 public static void stopTimer() {
\r
158 if(infoTimer!=null) {
\r
159 infoTimer.cancel();
\r
164 private final static class Refresh extends TimerTask {
\r
165 private static final int maxRefresh = 2*60*10000; // 20 mins
\r
166 private AuthzEnv env;
\r
167 private CIDAO<AuthzTrans> cidao;
\r
168 private int minRefresh;
\r
169 private long lastRun;
\r
171 public Refresh(AuthzEnv env, CIDAO<AuthzTrans> cidao, int minRefresh) {
\r
173 this.cidao = cidao;
\r
174 this.minRefresh = minRefresh;
\r
175 lastRun = System.currentTimeMillis()-maxRefresh-1000;
\r
179 public void run() {
\r
180 // Evaluate whether to refresh based on transaction rate
\r
181 long now = System.currentTimeMillis();
\r
182 long interval = now-lastRun;
\r
184 if(interval < minRefresh || interval < Math.min(env.transRate(),maxRefresh)) return;
\r
186 AuthzTrans trans = env.newTransNoAvg();
\r
187 Result<Void> rv = cidao.check(trans);
\r
188 if(rv.status!=Result.OK) {
\r
189 env.error().log("Error in CacheInfo Refresh",rv.details);
\r
191 if(env.debug().isLoggable()) {
\r
192 StringBuilder sb = new StringBuilder("Cache Info Refresh: ");
\r
193 trans.auditTrail(0, sb, Env.REMOTE);
\r
194 env.debug().log(sb);
\r