spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tdas <...@git.apache.org>
Subject [GitHub] spark pull request: SPARK-1729. Make Flume pull data from source, ...
Date Fri, 06 Jun 2014 01:17:07 GMT
Github user tdas commented on a diff in the pull request:

    https://github.com/apache/spark/pull/807#discussion_r13473209
  
    --- Diff: external/flume-sink/src/main/scala/org/apache/spark/flume/sink/SparkSink.scala
---
    @@ -0,0 +1,392 @@
    +/*
    + * 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 org.apache.flume.sink.AbstractSink
    +import java.util.concurrent.locks.ReentrantLock
    +import org.apache.flume.Sink.Status
    +import org.apache.spark.flume.{SparkSinkEvent, EventBatch, SparkFlumeProtocol}
    +import scala.util.control.Breaks
    +import java.nio.ByteBuffer
    +import org.apache.flume.{FlumeException, Context}
    +import org.slf4j.LoggerFactory
    +import java.util.concurrent.atomic.AtomicLong
    +import org.apache.commons.lang.RandomStringUtils
    +import java.util.concurrent._
    +import java.util
    +import org.apache.flume.conf.{ConfigurationException, Configurable}
    +import com.google.common.util.concurrent.ThreadFactoryBuilder
    +import org.apache.avro.ipc.NettyServer
    +import org.apache.avro.ipc.specific.SpecificResponder
    +import java.net.InetSocketAddress
    +
    +class SparkSink() extends AbstractSink with Configurable {
    +  private val LOG = LoggerFactory.getLogger(this.getClass)
    +  private val lock = new ReentrantLock()
    +  private val blockingCondition = lock.newCondition()
    +
    +  // This sink will not persist sequence numbers and reuses them if it gets restarted.
    +  // So it is possible to commit a transaction which may have been meant for the sink
before the
    +  // restart.
    +  // Since the new txn may not have the same sequence number we must guard against accidentally
    +  // committing
    +  // a new transaction. To reduce the probability of that happening a random string is
prepended
    +  // to the sequence number.
    +  // Does not change for life of sink
    +  private val seqBase = RandomStringUtils.randomAlphanumeric(8)
    +  // Incremented for each transaction
    +  private val seqNum = new AtomicLong(0)
    +
    +  private var transactionExecutorOpt: Option[ExecutorService] = None
    +
    +  private var numProcessors: Integer = SparkSinkConfig.DEFAULT_PROCESSOR_COUNT
    +  private var transactionTimeout = SparkSinkConfig.DEFAULT_TRANSACTION_TIMEOUT
    +
    +  private val processorMap = new ConcurrentHashMap[CharSequence, TransactionProcessor]()
    +
    +  private var processorFactory: Option[SparkHandlerFactory] = None
    +  private var hostname: String = SparkSinkConfig.DEFAULT_HOSTNAME
    +  private var port: Int = 0
    +  private var maxThreads: Int = SparkSinkConfig.DEFAULT_MAX_THREADS
    +  private var serverOpt: Option[NettyServer] = None
    +  private var running = false
    +
    +  override def start() {
    +    transactionExecutorOpt = Option(Executors.newFixedThreadPool(numProcessors,
    +      new ThreadFactoryBuilder().setDaemon(true)
    +        .setNameFormat("Spark Sink, " + getName + " Processor Thread - %d").build()))
    +
    +    processorFactory = Option(new SparkHandlerFactory(numProcessors))
    +
    +    val responder = new SpecificResponder(classOf[SparkFlumeProtocol], new AvroCallbackHandler())
    +
    +    // 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())
    +    lock.lock()
    +    try {
    +      running = true
    +    } finally {
    +      lock.unlock()
    +    }
    +    super.start()
    +  }
    +
    +  override def stop() {
    +    transactionExecutorOpt.map(executor => executor.shutdownNow())
    +    serverOpt.map(server => {
    +      server.close()
    +      server.join()
    +    })
    +    lock.lock()
    +    try {
    +      running = false
    +      blockingCondition.signalAll()
    +    } finally {
    +      lock.unlock()
    +    }
    +  }
    +
    +  override def configure(ctx: Context) {
    +    import SparkSinkConfig._
    +    hostname = ctx.getString(CONF_HOSTNAME, DEFAULT_HOSTNAME)
    +    val portOpt = Option(ctx.getInteger(CONF_PORT))
    +    if(portOpt.isDefined) {
    +      port = portOpt.get
    +    } else {
    +      throw new ConfigurationException("The Port to bind must be specified")
    +    }
    +    numProcessors = ctx.getInteger(PROCESSOR_COUNT, DEFAULT_PROCESSOR_COUNT)
    +    transactionTimeout = ctx.getInteger(CONF_TRANSACTION_TIMEOUT, DEFAULT_TRANSACTION_TIMEOUT)
    +    maxThreads = ctx.getInteger(CONF_MAX_THREADS, DEFAULT_MAX_THREADS)
    +  }
    +
    +  override def process(): Status = {
    +    // This method is called in a loop by the Flume framework - block it until the sink
is
    +    // stopped to save CPU resources
    +    lock.lock()
    +    try {
    +      while(running) {
    --- End diff --
    
    Did not know about this! Though I have often used [CountdownLatch.await()](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html#await()).
That does not seem to have the spurious waking up problem. Reduces three variables (lock,
running, blockingCondition) to one variable.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message