All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.cache.RequiredRoles Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.cache;

import com.gemstone.gemfire.distributed.Role;
import com.gemstone.gemfire.internal.Assert;
import com.gemstone.gemfire.internal.cache.DistributedRegion;
import com.gemstone.gemfire.internal.cache.LocalRegion;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;

import java.util.*;

/**
 * Provides information on presence or absence of a Region's 
 * required roles. Configuration of required roles is accomplished using the
 * Region's {@link MembershipAttributes}. 
 *
 * A {@link com.gemstone.gemfire.distributed.Role Role} may be present in the
 * distributed system even if it the Role is not present in the
 * Region membership. This would occur if none of the members
 * filling that Role currently have a Cache or the
 * specific Region created. In this case the Role is
 * considered to be absent for that Region.
 *
 * @author Kirk Lund
 * @see com.gemstone.gemfire.distributed.Role
 * @since 5.0
 */
public class RequiredRoles {
  
  /**
   * Returns a set of any currently missing required roles for the
   * specified region.  If the region is not configured to require roles
   * an empty set will always be returned.
   *
   * @param region the region to check for missing required roles
   * @return set of required roles that are currently missing
   * @throws IllegalStateException if region is not configured with required roles
   */
  public static Set checkForRequiredRoles(Region region) {
    try {
      return waitForRequiredRoles(region, 0);
    }
    catch (InterruptedException ie) {
      // This could happen if we were in an interrupted state
      // upon method entry
      Thread.currentThread().interrupt();
      ((LocalRegion)region).getCancelCriterion().checkCancelInProgress(ie);
      Assert.assertTrue(false, 
          "checkForRequiredRoles cannot throw InterruptedException");
      return Collections.emptySet(); // keep compiler happy
    }
  }

  /**
   * Returns a set of any currently missing required roles for the
   * specified region.  This will wait the specified timeout in milliseconds 
   * for any missing required roles to be filled.  If there are no missing 
   * required roles or if the region is not configured to require any roles
   * then an empty set will immediately be returned.
   *
   * @param region the region to check for missing required roles
   * @param timeout milliseconds to wait for any missing required roles
   * @return set of required roles that are currently missing
   * @throws NullPointerException if region is null
   * @throws InterruptedException if thread is interrupted while waiting
   * @throws IllegalStateException if region is not configured with required roles
   */
  public static Set waitForRequiredRoles(Region region, long timeout)
  throws InterruptedException {
//    if (Thread.interrupted()) throw new InterruptedException(); not necessary waitForRequiredRoles does this
    if (region == null) {
      throw new NullPointerException(LocalizedStrings.RequiredRoles_REGION_MUST_BE_SPECIFIED.toLocalizedString());
    }
    if (!(region instanceof DistributedRegion)) {
      throw new IllegalStateException(LocalizedStrings.RequiredRoles_REGION_HAS_NOT_BEEN_CONFIGURED_WITH_REQUIRED_ROLES.toLocalizedString());
    }
    DistributedRegion dr = (DistributedRegion) region;
    return dr.waitForRequiredRoles(timeout);
  }
  
  /**
   * Returns true if the {@link com.gemstone.gemfire.distributed.Role Role}
   * is currently present in the {@link Region} membership. This returns true
   * only if one or more members filling this role actually have the region
   * currently created. The role may be present in the distributed system even
   * if the role is not present in the region membership.
   *
   * @param region the region whose membership will be searched
   * @param role the role to check for
   */
  public static boolean isRoleInRegionMembership(Region region, Role role) {
    if (region instanceof DistributedRegion) {
      DistributedRegion dr = (DistributedRegion) region;
      return dr.isRoleInRegionMembership(role);
    }
    else {
      return role.isPresent();
    }
  }
  
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy