Skip to content

Instantly share code, notes, and snippets.

# Find the partition that contains messages from this connector name. Usually by grepping
kafkacat -C -Z -b localhost -t <connect.offsets.topics> -o <number> | grep "servername"
# Set an null offset.
echo '["<connector-name",{"server":"<servername>"}]|' \|
kafkacat -P -Z -b localhost -t <connect.offsets.topics> -K \|
-p <partition>
@isopropylcyanide
isopropylcyanide / generateRandomInRangeAroundNormal.py
Created February 21, 2021 07:34
Generate N random numbers in range(A, B) with an average mean of X
import numpy as np
from math import ceil, floor
"""
The idea is to assume a normal Gaussian distribution around the given
average for a given range. Random numbers will be generated on either
side of the normal.
"""
def randomCeilOrFloor(X):
#include <bits/stdc++.h>
enum direction {
N, S, W, E
};
//pre compute where robot will move when he moves left from a fixed direction
map<direction, direction> leftDir = {
{N, W}, {W, S}, {S, E}, {E, N}};
public class WorkExecutor {
private final MathService mathService;
public List<Integer> processExpensiveFunction(List<Integer> inputs, int firstK) {
try {
List<Integer> outputs = new ArrayList<>();
//our basic executor
ExecutorService executor = Executors.newCachedThreadPool();
ExecutorCompletionService<Integer> executorService = new ExecutorCompletionService<>(executor);
public interface ConcurrentWorkExecutor {
/**
* Splits a work iterable and submits each task into an executor where the task input is obtained by
* applying a mapper function. The intermediate non null results are joined until a {@code size} number
* of results are obtained post which they are collected using a custom user defined collector.
*
* @param size the number of results to wait for
* @param iterable the work represented as an iterable
* @param mapper the transformation of individual item U within the iterable to a result T
public List<Integer> processExpensiveFunctionElegant(List<Integer> inputs, int firstK) {
try {
return concurrentWorkExecutor.splitJoin(firstK,
inputs,
mathService::process,
Collectors.toList());
} catch (Exception ex) {
log.error("Error processing expensive function ", ex);
return null;
/**
* A concurrent work executor that blocks for the final result after individual execution results
* are obtained. The results are fed into the queue represented by completion service as they are
* getting completed. Note that this behavior can be changed by using a single threaded executor
* <p>
* If execution of any individual work results in an exception, an exception is raised
*/
static class OutOfOrderConcurrentWorkExecutor implements ConcurrentWorkExecutor {
@Override
@isopropylcyanide
isopropylcyanide / ConcurrentWorkExecutor.java
Last active April 12, 2021 21:16
Concurrently execute work and aggregate results using a completion service. Tests are added. In one file for brevity.
package com.random.aman;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletionService;
public List<PaymentMethods> getTopKPaymentMethodDetails(List<Integer> paymentIds, int k) {
try {
return concurrentWorkExecutor.splitJoin(k,
paymentIds,
paymentService::fetch,
Collectors.toList());
} catch (Exception ex) {
log.error("Error processing top K payment methods", ex);
return null;
public List<Integer> processExpensiveFunctionElegant(List<Integer> inputs, int firstK) {
try {
return concurrentWorkExecutor.splitJoin(firstK,
inputs,
mathService::process,
Collectors.toList());
} catch (Exception ex) {
log.error("Error processing expensive function", ex);
return null;