/** * Returns the real BucketRegion that is currently being locally hosted. Returns null if the real * bucket is null or if it is still being initialized. After the bucket is intialized isHosting * will be flagged true and future calls to this method will return the bucket. * * @return the real bucket if currently hosted or null */ public BucketRegion getHostedBucketRegion() { if (this.advisor.isHosting()) { return this.realBucket; } else { return null; } }
@Override public boolean isHosting() { return this.advisor.isHosting(); }
/** * Get the number of members that are hosting the bucket, and have finished initialization. * * This method is currently only used to check the bucket redundancy just before creating the * bucket. If it is used more frequently, it might be better to cache this count. */ private int getNumInitializedBuckets() { Profile[] locProfiles = this.profiles; // grab current profiles int count = 0; for (Profile profile : locProfiles) { BucketProfile bucketProfile = (BucketProfile) profile; if (bucketProfile.isHosting) { count++; } } if (isHosting()) { count++; } return count; }
@Override public boolean isHosting() { return getBucketAdvisor().isHosting(); }
public boolean areAllBucketsHosted(final Set<Integer> bucketSet) { // boolean arr[] = new boolean[]{false, true, false, true , false , false , false , false }; // Random random = new Random(); // int num = random.nextInt(7); // System.out.println("PRDS.verifyBuckets returning " + arr[num]); // return arr[num]; for (Integer bucket : bucketSet) { if (!this.partitionedRegion.getRegionAdvisor().getBucketAdvisor(bucket).isHosting()) { return false; } } return true; }
/** * Sets primaryMember and notifies all. Caller must be synced on this. * * @param id the member to use as primary for this bucket */ void setPrimaryMember(InternalDistributedMember id) { if (!getDistributionManager().getId().equals(id)) { // volunteerForPrimary handles primary state change if its our id if (isHosting()) { requestPrimaryState(OTHER_PRIMARY_HOSTING); } else { requestPrimaryState(OTHER_PRIMARY_NOT_HOSTING); } } this.primaryMember.set(id); this.everHadPrimary = true; if (id != null && id.equals(primaryElector)) { primaryElector = null; } this.notifyAll(); // wake up any threads in waitForPrimaryMember }
/** * Notify this proxy of the real bucket as its target. Future calls to this instance will then * proxy them back to the real bucket. * * @param br the real bucket which will be the target for this proxy */ public void setBucketRegion(BucketRegion br) { // fix several bugs including 36881... creation of BR may be occurring // at same time another thread is destroying the PR and now that this // BR is visible to the destroy thread we want to prevent sending bogus // CreateRegion or profile update messages this.partitionedRegion.checkReadiness(); this.partitionedRegion.checkClosed(); Assert.assertTrue(this.realBucket == null); Assert.assertTrue(!this.advisor.isHosting()); this.realBucket = br; }
private boolean continueVolunteering() { synchronized (BucketAdvisor.this) { // false if caller is not the volunteeringThread if (!Thread.currentThread().equals(this.volunteeringThread)) { return false; } if (!isVolunteering() && !isBecomingPrimary()) { return false; } // false if primaryMember is not null if (!isAggressive() && basicGetPrimaryMember() != null) { return false; } // false if this member is already primary if (isPrimary()) { return false; } // false if closed if (isClosed()) { return false; } // false if no longer hosting if (!isHosting()) { return false; } // must be true... need to continue volunteering return true; } }
/** * Return (and possibly choose) a thread-sticky member from whose data store this bucket's values * should be read * * @return member to use for reads, null if none available */ public InternalDistributedMember getPreferredNode() { if (isHosting()) { getPartitionedRegionStats().incPreferredReadLocal(); return getDistributionManager().getId(); } Profile locProfiles[] = this.profiles; // volatile read if (locProfiles.length == 0) { return null; } getPartitionedRegionStats().incPreferredReadRemote(); if (locProfiles.length == 1) { // only one choice! return locProfiles[0].peerMemberId; } // Pick one at random. int i = myRand.nextInt(locProfiles.length); return locProfiles[i].peerMemberId; }
protected void acquirePrimaryForRestOfTheBucket() { List<FixedPartitionAttributesImpl> fpas = this.pRegion.getFixedPartitionAttributesImpl(); if (fpas != null) { int bucketId = getBucket().getId(); for (FixedPartitionAttributesImpl fpa : fpas) { if (fpa.getStartingBucketID() == bucketId) { for (int i = bucketId + 1; i <= fpa.getLastBucketID();) { Bucket b = regionAdvisor.getBucket(i++); if (b != null) { BucketAdvisor ba = b.getBucketAdvisor(); ba.primaryMoveWriteLock.lock(); try { if (ba.isHosting()) { if (!ba.isPrimary()) { ba.setVolunteering(); ba.acquiredPrimaryLock(); } } } finally { ba.primaryMoveWriteLock.unlock(); } } } } else { continue; } } } }
/** * Releases the primary lock for this bucket. */ protected void releasePrimaryLock() { // We don't have a lock if we have a parent advisor if (parentAdvisor != null) { return; } assignStartingBucketAdvisorIfFixedPartitioned(); if (startingBucketAdvisor != null) { return; } try { DistributedMemberLock thePrimaryLock = getPrimaryLock(false); if (thePrimaryLock != null) { thePrimaryLock.unlock(); } } catch (LockNotHeldException e) { Assert.assertTrue(!isHosting(), "Got LockNotHeldException for Bucket = " + this); } catch (LockServiceDestroyedException e) { Assert.assertTrue(isClosed(), "BucketAdvisor was not closed before destroying PR lock service"); } }
@Override protected boolean operateOnPartitionedRegion(ClusterDistributionManager dm, PartitionedRegion region, long startTime) throws ForceReattemptException { // this is executing in the WAITING_POOL_EXECUTOR byte responseCode = BecomePrimaryBucketReplyMessage.NOT_SECONDARY; BucketAdvisor bucketAdvisor = region.getRegionAdvisor().getBucketAdvisor(this.bucketId); if (bucketAdvisor.isHosting()) { if (bucketAdvisor.becomePrimary(this.isRebalance)) { // sends a request/reply message responseCode = BecomePrimaryBucketReplyMessage.OK; } } region.getPrStats().endPartitionMessagesProcessing(startTime); BecomePrimaryBucketReplyMessage.send(getSender(), getProcessorId(), dm, null, responseCode); return false; }
doCallRealMethod().when(advisorSpy).volunteerForPrimary(); doReturn(true).when(advisorSpy).initializationGate(); doReturn(true).when(advisorSpy).isHosting();
/** * Attempt to acquire lock for primary until a primary exists. Caller hands off responsibility to * an executor (waiting pool) and returns early. */ public void volunteerForPrimary() { InternalDistributedMember elector = primaryElector; if (elector != null && regionAdvisor.hasPartitionedRegion(elector)) { // another server will determine the primary node return; } primaryElector = null; initializationGate(); synchronized (this) { if (isVolunteering() || isClosed() || !isHosting()) { // only one thread should be attempting to volunteer at one time return; } if (this.volunteeringDelegate == null) { setVolunteeringDelegate(new VolunteeringDelegate()); } this.volunteeringDelegate.volunteerForPrimary(); } }
public void setHosting(boolean value) { if (value) { PartitionedRegion region = this.getPartitionedRegion(); Assert.assertTrue(this.realBucket != null); Assert.assertTrue(!this.advisor.isHosting()); if (region.isFixedPartitionedRegion()) { List<FixedPartitionAttributesImpl> list = region.getFixedPartitionAttributesImpl(); if (list != null) { for (FixedPartitionAttributesImpl info : list) { if (info.hasBucket(bid)) { this.advisor.setHosting(true); break; } } } } else { // normal PR this.advisor.setHosting(true); } } else { // Assert.assertTrue(!getPartitionedRegion().getDataStore().isManagingBucket(this.bid)); this.advisor.setHosting(false); this.realBucket = null; } }
if (updateRedundancy() > 0 && isHosting()) {
/** * Move the primary of a bucket to the target member * * @param target the member which should be primary * @param bucketId the identifier of the bucket * @return true if the move was successful */ public boolean movePrimaryBucketForRegion(InternalDistributedMember target, int bucketId) { boolean movedPrimary = false; if (getLeaderRegion().getDistributionManager().getId().equals(target)) { // invoke directly on local member... BucketAdvisor bucketAdvisor = getLeaderRegion().getRegionAdvisor().getBucketAdvisor(bucketId); if (bucketAdvisor.isHosting()) { movedPrimary = bucketAdvisor.becomePrimary(isRebalance); } } else { // send message to remote member... BecomePrimaryBucketResponse response = BecomePrimaryBucketMessage.send(target, getLeaderRegion(), bucketId, isRebalance); if (response != null) { movedPrimary = response.waitForResponse(); } } return movedPrimary; }
try { synchronized (this) { if (isHosting() && (isVolunteering() || isBecomingPrimary())) { Bucket br = this.regionAdvisor.getBucket(getBucket().getId()); if (br != null && br instanceof BucketRegion) {
if (bucketAdvisor.isHosting()) { bucketAdvisor.clearPrimaryElector(); bucketAdvisor.volunteerForPrimary();