aboutsummaryrefslogtreecommitdiffstats
path: root/aai-failover/src
diff options
context:
space:
mode:
authorJames Forsyth <jf2512@att.com>2020-05-19 20:11:28 +0000
committerGerrit Code Review <gerrit@onap.org>2020-05-19 20:11:28 +0000
commit78719f3e460b2aceb7fd4e5453db2ba141f55516 (patch)
tree1ca19b57af986f8667aadc4c34b7c616fc58ca24 /aai-failover/src
parentb25f4c9fc18c993a012347d2e7a5e920982cddf2 (diff)
parent1c955fe5f3cc766b0a9de836488a55f6ac4708c3 (diff)
Merge "aai-common support for v20"
Diffstat (limited to 'aai-failover/src')
-rw-r--r--aai-failover/src/main/java/org/onap/aai/failover/FailoverAspect.java93
-rw-r--r--aai-failover/src/main/java/org/onap/aai/failover/FailoverMonitor.java58
2 files changed, 151 insertions, 0 deletions
diff --git a/aai-failover/src/main/java/org/onap/aai/failover/FailoverAspect.java b/aai-failover/src/main/java/org/onap/aai/failover/FailoverAspect.java
new file mode 100644
index 00000000..ee21d167
--- /dev/null
+++ b/aai-failover/src/main/java/org/onap/aai/failover/FailoverAspect.java
@@ -0,0 +1,93 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.failover;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+import java.util.concurrent.atomic.AtomicLong;
+
+@Aspect
+@Component
+public class FailoverAspect {
+
+ private final Logger LOGGER = LoggerFactory.getLogger(FailoverAspect.class);
+
+ private final FailoverMonitor failoverMonitor;
+ private final AtomicLong atomicLong;
+
+ public FailoverAspect(FailoverMonitor failoverMonitor){
+ this.failoverMonitor = failoverMonitor;
+ this.atomicLong = new AtomicLong(1l);
+ }
+
+ /*
+ * By default, check for the existence of the following file: /opt/app/failover/failover.properties
+ *
+ * If the file exists, open the file as properties
+ * and find the following property: is_primary
+ * Check if the following value is set to true
+ * If it is set to true, then proceed with running the scheduled task
+ * and store the current value into an thread safe variable
+ *
+ * If the file doesn't exist, then proceed with the execution of scheduled task
+ * as if it is the primary site since there is nothing helping identify if its primary
+ *
+ * If the application is not in an kubernetes environment, in order to emulate the behavior
+ * search for the file in the classpath of application
+ * If the file can be found then it will behavior similar to above in kubernetes env
+ *
+ * Since some tasks such as ones in history is constantly getting data
+ * with little time in between each runs of the task to get latest data
+ * we don't want to log too much when the failover properties isn't being changed
+ * So it will check the last time this got executed and see if its more than two minutes have passed
+ * then if it did, then it will log current status
+ */
+ @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
+ public void preSchedule(ProceedingJoinPoint pointcut) throws Throwable {
+
+ Object target = pointcut.getTarget();
+ MethodSignature signature = (MethodSignature) pointcut.getSignature();
+ String method = signature.getMethod().getName();
+
+ if(failoverMonitor.shouldRun()){
+ atomicLong.set(1l);
+ pointcut.proceed();
+ } else {
+ long currentTime = new Date().getTime();
+ long lastMessageTime = atomicLong.get();
+
+ if((currentTime - lastMessageTime) > 120000){
+ atomicLong.compareAndSet(lastMessageTime, new Date().getTime());
+ LOGGER.debug("Not proceeding the task {}#{} due to is_primary set to false in failover.properties",
+ target.getClass(),
+ method
+ );
+ }
+ }
+ }
+}
+
diff --git a/aai-failover/src/main/java/org/onap/aai/failover/FailoverMonitor.java b/aai-failover/src/main/java/org/onap/aai/failover/FailoverMonitor.java
new file mode 100644
index 00000000..cadddc24
--- /dev/null
+++ b/aai-failover/src/main/java/org/onap/aai/failover/FailoverMonitor.java
@@ -0,0 +1,58 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.failover;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Properties;
+
+@Component
+public class FailoverMonitor {
+
+ private static final String IS_PRIMARY = "is_primary";
+ private static final String TRUE = "true";
+ private static final String DEFAULT_FOR_PRIMARY = TRUE;
+
+ @Value("${failover.location:/opt/app/failover/failover.properties}")
+ private String failoverPropertiesPath;
+
+ public boolean shouldRun() throws IOException {
+
+ Path failoverPath = Paths.get(failoverPropertiesPath);
+
+ if(Files.exists(failoverPath)){
+ Properties properties = new Properties();
+ try (InputStream is = Files.newInputStream(failoverPath)){
+ properties.load(is);
+ // If the property is_primary is missing then it should proceed
+ return TRUE.equals(properties.getProperty(IS_PRIMARY, DEFAULT_FOR_PRIMARY));
+ }
+ } else {
+ // If the file doesn't exist, then scheduled task should execute
+ return true;
+ }
+ }
+}