From 701597775fd2e98d7c577c5c1c683ac15d42098c Mon Sep 17 00:00:00 2001 From: Mark Owens Date: Tue, 3 Oct 2017 10:48:42 -0400 Subject: [PATCH] NIFI-1547: DistributedMapCacheServer: Ambiguous error on misconfiguration Updated PersistentMapCache constructor in PersistentMapCache.java to catch an OverlappingFileLockException and present a more useful error message before propagating the exception forward. The log message alerts user to possible duplicated persistencePath in call to PersistentMapCache. Created a test method to verify the exception is thrown as expected. Signed-off-by: Matthew Burgess This closes #2192 --- .../cache/server/map/PersistentMapCache.java | 14 ++++++- .../server/map/TestPersistentMapCache.java | 42 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/test/java/org/apache/nifi/distributed/cache/server/map/TestPersistentMapCache.java diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/main/java/org/apache/nifi/distributed/cache/server/map/PersistentMapCache.java b/nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/main/java/org/apache/nifi/distributed/cache/server/map/PersistentMapCache.java index 9f1375c084..28861ea2b9 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/main/java/org/apache/nifi/distributed/cache/server/map/PersistentMapCache.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/main/java/org/apache/nifi/distributed/cache/server/map/PersistentMapCache.java @@ -21,12 +21,15 @@ import java.io.EOFException; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.channels.OverlappingFileLockException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.wali.MinimalLockingWriteAheadLog; import org.wali.SerDe; @@ -34,13 +37,22 @@ import org.wali.UpdateType; import org.wali.WriteAheadRepository; public class PersistentMapCache implements MapCache { + + private static final Logger logger = LoggerFactory.getLogger(PersistentMapCache.class); + private final MapCache wrapped; private final WriteAheadRepository wali; private final AtomicLong modifications = new AtomicLong(0L); public PersistentMapCache(final String serviceIdentifier, final File persistencePath, final MapCache cacheToWrap) throws IOException { - wali = new MinimalLockingWriteAheadLog<>(persistencePath.toPath(), 1, new Serde(), null); + try { + wali = new MinimalLockingWriteAheadLog<>(persistencePath.toPath(), 1, new Serde(), null); + } catch (OverlappingFileLockException ex) { + logger.error("OverlappingFileLockException thrown: Check lock location - possible duplicate persistencePath conflict in PersistentMapCache."); + // Propagate the exception + throw ex; + } wrapped = cacheToWrap; } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/test/java/org/apache/nifi/distributed/cache/server/map/TestPersistentMapCache.java b/nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/test/java/org/apache/nifi/distributed/cache/server/map/TestPersistentMapCache.java new file mode 100644 index 0000000000..55fb4704b1 --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/test/java/org/apache/nifi/distributed/cache/server/map/TestPersistentMapCache.java @@ -0,0 +1,42 @@ +/* + * 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.nifi.distributed.cache.server.map; + +import java.io.File; +import java.io.IOException; +import java.nio.channels.OverlappingFileLockException; +import org.apache.nifi.distributed.cache.server.EvictionPolicy; +import static org.junit.Assert.fail; +import org.junit.Test; + +public class TestPersistentMapCache { + + /** + * Test OverlappingFileLockException is caught when persistent path is duplicated. + */ + @Test(expected=OverlappingFileLockException.class) + public void testDuplicatePersistenceDirectory() { + try { + File duplicatedFilePath = new File("/tmp/path1"); + final MapCache cache = new SimpleMapCache("simpleCache", 2, EvictionPolicy.FIFO); + PersistentMapCache pmc1 = new PersistentMapCache("id1", duplicatedFilePath, cache); + PersistentMapCache pmc2 = new PersistentMapCache("id2", duplicatedFilePath, cache); + } catch (IOException ex) { + fail("Unexpected IOException thrown: " + ex.getMessage()); + } + } +} \ No newline at end of file