Github user tdas commented on a diff in the pull request:

    https://github.com/apache/spark/pull/807#discussion_r13455872
  
    --- Diff: 
external/flume/src/main/scala/org/apache/spark/streaming/flume/FlumePollingInputDStream.scala
 ---
    @@ -0,0 +1,165 @@
    +/*
    + * 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.spark.streaming.flume
    +
    +
    +import java.io.{ObjectOutput, ObjectInput, Externalizable}
    +import java.net.InetSocketAddress
    +import java.nio.ByteBuffer
    +import java.util.concurrent.{TimeUnit, Executors}
    +
    +import scala.collection.JavaConversions._
    +import scala.collection.mutable
    +import scala.reflect.ClassTag
    +
    +import com.google.common.util.concurrent.ThreadFactoryBuilder
    +import org.apache.avro.ipc.NettyTransceiver
    +import org.apache.avro.ipc.specific.SpecificRequestor
    +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.flume.{SparkSinkEvent, SparkFlumeProtocol}
    +import org.apache.spark.storage.StorageLevel
    +import org.apache.spark.streaming.StreamingContext
    +import org.apache.spark.streaming.dstream.ReceiverInputDStream
    +import org.apache.spark.streaming.receiver.Receiver
    +
    +
    +
    +class FlumePollingInputDStream[T: ClassTag](
    +  @transient ssc_ : StreamingContext,
    +  val addresses: Seq[InetSocketAddress],
    +  val maxBatchSize: Int,
    +  val parallelism: Int,
    +  storageLevel: StorageLevel
    +) extends ReceiverInputDStream[SparkPollingEvent](ssc_) {
    +  /**
    +   * Gets the receiver object that will be sent to the worker nodes
    +   * to receive data. This method needs to defined by any specific 
implementation
    +   * of a NetworkInputDStream.
    +   */
    +  override def getReceiver(): Receiver[SparkPollingEvent] = {
    +    new FlumePollingReceiver(addresses, maxBatchSize, parallelism, 
storageLevel)
    +  }
    +}
    +
    +private[streaming] class FlumePollingReceiver(
    +  addresses: Seq[InetSocketAddress],
    +  maxBatchSize: Int,
    +  parallelism: Int,
    +  storageLevel: StorageLevel
    +) extends Receiver[SparkPollingEvent](storageLevel) with Logging {
    +
    +  lazy val channelFactoryExecutor =
    +    Executors.newCachedThreadPool(new 
ThreadFactoryBuilder().setDaemon(true).
    +      setNameFormat("Flume Receiver Channel Thread - %d").build())
    +
    +  lazy val channelFactory =
    +    new NioClientSocketChannelFactory(channelFactoryExecutor, 
channelFactoryExecutor)
    +
    +  lazy val receiverExecutor = Executors.newFixedThreadPool(parallelism,
    +    new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Flume 
Receiver Thread - %d").build())
    +
    +  private var connections = Array.empty[FlumeConnection] // temporarily 
empty, filled in later
    +
    +  override def onStart(): Unit = {
    +    val connectionBuilder = new 
mutable.ArrayBuilder.ofRef[FlumeConnection]()
    +    addresses.map(host => {
    +      val transceiver = new NettyTransceiver(host, channelFactory)
    +      val client = 
SpecificRequestor.getClient(classOf[SparkFlumeProtocol.Callback], transceiver)
    +      connectionBuilder += new FlumeConnection(transceiver, client)
    +    })
    +    connections = connectionBuilder.result()
    --- End diff --
    
    `connections` could be built in a more Scala-like functional style. 
Something like this.
    ```
    connections = addresses.map { host =>  .....   new FlumeConnection(....) 
}.toArray
    ```
    
    No need for ArrayBuilder 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to