public boolean requiresInitialization(FileLock lock) {
      if (!didRebuild) {
        if (validator != null && !validator.isValid()) {
          LOGGER.debug(
              "Invalidating {} as cache validator return false.",
              DefaultPersistentDirectoryCache.this);
          return true;
        }
      }

      if (!lock.getUnlockedCleanly()) {
        if (!lock.getState().isInInitialState()) {
          LOGGER.warn(
              "Invalidating {} as it was not closed cleanly.",
              DefaultPersistentDirectoryCache.this);
        }
        return true;
      }

      Properties cachedProperties = GUtil.loadProperties(propertiesFile);
      for (Map.Entry<?, ?> entry : properties.entrySet()) {
        String previousValue = cachedProperties.getProperty(entry.getKey().toString());
        String currentValue = entry.getValue().toString();
        if (!previousValue.equals(currentValue)) {
          LOGGER.debug(
              "Invalidating {} as cache property {} has changed from {} to {}.",
              DefaultPersistentDirectoryCache.this,
              entry.getKey(),
              previousValue,
              currentValue);
          return true;
        }
      }
      return false;
    }
Ejemplo n.º 2
0
  private boolean onStartWork() {
    if (fileLock != null) {
      return false;
    }
    fileLock =
        lockManager.lock(
            lockFile,
            lockOptions.withMode(Exclusive),
            cacheDisplayName,
            operations.getDescription());
    stateAtOpen = fileLock.getState();
    for (UnitOfWorkParticipant cache : caches) {
      cache.onStartWork(operations.getDescription(), stateAtOpen);
    }

    lockManager.allowContention(fileLock, whenContended());

    return true;
  }
Ejemplo n.º 3
0
 private void closeFileLock() {
   try {
     cacheClosedCount++;
     try {
       // Close the caches and then notify them of the final state, in case the caches do work on
       // close
       new CompositeStoppable().add(caches).stop();
       FileLock.State state = fileLock.getState();
       for (MultiProcessSafePersistentIndexedCache cache : caches) {
         cache.onEndWork(state);
       }
     } finally {
       fileLock.close();
     }
   } finally {
     fileLock = null;
     stateAtOpen = null;
     contended = false;
   }
 }
Ejemplo n.º 4
0
 /**
  * Opens this cache access with the given lock mode. Calling this with {@link
  * org.gradle.cache.internal.FileLockManager.LockMode#Exclusive} will lock the cache for exclusive
  * access from all other threads (including those in this process and all other processes), until
  * {@link #close()} is called.
  *
  * @param lockOptions
  */
 public void open(LockOptions lockOptions) {
   lock.lock();
   try {
     if (owner != null) {
       throw new IllegalStateException(
           String.format("Cannot open the %s, as it is already in use.", cacheDisplayName));
     }
     this.lockOptions = lockOptions;
     if (lockOptions.getMode() == FileLockManager.LockMode.None) {
       return;
     }
     if (fileLock != null) {
       throw new IllegalStateException("File lock " + lockFile + " is already open.");
     }
     fileLock = lockManager.lock(lockFile, lockOptions, cacheDisplayName);
     stateAtOpen = fileLock.getState();
     takeOwnership(String.format("Access %s", cacheDisplayName));
     lockManager.allowContention(fileLock, whenContended());
   } finally {
     lock.unlock();
   }
 }