private void storeResourcesAffinity()

in server/src/jetbrains/buildServer/sharedResources/server/SharedResourcesStartBuildPrecondition.java [260:297]


  private void storeResourcesAffinity(@NotNull final BuildPromotionEx promotion,
                                      @NotNull final String projectId,
                                      @NotNull final Map<Resource, TakenLock> takenLocks,
                                      @NotNull final Collection<Lock> locksToTake,
                                      @NotNull final DistributionDataAccessor accessor,
                                      final boolean emulationMode) {
    if (emulationMode) return;

    final Map<String, Resource> resources = myResources.getResourcesMap(projectId);
    final Map<String, String> affinityMap = new HashMap<>();
    for (Lock lock: locksToTake) {
      if (lock.getType() != LockType.READ) continue;

      Resource r = resources.get(lock.getName());
      if (r instanceof CustomResource) {
        if (lock.isAnyValueLock()) {
          // if lock is ANY lock -> choose next available value
          final String nextVal = getNextAvailableValue((CustomResource)r, takenLocks, accessor);
          if (nextVal == null) {
            LOG.warn("Could not find a free shared resource value for promotion: " + promotion + ", resource: " + r);
          } else {
            affinityMap.put(r.getId(), nextVal);
          }
        } else {
          // if lock is SPECIFIC lock - choose lock value
          affinityMap.put(r.getId(), lock.getValue());
        }
      }
    }

    if (!affinityMap.isEmpty()) {
      // store assigned values in affinity set to be used by other builds inside current distribution cycle
      accessor.getReservedValuesProvider().rememberReservedValues(promotion, affinityMap);

      // store assigned value from resource affinity inside build promotion
      affinityMap.forEach((resourceId, value) -> promotion.setAttribute(getReservedResourceAttributeKey(resourceId), value));
    }
  }