Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.flink.runtime.io.network.netty;
import org.apache.flink.runtime.event.TaskEvent;
import org.apache.flink.runtime.executiongraph.ExecutionAttemptID;
import org.apache.flink.runtime.io.network.api.serialization.EventSerializer;
import org.apache.flink.runtime.io.network.buffer.Buffer;
import org.apache.flink.runtime.io.network.partition.ResultPartitionID;
import org.apache.flink.runtime.io.network.partition.consumer.InputChannel;
import org.apache.flink.runtime.io.network.partition.consumer.InputChannelID;
import org.apache.flink.runtime.jobgraph.IntermediateResultPartitionID;
import org.apache.flink.util.ExceptionUtils;
import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf;
import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator;
import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream;
import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufOutputStream;
import org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelOutboundHandlerAdapter;
import org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.apache.flink.shaded.netty4.io.netty.handler.codec.MessageToMessageDecoder;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ProtocolException;
import java.nio.ByteBuffer;
import java.util.List;
import static org.apache.flink.util.Preconditions.checkArgument;
import static org.apache.flink.util.Preconditions.checkNotNull;
import static org.apache.flink.util.Preconditions.checkState;
/**
* A simple and generic interface to serialize messages to Netty's buffer space.
*
*
This class must be public as long as we are using a Netty version prior to 4.0.45. Please check FLINK-7845 for
* more information.
*/
public abstract class NettyMessage {
// ------------------------------------------------------------------------
// Note: Every NettyMessage subtype needs to have a public 0-argument
// constructor in order to work with the generic deserializer.
// ------------------------------------------------------------------------
static final int FRAME_HEADER_LENGTH = 4 + 4 + 1; // frame length (4), magic number (4), msg ID (1)
static final int MAGIC_NUMBER = 0xBADC0FFE;
abstract ByteBuf write(ByteBufAllocator allocator) throws Exception;
// ------------------------------------------------------------------------
/**
* Allocates a new (header and contents) buffer and adds some header information for the frame
* decoder.
*
*
Before sending the buffer, you must write the actual length after adding the contents as
* an integer to position 0!
*
* @param allocator
* byte buffer allocator to use
* @param id
* {@link NettyMessage} subclass ID
*
* @return a newly allocated direct buffer with header data written for {@link
* NettyMessageDecoder}
*/
private static ByteBuf allocateBuffer(ByteBufAllocator allocator, byte id) {
return allocateBuffer(allocator, id, -1);
}
/**
* Allocates a new (header and contents) buffer and adds some header information for the frame
* decoder.
*
*
If the contentLength is unknown, you must write the actual length after adding
* the contents as an integer to position 0!
*
* @param allocator
* byte buffer allocator to use
* @param id
* {@link NettyMessage} subclass ID
* @param contentLength
* content length (or -1 if unknown)
*
* @return a newly allocated direct buffer with header data written for {@link
* NettyMessageDecoder}
*/
private static ByteBuf allocateBuffer(ByteBufAllocator allocator, byte id, int contentLength) {
return allocateBuffer(allocator, id, 0, contentLength, true);
}
/**
* Allocates a new buffer and adds some header information for the frame decoder.
*
*
If the contentLength is unknown, you must write the actual length after adding
* the contents as an integer to position 0!
*
* @param allocator
* byte buffer allocator to use
* @param id
* {@link NettyMessage} subclass ID
* @param messageHeaderLength
* additional header length that should be part of the allocated buffer and is written
* outside of this method
* @param contentLength
* content length (or -1 if unknown)
* @param allocateForContent
* whether to make room for the actual content in the buffer (true) or whether to
* only return a buffer with the header information (false)
*
* @return a newly allocated direct buffer with header data written for {@link
* NettyMessageDecoder}
*/
private static ByteBuf allocateBuffer(
ByteBufAllocator allocator,
byte id,
int messageHeaderLength,
int contentLength,
boolean allocateForContent) {
checkArgument(contentLength <= Integer.MAX_VALUE - FRAME_HEADER_LENGTH);
final ByteBuf buffer;
if (!allocateForContent) {
buffer = allocator.directBuffer(FRAME_HEADER_LENGTH + messageHeaderLength);
} else if (contentLength != -1) {
buffer = allocator.directBuffer(FRAME_HEADER_LENGTH + messageHeaderLength + contentLength);
} else {
// content length unknown -> start with the default initial size (rather than FRAME_HEADER_LENGTH only):
buffer = allocator.directBuffer();
}
buffer.writeInt(FRAME_HEADER_LENGTH + messageHeaderLength + contentLength); // may be updated later, e.g. if contentLength == -1
buffer.writeInt(MAGIC_NUMBER);
buffer.writeByte(id);
return buffer;
}
// ------------------------------------------------------------------------
// Generic NettyMessage encoder and decoder
// ------------------------------------------------------------------------
@ChannelHandler.Sharable
static class NettyMessageEncoder extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof NettyMessage) {
ByteBuf serialized = null;
try {
serialized = ((NettyMessage) msg).write(ctx.alloc());
}
catch (Throwable t) {
throw new IOException("Error while serializing message: " + msg, t);
}
finally {
if (serialized != null) {
ctx.write(serialized, promise);
}
}
}
else {
ctx.write(msg, promise);
}
}
// Create the frame length decoder here as it depends on the encoder
//
// +------------------+------------------+--------++----------------+
// | FRAME LENGTH (4) | MAGIC NUMBER (4) | ID (1) || CUSTOM MESSAGE |
// +------------------+------------------+--------++----------------+
static LengthFieldBasedFrameDecoder createFrameLengthDecoder() {
return new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, -4, 4);
}
}
@ChannelHandler.Sharable
static class NettyMessageDecoder extends MessageToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List