private void writeLevel0Table(MemTable mem, VersionEdit edit, Version base) throws IOException { Preconditions.checkState(mutex.isHeldByCurrentThread()); // skip empty mem table if (mem.isEmpty()) { return; } // write the memtable to a new sstable long fileNumber = versions.getNextFileNumber(); pendingOutputs.add(fileNumber); mutex.unlock(); FileMetaData meta; try { meta = buildTable(mem, fileNumber); } finally { mutex.lock(); } pendingOutputs.remove(fileNumber); // Note that if file size is zero, the file has been deleted and // should not be added to the manifest. int level = 0; if (meta != null && meta.getFileSize() > 0) { Slice minUserKey = meta.getSmallest().getUserKey(); Slice maxUserKey = meta.getLargest().getUserKey(); if (base != null) { level = base.pickLevelForMemTableOutput(minUserKey, maxUserKey); } edit.addFile(level, meta); } }
private void installCompactionResults(CompactionState compact) throws IOException { Preconditions.checkState(mutex.isHeldByCurrentThread()); // Add compaction outputs compact.compaction.addInputDeletions(compact.compaction.getEdit()); int level = compact.compaction.getLevel(); for (FileMetaData output : compact.outputs) { compact.compaction.getEdit().addFile(level + 1, output); pendingOutputs.remove(output.getNumber()); } try { versions.logAndApply(compact.compaction.getEdit()); deleteObsoleteFiles(); } catch (IOException e) { // Compaction failed for some reason. Simply discard the work and try again later. // Discard any files we may have created during this failed compaction for (FileMetaData output : compact.outputs) { File file = new File(databaseDir, Filename.tableFileName(output.getNumber())); file.delete(); } compact.outputs.clear(); } }
/** * Pre-generate enough keys to reach the lookahead size, but only if there are more than the * lookaheadThreshold to be generated, so that the Bloom filter does not have to be regenerated * that often. * * <p>The returned mutable list of keys must be inserted into the basic key chain. */ private List<DeterministicKey> maybeLookAhead( DeterministicKey parent, int issued, int lookaheadSize, int lookaheadThreshold) { checkState(lock.isHeldByCurrentThread()); final int numChildren = hierarchy.getNumChildren(parent.getPath()); final int needed = issued + lookaheadSize + lookaheadThreshold - numChildren; if (needed <= lookaheadThreshold) return new ArrayList<DeterministicKey>(); log.info( "{} keys needed for {} = {} issued + {} lookahead size + {} lookahead threshold - {} num children", needed, parent.getPathAsString(), issued, lookaheadSize, lookaheadThreshold, numChildren); List<DeterministicKey> result = new ArrayList<DeterministicKey>(needed); long now = System.currentTimeMillis(); int nextChild = numChildren; for (int i = 0; i < needed; i++) { DeterministicKey key = HDKeyDerivation.deriveThisOrNextChildKey(parent, nextChild); key = key.getPubOnly(); hierarchy.putKey(key); result.add(key); nextChild = key.getChildNumber().num() + 1; } log.info("Took {} msec", System.currentTimeMillis() - now); return result; }
/** * Fills the QueryDescriptor object that contains all queries used by the push listener framework * for the given registryTableName. * * @param registryTableName the name of the table storing information about the push listener * entries * @param clusterSize number of the members in the cluster * @param serverIndex unique index of the instance in the cluster * @throws com.funambol.pushlistener.service.registry.dao.DataAccessException */ private void fillQueryDescriptor(String registryTableName, int clusterSize, int serverIndex) throws DataAccessException { try { boolean myLock = lock.tryLock(5, TimeUnit.SECONDS); if (!myLock) { throw new DataAccessException( "Timeout expired waiting for entering a QueryDescriptor filling phase."); } if (queryDictionary.containsKey(registryTableName)) return; queryDesc = new QueryDescriptor(); discoverExtraProperties(registryTableName); createQueries(registryTableName, clusterSize, serverIndex); queryDictionary.put(registryTableName, queryDesc); columnSuffix = null; placeholderSuffix = null; updateSuffix = null; } catch (Exception ex) { throw new DataAccessException("Error filling QueryDescriptor object.", ex); } finally { if (lock.isHeldByCurrentThread()) { lock.unlock(); } } }
@JRubyMethod public RubyBoolean try_lock(ThreadContext context) { if (lock.isHeldByCurrentThread()) { return context.runtime.getFalse(); } return context.runtime.newBoolean(context.getThread().tryLock(lock)); }
private void compactMemTableInternal() throws IOException { Preconditions.checkState(mutex.isHeldByCurrentThread()); if (immutableMemTable == null) { return; } try { // Save the contents of the memtable as a new Table VersionEdit edit = new VersionEdit(); Version base = versions.getCurrent(); writeLevel0Table(immutableMemTable, edit, base); if (shuttingDown.get()) { throw new DatabaseShutdownException("Database shutdown during memtable compaction"); } // Replace immutable memtable with the generated Table edit.setPreviousLogNumber(0); edit.setLogNumber(log.getFileNumber()); // Earlier logs no longer needed versions.logAndApply(edit); immutableMemTable = null; deleteObsoleteFiles(); } finally { backgroundCondition.signalAll(); } }
/** * Acquire the lock so that the scene can be acted upon. * * <p>This returns null if the lock was just acquired, otherwise it returns {@link * Result.Status#SUCCESS} if the lock already belonged to that thread, or another instance (see * {@link Result#getStatus()}) if an error occurred. * * @param timeout the time to wait if another rendering is happening. * @return null if the lock was just acquire or another result depending on the state. * @throws IllegalStateException if the current context is different than the one owned by the * scene. */ private Result acquireLock(long timeout) { ReentrantLock lock = Bridge.getLock(); if (!lock.isHeldByCurrentThread()) { try { boolean acquired = lock.tryLock(timeout, TimeUnit.MILLISECONDS); if (!acquired) { return ERROR_TIMEOUT.createResult(); } } catch (InterruptedException e) { return ERROR_LOCK_INTERRUPTED.createResult(); } } else { // This thread holds the lock already. Checks that this wasn't for a different context. // If this is called by init, mContext will be null and so should sCurrentContext // anyway if (mContext != sCurrentContext) { throw new IllegalStateException( "Acquiring different scenes from same thread without releases"); } return SUCCESS.createResult(); } return null; }
private void maybeScheduleCompaction() { Preconditions.checkState(mutex.isHeldByCurrentThread()); if (backgroundCompaction != null) { // Already scheduled } else if (shuttingDown.get()) { // DB is being shutdown; no more background compactions } else if (immutableMemTable == null && manualCompaction == null && !versions.needsCompaction()) { // No work to be done } else { backgroundCompaction = compactionExecutor.submit( new Callable<Void>() { @Override public Void call() throws Exception { try { backgroundCall(); } catch (DatabaseShutdownException ignored) { } catch (Throwable e) { backgroundException = e; } return null; } }); } }
public void putDown(Philosopher chosenOne, String leftOrRight) throws InterruptedException { if (reentrantLock.isHeldByCurrentThread()) { reentrantLock.unlock(); this.chopstickObject.setFill(Paint.valueOf("#28e7b2")); System.out.println(chosenOne + " put down " + leftOrRight + " chopstick"); isAvailable = true; } }
public void validateLocked() { if (!lock.isHeldByCurrentThread()) { if (!lock.isLocked()) { throw new RuntimeException(Thread.currentThread() + ": Not locked"); } else { throw new RuntimeException(Thread.currentThread() + ": Not owner, owner is " + owner); } } }
public boolean enterWhenUninterruptibly(Monitor.Guard var1, long var2, TimeUnit var4) { long var5 = var4.toNanos(var2); if (var1.monitor != this) { throw new IllegalMonitorStateException(); } else { ReentrantLock var7 = this.lock; long var8 = System.nanoTime() + var5; boolean var10 = var7.isHeldByCurrentThread(); boolean var11 = Thread.interrupted(); try { boolean var12; InterruptedException var13; if (this.fair || !var7.tryLock()) { var12 = false; do { try { var12 = var7.tryLock(var5, TimeUnit.NANOSECONDS); if (!var12) { boolean var28 = false; return var28; } } catch (InterruptedException var24) { var13 = var24; var11 = true; } var5 = var8 - System.nanoTime(); } while (!var12); } var12 = false; try { while (true) { try { var13 = var12 = var1.isSatisfied() || this.awaitNanos(var1, var5, var10); return (boolean) var13; } catch (InterruptedException var25) { var11 = true; var10 = false; var5 = var8 - System.nanoTime(); } } } finally { if (!var12) { var7.unlock(); } } } finally { if (var11) { Thread.currentThread().interrupt(); } } } }
/** * Checks that the lock is owned by the current thread and that the current context is the one * from this scene. * * @throws IllegalStateException if the current context is different than the one owned by the * scene, or if {@link #acquire(long)} was not called. */ protected void checkLock() { ReentrantLock lock = Bridge.getLock(); if (!lock.isHeldByCurrentThread()) { throw new IllegalStateException("scene must be acquired first. see #acquire(long)"); } if (sCurrentContext != mContext) { throw new IllegalStateException("Thread acquired a scene but is rendering a different one"); } }
private long recoverLogFile(long fileNumber, VersionEdit edit) throws IOException { Preconditions.checkState(mutex.isHeldByCurrentThread()); File file = new File(databaseDir, Filename.logFileName(fileNumber)); FileChannel channel = new FileInputStream(file).getChannel(); try { LogMonitor logMonitor = LogMonitors.logMonitor(); LogReader logReader = new LogReader(channel, logMonitor, true, 0); // Log(options_.info_log, "Recovering log #%llu", (unsigned long long) log_number); // Read all the records and add to a memtable long maxSequence = 0; MemTable memTable = null; for (Slice record = logReader.readRecord(); record != null; record = logReader.readRecord()) { SliceInput sliceInput = record.input(); // read header if (sliceInput.available() < 12) { logMonitor.corruption(sliceInput.available(), "log record too small"); continue; } long sequenceBegin = sliceInput.readLong(); int updateSize = sliceInput.readInt(); // read entries WriteBatchImpl writeBatch = readWriteBatch(sliceInput, updateSize); // apply entries to memTable if (memTable == null) { memTable = new MemTable(internalKeyComparator); } writeBatch.forEach(new InsertIntoHandler(memTable, sequenceBegin)); // update the maxSequence long lastSequence = sequenceBegin + updateSize - 1; if (lastSequence > maxSequence) { maxSequence = lastSequence; } // flush mem table if necessary if (memTable.approximateMemoryUsage() > options.writeBufferSize()) { writeLevel0Table(memTable, edit, null); memTable = null; } } // flush mem table if (memTable != null && !memTable.isEmpty()) { writeLevel0Table(memTable, edit, null); } return maxSequence; } finally { channel.close(); } }
/** Cleans up the scene after an action. */ public void release() { ReentrantLock lock = Bridge.getLock(); // with the use of finally blocks, it is possible to find ourself calling this // without a successful call to prepareScene. This test makes sure that unlock() will // not throw IllegalMonitorStateException. if (lock.isHeldByCurrentThread()) { tearDown(); lock.unlock(); } }
private void cleanupCompaction(CompactionState compactionState) { Preconditions.checkState(mutex.isHeldByCurrentThread()); if (compactionState.builder != null) { compactionState.builder.abandon(); } else { Preconditions.checkArgument(compactionState.outfile == null); } for (FileMetaData output : compactionState.outputs) { pendingOutputs.remove(output.getNumber()); } }
/** * Change the {@link RunState}. * * @param newval The new {@link RunState}. * @throws IllegalArgumentException if the argument is <code>null</code>. * @throws IllegalStateException if the state transition is not allowed. * @see RunState#isTransitionAllowed(RunState) */ public void setRunState(final RunState newval) { if (!lock.isHeldByCurrentThread()) throw new IllegalMonitorStateException(); if (newval == null) throw new IllegalArgumentException(); if (!runState.isTransitionAllowed(newval)) { throw new IllegalStateException("runState=" + runState + ", newValue=" + newval); } this.runState = newval; }
/** * Resets {@link Locale#getDefault()} to what it was when {@link #setDefault(Locale)} was called. * Should be called after you're done with using {@link Locale#getDefault()}. * * @return the default {@link Locale} that was set * @throws IllegalStateException if this thread wasn't the last one to successfully call {@link * #setDefault(Locale)} */ public static Locale resetDefaultLocale() { checkState( LOCK.isHeldByCurrentThread(), "Current thread: %s may not unlock: %s", Thread.currentThread().getName(), LOCK); try { Locale.setDefault(defaultLocale); return defaultLocale; } finally { LOCK.unlock(); } }
/** * Validate the write set of the named indices isolated transaction and merge down that write set * onto the corresponding unisolated indices but DOES NOT commit the data. The {@link RunState} is * NOT changed by this method. * * <p>For a single-phase commit the caller MUST hold an exclusive lock on the unisolated indices * on which this operation will write. * * <p>For a distributed transaction, the caller MUST hold a lock on the {@link * WriteExecutorService} for each {@link IDataService} on which the transaction has written. * * @param revisionTime The revision time assigned by a centralized transaction manager service * -or- ZERO (0L) IFF the transaction is read-only. * @throws IllegalStateException if the transaction is not active. * @throws ValidationError If the transaction can not be validated. * @throws IllegalMonitorStateException unless the caller holds the {@link #lock}. * @throws UnsupportedOperationException if the transaction is read-only. */ public void prepare(final long revisionTime) { if (readOnly) throw new UnsupportedOperationException(); if (!lock.isHeldByCurrentThread()) throw new IllegalMonitorStateException(); if (log.isInfoEnabled()) log.info("tx=" + this); if (!isActive()) { throw new IllegalStateException(NOT_ACTIVE); } if (!isEmptyWriteSet()) { try { /* * Validate against the current state of the various indices on * write the transaction has written. */ if (!validateWriteSets()) { throw new ValidationError(); } /* * Merge each isolated index into the global scope. This also * marks the tuples on which the transaction has written with * the [revisionTime]. This operation MUST succeed (at a logical * level) since we have already validated (neither read-write * nor write-write conflicts exist). * * Note: This MUST be run as an AbstractTask which declares the * unisolated indices so that has the appropriate locks on those * indices when it executes. The AbstractTask will either * succeed or fail. If it succeeds, then the tx will be made * restart-safe at the group commit. If it fails or if the group * commit fails, then the writes on the unisolated indices will * be discarded. */ mergeOntoGlobalState(revisionTime); } catch (ValidationError ex) { throw ex; } } }
@JRubyMethod public synchronized IRubyObject unlock(ThreadContext context) { Ruby runtime = context.getRuntime(); if (!lock.isLocked()) { throw runtime.newThreadError("Mutex is not locked"); } if (!lock.isHeldByCurrentThread()) { throw runtime.newThreadError("Mutex is not owned by calling thread"); } boolean hasQueued = lock.hasQueuedThreads(); context.getThread().unlock(lock); return hasQueued ? context.nil : this; }
private void deleteObsoleteFiles() { Preconditions.checkState(mutex.isHeldByCurrentThread()); // Make a set of all of the live files List<Long> live = newArrayList(this.pendingOutputs); for (FileMetaData fileMetaData : versions.getLiveFiles()) { live.add(fileMetaData.getNumber()); } for (File file : Filename.listFiles(databaseDir)) { FileInfo fileInfo = Filename.parseFileName(file); if (fileInfo == null) continue; long number = fileInfo.getFileNumber(); boolean keep = true; switch (fileInfo.getFileType()) { case LOG: keep = ((number >= versions.getLogNumber()) || (number == versions.getPrevLogNumber())); break; case DESCRIPTOR: // Keep my manifest file, and any newer incarnations' // (in case there is a race that allows other incarnations) keep = (number >= versions.getManifestFileNumber()); break; case TABLE: keep = live.contains(number); break; case TEMP: // Any temp files that are currently being written to must // be recorded in pending_outputs_, which is inserted into "live" keep = live.contains(number); break; case CURRENT: case DB_LOCK: case INFO_LOG: keep = true; break; } if (!keep) { if (fileInfo.getFileType() == FileType.TABLE) { tableCache.evict(number); } // todo info logging system needed // Log(options_.info_log, "Delete type=%d #%lld\n", // int(type), // static_cast < unsigned long long>(number)); file.delete(); } } }
private void cleanupCache() { assert cacheLock.isHeldByCurrentThread(); int currentMaxCapacity = maxCapacity.get(); // Don't create the iterator. if (cache.size() <= currentMaxCapacity) { return; } Iterator<?> itr = cache.entrySet().iterator(); while (cache.size() > currentMaxCapacity && itr.hasNext()) { itr.next(); itr.remove(); } }
@VisibleForTesting void acquireLock() { LOG.trace("Acquiring lock for '{}'", file); ReentrantLock lock = null; synchronized (DataStore.class) { lock = FILE_LOCKS.get(file); if (lock == null) { lock = new ReentrantLock(); FILE_LOCKS.put(file, lock); } else { Utils.checkState( !lock.isHeldByCurrentThread(), Utils.format("The current thread already has a lock on '{}'", file)); } } lock.lock(); LOG.trace("Acquired lock for '{}'", file); }
/** * @param key Key. * @param ver Version. */ public void onEntryEvicted(KeyCacheObject key, GridCacheVersion ver) { assert key != null; assert ver != null; assert lock.isHeldByCurrentThread(); // Only one thread can enter this method at a time. if (state() != MOVING) return; Map<KeyCacheObject, GridCacheVersion> evictHist0 = evictHist; if (evictHist0 != null) { GridCacheVersion ver0 = evictHist0.get(key); if (ver0 == null || ver0.isLess(ver)) { GridCacheVersion ver1 = evictHist0.put(key, ver); assert ver1 == ver0; } } }
/** * Cache preloader should call this method within partition lock. * * @param key Key. * @param ver Version. * @return {@code True} if preloading is permitted. */ public boolean preloadingPermitted(KeyCacheObject key, GridCacheVersion ver) { assert key != null; assert ver != null; assert lock.isHeldByCurrentThread(); // Only one thread can enter this method at a time. if (state() != MOVING) return false; Map<KeyCacheObject, GridCacheVersion> evictHist0 = evictHist; if (evictHist0 != null) { GridCacheVersion ver0 = evictHist0.get(key); // Permit preloading if version in history // is missing or less than passed in. return ver0 == null || ver0.isLess(ver); } return false; }
@TruffleBoundary public void pauseAllThreadsAndExecuteFromNonRubyThread(boolean deferred, SafepointAction action) { if (lock.isHeldByCurrentThread()) { throw new IllegalStateException("Re-entered SafepointManager"); } assert !runningThreads.contains(Thread.currentThread()); // Just wait to grab the lock, since we are not in the registered threads. lock.lock(); try { enterThread(); try { pauseAllThreadsAndExecute(null, false, action, deferred); } finally { leaveThread(); } } finally { lock.unlock(); } }
/** * call this method once you are done with the index writer. Once all consumers have returned * their instance, the writer will be closed and disposed * * @param writer * @throws CorruptIndexException * @throws IOException */ public void returnIndexWriter(IndexWriter writer) throws CorruptIndexException, IOException { if (!lock.isHeldByCurrentThread()) throw new IllegalStateException( "The calling thread isn't the one that obtained the writer initially"); if (writer != indexWriter) throw new IllegalStateException("The given index writer is not the current index writer"); if (indexWriter != null) { try { indexWriter.close(); } finally { indexWriter = null; try { if (IndexWriter.isLocked(directory)) { IndexWriter.unlock(directory); } } finally { lock.unlock(); } } } }
public boolean enterWhen(Monitor.Guard var1, long var2, TimeUnit var4) throws InterruptedException { long var5 = var4.toNanos(var2); if (var1.monitor != this) { throw new IllegalMonitorStateException(); } else { ReentrantLock var7 = this.lock; boolean var8 = var7.isHeldByCurrentThread(); if (this.fair || !var7.tryLock()) { long var9 = System.nanoTime() + var5; if (!var7.tryLock(var2, var4)) { return false; } var5 = var9 - System.nanoTime(); } boolean var26 = false; boolean var10 = true; boolean var11; try { var26 = var1.isSatisfied() || this.awaitNanos(var1, var5, var8); var10 = false; var11 = var26; } finally { if (!var26) { try { if (var10 && !var8) { this.signalNextWaiter(); } } finally { var7.unlock(); } } } return var11; } }
protected ReceiverEntry getReceiverEntry( Address sender, long seqno, boolean first, short conn_id) { ReceiverEntry entry = recv_table.get(sender); if (entry != null && entry.recv_conn_id == conn_id) return entry; recv_table_lock.lock(); try { entry = recv_table.get(sender); if (first) { if (entry == null) { entry = getOrCreateReceiverEntry(sender, seqno, conn_id); } else { // entry != null && win != null if (conn_id != entry.recv_conn_id) { if (log.isTraceEnabled()) log.trace( local_addr + ": conn_id=" + conn_id + " != " + entry.recv_conn_id + "; resetting receiver window"); recv_table.remove(sender); entry = getOrCreateReceiverEntry(sender, seqno, conn_id); } else {; } } } else { // entry == null && win == null OR entry != null && win == null OR entry != null && // win != null if (entry == null || entry.recv_conn_id != conn_id) { recv_table_lock.unlock(); sendRequestForFirstSeqno(sender, seqno); // drops the message and returns (see below) return null; } } return entry; } finally { if (recv_table_lock.isHeldByCurrentThread()) recv_table_lock.unlock(); } }
protected static boolean wasCalledDuringClassLoading() { if (LOCK.isHeldByCurrentThread()) return true; LOCK.lock(); try { StackTrace st = new StackTrace(new Throwable()); int n = st.getDepth(); for (int i = 3; i < n; i++) { StackTraceElement ste = st.getElement(i); if ("ClassLoader.java".equals(ste.getFileName()) && "loadClass".equals(ste.getMethodName())) { return true; } } return false; } finally { LOCK.unlock(); } }
public void enterWhenUninterruptibly(Monitor.Guard var1) { if (var1.monitor != this) { throw new IllegalMonitorStateException(); } else { ReentrantLock var2 = this.lock; boolean var3 = var2.isHeldByCurrentThread(); var2.lock(); boolean var4 = false; try { if (!var1.isSatisfied()) { this.awaitUninterruptibly(var1, var3); } var4 = true; } finally { if (!var4) { this.leave(); } } } }