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

    https://github.com/apache/spark/pull/807#discussion_r13835043
  
    --- Diff: 
external/flume-sink/src/main/scala/org/apache/spark/flume/sink/SparkSink.scala 
---
    @@ -0,0 +1,432 @@
    +/*
    + * 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.flume.sink
    +
    +import java.net.InetSocketAddress
    +import java.nio.ByteBuffer
    +import java.util
    +import java.util.concurrent._
    +import java.util.concurrent.atomic.AtomicLong
    +
    +import scala.util.control.Breaks
    +
    +import com.google.common.util.concurrent.ThreadFactoryBuilder
    +import org.apache.avro.ipc.NettyServer
    +import org.apache.avro.ipc.specific.SpecificResponder
    +import org.apache.commons.lang.RandomStringUtils
    +import org.apache.flume.Sink.Status
    +import org.apache.flume.conf.{ConfigurationException, Configurable}
    +import org.apache.flume.sink.AbstractSink
    +import org.apache.flume.{Channel, Transaction, FlumeException, Context}
    +import org.slf4j.LoggerFactory
    +
    +import org.apache.spark.flume.{SparkSinkEvent, EventBatch, 
SparkFlumeProtocol}
    +
    +/**
    + * A sink that uses Avro RPC to run a server that can be polled by Spark's
    + * FlumePollingInputDStream. This sink has the following configuration 
parameters:
    + *
    + * hostname - The hostname to bind to. Default: 0.0.0.0
    + * port - The port to bind to. (No default - mandatory)
    + * timeout - Time in seconds after which a transaction is rolled back,
    + * if an ACK is not received from Spark within that time
    + * threads - Number of threads to use to receive requests from Spark 
(Default: 10)
    + *
    + */
    +// Flume forces transactions to be thread-local. So each transaction 
*must* be committed, or
    +// rolled back from the thread it was originally created in. So each 
getEvents call from Spark
    +// creates a TransactionProcessor which runs in a new thread, in which the 
transaction is created
    +// and events are pulled off the channel. Once the events are sent to 
spark,
    +// that thread is blocked and the TransactionProcessor is saved in a map,
    +// until an ACK or NACK comes back or the transaction times out (after the 
specified timeout).
    +// When the response comes, the TransactionProcessor is retrieved and then 
unblocked,
    +// at which point the transaction is committed or rolled back.
    +class SparkSink extends AbstractSink with Configurable {
    +
    +  // Size of the pool to use for holding transaction processors.
    +  private var poolSize: Integer = SparkSinkConfig.DEFAULT_THREADS
    +
    +  // Timeout for each transaction. If spark does not respond in this much 
time,
    +  // rollback the transaction
    +  private var transactionTimeout = 
SparkSinkConfig.DEFAULT_TRANSACTION_TIMEOUT
    +
    +  // Address info to bind on
    +  private var hostname: String = SparkSinkConfig.DEFAULT_HOSTNAME
    +  private var port: Int = 0
    +
    +  // Handle to the server
    +  private var serverOpt: Option[NettyServer] = None
    +
    +  // The handler that handles the callback from Avro
    +  private var handler: Option[SparkAvroCallbackHandler] = None
    +
    +  // Latch that blocks off the Flume framework from wasting 1 thread.
    +  private val blockingLatch = new CountDownLatch(1)
    +
    +  override def start() {
    +    handler = Option(new SparkAvroCallbackHandler(poolSize, getChannel, 
transactionTimeout))
    +    val responder = new SpecificResponder(classOf[SparkFlumeProtocol], 
handler.get)
    +    // Using the constructor that takes specific thread-pools requires 
bringing in netty
    +    // dependencies which are being excluded in the build. In practice,
    +    // Netty dependencies are already available on the JVM as Flume would 
have pulled them in.
    +    serverOpt = Option(new NettyServer(responder, new 
InetSocketAddress(hostname, port)))
    +    serverOpt.map(server => {
    +      server.start()
    +    })
    +    super.start()
    +  }
    +
    +  override def stop() {
    +    handler.map(callbackHandler => {
    +      callbackHandler.shutdown()
    +    })
    +    serverOpt.map(server => {
    +      server.close()
    +      server.join()
    +    })
    +    blockingLatch.countDown()
    +    super.stop()
    +  }
    +
    +  /**
    +   * @param ctx
    --- End diff --
    
    this  doc is not really useful


---
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