Skip to content

Instantly share code, notes, and snippets.

@jpe42
jpe42 / ClientSideMapping.java
Last active June 7, 2016 03:59
Redis Cluster client side host/port mapping using Jedipus
final Map<Node, Node> nodeMappings = new HashMap<>(3);
nodeMappings.put(Node.create("internal1", 7001), Node.create("public1", 7001));
nodeMappings.put(Node.create("internal1", 7002), Node.create("public2", 7002));
nodeMappings.put(Node.create("internal1", 7003), Node.create("public3", 7003));
final Collection<Node> discoveryNodes = Collections.singleton(Node.create("public1", 7001));
// Static mapper, could easily be something dynamic as well.
final NodeMapper nodeMapper =
@jpe42
jpe42 / BITFIELD_INCRBY_GET_Demo.java
Last active May 31, 2016 21:47
Jedipus BITFIELD Examples
// BITFIELD command taken from http://redis.io/commands/BITFIELD
// BITFIELD mykey INCRBY i5 100 1 GET u4 0
final CmdByteArray<long[]> bitfieldCmd = CmdByteArray.startBuilding(Cmds.BITFIELD, 9)
.addSlotKey("mykey")
.addSubCmd(Cmds.BITFIELD_INCRBY, "i5", "100", "1")
.addSubCmd(Cmds.BITFIELD_GET, "u4", "0").create();
try (final RedisClient client =
RedisClientFactory.startBuilding().create(Node.create("localhost", 6379))) {
@jpe42
jpe42 / keybase.md
Last active November 19, 2020 17:20
Keybase.io PGP Proof

Keybase proof

I hereby claim:

  • I am jpe42 on github.
  • I am jamespedwards42 (https://keybase.io/jamespedwards42) on keybase.
  • I have a public key whose fingerprint is 6898 5D15 3F6F 6890 ADA1 F473 30CE 9FFF 6EDE 7CAF

To claim this, I am signing this object:

@jpe42
jpe42 / JedipusBulkReplyOffLoading.java
Last active June 2, 2016 18:35
Bulk REPLY OFF Loading w/ Jedipus
private static int runWithJedipus(final IntStream keys) {
int count = 0;
try (final RedisClient client =
RedisClientFactory.startBuilding().create(Node.create("127.0.0.1", 6379))) {
client.sendCmd(Cmds.FLUSHALL);
final long start = System.currentTimeMillis();
@jpe42
jpe42 / JedipusPubSub.java
Last active June 7, 2016 04:05
Jedipus Redis Pub/Sub
final Node node = Node.create("127.0.0.1", 6379);
final String channel = "jedipus";
final RedisClientFactory.Builder clientFactory = RedisClientFactory.startBuilding();
final ElementRetryDelay<Node> nodeRetryDelay =
ElementRetryDelay.startBuilding().withMaxDelay(Duration.ofSeconds(30)).create();
final RedisClientExecutor clientExecutor = RedisClientExecutor.startBuilding()
.withClientFactory(clientFactory)
@jpe42
jpe42 / Hex.java
Created August 22, 2016 18:48
Java 9 Hex Coder
import java.util.PrimitiveIterator;
import static java.lang.Character.digit;
public final class Hex {
private Hex() {}
private static final char[] HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
@jpe42
jpe42 / AtomicLogCounters.java
Last active April 12, 2017 06:49
Compact Atomic Log Counters
package systems.comodal.collision.cache;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.ThreadLocalRandom;
/**
* Provides atomic operations for 8-bit logarithmic counters backed by a byte array.
*
* @author James P. Edwards
@jpe42
jpe42 / HttpServletRequestUtil.java
Created October 13, 2016 16:49
Get requestor ip
import javax.servlet.http.HttpServletRequest;
public class RequestUtil {
public static String getRequestIp(final HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null) {
return request.getRemoteAddr();
}
ip = ip.trim();
@jpe42
jpe42 / createMasters.sh
Last active December 19, 2016 04:09
Create Redis Masters as Swarm Services
#!/bin/bash
#./createMasters.sh 7001 5 redis-cluster-m- comodal/alpine-redis
readonly STARTING_PORT=${1:-0}
readonly NUM_MASTERS=${2:-0}
readonly NAME_PREFIX=${3:-"redis-cluster-m-"}
readonly IMAGE=${4:-"comodal/alpine-redis"}
for ((port = STARTING_PORT, endPort = port + NUM_MASTERS; port < endPort; port++)) do
name="$NAME_PREFIX$port"
docker service create\
--name "$name"\
@jpe42
jpe42 / meetNodes.sh
Last active December 21, 2016 17:36
Automates the MEET process for Redis Swarm Services.
#!/bin/bash
#./meetNodes.sh 7001 5 redis-cluster-m-
readonly STARTING_PORT=${1:-0}
readonly NUM_MASTERS=${2:-0}
readonly NAME_PREFIX=${3:-"redis-cluster-m-"}
readonly LOCAL_CONTAINER_ID=$(docker ps -f name="$NAME_PREFIX" -q | head -n 1)
readonly LOCAL_PORT=$(docker inspect --format='{{index .Config.Labels "com.docker.swarm.service.name"}}' "$LOCAL_CONTAINER_ID" | sed 's|.*-||')
# From local service task, meet all other nodes
for ((port = STARTING_PORT, endPort = STARTING_PORT + NUM_MASTERS; port < endPort; port++)) do
if [ "$LOCAL_PORT" == $port ]; then