Biranavan-Parameswaran commented on code in PR #2591: URL: https://github.com/apache/systemds/pull/2591#discussion_r3919348567
########## src/main/java/org/apache/sysds/runtime/controlprogram/federated/FederatedChunkDecoder.java: ########## @@ -0,0 +1,221 @@ +/* + * 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.sysds.runtime.controlprogram.federated; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectStreamClass; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.sysds.runtime.util.CommonThreadPool; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; + +public class FederatedChunkDecoder extends MessageToMessageDecoder<ByteBuf> { + private static final Object END_OF_STREAM = new Object(); + // stop reading at QUEUE_DEPTH, resume at half: gap avoids autoRead thrash + private static final int LOW_WATERMARK = FederatedChunkProtocol.QUEUE_DEPTH / 2; + + private final BlockingQueue<Object> _payloads = new LinkedBlockingQueue<>(); + private boolean _started; + private volatile boolean _throttled; + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) { + startReader(ctx); + byte type = buf.readByte(); + int len = buf.readInt(); + switch(type) { + case FederatedChunkProtocol.TYPE_DATA: + _payloads.add(readBytes(buf, len)); + break; + case FederatedChunkProtocol.TYPE_END: + _payloads.add(END_OF_STREAM); + break; + case FederatedChunkProtocol.TYPE_ERROR: + _payloads.add(new IOException(buf.toString(buf.readerIndex(), len, StandardCharsets.UTF_8))); + break; + default: + _payloads.add(new IOException("Unknown federated chunk frame type: " + type)); + break; + } + if(_payloads.size() >= FederatedChunkProtocol.QUEUE_DEPTH) { + _throttled = true; + ctx.channel().config().setAutoRead(false); + } + } + + /** + * Start the deserializer on a pool thread, at most once per channel. + * + * @param ctx handler context + */ + private void startReader(ChannelHandlerContext ctx) { + if(_started) + return; + _started = true; + CommonThreadPool.getDynamicPool().execute(() -> runDeserializer(ctx)); + } + + /** + * Read one object from the queued payloads and fire it up the pipeline. A failure is fired as an exception on the + * event loop instead. + * + * @param ctx handler context + */ + private void runDeserializer(ChannelHandlerContext ctx) { + try(ObjectInputStream ois = getObjectInputStream(new PayloadInputStream(this, ctx))) { Review Comment: ```suggestion try(PayloadInputStream in = new PayloadInputStream(this, ctx); ObjectInputStream ois = getObjectInputStream(in)) { ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
