Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 67244200CDB for ; Sat, 22 Jul 2017 02:01:15 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 654C516E16F; Sat, 22 Jul 2017 00:01:15 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id DEACB16E16D for ; Sat, 22 Jul 2017 02:01:12 +0200 (CEST) Received: (qmail 3927 invoked by uid 500); 22 Jul 2017 00:01:12 -0000 Mailing-List: contact commits-help@geode.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.apache.org Delivered-To: mailing list commits@geode.apache.org Received: (qmail 3908 invoked by uid 99); 22 Jul 2017 00:01:11 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Jul 2017 00:01:11 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D75BEDF9AB; Sat, 22 Jul 2017 00:01:09 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: udo@apache.org To: commits@geode.apache.org Date: Sat, 22 Jul 2017 00:01:10 -0000 Message-Id: <08995f237e1647dbbe0a5ea8523a109f@git.apache.org> In-Reply-To: <501a6739bcc549bb91a430d18ed368cc@git.apache.org> References: <501a6739bcc549bb91a430d18ed368cc@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/2] geode git commit: GEODE-3213: Refactor ProtoBuf handler flow. This now closes #646 archived-at: Sat, 22 Jul 2017 00:01:15 -0000 GEODE-3213: Refactor ProtoBuf handler flow. This now closes #646 Signed-off-by: Alexander Murmann Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/64eab455 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/64eab455 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/64eab455 Branch: refs/heads/develop Commit: 64eab4559900f2f9e3c30a62e6daa45c422c22ac Parents: 208f5ae Author: Galen O'Sullivan Authored: Mon Jul 17 11:18:57 2017 -0700 Committer: Udo Kohlmeyer Committed: Fri Jul 21 17:00:47 2017 -0700 ---------------------------------------------------------------------- .../GenericProtocolServerConnection.java | 1 - .../protocol/operations/OperationHandler.java | 5 +- .../registry/OperationsHandlerRegistry.java | 52 --------- ...rationHandlerAlreadyRegisteredException.java | 25 ----- .../OperationHandlerNotRegisteredException.java | 24 ----- .../protobuf/EncodingTypeTranslator.java | 4 +- .../apache/geode/protocol/protobuf/Failure.java | 45 ++++++++ .../protocol/protobuf/OperationContext.java | 57 ++++++++++ .../protocol/protobuf/ProtobufOpsProcessor.java | 27 ++--- .../protobuf/ProtobufStreamProcessor.java | 66 +++--------- .../apache/geode/protocol/protobuf/Result.java | 26 +++++ .../apache/geode/protocol/protobuf/Success.java | 45 ++++++++ .../GetAllRequestOperationHandler.java | 49 ++++----- .../GetRegionNamesRequestOperationHandler.java | 17 +-- .../GetRegionRequestOperationHandler.java | 19 ++-- .../operations/GetRequestOperationHandler.java | 43 ++++---- .../PutAllRequestOperationHandler.java | 86 ++++++--------- .../operations/PutRequestOperationHandler.java | 50 ++++----- .../RemoveRequestOperationHandler.java | 37 ++++--- .../registry/OperationContextRegistry.java | 79 ++++++++++++++ .../serializer/ProtobufProtocolSerializer.java | 8 +- .../utilities/ProtobufRequestUtilities.java | 25 +++-- .../utilities/ProtobufResponseUtilities.java | 105 ++----------------- .../protobuf/utilities/ProtobufUtilities.java | 36 +++++-- .../protocol/serializer/ProtocolSerializer.java | 4 +- .../geode/serialization/codec/BooleanCodec.java | 4 +- .../geode/serialization/codec/ByteCodec.java | 4 +- .../geode/serialization/codec/DoubleCodec.java | 4 +- .../geode/serialization/codec/FloatCodec.java | 4 +- .../geode/serialization/codec/IntCodec.java | 4 +- .../geode/serialization/codec/LongCodec.java | 4 +- .../geode/serialization/codec/ShortCodec.java | 4 +- .../geode/serialization/codec/StringCodec.java | 4 +- .../registry/SerializationCodecRegistry.java | 6 +- .../geode/protocol/IntegrationJUnitTest.java | 30 ++++-- .../org/apache/geode/protocol/MessageUtil.java | 8 +- .../RoundTripCacheConnectionJUnitTest.java | 79 +++++++------- .../OperationsHandlerRegistryJUnitTest.java | 92 ---------------- ...eToSerializationTypeTranslatorJUnitTest.java | 9 +- .../protobuf/ProtobufOpsProcessorJUnitTest.java | 59 ----------- .../protobuf/ProtobufStreamProcessorTest.java | 11 +- .../GetAllRequestOperationHandlerJUnitTest.java | 55 +++++----- ...onNamesRequestOperationHandlerJUnitTest.java | 47 ++++----- ...tRegionRequestOperationHandlerJUnitTest.java | 38 +++---- .../GetRequestOperationHandlerJUnitTest.java | 86 ++++++++------- .../operations/OperationHandlerJUnitTest.java | 5 +- .../PutAllRequestOperationHandlerJUnitTest.java | 56 +++++----- .../PutRequestOperationHandlerJUnitTest.java | 70 ++++++++----- .../RemoveRequestOperationHandlerJUnitTest.java | 63 +++++------ .../ProtobufProtocolSerializerJUnitTest.java | 21 ++-- .../ProtobufSerializationServiceJUnitTest.java | 9 +- .../codec/BinaryFormatJUnitTest.java | 11 +- .../registry/CodecRegistryJUnitTest.java | 11 +- 53 files changed, 810 insertions(+), 923 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/GenericProtocolServerConnection.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/GenericProtocolServerConnection.java b/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/GenericProtocolServerConnection.java index b155d7e..76b3b7e 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/GenericProtocolServerConnection.java +++ b/geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/GenericProtocolServerConnection.java @@ -52,7 +52,6 @@ public class GenericProtocolServerConnection extends ServerConnection { InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); - // TODO serialization types? messageHandler.receiveMessage(inputStream, outputStream, this.getCache()); } catch (IOException e) { logger.warn(e); http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java index 8a064a8..92a844f 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java @@ -15,8 +15,8 @@ package org.apache.geode.protocol.operations; import org.apache.geode.cache.Cache; -import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.ProtobufOpsProcessor; +import org.apache.geode.protocol.protobuf.Result; import org.apache.geode.serialization.SerializationService; /** @@ -30,5 +30,6 @@ public interface OperationHandler { * Decode the message, deserialize contained values using the serialization service, do the work * indicated on the provided cache, and return a response. */ - Resp process(SerializationService serializationService, Req request, Cache cache); + Result process(SerializationService serializationService, Req request, Cache cache); } + http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java deleted file mode 100644 index b2021af..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.protocol.operations.registry; - -import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; - -import java.util.HashMap; - -/** - * This class tracks which operation handlers are expected to handle which types of operations. - */ -public class OperationsHandlerRegistry { - private HashMap registeredOperations = new HashMap<>(); - - public OperationHandler getOperationHandlerForOperationId(int operationCode) - throws OperationHandlerNotRegisteredException { - OperationHandler operationHandler = registeredOperations.get(operationCode); - if (operationHandler == null) { - throw new OperationHandlerNotRegisteredException( - "There is no operation handler registered for operation code: " + operationCode); - } - return operationHandler; - } - - public synchronized void registerOperationHandlerForOperationId(int operationCode, - OperationHandler operationHandler) throws OperationHandlerAlreadyRegisteredException { - if (registeredOperations.containsKey(operationCode)) { - throw new OperationHandlerAlreadyRegisteredException( - "An operation handler for operationCode: " + operationCode - + " has already been registered!"); - } - registeredOperations.put(operationCode, operationHandler); - } - - public int getRegisteredOperationHandlersCount() { - return registeredOperations.size(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java deleted file mode 100644 index 18475d8..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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.protocol.operations.registry.exception; - -/** - * Indicates that an operation handler is attempting to register for an already handled operation - * type. - */ -public class OperationHandlerAlreadyRegisteredException extends Exception { - public OperationHandlerAlreadyRegisteredException(String message) { - super(message); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java deleted file mode 100644 index 6772e56..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.protocol.operations.registry.exception; - -/** - * Indicates that we're trying to handle an operation for which no operation handler has registered. - */ -public class OperationHandlerNotRegisteredException extends Exception { - public OperationHandlerNotRegisteredException(String message) { - super(message); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java index da43f4b..1f65b9e 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java @@ -14,13 +14,13 @@ */ package org.apache.geode.protocol.protobuf; +import java.util.HashMap; + import org.apache.geode.pdx.JSONFormatter; import org.apache.geode.pdx.PdxInstance; import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import java.util.HashMap; - /** * This class maps protobuf specific encoding types and the corresponding serialization types. */ http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Failure.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Failure.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Failure.java new file mode 100644 index 0000000..fb75c26 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Failure.java @@ -0,0 +1,45 @@ +/* + * 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.protocol.protobuf; + +import java.util.function.Function; + +public class Failure implements Result { + private final ClientProtocol.ErrorResponse errorResponse; + + public Failure(ClientProtocol.ErrorResponse errorResponse) { + this.errorResponse = errorResponse; + } + + public static Failure of(ClientProtocol.ErrorResponse errorResponse) { + return new Failure<>(errorResponse); + } + + @Override + public T map(Function successFunction, + Function errorFunction) { + return errorFunction.apply(errorResponse); + } + + @Override + public SuccessType getMessage() { + throw new RuntimeException("This is not a Success result"); + } + + @Override + public ClientProtocol.ErrorResponse getErrorMessage() { + return errorResponse; + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/OperationContext.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/OperationContext.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/OperationContext.java new file mode 100644 index 0000000..9b234f2 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/OperationContext.java @@ -0,0 +1,57 @@ +/* + * 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.protocol.protobuf; + +import java.util.function.Function; + +import org.apache.geode.protocol.operations.OperationHandler; + +public class OperationContext { + private final OperationHandler operationHandler; + private final Function fromRequest; + private final Function toResponse; + private final Function toErrorResponse; + + public OperationContext(Function fromRequest, + OperationHandler operationHandler, + Function toResponse) { + this.operationHandler = operationHandler; + this.fromRequest = fromRequest; + this.toResponse = toResponse; + this.toErrorResponse = OperationContext::makeErrorBuilder; + } + + public static ClientProtocol.Response.Builder makeErrorBuilder( + ClientProtocol.ErrorResponse errorResponse) { + return ClientProtocol.Response.newBuilder().setErrorResponse(errorResponse); + } + + public OperationHandler getOperationHandler() { + return operationHandler; + } + + public Function getFromRequest() { + return fromRequest; + } + + public Function getToResponse() { + return toResponse; + } + + public Function getToErrorResponse() { + return toErrorResponse; + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java index 13f2b00..c11b534 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java @@ -15,10 +15,7 @@ package org.apache.geode.protocol.protobuf; import org.apache.geode.cache.Cache; -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; +import org.apache.geode.protocol.protobuf.registry.OperationContextRegistry; import org.apache.geode.serialization.SerializationService; /** @@ -26,21 +23,25 @@ import org.apache.geode.serialization.SerializationService; * it to the appropriate handler. */ public class ProtobufOpsProcessor { - private final OperationsHandlerRegistry opsHandlerRegistry; + + private final OperationContextRegistry operationContextRegistry; private final SerializationService serializationService; - public ProtobufOpsProcessor(OperationsHandlerRegistry opsHandlerRegistry, - SerializationService serializationService) { - this.opsHandlerRegistry = opsHandlerRegistry; + public ProtobufOpsProcessor(SerializationService serializationService, + OperationContextRegistry operationContextRegistry) { this.serializationService = serializationService; + this.operationContextRegistry = operationContextRegistry; } - public ClientProtocol.Response process(ClientProtocol.Request request, Cache cache) - throws OperationHandlerNotRegisteredException, InvalidProtocolMessageException { + public ClientProtocol.Response process(ClientProtocol.Request request, Cache cache) { ClientProtocol.Request.RequestAPICase requestType = request.getRequestAPICase(); - OperationHandler opsHandler = - opsHandlerRegistry.getOperationHandlerForOperationId(requestType.getNumber()); + OperationContext operationContext = operationContextRegistry.getOperationContext(requestType); + ClientProtocol.Response.Builder builder; + Result result = operationContext.getOperationHandler().process(serializationService, + operationContext.getFromRequest().apply(request), cache); - return (ClientProtocol.Response) opsHandler.process(serializationService, request, cache); + builder = (ClientProtocol.Response.Builder) result.map(operationContext.getToResponse(), + operationContext.getToErrorResponse()); + return builder.build(); } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java index 87bc38f..118ccc4 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java @@ -14,77 +14,37 @@ */ package org.apache.geode.protocol.protobuf; +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + import org.apache.geode.cache.Cache; import org.apache.geode.internal.cache.InternalCache; import org.apache.geode.internal.cache.tier.sockets.ClientProtocolMessageHandler; import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; -import org.apache.geode.protocol.protobuf.operations.GetAllRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.GetRegionNamesRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.GetRegionRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.GetRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.PutAllRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.PutRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.RemoveRequestOperationHandler; +import org.apache.geode.protocol.protobuf.registry.OperationContextRegistry; import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; -import java.io.EOFException; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - /** * This object handles an incoming stream containing protobuf messages. It parses the protobuf * messages, hands the requests to an appropriate handler, wraps the response in a protobuf message, * and then pushes it to the output stream. */ public class ProtobufStreamProcessor implements ClientProtocolMessageHandler { - private ProtobufProtocolSerializer protobufProtocolSerializer; - private OperationsHandlerRegistry registry; - private ProtobufSerializationService protobufSerializationService; - private ProtobufOpsProcessor protobufOpsProcessor; + private final ProtobufProtocolSerializer protobufProtocolSerializer; + private final ProtobufOpsProcessor protobufOpsProcessor; - public ProtobufStreamProcessor() - throws OperationHandlerAlreadyRegisteredException, CodecAlreadyRegisteredForTypeException { + public ProtobufStreamProcessor() throws CodecAlreadyRegisteredForTypeException { protobufProtocolSerializer = new ProtobufProtocolSerializer(); - registry = new OperationsHandlerRegistry(); - - addOperationHandlers(registry); - protobufSerializationService = new ProtobufSerializationService(); - protobufOpsProcessor = new ProtobufOpsProcessor(registry, protobufSerializationService); - } - - private void addOperationHandlers(OperationsHandlerRegistry registry) - throws OperationHandlerAlreadyRegisteredException { - registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.GETREQUEST.getNumber(), - new GetRequestOperationHandler()); - registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.PUTREQUEST.getNumber(), - new PutRequestOperationHandler()); - registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.GETREGIONNAMESREQUEST.getNumber(), - new GetRegionNamesRequestOperationHandler()); - registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.GETALLREQUEST.getNumber(), - new GetAllRequestOperationHandler()); - registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.PUTALLREQUEST.getNumber(), - new PutAllRequestOperationHandler()); - registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.REMOVEREQUEST.getNumber(), - new RemoveRequestOperationHandler()); - registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.GETREGIONREQUEST.getNumber(), - new GetRegionRequestOperationHandler()); + protobufOpsProcessor = new ProtobufOpsProcessor(new ProtobufSerializationService(), + new OperationContextRegistry()); } public void processOneMessage(InputStream inputStream, OutputStream outputStream, Cache cache) - throws InvalidProtocolMessageException, OperationHandlerNotRegisteredException, IOException { + throws InvalidProtocolMessageException, IOException { ClientProtocol.Message message = protobufProtocolSerializer.deserialize(inputStream); if (message == null) { throw new EOFException("Tried to deserialize protobuf message at EOF"); @@ -104,7 +64,7 @@ public class ProtobufStreamProcessor implements ClientProtocolMessageHandler { InternalCache cache) throws IOException { try { processOneMessage(inputStream, outputStream, cache); - } catch (InvalidProtocolMessageException | OperationHandlerNotRegisteredException e) { + } catch (InvalidProtocolMessageException e) { throw new IOException(e); } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Result.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Result.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Result.java new file mode 100644 index 0000000..f22d845 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Result.java @@ -0,0 +1,26 @@ +/* + * 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.protocol.protobuf; + +import java.util.function.Function; + +public interface Result { + T map(Function successFunction, + Function errorFunction); + + SuccessType getMessage(); + + ClientProtocol.ErrorResponse getErrorMessage(); +} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Success.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Success.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Success.java new file mode 100644 index 0000000..4bb07b8 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/Success.java @@ -0,0 +1,45 @@ +/* + * 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.protocol.protobuf; + +import java.util.function.Function; + +public class Success implements Result { + private final SuccessType successResponse; + + public Success(SuccessType successResponse) { + this.successResponse = successResponse; + } + + public static Success of(T result) { + return new Success<>(result); + } + + @Override + public T map(Function successFunction, + Function errorFunction) { + return successFunction.apply(successResponse); + } + + @Override + public SuccessType getMessage() { + return successResponse; + } + + @Override + public ClientProtocol.ErrorResponse getErrorMessage() { + throw new RuntimeException("This is a not Failure result"); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandler.java index 75ae842..e3401c8 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandler.java @@ -14,62 +14,55 @@ */ package org.apache.geode.protocol.protobuf.operations; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.Failure; import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.protocol.protobuf.Result; +import org.apache.geode.protocol.protobuf.Success; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; public class GetAllRequestOperationHandler - implements OperationHandler { - private static Logger logger = LogManager.getLogger(); + implements OperationHandler { @Override - public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { - if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.GETALLREQUEST) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Improperly formatted getAll request message.", logger, null); - } - RegionAPI.GetAllRequest getAllRequest = request.getGetAllRequest(); - - String regionName = getAllRequest.getRegionName(); + public Result process(SerializationService serializationService, + RegionAPI.GetAllRequest request, Cache cache) { + String regionName = request.getRegionName(); Region region = cache.getRegion(regionName); if (region == null) { - return ProtobufResponseUtilities.createErrorResponse("Region not found"); + return Failure + .of(ClientProtocol.ErrorResponse.newBuilder().setMessage("Region not found").build()); } try { Set keys = new HashSet<>(); - for (BasicTypes.EncodedValue key : getAllRequest.getKeyList()) { + for (BasicTypes.EncodedValue key : request.getKeyList()) { keys.add(ProtobufUtilities.decodeValue(serializationService, key)); } - Map results = region.getAll(keys); + Map results = region.getAll(keys); Set entries = new HashSet<>(); - for (Map.Entry entry : results.entrySet()) { + for (Map.Entry entry : results.entrySet()) { entries.add( ProtobufUtilities.createEntry(serializationService, entry.getKey(), entry.getValue())); } - return ProtobufResponseUtilities.createGetAllResponse(entries); + return Success.of(RegionAPI.GetAllResponse.newBuilder().addAllEntries(entries).build()); } catch (UnsupportedEncodingTypeException ex) { - // can be thrown by encoding or decoding. - return ProtobufResponseUtilities.createAndLogErrorResponse("Encoding not supported.", logger, - ex); + return Failure.of( + ClientProtocol.ErrorResponse.newBuilder().setMessage("Encoding not supported.").build()); } catch (CodecNotRegisteredForTypeException ex) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Codec error in protobuf deserialization.", logger, ex); + return Failure.of(ClientProtocol.ErrorResponse.newBuilder() + .setMessage("Codec error in protobuf deserialization.").build()); } } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java index 8befdd7..50e121e 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java @@ -14,21 +14,24 @@ */ package org.apache.geode.protocol.protobuf.operations; +import java.util.Set; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.Result; +import org.apache.geode.protocol.protobuf.Success; import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; import org.apache.geode.serialization.SerializationService; -import java.util.Set; - public class GetRegionNamesRequestOperationHandler - implements OperationHandler { + implements OperationHandler { + @Override - public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { + public Result process(SerializationService serializationService, + RegionAPI.GetRegionNamesRequest request, Cache cache) { Set> regions = cache.rootRegions(); - return ProtobufResponseUtilities.createGetRegionNamesResponse(regions); + return Success.of(ProtobufResponseUtilities.createGetRegionNamesResponse(regions)); } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java index 6ac951c..f2e1b37 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java @@ -19,29 +19,30 @@ import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.Failure; import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.protocol.protobuf.Result; +import org.apache.geode.protocol.protobuf.Success; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; public class GetRegionRequestOperationHandler - implements OperationHandler { + implements OperationHandler { @Override - public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { + public Result process(SerializationService serializationService, + RegionAPI.GetRegionRequest request, Cache cache) { - RegionAPI.GetRegionRequest regionRequest = request.getGetRegionRequest(); - String regionName = regionRequest.getRegionName(); + String regionName = request.getRegionName(); Region region = cache.getRegion(regionName); if (region == null) { - return ProtobufResponseUtilities - .createErrorResponse("No region exists for name: " + regionName); + return Failure.of(ClientProtocol.ErrorResponse.newBuilder() + .setMessage("No region exists for name: " + regionName).build()); } BasicTypes.Region protoRegion = ProtobufUtilities.createRegionMessageFromRegion(region); - return ProtobufResponseUtilities.createGetRegionResponse(protoRegion); + return Success.of(RegionAPI.GetRegionResponse.newBuilder().setRegion(protoRegion).build()); } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java index d5bcfb9..79c59f8 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java @@ -17,52 +17,47 @@ package org.apache.geode.protocol.protobuf.operations; import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.protobuf.*; -import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.Failure; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.Result; +import org.apache.geode.protocol.protobuf.Success; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; public class GetRequestOperationHandler - implements OperationHandler { - private static Logger logger = LogManager.getLogger(); + implements OperationHandler { @Override - public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { - if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.GETREQUEST) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Improperly formatted get request message.", logger, null); - } - RegionAPI.GetRequest getRequest = request.getGetRequest(); - - String regionName = getRequest.getRegionName(); + public Result process(SerializationService serializationService, + RegionAPI.GetRequest request, Cache cache) { + String regionName = request.getRegionName(); Region region = cache.getRegion(regionName); if (region == null) { - return ProtobufResponseUtilities.createErrorResponse("Region not found"); + return Failure + .of(ClientProtocol.ErrorResponse.newBuilder().setMessage("Region not found").build()); } try { - Object decodedKey = ProtobufUtilities.decodeValue(serializationService, getRequest.getKey()); + Object decodedKey = ProtobufUtilities.decodeValue(serializationService, request.getKey()); Object resultValue = region.get(decodedKey); if (resultValue == null) { - return ProtobufResponseUtilities.createNullGetResponse(); + return Success.of(RegionAPI.GetResponse.newBuilder().build()); } BasicTypes.EncodedValue encodedValue = ProtobufUtilities.createEncodedValue(serializationService, resultValue); - return ProtobufResponseUtilities.createGetResponse(encodedValue); + return Success.of(RegionAPI.GetResponse.newBuilder().setResult(encodedValue).build()); } catch (UnsupportedEncodingTypeException ex) { - // can be thrown by encoding or decoding. - return ProtobufResponseUtilities.createAndLogErrorResponse("Encoding not supported.", logger, - ex); + return Failure.of( + ClientProtocol.ErrorResponse.newBuilder().setMessage("Encoding not supported.").build()); } catch (CodecNotRegisteredForTypeException ex) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Codec error in protobuf deserialization.", logger, ex); + return Failure.of(ClientProtocol.ErrorResponse.newBuilder() + .setMessage("Codec error in protobuf deserialization.").build()); } } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandler.java index 7e62bba..55d2f3f 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandler.java @@ -14,80 +14,57 @@ */ package org.apache.geode.protocol.protobuf.operations; +import java.util.HashMap; +import java.util.Map; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.Failure; import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.Result; +import org.apache.geode.protocol.protobuf.Success; import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.HashMap; -import java.util.Map; public class PutAllRequestOperationHandler - implements OperationHandler { + implements OperationHandler { private static Logger logger = LogManager.getLogger(); - private RegionAPI.PutAllRequest putAllRequest = null; - private Region region = null; - private Map entries = null; - @Override - public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { - ClientProtocol.Response errorResponse = validatePutAllRequest(request); - if (errorResponse == null) { - errorResponse = determinePutAllRegion(cache); - } - if (errorResponse == null) { - errorResponse = extractPutAllEntries(serializationService); - } - if (errorResponse == null) { - try { - region.putAll(entries); - } catch (Exception ex) { - return ProtobufResponseUtilities.createAndLogErrorResponse(ex.getMessage(), logger, ex); - } + public Result process(SerializationService serializationService, + RegionAPI.PutAllRequest request, Cache cache) { + String regionName = request.getRegionName(); + Region region = cache.getRegion(regionName); - return ProtobufResponseUtilities.createPutAllResponse(); - } else { - return errorResponse; + if (region == null) { + return Failure.of(ProtobufResponseUtilities.createAndLogErrorResponse( + "Region passed by client did not exist: " + regionName, logger, null)); } - } - private ClientProtocol.Response validatePutAllRequest(ClientProtocol.Request request) { - if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.PUTALLREQUEST) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Improperly formatted put request message.", logger, null); + Map entries = extractPutAllEntries(serializationService, request); + try { + region.putAll(entries); + } catch (Exception ex) { + return Failure + .of(ProtobufResponseUtilities.createAndLogErrorResponse(ex.getMessage(), logger, ex)); } - putAllRequest = request.getPutAllRequest(); - return null; - } - - private ClientProtocol.Response determinePutAllRegion(Cache cache) { - String regionName = putAllRequest.getRegionName(); - region = cache.getRegion(regionName); - - if (region == null) { - return ProtobufResponseUtilities.createAndLogErrorResponse( - "Region passed by client did not exist: " + regionName, logger, null); - } else { - return null; - } + return Success.of(RegionAPI.PutAllResponse.newBuilder().build()); } // Read all of the entries out of the protobuf and return an error (without performing any puts) // if any of the entries can't be decoded - private ClientProtocol.Response extractPutAllEntries(SerializationService serializationService) { - entries = new HashMap(); + private Map extractPutAllEntries(SerializationService serializationService, + RegionAPI.PutAllRequest putAllRequest) { + Map entries = new HashMap(); try { for (BasicTypes.Entry entry : putAllRequest.getEntryList()) { Object decodedValue = ProtobufUtilities.decodeValue(serializationService, entry.getValue()); @@ -96,14 +73,11 @@ public class PutAllRequestOperationHandler entries.put(decodedKey, decodedValue); } } catch (UnsupportedEncodingTypeException ex) { - return ProtobufResponseUtilities.createAndLogErrorResponse("Encoding not supported ", logger, - ex); + throw new RuntimeException("This exception still needs to be handled in an ErrorMessage"); } catch (CodecNotRegisteredForTypeException ex) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Codec error in protobuf deserialization ", logger, ex); + throw new RuntimeException("This exception still needs to be handled in an ErrorMessage"); } - return null; + return entries; } - } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java index 195aa7a..be2308e 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java @@ -17,55 +17,51 @@ package org.apache.geode.protocol.protobuf.operations; import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.protobuf.*; -import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.Failure; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.Result; +import org.apache.geode.protocol.protobuf.Success; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; public class PutRequestOperationHandler - implements OperationHandler { - private static Logger logger = LogManager.getLogger(); + implements OperationHandler { @Override - public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { - if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.PUTREQUEST) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Improperly formatted put request message.", logger, null); - } - RegionAPI.PutRequest putRequest = request.getPutRequest(); - - String regionName = putRequest.getRegionName(); + public Result process(SerializationService serializationService, + RegionAPI.PutRequest request, Cache cache) { + String regionName = request.getRegionName(); Region region = cache.getRegion(regionName); if (region == null) { - return ProtobufResponseUtilities.createAndLogErrorResponse( - "Region passed by client did not exist: " + regionName, logger, null); + return Failure.of(ClientProtocol.ErrorResponse.newBuilder() + .setMessage("Region passed by client did not exist: " + regionName).build()); } try { - BasicTypes.Entry entry = putRequest.getEntry(); + BasicTypes.Entry entry = request.getEntry(); Object decodedValue = ProtobufUtilities.decodeValue(serializationService, entry.getValue()); Object decodedKey = ProtobufUtilities.decodeValue(serializationService, entry.getKey()); try { region.put(decodedKey, decodedValue); - return ProtobufResponseUtilities.createPutResponse(); + return Success.of(RegionAPI.PutResponse.newBuilder().build()); } catch (ClassCastException ex) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("invalid key or value type for region " + regionName - + ",passed key: " + entry.getKey().getEncodingType() + " value: " - + entry.getValue().getEncodingType(), logger, ex); + return Failure.of(ClientProtocol.ErrorResponse.newBuilder() + .setMessage("invalid key or value type for region " + regionName + ",passed key: " + + entry.getKey().getEncodingType() + " value: " + + entry.getValue().getEncodingType()) + .build()); } } catch (UnsupportedEncodingTypeException ex) { - return ProtobufResponseUtilities.createAndLogErrorResponse("encoding not supported ", logger, - ex); + return Failure + .of(ClientProtocol.ErrorResponse.newBuilder().setMessage(ex.getMessage()).build()); } catch (CodecNotRegisteredForTypeException ex) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("codec error in protobuf deserialization ", logger, ex); + return Failure + .of(ClientProtocol.ErrorResponse.newBuilder().setMessage(ex.getMessage()).build()); } } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java index 725a338..1058ca3 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java @@ -14,51 +14,50 @@ */ package org.apache.geode.protocol.protobuf.operations; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.Failure; import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.Result; +import org.apache.geode.protocol.protobuf.Success; import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; public class RemoveRequestOperationHandler - implements OperationHandler { + implements OperationHandler { private static Logger logger = LogManager.getLogger(); @Override - public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { - if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.REMOVEREQUEST) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Improperly formatted get request message.", logger, null); - } - RegionAPI.RemoveRequest removeRequest = request.getRemoveRequest(); + public Result process(SerializationService serializationService, + RegionAPI.RemoveRequest request, Cache cache) { - String regionName = removeRequest.getRegionName(); + String regionName = request.getRegionName(); Region region = cache.getRegion(regionName); if (region == null) { - return ProtobufResponseUtilities.createErrorResponse("Region not found"); + return Failure + .of(ClientProtocol.ErrorResponse.newBuilder().setMessage("Region not found").build()); } try { - Object decodedKey = - ProtobufUtilities.decodeValue(serializationService, removeRequest.getKey()); + Object decodedKey = ProtobufUtilities.decodeValue(serializationService, request.getKey()); region.remove(decodedKey); - return ProtobufResponseUtilities.createRemoveResponse(); + return Success.of(RegionAPI.RemoveResponse.newBuilder().build()); } catch (UnsupportedEncodingTypeException ex) { // can be thrown by encoding or decoding. - return ProtobufResponseUtilities.createAndLogErrorResponse("Encoding not supported.", logger, - ex); + return Failure.of(ProtobufResponseUtilities + .createAndLogErrorResponse("Encoding not supported.", logger, ex)); } catch (CodecNotRegisteredForTypeException ex) { - return ProtobufResponseUtilities - .createAndLogErrorResponse("Codec error in protobuf deserialization.", logger, ex); + return Failure.of(ProtobufResponseUtilities + .createAndLogErrorResponse("Codec error in protobuf deserialization.", logger, ex)); } } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/registry/OperationContextRegistry.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/registry/OperationContextRegistry.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/registry/OperationContextRegistry.java new file mode 100644 index 0000000..37bb322 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/registry/OperationContextRegistry.java @@ -0,0 +1,79 @@ +/* + * 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.protocol.protobuf.registry; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.ClientProtocol.Request.RequestAPICase; +import org.apache.geode.protocol.protobuf.OperationContext; +import org.apache.geode.protocol.protobuf.operations.GetAllRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.GetRegionNamesRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.GetRegionRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.GetRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.PutAllRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.PutRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.RemoveRequestOperationHandler; + +public class OperationContextRegistry { + private Map operationContexts = new ConcurrentHashMap<>(); + + public OperationContextRegistry() { + addContexts(); + } + + public OperationContext getOperationContext(RequestAPICase apiCase) { + return operationContexts.get(apiCase); + } + + private void addContexts() { + operationContexts.put(RequestAPICase.GETREQUEST, + new OperationContext<>(ClientProtocol.Request::getGetRequest, + new GetRequestOperationHandler(), + opsResp -> ClientProtocol.Response.newBuilder().setGetResponse(opsResp))); + + operationContexts.put(RequestAPICase.GETALLREQUEST, + new OperationContext<>(ClientProtocol.Request::getGetAllRequest, + new GetAllRequestOperationHandler(), + opsResp -> ClientProtocol.Response.newBuilder().setGetAllResponse(opsResp))); + + operationContexts.put(RequestAPICase.PUTREQUEST, + new OperationContext<>(ClientProtocol.Request::getPutRequest, + new PutRequestOperationHandler(), + opsResp -> ClientProtocol.Response.newBuilder().setPutResponse(opsResp))); + + operationContexts.put(RequestAPICase.PUTALLREQUEST, + new OperationContext<>(ClientProtocol.Request::getPutAllRequest, + new PutAllRequestOperationHandler(), + opsResp -> ClientProtocol.Response.newBuilder().setPutAllResponse(opsResp))); + + operationContexts.put(RequestAPICase.REMOVEREQUEST, + new OperationContext<>(ClientProtocol.Request::getRemoveRequest, + new RemoveRequestOperationHandler(), + opsResp -> ClientProtocol.Response.newBuilder().setRemoveResponse(opsResp))); + + operationContexts.put(RequestAPICase.GETREGIONNAMESREQUEST, + new OperationContext<>(ClientProtocol.Request::getGetRegionNamesRequest, + new GetRegionNamesRequestOperationHandler(), + opsResp -> ClientProtocol.Response.newBuilder().setGetRegionNamesResponse(opsResp))); + + operationContexts.put(RequestAPICase.GETREGIONREQUEST, + new OperationContext<>(ClientProtocol.Request::getGetRegionRequest, + new GetRegionRequestOperationHandler(), + opsResp -> ClientProtocol.Response.newBuilder().setGetRegionResponse(opsResp))); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java index 566464c..1c6e847 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java @@ -14,14 +14,14 @@ */ package org.apache.geode.protocol.protobuf.serializer; -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.protocol.serializer.ProtocolSerializer; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.serializer.ProtocolSerializer; + public class ProtobufProtocolSerializer implements ProtocolSerializer { @Override public ClientProtocol.Message deserialize(InputStream inputStream) http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java index b0f4795..01be750 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java @@ -14,12 +14,12 @@ */ package org.apache.geode.protocol.protobuf.utilities; +import java.util.Set; + import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.RegionAPI; -import java.util.Set; - /** * This class contains helper functions for generating ClientProtocol.Request objects * @@ -29,7 +29,7 @@ import java.util.Set; public abstract class ProtobufRequestUtilities { /** * Creates a request object containing a RegionAPI.GetRequest - * + * * @param regionName - Name of the region being fetched from * @param key - Encoded key, see createEncodedValue in {@link ProtobufRequestUtilities} * @return Request object containing the passed params. @@ -43,7 +43,7 @@ public abstract class ProtobufRequestUtilities { /** * Creates a request object containing a RegionAPI.RemoveRequest - * + * * @param regionName - Name of the region being deleted from * @param key - Encoded key, see createEncodedValue in {@link ProtobufRequestUtilities} * @return Request object containing the passed params. @@ -57,17 +57,16 @@ public abstract class ProtobufRequestUtilities { /** * Creates a request object containing a RegionAPI.GetRegionNamesRequest - * + * * @return Request object for a getRegionNames operation */ - public static ClientProtocol.Request createGetRegionNamesRequest() { - return ClientProtocol.Request.newBuilder() - .setGetRegionNamesRequest(RegionAPI.GetRegionNamesRequest.newBuilder()).build(); + public static RegionAPI.GetRegionNamesRequest createGetRegionNamesRequest() { + return RegionAPI.GetRegionNamesRequest.newBuilder().build(); } /** * Creates a request object containing a RegionAPI.PutRequest - * + * * @param region - Name of the region to put data in * @param entry - Encoded key,value pair, see createEntry in {@link ProtobufRequestUtilities} * @return Request object containing the passed params. @@ -80,22 +79,22 @@ public abstract class ProtobufRequestUtilities { /** * Create a request to get the values for multiple keys - * + * * @param regionName - Name of the region to fetch from * @param keys - Set of keys being fetched * @return Request object containing the getAll request */ - public static ClientProtocol.Request createGetAllRequest(String regionName, + public static RegionAPI.GetAllRequest createGetAllRequest(String regionName, Set keys) { RegionAPI.GetAllRequest.Builder getAllRequestBuilder = RegionAPI.GetAllRequest.newBuilder().setRegionName(regionName); getAllRequestBuilder.addAllKey(keys); - return ClientProtocol.Request.newBuilder().setGetAllRequest(getAllRequestBuilder).build(); + return getAllRequestBuilder.build(); } /** * Create a request to insert multiple entries in a region - * + * * @param regionName - Region to which entries are being added * @param entries - key, value pairs to add to the region * @return Request object containing the putAll request for the passed parameters http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java index 5e262d0..6da4730 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java @@ -14,13 +14,13 @@ */ package org.apache.geode.protocol.protobuf.utilities; +import java.util.Set; + +import org.apache.logging.log4j.Logger; + import org.apache.geode.cache.Region; -import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.logging.log4j.Logger; - -import java.util.Set; /** * This class contains helper functions for generating ClientProtocol.Response objects. @@ -29,17 +29,6 @@ import java.util.Set; * purpose helpers can be found in {@link ProtobufUtilities} */ public abstract class ProtobufResponseUtilities { - /** - * This creates response object containing a ClientProtocol.ErrorResponse - * - * @param errorMessage - description of the error - * @return An error response containing the above parameters - */ - public static ClientProtocol.Response createErrorResponse(String errorMessage) { - ClientProtocol.ErrorResponse error = - ClientProtocol.ErrorResponse.newBuilder().setMessage(errorMessage).build(); - return ClientProtocol.Response.newBuilder().setErrorResponse(error).build(); - } /** * This creates response object containing a ClientProtocol.ErrorResponse, and also logs the @@ -50,47 +39,14 @@ public abstract class ProtobufResponseUtilities { * @param ex - exception which should be logged * @return An error response containing the first three parameters. */ - public static ClientProtocol.Response createAndLogErrorResponse(String errorMessage, + public static ClientProtocol.ErrorResponse createAndLogErrorResponse(String errorMessage, Logger logger, Exception ex) { if (ex != null) { logger.error(errorMessage, ex); } else { logger.error(errorMessage); } - return createErrorResponse(errorMessage); - } - - /** - * This creates a response object containing a RegionAPI.GetResponse - * - * @param resultValue - the encoded result value, see createEncodedValue in - * {@link ProtobufUtilities} - * @return A response indicating the passed value was found for a incoming GetRequest - */ - public static ClientProtocol.Response createGetResponse(BasicTypes.EncodedValue resultValue) { - RegionAPI.GetResponse getResponse = - RegionAPI.GetResponse.newBuilder().setResult(resultValue).build(); - return ClientProtocol.Response.newBuilder().setGetResponse(getResponse).build(); - } - - /** - * This creates a response object containing a RegionAPI.RemoveResponse - * - * @return A response indicating the entry with the passed key was removed - */ - public static ClientProtocol.Response createRemoveResponse() { - RegionAPI.RemoveResponse removeResponse = RegionAPI.RemoveResponse.newBuilder().build(); - return ClientProtocol.Response.newBuilder().setRemoveResponse(removeResponse).build(); - } - - /** - * This creates a response object containing a RegionAPI.GetResponse - * - * @return A response indicating a failure to find a requested key or value - */ - public static ClientProtocol.Response createNullGetResponse() { - return ClientProtocol.Response.newBuilder().setGetResponse(RegionAPI.GetResponse.newBuilder()) - .build(); + return ClientProtocol.ErrorResponse.newBuilder().setMessage(errorMessage).build(); } /** @@ -99,56 +55,13 @@ public abstract class ProtobufResponseUtilities { * @param regionSet - A set of regions * @return A response object containing the names of the regions in the passed regionSet */ - public static ClientProtocol.Response createGetRegionNamesResponse(Set> regionSet) { + public static RegionAPI.GetRegionNamesResponse createGetRegionNamesResponse( + Set> regionSet) { RegionAPI.GetRegionNamesResponse.Builder builder = RegionAPI.GetRegionNamesResponse.newBuilder(); for (Region region : regionSet) { builder.addRegions(region.getName()); } - return ClientProtocol.Response.newBuilder().setGetRegionNamesResponse(builder).build(); - } - - /** - * This creates a response object containing a RegionAPI.PutResponse - * - * @return A response object indicating a successful put - */ - public static ClientProtocol.Response createPutResponse() { - return ClientProtocol.Response.newBuilder().setPutResponse(RegionAPI.PutResponse.newBuilder()) - .build(); - } - - /** - * This creates a response object containing a RegionAPI.GetAllResponse - * - * @param entries - key, value pairs for which lookups succeeded - * @return A response object containing all the passed results - */ - public static ClientProtocol.Response createGetAllResponse(Set entries) { - RegionAPI.GetAllResponse.Builder builder = RegionAPI.GetAllResponse.newBuilder(); - builder.addAllEntries(entries); - return ClientProtocol.Response.newBuilder().setGetAllResponse(builder).build(); - } - - /** - * This creates a response for a putAll request - * - * @return A response object indicating any invalid keys (all others are assumed to have - * succeeded) - */ - public static ClientProtocol.Response createPutAllResponse() { - return ClientProtocol.Response.newBuilder() - .setPutAllResponse(RegionAPI.PutAllResponse.newBuilder()).build(); - } - - /** - * This creates a ClientProtocol.Response object containing a RegionAPI.GetRegionResponse - * - * @return A ClientProtocol.Response object indicating a successful getRegionRequest - */ - public static ClientProtocol.Response createGetRegionResponse(BasicTypes.Region protoRegion) { - return ClientProtocol.Response.newBuilder() - .setGetRegionResponse(RegionAPI.GetRegionResponse.newBuilder().setRegion(protoRegion)) - .build(); + return builder.build(); } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java index 0f2628c..242f5e3 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java @@ -22,6 +22,7 @@ import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; import org.apache.geode.protocol.protobuf.ProtobufSerializationService; +import org.apache.geode.protocol.protobuf.RegionAPI; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; @@ -38,7 +39,7 @@ import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTy public abstract class ProtobufUtilities { /** * Creates a object containing the type and value encoding of a piece of data - * + * * @param serializationService - object which knows how to encode objects for the protobuf * protocol {@link ProtobufSerializationService} * @param unencodedValue - the value object which is to be encoded @@ -60,7 +61,7 @@ public abstract class ProtobufUtilities { /** * Creates a protobuf key,value pair from an encoded key and value - * + * * @param key - an EncodedValue containing the key of the entry * @param value - an EncodedValue containing the value of the entry * @return a protobuf Entry object containing the passed key and value @@ -72,7 +73,7 @@ public abstract class ProtobufUtilities { /** * Creates a protobuf key,value pair from unencoded data - * + * * @param serializationService - object which knows how to encode objects for the protobuf * protocol {@link ProtobufSerializationService} * @param unencodedKey - the unencoded key for the entry @@ -92,7 +93,7 @@ public abstract class ProtobufUtilities { /** * This creates a protobuf message containing a ClientProtocol.Response - * + * * @param messageHeader - The header for the message * @param response - The response for the message * @return a protobuf Message containing the above parameters @@ -105,20 +106,31 @@ public abstract class ProtobufUtilities { /** * This creates a protobuf message containing a ClientProtocol.Request - * + * * @param messageHeader - The header for the message * @param request - The request for the message * @return a protobuf Message containing the above parameters */ - public static ClientProtocol.Message createProtobufRequest( + public static ClientProtocol.Message createProtobufMessage( ClientProtocol.MessageHeader messageHeader, ClientProtocol.Request request) { return ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request) .build(); } /** + * This creates a protobuf message containing a ClientProtocol.Request + * + * @param getAllRequest - The request for the message + * @return a protobuf Message containing the above parameters + */ + public static ClientProtocol.Request createProtobufRequestWithGetAllRequest( + RegionAPI.GetAllRequest getAllRequest) { + return ClientProtocol.Request.newBuilder().setGetAllRequest(getAllRequest).build(); + } + + /** * This builds the MessageHeader for a response which matches an incoming request - * + * * @param request - The request message that we're responding to. * @return the MessageHeader the response to the passed request */ @@ -129,7 +141,7 @@ public abstract class ProtobufUtilities { /** * This creates a MessageHeader - * + * * @param correlationId - An identifier used to correlate requests and responses * @return a MessageHeader containing the above parameters */ @@ -139,7 +151,7 @@ public abstract class ProtobufUtilities { /** * This will return the object encoded in a protobuf EncodedValue - * + * * @param serializationService - object which knows how to encode objects for the protobuf * protocol {@link ProtobufSerializationService} * @param encodedValue - The value to be decoded @@ -181,4 +193,10 @@ public abstract class ProtobufUtilities { protoRegionBuilder.setDataPolicy(regionAttributes.getDataPolicy().toString()); return protoRegionBuilder.build(); } + + public static ClientProtocol.Request createProtobufRequestWithGetRegionNamesRequest( + RegionAPI.GetRegionNamesRequest getRegionNamesRequest) { + return ClientProtocol.Request.newBuilder().setGetRegionNamesRequest(getRegionNamesRequest) + .build(); + } } http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java index 46e491f..0a48e1b 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java @@ -14,12 +14,12 @@ */ package org.apache.geode.protocol.serializer; -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; + /** * This interface is used to translate between binary data and protocol specific messages. * http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java index e3e234d..d9f2d07 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/BooleanCodec.java @@ -14,11 +14,11 @@ */ package org.apache.geode.serialization.codec; +import java.nio.ByteBuffer; + import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.TypeCodec; -import java.nio.ByteBuffer; - public class BooleanCodec implements TypeCodec { @Override public Boolean decode(byte[] incoming) { http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java index 10db041..8e71149 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/ByteCodec.java @@ -14,11 +14,11 @@ */ package org.apache.geode.serialization.codec; +import java.nio.ByteBuffer; + import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.TypeCodec; -import java.nio.ByteBuffer; - public class ByteCodec implements TypeCodec { @Override public Byte decode(byte[] incoming) { http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java index 0bcb852..ab09537 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/DoubleCodec.java @@ -14,11 +14,11 @@ */ package org.apache.geode.serialization.codec; +import java.nio.ByteBuffer; + import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.TypeCodec; -import java.nio.ByteBuffer; - public class DoubleCodec implements TypeCodec { @Override public Double decode(byte[] incoming) { http://git-wip-us.apache.org/repos/asf/geode/blob/64eab455/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java index c41914b..5ff79ce 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/FloatCodec.java @@ -14,11 +14,11 @@ */ package org.apache.geode.serialization.codec; +import java.nio.ByteBuffer; + import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.TypeCodec; -import java.nio.ByteBuffer; - public class FloatCodec implements TypeCodec { @Override public Float decode(byte[] incoming) {