List of usage examples for java.util.concurrent.locks ReentrantLock unlock
public void unlock()
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Interrupts threads that might be waiting for tasks (as indicated by not * being locked) so they can check for termination or configuration changes. * Ignores SecurityExceptions (in which case some threads may remain * uninterrupted)./*from w ww. j a va 2 s . co m*/ * * @param onlyOne * If true, interrupt at most one worker. This is called only * from tryTerminate when termination is otherwise enabled but * there are still other workers. In this case, at most one * waiting worker is interrupted to propagate shutdown signals in * case all threads are currently waiting. Interrupting any * arbitrary thread ensures that newly arriving workers since * shutdown began will also eventually exit. To guarantee * eventual termination, it suffices to always interrupt only one * idle worker, but shutdown() interrupts all idle workers so * that redundant workers exit promptly, not waiting for a * straggler task to finish. */ private void interruptIdleWorkers(boolean onlyOne) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (Worker w : workers) { Thread t = w.thread; if (!t.isInterrupted() && w.tryLock()) { try { t.interrupt(); } catch (SecurityException ignore) { } finally { w.unlock(); } } if (onlyOne) break; } } finally { mainLock.unlock(); } }
From source file:org.apache.hadoop.hbase.master.procedure.MasterProcedureScheduler.java
/** * Tries to remove the queue and the table-lock of the specified table. * If there are new operations pending (e.g. a new create), * the remove will not be performed.// w w w . j a v a2 s. c o m * @param table the name of the table that should be marked as deleted * @return true if deletion succeeded, false otherwise meaning that there are * other new operations pending for that table (e.g. a new create). */ protected boolean markTableAsDeleted(final TableName table) { final ReentrantLock l = schedLock; l.lock(); try { TableQueue queue = getTableQueue(table); if (queue == null) return true; if (queue.isEmpty() && queue.tryExclusiveLock(0)) { // remove the table from the run-queue and the map if (IterableList.isLinked(queue)) { tableRunQueue.remove(queue); } // Remove the table lock try { lockManager.tableDeleted(table); } catch (IOException e) { LOG.warn("Received exception from TableLockManager.tableDeleted:", e); //not critical } removeTableQueue(table); } else { // TODO: If there are no create, we can drop all the other ops return false; } } finally { l.unlock(); } return true; }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Checks if a new worker can be added with respect to current pool state * and the given bound (either core or maximum). If so, the worker count is * adjusted accordingly, and, if possible, a new worker is created and * started running firstTask as its first task. This method returns false if * the pool is stopped or eligible to shut down. It also returns false if * the thread factory fails to create a thread when asked, which requires a * backout of workerCount, and a recheck for termination, in case the * existence of this worker was holding up termination. * // w w w .j a v a 2 s . c o m * @param firstTask * the task the new thread should run first (or null if none). * Workers are created with an initial first task (in method * execute()) to bypass queuing when there are fewer than * corePoolSize threads (in which case we always start one), or * when the queue is full (in which case we must bypass queue). * Initially idle threads are usually created via * prestartCoreThread or to replace other dying workers. * * @param core * if true use corePoolSize as bound, else maximumPoolSize. (A * boolean indicator is used here rather than a value to ensure * reads of fresh values after checking other pool state). * @return true if successful */ private boolean addWorker(Runnable firstTask, boolean core) { retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c); // Check if queue empty only if necessary. if (rs >= SHUTDOWN && !(rs == SHUTDOWN && firstTask == null && !workQueue.isEmpty())) return false; for (;;) { int wc = workerCountOf(c); if (wc >= CAPACITY || wc >= (core ? corePoolSize : maximumPoolSize)) return false; if (compareAndIncrementWorkerCount(c)) break retry; c = ctl.get(); // Re-read ctl if (runStateOf(c) != rs) continue retry; // else CAS failed due to workerCount change; retry inner loop } } Worker w = new Worker(firstTask); Thread t = w.thread; final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { // Recheck while holding lock. // Back out on ThreadFactory failure or if // shut down before lock acquired. int c = ctl.get(); int rs = runStateOf(c); if (t == null || (rs >= SHUTDOWN && !(rs == SHUTDOWN && firstTask == null))) { decrementWorkerCount(); tryTerminate(); return false; } workers.add(w); int s = workers.size(); if (s > largestPoolSize) largestPoolSize = s; } finally { mainLock.unlock(); } t.start(); // It is possible (but unlikely) for a thread to have been // added to workers, but not yet started, during transition to // STOP, which could result in a rare missed interrupt, // because Thread.interrupt is not guaranteed to have any effect // on a non-yet-started Thread (see Thread#interrupt). if (runStateOf(ctl.get()) == STOP && !t.isInterrupted()) t.interrupt(); return true; }
From source file:org.apache.hadoop.hive.ql.Driver.java
private int compileInternal(String command, boolean deferClose) { int ret;/*from w w w.j ava 2 s.c o m*/ Metrics metrics = MetricsFactory.getInstance(); if (metrics != null) { metrics.incrementCounter(MetricsConstant.WAITING_COMPILE_OPS, 1); } PerfLogger perfLogger = SessionState.getPerfLogger(); perfLogger.PerfLogBegin(CLASS_NAME, PerfLogger.WAIT_COMPILE); final ReentrantLock compileLock = tryAcquireCompileLock(isParallelEnabled, command); perfLogger.PerfLogEnd(CLASS_NAME, PerfLogger.WAIT_COMPILE); if (metrics != null) { metrics.decrementCounter(MetricsConstant.WAITING_COMPILE_OPS, 1); } if (compileLock == null) { return ErrorMsg.COMPILE_LOCK_TIMED_OUT.getErrorCode(); } try { ret = compile(command, true, deferClose); } finally { compileLock.unlock(); } if (ret != 0) { try { releaseLocksAndCommitOrRollback(false, null); } catch (LockException e) { LOG.warn("Exception in releasing locks. " + org.apache.hadoop.util.StringUtils.stringifyException(e)); } } //Save compile-time PerfLogging for WebUI. //Execution-time Perf logs are done by either another thread's PerfLogger //or a reset PerfLogger. queryDisplay.setPerfLogStarts(QueryDisplay.Phase.COMPILATION, perfLogger.getStartTimes()); queryDisplay.setPerfLogEnds(QueryDisplay.Phase.COMPILATION, perfLogger.getEndTimes()); return ret; }
From source file:org.apache.hadoop.hbase.master.AssignmentManager.java
/** * This call is invoked only (1) master assign meta; * (2) during failover mode startup, zk assignment node processing. * The locker is set in the caller. It returns true if the region * is in transition for sure, false otherwise. * * It should be private but it is used by some test too. *///w ww . j ava 2 s . com boolean processRegionsInTransition(final RegionTransition rt, final HRegionInfo regionInfo, final int expectedVersion) throws KeeperException { EventType et = rt.getEventType(); // Get ServerName. Could not be null. final ServerName sn = rt.getServerName(); final byte[] regionName = rt.getRegionName(); final String encodedName = HRegionInfo.encodeRegionName(regionName); final String prettyPrintedRegionName = HRegionInfo.prettyPrint(encodedName); LOG.info("Processing " + prettyPrintedRegionName + " in state: " + et); if (regionStates.isRegionInTransition(encodedName)) { LOG.info("Processed region " + prettyPrintedRegionName + " in state: " + et + ", does nothing since the region is already in transition " + regionStates.getRegionTransitionState(encodedName)); // Just return return true; } if (!serverManager.isServerOnline(sn)) { // It was transitioning on a dead server, so it's closed now. // Force to OFFLINE and put it in transition, but not assign it // since log splitting for the dead server is not done yet. LOG.debug("RIT " + encodedName + " in state=" + rt.getEventType() + " was on deadserver; forcing offline"); if (regionStates.isRegionOnline(regionInfo)) { // Meta could still show the region is assigned to the previous // server. If that server is online, when we reload the meta, the // region is put back to online, we need to offline it. regionStates.regionOffline(regionInfo); sendRegionClosedNotification(regionInfo); } // Put it back in transition so that SSH can re-assign it regionStates.updateRegionState(regionInfo, State.OFFLINE, sn); if (regionInfo.isMetaRegion()) { // If it's meta region, reset the meta location. // So that master knows the right meta region server. MetaRegionTracker.setMetaLocation(watcher, sn); } else { // No matter the previous server is online or offline, // we need to reset the last region server of the region. regionStates.setLastRegionServerOfRegion(sn, encodedName); // Make sure we know the server is dead. if (!serverManager.isServerDead(sn)) { serverManager.expireServer(sn); } } return false; } switch (et) { case M_ZK_REGION_CLOSING: // Insert into RIT & resend the query to the region server: may be the previous master // died before sending the query the first time. final RegionState rsClosing = regionStates.updateRegionState(rt, State.CLOSING); this.executorService.submit(new EventHandler(server, EventType.M_MASTER_RECOVERY) { @Override public void process() throws IOException { ReentrantLock lock = locker.acquireLock(regionInfo.getEncodedName()); try { unassign(regionInfo, rsClosing, expectedVersion, null, true, null); if (regionStates.isRegionOffline(regionInfo)) { assign(regionInfo, true); } } finally { lock.unlock(); } } }); break; case RS_ZK_REGION_CLOSED: case RS_ZK_REGION_FAILED_OPEN: // Region is closed, insert into RIT and handle it regionStates.updateRegionState(regionInfo, State.CLOSED, sn); invokeAssign(regionInfo); break; case M_ZK_REGION_OFFLINE: // Insert in RIT and resend to the regionserver regionStates.updateRegionState(rt, State.PENDING_OPEN); final RegionState rsOffline = regionStates.getRegionState(regionInfo); this.executorService.submit(new EventHandler(server, EventType.M_MASTER_RECOVERY) { @Override public void process() throws IOException { ReentrantLock lock = locker.acquireLock(regionInfo.getEncodedName()); try { RegionPlan plan = new RegionPlan(regionInfo, null, sn); addPlan(encodedName, plan); assign(rsOffline, false, false); } finally { lock.unlock(); } } }); break; case RS_ZK_REGION_OPENING: regionStates.updateRegionState(rt, State.OPENING); break; case RS_ZK_REGION_OPENED: // Region is opened, insert into RIT and handle it // This could be done asynchronously, we would need then to acquire the lock in the // handler. regionStates.updateRegionState(rt, State.OPEN); new OpenedRegionHandler(server, this, regionInfo, sn, expectedVersion).process(); break; case RS_ZK_REQUEST_REGION_SPLIT: case RS_ZK_REGION_SPLITTING: case RS_ZK_REGION_SPLIT: // Splitting region should be online. We could have skipped it during // user region rebuilding since we may consider the split is completed. // Put it in SPLITTING state to avoid complications. regionStates.regionOnline(regionInfo, sn); regionStates.updateRegionState(rt, State.SPLITTING); if (!handleRegionSplitting(rt, encodedName, prettyPrintedRegionName, sn)) { deleteSplittingNode(encodedName, sn); } break; case RS_ZK_REQUEST_REGION_MERGE: case RS_ZK_REGION_MERGING: case RS_ZK_REGION_MERGED: if (!handleRegionMerging(rt, encodedName, prettyPrintedRegionName, sn)) { deleteMergingNode(encodedName, sn); } break; default: throw new IllegalStateException("Received region in state:" + et + " is not valid."); } LOG.info("Processed region " + prettyPrintedRegionName + " in state " + et + ", on " + (serverManager.isServerOnline(sn) ? "" : "dead ") + "server: " + sn); return true; }
From source file:org.openspaces.persistency.cassandra.HectorCassandraClient.java
/** * Adds a secondary index to the provided columns in the column family matching the provided * typeName. Creates the column if it does not already exist. * * @param typeName The type name describing the matchin column family. * @param columnNames the columns to which secondary indexes should be added. *///from w ww .j ava 2 s .com public void addIndexesToColumnFamily(String typeName, List<String> columnNames, SpaceDocumentColumnFamilyMapper mapper) { ColumnFamilyMetadata metadata = metadataCache.getColumnFamilyMetadata(typeName); if (metadata == null) { metadata = fetchColumnFamilyMetadata(typeName, mapper); if (metadata == null) { throw new SpaceCassandraSchemaUpdateException( "Failed finding column family metadata for " + typeName, null, false); } } ReentrantLock lockForType = namedLock.forName(metadata.getTypeName()); lockForType.lock(); try { metadata.getIndexes().addAll(columnNames); ColumnFamilyDefinition columnFamilyDefinition = getColumnFamilyDefinition(metadata); if (columnFamilyDefinition == null) { throw new SpaceCassandraSchemaUpdateException("column family definition: " + metadata.getColumnFamilyName() + " for type: " + typeName + " not found", null, false); } ThriftCfDef thriftCfDef = new ThriftCfDef(columnFamilyDefinition); for (String columnName : columnNames) { ByteBuffer serializedColumnName = StringSerializer.get().toByteBuffer(columnName); ColumnDefinition originalColumnDefinition = getColumnDefinition(serializedColumnName, thriftCfDef); if (originalColumnDefinition == null) { String validationClass = ValidatorClassInferer.getBytesTypeValidationClass(); addColumnDefinitionToColumnFamilyDefinition(metadata, thriftCfDef, columnName, validationClass); } else if (originalColumnDefinition.getIndexName() != null) { // index already exists for this column, no need to proceed continue; } else { List<ColumnDefinition> currentColumns = thriftCfDef.getColumnMetadata(); thriftCfDef.setColumnMetadata(new ArrayList<ColumnDefinition>()); BasicColumnDefinition replacedColumnDefinition = new BasicColumnDefinition(); replacedColumnDefinition.setName(originalColumnDefinition.getName()); replacedColumnDefinition.setValidationClass(originalColumnDefinition.getValidationClass()); replacedColumnDefinition.setIndexName(generateIndexName(typeName, columnName)); replacedColumnDefinition.setIndexType(ColumnIndexType.KEYS); for (ColumnDefinition columnDef : currentColumns) { if (columnDef != originalColumnDefinition) { thriftCfDef.addColumnDefinition(columnDef); } else { thriftCfDef.addColumnDefinition(replacedColumnDefinition); } } } } if (logger.isInfoEnabled()) { logger.info("Adding indexes to columns: " + columnNames + " of type: " + metadata.getTypeName() + ", column family: " + metadata.getColumnFamilyName()); } try { cluster.updateColumnFamily(thriftCfDef, true); } catch (Exception e) { throw new SpaceCassandraSchemaUpdateException("Failed adding column family definition to cassandra", null, true); } } finally { lockForType.unlock(); } }
From source file:org.apache.hadoop.hbase.master.AssignmentManager.java
/** * @param plan Plan to execute.//from ww w . j a va2 s .co m */ public void balance(final RegionPlan plan) { HRegionInfo hri = plan.getRegionInfo(); TableName tableName = hri.getTable(); if (tableStateManager.isTableState(tableName, ZooKeeperProtos.Table.State.DISABLED, ZooKeeperProtos.Table.State.DISABLING)) { LOG.info("Ignored moving region of disabling/disabled table " + tableName); return; } // Move the region only if it's assigned String encodedName = hri.getEncodedName(); ReentrantLock lock = locker.acquireLock(encodedName); try { if (!regionStates.isRegionOnline(hri)) { RegionState state = regionStates.getRegionState(encodedName); LOG.info("Ignored moving region not assigned: " + hri + ", " + (state == null ? "not in region states" : state)); return; } synchronized (this.regionPlans) { this.regionPlans.put(plan.getRegionName(), plan); } unassign(hri, false, plan.getDestination()); } finally { lock.unlock(); } }
From source file:org.openanzo.datasource.nodecentric.internal.NodeCentricDatasource.java
/** * Commit database transaction//w w w . j a v a2 s .com * * Note:Database already in transaction * * @param connection * {@link Connection} to underlying database * @param needsWrite * if true, tables will be locked if needed * @param needsTransaction * TODO * @throws AnzoException * {@link ExceptionConstants.RDB#NOT_IN_RDB_TRANSACTION} if connection isn't in a transaction * @throws AnzoException * {@link ExceptionConstants.RDB#DIDNT_START_RDB_TRANSACTION} if this thread didn't start the transaction * @throws AnzoException * {@link ExceptionConstants.RDB#FAILED_COMMIT_RDB_TRANSACTION} if there was a problem committing the connection */ public void commit(Connection connection, boolean needsWrite, boolean needsTransaction) throws AnzoException { long start = 0; if (stats.isEnabled()) { start = System.currentTimeMillis(); stats.getCommitUse().increment(); } try { ReentrantLock lock = connectionLocks.get(connection); if (lock == null) { lock = new ReentrantLock(); connectionLocks.put(connection, lock); } if (lock.isLocked()) { if (lock.isHeldByCurrentThread()) { try { if (needsTransaction) { ArrayList<AnzoException> exceptions = null; try { connection.commit(); connection.setAutoCommit(true); } catch (SQLException e) { log.error(LogUtils.RDB_MARKER, "Error commmiting jdbc transaction", e); exceptions = new ArrayList<AnzoException>(); exceptions.add( new AnzoException(ExceptionConstants.RDB.FAILED_COMMIT_RDB_TRANSACTION, e)); } try { unlockTable(connection, needsWrite); } catch (AnzoException ae) { log.error(LogUtils.RDB_MARKER, "Error unlocking tables", ae); if (exceptions == null) { exceptions = new ArrayList<AnzoException>(); } exceptions.add(ae); } if (exceptions != null && exceptions.size() > 0) { throw new CompoundAnzoException(exceptions, ExceptionConstants.RDB.FAILED_COMMIT_RDB_TRANSACTION); } } } finally { lock.unlock(); nodeLayout.clearUncommittedCache(); } } else { throw new AnzoException(ExceptionConstants.RDB.DIDNT_START_RDB_TRANSACTION); } } else { throw new AnzoException(ExceptionConstants.RDB.NOT_IN_RDB_TRANSACTION); } } finally { if (stats.isEnabled()) { stats.getCommitDuration().addTime((System.currentTimeMillis() - start)); } } }
From source file:org.exoplatform.social.core.storage.impl.ActivityStreamStorageImpl.java
@Override public void updateCommenter(ProcessContext ctx) { final ReentrantLock lock = new ReentrantLock(); try {//from w w w .j a v a 2s. com StreamProcessContext streamCtx = ObjectHelper.cast(StreamProcessContext.class, ctx); ExoSocialActivity activity = streamCtx.getActivity(); Identity commenter = streamCtx.getIdentity(); IdentityEntity identityEntity = identityStorage._findIdentityEntity(commenter.getProviderId(), commenter.getRemoteId()); ActivityEntity activityEntity = _findById(ActivityEntity.class, activity.getId()); // lock.lock(); QueryResult<ActivityRef> got = getActivityRefs(identityEntity, activityEntity); ActivityRef activityRef = null; while (got.hasNext()) { activityRef = got.next(); activityRef.setName("" + activity.getUpdated().getTime()); activityRef.setLastUpdated(activity.getUpdated().getTime()); } long oldUpdated = streamCtx.getOldLastUpdated(); //activity's poster != comment's poster //don't have on My Activity stream boolean has = hasActivityRefs(identityEntity, activityEntity, ActivityRefType.MY_ACTIVITIES, oldUpdated); if (has == false) { manageRefList(new UpdateContext(commenter, null), activityEntity, ActivityRefType.MY_ACTIVITIES); } //post comment also put the activity on feed if have not any has = hasActivityRefs(identityEntity, activityEntity, ActivityRefType.FEED, oldUpdated); if (has == false) { manageRefList(new UpdateContext(commenter, null), activityEntity, ActivityRefType.FEED); } //create activityref for owner's activity createRefForPoster(activityEntity, oldUpdated); } catch (NodeNotFoundException ex) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); } catch (ChromatticException ex) { Throwable throwable = ex.getCause(); if (throwable instanceof ItemExistsException || throwable instanceof InvalidItemStateException || throwable instanceof PathNotFoundException) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); } else { LOG.warn("Probably was updated activity reference by another session", ex); LOG.debug(ex.getMessage(), ex); } } finally { lock.unlock(); } }
From source file:org.exoplatform.social.core.storage.impl.ActivityStreamStorageImpl.java
@Override public void update(ProcessContext ctx) { final ReentrantLock lock = new ReentrantLock(); ThreadLocal<Set<String>> idLocal = new ThreadLocal<Set<String>>(); try {/*from ww w . j a v a 2 s. c o m*/ StreamProcessContext streamCtx = ObjectHelper.cast(StreamProcessContext.class, ctx); ExoSocialActivity activity = streamCtx.getActivity(); ActivityEntity activityEntity = _findById(ActivityEntity.class, activity.getId()); lock.lock(); // block until condition holds Collection<ActivityRef> references = activityEntity.getActivityRefs(); Set<String> ids = new ConcurrentSkipListSet<String>(); for (ActivityRef ref : references) { ids.add(ref.getId()); } idLocal.set(ids); Set<String> idList = idLocal.get(); if (idList.size() > 0) { for (String id : idList) { ActivityRef old = _findById(ActivityRef.class, id); LOG.debug("ActivityRef will be deleted: " + old.toString()); ActivityRefListEntity refList = old.getDay().getMonth().getYear().getList(); // if (refList.isOnlyUpdate(old, activity.getUpdated().getTime())) { old.setName("" + activity.getUpdated().getTime()); old.setLastUpdated(activity.getUpdated().getTime()); } else { ActivityRef newRef = refList.getOrCreated(activity.getUpdated().getTime()); newRef.setLastUpdated(activity.getUpdated().getTime()); newRef.setActivityEntity(activityEntity); getSession().remove(old); } } } // mentioners addMentioner(streamCtx.getMentioners(), activityEntity); //turnOffLock to get increase perf //turnOnUpdateLock = false; } catch (NodeNotFoundException ex) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); //turnOnLock to avoid next exception } catch (ChromatticException ex) { Throwable throwable = ex.getCause(); if (throwable instanceof ItemExistsException || throwable instanceof InvalidItemStateException || throwable instanceof PathNotFoundException) { LOG.warn("Probably was updated activity reference by another session"); LOG.debug(ex.getMessage(), ex); //turnOnLock to avoid next exception } else { LOG.warn("Probably was updated activity reference by another session", ex); LOG.debug(ex.getMessage(), ex); } } finally { getSession().save(); lock.unlock(); } }