2626public class FileLockBasedLockChecker implements LockChecker {
2727
2828 private static final int MAX_LOCK_RETRIES = 5 ;
29- private static final SimpleDateFormat df = new SimpleDateFormat ("yyyy.MM" + ".dd 'at' HH:mm:ss z" );
29+ private static final ThreadLocal < SimpleDateFormat > dfTL = ThreadLocal . withInitial (() -> new SimpleDateFormat ("yyyy.MM" + ".dd 'at' HH:mm:ss z" ) );
3030 private static final FileAttribute <Set <PosixFilePermission >> LOCK_FILE_ATTRIBUTES = PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rw-rw-rw-" ));
3131 private static final Set <OpenOption > LOCK_FILE_OPEN_OPTIONS = new HashSet <>(Arrays .asList (READ , WRITE , CREATE , SYNC ));
3232 private static final Logger LOGGER = LoggerFactory .getLogger (FileLockBasedLockChecker .class );
@@ -54,7 +54,9 @@ public synchronized boolean isLockFree(int id) {
5454 // if we can acquire a shared lock, nobody has an exclusive lock
5555 try (final FileLock fileLock = channel .tryLock (0 , Long .MAX_VALUE , true )) {
5656 if (fileLock != null && fileLock .isValid ()) {
57- lockFile .delete (); // clean up the orphaned lock file
57+ if (!lockFile .delete ()) { // try and clean up the orphaned lock file
58+ LOGGER .debug ("Couldn't delete orphaned lock file " + lockFile );
59+ }
5860 return true ;
5961 } else {
6062 // another process has an exclusive lock
@@ -102,7 +104,7 @@ public synchronized boolean obtainLock(int id, String metaInfo) throws IOExcepti
102104 */
103105 private LockReference tryAcquireLockOnFile (int id , String metaInfo ) throws IOException , ConcurrentLockFileDeletionException {
104106 final File lockFile = toFile (id );
105- final FileChannel fileChannel = FileChannel .open (lockFile .toPath (), LOCK_FILE_OPEN_OPTIONS , LOCK_FILE_ATTRIBUTES );
107+ final FileChannel fileChannel = FileChannel .open (lockFile .toPath (), LOCK_FILE_OPEN_OPTIONS , LOCK_FILE_ATTRIBUTES ); // NOSONAR
106108 final FileLock fileLock = fileChannel .tryLock (0 , Long .MAX_VALUE , false );
107109 if (fileLock == null ) {
108110 // someone else has a lock (exclusive or shared), fail to acquire
@@ -122,7 +124,7 @@ private LockReference tryAcquireLockOnFile(int id, String metaInfo) throws IOExc
122124 }
123125
124126 private void writeMetaInfoToFile (FileChannel fc , String metaInfo ) throws IOException {
125- byte [] content = String .format ("%s%n%s" , metaInfo , df .format (new Date ())).getBytes ();
127+ byte [] content = String .format ("%s%n%s" , metaInfo , dfTL . get () .format (new Date ())).getBytes ();
126128 ByteBuffer buffer = ByteBuffer .wrap (content );
127129 while (buffer .hasRemaining ()) {
128130 fc .write (buffer );
0 commit comments