From dev-return-1439-archive-asf-public=cust-asf.ponee.io@nemo.apache.org Fri Oct 19 07:08:52 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 8F32A180778 for ; Fri, 19 Oct 2018 07:08:51 +0200 (CEST) Received: (qmail 82309 invoked by uid 500); 19 Oct 2018 05:08:50 -0000 Mailing-List: contact dev-help@nemo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@nemo.apache.org Delivered-To: mailing list dev@nemo.apache.org Received: (qmail 82287 invoked by uid 99); 19 Oct 2018 05:08:50 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 19 Oct 2018 05:08:50 +0000 From: GitBox To: dev@nemo.apache.org Subject: [GitHub] johnyangk commented on a change in pull request #125: [NEMO-128] Support Beam UnboundedSource Message-ID: <153992572997.13959.15894994055793562600.gitbox@gitbox.apache.org> Date: Fri, 19 Oct 2018 05:08:49 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit johnyangk commented on a change in pull request #125: [NEMO-128] Support Beam UnboundedSource URL: https://github.com/apache/incubator-nemo/pull/125#discussion_r226535152 ########## File path: compiler/frontend/beam/src/main/java/org/apache/nemo/compiler/frontend/beam/source/BeamUnboundedSourceVertex.java ########## @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2018 Seoul National University + * + * Licensed 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.nemo.compiler.frontend.beam.source; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.apache.beam.sdk.io.UnboundedSource; +import org.apache.beam.sdk.util.WindowedValue; +import org.apache.nemo.common.ir.Readable; +import org.apache.nemo.common.ir.vertex.IRVertex; +import org.apache.nemo.common.ir.vertex.SourceVertex; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * SourceVertex implementation for UnboundedSource. + * @param output type. + * @param checkpoint mark type. + */ +public final class BeamUnboundedSourceVertex extends + SourceVertex> { + + private static final Logger LOG = LoggerFactory.getLogger(BeamUnboundedSourceVertex.class.getName()); + private UnboundedSource source; + private final String sourceDescription; + + /** + * The default constructor for beam unbounded source. + * @param source unbounded source. + */ + public BeamUnboundedSourceVertex(final UnboundedSource source) { + super(); + this.source = source; + this.sourceDescription = source.toString(); + } + + private BeamUnboundedSourceVertex(final BeamUnboundedSourceVertex that) { + super(that); + this.source = that.source; + this.sourceDescription = that.source.toString(); + } + + @Override + public IRVertex getClone() { + return new BeamUnboundedSourceVertex<>(this); + } + + @Override + public List>> getReadables(final int desiredNumOfSplits) throws Exception { + final List>> readables = new ArrayList<>(); + source.split(desiredNumOfSplits, null) + .forEach(unboundedSource -> readables.add(new UnboundedSourceReadable<>(unboundedSource))); + return readables; + } + + @Override + public void clearInternalStates() { + source = null; + } + + @Override + public ObjectNode getPropertiesAsJsonNode() { + final ObjectNode node = getIRVertexPropertiesAsJsonNode(); + node.put("source", sourceDescription); + return node; + } + + /** + * UnboundedSourceReadable class. + * @param output type. + * @param checkpoint mark type. + */ + private static final class UnboundedSourceReadable + implements Readable> { + private final UnboundedSource unboundedSource; + + UnboundedSourceReadable(final UnboundedSource unboundedSource) { + this.unboundedSource = unboundedSource; + } + + @Override + public Iterable> read() throws IOException { + return new UnboundedSourceIterable<>(unboundedSource); + } + + @Override + public List getLocations() throws Exception { + return null; + } + } + + /** + * The iterable class for unbounded sources. + * @param output type. + * @param checkpoint mark type. + */ + private static final class UnboundedSourceIterable + implements Iterable> { + + private UnboundedSourceIterator iterator; + + UnboundedSourceIterable(final UnboundedSource unboundedSource) throws IOException { + this.iterator = new UnboundedSourceIterator<>(unboundedSource); + } + + @Override + public Iterator> iterator() { + return iterator; + } + } + + /** + * The iterator for unbounded sources. + * @param output type. + * @param checkpoint mark type. + */ + private static final class UnboundedSourceIterator + implements Iterator> { + + private final UnboundedSource.UnboundedReader unboundedReader; + private boolean available; + + UnboundedSourceIterator(final UnboundedSource unboundedSource) throws IOException { + this.unboundedReader = unboundedSource.createReader(null, null); + available = unboundedReader.start(); + } + + @Override + public boolean hasNext() { + // Unbounded source always has next element until it finishes. + return true; + } + + @Override + @SuppressWarnings("unchecked") + public WindowedValue next() { + try { + while (true) { + if (!available) { Review comment: +1 ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services