2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
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.onap.aai.failover;
23 import java.util.Date;
24 import java.util.concurrent.atomic.AtomicLong;
26 import org.aspectj.lang.ProceedingJoinPoint;
27 import org.aspectj.lang.annotation.Around;
28 import org.aspectj.lang.annotation.Aspect;
29 import org.aspectj.lang.reflect.MethodSignature;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
36 public class FailoverAspect {
38 private final Logger LOGGER = LoggerFactory.getLogger(FailoverAspect.class);
40 private final FailoverMonitor failoverMonitor;
41 private final AtomicLong atomicLong;
43 public FailoverAspect(FailoverMonitor failoverMonitor) {
44 this.failoverMonitor = failoverMonitor;
45 this.atomicLong = new AtomicLong(1l);
49 * By default, check for the existence of the following file: /opt/app/failover/failover.properties
51 * If the file exists, open the file as properties
52 * and find the following property: is_primary
53 * Check if the following value is set to true
54 * If it is set to true, then proceed with running the scheduled task
55 * and store the current value into an thread safe variable
57 * If the file doesn't exist, then proceed with the execution of scheduled task
58 * as if it is the primary site since there is nothing helping identify if its primary
60 * If the application is not in an kubernetes environment, in order to emulate the behavior
61 * search for the file in the classpath of application
62 * If the file can be found then it will behavior similar to above in kubernetes env
64 * Since some tasks such as ones in history is constantly getting data
65 * with little time in between each runs of the task to get latest data
66 * we don't want to log too much when the failover properties isn't being changed
67 * So it will check the last time this got executed and see if its more than two minutes have passed
68 * then if it did, then it will log current status
70 @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
71 public void preSchedule(ProceedingJoinPoint pointcut) throws Throwable {
73 Object target = pointcut.getTarget();
74 MethodSignature signature = (MethodSignature) pointcut.getSignature();
75 String method = signature.getMethod().getName();
77 if (failoverMonitor.shouldRun()) {
81 long currentTime = new Date().getTime();
82 long lastMessageTime = atomicLong.get();
84 if ((currentTime - lastMessageTime) > 120000) {
85 atomicLong.compareAndSet(lastMessageTime, new Date().getTime());
86 LOGGER.debug("Not proceeding the task {}#{} due to is_primary set to false in failover.properties",
87 target.getClass(), method);