From issues-return-186481-archive-asf-public=cust-asf.ponee.io@flink.apache.org Tue Aug 28 17:07:06 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 3D99C18072F for ; Tue, 28 Aug 2018 17:07:06 +0200 (CEST) Received: (qmail 29097 invoked by uid 500); 28 Aug 2018 15:07:05 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 28984 invoked by uid 99); 28 Aug 2018 15:07:05 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Aug 2018 15:07:05 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id E9C6EC1557 for ; Tue, 28 Aug 2018 15:07:04 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -109.501 X-Spam-Level: X-Spam-Status: No, score=-109.501 tagged_above=-999 required=6.31 tests=[ENV_AND_HDR_SPF_MATCH=-0.5, KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_MED=-2.3, SPF_PASS=-0.001, USER_IN_DEF_SPF_WL=-7.5, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id VRONbfgfcJLS for ; Tue, 28 Aug 2018 15:07:03 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id ABF855F433 for ; Tue, 28 Aug 2018 15:07:02 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id CF4EFE0E5D for ; Tue, 28 Aug 2018 15:07:01 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 2045526B64 for ; Tue, 28 Aug 2018 15:07:01 +0000 (UTC) Date: Tue, 28 Aug 2018 15:07:01 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: issues@flink.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (FLINK-9913) Improve output serialization only once in RecordWriter MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/FLINK-9913?page=3Dcom.atlassian= .jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=3D1659= 5110#comment-16595110 ]=20 ASF GitHub Bot commented on FLINK-9913: --------------------------------------- zhijiangW commented on a change in pull request #6417: [FLINK-9913][runtime= ] Improve output serialization only once in RecordWriter URL: https://github.com/apache/flink/pull/6417#discussion_r213343581 =20 =20 ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network= /api/writer/RecordWriter.java ########## @@ -87,62 +86,71 @@ public RecordWriter(ResultPartitionWriter writer, Chan= nelSelector channelSele =20 =09=09this.numChannels =3D writer.getNumberOfSubpartitions(); =20 -=09=09/* -=09=09 * The runtime exposes a channel abstraction for the produced result= s -=09=09 * (see {@link ChannelSelector}). Every channel has an independent -=09=09 * serializer. -=09=09 */ -=09=09this.serializers =3D new SpanningRecordSerializer[numChannels]; +=09=09this.serializer =3D new SpanningRecordSerializer(); =09=09this.bufferBuilders =3D new Optional[numChannels]; =09=09for (int i =3D 0; i < numChannels; i++) { -=09=09=09serializers[i] =3D new SpanningRecordSerializer(); =09=09=09bufferBuilders[i] =3D Optional.empty(); =09=09} =09} =20 =09public void emit(T record) throws IOException, InterruptedException { +=09=09serializer.serializeRecord(record); + =09=09for (int targetChannel : channelSelector.selectChannels(record, numC= hannels)) { -=09=09=09sendToTarget(record, targetChannel); +=09=09=09copyToTarget(targetChannel); =09=09} + +=09=09// Make sure we don't hold onto the large intermediate serialization= buffer for too long +=09=09serializer.prune(); =09} =20 =09/** =09 * This is used to broadcast Streaming Watermarks in-band with records.= This ignores =09 * the {@link ChannelSelector}. =09 */ =09public void broadcastEmit(T record) throws IOException, InterruptedExce= ption { +=09=09serializer.serializeRecord(record); + =09=09for (int targetChannel =3D 0; targetChannel < numChannels; targetCha= nnel++) { -=09=09=09sendToTarget(record, targetChannel); +=09=09=09copyToTarget(targetChannel); =09=09} + +=09=09serializer.prune(); =09} =20 =09/** =09 * This is used to send LatencyMarks to a random target channel. =09 */ =09public void randomEmit(T record) throws IOException, InterruptedExcepti= on { -=09=09sendToTarget(record, rng.nextInt(numChannels)); -=09} +=09=09serializer.serializeRecord(record); =20 -=09private void sendToTarget(T record, int targetChannel) throws IOExcepti= on, InterruptedException { -=09=09RecordSerializer serializer =3D serializers[targetChannel]; +=09=09copyToTarget(rng.nextInt(numChannels)); =20 -=09=09SerializationResult result =3D serializer.addRecord(record); +=09=09serializer.prune(); +=09} =20 +=09private void copyToTarget(int targetChannel) throws IOException, Interr= uptedException { +=09=09// We should reset the initial position of the intermediate serializ= ation buffer before +=09=09// copying, so the serialization results can be copied to multiple t= arget buffers. +=09=09serializer.reset(); + +=09=09BufferBuilder bufferBuilder =3D getBufferBuilder(targetChannel); +=09=09SerializationResult result =3D serializer.copyToBufferBuilder(buffer= Builder); =09=09while (result.isFullBuffer()) { -=09=09=09if (tryFinishCurrentBufferBuilder(targetChannel, serializer)) { -=09=09=09=09// If this was a full record, we are done. Not breaking -=09=09=09=09// out of the loop at this point will lead to another -=09=09=09=09// buffer request before breaking out (that would not be -=09=09=09=09// a problem per se, but it can lead to stalls in the -=09=09=09=09// pipeline). -=09=09=09=09if (result.isFullRecord()) { -=09=09=09=09=09break; -=09=09=09=09} +=09=09=09tryFinishCurrentBufferBuilder(targetChannel); =20 Review comment: You pointed out a good question! 1. Considering `tryFinishCurrentBufferBuilder()`, the logic is somewhat = different from before. In the past, the buffer builder may be empty when ca= lling `tryFinishCurrentBufferBuilder()`, then it returns a boolean value to= indicate the result. But now, we know the buffer builder is always present= when calling `tryFinishCurrentBufferBuilder`, so we may change it to `fini= shCurrentBufferBuilder()` seems more appropriate. And adds the check code i= nstead as following: ``` private void finishCurrentBufferBuilder(int targetChannel) { =09=09checkState(bufferBuilders[targetChannel].isPresent()); =20 =09=09BufferBuilder bufferBuilder =3D bufferBuilders[targetChannel].get(= ); =09=09bufferBuilders[targetChannel] =3D Optional.empty(); =09=09numBytesOut.inc(bufferBuilder.finish()); =09} ``` 2. But I think we still need call `finishCurrentBufferBuilder()` after = checking every full buffer. Because in the while loop the serialized record= may be copied into several different buffer builders, and each buffer buil= d referenced by the `bufferBuilders ` needs to be finished when full. =20 3. It actually seems better than before, as the loop exit not rely on th= e return value of `tryFinishCurrentBufferBuilder()`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. =20 For queries about this service, please contact Infrastructure at: users@infra.apache.org > Improve output serialization only once in RecordWriter > ------------------------------------------------------ > > Key: FLINK-9913 > URL: https://issues.apache.org/jira/browse/FLINK-9913 > Project: Flink > Issue Type: Improvement > Components: Network > Affects Versions: 1.6.0 > Reporter: zhijiang > Assignee: zhijiang > Priority: Major > Labels: pull-request-available > Fix For: 1.7.0 > > > Currently=C2=A0the=C2=A0{{RecordWriter}} emits output into multi channels= via=C2=A0{{ChannelSelector}}=C2=A0 or broadcasts output to all channels di= rectly. Each channel has a separate {{RecordSerializer}} for serializing ou= tputs, that means the output will be serialized as many times as the number= of selected channels. > As we know, data serialization is a high cost operation, so we can get go= od benefits by improving=C2=A0the serialization only once. > I would suggest the following changes for realizing it. > # Only one {{RecordSerializer}}=C2=A0is created in {{RecordWriter}}=C2= =A0for all the channels. > # The output is serialized into the intermediate data buffer only once f= or different channels. > # The intermediate serialization results are copied into different {{Buf= ferBuilder}}s for different channels. > An additional benefit by using a single serializer for all channels is th= at we get a potentially significant reduction on heap space overhead from f= ewer intermediate serialization buffers (only once we got over 5MiB, these = buffers were pruned back to 128B!). -- This message was sent by Atlassian JIRA (v7.6.3#76005)