From: GuangrongFu Date: Thu, 29 Mar 2018 09:20:07 +0000 (+0800) Subject: Fixed the Start-up Logic X-Git-Tag: 1.2.0~52 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=holmes%2Frule-management.git;a=commitdiff_plain;h=82d2318187ea739a0b14442cbd82a3a7f6a419a7 Fixed the Start-up Logic Change-Id: I5646e771e05f5f774162373a2f69a0815acb53e5 Issue-ID: HOLMES-106 Signed-off-by: GuangrongFu --- diff --git a/rulemgt-standalone/pom.xml b/rulemgt-standalone/pom.xml index 2b9ea50..606acbf 100644 --- a/rulemgt-standalone/pom.xml +++ b/rulemgt-standalone/pom.xml @@ -69,23 +69,6 @@ true - - copy-lib-${linux64id} - process-resources - - copy-resources - - - ${linux64outputdir}/lib - - - ${project.parent.basedir}/rulemgt/target/lib - false - - - true - - copy-resources-${win64id} process-resources @@ -114,23 +97,6 @@ true - - copy-lib-${win64id} - process-resources - - copy-resources - - - ${win64outputdir}/lib - - - ${project.parent.basedir}/rulemgt/target/lib - false - - - true - - copy-dockerfile process-resources diff --git a/rulemgt-standalone/src/main/assembly/bin/run.sh b/rulemgt-standalone/src/main/assembly/bin/run.sh index 1b77f33..8d52c04 100644 --- a/rulemgt-standalone/src/main/assembly/bin/run.sh +++ b/rulemgt-standalone/src/main/assembly/bin/run.sh @@ -25,12 +25,12 @@ JAVA="$JAVA_HOME/bin/java" echo @JAVA@ $JAVA main_path=$RUNHOME/.. cd $main_path -JAVA_OPTS="-Xms50m -Xmx128m" +JAVA_OPTS="-Xms50m -Xmx128m -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9201" port=8312 #JAVA_OPTS="$JAVA_OPTS -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=$port,server=y,suspend=n" echo @JAVA_OPTS@ $JAVA_OPTS -class_path="$main_path/*:$main_path/lib/*:$main_path/holmes-rulemgt.jar" +class_path="$main_path/:$main_path/holmes-rulemgt.jar" echo @class_path@ $class_path if [ -z ${JDBC_USERNAME} ]; then diff --git a/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml b/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml index c3dc4cc..26a94e8 100644 --- a/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml +++ b/rulemgt-standalone/src/main/assembly/conf/rulemgt.yml @@ -41,7 +41,7 @@ logging: threshold: ERROR #logFormat: "%nopexception%logger\n|%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC}\n|%level\n|%message\n|%X{InvocationID}\n|%rootException\n|%marker\n|%thread\n|%n \r\n" logFormat: "%d{yyyy-MM-dd HH:mm:ss SSS} %-5p [%c][%t] invocationID:{InvocationID} - %m%n" - currentLogFilename: var/log/ONAP/holmes/rulemgt-relation-error.log + currentLogFilename: /var/log/ONAP/holmes/rulemgt-relation-error.log archivedLogFilenamePattern: /var/log/ONAP/holmes/zip/rulemgt-relation-error-%d{yyyy-MM-dd}.log.gz archivedFileCount: 7 - type: file diff --git a/rulemgt/pom.xml b/rulemgt/pom.xml index ba5524c..d07689f 100644 --- a/rulemgt/pom.xml +++ b/rulemgt/pom.xml @@ -286,8 +286,6 @@ true - org.onap.holmes.rulemgt.RuleActiveApp - lib/ @@ -295,31 +293,48 @@ org.apache.maven.plugins - maven-dependency-plugin + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-shade-plugin + + true + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + - copy package - copy-dependencies + shade - - ${project.build.directory}/lib - + + + + org.onap.holmes.rulemgt.RuleActiveApp + + - - org.apache.maven.plugins - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - - diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java index 5f96912..daebff3 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java @@ -31,6 +31,7 @@ import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.MSBRegisterUtil; import org.onap.holmes.common.utils.transactionid.TransactionIdFilter; import org.onap.holmes.rulemgt.dcae.DcaeConfigurationPolling; +import org.onap.holmes.rulemgt.msb.MsbQuery; import org.onap.holmes.rulemgt.resources.RuleMgtResources; import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; import org.onap.msb.sdk.discovery.entity.Node; @@ -64,6 +65,8 @@ public class RuleActiveApp extends IOCApplication { DcaeConfigurationPolling.POLLING_PERIOD, TimeUnit.MILLISECONDS); environment.servlets().addFilter("customFilter",new TransactionIdFilter()).addMappingForUrlPatterns(EnumSet .allOf(DispatcherType.class),true,"/*"); + + new MsbQuery().startTimer(); } private MicroServiceInfo createMicroServiceInfo() { diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java index ed9b9af..2f440b6 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java @@ -1,12 +1,12 @@ /** * Copyright 2017 ZTE Corporation. - * + *

