Repository: lens Updated Branches: refs/heads/master 4a24fb94f -> 648fce1c5
LENS-1371 : Adding new files that were missing in previous commit Project: http://git-wip-us.apache.org/repos/asf/lens/repo Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/648fce1c Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/648fce1c Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/648fce1c Branch: refs/heads/master Commit: 648fce1c5075e1bbebd38084774229edada27a3f Parents: 4a24fb9 Author: Rajitha R <[email protected]> Authored: Tue Jul 10 19:30:43 2018 +0530 Committer: Rajitha.R <[email protected]> Committed: Tue Jul 10 19:30:43 2018 +0530 ---------------------------------------------------------------------- .../retry/SubstringMessagePolicyDecider.java | 96 ++++++++++++++++++++ .../TestSubStringMessagePolicyDecider.java | 80 ++++++++++++++++ 2 files changed, 176 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lens/blob/648fce1c/lens-server-api/src/main/java/org/apache/lens/server/api/retry/SubstringMessagePolicyDecider.java ---------------------------------------------------------------------- diff --git a/lens-server-api/src/main/java/org/apache/lens/server/api/retry/SubstringMessagePolicyDecider.java b/lens-server-api/src/main/java/org/apache/lens/server/api/retry/SubstringMessagePolicyDecider.java new file mode 100644 index 0000000..b119227 --- /dev/null +++ b/lens-server-api/src/main/java/org/apache/lens/server/api/retry/SubstringMessagePolicyDecider.java @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.lens.server.api.retry; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.lens.api.util.CommonUtils; +import org.apache.lens.server.api.LensConfConstants; + +import org.apache.hadoop.conf.Configurable; +import org.apache.hadoop.conf.Configuration; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class SubstringMessagePolicyDecider<FC extends FailureContext> implements RetryPolicyDecider<FC>, Configurable { + private Configuration conf = null; + private Map<String, Constructor> constructorMap = new HashMap<>(); + private Map<String, String[]> argsMap = new HashMap<>(); + + @Override + public BackOffRetryHandler<FC> decidePolicy(String errorMessage) { + if (errorMessage == null) { + return null; + } + for (Map.Entry<String, Constructor> entry : constructorMap.entrySet()) { + String key = entry.getKey(); + Constructor constructor = entry.getValue(); + if (errorMessage.contains(key)) { + try { + return (BackOffRetryHandler) constructor.newInstance(argsMap.get(key)); + } catch (InstantiationException e) { + log.warn("Instantiation Error ", e); + } catch (IllegalAccessException e) { + log.warn("Illegal Access Exception ", e); + } catch (InvocationTargetException e) { + log.warn("Invocation Target Exception ", e); + } + } + } + return null; + } + + @Override + public Configuration getConf() { + return this.conf; + } + + @Override + public void setConf(Configuration conf) { + this.conf = conf; + if (this.conf != null) { + String mapString = conf.get(LensConfConstants.RETRY_MESSAGE_MAP); + Map<String, String> errorMessageMap = CommonUtils.parseMapFromString(mapString); + // For all the values get the constructors. + // The assumption here is that the policies will have few constructors with all string parameters. + for (Map.Entry<String, String> entry : errorMessageMap.entrySet()) { + String val = entry.getValue(); + String className = val.substring(0, val.indexOf("(")); + String paramString = val.substring(val.indexOf("(") + 1, val.length() - 1); + String[] args = paramString.split(" "); + Class[] constructorTypes = new Class[args.length]; + Arrays.fill(constructorTypes, String.class); + try { + constructorMap.put(entry.getKey(), Class.forName(className).getConstructor(constructorTypes)); + argsMap.put(entry.getKey(), args); + } catch (ClassNotFoundException e) { + log.warn("Class not found", e); + } catch (NoSuchMethodException e) { + log.warn("No method found", e); + } + } + } + } +} + http://git-wip-us.apache.org/repos/asf/lens/blob/648fce1c/lens-server-api/src/test/java/org/apache/lens/server/api/retry/TestSubStringMessagePolicyDecider.java ---------------------------------------------------------------------- diff --git a/lens-server-api/src/test/java/org/apache/lens/server/api/retry/TestSubStringMessagePolicyDecider.java b/lens-server-api/src/test/java/org/apache/lens/server/api/retry/TestSubStringMessagePolicyDecider.java new file mode 100644 index 0000000..4fe9e0e --- /dev/null +++ b/lens-server-api/src/test/java/org/apache/lens/server/api/retry/TestSubStringMessagePolicyDecider.java @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.lens.server.api.retry; + +import org.apache.lens.server.api.LensConfConstants; + +import org.apache.hadoop.conf.Configuration; + +import org.testng.Assert; +import org.testng.annotations.Test; + +@Test +public class TestSubStringMessagePolicyDecider { + public void testConfiguredPolicyDecider() { + Configuration conf = new Configuration(); + conf.set(LensConfConstants.RETRY_MESSAGE_MAP, + "this string can be anywhere=org.apache.lens.server.api.retry.ImmediateRetryHandler(2)," + + "Another Message=org.apache.lens.server.api.retry.FibonacciExponentialBackOffRetryHandler(3 1 1)," + + "Third Message=org.apache.lens.server.api.retry.ImmediateRetryHandler(3)"); + + SubstringMessagePolicyDecider decider = new SubstringMessagePolicyDecider(); + decider.setConf(conf); + BackOffRetryHandler handler = decider + .decidePolicy("abcd random messag )" + "Das__DAS this string can be anywhere and then some other string"); + FailureContext fc1 = new FailureContext() { + @Override + public long getLastFailedTime() { + return 0; + } + + @Override + public int getFailCount() { + return 1; + } + }; + + FailureContext fc2 = new FailureContext() { + @Override + public long getLastFailedTime() { + return 0; + } + + @Override + public int getFailCount() { + return 4; + } + }; + Assert.assertEquals(handler instanceof ImmediateRetryHandler, true); + Assert.assertEquals(handler.hasExhaustedRetries(fc1), false); + Assert.assertEquals(handler.hasExhaustedRetries(fc2), true); + + handler = decider.decidePolicy("some error message string Another Message here there and everywhere"); + Assert.assertEquals(handler instanceof FibonacciExponentialBackOffRetryHandler, true); + + handler = decider.decidePolicy("some error message string Third Message here there and everywhere"); + Assert.assertEquals(handler instanceof ImmediateRetryHandler, true); + Assert.assertEquals(handler.hasExhaustedRetries(fc1), false); + Assert.assertEquals(handler.hasExhaustedRetries(fc2), true); + + handler = decider.decidePolicy("this should return null"); + Assert.assertNull(handler); + } +} +
