예제 #1
0
  private List<ResourceConsumer> getConsumers(DiscreteResource resource) {
    Versioned<ResourceConsumer> consumer = discreteConsumers.get(resource);
    if (consumer == null) {
      return ImmutableList.of();
    }

    return ImmutableList.of(consumer.value());
  }
예제 #2
0
  private boolean isAvailable(ContinuousResource resource) {
    Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id());
    if (allocation == null) {
      // no allocation (=no consumer) full registered resources available
      return true;
    }

    return hasEnoughResource(allocation.value().original(), resource, allocation.value());
  }
예제 #3
0
  @Override
  public <S, T> Optional<ResourceConsumer> getConsumer(Resource<S, T> resource) {
    checkNotNull(resource);

    Versioned<ResourceConsumer> consumer = consumers.get(resource);
    if (consumer == null) {
      return Optional.empty();
    }

    return Optional.of(consumer.value());
  }
예제 #4
0
  private List<ResourceConsumer> getConsumers(ContinuousResource resource) {
    Versioned<ContinuousResourceAllocation> allocations = continuousConsumers.get(resource.id());
    if (allocations == null) {
      return ImmutableList.of();
    }

    return allocations
        .value()
        .allocations()
        .stream()
        .filter(x -> x.resource().equals(resource))
        .map(ResourceAllocation::consumer)
        .collect(GuavaCollectors.toImmutableList());
  }
 @Override
 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
   checkNotNull(key, ERROR_NULL_KEY);
   checkNotNull(value, ERROR_NULL_VALUE);
   checkIfUnmodifiable();
   return database
       .putAndGet(name, keyCache.getUnchecked(key), serializer.encode(value))
       .thenApply(this::unwrapResult)
       .thenApply(
           v -> {
             Versioned<byte[]> rawNewValue = v.newValue();
             return new Versioned<>(
                 serializer.decode(rawNewValue.value()),
                 rawNewValue.version(),
                 rawNewValue.creationTime());
           });
 }
예제 #6
0
 @Override
 public void event(MapEvent<String, byte[]> event) {
   if (event.key().equals(name)) {
     final MeteringAgent.Context newTimer = monitor.startTimer(NOTIFY_LISTENER);
     byte[] rawNewValue = Versioned.valueOrNull(event.newValue());
     byte[] rawOldValue = Versioned.valueOrNull(event.oldValue());
     try {
       listener.event(
           new AtomicValueEvent<>(
               name,
               rawNewValue == null ? null : serializer.decode(rawNewValue),
               rawOldValue == null ? null : serializer.decode(rawOldValue)));
       newTimer.stop(null);
     } catch (Exception e) {
       newTimer.stop(e);
       Throwables.propagate(e);
     }
   }
 }
 @Override
 public CompletableFuture<Optional<Versioned<V>>> replaceAndGet(
     K key, long oldVersion, V newValue) {
   checkNotNull(key, ERROR_NULL_KEY);
   checkNotNull(newValue, ERROR_NULL_VALUE);
   checkIfUnmodifiable();
   return database
       .replaceAndGet(name, keyCache.getUnchecked(key), oldVersion, serializer.encode(newValue))
       .thenApply(this::unwrapResult)
       .thenApply(
           v -> {
             if (v.updated()) {
               Versioned<byte[]> rawNewValue = v.newValue();
               return Optional.of(
                   new Versioned<>(
                       serializer.decode(rawNewValue.value()),
                       rawNewValue.version(),
                       rawNewValue.creationTime()));
             } else {
               return Optional.empty();
             }
           });
 }