java.nio.channels.OverlappingFileLockException Java Examples
The following examples show how to use
java.nio.channels.OverlappingFileLockException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Storage.java From hadoop-gpu with Apache License 2.0 | 6 votes |
/** * Attempts to acquire an exclusive lock on the storage. * * @return A lock object representing the newly-acquired lock or * <code>null</code> if storage is already locked. * @throws IOException if locking fails. */ FileLock tryLock() throws IOException { File lockF = new File(root, STORAGE_FILE_LOCK); lockF.deleteOnExit(); RandomAccessFile file = new RandomAccessFile(lockF, "rws"); FileLock res = null; try { res = file.getChannel().tryLock(); } catch(OverlappingFileLockException oe) { file.close(); return null; } catch(IOException e) { LOG.info(StringUtils.stringifyException(e)); file.close(); throw e; } return res; }
Example #2
Source File: DefaultMessageStoreTest.java From rocketmq-read with Apache License 2.0 | 6 votes |
@Test(expected = OverlappingFileLockException.class) public void test_repate_restart() throws Exception { QUEUE_TOTAL = 1; MessageBody = StoreMessage.getBytes(); MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); messageStoreConfig.setMapedFileSizeCommitLog(1024 * 8); messageStoreConfig.setMapedFileSizeConsumeQueue(1024 * 4); messageStoreConfig.setMaxHashSlotNum(100); messageStoreConfig.setMaxIndexNum(100 * 10); MessageStore master = new DefaultMessageStore(messageStoreConfig, null, new MyMessageArrivingListener(), new BrokerConfig()); boolean load = master.load(); assertTrue(load); try { master.start(); master.start(); } finally { master.shutdown(); master.destroy(); } }
Example #3
Source File: TailableFile.java From apm-agent-java with Apache License 2.0 | 6 votes |
public TailableFile(File file) throws IOException { this.file = file; stateFile = new File(file + ".state"); stateFileChannel = FileChannel.open(stateFile.toPath(), CREATE, READ, WRITE); try { stateFileLock = stateFileChannel.tryLock(); if (stateFileLock == null) { throw new IllegalStateException("This file is currently locked by another process: " + stateFile); } } catch (OverlappingFileLockException e) { throw new IllegalStateException("This file is currently locked by this process: " + stateFile, e); } Properties state = readState(); if (!state.isEmpty()) { restoreState(state); } else { tryOpenFile(); } }
Example #4
Source File: JpomApplicationEvent.java From Jpom with MIT License | 6 votes |
/** * 锁住进程文件 * * @throws IOException IO */ private void lockFile() throws IOException { this.fileOutputStream = new FileOutputStream(ConfigBean.getInstance().getPidFile(), true); this.fileChannel = fileOutputStream.getChannel(); while (true) { try { lock = fileChannel.lock(); break; } catch (OverlappingFileLockException | IOException ignored) { } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } }
Example #5
Source File: DefaultMessageStoreTest.java From DDMQ with Apache License 2.0 | 6 votes |
@Test(expected = OverlappingFileLockException.class) public void test_repate_restart() throws Exception { long totalMsgs = 100; QUEUE_TOTAL = 1; MessageBody = StoreMessage.getBytes(); MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); messageStoreConfig.setMapedFileSizeCommitLog(1024 * 8); messageStoreConfig.setMapedFileSizeConsumeQueue(1024 * 4); messageStoreConfig.setMaxHashSlotNum(100); messageStoreConfig.setMaxIndexNum(100 * 10); MessageStore master = new DefaultMessageStore(messageStoreConfig, null, new MyMessageArrivingListener(), new BrokerConfig()); boolean load = master.load(); assertTrue(load); try { master.start(); master.start(); } finally { master.shutdown(); master.destroy(); } }
Example #6
Source File: DefaultMessageStoreTest.java From DDMQ with Apache License 2.0 | 6 votes |
@Test(expected = OverlappingFileLockException.class) public void test_repate_restart() throws Exception { long totalMsgs = 100; QUEUE_TOTAL = 1; MessageBody = StoreMessage.getBytes(); MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); messageStoreConfig.setMapedFileSizeCommitLog(1024 * 8); messageStoreConfig.setMapedFileSizeConsumeQueue(1024 * 4); messageStoreConfig.setMaxHashSlotNum(100); messageStoreConfig.setMaxIndexNum(100 * 10); MessageStore master = new DefaultMessageStore(messageStoreConfig, null, new MyMessageArrivingListener(), new BrokerConfig()); boolean load = master.load(); assertTrue(load); try { master.start(); master.start(); } finally { master.shutdown(); master.destroy(); } }
Example #7
Source File: Locker.java From orion.server with Eclipse Public License 1.0 | 6 votes |
public synchronized boolean lock() throws IOException { raFile = new RandomAccessFile(lockFile, "rw"); //$NON-NLS-1$ try { /* * fix for bug http://bugs.sun.com/view_bug.do?bug_id=6628575 and * https://bugs.eclipse.org/bugs/show_bug.cgi?id=44735#c17 */ fileLock = raFile.getChannel().tryLock(0, 1, false); } catch (OverlappingFileLockException e) { // handle it as null result fileLock = null; } finally { if (fileLock != null) return true; raFile.close(); raFile = null; } return false; }
Example #8
Source File: DirectoryLockManager.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void removeInvalidLock(File lockDir) { try { boolean revokeLock = false; File lockedFile = new File(lockDir, LOCK_FILE_NAME); try (RandomAccessFile raf = new RandomAccessFile(lockedFile, "rw")) { FileLock fileLock = raf.getChannel().tryLock(); if (fileLock != null) { logger.warn("Removing invalid lock {}", getLockedBy()); fileLock.release(); revokeLock = true; } } catch (OverlappingFileLockException exc) { // lock is still valid } if (revokeLock) { revokeLock(); } } catch (IOException e) { logger.warn(e.toString(), e); } }
Example #9
Source File: TestRaftServerWithGrpc.java From ratis with Apache License 2.0 | 6 votes |
@Test public void testServerRestartOnException() throws Exception { RaftProperties properties = new RaftProperties(); final MiniRaftClusterWithGrpc cluster = MiniRaftClusterWithGrpc.FACTORY.newCluster(1, properties); cluster.start(); RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId(); GrpcConfigKeys.Server.setPort(properties, cluster.getLeader().getServerRpc().getInetSocketAddress().getPort()); // Create a raft server proxy with server rpc bound to a different address // compared to leader. This helps in locking the raft storage directory to // be used by next raft server proxy instance. final StateMachine stateMachine = cluster.getLeader().getStateMachine(); ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, properties, null); // Close the server rpc for leader so that new raft server can be bound to it. cluster.getLeader().getServerRpc().close(); // Create a raft server proxy with server rpc bound to same address as // the leader. This step would fail as the raft storage has been locked by // the raft server proxy created earlier. Raft server proxy should close // the rpc server on failure. testFailureCase("start a new server with the same address", () -> ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, properties, null).start(), IOException.class, OverlappingFileLockException.class); // Try to start a raft server rpc at the leader address. cluster.getServer(leaderId).getFactory().newRaftServerRpc(cluster.getServer(leaderId)); }
Example #10
Source File: AsyncFileLock.java From DataHubSystem with GNU Affero General Public License v3.0 | 6 votes |
/** * Locks the file, with a timeout (non-blocking). * @param timeout_ms timeout duration in milliseconds. * @throws IOException I/O exception occured. * @throws InterruptedException current thread interrupted. * @throws TimeoutException failed to obtain lock. */ public void obtain(long timeout_ms) throws IOException, InterruptedException, TimeoutException { Long quit_time = System.currentTimeMillis() + timeout_ms; if (fileLock != null && fileLock.isValid()) { // lock has already been obtained. return; } do { try { fileLock = fileToLock.tryLock(); return; } catch (OverlappingFileLockException e) { Thread.sleep(1000); } } while (System.currentTimeMillis() < quit_time); throw new TimeoutException(); }
Example #11
Source File: PersistentCacheTest.java From ehcache3 with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("try") public void testPersistentCachesColliding() throws Exception { File folder = temporaryFolder.newFolder(testName.getMethodName()); try (PersistentCacheManager cm = CacheManagerBuilder.newCacheManagerBuilder() .with(new CacheManagerPersistenceConfiguration(folder)).build(true)) { CacheManagerBuilder.newCacheManagerBuilder() .with(new CacheManagerPersistenceConfiguration(folder)) .build(true) .close(); Assert.fail("Expected StateTransitionException"); } catch (StateTransitionException e) { assertThat(e.getCause().getMessage(), containsString("Persistence directory already locked by this process")); assertThat(e.getCause().getCause(), instanceOf(OverlappingFileLockException.class)); } }
Example #12
Source File: Log.java From mt-flume with Apache License 2.0 | 6 votes |
/** * Attempts to acquire an exclusive lock on the directory. * * @return A lock object representing the newly-acquired lock or * <code>null</code> if directory is already locked. * @throws IOException if locking fails. */ @SuppressWarnings("resource") private FileLock tryLock(File dir) throws IOException { File lockF = new File(dir, FILE_LOCK); lockF.deleteOnExit(); RandomAccessFile file = new RandomAccessFile(lockF, "rws"); FileLock res = null; try { res = file.getChannel().tryLock(); } catch(OverlappingFileLockException oe) { file.close(); return null; } catch(IOException e) { LOGGER.error("Cannot create lock on " + lockF, e); file.close(); throw e; } return res; }
Example #13
Source File: Storage.java From RDFS with Apache License 2.0 | 6 votes |
/** * Attempts to acquire an exclusive lock on the storage. * * @return A lock object representing the newly-acquired lock or * <code>null</code> if storage is already locked. * @throws IOException if locking fails. */ FileLock tryLock() throws IOException { File lockF = new File(root, STORAGE_FILE_LOCK); lockF.deleteOnExit(); RandomAccessFile file = new RandomAccessFile(lockF, "rws"); FileLock res = null; try { res = file.getChannel().tryLock(); } catch(OverlappingFileLockException oe) { file.close(); return null; } catch(IOException e) { LOG.error("Cannot create lock on " + lockF, e); file.close(); throw e; } return res; }
Example #14
Source File: DefaultMessageStoreTest.java From rocketmq with Apache License 2.0 | 6 votes |
@Test(expected = OverlappingFileLockException.class) public void test_repeat_restart() throws Exception { QUEUE_TOTAL = 1; MessageBody = StoreMessage.getBytes(); MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); messageStoreConfig.setMappedFileSizeCommitLog(1024 * 8); messageStoreConfig.setMappedFileSizeConsumeQueue(1024 * 4); messageStoreConfig.setMaxHashSlotNum(100); messageStoreConfig.setMaxIndexNum(100 * 10); MessageStore master = new DefaultMessageStore(messageStoreConfig, null, new MyMessageArrivingListener(), new BrokerConfig()); boolean load = master.load(); assertTrue(load); try { master.start(); master.start(); } finally { master.shutdown(); master.destroy(); } }
Example #15
Source File: JavaWriteToFileUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void whenTryToLockFile_thenItShouldBeLocked() throws IOException { final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); final FileChannel channel = stream.getChannel(); FileLock lock = null; try { lock = channel.tryLock(); } catch (final OverlappingFileLockException e) { stream.close(); channel.close(); } stream.writeChars("test lock"); lock.release(); stream.close(); channel.close(); }
Example #16
Source File: MainWithNailgun.java From buck with Apache License 2.0 | 6 votes |
/** * To prevent 'buck kill' from deleting resources from underneath a 'live' buckd we hold on to the * FileLock for the entire lifetime of the process. We depend on the fact that on Linux and MacOS * Java FileLock is implemented using the same mechanism as the Python fcntl.lockf method. Should * this not be the case we'll simply have a small race between buckd start and `buck kill`. */ private static void obtainResourceFileLock() { if (resourcesFileLock != null) { return; } String resourceLockFilePath = System.getProperties().getProperty("buck.resource_lock_path"); if (resourceLockFilePath == null) { // Running from ant, no resource lock needed. return; } try { // R+W+A is equivalent to 'a+' in Python (which is how the lock file is opened in Python) // because WRITE in Java does not imply truncating the file. FileChannel fileChannel = FileChannel.open( Paths.get(resourceLockFilePath), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); resourcesFileLock = fileChannel.tryLock(0L, Long.MAX_VALUE, true); } catch (IOException | OverlappingFileLockException e) { LOG.warn(e, "Error when attempting to acquire resources file lock."); } }
Example #17
Source File: Fetcher.java From tez with Apache License 2.0 | 6 votes |
private FileLock getLock() throws OverlappingFileLockException, InterruptedException, IOException { File lockFile = localFs.pathToFile(new Path(lockPath, host + ".lock")); final boolean created = lockFile.createNewFile(); if (created == false && !lockFile.exists()) { // bail-out cleanly return null; } // invariant - file created (winner writes to this file) // caveat: closing lockChannel does close the file (do not double close) // JDK7 - TODO: use AsynchronousFileChannel instead of RandomAccessFile FileChannel lockChannel = new RandomAccessFile(lockFile, "rws") .getChannel(); FileLock xlock = null; xlock = lockChannel.tryLock(0, Long.MAX_VALUE, false); if (xlock != null) { return xlock; } lockChannel.close(); return null; }
Example #18
Source File: DefaultMessageStoreTest.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
@Test(expected = OverlappingFileLockException.class) public void test_repate_restart() throws Exception { QUEUE_TOTAL = 1; MessageBody = StoreMessage.getBytes(); MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); messageStoreConfig.setMapedFileSizeCommitLog(1024 * 8); messageStoreConfig.setMapedFileSizeConsumeQueue(1024 * 4); messageStoreConfig.setMaxHashSlotNum(100); messageStoreConfig.setMaxIndexNum(100 * 10); MessageStore master = new DefaultMessageStore(messageStoreConfig, null, new MyMessageArrivingListener(), new BrokerConfig()); boolean load = master.load(); assertTrue(load); try { master.start(); master.start(); } finally { master.shutdown(); master.destroy(); } }
Example #19
Source File: FileLockBasedLockChecker.java From Java-Thread-Affinity with Apache License 2.0 | 5 votes |
private boolean isLockFree(File file, int id) { //if no file exists - nobody has the lock for sure if (!file.exists()) { return true; } //do we have the lock already? LockReference existingLock = locks[id]; if (existingLock != null) { return false; } //does another process have the lock? try { FileChannel fc = FileChannel.open(file.toPath(), WRITE); FileLock fileLock = fc.tryLock(); if (fileLock == null) { return false; } } catch (IOException | OverlappingFileLockException e) { LOGGER.error(String.format("Exception occurred whilst trying to check lock on file %s : %s%n", file.getAbsolutePath(), e)); } //file is present but nobody has it locked - delete it LOGGER.info(String.format("Deleting %s as nobody has the lock", file.getAbsolutePath())); file.delete(); return true; }
Example #20
Source File: TestPersistentMapCache.java From nifi with Apache License 2.0 | 5 votes |
/** * 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()); } }
Example #21
Source File: FileChannelImpl.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private void checkList(long position, long size) throws OverlappingFileLockException { assert Thread.holdsLock(lockList); for (FileLock fl: lockList) { if (fl.overlaps(position, size)) { throw new OverlappingFileLockException(); } } }
Example #22
Source File: FileChannelImpl.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
private void checkList(long position, long size) throws OverlappingFileLockException { assert Thread.holdsLock(lockList); for (FileLock fl: lockList) { if (fl.overlaps(position, size)) { throw new OverlappingFileLockException(); } } }
Example #23
Source File: GridFileLock.java From ignite with Apache License 2.0 | 5 votes |
/** * Performs a lock (shared or exclusive) on a file, that * this lock instance was constructed with. * * @param shared Whether a lock is shared (non-exclusive). * @throws IgniteCheckedException If failed to perform locking. The file remains open. */ public void lock(boolean shared) throws IgniteCheckedException { if (fileLock != null) throw new IgniteCheckedException("Already locked [lockFile=" + file + ']'); try { fileLock = raFile.getChannel().tryLock(0, Long.MAX_VALUE, shared); if (fileLock == null) throw new IgniteCheckedException("Failed to get exclusive lock on lock file [lockFile=" + file + ']'); } catch (IOException | OverlappingFileLockException e) { throw new IgniteCheckedException("Failed to get exclusive lock on lock file [lockFile=" + file + ']', e); } }
Example #24
Source File: FileChannelImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private void checkList(long position, long size) throws OverlappingFileLockException { assert Thread.holdsLock(lockList); for (FileLock fl: lockList) { if (fl.overlaps(position, size)) { throw new OverlappingFileLockException(); } } }
Example #25
Source File: FileLockManagerTest.java From terracotta-platform with Apache License 2.0 | 5 votes |
@Test(expected = OverlappingFileLockException.class) public void locked() throws Exception { Path directory = temporaryFolder.newFolder().toPath(); FileLockManager lockManager = new FileLockManager(); try (CloseLock closeLock = lockManager.lockOrFail(directory)) { assertNotNull(closeLock); lockManager.lockOrFail(directory); } }
Example #26
Source File: SCCRefreshLock.java From uyuni with GNU General Public License v2.0 | 5 votes |
/** * tries to obtain the lock or throws an exception */ public static void tryGetLock() { try { f = new File(REFRESH_FILE_LOCKED_PATH); synchronized (f) { // create the lock file channel = new RandomAccessFile(f, "rw").getChannel(); // create it with correct user FileSystem fileSystem = FileSystems.getDefault(); UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService(); UserPrincipal rootUser = service.lookupPrincipalByName("root"); UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat"); if (Files.getOwner(f.toPath(), LinkOption.NOFOLLOW_LINKS).equals(rootUser)) { Files.setOwner(f.toPath(), tomcatUser); } lock = channel.tryLock(); if (lock == null) { // File is lock by other application channel.close(); log.warn("SCC refresh is already running."); throw new OverlappingFileLockException(); } log.info("Got the Lock for scc refresh"); // Add on exit handler to release lock when application shutdown OnExitHandler onExitHandler = new OnExitHandler(); Runtime.getRuntime().addShutdownHook(onExitHandler); } } catch (IOException e) { throw new RuntimeException("Could not start process.", e); } }
Example #27
Source File: OffsetCheckpoint.java From kcache with Apache License 2.0 | 5 votes |
private FileLock tryLock(final FileChannel channel) throws IOException { try { return channel.tryLock(); } catch (final OverlappingFileLockException e) { return null; } }
Example #28
Source File: JsonReader.java From jsondb-core with MIT License | 5 votes |
public JsonReader(JsonDBConfig dbConfig, File collectionFile) throws IOException { this.collectionFile = collectionFile; this.lockFilesLocation = new File(collectionFile.getParentFile(), "lock"); this.fileLockLocation = new File(lockFilesLocation, collectionFile.getName() + ".lock"); if(!lockFilesLocation.exists()) { lockFilesLocation.mkdirs(); } if(!fileLockLocation.exists()) { fileLockLocation.createNewFile(); } CharsetDecoder decoder = dbConfig.getCharset().newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); raf = new RandomAccessFile(fileLockLocation, "rw"); channel = raf.getChannel(); try { lock = channel.lock(); } catch (IOException | OverlappingFileLockException e) { try { channel.close(); raf.close(); } catch (IOException e1) { logger.error("Failed while closing RandomAccessFile for collection file {}", collectionFile.getName()); } throw new JsonFileLockException("JsonReader failed to obtain a file lock for file " + fileLockLocation, e); } fis = new FileInputStream(collectionFile); isr = new InputStreamReader(fis, decoder); reader = new BufferedReader(isr); }
Example #29
Source File: AsyncFileLock.java From DataHubSystem with GNU Affero General Public License v3.0 | 5 votes |
/** * Returns wether the underlying file is locked, non-blocking. * @return true if the underlying file is locked. * @throws ClosedChannelException the underlying FileChannel has been closed. * @throws NonWritableChannelException the underlying FileChannel * hasn't been opened with the WRITE OpenOption. */ public boolean isLocked() throws IOException { try (FileLock fl = fileToLock.tryLock()) { return fl != null; } catch (OverlappingFileLockException e) { return true; } }
Example #30
Source File: FileChannelImpl.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
private void checkList(long position, long size) throws OverlappingFileLockException { assert Thread.holdsLock(lockList); for (FileLock fl: lockList) { if (fl.overlaps(position, size)) { throw new OverlappingFileLockException(); } } }