DonalEvans commented on a change in pull request #6386: URL: https://github.com/apache/geode/pull/6386#discussion_r623372178
########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/PartitionedRegionClearMessageTest.java ########## @@ -0,0 +1,285 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.geode.internal.cache; + +import static java.util.Collections.emptySet; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.Operation; +import org.apache.geode.cache.RegionEvent; +import org.apache.geode.distributed.DistributedMember; +import org.apache.geode.distributed.internal.ClusterDistributionManager; +import org.apache.geode.distributed.internal.DistributionAdvisor; +import org.apache.geode.distributed.internal.DistributionManager; +import org.apache.geode.distributed.internal.ReplyProcessor21; +import org.apache.geode.distributed.internal.membership.InternalDistributedMember; +import org.apache.geode.internal.cache.PartitionedRegionClearMessage.OperationType; + +public class PartitionedRegionClearMessageTest { + + private Collection<InternalDistributedMember> recipients; + private DistributionManager distributionManager; + private PartitionedRegion partitionedRegion; + private ReplyProcessor21 replyProcessor21; + private Object callbackArgument; + private EventID eventId; + private RegionEventFactory regionEventFactory; + + @Before + public void setUp() { + recipients = emptySet(); + distributionManager = mock(DistributionManager.class); + partitionedRegion = mock(PartitionedRegion.class); + replyProcessor21 = mock(ReplyProcessor21.class); + callbackArgument = new Object(); + eventId = mock(EventID.class); + regionEventFactory = mock(RegionEventFactory.class); + } + + @Test + public void construction_throwsNullPointerExceptionIfRecipientsIsNull() { + Throwable thrown = catchThrowable(() -> { + new PartitionedRegionClearMessage(null, distributionManager, 1, + replyProcessor21, OperationType.OP_PR_CLEAR, callbackArgument, eventId, false, + regionEventFactory); + }); + + assertThat(thrown).isInstanceOf(NullPointerException.class); + } + + @Test + public void construction_findsAllDependencies() { + boolean isTransactionDistributed = true; + int regionId = 10; + InternalCache cache = mock(InternalCache.class); + RegionEventImpl regionEvent = mock(RegionEventImpl.class); + TXManagerImpl txManager = mock(TXManagerImpl.class); + when(cache.getTxManager()).thenReturn(txManager); + when(partitionedRegion.getCache()).thenReturn(cache); + when(partitionedRegion.getDistributionManager()).thenReturn(distributionManager); + when(partitionedRegion.getPRId()).thenReturn(regionId); + when(regionEvent.getEventId()).thenReturn(eventId); + when(regionEvent.getRawCallbackArgument()).thenReturn(callbackArgument); + when(txManager.isDistributed()).thenReturn(isTransactionDistributed); + + PartitionedRegionClearMessage message = new PartitionedRegionClearMessage(recipients, + partitionedRegion, + replyProcessor21, + OperationType.OP_PR_CLEAR, + regionEvent); + + assertThat(message.getDistributionManagerForTesting()).isSameAs(distributionManager); + assertThat(message.getCallbackArgumentForTesting()).isSameAs(callbackArgument); + assertThat(message.getRegionId()).isEqualTo(regionId); + assertThat(message.getEventID()).isEqualTo(eventId); + assertThat(message.isTransactionDistributed()).isEqualTo(isTransactionDistributed); + + RegionEventFactory regionEventFactory = message.getRegionEventFactoryForTesting(); + RegionEvent<?, ?> created = + regionEventFactory.create(partitionedRegion, Operation.DESTROY, callbackArgument, false, + mock(DistributedMember.class), mock(EventID.class)); + assertThat(created).isInstanceOf(RegionEventImpl.class); + } + + @Test + public void construction_setsTransactionDistributed() { Review comment: This test case seems to be covered in the test above it. Is it necessary to have a seperate test for it here too? ########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/PartitionedRegionClearMessageTest.java ########## @@ -0,0 +1,285 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.geode.internal.cache; + +import static java.util.Collections.emptySet; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.Operation; +import org.apache.geode.cache.RegionEvent; +import org.apache.geode.distributed.DistributedMember; +import org.apache.geode.distributed.internal.ClusterDistributionManager; +import org.apache.geode.distributed.internal.DistributionAdvisor; +import org.apache.geode.distributed.internal.DistributionManager; +import org.apache.geode.distributed.internal.ReplyProcessor21; +import org.apache.geode.distributed.internal.membership.InternalDistributedMember; +import org.apache.geode.internal.cache.PartitionedRegionClearMessage.OperationType; + +public class PartitionedRegionClearMessageTest { + + private Collection<InternalDistributedMember> recipients; + private DistributionManager distributionManager; + private PartitionedRegion partitionedRegion; + private ReplyProcessor21 replyProcessor21; + private Object callbackArgument; + private EventID eventId; + private RegionEventFactory regionEventFactory; + + @Before + public void setUp() { + recipients = emptySet(); + distributionManager = mock(DistributionManager.class); + partitionedRegion = mock(PartitionedRegion.class); + replyProcessor21 = mock(ReplyProcessor21.class); + callbackArgument = new Object(); + eventId = mock(EventID.class); + regionEventFactory = mock(RegionEventFactory.class); + } + + @Test + public void construction_throwsNullPointerExceptionIfRecipientsIsNull() { + Throwable thrown = catchThrowable(() -> { + new PartitionedRegionClearMessage(null, distributionManager, 1, + replyProcessor21, OperationType.OP_PR_CLEAR, callbackArgument, eventId, false, + regionEventFactory); + }); + + assertThat(thrown).isInstanceOf(NullPointerException.class); + } + + @Test + public void construction_findsAllDependencies() { + boolean isTransactionDistributed = true; + int regionId = 10; + InternalCache cache = mock(InternalCache.class); + RegionEventImpl regionEvent = mock(RegionEventImpl.class); + TXManagerImpl txManager = mock(TXManagerImpl.class); + when(cache.getTxManager()).thenReturn(txManager); + when(partitionedRegion.getCache()).thenReturn(cache); + when(partitionedRegion.getDistributionManager()).thenReturn(distributionManager); + when(partitionedRegion.getPRId()).thenReturn(regionId); + when(regionEvent.getEventId()).thenReturn(eventId); + when(regionEvent.getRawCallbackArgument()).thenReturn(callbackArgument); + when(txManager.isDistributed()).thenReturn(isTransactionDistributed); + + PartitionedRegionClearMessage message = new PartitionedRegionClearMessage(recipients, + partitionedRegion, + replyProcessor21, + OperationType.OP_PR_CLEAR, + regionEvent); + + assertThat(message.getDistributionManagerForTesting()).isSameAs(distributionManager); + assertThat(message.getCallbackArgumentForTesting()).isSameAs(callbackArgument); + assertThat(message.getRegionId()).isEqualTo(regionId); + assertThat(message.getEventID()).isEqualTo(eventId); + assertThat(message.isTransactionDistributed()).isEqualTo(isTransactionDistributed); + + RegionEventFactory regionEventFactory = message.getRegionEventFactoryForTesting(); + RegionEvent<?, ?> created = + regionEventFactory.create(partitionedRegion, Operation.DESTROY, callbackArgument, false, + mock(DistributedMember.class), mock(EventID.class)); + assertThat(created).isInstanceOf(RegionEventImpl.class); + } + + @Test + public void construction_setsTransactionDistributed() { + boolean isTransactionDistributed = true; + PartitionedRegionClearMessage message = + new PartitionedRegionClearMessage(recipients, distributionManager, 1, + replyProcessor21, OperationType.OP_PR_CLEAR, callbackArgument, eventId, + isTransactionDistributed, regionEventFactory); + + boolean value = message.isTransactionDistributed(); + + assertThat(value).isEqualTo(isTransactionDistributed); + } + + @Test + public void getEventID_returnsTheEventId() { Review comment: This test case seem to be covered in `construction_findsAllDependencies()`. Is it necessary to test it here too? ########## File path: geode-core/src/main/java/org/apache/geode/internal/cache/DistributedRegion.java ########## @@ -2082,30 +2082,31 @@ private void distributedUnlockForClear() { } } - /** * obtain locks preventing generation of new versions in other members */ protected void obtainWriteLocksForClear(RegionEventImpl regionEvent, - Set<InternalDistributedMember> participants, boolean localLockedAlready) { - if (!localLockedAlready) { - lockLocallyForClear(getDistributionManager(), getMyId(), regionEvent); - } - lockAndFlushClearToOthers(regionEvent, participants); + Set<InternalDistributedMember> recipients) { + lockLocallyForClear(getDistributionManager(), getMyId(), regionEvent); Review comment: It might just be me misunderstanding the ticket description, but it made it seem like this call would be removed, rather than just the conditional around it. Why is the local locking still present here? ########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/BucketRegionJUnitTest.java ########## @@ -211,4 +204,48 @@ public void updateSizeToZeroOnClearBucketRegion() { long sizeAfterClear = region.getTotalBytes(); assertEquals(0, sizeAfterClear); } + + @Test + public void obtainWriteLocksForClearInBRShouldLockAndFlushToOthers() { Review comment: It's unclear to me what this test case is testing that's different from the subsequent test case. Both appear to be verifying that `lockAndFlushClearToOthers()` is called when `obtainWriteLocksForClear()` is called on the BucketRegion. ########## File path: geode-core/src/main/java/org/apache/geode/internal/cache/BucketRegion.java ########## @@ -577,22 +578,38 @@ public void cmnClearRegion(RegionEventImpl regionEvent, boolean cacheWrite, bool // get rvvLock Set<InternalDistributedMember> participants = getCacheDistributionAdvisor().adviseInvalidateRegion(); - boolean isLockedAlready = this.partitionedRegion.getPartitionedRegionClear() - .isLockedForListenerAndClientNotification(); try { - obtainWriteLocksForClear(regionEvent, participants, isLockedAlready); + obtainWriteLocksForClear(regionEvent, participants); // no need to dominate my own rvv. // Clear is on going here, there won't be GII for this member clearRegionLocally(regionEvent, cacheWrite, null); distributeClearOperation(regionEvent, null, participants); // TODO: call reindexUserDataRegion if there're lucene indexes } finally { - releaseWriteLocksForClear(regionEvent, participants, isLockedAlready); + releaseWriteLocksForClear(regionEvent, participants); } } + @Override + protected void obtainWriteLocksForClear(RegionEventImpl regionEvent, + Set<InternalDistributedMember> participants) { + lockAndFlushClearToOthers(regionEvent, participants); + } + + @Override + protected void releaseWriteLocksForClear(RegionEventImpl regionEvent, + Set<InternalDistributedMember> participants) { + distributedClearOperationReleaseLocks(regionEvent, participants); + } + + @VisibleForTesting + void distributedClearOperationReleaseLocks(RegionEventImpl regionEvent, Review comment: I'm a little unclear on why this method exists in both `BucketRegion` and `DistributedRegion` with identical implementation, but `lockAndFlushClearToOthers()` exists only in `DistributedRegion`. Also, in terms of naming consistency, could this method either be renamed "releaseLocks" or could `lockAndFlushClearToOthers()` be renamed "distributedClearOperationLockAndFlushClearToOthers"? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
