5e5323ccf997dc6c6d46b367c26bb16022611cb7
[aaf/authz.git] / authz-cass / src / main / java / org / onap / aaf / dao / Cached.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.dao;\r
24 \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
30 \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
36 \r
37 import org.onap.aaf.inno.env.Env;\r
38 import org.onap.aaf.inno.env.Trans;\r
39 \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
44         \r
45         private static Timer infoTimer;\r
46         private Object cache[];\r
47         public final int segSize;\r
48 \r
49         protected final String name;\r
50         \r
51 \r
52 \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
55                 int h = 0;\r
56                 for (int i = 0; i < key.length(); i++) {\r
57                     h = 31*h + key.charAt(i);\r
58                 }\r
59                 if(h<0)h*=-1;\r
60                 return h%segSize;\r
61         }\r
62         \r
63         public Cached(CIDAO<TRANS> info, String name, int segSize) {\r
64                 this.name =name;\r
65                 this.segSize = segSize;\r
66                 this.info = info;\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
71                 }\r
72         }\r
73         \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
78         }\r
79 \r
80 \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
88                 return cacheIdx;\r
89         }\r
90 \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
95                 if(map!=null) {\r
96                         map.clear();\r
97                 }\r
98                 return Result.ok();\r
99         }\r
100 \r
101         protected interface Getter<D> {\r
102                 public abstract Result<List<D>> get();\r
103         };\r
104         \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
110                 \r
111                 int cacheIdx = cacheIdx(key);\r
112                 Map<String, Dated> map = ((Map<String,Dated>)cache[cacheIdx]);\r
113                 \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
118                 \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
123                 } else {\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
129                         }\r
130                 }\r
131                 return rld;\r
132         }\r
133 \r
134         /**\r
135          * Each Cached object has multiple Segments that need cleaning.  Derive each, and add to Cleansing Thread\r
136          * @param env\r
137          * @param dao\r
138          */\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
143                         }\r
144                 }\r
145         }\r
146 \r
147 \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
153                 }\r
154         }\r
155         \r
156         public static void stopTimer() {\r
157                 Cache.stopTimer();\r
158                 if(infoTimer!=null) {\r
159                         infoTimer.cancel();\r
160                         infoTimer = null;\r
161                 }\r
162         }\r
163         \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
170                 \r
171                 public Refresh(AuthzEnv env, CIDAO<AuthzTrans> cidao, int minRefresh) {\r
172                         this.env = env;\r
173                         this.cidao = cidao;\r
174                         this.minRefresh = minRefresh;\r
175                         lastRun = System.currentTimeMillis()-maxRefresh-1000;\r
176                 }\r
177                 \r
178                 @Override\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
183 \r
184                         if(interval < minRefresh || interval < Math.min(env.transRate(),maxRefresh)) return;\r
185                         lastRun = now;\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
190                         }\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
195                         }\r
196                 }\r
197         }\r
198 }\r