* 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 - * + *

+ * 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. @@ -16,63 +16,71 @@ package org.onap.holmes.rulemgt.msb; import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; +import org.glassfish.hk2.api.ServiceLocator; +import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; import org.onap.holmes.rulemgt.send.RuleAllocation; import org.onap.holmes.rulemgt.send.Ip4AddingRule; import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper; -import javax.annotation.PostConstruct; -import javax.inject.Inject; import java.util.List; import java.util.Timer; import java.util.TimerTask; -@Service @Slf4j public class MsbQuery { - @Inject private RuleAllocation ruleAllocation; - @Inject private Ip4AddingRule ip4AddingRule; - @Inject private EngineIpList engineIpList; - @Inject private RuleMgtWrapper ruleMgtWrapper; - private List timerIpList; + private List timerIpList; - @PostConstruct - public void init() { + public MsbQuery() { + ruleAllocation = new RuleAllocation(); - try{ + ServiceLocator locator = ServiceLocatorHolder.getLocator(); + ip4AddingRule = locator.getService(Ip4AddingRule.class); + engineIpList = locator.getService(EngineIpList.class); + ruleMgtWrapper = locator.getService(RuleMgtWrapper.class); + } + + public void startTimer() { + try { timer(); - }catch(Exception e){ - log.error("MSBQuery init timer task failed !" + e.getMessage()); + } catch (Exception e) { + log.error("MSBQuery startTimer timer task failed !" + e.getMessage(), e); + try { + Thread.sleep(30000); + } catch (InterruptedException e1) { + Thread.currentThread().interrupt(); + } } + } - public void timer() throws Exception{ + public void timer() throws Exception { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { try { timerIpList = engineIpList.getServiceCount(); - ip4AddingRule.getIpList(timerIpList); - ruleAllocation.judgeAndAllocateRule(timerIpList); + log.info(String.format("There are %d engine instance(s) running currently.", timerIpList.size())); + ip4AddingRule.setIpList(timerIpList); + ruleAllocation.judgeAndAllocateRule(timerIpList); } catch (Exception e) { - log.error("The timing query engine instance failed " ,e); + log.error("The timing query engine instance failed ", e); } } - }, 5000, 30000); + }, 10000, 30000); } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java index f82d3a4..e224bb5 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java @@ -32,7 +32,7 @@ public class Ip4AddingRule { private RuleQueryWrapper ruleQueryWrapper; private List engineService; - public void getIpList(List ipList){ + public void setIpList(List ipList){ engineService = ipList; } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java index e69be51..75f0a08 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java @@ -1,12 +1,12 @@ /** * Copyright 2017 ZTE Corporation. - * + *

* 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 - * + *

+ * 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. @@ -17,8 +17,10 @@ package org.onap.holmes.rulemgt.send; import lombok.extern.slf4j.Slf4j; +import org.glassfish.hk2.api.ServiceLocator; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.DbDaoUtil; import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper; @@ -32,56 +34,62 @@ import javax.inject.Inject; import java.util.*; -@Service @Slf4j public class RuleAllocation { private final static int ENABLE = 1; - - @Inject private RuleMgtWrapper ruleMgtWrapper; - @Inject private RuleQueryWrapper ruleQueryWrapper; - @Inject private EngineWrapper engineWrapper; - @Inject private EngineIpList engineIpList; - @Inject private DbDaoUtil daoUtil; - private CorrelationRuleDao correlationRuleDao; - private int ruleCount; private int serviceCount; private List temIpList = new ArrayList<>(); private List engineService = new ArrayList<>(); private List allRules = new ArrayList<>(); - @PostConstruct - public void initDaoUtilAndEngineIp() throws Exception{ + public RuleAllocation() { + ServiceLocator locator = ServiceLocatorHolder.getLocator(); + ruleMgtWrapper = locator.getService(RuleMgtWrapper.class); + ruleQueryWrapper = locator.getService(RuleQueryWrapper.class); + engineWrapper = locator.getService(EngineWrapper.class); + engineIpList = locator.getService(EngineIpList.class); + daoUtil = locator.getService(DbDaoUtil.class); + + initDaoUtilAndEngineIp(); + } + + private void initDaoUtilAndEngineIp() { correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class); - temIpList = engineIpList.getServiceCount(); + try { + temIpList = engineIpList.getServiceCount(); + + } catch (Exception e) { + log.warn("Failed to get the number of engine instances.", e); + } } - public void judgeAndAllocateRule(List ipList)throws Exception{ - if(ipList != null) { + public void judgeAndAllocateRule(List ipList) throws Exception { + if (ipList != null) { engineService = ipList; serviceCount = ipList.size(); } - if(temIpList.size() < serviceCount){ + if (temIpList.size() < serviceCount) { //extend List deleteRule = calculateRule(temIpList); - List allocateRule = calculateRule(temIpList); - List extendIp = extendCompareIp(engineService,temIpList); - AllocateService(extendIp,allocateRule); - deleteRuleFromFormerEngine(deleteRule,temIpList); + List allocateRule = calculateRule(temIpList); + List extendIp = extendCompareIp(engineService, temIpList); + AllocateService(extendIp, allocateRule); + deleteRuleFromFormerEngine(deleteRule, temIpList); } else if (temIpList.size() > serviceCount) { //destroy List destroyIp = destroyCompareIp(engineService, temIpList); AllocateService(restIp(destroyIp), relocateRuleAfterDestroy(destroyIp)); - } else if(temIpList.size() == serviceCount) { + } else if (temIpList.size() == serviceCount) { temIpList = engineService; return; } @@ -91,18 +99,18 @@ public class RuleAllocation { // When the engine is expanding, the rules that need to be allocated are calculated. - private List calculateRule(List oldIpList) throws Exception{ + private List calculateRule(List oldIpList) throws Exception { allRules = ruleQueryWrapper.queryRuleByEnable(ENABLE); - if(allRules != null) { + if (allRules != null) { ruleCount = allRules.size(); } int count = ruleCount / serviceCount; int remainder = ruleCount % serviceCount; List subRule = new ArrayList<>(); - for(String ip : oldIpList) { + for (String ip : oldIpList) { List rules = ruleQueryWrapper.queryRuleByEngineInstance(ip); - List tem = rules.subList(count + (remainder-- / oldIpList.size()),rules.size()); + List tem = rules.subList(count + (remainder-- / oldIpList.size()), rules.size()); subRule.addAll(tem); } return subRule; @@ -111,24 +119,24 @@ public class RuleAllocation { //Rules that need to be allocated after the engine is destroyed private List relocateRuleAfterDestroy(List destroyIpList) throws CorrelationException { List rules = new ArrayList<>(); - try{ - if(destroyIpList != null){ - for(String ip : destroyIpList) { + try { + if (destroyIpList != null) { + for (String ip : destroyIpList) { rules.addAll(ruleQueryWrapper.queryRuleByEngineInstance(ip)); } } - }catch(CorrelationException e) { - log.error("method relocateRuleAfterDestroy get data from DB failed !" +e.getMessage()); + } catch (CorrelationException e) { + log.error("method relocateRuleAfterDestroy get data from DB failed !", e); } return rules; } //Extended IP - private List extendCompareIp(List newList, List oldList){ + private List extendCompareIp(List newList, List oldList) { List extendIpList = new ArrayList<>(); - for( String ip :newList) { - if(! oldList.contains(ip)) { + for (String ip : newList) { + if (!oldList.contains(ip)) { extendIpList.add(ip); } } @@ -138,8 +146,8 @@ public class RuleAllocation { //Destroyed IP private List destroyCompareIp(List newList, List oldList) { List destroyIpList = new ArrayList<>(); - for(String ip : oldList) { - if(!newList.contains(ip)) { + for (String ip : oldList) { + if (!newList.contains(ip)) { destroyIpList.add(ip); } } @@ -149,52 +157,51 @@ public class RuleAllocation { //Residual IP after destruction private List restIp(List destroyIp) { List restIpList = new ArrayList<>(); - for(String ip : engineService) { - if(!destroyIp.contains(ip)) { + for (String ip : engineService) { + if (!destroyIp.contains(ip)) { restIpList.add(ip); } } return restIpList; } - public void AllocateService(List extendIpList, List subList) throws Exception{ + public void AllocateService(List extendIpList, List subList) throws Exception { List needIpList = getSortIp(extendIpList); - for(int i=0,j=0;j < subList.size();i++,j++ ){ + for (int i = 0, j = 0; j < subList.size(); i++, j++) { int index = i % needIpList.size(); String deployIp = needIpList.get(index); CorrelationRule rule = subList.get(j); rule.setEngineInstance(deployIp); - allocateDeployRule(rule,deployIp); + allocateDeployRule(rule, deployIp); } } //The IP to be allocated is in ascending order, and the least is circulate. - private List getSortIp(List ipList){ - List ipRuleList = new ArrayList<>(); - HashMap hashMap = new HashMap(); + private List getSortIp(List ipList) { + List ipRuleList = new ArrayList<>(); + HashMap hashMap = new HashMap(); - try{ - for(String ip : ipList){ + try { + for (String ip : ipList) { ipRuleList = ruleQueryWrapper.queryRuleByEngineInstance(ip); - if(ipRuleList != null) { + if (ipRuleList != null) { hashMap.put(ip, String.valueOf(ipRuleList.size())); } } - }catch (Exception e){ - log.error("getEngineIp4AddRule failed !" + e.getMessage()); + } catch (Exception e) { + log.error("getEngineIp4AddRule failed !", e); } List> list_Data = new ArrayList>(hashMap.entrySet()); Collections.sort(list_Data, new Comparator>() { - public int compare(Map.Entry o1, Map.Entry o2) - { + public int compare(Map.Entry o1, Map.Entry o2) { return o1.getValue().compareTo(o2.getValue()); } }); List needList = new ArrayList<>(); - for(Map.Entry map: list_Data) { + for (Map.Entry map : list_Data) { String key = map.getKey(); needList.add(key); } @@ -202,25 +209,25 @@ public class RuleAllocation { } private void allocateDeployRule(CorrelationRule rule, String ip) throws CorrelationException { - try{ - ruleMgtWrapper.deployRule2Engine(rule,ip); + try { + ruleMgtWrapper.deployRule2Engine(rule, ip); correlationRuleDao.updateRule(rule); - }catch (CorrelationException e){ + } catch (CorrelationException e) { throw new CorrelationException("allocate Deploy Rule failed", e); } } private void deleteRuleFromFormerEngine(List subRule, List oldList) { - try{ - for(String ip : oldList){ - for(CorrelationRule rule: subRule) { - if(ip.equals(rule.getEngineInstance())) { - engineWrapper.deleteRuleFromEngine(rule.getPackageName(),ip); + try { + for (String ip : oldList) { + for (CorrelationRule rule : subRule) { + if (ip.equals(rule.getEngineInstance())) { + engineWrapper.deleteRuleFromEngine(rule.getPackageName(), ip); } } } - }catch (CorrelationException e) { - log.error("When the engine is extended, deleting rule failed" +e.getMessage()); + } catch (CorrelationException e) { + log.error("When the engine is extended, deleting rule failed", e); } } diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java index 24c5f3a..696e32a 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java @@ -17,8 +17,22 @@ package org.onap.holmes.rulemgt.send; +import org.easymock.EasyMock; +import org.glassfish.hk2.api.ServiceLocator; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; +import org.onap.holmes.common.utils.DbDaoUtil; +import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper; +import org.onap.holmes.rulemgt.db.CorrelationRuleDao; +import org.onap.holmes.rulemgt.msb.EngineIpList; +import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper; +import org.onap.holmes.rulemgt.wrapper.RuleQueryWrapper; import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; import java.util.ArrayList; @@ -27,11 +41,50 @@ import java.util.List; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; +@RunWith(PowerMockRunner.class) +@PrepareForTest({ServiceLocator.class, RuleMgtWrapper.class, RuleQueryWrapper.class, EngineWrapper.class, + EngineIpList.class, DbDaoUtil.class, RuleAllocation.class, ServiceLocatorHolder.class}) public class RuleAllocationTest { - private RuleAllocation ruleAllocation = new RuleAllocation(); + @Before + public void prepare() { + + ServiceLocator locator = PowerMock.createMock(ServiceLocator.class); + RuleMgtWrapper ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class); + RuleQueryWrapper ruleQueryWrapper = PowerMock.createMock(RuleQueryWrapper.class); + EngineWrapper engineWrapper = PowerMock.createMock(EngineWrapper.class); + EngineIpList engineIpList = PowerMock.createMock(EngineIpList.class); + CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); + DbDaoUtil daoUtil = PowerMock.createMock(DbDaoUtil.class); + PowerMock.mockStatic(ServiceLocatorHolder.class); + + EasyMock.expect(ServiceLocatorHolder.getLocator()).andReturn(locator); + EasyMock.expect(locator.getService(RuleMgtWrapper.class)).andReturn(ruleMgtWrapper); + EasyMock.expect(locator.getService(RuleQueryWrapper.class)).andReturn(ruleQueryWrapper); + EasyMock.expect(locator.getService(EngineWrapper.class)).andReturn(engineWrapper); + EasyMock.expect(locator.getService(EngineIpList.class)).andReturn(engineIpList); + EasyMock.expect(locator.getService(DbDaoUtil.class)).andReturn(daoUtil); + EasyMock.expect(daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(correlationRuleDao); + try { + EasyMock.expect(engineIpList.getServiceCount()).andReturn(new ArrayList()); + } catch (Exception e) { + // Do nothing + } + + + PowerMock.replayAll(); + + } + + @After + public void destroy() { + PowerMock.resetAll(); + } + @Test public void extendCompareIpTest() throws Exception{ + RuleAllocation ruleAllocation = new RuleAllocation(); + List newList = new ArrayList<>(); newList.add("10.96.33.34"); newList.add("10.74.65.24"); @@ -47,6 +100,8 @@ public class RuleAllocationTest { @Test public void destroyCompareIpTest() throws Exception{ + RuleAllocation ruleAllocation = new RuleAllocation(); + List newList = new ArrayList<>(); newList.add("10.96.33.34");