Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / health / RedisHealthCheck.java
1 package org.onap.msb.apiroute.health;
2
3 import java.text.SimpleDateFormat;
4
5 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import redis.clients.jedis.Jedis;
10
11 import com.codahale.metrics.health.HealthCheck;
12
13 public class RedisHealthCheck extends HealthCheck implements Runnable {
14         private static final Logger LOGGER = LoggerFactory
15                         .getLogger(RedisHealthCheck.class);
16
17         public static boolean writeCheckFlag = true;
18         private static Result result = Result.healthy();
19         
20         private static int failedLoopCheckNum = 12;
21         private static int failedTimer = 5 * 1000;
22
23         private static int normalTimer = 20 * 1000;
24
25         public static Result getResult() {
26                 return result;
27         }
28
29         @Override
30         protected Result check() {
31
32                 // check write
33                 if (writeCheckFlag) {
34                         Result writeCheckResult = checkWrite();
35                         if (writeCheckResult.isHealthy()) {
36                                 writeCheckFlag = false;
37                         }
38
39                         // write failed
40                         if (!writeCheckResult.isHealthy()) {
41                                 return writeCheckResult;
42                         }
43                 }
44                 
45                 // check read
46                 Result readCheckResult = checkRead();
47
48                 // read failed
49                 if (!readCheckResult.isHealthy()) {
50                         return readCheckResult;
51                 }
52
53                 return Result.healthy();
54         }
55
56         private Result checkRead() {
57                 Jedis jedisHandle = null;
58
59                 Result healthRst = Result.healthy();
60                 try {
61
62                         jedisHandle = JedisUtil.borrowJedisInstance();
63                         jedisHandle.get("healthchek:checktime");
64
65                 } catch (Exception e) {
66                         LOGGER.warn("RedisHealthCheck exception", e);
67                         healthRst = Result.unhealthy(e);
68                 } finally {
69                         JedisUtil.returnJedisInstance(jedisHandle);
70                 }
71
72                 return healthRst;
73         }
74
75         private Result checkWrite() {
76                 Jedis jedisHandle = null;
77
78                 Result healthRst = Result.healthy();
79                 try {
80
81                         long currentTime = System.currentTimeMillis();
82                         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
83                         String date = sdf.format(currentTime);
84
85                         jedisHandle = JedisUtil.borrowJedisInstance();
86                         String statusCode = jedisHandle.set("healthchek:checktime", date);
87
88                         if (statusCode != null && statusCode.equals("OK")) {
89                                 healthRst = Result.healthy("check redis:" + statusCode);
90                         } else {
91                                 healthRst = Result.unhealthy("check redis:" + statusCode);
92                         }
93
94                 } catch (Exception e) {
95                         LOGGER.warn("RedisHealthCheck exception", e);
96                         healthRst = Result.unhealthy(e);
97                 } finally {
98                         JedisUtil.returnJedisInstance(jedisHandle);
99                 }
100
101                 return healthRst;
102         }
103
104         @Override
105         public void run() {
106                 // TODO Auto-generated method stub
107                 while (true) {
108
109                         if (LOGGER.isDebugEnabled()) {
110                                 LOGGER.debug("redis check starttime:"
111                                                 + System.currentTimeMillis());
112                         }
113
114                         result = checkWithPolicy();
115
116                         if (LOGGER.isDebugEnabled()) {
117                                 LOGGER.debug("redis check result:" + result.isHealthy()
118                                                 + " message:" + result.getMessage());
119                                 
120                                 LOGGER.debug("redis check endtime:"
121                                                 + System.currentTimeMillis());
122                         }
123
124                         try {
125                                 Thread.sleep(normalTimer);
126                         } catch (InterruptedException e) {
127                                 // TODO Auto-generated catch block
128                                 LOGGER.warn("loop check redis,thread sleep excepiton", e);
129                         }
130                 }
131         }
132         
133         private Result checkWithPolicy() {
134                 int failedNum = 0;
135                 Result temp = Result.healthy();
136
137                 do {
138                         // check again
139                         temp = check();
140
141                         // healthy break;
142                         if (temp.isHealthy()) {
143                                 break;
144                         }
145
146                         // unhealthy go on
147                         failedNum++;
148                         
149                         try {
150                                 Thread.sleep(failedTimer);
151                         } catch (InterruptedException e) {
152                                 // TODO Auto-generated catch block
153                                 LOGGER.warn("loop check redis,thread sleep excepiton", e);
154                         }
155                         
156                 } while (failedNum <= failedLoopCheckNum);
157
158                 return temp;
159         }
160 }