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 <mattyb149@apache.org>

This closes #2192
This commit is contained in:
Mark Owens 2017-10-03 10:48:42 -04:00 committed by Matthew Burgess
parent 0876cf12b1
commit 701597775f
2 changed files with 55 additions and 1 deletions

View File

@ -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<MapWaliRecord> wali;
private final AtomicLong modifications = new AtomicLong(0L);
public PersistentMapCache(final String serviceIdentifier, final File persistencePath, final MapCache cacheToWrap) throws IOException {
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;
}

View File

@ -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());
}
}
}