GithubHelp home page GithubHelp logo

KafkaSparkStreamingSpec.scala: NotSerializableException: org.apache.commons.pool2.impl.GenericObjectPool about kafka-storm-starter HOT 4 CLOSED

miguno avatar miguno commented on May 26, 2024
KafkaSparkStreamingSpec.scala: NotSerializableException: org.apache.commons.pool2.impl.GenericObjectPool

from kafka-storm-starter.

Comments (4)

nickshoe avatar nickshoe commented on May 26, 2024 1

@btiernay thanks for sharing your solution, it really helped me out.

from kafka-storm-starter.

miguno avatar miguno commented on May 26, 2024

This is a known issue because Apache Commons' pool implementation is not serializable.

Here's my comment from the link above:

This serialization issue is similar to the issue described at dbpedia/distributed-extraction-framework#9. It looks as if one would need to use a different object pool implementation than the one from Apache Commons Pool, as the latter may be hard to serialize (see sparkContext broadcast JedisPool not work).

Furthermore I noticed that the serialization issue is also triggered locally (e.g. when running ./sbt test in kafka-storm-starter) when using Spark 1.2+.

To fix this issue you need to implement your own, serializable pool.

from kafka-storm-starter.

uladzimir-shelhunou avatar uladzimir-shelhunou commented on May 26, 2024

I understand why it happens, but I did't know that is known issue.
I want to implement own pool, but I have some concerns about partitions and offsets.

from kafka-storm-starter.

btiernay avatar btiernay commented on May 26, 2024

I did something like the following for Java:

import static lombok.AccessLevel.PRIVATE;

import java.io.Serializable;
import java.util.NoSuchElementException;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import org.apache.commons.pool2.ObjectPool;

@RequiredArgsConstructor
public abstract class LazySerializableObjectPool<T> implements ObjectPool<T>, Serializable {

  @NonNull
  @Getter(lazy = true, value = PRIVATE)
  @Accessors(fluent = true)
  private final transient ObjectPool<T> delegate = createDelegate();

  protected abstract ObjectPool<T> createDelegate();

  @Override
  public T borrowObject() throws Exception, NoSuchElementException, IllegalStateException {
    return delegate().borrowObject();
  }

  @Override
  public void returnObject(T obj) throws Exception {
    delegate().returnObject(obj);
  }

  @Override
  public void invalidateObject(T obj) throws Exception {
    delegate().invalidateObject(obj);
  }

  @Override
  public void addObject() throws Exception, IllegalStateException, UnsupportedOperationException {
    delegate().addObject();
  }

  @Override
  public int getNumIdle() {
    return delegate().getNumIdle();
  }

  @Override
  public int getNumActive() {
    return delegate().getNumActive();
  }

  @Override
  public void clear() throws Exception, UnsupportedOperationException {
    delegate().clear();
  }

  @Override
  public void close() {
    delegate().close();
  }

}
@RequiredArgsConstructor
public class KafkaProducerObjectPool extends LazySerializableObjectPool<...> {

  /**
   * Configuration.
   */
  @NonNull
  private final Map<String, String> producerProperties;

  @Override
  protected ObjectPool<KafkaProducer> createDelegate() {
    val pooledObjectFactory =  ...
    val maxNumProducers = 10;
    val poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(maxNumProducers);
    poolConfig.setMaxIdle(maxNumProducers);

    return new GenericObjectPool<KafkaProducer>(pooledObjectFactory, poolConfig);
  }

}

See https://projectlombok.org/features/GetterLazy.html for details.

from kafka-storm-starter.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.