Skip to content

Instantly share code, notes, and snippets.

View HeartSaVioR's full-sized avatar
🏠
Working from home

Jungtaek Lim HeartSaVioR

🏠
Working from home
View GitHub Profile
@HeartSaVioR
HeartSaVioR / pipelined_transaction_workaround.java
Last active August 29, 2015 13:56
Jedis pipelined transaction sync() errors workaround
Jedis j = new Jedis(HOSTNAME, PORT);
j.set("foo", "314");
Pipeline p = j.pipelined();
p.multi();
Response<String> r = p.get("foo");
Response<String> multiResp = p.exec();
p.sync();
multiResp.get(); // to achieve r.get()
System.out.println(r.get());
@HeartSaVioR
HeartSaVioR / JedisPubSubTest.java
Last active August 29, 2015 13:56
JedisPubSubTest with Redis Cluster (include node down)
package net.heartsavior.dev;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
package net.heartsavior.dev;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
public class JedisConnectionSuddenlyBrokenTest {
public static void main(String[] args) throws InterruptedException {
Jedis jedis = new Jedis("127.0.0.1", 6379);
@HeartSaVioR
HeartSaVioR / BlpopTest.java
Last active August 29, 2015 14:00
Blpop with forcing waiting thread to interrupt
public class ListCommandsTest extends JedisCommandTestBase {
public void blpopWithForceKill() throws InterruptedException {
Thread th = new Thread(new Runnable() {
@Override
public void run() {
jedis.blpop(100, "foo");
}
});
th.start();
@HeartSaVioR
HeartSaVioR / ex-2-1.scala
Last active August 29, 2015 14:17
Functional Programming in SCALA (chap 2)
// not a good solution, loop relies on n, outer variable.
// I can include it to loop but then loop needs 4 parameters.
def fib(n: Int): Int = {
@annotation.tailrec
def loop(pp: Int, p: Int, idx: Int): Int = {
if (idx == n)
pp + p
else
loop(p, pp + p, idx + 1)
}
@HeartSaVioR
HeartSaVioR / ex-3-2.scala
Last active August 29, 2015 14:17
Functional Programming in Scala (chap 3)
def tail[A](lst: List[A]): List[A] = lst match {
case Nil => Nil
case x :: xs => xs
}
// solution in a book
// https://github.com/fpinscala/fpinscala/blob/master/answerkey/datastructures/02.answer.scala
/*
// Although we could return `Nil` when the input list is empty, we choose to throw an exception instead. This is a somewhat subjective choice. In our experience, taking the tail of an empty list is often a bug, and silently returning a value just means this bug will be discovered later, further from the place where it was introduced.
// It's generally good practice when pattern matching to use `_` for any variables you don't intend to use on the right hand side of a pattern. This makes it clear the value isn't relevant.
@HeartSaVioR
HeartSaVioR / proto.rb
Created June 12, 2015 08:42
proto.rb
def gen_redis_proto(*cmd)
proto = ""
proto << "*"+cmd.length.to_s+"\r\n"
cmd.each{|arg|
proto << "$"+arg.to_s.bytesize.to_s+"\r\n"
proto << arg.to_s+"\r\n"
}
proto
end
@HeartSaVioR
HeartSaVioR / experiment-gil-with-cpu-intensive.py
Created July 10, 2015 02:08
Python GIL with CPU intensive
import threading
def repeatEvery(n, func, *args, **kwargs):
def and_again():
func(*args, **kwargs)
t = threading.Timer(n, and_again)
t.daemon = True
t.start()
t = threading.Timer(n, and_again)
t.daemon = True
015-07-14 17:56:14 TRACE CommonsHttpTransport:186 - Opening HTTP transport to <ip>:9200
2015-07-14 17:56:14 ERROR NetworkClient:130 - Node [Invalid target URI HEAD@null/bfdc.error-2015.07.03/fluentd}] failed (<ip>:9200); selected next node [<ip>:9200]
2015-07-14 17:56:14 TRACE NetworkClient:118 - Caught exception while performing request [<ip>:9200][bfdc.error-2015.07.03/fluentd}] - falling back to the next node in line...
org.elasticsearch.hadoop.rest.EsHadoopTransportException: Invalid target URI HEAD@null/bfdc.error-2015.07.03/fluentd}
at org.elasticsearch.hadoop.rest.commonshttp.CommonsHttpTransport.execute(CommonsHttpTransport.java:393)
at org.elasticsearch.hadoop.rest.NetworkClient.execute(NetworkClient.java:101)
at org.elasticsearch.hadoop.rest.RestClient.execute(RestClient.java:317)
at org.elasticsearch.hadoop.rest.RestClient.execute(RestClient.java:309)
at org.elasticsearch.hadoop.rest.RestClient.exists(RestClient.java:366)
at org.elasticsearch.hadoop.rest.RestRepository.indexExists(RestReposit
@HeartSaVioR
HeartSaVioR / test-bulk-insert-normal-key.py
Created August 5, 2012 13:41
Redis bulk insert test - normal keys
# -*- encoding: utf-8 -*-
import sys
if len(sys.argv) < 3:
print "usage: %s [start] [end]" % sys.argv[0]
sys.exit(1)
start = int(sys.argv[1])
end = int(sys.argv[2])