Skip to content

Commit

Permalink
GEODE-4772: Enhance new protocol with ability to clear region of all …
Browse files Browse the repository at this point in the history
…entries. (apache#1590)

* GEODE-4772: Enhance region with ability to clear.

* GEODE-4772: Address review comments.

* GEODE-4772: Spotless!!!!!
  • Loading branch information
PivotalSarge authored Mar 12, 2018
1 parent 0908acb commit 3939595
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public void putAll(Map<K, V> values) throws IOException {
}
}

@Override
public void clear() throws IOException {
final Message request = Message.newBuilder()
.setClearRequest(RegionAPI.ClearRequest.newBuilder().setRegionName(name)).build();

protobufChannel.sendRequest(request, MessageTypeCase.CLEARRESPONSE);
}

@Override
public void remove(K key) throws IOException {
final Message request = Message.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public interface Region<K, V> {
*/
void putAll(Map<K, V> values) throws IOException;

/**
* Removes all keys and values associated from this region.
*
* @throws IOException
*/
void clear() throws IOException;

/**
* Removes any value associated with the <code>key</code> from this region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,15 @@ public void removeWithBadJSONKey() throws IOException {
Region<JSONWrapper, JSONWrapper> region = driver.getRegion("region");
region.remove(JSONWrapper.wrapJSON("{\"name\":\"Johan\"]"));
}

@Test
public void clearNonEmptyRegion() throws Exception {
Region<String, String> region = driver.getRegion("region");

region.put("key", "value");
assertEquals(1, region.size());

region.clear();
assertEquals(0, region.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ message Message {

DisconnectClientRequest disconnectClientRequest = 30;
DisconnectClientResponse disconnectClientResponse = 31;

ClearRequest clearRequest = 32;
ClearResponse clearResponse = 33;
}
}

Expand Down
7 changes: 7 additions & 0 deletions geode-protobuf-messages/src/main/proto/v1/region_API.proto
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,10 @@ message KeySetRequest {
message KeySetResponse {
repeated EncodedValue keys = 1;
}

message ClearRequest {
string regionName = 1;
}

message ClearResponse {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.protocol.protobuf.v1.operations;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.apache.geode.annotations.Experimental;
import org.apache.geode.cache.Region;
import org.apache.geode.internal.exception.InvalidExecutionContextException;
import org.apache.geode.internal.protocol.operations.ProtobufOperationHandler;
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
import org.apache.geode.internal.protocol.protobuf.v1.Failure;
import org.apache.geode.internal.protocol.protobuf.v1.MessageExecutionContext;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException;
import org.apache.geode.security.ResourcePermission;

@Experimental
public class ClearRequestOperationHandler
implements ProtobufOperationHandler<RegionAPI.ClearRequest, RegionAPI.ClearResponse> {
private static final Logger logger = LogManager.getLogger();

@Override
public Result<RegionAPI.ClearResponse> process(ProtobufSerializationService serializationService,
RegionAPI.ClearRequest request, MessageExecutionContext messageExecutionContext)
throws InvalidExecutionContextException, DecodingException {

String regionName = request.getRegionName();
Region region = messageExecutionContext.getCache().getRegion(regionName);
if (region == null) {
logger.error("Received clear request for nonexistent region: {}", regionName);
return Failure.of(BasicTypes.ErrorCode.SERVER_ERROR,
"Region \"" + regionName + "\" not found");
}

region.clear();

return Success.of(RegionAPI.ClearResponse.newBuilder().build());
}

public static ResourcePermission determineRequiredPermission(RegionAPI.ClearRequest request,
ProtobufSerializationService serializer) throws DecodingException {
return new ResourcePermission(ResourcePermission.Resource.DATA,
ResourcePermission.Operation.WRITE, request.getRegionName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol.Message.MessageTypeCase;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufOperationContext;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.operations.ClearRequestOperationHandler;
import org.apache.geode.internal.protocol.protobuf.v1.operations.DisconnectClientRequestOperationHandler;
import org.apache.geode.internal.protocol.protobuf.v1.operations.ExecuteFunctionOnGroupRequestOperationHandler;
import org.apache.geode.internal.protocol.protobuf.v1.operations.ExecuteFunctionOnMemberRequestOperationHandler;
Expand Down Expand Up @@ -167,5 +168,11 @@ private void addContexts() {
new KeySetOperationHandler(),
opsResp -> ClientProtocol.Message.newBuilder().setKeySetResponse(opsResp),
KeySetOperationHandler::determineRequiredPermission));

operationContexts.put(ClientProtocol.Message.MessageTypeCase.CLEARREQUEST,
new ProtobufOperationContext<>(ClientProtocol.Message::getClearRequest,
new ClearRequestOperationHandler(),
opsResp -> ClientProtocol.Message.newBuilder().setClearResponse(opsResp),
ClearRequestOperationHandler::determineRequiredPermission));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.apache.geode.internal.protocol.protobuf.ProtocolVersion;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufUtilities;

public class MessageUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.internal.protocol.protobuf.v1.utilities;
package org.apache.geode.internal.protocol.protobuf.v1;

import java.util.Set;

Expand Down Expand Up @@ -43,6 +43,18 @@ public static ClientProtocol.Message createGetRequest(String regionName,
return ClientProtocol.Message.newBuilder().setGetRequest(getRequest).build();
}

/**
* Creates a request object containing a RegionAPI.ClearRequest
*
* @param regionName - Name of the region being cleared
* @return Request object containing the passed params.
*/
public static ClientProtocol.Message createClearRequest(String regionName) {
RegionAPI.ClearRequest clearRequest =
RegionAPI.ClearRequest.newBuilder().setRegionName(regionName).build();
return ClientProtocol.Message.newBuilder().setClearRequest(clearRequest).build();
}

/**
* Creates a request object containing a RegionAPI.RemoveRequest
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol;
import org.apache.geode.internal.protocol.protobuf.v1.MessageUtil;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.internal.protocol.protobuf.v1.serializer.ProtobufProtocolSerializer;
import org.apache.geode.internal.protocol.protobuf.v1.serializer.exception.InvalidProtocolMessageException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufUtilities;
import org.apache.geode.test.junit.categories.IntegrationTest;
import org.apache.geode.util.test.TestUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol;
import org.apache.geode.internal.protocol.protobuf.v1.ConnectionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.MessageUtil;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.serializer.ProtobufProtocolSerializer;
import org.apache.geode.internal.protocol.protobuf.v1.state.exception.ConnectionStateException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufUtilities;
import org.apache.geode.security.SimpleTestSecurityManager;
import org.apache.geode.test.dunit.Host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol;
import org.apache.geode.internal.protocol.protobuf.v1.LocatorAPI;
import org.apache.geode.internal.protocol.protobuf.v1.MessageUtil;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.serializer.ProtobufProtocolSerializer;
import org.apache.geode.internal.protocol.protobuf.v1.serializer.exception.InvalidProtocolMessageException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.test.dunit.DistributedTestUtils;
import org.apache.geode.test.dunit.Host;
import org.apache.geode.test.dunit.IgnoredException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.protocol.protobuf.v1.operations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import org.apache.geode.cache.Region;
import org.apache.geode.internal.protocol.TestExecutionContext;
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol;
import org.apache.geode.internal.protocol.protobuf.v1.Failure;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.test.junit.categories.UnitTest;

@Category(UnitTest.class)
public class ClearRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest {
private final String TEST_REGION = "test region";
private final String MISSING_REGION = "missing region";
private Region regionStub;

@Before
public void setUp() throws Exception {
regionStub = mock(Region.class);

when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionStub);
when(cacheStub.getRegion(MISSING_REGION)).thenReturn(null);
operationHandler = new ClearRequestOperationHandler();
}

@Test
public void processReturnsSuccessForValidRegion() throws Exception {
RegionAPI.ClearRequest removeRequest =
ProtobufRequestUtilities.createClearRequest(TEST_REGION).getClearRequest();
Result result = operationHandler.process(serializationService, removeRequest,
TestExecutionContext.getNoAuthCacheExecutionContext(cacheStub));

assertTrue(result instanceof Success);
}

@Test
public void processReturnsFailureForInvalidRegion() throws Exception {
RegionAPI.ClearRequest removeRequest =
ProtobufRequestUtilities.createClearRequest(MISSING_REGION).getClearRequest();
Result result = operationHandler.process(serializationService, removeRequest,
TestExecutionContext.getNoAuthCacheExecutionContext(cacheStub));

assertTrue(result instanceof Failure);
ClientProtocol.ErrorResponse errorMessage = result.getErrorMessage();
assertEquals(BasicTypes.ErrorCode.SERVER_ERROR, errorMessage.getError().getErrorCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
import org.apache.geode.cache.Region;
import org.apache.geode.internal.protocol.TestExecutionContext;
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.test.junit.categories.UnitTest;

@Category(UnitTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.internal.protocol.TestExecutionContext;
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufUtilities;
import org.apache.geode.pdx.PdxInstance;
import org.apache.geode.pdx.PdxReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

import org.apache.geode.cache.Region;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.test.junit.categories.UnitTest;

@Category(UnitTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol;
import org.apache.geode.internal.protocol.protobuf.v1.Failure;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufOpsProcessor;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.registry.ProtobufOperationContextRegistry;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.test.junit.categories.UnitTest;

@Category(UnitTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.apache.geode.internal.protocol.protobuf.v1.Failure;
import org.apache.geode.internal.protocol.protobuf.v1.LocatorAPI;
import org.apache.geode.internal.protocol.protobuf.v1.LocatorAPI.GetServerResponse;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.test.junit.categories.UnitTest;

@Category(UnitTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol;
import org.apache.geode.internal.protocol.protobuf.v1.Failure;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.test.junit.categories.UnitTest;

@Category(UnitTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
import org.apache.geode.cache.Region;
import org.apache.geode.internal.protocol.TestExecutionContext;
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.ProtobufSerializationService;
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
import org.apache.geode.internal.protocol.protobuf.v1.Result;
import org.apache.geode.internal.protocol.protobuf.v1.Success;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException;
import org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.EncodingException;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufRequestUtilities;
import org.apache.geode.internal.protocol.protobuf.v1.utilities.ProtobufUtilities;
import org.apache.geode.test.dunit.Assert;
import org.apache.geode.test.junit.categories.UnitTest;
Expand Down
Loading

0 comments on commit 3939595

Please sign in to comment.