Skip to main content



Apache Kafka: Low Level api Consumer

If you want greater control over partition consumption then High Level api consumer you may implement it in low level api. It will require to do more work that is not required in consumer group, like:

  • Keeping track of offset where consumer left consumption.
  • Identify lead Broker and adjust with Broker leader changes.
Steps for implementing :

  • Find an active Broker and find out which Broker is the leader for your topic and partition
  • Determine who the replica Brokers are for your topic and partition
  • Build the request defining what data you are interested in
  • Fetch the data
  • Identify and recover from leader changes
package learning.kafka;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import kafka.api.FetchRequestBuilder;
import kafka.api.PartitionOffsetRequestInfo;
import kafka.cluster.Broker;
import kafka.common.ErrorMapping;
import kafka.common.TopicAndPartition;
import kafka.javaapi.FetchResponse;
import kafka.javaapi.OffsetRequest;
import kafka.javaapi.OffsetResponse;
import kafka.javaapi.PartitionMetadata;
import kafka.javaapi.TopicMetadata;
import kafka.javaapi.TopicMetadataRequest;
import kafka.javaapi.TopicMetadataResponse;
import kafka.javaapi.consumer.SimpleConsumer;
import kafka.message.MessageAndOffset;

public class SimpleExample {

       public static void main(String[] args) {
              SimpleExample example = new SimpleExample();

              // max messages to read
              long maxReads = Long.parseLong(args[0]);

              String topic = args[1];
              int partition = Integer.parseInt(args[2]);

              List<String> seeds = new ArrayList<String>();
              // broker to detrmine lead
              seeds.add(args[3]);

              // port
              int port = Integer.parseInt(args[4]);

              try {
                     example.run(maxReads, topic, partition, seeds, port);
              } catch (Exception e) {
                     System.out.println("Oops:" + e);
                     e.printStackTrace();
              }
       }
      
      
       public void run(long maxReads, String topic, int partition, List<String> seeds, int port) throws Exception {
              PartitionMetadata metadata = findLeader(seeds, port, topic, partition);
             
              if (metadata == null) {
                   System.err.println("Can't find metadata for Topic and Partition. Exiting");
                   return;
            }
             
              if(metadata.leader() ==null){
                       System.err.println("Can't find Leader for Topic and Partition. Exiting");
                 return;
              }
             
              String leadBroker = metadata.leader().host();
              String clientName = "Client_" + topic + "_" + partition;
             
              SimpleConsumer consumer = new SimpleConsumer(leadBroker, port, 100000, 64 * 1024, clientName);
              long readoffset = getLastOffset(consumer, topic, partition, kafka.api.OffsetRequest.EarliestTime(), clientName);
             
              int numErrors = 0;
             
              while(maxReads > 0){
                     if (consumer == null) {
                consumer = new SimpleConsumer(leadBroker, port, 100000, 64 * 1024, clientName);
            }
                    
                     kafka.api.FetchRequest req = new FetchRequestBuilder().clientId(clientName)
                                  .addFetch(topic, partition, readoffset, 100000)
                                  .build();
                    
                     FetchResponse fetchResponse = consumer.fetch(req);
                    
                     if (fetchResponse.hasError()) {
                            numErrors++;
                           // Something went wrong!
                            short code = fetchResponse.errorCode(topic, partition);
                            System.out.println("Error fetching data from the Broker:" + leadBroker + " Reason: " + code);
                            if (numErrors > 5) break;
                            if (code == ErrorMapping.OffsetOutOfRangeCode())  {
                                  // We asked for an invalid offset. For simple case ask for the last element to reset
                                   readoffset = getLastOffset(consumer,topic, partition, kafka.api.OffsetRequest.LatestTime(), clientName);
                        continue;
                            }
                            consumer.close();
                    consumer = null;
                    leadBroker = findNewLeader(leadBroker, topic, partition, port);
                    continue;
                     }
                     numErrors = 0;
                     long numRead = 0;
                    
                     for(MessageAndOffset messageAndOffset : fetchResponse.messageSet(topic, partition)){
                           long currentOffset = messageAndOffset.offset();
                           // Safety check: It may be case we get old messages so just discard them (like in case of compression)
                           if(currentOffset< readoffset){
                                  System.out.println("Found an old offset: " + currentOffset + " Expecting: " + readoffset);
                    continue;
                           }
                           readoffset = messageAndOffset.nextOffset();
                           ByteBuffer payload = messageAndOffset.message().payload();
                          
                           byte[] bytes = new byte[payload.limit()];
                           payload.get(bytes);
                            System.out.println(String.valueOf(messageAndOffset.offset()) + ": " + new String(bytes, "UTF-8"));
                numRead++;
                maxReads--;
                     }
                    
                     if (numRead == 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                }
            }
                    
                    
              }
              if (consumer != null) consumer.close();
       }


       private List<String> m_replicaBrokers = new ArrayList<String>();

       private PartitionMetadata findLeader(List<String> a_seedBrokers,
                     int a_port, String a_topic, int a_partition) {

              PartitionMetadata returnMetaData = null;

              loop: for (String seed : a_seedBrokers) {
                     SimpleConsumer consumer = null;
                     try {
                           consumer = new SimpleConsumer(seed, a_port, 100000, 64 * 1024,
                                         "leaderLookup");

                           List<String> topics = Collections.singletonList(a_topic);
                           TopicMetadataRequest req = new TopicMetadataRequest(topics);
                           TopicMetadataResponse resp = consumer.send(req);
                           List<TopicMetadata> metaData = resp.topicsMetadata();

                           for (TopicMetadata item : metaData) {
                                  for (PartitionMetadata part : item.partitionsMetadata()) {
                                         if (part.partitionId() == a_partition) {
                                                returnMetaData = part;
                                                break loop;
                                         }
                                  }
                           }

                     } catch (Exception e) {
                           System.out.println("Error communicating with Broker [" + seed
                                         + "] to find Leader for [" + a_topic + ", "
                                         + a_partition + "] Reason: " + e);
                     } finally {
                           if (consumer != null)
                                  consumer.close();
                     }
              }

              if (returnMetaData != null) {
                     m_replicaBrokers.clear();
                    
                     for (Broker replica : returnMetaData.replicas()) {
                           m_replicaBrokers.add(replica.host());
                     }
              }

              return returnMetaData;
       }

       public static long getLastOffset(SimpleConsumer consumer, String topic,
                     int partition, long whichTime, String clientName) {
              TopicAndPartition topicAndPartition = new TopicAndPartition(topic,
                           partition);
              Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
              requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(
                           whichTime, 1));
              OffsetRequest request = new OffsetRequest(requestInfo,
                           kafka.api.OffsetRequest.CurrentVersion(), clientName);

              OffsetResponse response = consumer.getOffsetsBefore(request);

              if (response.hasError()) {
                     System.out
                                  .println("Error fetching data Offset Data the Broker. Reason: "
                                                + response.errorCode(topic, partition));
                     return 0;
              }
              long[] offsets = response.offsets(topic, partition);
              return offsets[0];
       }

       private String findNewLeader(String a_oldLeader, String a_topic,
                     int a_partition, int a_port) throws Exception {
              for (int i = 0; i < 3; i++) {
                     boolean goToSleep = false;
                     PartitionMetadata metadata = findLeader(m_replicaBrokers, a_port,
                                  a_topic, a_partition);
                     if (metadata == null) {
                           goToSleep = true;
                     } else if (metadata.leader() == null) {
                           goToSleep = true;
                     } else if (a_oldLeader.equalsIgnoreCase(metadata.leader().host())
                                  && i == 0) {
                           // first time through if the leader hasn't changed give
                           // ZooKeeper a second to recover
                           // second time, assume the broker did recover before failover,
                           // or it was a non-Broker issue
                           //
                           goToSleep = true;
                     } else {
                           return metadata.leader().host();
                     }
                     if (goToSleep) {
                           try {
                                  Thread.sleep(1000);
                           } catch (InterruptedException ie) {
                           }
                     }
              }
              System.out
                           .println("Unable to find new leader after Broker failure. Exiting");
              throw new Exception(
                           "Unable to find new leader after Broker failure. Exiting");
       }
}


