Examples of RepositoryItemUidLock


Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

    // This will catch immediately an uploader if an upload already happens
    // and prevent deadlocks, since uploader still does not have
    // shared lock
    final RepositoryItemUid uploaderUid = createUid(item.getPath() + ".storeItem()");

    final RepositoryItemUidLock uidUploaderLock = uploaderUid.getLock();

    uidUploaderLock.lock(Action.create);

    final Action action = getResultingActionOnWrite(item.getResourceStoreRequest());

    try {
      // NEXUS-4550: we are shared-locking the actual UID (to not prevent downloaders while
      // we save to temporary location. But this depends on actual LS backend actually...)
      // but we exclusive lock uploaders to serialize them!
      // And the LS has to take care of whatever stricter locking it has to use or not
      // Think: RDBMS LS or some trickier LS implementations for example
      final RepositoryItemUidLock uidLock = uid.getLock();

      uidLock.lock(Action.read);

      try {
        // store it
        getLocalStorage().storeItem(this, item);
      }
      finally {
        uidLock.unlock();
      }
    }
    finally {
      uidUploaderLock.unlock();
    }
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

  // ==

  protected <R> R doReadProtected(final Callable<R> callable)
      throws IOException
  {
    final RepositoryItemUidLock lock = getRepositoryItemUid().getLock();
    lock.lock(Action.read);
    try {
      try {
        return callable.call();
      }
      catch (IOException e) {
        throw e;
      }
      catch (Exception e) {
        Throwables.propagate(e);
      }
    }
    finally {
      lock.unlock();
    }
    return null; // to make compiler happy. but is unreachable
  }
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

      if (log.isDebugEnabled()) {
        log.debug(
            "Caching item " + item.getRepositoryItemUid().toString() + " in local storage of repository.");
      }

      final RepositoryItemUidLock itemLock = item.getRepositoryItemUid().getLock();

      itemLock.lock(Action.create);

      final Action action;

      try {
        action = getResultingActionOnWrite(item.getResourceStoreRequest());

        getLocalStorage().storeItem(this, item);

        removeFromNotFoundCache(item.getResourceStoreRequest());

        // we swapped the remote item with the one from local storage
        // using this method below, we ensure that we get a "wrapped"
        // content locator that will keel shared-lock on the content
        // until being fully read
        result = doRetrieveLocalItem(item.getResourceStoreRequest());

      }
      finally {
        itemLock.unlock();
      }

      result.getItemContext().setParentContext(item.getItemContext());

      if (Action.create.equals(action)) {
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

    // if proxy and need to go remote, we want to _protect_ ourselves from
    // serving up partial downloads...

    final RepositoryItemUid itemUid = createUid(request.getRequestPath());

    final RepositoryItemUidLock itemUidLock = itemUid.getLock();

    itemUidLock.lock(Action.read);

    try {
      if (!getRepositoryKind().isFacetAvailable(ProxyRepository.class)) {
        // we have no proxy facet, just get 'em!
        return super.doRetrieveItem(request);
      }
      else {
        // we have Proxy facet, so we want to check carefully local storage
        // Reason: a previous thread may still _downloading_ the stuff we want to
        // serve to another client, so we have to _wait_ for download, but for download
        // only.
        AbstractStorageItem localItem = null;

        if (!request.isRequestRemoteOnly()) {
          try {
            localItem = (AbstractStorageItem) super.doRetrieveItem(request);

            if (localItem != null && !request.isRequestAsExpired() && !isOld(localItem)) {
              // local copy is just fine, so, we are proxy but we have valid local copy in cache
              return localItem;
            }
          }
          catch (ItemNotFoundException e) {
            localItem = null;
          }
        }

        // we are a proxy, and we either don't have local copy or is stale, we need to
        // go remote and potentially check for new version of file, but we still don't know
        // will we actually fetch it (since aging != remote file changed!)
        // BUT, from this point on, we want to _serialize_ access, so upgrade to CREATE lock

        itemUidLock.lock(Action.create);

        try {
          // check local copy again, we were maybe blocked for a download, and we need to
          // recheck local copy after we acquired exclusive lock
          if (!request.isRequestRemoteOnly()) {
            try {
              localItem = (AbstractStorageItem) super.doRetrieveItem(request);

              if (localItem != null && !request.isRequestAsExpired() && !isOld(localItem)) {
                // local copy is just fine (downloaded by a thread holding us blocked on acquiring
                // exclusive lock)
                return localItem;
              }
            }
            catch (ItemNotFoundException e) {
              localItem = null;
            }
          }

          // this whole method happens with exclusive lock on UID
          return doRetrieveItem0(request, localItem);
        }
        finally {
          itemUidLock.unlock();
        }
      }
    }
    finally {
      itemUidLock.unlock();
    }
  }
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

  protected AbstractStorageItem doRetrieveRemoteItem(ResourceStoreRequest request)
      throws ItemNotFoundException, RemoteAccessException, StorageException
  {
    final RepositoryItemUid itemUid = createUid(request.getRequestPath());

    final RepositoryItemUidLock itemUidLock = itemUid.getLock();

    // all this remote download happens in exclusive lock
    itemUidLock.lock(Action.create);

    try {
      List<String> remoteUrls = getRemoteUrls(request);

      List<RepositoryItemValidationEvent> events = new ArrayList<>();

      Exception lastException = null;

      all_urls:
      for (String remoteUrl : remoteUrls) {
        int retryCount = 1;

        if (getRemoteStorageContext() != null) {
          RemoteConnectionSettings settings = getRemoteStorageContext().getRemoteConnectionSettings();
          if (settings != null) {
            retryCount = settings.getRetrievalRetryCount();
          }
        }

        if (log.isDebugEnabled()) {
          log.debug("Using URL:" + remoteUrl + ", retryCount=" + retryCount);
        }

        // Validate the mirror URL
        try {
          getRemoteStorage().validateStorageUrl(remoteUrl);
        }
        catch (RemoteStorageException e) {
          lastException = e;

          logFailedUrl(remoteUrl, e);

          continue all_urls; // retry with next url
        }
        catch (Exception e) {
          lastException = e;

          // make it logged, this is RuntimeEx
          log.warn("Failed URL validation: {}", remoteUrl, e);

          continue all_urls; // retry with next url
        }

        for (int i = 0; i < retryCount; i++) {
          try {
            // events.clear();

            AbstractStorageItem remoteItem =
                getRemoteStorage().retrieveItem(this, request, remoteUrl);

            remoteItem = doCacheItem(remoteItem);

            if (doValidateRemoteItemContent(request, remoteUrl, remoteItem, events)) {
              sendContentValidationEvents(request, events, true);

              return remoteItem;
            }
            else {
              continue all_urls; // retry with next url
            }
          }
          catch (ItemNotFoundException e) {
            lastException = e;

            continue all_urls; // retry with next url
          }
          catch (RemoteAccessException e) {
            lastException = e;

            logFailedUrl(remoteUrl, e);

            continue all_urls; // retry with next url
          }
          catch (RemoteStorageException e) {
            // in case when we were unable to make outbound request
            // at all, do not retry
            if (e instanceof RemoteStorageTransportException) {
              throw e;
            }

            lastException = e;

            // debug, print all
            if (log.isDebugEnabled()) {
              logFailedUrl(remoteUrl, e);
            }
            // not debug, only print the message
            else {
              Throwable t = ExceptionUtils.getRootCause(e);

              if (t == null) {
                t = e;
              }

              log.error(
                  String.format(
                      "Got RemoteStorageException in proxy repository %s while retrieving remote artifact \"%s\" from URL %s, this is %s (re)try, cause: %s: %s",
                      RepositoryStringUtils.getHumanizedNameString(this), request.toString(),
                      remoteUrl, String.valueOf(i + 1), t.getClass().getName(),
                      t.getMessage()));
            }
            // do not switch url yet, obey the retries
          }
          catch (LocalStorageException e) {
            lastException = e;

            // debug, print all
            if (log.isDebugEnabled()) {
              logFailedUrl(remoteUrl, e);
            }
            // not debug, only print the message
            else {
              Throwable t = ExceptionUtils.getRootCause(e);

              if (t == null) {
                t = e;
              }

              log.error(
                  "Got LocalStorageException in proxy repository {} while caching retrieved artifact \"{}\" got from URL {}, will attempt next mirror",
                  RepositoryStringUtils.getHumanizedNameString(this),
                  request,
                  remoteUrl,
                  t
              );
            }
            // do not switch url yet, obey the retries
            // TODO: IOException _might_ be actually a fatal error (like Nx process have no perms to write to disk)
            // but also might come when caching, from inability to READ the HTTP response body (see NEXUS-5898)
            // Hence, we will retry here too, and in case of first type of IO problems no harm will be done
            // anyway, but will solve the second type of problems, where retry will be attempted
          }
          catch (RuntimeException e) {
            lastException = e;

            // make it logged, this is RuntimeEx
            log.warn("Failed URL retrieve/cache: {}", remoteUrl, e);

            continue all_urls; // retry with next url
          }

          // retry with same url
        }
      }

      // if we got here, requested item was not retrieved for some reason

      sendContentValidationEvents(request, events, false);

      try {
        getLocalStorage().deleteItem(this, request);
      }
      catch (ItemNotFoundException e) {
        // good, we want this item deleted
      }
      catch (UnsupportedStorageOperationException e) {
        log.warn("Unexpected Exception in " + RepositoryStringUtils.getHumanizedNameString(this), e);
      }

      if (lastException instanceof StorageException) {
        throw (StorageException) lastException;
      }
      else if (lastException instanceof ItemNotFoundException) {
        throw (ItemNotFoundException) lastException;
      }

      // validation failed, I guess.
      throw new ItemNotFoundException(reasonFor(request, this,
          "Path %s fetched from remote storage of %s but failed validation.", request.getRequestPath(),
          this));
    }
    finally {
      itemUidLock.unlock();
    }
  }
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

  }

  private void addItemToIndex(Repository repository, StorageItem item, IndexingContext context)
      throws LocalStorageException, IOException
  {
    final RepositoryItemUidLock uidLock = item.getRepositoryItemUid().getLock();

    uidLock.lock(Action.read);

    try {

      ArtifactContext ac = null;

      // if we have a valid indexing context and have access to a File
      if (DefaultFSLocalRepositoryStorage.class.isAssignableFrom(repository.getLocalStorage().getClass())) {
        File file =
            ((DefaultFSLocalRepositoryStorage) repository.getLocalStorage()).getFileFromBase(repository,
                new ResourceStoreRequest(item));

        if (file.exists()) {
          try {
            ac = artifactContextProducer.getArtifactContext(context, file);
          }
          catch (IllegalArgumentException e) {
            // cannot create artifact context, forget it
            return;
          }

          if (ac != null) {
            if (log.isDebugEnabled()) {
              log.debug("The ArtifactContext created from file is fine, continuing.");
            }

            ArtifactInfo ai = ac.getArtifactInfo();

            if (ai.sha1 == null) {
              // if repo has no sha1 checksum, odd nexus one
              ai.sha1 =
                  item.getRepositoryItemAttributes().get(DigestCalculatingInspector.DIGEST_SHA1_KEY);
            }
          }
        }
      }

      // and finally: index it
      getNexusIndexer().addArtifactToIndex(ac, context);
    }
    finally {
      uidLock.unlock();
    }
  }
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

          + " from index (DELETE).");
    }

    // NEXUS-814: we should not delete always
    if (!item.getItemContext().containsKey(SnapshotRemover.MORE_TS_SNAPSHOTS_EXISTS_FOR_GAV)) {
      final RepositoryItemUidLock uidLock = item.getRepositoryItemUid().getLock();

      uidLock.lock(Action.read);

      try {
        getNexusIndexer().deleteArtifactFromIndex(ac, context);
      }
      finally {
        uidLock.unlock();
      }
    }
    else {
      // do NOT remove file from index
      if (log.isDebugEnabled()) {
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

  @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();
    }
  }
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

  {
    // 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();
    }
  }
View Full Code Here

Examples of org.sonatype.nexus.proxy.item.RepositoryItemUidLock

      throws LocalStorageException
  {
    StorageFileItem result = null;

    try {
      final RepositoryItemUidLock itemLock = item.getRepositoryItemUid().getLock();
      itemLock.lock(Action.create);
      try {
        repository.getLocalStorage().storeItem(repository, item);
        repository.removeFromNotFoundCache(item.getResourceStoreRequest());
        final ResourceStoreRequest request = new ResourceStoreRequest(item);
        result = (StorageFileItem) repository.getLocalStorage().retrieveItem(repository, request);
      }
      finally {
        itemLock.unlock();
      }
    }
    catch (ItemNotFoundException ex) {
      log.warn(
          "Nexus BUG in "
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.