@Override public void finalize() throws Throwable { try { if ( lock != null ) { lock.unlock(); lock = null; } } finally { super.finalize(); } } }
@Override public void close() throws IOException { try { super.close(); } finally { if ( lock != null ) { lock.unlock(); lock = null; } } }
@Override public StorageItem retrieveItem(final boolean fromTask, final ResourceStoreRequest request) throws IllegalOperationException, ItemNotFoundException, StorageException { final RepositoryItemUid uid = createUid(P2Constants.METADATA_LOCK_PATH); final RepositoryItemUidLock lock = uid.getLock(); try { // Lock the metadata do be sure that the artifacts are retrieved from consistent paths. lock.lock(Action.read); try { // NOTE - THIS CANNOT be a write action (create/delete/update) as that will force a write lock // thus serializing access to this p2 repo. Using a read action here, will block the file from // being deleted/updated while retrieving the remote item, and that is all we need. // NXCM-2499 temporarily we do put access serialization back here, to avoid all the deadlocks. lock.lock(Action.create); return super.retrieveItem(fromTask, request); } finally { lock.unlock(); } } finally { lock.unlock(); } }
@Override public StorageItem retrieveItem(final boolean fromTask, final ResourceStoreRequest request) throws IllegalOperationException, ItemNotFoundException, StorageException { final RepositoryItemUid uid = createUid(P2Constants.METADATA_LOCK_PATH); final RepositoryItemUidLock lock = uid.getLock(); try { lock.lock(Action.read); return super.retrieveItem(fromTask, request); } finally { lock.unlock(); } }
@Override public InputStream getContent() throws IOException { final RepositoryItemUidLock lock = wrappedUid.getLock(); lock.lock( Action.read ); try { return new ReadLockingInputStream( lock, getTarget().getContent() ); } catch ( IOException e ) { lock.unlock(); throw e; } catch ( Exception e ) { lock.unlock(); // wrap it IOException w = new IOException( e.getMessage() ); w.initCause( e ); throw w; } }
public Attributes getAttributes( final RepositoryItemUid uid ) throws IOException { final RepositoryItemUidLock uidLock = uid.getAttributeLock(); uidLock.lock( Action.read ); try { if ( getLogger().isDebugEnabled() ) { getLogger().debug( "Loading attributes on UID=" + uid.toString() ); } return doGetAttributes( uid ); } finally { uidLock.unlock(); } }
@Override public Map<String, ArtifactMapping> getArtifactMappings() throws IllegalOperationException, StorageException { // cstamas: this method is called from other paths, not like getMirrorsURLsByRepositoryURL() above (called from // configureMirrors()), so the safest is to protect it with similar locking stuff even if we do suffer in // performance, but we avoiding potential deadlocks (this method was synchronized). final RepositoryItemUid uid = createUid(P2Constants.METADATA_LOCK_PATH); final RepositoryItemUidLock lock = uid.getLock(); try { lock.lock(Action.create); if (!hasArtifactMappings) { return null; } if (remoteArtifactMappings == null) { loadArtifactMappings(); } return remoteArtifactMappings; } finally { lock.unlock(); } }
public boolean deleteAttributes( final RepositoryItemUid uid ) { final RepositoryItemUidLock uidLock = uid.getAttributeLock(); uidLock.lock( Action.delete ); try { if ( getLogger().isDebugEnabled() ) { getLogger().debug( "Deleting attributes on UID=" + uid.toString() ); } boolean result = false; try { File ftarget = getFileFromBase( uid ); result = ftarget.exists() && ftarget.isFile() && ftarget.delete(); } catch ( IOException e ) { getLogger().warn( "Got IOException during delete of UID=" + uid.toString(), e ); } return result; } finally { uidLock.unlock(); } }
public Attributes getAttributes( final RepositoryItemUid uid ) { final RepositoryItemUidLock uidLock = uid.getAttributeLock(); uidLock.lock( Action.read ); try { if ( getLogger().isDebugEnabled() ) { getLogger().debug( "Loading attributes on UID=" + uid.toString() ); } try { return doGetAttributes( uid ); } catch ( IOException ex ) { getLogger().error( "Got IOException during reading of UID=" + uid.toString(), ex ); return null; } } finally { uidLock.unlock(); } }
@Override public Attributes getAttributes( final RepositoryItemUid uid ) throws IOException { final RepositoryItemUidLock uidLock = uid.getAttributeLock(); uidLock.lock( Action.read ); try { Attributes result = mainAttributeStorage.getAttributes( uid ); if ( result == null && fallbackAttributeStorage != null ) { result = fallbackAttributeStorage.getAttributes( uid ); if ( result != null ) { // mark it as legacy result.put( FALLBACK_MARKER_KEY, Boolean.TRUE.toString() ); } } return result; } finally { uidLock.unlock(); } }
@Override public void removeConfiguration(final P2RepositoryAggregatorConfiguration configuration) { try { final Repository repository = repositories.getRepository(configuration.repositoryId()); final RepositoryItemUid p2RepoUid = repository.createUid(P2_REPOSITORY_ROOT_PATH); try { p2RepoUid.getLock().lock(Action.create); final ResourceStoreRequest request = new ResourceStoreRequest(P2_REPOSITORY_ROOT_PATH); repository.deleteItem(request); } finally { p2RepoUid.getLock().unlock(); } } catch (final Exception e) { logger.warn(String.format("Could not delete P2 repository [%s:%s] due to [%s]", configuration.repositoryId(), P2_REPOSITORY_ROOT_PATH, e.getMessage()), e); } }
@Override protected StorageItem doRetrieveItem(final ResourceStoreRequest request) throws IllegalOperationException, ItemNotFoundException, StorageException { final RepositoryItemUid uid = createUid(P2Constants.METADATA_LOCK_PATH); final RepositoryItemUidLock lock = uid.getLock(); final boolean requestGroupLocalOnly = request.isRequestGroupLocalOnly(); try { lock.lock(Action.read); request.setRequestGroupLocalOnly(true); return super.doRetrieveItem(request); } finally { request.setRequestGroupLocalOnly(requestGroupLocalOnly); lock.unlock(); } }
@Override protected Collection<StorageItem> doListItems(final ResourceStoreRequest request) throws ItemNotFoundException, StorageException { final RepositoryItemUid uid = createUid(P2Constants.METADATA_LOCK_PATH); final RepositoryItemUidLock lock = uid.getLock(); final boolean requestGroupLocalOnly = request.isRequestGroupLocalOnly(); try { lock.lock(Action.read); request.setRequestGroupLocalOnly(true); return super.doListItems(request); } finally { request.setRequestGroupLocalOnly(requestGroupLocalOnly); lock.unlock(); } }
@Override public void addConfiguration(final P2RepositoryAggregatorConfiguration configuration) { try { final Repository repository = repositories.getRepository(configuration.repositoryId()); final StorageItem p2Dir = safeRetrieveItem(repository, P2_REPOSITORY_ROOT_PATH); // create if it does not exist if (p2Dir == null) { final RepositoryItemUid p2RepoUid = repository.createUid(P2_REPOSITORY_ROOT_PATH); try { p2RepoUid.getLock().lock(Action.create); createP2Repository(repository); } finally { p2RepoUid.getLock().unlock(); } } } catch (final NoSuchRepositoryException e) { logger.warn("Could not delete P2 repository [{}] as repository could not be found"); } catch (final Exception e) { throw new RuntimeException(e); } }