public static Lock recoverLock(QueueLock qlock, byte[] data) { SortedMap<Long,byte[]> entries = qlock.getEarlierEntries(Long.MAX_VALUE); for (Entry<Long,byte[]> entry : entries.entrySet()) { ParsedLock parsed = new ParsedLock(entry.getValue()); if (Arrays.equals(data, parsed.getUserData())) { switch (parsed.getType()) { case READ: return new ReadLock(qlock, parsed.getUserData(), entry.getKey()); case WRITE: return new WriteLock(qlock, parsed.getUserData(), entry.getKey()); } } } return null; }
@Override public boolean tryLock() { if (entry == -1) { entry = qlock.addEntry(new ParsedLock(this.lockType(), this.userData).getLockData()); log.info("Added lock entry {} userData {} lockTpye {}", entry, new String(this.userData, UTF_8), lockType()); } SortedMap<Long,byte[]> entries = qlock.getEarlierEntries(entry); for (Entry<Long,byte[]> entry : entries.entrySet()) { ParsedLock parsed = new ParsedLock(entry.getValue()); if (entry.getKey().equals(this.entry)) return true; if (parsed.type == LockType.WRITE) return false; } throw new IllegalStateException("Did not find our own lock in the queue: " + this.entry + " userData " + new String(this.userData, UTF_8) + " lockType " + lockType()); }
@Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { long now = System.currentTimeMillis(); long returnTime = now + TimeUnit.MILLISECONDS.convert(time, unit); while (returnTime > now) { if (tryLock()) return true; // TODO: do something better than poll - ACCUMULO-1310 UtilWaitThread.sleep(100); now = System.currentTimeMillis(); } return false; }
static public Lock recoverLock(QueueLock qlock, byte[] data) { SortedMap<Long,byte[]> entries = qlock.getEarlierEntries(Long.MAX_VALUE); for (Entry<Long,byte[]> entry : entries.entrySet()) { ParsedLock parsed = new ParsedLock(entry.getValue()); if (Arrays.equals(data, parsed.getUserData())) { switch (parsed.getType()) { case READ: return new ReadLock(qlock, parsed.getUserData(), entry.getKey()); case WRITE: return new WriteLock(qlock, parsed.getUserData(), entry.getKey()); } } } return null; }
@Override public boolean tryLock() { if (entry == -1) { entry = qlock.addEntry(new ParsedLock(this.lockType(), this.userData).getLockData()); log.info("Added lock entry " + entry + " userData " + new String(this.userData, UTF_8) + " lockType " + lockType()); } SortedMap<Long,byte[]> entries = qlock.getEarlierEntries(entry); for (Entry<Long,byte[]> entry : entries.entrySet()) { ParsedLock parsed = new ParsedLock(entry.getValue()); if (entry.getKey().equals(this.entry)) return true; if (parsed.type == LockType.WRITE) return false; } throw new IllegalStateException("Did not find our own lock in the queue: " + this.entry + " userData " + new String(this.userData, UTF_8) + " lockType " + lockType()); }
@Override public Lock readLock() { return new ReadLock(qlock, data); }
@Override public void lock() { while (true) { try { if (tryLock(1, TimeUnit.DAYS)) return; } catch (InterruptedException ex) { // ignored } } }
@Override public void unlock() { if (entry == -1) return; log.debug("Removing lock entry " + entry + " userData " + new String(this.userData, UTF_8) + " lockType " + lockType()); qlock.removeEntry(entry); entry = -1; }
@Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { long now = System.currentTimeMillis(); long returnTime = now + TimeUnit.MILLISECONDS.convert(time, unit); while (returnTime > now) { if (tryLock()) return true; // TODO: do something better than poll - ACCUMULO-1310 UtilWaitThread.sleep(100); now = System.currentTimeMillis(); } return false; }
@Override public void lockInterruptibly() throws InterruptedException { while (!Thread.currentThread().isInterrupted()) { if (tryLock(100, TimeUnit.MILLISECONDS)) return; } }
@Override public Lock readLock() { return new ReadLock(qlock, data); }
@Override public void lockInterruptibly() throws InterruptedException { while (!Thread.currentThread().isInterrupted()) { if (tryLock(100, TimeUnit.MILLISECONDS)) return; } }
@Override public void unlock() { if (entry == -1) return; log.debug("Removing lock entry {} userData {} lockType {}", entry, new String(this.userData, UTF_8), lockType()); qlock.removeEntry(entry); entry = -1; }
@Override public void lock() { while (true) { try { if (tryLock(1, TimeUnit.DAYS)) return; } catch (InterruptedException ex) { // ignored } } }