-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-8572. Support CodecBuffer for protobuf v3 codecs. #4693
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/db/DelegatedCodec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * 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.utils.db; | ||
|
|
||
| import org.apache.ratis.util.function.CheckedFunction; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.io.IOException; | ||
| import java.util.function.IntFunction; | ||
|
|
||
| /** | ||
| * A {@link Codec} to serialize/deserialize objects by delegation. | ||
| * | ||
| * @param <T> The object type of this {@link Codec}. | ||
| * @param <DELEGATE> The object type of the {@link #delegate}. | ||
| */ | ||
| public class DelegatedCodec<T, DELEGATE> implements Codec<T> { | ||
| private final Codec<DELEGATE> delegate; | ||
| private final CheckedFunction<DELEGATE, T, IOException> forward; | ||
| private final CheckedFunction<T, DELEGATE, IOException> backward; | ||
| private final boolean immutable; | ||
|
|
||
| /** | ||
| * Construct a {@link Codec} using the given delegate. | ||
| * | ||
| * @param delegate the delegate {@link Codec} | ||
| * @param forward a function to convert {@link DELEGATE} to {@link T}. | ||
| * @param backward a function to convert {@link T} back to {@link DELEGATE}. | ||
| * @param immutable are the objects in {@link T} immutable? | ||
| */ | ||
| public DelegatedCodec(Codec<DELEGATE> delegate, | ||
| CheckedFunction<DELEGATE, T, IOException> forward, | ||
| CheckedFunction<T, DELEGATE, IOException> backward, | ||
| boolean immutable) { | ||
| this.delegate = delegate; | ||
| this.forward = forward; | ||
| this.backward = backward; | ||
| this.immutable = immutable; | ||
| } | ||
|
|
||
| /** The same as new DelegatedCodec(delegate, forward, backward, false). */ | ||
| public DelegatedCodec(Codec<DELEGATE> delegate, | ||
| CheckedFunction<DELEGATE, T, IOException> forward, | ||
| CheckedFunction<T, DELEGATE, IOException> backward) { | ||
| this(delegate, forward, backward, false); | ||
| } | ||
|
|
||
| @Override | ||
| public final boolean supportCodecBuffer() { | ||
| return delegate.supportCodecBuffer(); | ||
| } | ||
|
|
||
| @Override | ||
| public final CodecBuffer toCodecBuffer(@Nonnull T message, | ||
| IntFunction<CodecBuffer> allocator) throws IOException { | ||
| return delegate.toCodecBuffer(backward.apply(message), allocator); | ||
| } | ||
|
|
||
| @Override | ||
| public final T fromCodecBuffer(@Nonnull CodecBuffer buffer) | ||
| throws IOException { | ||
| return forward.apply(delegate.fromCodecBuffer(buffer)); | ||
| } | ||
|
|
||
| @Override | ||
| public final byte[] toPersistedFormat(T message) throws IOException { | ||
| return delegate.toPersistedFormat(backward.apply(message)); | ||
| } | ||
|
|
||
| @Override | ||
| public final T fromPersistedFormat(byte[] bytes) throws IOException { | ||
| return forward.apply(delegate.fromPersistedFormat(bytes)); | ||
| } | ||
|
|
||
| @Override | ||
| public T copyObject(T message) { | ||
| if (immutable) { | ||
| return message; | ||
| } | ||
| try { | ||
| return forward.apply(delegate.copyObject(backward.apply(message))); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException("Failed to copyObject", e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/db/Proto3Codec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * 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.utils.db; | ||
|
|
||
| import org.apache.ratis.thirdparty.com.google.protobuf.CodedOutputStream; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.MessageLite; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.Parser; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.function.IntFunction; | ||
|
|
||
| /** | ||
| * Codecs to serialize/deserialize Protobuf v3 messages. | ||
| */ | ||
| public final class Proto3Codec<M extends MessageLite> | ||
| implements Codec<M> { | ||
| private static final ConcurrentMap<Class<? extends MessageLite>, | ||
| Codec<? extends MessageLite>> CODECS | ||
| = new ConcurrentHashMap<>(); | ||
|
|
||
| /** | ||
| * @return the {@link Codec} for the given class. | ||
| */ | ||
| public static <T extends MessageLite> Codec<T> get(Class<T> clazz) { | ||
| final Codec<?> codec = CODECS.computeIfAbsent(clazz, Proto3Codec::new); | ||
| return (Codec<T>) codec; | ||
| } | ||
|
|
||
| private static <M extends MessageLite> Parser<M> getParser(Class<M> clazz) { | ||
| final String name = "PARSER"; | ||
| try { | ||
| return (Parser<M>) clazz.getField(name).get(null); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException( | ||
| "Failed to get " + name + " field from " + clazz, e); | ||
| } | ||
| } | ||
|
|
||
| private final Parser<M> parser; | ||
|
|
||
| private Proto3Codec(Class<M> clazz) { | ||
| this.parser = getParser(clazz); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supportCodecBuffer() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public CodecBuffer toCodecBuffer(@Nonnull M message, | ||
| IntFunction<CodecBuffer> allocator) { | ||
| final int size = message.getSerializedSize(); | ||
| return allocator.apply(size).put(buffer -> { | ||
| try { | ||
| message.writeTo(CodedOutputStream.newInstance(buffer)); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException( | ||
| "Failed to writeTo: message=" + message, e); | ||
| } | ||
| return size; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public M fromCodecBuffer(@Nonnull CodecBuffer buffer) | ||
| throws InvalidProtocolBufferException { | ||
| return parser.parseFrom(buffer.asReadOnlyByteBuffer()); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] toPersistedFormat(M message) { | ||
| return message.toByteArray(); | ||
| } | ||
|
|
||
| @Override | ||
| public M fromPersistedFormat(byte[] bytes) | ||
| throws InvalidProtocolBufferException { | ||
| return parser.parseFrom(bytes); | ||
| } | ||
|
|
||
| @Override | ||
| public M copyObject(M message) { | ||
| // proto messages are immutable | ||
| return message; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 0 additions & 47 deletions
47
...iner-service/src/main/java/org/apache/hadoop/ozone/container/metadata/BlockDataCodec.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is the benefit of saving codec instances to the map? Are we likely to have multiple uses of the same proto message in different domain objects?
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.
The map is to enforce singleton. We may consider
Proto3Codec::newexpensive since it uses reflection to create an object. Suppose we return a new object as belowThen, it can possibly be misused as
i.e. it creates a new codec for each serialization.