Package org.apache.aurora.scheduler.storage

Source Code of org.apache.aurora.scheduler.storage.AttributeStore$Util

/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.aurora.scheduler.storage;

import java.util.Set;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;

import org.apache.aurora.gen.MaintenanceMode;
import org.apache.aurora.scheduler.base.Conversions;
import org.apache.aurora.scheduler.storage.Storage.StoreProvider;
import org.apache.aurora.scheduler.storage.entities.IAttribute;
import org.apache.aurora.scheduler.storage.entities.IHostAttributes;
import org.apache.mesos.Protos;

/**
* Storage interface for host attributes.
*/
public interface AttributeStore {
  /**
   * Fetches all host attributes given by the host.
   *
   * @param host host name.
   * @return attributes associated with {@code host}, if the host is known.
   */
  Optional<IHostAttributes> getHostAttributes(String host);

  /**
   * Fetches all attributes in the store.
   *
   * @return All host attributes.
   */
  Set<IHostAttributes> getHostAttributes();

  /**
   * Attributes are considered mostly ephemeral and extremely low risk when inconsistency
   * is present.
   */
  interface Mutable extends AttributeStore {

    /**
     * Deletes all attributes in the store.
     */
    void deleteHostAttributes();

    /**
     * Save a host attribute in the attribute store.
     *
     * @param hostAttributes The attribute we are going to save.
     * @return {@code true} if the operation changed the attributes stored for the given
     *         {@link IHostAttributes#getHost() host}, or {@code false} if the save was a no-op.
     */
    boolean saveHostAttributes(IHostAttributes hostAttributes);
  }

  final class Util {
    private Util() {
    }

    /**
     * Fetches attributes about a {@code host}.
     *
     * @param store Store to fetch host attributes from.
     * @param host Host to fetch attributes about.
     * @return Attributes associated with {@code host}, or an empty iterable if the host is
     *         unknown.
     */
    public static Iterable<IAttribute> attributesOrNone(StoreProvider store, String host) {
      Optional<IHostAttributes> attributes = store.getAttributeStore().getHostAttributes(host);
      return attributes.isPresent()
          ? attributes.get().getAttributes() : ImmutableList.<IAttribute>of();
    }

    /**
     * Merges a desired maintenance mode with the existing attributes of a host, if present.
     *
     * @param store The store to fetch existing attributes from.
     * @param host The host to merge existing attributes from.
     * @param mode Maintenance mode to save if the host is known.
     * @return The attributes that should be saved if there were already attributes stored for
     *         the {@code host}, or {@link Optional#absent() none} if the host is unknown and
     *         attributes should not be saved.
     */
    public static Optional<IHostAttributes> mergeMode(
        AttributeStore store,
        String host,
        MaintenanceMode mode) {

      Optional<IHostAttributes> stored = store.getHostAttributes(host);
      if (stored.isPresent()) {
        return Optional.of(IHostAttributes.build(stored.get().newBuilder().setMode(mode)));
      } else {
        return Optional.absent();
      }
    }

    public static final Function<IHostAttributes, MaintenanceMode> GET_MODE =
        new Function<IHostAttributes, MaintenanceMode>() {
          @Override
          public MaintenanceMode apply(IHostAttributes attrs) {
            return attrs.getMode();
          }
        };

    /**
     * Merges the attributes from an offer, applying the default maintenance mode.
     *
     * @param store Store to fetch the existing maintenance mode for this host.
     * @param offer Offer to merge.
     * @return attributes from {@code offer} and the existing (or default) maintenance mode.
     */
    public static IHostAttributes mergeOffer(AttributeStore store, Protos.Offer offer) {
      IHostAttributes fromOffer = Conversions.getAttributes(offer);
      MaintenanceMode mode =
          store.getHostAttributes(fromOffer.getHost()).transform(GET_MODE).or(MaintenanceMode.NONE);
      return IHostAttributes.build(fromOffer.newBuilder().setMode(mode));
    }
  }
}
TOP

Related Classes of org.apache.aurora.scheduler.storage.AttributeStore$Util

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.