@Override public void ungroup(String group) { Transaction transaction = null; writeLock.lock(); try { // start the transaction transaction = transactionBuilder.start(); // ungroup the specified user final UngroupUserGroupAction ungroupUserGroup = new UngroupUserGroupAction(group); transaction.execute(ungroupUserGroup); // commit the transaction transaction.commit(); } catch (TransactionException | DataAccessException te) { rollback(transaction); throw new AdministrationException(te); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
/** * Called by methods that need to force the map into a safe state. * * <p>This method can be called without any locks being active. */ private void cloneMap() { readLock.lock(); try { // Check that it hasn't been copied already if (cloned) { return; } } finally { readLock.unlock(); } /* * Note: This space here is a window during which some code could have made * a copy. Therefore we will do a cautious double-check. */ // Put in a write lock before cloning the map writeLock.lock(); try { // Check that it hasn't been copied already if (cloned) { return; } Map<K, V> copy = ValueProtectingMap.cloneMap(map, immutableClasses); // Discard the original this.map = copy; this.cloned = true; } finally { writeLock.unlock(); } }
@Override public void deleteUser(String id) { Transaction transaction = null; writeLock.lock(); try { // create the connection transaction = transactionBuilder.start(); // delete the user DeleteUserAction deleteUser = new DeleteUserAction(id); transaction.execute(deleteUser); // commit the transaction transaction.commit(); } catch (DataAccessException | TransactionException dae) { rollback(transaction); throw new AdministrationException(dae); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
@Override public void invalidateUserGroupAccount(String group) { Transaction transaction = null; writeLock.lock(); try { // start the transaction transaction = transactionBuilder.start(); // invalidate the user account InvalidateUserGroupAccountsAction invalidateUserGroupAccounts = new InvalidateUserGroupAccountsAction(group); transaction.execute(invalidateUserGroupAccounts); // commit the transaction transaction.commit(); } catch (TransactionException | DataAccessException te) { rollback(transaction); throw new AdministrationException(te); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
@Override public NiFiUser createPendingUserAccount(String dn, String justification) { Transaction transaction = null; writeLock.lock(); try { // start the transaction transaction = transactionBuilder.start(); // create the account request RequestUserAccountAction requestUserAccount = new RequestUserAccountAction(dn, justification); NiFiUser user = transaction.execute(requestUserAccount); // commit the transaction transaction.commit(); // return the nifi user return user; } catch (TransactionException | DataAccessException te) { rollback(transaction); throw new AdministrationException(te); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
@Override public Key getOrCreateKey(String identity) { Transaction transaction = null; Key key = null; writeLock.lock(); try { // start the transaction transaction = transactionBuilder.start(); // get or create a key GetOrCreateKeyAction addActions = new GetOrCreateKeyAction(identity); key = transaction.execute(addActions); // commit the transaction transaction.commit(); } catch (TransactionException | DataAccessException te) { rollback(transaction); throw new AdministrationException(te); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } return key; }
/** add session to the end of queue when a transaction starts (depending on isolation mode) */ public void beginAction(Session session, Statement cs) { synchronized (liveTransactionTimestamps) { session.actionTimestamp = nextChangeTimestamp(); if (!session.isTransaction) { session.transactionTimestamp = session.actionTimestamp; session.isTransaction = true; liveTransactionTimestamps.addLast(session.actionTimestamp); } } if (session.hasLocks()) { return; } try { writeLock.lock(); boolean canProceed = beginActionTPL(session, cs); if (!canProceed) { session.abortTransaction = true; } } finally { writeLock.unlock(); } }
@Override public void deleteKey(String identity) { Transaction transaction = null; writeLock.lock(); try { // start the transaction transaction = transactionBuilder.start(); // delete the keys DeleteKeysAction deleteKeys = new DeleteKeysAction(identity); transaction.execute(deleteKeys); // commit the transaction transaction.commit(); } catch (TransactionException | DataAccessException te) { rollback(transaction); throw new AdministrationException(te); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
/** Seed any users from the authority provider that are not already present. */ public void seedUserAccounts() { // do not seed node's user cache. when/if the node disconnects its // cache will be populated lazily (as needed) if (properties.isNode()) { return; } Transaction transaction = null; writeLock.lock(); try { // start the transaction transaction = transactionBuilder.start(); // seed the accounts SeedUserAccountsAction seedUserAccounts = new SeedUserAccountsAction(); transaction.execute(seedUserAccounts); // commit the transaction transaction.commit(); } catch (AdministrationException ae) { rollback(transaction); throw ae; } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
@Override public NiFiUserGroup disableGroup(String group) { Transaction transaction = null; writeLock.lock(); try { // create the connection transaction = transactionBuilder.start(); // disable the user DisableUserGroupAction disableUser = new DisableUserGroupAction(group); NiFiUserGroup userGroup = transaction.execute(disableUser); // commit the transaction transaction.commit(); // return the user return userGroup; } catch (DataAccessException | TransactionException dae) { rollback(transaction); throw new AdministrationException(dae); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
void cleanup() { nodesWriteLock.lock(); try { nodes.clear(); } finally { nodesWriteLock.unlock(); } }
NodeInternal getAndRemoveNode(NodeID nodeId) { nodesWriteLock.lock(); try { NodeInternal node = nodes.remove(nodeId); return node; } finally { nodesWriteLock.unlock(); } }
@Around("within(org.apache.nifi.web.NiFiServiceFacade+) && " + "execution(* copy*(..))") public Object copyLock(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { writeLock.lock(); try { return proceedingJoinPoint.proceed(); } finally { writeLock.unlock(); } }
/** * @brief Atomically decrements by one the current value * @returns the previous value */ public long getAndDecrement() { long value = 0; // TODO -- you fill in here ReentrantReadWriteLock.WriteLock writeLock = mRWLock.writeLock(); writeLock.lock(); value = mValue; mValue--; writeLock.unlock(); return value; }
/** * @brief Atomically increments by one the current value * @returns the updated value */ public long incrementAndGet() { long value = 0; // TODO -- you fill in here ReentrantReadWriteLock.WriteLock writeLock = mRWLock.writeLock(); writeLock.lock(); mValue++; value = mValue; writeLock.unlock(); return value; }
NodeInternal updateOnRejoin(ClientID thisNodeId, NodeID[] clusterMembers) { nodesWriteLock.lock(); try { for (NodeID otherNode : clusterMembers) { if (!thisNodeId.equals(otherNode)) { registerNodeBase((ClientID) otherNode, false); } } return registerNodeBase(thisNodeId, true); } finally { nodesWriteLock.unlock(); } }
/** Closes all virtual spaces mounted by this mount manager */ public void close() { logger.debug("[VFSMountManager] Closing mount manager"); try { writeLock.lock(); for (final DataSpacesURI spaceUri : new ArrayList<DataSpacesURI>(mountedSpaces.keySet())) { unmountAllFileSystems(spaceUri); } } finally { writeLock.unlock(); } logger.debug("[VFSMountManager] Mount manager closed"); }
public void rollback(Session session) { session.abortTransaction = false; session.actionTimestamp = nextChangeTimestamp(); rollbackPartial(session, 0, session.transactionTimestamp); endTransaction(session); try { writeLock.lock(); endTransactionTPL(session); } finally { writeLock.unlock(); } }
synchronized void nContextDestroy() { validate(); // take teardown lock // teardown lock can only be taken when no objects are being destroyed ReentrantReadWriteLock.WriteLock wlock = mRWLock.writeLock(); wlock.lock(); long curCon = mContext; // context is considered dead as of this point mContext = 0; wlock.unlock(); rsnContextDestroy(curCon); }
private NodeInternal registerNodeBase(ClientID clientId, boolean isLocalNode) { final NodeInternal node = new NodeImpl(clientId.toString(), clientId.toLong()); nodesWriteLock.lock(); try { NodeInternal old = nodes.putIfAbsent(clientId, node); if (old != null) { return old; } else { return node; } } finally { nodesWriteLock.unlock(); } }
public void setTransactionControl(Session session, int mode) { writeLock.lock(); try { synchronized (liveTransactionTimestamps) { // statement runs as transaction if (liveTransactionTimestamps.size() == 1) { switch (mode) { case Database.MVCC: { TransactionManagerMVCC manager = new TransactionManagerMVCC(database); manager.globalChangeTimestamp.set(globalChangeTimestamp.get()); manager.liveTransactionTimestamps.addLast(session.transactionTimestamp); database.txManager = manager; break; } case Database.MVLOCKS: break; case Database.LOCKS: { TransactionManager2PL manager = new TransactionManager2PL(database); manager.globalChangeTimestamp.set(globalChangeTimestamp.get()); database.txManager = manager; break; } } return; } } } finally { writeLock.unlock(); } throw Error.error(ErrorCode.X_25001); }
public boolean prepareCommitActions(Session session) { Object[] list = session.rowActionList.getArray(); int limit = session.rowActionList.size(); if (session.abortTransaction) { // System.out.println("cascade fail " + session + " " + session.actionTimestamp); return false; } try { writeLock.lock(); for (int i = 0; i < limit; i++) { RowAction rowact = (RowAction) list[i]; if (!rowact.canCommit(session, session.tempSet)) { // System.out.println("commit conflicts " + session + " " + // session.actionTimestamp); return false; } } session.actionTimestamp = nextChangeTimestamp(); for (int i = 0; i < limit; i++) { RowAction action = (RowAction) list[i]; action.prepareCommit(session); } for (int i = 0; i < session.tempSet.size(); i++) { Session current = (Session) session.tempSet.get(i); current.abortTransaction = true; } return true; } finally { writeLock.unlock(); session.tempSet.clear(); } }
@Override public NiFiUser checkAuthorization(String dn) { Transaction transaction = null; writeLock.lock(); try { // create the connection transaction = transactionBuilder.start(); // determine how long the cache is valid for final int cacheSeconds; try { cacheSeconds = (int) FormatUtils.getTimeDuration( properties.getUserCredentialCacheDuration(), TimeUnit.SECONDS); } catch (IllegalArgumentException iae) { throw new AdministrationException( "User credential cache duration is not configured correctly."); } // attempt to authorize the user AuthorizeUserAction authorizeUser = new AuthorizeUserAction(dn, cacheSeconds); NiFiUser user = transaction.execute(authorizeUser); // commit the transaction transaction.commit(); // return the nifi user return user; } catch (DataAccessException | TransactionException dae) { rollback(transaction); throw new AdministrationException(dae); } catch (AccountDisabledException | AccountPendingException ade) { rollback(transaction); throw ade; } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
@Override public NiFiUserGroup updateGroup( final String group, final Set<String> userIds, final Set<Authority> authorities) { Transaction transaction = null; writeLock.lock(); try { // if user ids have been specified, invalidate the user accounts before performing // the desired updates. if case of an error, this will ensure that these users are // authorized the next time the access the application if (userIds != null) { for (final String userId : userIds) { invalidateUserAccount(userId); } } // start the transaction transaction = transactionBuilder.start(); // set the authorities for each user in this group if specified final UpdateUserGroupAction updateUserGroup = new UpdateUserGroupAction(group, userIds, authorities); transaction.execute(updateUserGroup); // get all the users that are now in this group final GetUserGroupAction getUserGroup = new GetUserGroupAction(group); final NiFiUserGroup userGroup = transaction.execute(getUserGroup); // commit the transaction transaction.commit(); return userGroup; } catch (TransactionException | DataAccessException te) { rollback(transaction); throw new AdministrationException(te); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
/** * Makes sure that the provided file system is mounted for the given dataspace (identified by its * root url) * * @param mountingPoint dataspace uri * @param spaceRootFOUri file system root * @return true if the file system is mounted * @throws FileSystemException */ private boolean ensureFileSystemIsMounted( final DataSpacesURI mountingPoint, final String spaceRootFOUri) throws FileSystemException { ConcurrentHashMap<String, FileObject> fileSystems = null; DataSpacesURI spacePart = mountingPoint.getSpacePartOnly(); try { readLock.lock(); fileSystems = mountedSpaces.get(spacePart); // already mounted if (fileSystems.get(spaceRootFOUri) != null) { return true; } } finally { readLock.unlock(); } logger.debug("[VFSMountManager] Request mounting VFS root = " + spaceRootFOUri); FileObject mountedRoot; try { mountedRoot = VFSMountManagerHelper.mount(spaceRootFOUri); // the fs is accessible try { writeLock.lock(); fileSystems.put(spaceRootFOUri, mountedRoot); mountedSpaces.put(spacePart, fileSystems); } finally { writeLock.unlock(); } if (logger.isDebugEnabled()) logger.debug( String.format( "[VFSMountManager] Mounted space: %s (access URL: %s)", spacePart, spaceRootFOUri)); return true; } catch (org.apache.commons.vfs.FileSystemException x) { String err = String.format( "[VFSMountManager] Could not access URL %s to mount %s", spaceRootFOUri, spacePart); logger.info(err); removeSpaceRootUri(spacePart, spaceRootFOUri); throw new FileSystemException(err, x); } }
/** * remove session from queue when a transaction ends and expire any committed transactions that * are no longer required. remove transactions ended before the first timestamp in * liveTransactionsSession queue */ void endTransaction(Session session) { try { writeLock.lock(); long timestamp = session.transactionTimestamp; synchronized (liveTransactionTimestamps) { session.isTransaction = false; int index = liveTransactionTimestamps.indexOf(timestamp); liveTransactionTimestamps.remove(index); } mergeExpiredTransactions(session); } finally { writeLock.unlock(); } }
@Override public NiFiUser update(String id, Set<Authority> authorities) { Transaction transaction = null; // may be empty but not null if (authorities == null) { throw new IllegalArgumentException("The specified authorities cannot be null."); } writeLock.lock(); try { // invalidate the user account in preparation for potential subsequent errors invalidateUserAccount(id); // at this point the current user account has been invalidated so we will // attempt to update the account. if any part fails we are assured the // user will be need to be given approval before they access the system at // a later time // start the transaction transaction = transactionBuilder.start(); // update the user authorities UpdateUserAction setUserAuthorities = new UpdateUserAction(id, authorities); NiFiUser user = transaction.execute(setUserAuthorities); // commit the transaction transaction.commit(); // return the user return user; } catch (TransactionException | DataAccessException e) { rollback(transaction); throw new AdministrationException(e); } catch (Throwable t) { rollback(transaction); throw t; } finally { closeQuietly(transaction); writeLock.unlock(); } }
/** * Return a lookup of all row ids for cached tables in transactions. For auto-defrag, as currently * there will be no RowAction entries at the time of defrag. */ public DoubleIntIndex getTransactionIDList() { writeLock.lock(); try { int size = rowActionMap.size(); DoubleIntIndex lookup = new DoubleIntIndex(size, false); lookup.setKeysSearchTarget(); Iterator it = this.rowActionMap.keySet().iterator(); for (; it.hasNext(); ) { lookup.addUnique(it.nextInt(), 0); } return lookup; } finally { writeLock.unlock(); } }
/** Convert row ID's for cached table rows in transactions */ public void convertTransactionIDs(DoubleIntIndex lookup) { writeLock.lock(); try { RowAction[] list = new RowAction[rowActionMap.size()]; Iterator it = this.rowActionMap.values().iterator(); for (int i = 0; it.hasNext(); i++) { list[i] = (RowAction) it.next(); } rowActionMap.clear(); for (int i = 0; i < list.length; i++) { int pos = lookup.lookupFirstEqual(list[i].getPos()); list[i].setPos(pos); rowActionMap.put(pos, list[i]); } } finally { writeLock.unlock(); } }
public boolean commitTransaction(Session session) { if (session.abortTransaction) { return false; } int limit = session.rowActionList.size(); Object[] list = limit == 0 ? ValuePool.emptyObjectArray : session.rowActionList.getArray(); try { writeLock.lock(); endTransaction(session); if (limit == 0) { endTransactionTPL(session); try { session.logSequences(); } catch (HsqlException e) { } return true; } // new actionTimestamp used for commitTimestamp // already set in prepareCommitActions(); session.actionTimestamp = nextChangeTimestamp(); for (int i = 0; i < limit; i++) { RowAction action = (RowAction) list[i]; action.commit(session); } for (int i = 0; i < limit; i++) { RowAction action = (RowAction) list[i]; if (action.type == RowActionBase.ACTION_NONE) { continue; } int type = action.getCommitType(session.actionTimestamp); PersistentStore store = session.sessionData.getRowStore(action.table); Row row = action.memoryRow; if (row == null) { row = (Row) store.get(action.getPos(), false); } if (action.table.hasLobColumn) { switch (type) { case RowActionBase.ACTION_INSERT: session.sessionData.addLobUsageCount(action.table, row.getData()); break; default: } } if (action.table.tableType == TableBase.TEXT_TABLE) { switch (type) { case RowActionBase.ACTION_DELETE: store.removePersistence(action.getPos()); break; case RowActionBase.ACTION_INSERT: store.commitPersistence(row); break; default: } } } // session.actionTimestamp is the committed tx timestamp if (getFirstLiveTransactionTimestamp() > session.actionTimestamp) { mergeTransaction(session, list, 0, limit, session.actionTimestamp); rowActionMapRemoveTransaction(list, 0, limit, true); } else { list = session.rowActionList.toArray(); addToCommittedQueue(session, list); } try { session.logSequences(); database.logger.writeCommitStatement(session); } catch (HsqlException e) { } endTransactionTPL(session); return true; } finally { writeLock.unlock(); session.tempSet.clear(); } }