Skip to content

Instantly share code, notes, and snippets.

View zmstone's full-sized avatar

zmstone zmstone

View GitHub Profile
@zmstone
zmstone / safe_delegate.erl
Last active January 22, 2017 21:47
safe_delegate
%% @doc Safely delegate a task to another process.
%% The task can spawn linked processes without implicitly
%% bringing down the caller process.
%% EXIT reasons are captured and propagated to caller.
%% @end
safe_delegate(TaskFun) when is_function(TaskFun, 0) ->
Parent = self(),
TaskRef = make_ref(),
{Pid, Ref} =
erlang:spawn_monitor(
@zmstone
zmstone / brod_demo.ex
Created March 17, 2017 22:15
brod group subscriber test
defmodule BrodDemo do
@kafka_hosts [localhost: 9092]
@topic "brod-demo-group-subscriber-koc"
@produce_delay_seconds 2
## bootstrap demo with partition producers producing sequence numbers
## every DEFAULT seconds.
def bootstrap() do
bootstrap(@produce_delay_seconds)
end
## bootstrap demo subscrirber and a number of sequence number producers
@zmstone
zmstone / kpro_schema.erl
Created April 13, 2017 21:40
kafka protocol schema specs
%% generated code, do not edit!
-module(kpro_schema).
-export([get/2]).
get(produce_request, V) when V >= 0, V =< 2 ->
[{acks,int16},
{timeout,int32},
{topic_data,{array,[{topic,string},
{data,{array,[{partition,int32},
{record_set,records}]}}]}}];
%%% Copyright (c) 2014-2017, Klarna AB
%%%
%%% 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,
cd /tmp
# get code
rm -rf kafka_protocol
git clone https://github.com/klarna/kafka_protocol.git -b hack-in-magic-v2-decode
rm -rf brod
git clone https://github.com/klarna/brod.git -b hack-in-magic-v2-decode
# compile code
COMP="/usr/kastlex/erts-9.3.3.9/bin/erlc -I brod/include -I kafka_protocol/include"
@zmstone
zmstone / gen-preferred-replica-election.sh
Created December 2, 2019 10:11
shell script to generate JSON file for kafka-preferred-replica-election.sh
#!/bin/bash -eu
TOPIC="$1"
PARTITION_MIN="$2"
PARTITION_MAX="$3"
echo '{"partitions":['
for i in $(seq ${PARTITION_MIN} ${PARTITION_MAX}); do
echo -n "{\"topic\": \"${TOPIC}\",\"partition\": $i}"
if [ $i -lt $PARTITION_MAX ]; then
@zmstone
zmstone / replica-reassign.sh
Last active December 11, 2019 11:58
make kafka-reassign-partitions easier to use/control
#!/bin/bash -e
usage() {
echo "Usage: $0 -t <topic-name> -p <partition-number> -r <replicas> --throttle <bytes/s> [-z <zookeeper-host>] [--reorder-replicas]"
echo " where <replicas> is a comma separated node IDs in preferred order, or in natural order if --reorder-replicas is given"
echo "Example: $0 -t foo -p 0 -r 1,2,3 --throttle 10000000"
exit "$1"
}
REORDER_REPLICAS_LIST="NO"
while [[ $# -gt 0 ]]; do
@zmstone
zmstone / demote-leader.sh
Last active December 11, 2019 14:08
Help script to demote kafka leader node
#!/bin/bash -e
# Demoting leader is a necessary step before moving replicas off that node otherwise follower fetchers may crash due to a kafka bug.
# This script makes use of replica-reassign.sh script to demote a node from preferred leader in the given topic-partition's replicas list
# It then forces a preferred leader election for this topic-partition.
usage() {
echo "Usage: $0 -t <topic-name> -p <partition-number> -n <node-id> [-z <zookeeper-host>]"
echo "For example, if topic 'foo' partition 0 has replicas list [0,1,2]"
echo "Running command '$0 -t foo -p 0 -n 0' will change the order to [1,2,0]"
@zmstone
zmstone / unsmile.clj
Created February 28, 2020 14:30
cli tool to unsmile a jackson smile json file
#!/usr/bin/env lein-exec
;; this script requires clojure, lein with lein-exec plugin
;; ref: https://github.com/kumarshantanu/lein-exec
;; it accepts first one argument as the smile-format json file path
;; decodes the smile json and print the plain json to console
(use '[leiningen.exec :only (deps)])
(deps '[[cheshire "5.10.0"]])
(require '[cheshire.core :refer :all])
@zmstone
zmstone / start-emqx-cluster-in-docker.sh
Last active August 30, 2020 07:46
Start 2-node EMQX cluster in docker containers
#!/bin/bash
set -euo pipefail
## EMQX can only start with longname (https://erlang.org/doc/reference_manual/distributed.html)
## The host name part of EMQX's node name has to be static, this means we should either
## pre-assign static IP for containers, or ensure containers can communiate with each other by name
## this is why a docker network is created, and the containers's names have a dot.
NET='my.net'