<commit_before>/**
 * Copyright 2015 Confluent Inc.
 *
 * 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 io.confluent.kafkarest;

import org.I0Itec.zkclient.ZkClient;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import io.confluent.kafka.serializers.KafkaAvroSerializer;
import io.confluent.kafkarest.entities.EmbeddedFormat;
import io.confluent.kafkarest.entities.ProduceRecord;
import io.confluent.kafkarest.entities.SchemaHolder;
import kafka.cluster.Broker;
import kafka.utils.ZkUtils;
import scala.collection.JavaConversions;
import scala.collection.Seq;

/**
 * Shared pool of Kafka producers used to send messages. The pool manages batched sends, tracking
 * all required acks for a batch and managing timeouts. Currently this pool only contains one
 * producer per serialization format (e.g. byte[], Avro).
 */
public class ProducerPool {

  private static final Logger log = LoggerFactory.getLogger(ProducerPool.class);
  private Map<EmbeddedFormat, RestProducer> producers =
      new HashMap<EmbeddedFormat, RestProducer>();

  public ProducerPool(KafkaRestConfig appConfig, ZkClient zkClient) {
    this(appConfig, zkClient, null);
  }

  public ProducerPool(KafkaRestConfig appConfig, ZkClient zkClient,
                      Properties producerConfigOverrides) {
    this(appConfig, getBootstrapBrokers(zkClient), producerConfigOverrides);
  }

  public ProducerPool(KafkaRestConfig appConfig, String bootstrapBrokers,
                      Properties producerConfigOverrides) {

    Properties originalUserProps = appConfig.getOriginalProperties();

    // Note careful ordering: built-in values we look up automatically first, then configs
    // specified by user with initial KafkaRestConfig, and finally explicit overrides passed to
    // this method (only used for tests)
    Map<String, Object> binaryProps = new HashMap<String, Object>();
    binaryProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapBrokers);
    for (String propName : originalUserProps.stringPropertyNames()) {
      binaryProps.put(propName, originalUserProps.getProperty(propName));
    }
    if (producerConfigOverrides != null) {
      for (String propName : producerConfigOverrides.stringPropertyNames()) {
        binaryProps.put(propName, producerConfigOverrides.getProperty(propName));
      }
    }
    ByteArraySerializer keySerializer = new ByteArraySerializer();
    keySerializer.configure(binaryProps, true);
    ByteArraySerializer valueSerializer = new ByteArraySerializer();
    keySerializer.configure(binaryProps, false);<commit_msg>Fix incorrect config<commit_after>/**
 * Copyright 2015 Confluent Inc.
 *
 * 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 io.confluent.kafkarest;

import org.I0Itec.zkclient.ZkClient;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import io.confluent.kafka.serializers.KafkaAvroSerializer;
import io.confluent.kafkarest.entities.EmbeddedFormat;
import io.confluent.kafkarest.entities.ProduceRecord;
import io.confluent.kafkarest.entities.SchemaHolder;
import kafka.cluster.Broker;
import kafka.utils.ZkUtils;
import scala.collection.JavaConversions;
import scala.collection.Seq;

/**
 * Shared pool of Kafka producers used to send messages. The pool manages batched sends, tracking
 * all required acks for a batch and managing timeouts. Currently this pool only contains one
 * producer per serialization format (e.g. byte[], Avro).
 */
public class ProducerPool {

  private static final Logger log = LoggerFactory.getLogger(ProducerPool.class);
  private Map<EmbeddedFormat, RestProducer> producers =
      new HashMap<EmbeddedFormat, RestProducer>();

  public ProducerPool(KafkaRestConfig appConfig, ZkClient zkClient) {
    this(appConfig, zkClient, null);
  }

  public ProducerPool(KafkaRestConfig appConfig, ZkClient zkClient,
                      Properties producerConfigOverrides) {
    this(appConfig, getBootstrapBrokers(zkClient), producerConfigOverrides);
  }

  public ProducerPool(KafkaRestConfig appConfig, String bootstrapBrokers,
                      Properties producerConfigOverrides) {

    Properties originalUserProps = appConfig.getOriginalProperties();

    // Note careful ordering: built-in values we look up automatically first, then configs
    // specified by user with initial KafkaRestConfig, and finally explicit overrides passed to
    // this method (only used for tests)
    Map<String, Object> binaryProps = new HashMap<String, Object>();
    binaryProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapBrokers);
    for (String propName : originalUserProps.stringPropertyNames()) {
      binaryProps.put(propName, originalUserProps.getProperty(propName));
    }
    if (producerConfigOverrides != null) {
      for (String propName : producerConfigOverrides.stringPropertyNames()) {
        binaryProps.put(propName, producerConfigOverrides.getProperty(propName));
      }
    }
    ByteArraySerializer keySerializer = new ByteArraySerializer();
    keySerializer.configure(binaryProps, true);
    ByteArraySerializer valueSerializer = new ByteArraySerializer();
    valueSerializer.configure(binaryProps, false);
{'fraction_non_alphanumeric': 0.08192132017091498, 'fraction_numerical': 0.0032414910858995136, 'mean_word_length': 4.7089991589571065, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5827416', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1854, 'n_words': 539}