From 253ae56182744411bcb2873cb5e40338479a2b52 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Tue, 13 Sep 2016 15:25:54 +0200 Subject: Dump cache management moved to translate-utils Removed all references to vpp to be usable in any plugin Change-Id: Id63b06d643f4004e67b2502fc02e58cf3d238b3b Signed-off-by: Jan Srnicek --- .../util/read/cache/DumpCacheManager.java | 123 +++++++++++++++++++++ .../util/read/cache/EntityDumpExecutor.java | 38 +++++++ .../util/read/cache/EntityDumpNonEmptyCheck.java | 31 ++++++ .../cache/EntityDumpPostProcessingFunction.java | 35 ++++++ .../exceptions/check/DumpCheckFailedException.java | 31 ++++++ .../exceptions/check/i/DumpEmptyException.java | 40 +++++++ .../execution/DumpExecutionFailedException.java | 27 +++++ .../execution/i/DumpCallFailedException.java | 33 ++++++ .../execution/i/DumpTimeoutException.java | 35 ++++++ .../cache/noop/NoopDumpPostProcessingFunction.java | 32 ++++++ 10 files changed, 425 insertions(+) create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpExecutor.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpNonEmptyCheck.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpPostProcessingFunction.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/DumpCheckFailedException.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/i/DumpEmptyException.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/DumpExecutionFailedException.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpCallFailedException.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpTimeoutException.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/noop/NoopDumpPostProcessingFunction.java (limited to 'infra') diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java new file mode 100644 index 000000000..dfb62146c --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.ModificationCache; +import io.fd.honeycomb.translate.util.read.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException; +import io.fd.honeycomb.translate.util.read.cache.noop.NoopDumpPostProcessingFunction; +import javax.annotation.Nonnull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Manager responsible for returning Data object dumps
either from cache or by invoking specified {@link + * EntityDumpExecutor} + */ +public final class DumpCacheManager { + + private static final Logger LOG = LoggerFactory.getLogger(DumpCacheManager.class); + + private final EntityDumpExecutor dumpExecutor; + private final EntityDumpNonEmptyCheck dumpNonEmptyCheck; + private final EntityDumpPostProcessingFunction postProcessor; + + private DumpCacheManager(DumpCacheManagerBuilder builder) { + this.dumpExecutor = builder.dumpExecutor; + this.dumpNonEmptyCheck = builder.dumpNonEmptyCheck; + this.postProcessor = builder.postProcessingFunction; + } + + /** + * Returns {@link Optional} of dump + */ + public Optional getDump(@Nonnull String entityKey, @Nonnull ModificationCache cache, final U dumpParams) + throws DumpExecutionFailedException { + + // this key binding to every log has its logic ,because every customizer have its own cache manager and if + // there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs + LOG.debug("Loading dump for KEY[{}]", entityKey); + + T dump = (T) cache.get(entityKey); + + if (dump == null) { + LOG.debug("Dump for KEY[{}] not present in cache,invoking dump executor", entityKey); + // binds and execute dump to be thread-save + dump = dumpExecutor.executeDump(dumpParams); + + // TODO (HONEYCOMB-210): remove empty check (empty dump is normal state, special handling is not needed) + try { + dumpNonEmptyCheck.assertNotEmpty(dump); + } catch (DumpCheckFailedException e) { + LOG.debug("Dump for KEY[{}] has been resolved as empty: {}", entityKey, e.getMessage()); + return Optional.absent(); + } + + // no need to check if post processor active,if wasn't set,default no-op will be used + LOG.debug("Post-processing dump for KEY[{}]", entityKey); + dump = postProcessor.apply(dump); + + LOG.debug("Caching dump for KEY[{}]", entityKey); + cache.put(entityKey, dump); + return Optional.of(dump); + } else { + return Optional.of(dump); + } + } + + public static final class DumpCacheManagerBuilder { + + private EntityDumpExecutor dumpExecutor; + private EntityDumpNonEmptyCheck dumpNonEmptyCheck; + private EntityDumpPostProcessingFunction postProcessingFunction; + + public DumpCacheManagerBuilder() { + // for cases when user does not set specific post-processor + postProcessingFunction = new NoopDumpPostProcessingFunction(); + } + + public DumpCacheManagerBuilder withExecutor(@Nonnull EntityDumpExecutor executor) { + this.dumpExecutor = executor; + return this; + } + + public DumpCacheManagerBuilder withNonEmptyPredicate(@Nonnull EntityDumpNonEmptyCheck check) { + this.dumpNonEmptyCheck = check; + return this; + } + + public DumpCacheManagerBuilder withPostProcessingFunction( + EntityDumpPostProcessingFunction postProcessingFunction) { + this.postProcessingFunction = postProcessingFunction; + return this; + } + + public DumpCacheManager build() { + checkNotNull(dumpExecutor, "Dump executor cannot be null"); + checkNotNull(dumpNonEmptyCheck, "Dump verifier cannot be null"); + checkNotNull(postProcessingFunction, + "Dump post-processor cannot be null cannot be null, default implementation is used if its not set"); + + return new DumpCacheManager<>(this); + } + } +} + + diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpExecutor.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpExecutor.java new file mode 100644 index 000000000..c220da4ff --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpExecutor.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache; + +import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException; +import javax.annotation.concurrent.ThreadSafe; + +/** + * Generic interface for classes that return dumps for Data objects. + * Must be implemented in Thread-save fashion. + */ +@ThreadSafe +public interface EntityDumpExecutor { + + static Void NO_PARAMS = null; + + /** + * Performs dump on {@link T} entity + * + * @return dump of specified {@link T} entity + * @throws DumpExecutionFailedException when dump fails + */ + T executeDump(final U params) throws DumpExecutionFailedException; +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpNonEmptyCheck.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpNonEmptyCheck.java new file mode 100644 index 000000000..7c59270e0 --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpNonEmptyCheck.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache; + +import io.fd.honeycomb.translate.util.read.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.translate.util.read.cache.exceptions.check.i.DumpEmptyException; + +/** + * Generic interface for classes that verifies if dump of data object is non-empty + */ +public interface EntityDumpNonEmptyCheck { + + /** + * Verifies if data are non-empty,if not throws {@link DumpEmptyException} + */ + public void assertNotEmpty(T data) throws DumpCheckFailedException; +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpPostProcessingFunction.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpPostProcessingFunction.java new file mode 100644 index 000000000..ce3942515 --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/EntityDumpPostProcessingFunction.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache; + +import java.util.function.Function; + +/** + * Generic interface for class that are post-processing data dumped from vpp + */ +@FunctionalInterface +public interface EntityDumpPostProcessingFunction extends Function { + + + /** + * Performs postprocessing on dumped data + * + * @return Post-processed data + */ + @Override + T apply(T t); +} \ No newline at end of file diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/DumpCheckFailedException.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/DumpCheckFailedException.java new file mode 100644 index 000000000..097ef344d --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/DumpCheckFailedException.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache.exceptions.check; + +/** + * Abstract parent of exceptions thrown while checking if dump is not empty + */ +public abstract class DumpCheckFailedException extends Exception { + + public DumpCheckFailedException(String message) { + super(message); + } + + public DumpCheckFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/i/DumpEmptyException.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/i/DumpEmptyException.java new file mode 100644 index 000000000..8769827ee --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/check/i/DumpEmptyException.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache.exceptions.check.i; + +import io.fd.honeycomb.translate.util.read.cache.EntityDumpNonEmptyCheck; +import io.fd.honeycomb.translate.util.read.cache.exceptions.check.DumpCheckFailedException; + +/** + * This exception occurs when dump is resolved as empty by {@link EntityDumpNonEmptyCheck} + */ +public class DumpEmptyException extends DumpCheckFailedException { + + /** + * Creates {@link DumpEmptyException} with specified reason + */ + public DumpEmptyException(String reason) { + super(reason); + } + + /** + * Creates {@link DumpEmptyException} with specified reason and sub-exception + */ + public DumpEmptyException(String reason, Exception e) { + super(reason, e); + } +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/DumpExecutionFailedException.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/DumpExecutionFailedException.java new file mode 100644 index 000000000..806a5500c --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/DumpExecutionFailedException.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache.exceptions.execution; + +/** + * Default parent for all exceptions connected to dumping of data + */ +public abstract class DumpExecutionFailedException extends Exception { + + public DumpExecutionFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpCallFailedException.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpCallFailedException.java new file mode 100644 index 000000000..ba794070b --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpCallFailedException.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache.exceptions.execution.i; + +import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException; + +/** + * Wrapper exception for any execution exception during dumping + */ +public class DumpCallFailedException extends DumpExecutionFailedException { + + public DumpCallFailedException(String message, Exception cause) { + super(message, cause); + } + + public static final DumpCallFailedException wrapFailedCallException(String message, Exception cause) { + return new DumpCallFailedException(message, cause); + } +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpTimeoutException.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpTimeoutException.java new file mode 100644 index 000000000..be440ba4f --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/exceptions/execution/i/DumpTimeoutException.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache.exceptions.execution.i; + +import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException; +import java.util.concurrent.TimeoutException; + +/** + * Exception thrown when dump call ends in timeout + */ +public class DumpTimeoutException extends DumpExecutionFailedException { + + public DumpTimeoutException(String message, TimeoutException cause) { + super(message, cause); + + } + + public static final DumpTimeoutException wrapTimeoutException(String message, TimeoutException cause) { + return new DumpTimeoutException(message, cause); + } +} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/noop/NoopDumpPostProcessingFunction.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/noop/NoopDumpPostProcessingFunction.java new file mode 100644 index 000000000..db4d1dc5d --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/noop/NoopDumpPostProcessingFunction.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * 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. + */ + +package io.fd.honeycomb.translate.util.read.cache.noop; + +import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NoopDumpPostProcessingFunction implements EntityDumpPostProcessingFunction { + + private static final Logger LOG = LoggerFactory.getLogger(NoopDumpPostProcessingFunction.class); + + @Override + public T apply(final T t) { + LOG.debug("Default post processing function called for {}", t); + return t; + } +} -- cgit 1.2.3-korg