-
Notifications
You must be signed in to change notification settings - Fork 599
HDDS-14561. SCMRatisRequest/ResponseProto should use the shaded protobuf from Ratis #9733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
e87984a
c0b3145
4aa8930
eaa58a0
8eeaa6e
298f5de
ac43a62
2351cd1
df9720b
b1955b1
d2702dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,34 +17,35 @@ | |
|
|
||
| package org.apache.hadoop.hdds.scm.ha.io; | ||
|
|
||
| import com.google.protobuf.ByteString; | ||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.Message; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import org.apache.hadoop.hdds.scm.ha.ReflectionUtil; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException; | ||
|
|
||
| /** | ||
| * {@link Codec} for {@link Message} objects. | ||
| * {@link Codec} implementation for non-shaded | ||
| * {@link com.google.protobuf.Message} objects. | ||
| */ | ||
| public class GeneratedMessageCodec implements Codec { | ||
|
|
||
| @Override | ||
| public ByteString serialize(Object object) { | ||
| return ((Message)object).toByteString(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to support both com.google.protobuf.Message and org.apache.ratis.thirdparty.com.google.protobuf.Message? If yes, use two GeneratedMessageCodec classes instead of using one class. Also, we need to put them to CodecFactory. //CodecFactory
codecs.put(com.google.protobuf.Message.class, new GeneratedMessageCodec());
codecs.put(Message.class, new ScmGeneratedMessageCodec()); |
||
| public ByteString serialize(Object object) | ||
| throws org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException { | ||
| final byte[] bytes = ((Message) object).toByteString().toByteArray(); | ||
| return ByteString.copyFrom(bytes); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use unsafeWarp as below? UnsafeByteOperations.unsafeWrap(((Message) object).toByteString().asReadOnlyByteBuffer()); |
||
| } | ||
|
|
||
| @Override | ||
| public Message deserialize(Class<?> type, ByteString value) | ||
| throws InvalidProtocolBufferException { | ||
| public Object deserialize(Class<?> type, ByteString value) | ||
| throws org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep |
||
| try { | ||
| return (Message) ReflectionUtil.getMethod(type, | ||
| "parseFrom", byte[].class) | ||
| return ReflectionUtil.getMethod(type, "parseFrom", byte[].class) | ||
| .invoke(null, (Object) value.toByteArray()); | ||
| } catch (NoSuchMethodException | IllegalAccessException | ||
| | InvocationTargetException ex) { | ||
| | InvocationTargetException ex) { | ||
| ex.printStackTrace(); | ||
| throw new InvalidProtocolBufferException( | ||
| "Message cannot be decoded: " + ex.getMessage()); | ||
| throw new InvalidProtocolBufferException("Message cannot be decoded: " + ex.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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.hadoop.hdds.scm.ha.io; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import org.apache.hadoop.hdds.scm.ha.ReflectionUtil; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.Message; | ||
|
|
||
| /** | ||
| * {@link Codec} for {@link Message} objects. | ||
| */ | ||
| public class ScmGeneratedMessageCodec implements Codec { | ||
|
|
||
| @Override | ||
| public ByteString serialize(Object object) throws InvalidProtocolBufferException { | ||
| return ((Message)object).toByteString(); | ||
| } | ||
|
|
||
| @Override | ||
| public Message deserialize(Class<?> type, ByteString value) | ||
| throws InvalidProtocolBufferException { | ||
| try { | ||
| return (Message) ReflectionUtil.getMethod(type, | ||
| "parseFrom", byte[].class) | ||
| .invoke(null, (Object) value.toByteArray()); | ||
| } catch (NoSuchMethodException | IllegalAccessException | ||
| | InvocationTargetException ex) { | ||
| ex.printStackTrace(); | ||
| throw new InvalidProtocolBufferException( | ||
| "Message cannot be decoded: " + ex.getMessage()); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, filed HDDS-14623 for removing ProtoUtils.