In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use BitCaskLock in java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Order
This paper mainly studies BitCaskLock.
BitCaskLock
Bitcask-java/src/main/java/com/trifork/bitcask/BitCaskLock.java
Public class BitCaskLock {public static enum Stale {OK, NOT_STALE} public static enum Type {WRITE, MERGE; private String type_name () {if (this = = MERGE) return "merge"; if (this = = WRITE) return "write" Throw new RuntimeException ();}} private RandomAccessFile file; private boolean is_write_lock; private File filename; private BitCaskLock (RandomAccessFile file, File filename, boolean isWriteLock) {this.file = file; this.filename = filename; this.is_write_lock = isWriteLock } public static BitCaskLock acquire (Type type, File dirname) throws IOException {File lock_filename = lock_filename (type, dirname); foo: do {try {BitCaskLock lock = lock_acquire (lock_filename, true) String lock_contents = Integer.toString (OS.getpid ()) + "\ n"; lock.write_data (ByteString.copyFromUtf8 (lock_contents)); return lock } catch (FileAlreadyExistsException e) {delete_stale_lock (lock_filename); continue foo;}} while (false); return null } public void release () throws IOException {if (file! = null) {if (is_write_lock) {file.close (); filename.delete () }} file = null;} / /.}
The acquire method of BitCaskLock first obtains BitCaskLock through lock_acquire, and if FileAlreadyExistsException occurs, executes delete_stale_lock; and its release method executes file.close () and filename.delete () against is_write_lock.
Lock_acquire
Bitcask-java/src/main/java/com/trifork/bitcask/BitCaskLock.java
Private static BitCaskLock lock_acquire (File lockFilename, boolean is_write_lock) throws IOException {if (is_write_lock) {if (lockFilename.createNewFile () = = false) {/ / file already exists, so we fail! Throw new FileAlreadyExistsException (lockFilename);}} RandomAccessFile f = new RandomAccessFile (lockFilename, is_write_lock? "rws": "r"); return new BitCaskLock (f, lockFilename, is_write_lock);}
The lock_acquire method executes lockFilename.createNewFile () against is_write_lock, and throws FileAlreadyExistsException if it is false
Delete_stale_lock
Bitcask-java/src/main/java/com/trifork/bitcask/BitCaskLock.java
Public static Stale delete_stale_lock (Type type, File dirname) throws IOException {return delete_stale_lock (lock_filename (type, dirname));} private static Stale delete_stale_lock (File lockFilename) throws IOException {BitCaskLock l = null; try {l = lock_acquire (lockFilename, false) } catch (FileNotFoundException e) {return Stale.OK;} catch (IOException e) {return Stale.NOT_STALE;} try {int pid = l.read_lock_data_pid () If (OS.pid_exists (pid)) {return Stale.NOT_STALE;} else {lockFilename.delete (); return Stale.OK } catch (IOException e) {return Stale.NOT_STALE;} finally {l.release ();}}
The delete_stale_lock method executes lock_acquire first, returns Stale.OK if FileNotFoundException occurs, returns Stale.NOT_STALE; if IOException occurs, otherwise gets pid through l.read_lock_data_pid (), returns Stale.NOT_STALE if OS.pid_exists (pid), executes lockFilename.delete () otherwise, returns Stale.OK; and finally executes l.release ()
Summary
The acquire method of BitCaskLock first obtains BitCaskLock through lock_acquire, and if FileAlreadyExistsException occurs, executes delete_stale_lock; and its release method executes file.close () and filename.delete () against is_write_lock.
This is the end of the content of "how to use BitCaskLock in java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.