Comments

Popular posts

Python [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Missing Authority Key Identifier

  Error requests.exceptions.SSLError: HTTPSConnectionPool  Max retries exceeded with url:  (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Missing Authority Key Identifier (_ssl.c:1028)'))). Analysis & Solution Recently, we updated from Python 3.11 to 3.13, which resulted in error above. We did verify AKI = SKI in chain of certificates. Also, imported chain of certificates into certifi. Nothing worked for us. Seemingly, it is a bug with Python 3.13. So, we downgraded to Python 3.12 and it started working. Other problems and solution -  '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1006)'))) solution  pip install pip-system-certs [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired  (_ssl.c:1006) solution  1# openssl s_client -showcerts -connect  signin.aws.amazon.com:443  </dev/...




Spring MongoDB Rest API not returning response in 90 seconds which is leading to client timeout

  We have Spring Boot  Rest API deployed in Kubernetes cluster which integrates with MongoDB to fetch the data.  MongoDB is fed with data by a real time Spark & NiFi job.  Our clients complained that for a request what they send they don't have response within 90 seconds. Consider it like an OMS ( Order ManagEment System).  On further analysis, we found that Spark & NiFi processing is happenning within 10 seconds after consuming response data from Kafka. Thus, initally out thought was that it due to delay from upstream to produce data in to Kafka.  Thankfully, our data had create / request  timestamp, and when response was received, and when response was inserted into MongoDB. Subtracting response insert time from request time seemed to be well within 90 seconds. But, still client did timeout on not seeing a response within 90 seconds. This led to confusion on our side.  But, then we realized it was due to Read Preference . We updated this...




MongoDB Regex Query taking more time in Production but same query perform well in UAT

   We came across a situation where-in, MongoDB Query was taking more time in Production like 10 seconds and 4.2 seconds but same query performed well in UAT taking under 400 ms. The very first thought that was evident to us that it is because of amount of data which differed in UAT and Production. Then we ran following to see the execution plan -   db.collection.aggregate(<queries>).explain() This gave us Winning and Rejected Plans. Under which, we analyzed that although it was using 'IXSCAN.' But, it was incorrect index- as we had one compound index built on time field and other fields, and there was other index just on time field for TTL purposes. Winning plan picked TTL index rather than compound index. Thus, we dropped TTL index and built TTL index on a different time field.  That got our query performance time from 10 seconds to 726 ms. Also, for other query the performance came down from 8 seconds to 4.3 seconds. Then, we ran following -  ...




What is Leadership

 




Spark MongoDB Connector Not leading to correct count or data while reading

  We are using Scala 2.11 , Spark 2.4 and Spark MongoDB Connector 2.4.4 Use Case 1 - We wanted to read a Shareded Mongo Collection and copy its data to another Mongo Collection. We noticed that after Spark Job successful completion. Output MongoDB did not had many records. Use Case 2 -  We read a MongoDB collection and doing count on dataframe lead to different count on each execution. Analysis,  We realized that MongoDB Spark Connector is missing data on bulk read as a dataframe. We tried various partitioner, listed on page -  https://www.mongodb.com/docs/spark-connector/v2.4/configuration/  But, none of them worked for us. Finally, we tried  MongoShardedPartitioner  this lead to constant count on each execution. But, it was greater than the actual count of records on the collection. This seems to be limitation with MongoDB Spark Connector. But,  MongoShardedPartitioner  seemed closest possible solution to this kind of situation. But, it per...