summaryrefslogtreecommitdiff
path: root/lib/vendor/redis/examples
diff options
context:
space:
mode:
authorJacob Vosmaer <jacob@gitlab.com>2017-01-02 16:12:06 +0100
committerJacob Vosmaer <jacob@gitlab.com>2017-01-02 16:29:14 +0100
commit3fe9cea03a6384fd8f57f10e172c134ed5c0552d (patch)
tree1148f745058b8885cd07edeb414f5822b05d616f /lib/vendor/redis/examples
parenta3712cc18de8283b25c3a8a034ecc8c9b7feca48 (diff)
downloadgitlab-shell-redis-full-gem.tar.gz
Vendor redis_rb from Rubygems, not GitHubredis-full-gem
Also just include the entire gem. Space used is negligible and this way we don't have to think about what to leave in/out. Create a vendor-gem script in Ruby instead of Make.
Diffstat (limited to 'lib/vendor/redis/examples')
-rw-r--r--lib/vendor/redis/examples/basic.rb15
-rw-r--r--lib/vendor/redis/examples/consistency.rb114
-rw-r--r--lib/vendor/redis/examples/dist_redis.rb43
-rw-r--r--lib/vendor/redis/examples/incr-decr.rb17
-rw-r--r--lib/vendor/redis/examples/list.rb26
-rw-r--r--lib/vendor/redis/examples/pubsub.rb37
-rw-r--r--lib/vendor/redis/examples/sentinel.rb41
-rwxr-xr-xlib/vendor/redis/examples/sentinel/start49
-rw-r--r--lib/vendor/redis/examples/sets.rb36
-rw-r--r--lib/vendor/redis/examples/unicorn/config.ru3
-rw-r--r--lib/vendor/redis/examples/unicorn/unicorn.rb20
11 files changed, 401 insertions, 0 deletions
diff --git a/lib/vendor/redis/examples/basic.rb b/lib/vendor/redis/examples/basic.rb
new file mode 100644
index 0000000..7800cf7
--- /dev/null
+++ b/lib/vendor/redis/examples/basic.rb
@@ -0,0 +1,15 @@
+require 'redis'
+
+r = Redis.new
+
+r.del('foo')
+
+puts
+
+p'set foo to "bar"'
+r['foo'] = 'bar'
+
+puts
+
+p 'value of foo'
+p r['foo']
diff --git a/lib/vendor/redis/examples/consistency.rb b/lib/vendor/redis/examples/consistency.rb
new file mode 100644
index 0000000..df34e1a
--- /dev/null
+++ b/lib/vendor/redis/examples/consistency.rb
@@ -0,0 +1,114 @@
+# This file implements a simple consistency test for Redis-rb (or any other
+# Redis environment if you pass a different client object) where a client
+# writes to the database using INCR in order to increment keys, but actively
+# remember the value the key should have. Before every write a read is performed
+# to check if the value in the database matches the value expected.
+#
+# In this way this program can check for lost writes, or acknowledged writes
+# that were executed.
+#
+# Copyright (C) 2013-2014 Salvatore Sanfilippo <antirez@gmail.com>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+require 'redis'
+
+class ConsistencyTester
+ def initialize(redis)
+ @r = redis
+ @working_set = 10000
+ @keyspace = 100000
+ @writes = 0
+ @reads = 0
+ @failed_writes = 0
+ @failed_reads = 0
+ @lost_writes = 0
+ @not_ack_writes = 0
+ @delay = 0
+ @cached = {} # We take our view of data stored in the DB.
+ @prefix = [Process.pid.to_s,Time.now.usec,@r.object_id,""].join("|")
+ @errtime = {}
+ end
+
+ def genkey
+ # Write more often to a small subset of keys
+ ks = rand() > 0.5 ? @keyspace : @working_set
+ @prefix+"key_"+rand(ks).to_s
+ end
+
+ def check_consistency(key,value)
+ expected = @cached[key]
+ return if !expected # We lack info about previous state.
+ if expected > value
+ @lost_writes += expected-value
+ elsif expected < value
+ @not_ack_writes += value-expected
+ end
+ end
+
+ def puterr(msg)
+ if !@errtime[msg] || Time.now.to_i != @errtime[msg]
+ puts msg
+ end
+ @errtime[msg] = Time.now.to_i
+ end
+
+ def test
+ last_report = Time.now.to_i
+ while true
+ # Read
+ key = genkey
+ begin
+ val = @r.get(key)
+ check_consistency(key,val.to_i)
+ @reads += 1
+ rescue => e
+ puterr "Reading: #{e.class}: #{e.message} (#{e.backtrace.first})"
+ @failed_reads += 1
+ end
+
+ # Write
+ begin
+ @cached[key] = @r.incr(key).to_i
+ @writes += 1
+ rescue => e
+ puterr "Writing: #{e.class}: #{e.message} (#{e.backtrace.first})"
+ @failed_writes += 1
+ end
+
+ # Report
+ sleep @delay
+ if Time.now.to_i != last_report
+ report = "#{@reads} R (#{@failed_reads} err) | " +
+ "#{@writes} W (#{@failed_writes} err) | "
+ report += "#{@lost_writes} lost | " if @lost_writes > 0
+ report += "#{@not_ack_writes} noack | " if @not_ack_writes > 0
+ last_report = Time.now.to_i
+ puts report
+ end
+ end
+ end
+end
+
+Sentinels = [{:host => "127.0.0.1", :port => 26379},
+ {:host => "127.0.0.1", :port => 26380}]
+r = Redis.new(:url => "redis://master1", :sentinels => Sentinels, :role => :master)
+tester = ConsistencyTester.new(r)
+tester.test
diff --git a/lib/vendor/redis/examples/dist_redis.rb b/lib/vendor/redis/examples/dist_redis.rb
new file mode 100644
index 0000000..fe1c5da
--- /dev/null
+++ b/lib/vendor/redis/examples/dist_redis.rb
@@ -0,0 +1,43 @@
+require "redis"
+require "redis/distributed"
+
+r = Redis::Distributed.new %w[redis://localhost:6379 redis://localhost:6380 redis://localhost:6381 redis://localhost:6382]
+
+r.flushdb
+
+r['urmom'] = 'urmom'
+r['urdad'] = 'urdad'
+r['urmom1'] = 'urmom1'
+r['urdad1'] = 'urdad1'
+r['urmom2'] = 'urmom2'
+r['urdad2'] = 'urdad2'
+r['urmom3'] = 'urmom3'
+r['urdad3'] = 'urdad3'
+p r['urmom']
+p r['urdad']
+p r['urmom1']
+p r['urdad1']
+p r['urmom2']
+p r['urdad2']
+p r['urmom3']
+p r['urdad3']
+
+r.rpush 'listor', 'foo1'
+r.rpush 'listor', 'foo2'
+r.rpush 'listor', 'foo3'
+r.rpush 'listor', 'foo4'
+r.rpush 'listor', 'foo5'
+
+p r.rpop('listor')
+p r.rpop('listor')
+p r.rpop('listor')
+p r.rpop('listor')
+p r.rpop('listor')
+
+puts "key distribution:"
+
+r.ring.nodes.each do |node|
+ p [node.client, node.keys("*")]
+end
+r.flushdb
+p r.keys('*')
diff --git a/lib/vendor/redis/examples/incr-decr.rb b/lib/vendor/redis/examples/incr-decr.rb
new file mode 100644
index 0000000..98ff9d1
--- /dev/null
+++ b/lib/vendor/redis/examples/incr-decr.rb
@@ -0,0 +1,17 @@
+require 'redis'
+
+r = Redis.new
+
+puts
+p 'incr'
+r.del 'counter'
+
+p r.incr('counter')
+p r.incr('counter')
+p r.incr('counter')
+
+puts
+p 'decr'
+p r.decr('counter')
+p r.decr('counter')
+p r.decr('counter')
diff --git a/lib/vendor/redis/examples/list.rb b/lib/vendor/redis/examples/list.rb
new file mode 100644
index 0000000..b2f25cb
--- /dev/null
+++ b/lib/vendor/redis/examples/list.rb
@@ -0,0 +1,26 @@
+require 'rubygems'
+require 'redis'
+
+r = Redis.new
+
+r.del 'logs'
+
+puts
+
+p "pushing log messages into a LIST"
+r.rpush 'logs', 'some log message'
+r.rpush 'logs', 'another log message'
+r.rpush 'logs', 'yet another log message'
+r.rpush 'logs', 'also another log message'
+
+puts
+p 'contents of logs LIST'
+
+p r.lrange('logs', 0, -1)
+
+puts
+p 'Trim logs LIST to last 2 elements(easy circular buffer)'
+
+r.ltrim('logs', -2, -1)
+
+p r.lrange('logs', 0, -1)
diff --git a/lib/vendor/redis/examples/pubsub.rb b/lib/vendor/redis/examples/pubsub.rb
new file mode 100644
index 0000000..9da1506
--- /dev/null
+++ b/lib/vendor/redis/examples/pubsub.rb
@@ -0,0 +1,37 @@
+require "redis"
+
+puts <<-EOS
+To play with this example use redis-cli from another terminal, like this:
+
+ $ redis-cli publish one hello
+
+Finally force the example to exit sending the 'exit' message with:
+
+ $ redis-cli publish two exit
+
+EOS
+
+redis = Redis.new
+
+trap(:INT) { puts; exit }
+
+begin
+ redis.subscribe(:one, :two) do |on|
+ on.subscribe do |channel, subscriptions|
+ puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
+ end
+
+ on.message do |channel, message|
+ puts "##{channel}: #{message}"
+ redis.unsubscribe if message == "exit"
+ end
+
+ on.unsubscribe do |channel, subscriptions|
+ puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
+ end
+ end
+rescue Redis::BaseConnectionError => error
+ puts "#{error}, retrying in 1s"
+ sleep 1
+ retry
+end
diff --git a/lib/vendor/redis/examples/sentinel.rb b/lib/vendor/redis/examples/sentinel.rb
new file mode 100644
index 0000000..62fc5a4
--- /dev/null
+++ b/lib/vendor/redis/examples/sentinel.rb
@@ -0,0 +1,41 @@
+require 'redis'
+
+# This example creates a master-slave setup with a sentinel, then connects to
+# it and sends write commands in a loop.
+#
+# After 30 seconds, the master dies. You will be able to see how a new master
+# is elected and things continue to work as if nothing happened.
+#
+# To run this example:
+#
+# $ ruby -I./lib examples/sentinel.rb
+#
+
+at_exit do
+ begin
+ Process.kill(:INT, $redises)
+ rescue Errno::ESRCH
+ end
+
+ Process.waitall
+end
+
+$redises = spawn("examples/sentinel/start")
+
+Sentinels = [{:host => "127.0.0.1", :port => 26379},
+ {:host => "127.0.0.1", :port => 26380}]
+r = Redis.new(:url => "redis://master1", :sentinels => Sentinels, :role => :master)
+
+# Set keys into a loop.
+#
+# The example traps errors so that you can actually try to failover while
+# running the script to see redis-rb reconfiguring.
+(0..1000000).each{|i|
+ begin
+ r.set(i,i)
+ $stdout.write("SET (#{i} times)\n") if i % 100 == 0
+ rescue => e
+ $stdout.write("E")
+ end
+ sleep(0.01)
+}
diff --git a/lib/vendor/redis/examples/sentinel/start b/lib/vendor/redis/examples/sentinel/start
new file mode 100755
index 0000000..46567e1
--- /dev/null
+++ b/lib/vendor/redis/examples/sentinel/start
@@ -0,0 +1,49 @@
+#! /usr/bin/env ruby
+
+# This is a helper script used together with examples/sentinel.rb
+# It runs two Redis masters, two slaves for each of them, and two sentinels.
+# After 30 seconds, the first master dies.
+#
+# You don't need to run this script yourself. Rather, use examples/sentinel.rb.
+
+require "fileutils"
+
+$pids = []
+
+at_exit do
+ $pids.each do |pid|
+ begin
+ Process.kill(:INT, pid)
+ rescue Errno::ESRCH
+ end
+ end
+
+ Process.waitall
+end
+
+base = File.expand_path(File.dirname(__FILE__))
+
+# Masters
+$pids << spawn("redis-server --port 6380 --loglevel warning")
+$pids << spawn("redis-server --port 6381 --loglevel warning")
+
+# Slaves of Master 1
+$pids << spawn("redis-server --port 63800 --slaveof 127.0.0.1 6380 --loglevel warning")
+$pids << spawn("redis-server --port 63801 --slaveof 127.0.0.1 6380 --loglevel warning")
+
+# Slaves of Master 2
+$pids << spawn("redis-server --port 63810 --slaveof 127.0.0.1 6381 --loglevel warning")
+$pids << spawn("redis-server --port 63811 --slaveof 127.0.0.1 6381 --loglevel warning")
+
+FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel1.conf")
+FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel2.conf")
+
+# Sentinels
+$pids << spawn("redis-server tmp/sentinel1.conf --sentinel --port 26379")
+$pids << spawn("redis-server tmp/sentinel2.conf --sentinel --port 26380")
+
+sleep 30
+
+Process.kill(:KILL, $pids[0])
+
+Process.waitall
diff --git a/lib/vendor/redis/examples/sets.rb b/lib/vendor/redis/examples/sets.rb
new file mode 100644
index 0000000..31c49c3
--- /dev/null
+++ b/lib/vendor/redis/examples/sets.rb
@@ -0,0 +1,36 @@
+require 'rubygems'
+require 'redis'
+
+r = Redis.new
+
+r.del 'foo-tags'
+r.del 'bar-tags'
+
+puts
+p "create a set of tags on foo-tags"
+
+r.sadd 'foo-tags', 'one'
+r.sadd 'foo-tags', 'two'
+r.sadd 'foo-tags', 'three'
+
+puts
+p "create a set of tags on bar-tags"
+
+r.sadd 'bar-tags', 'three'
+r.sadd 'bar-tags', 'four'
+r.sadd 'bar-tags', 'five'
+
+puts
+p 'foo-tags'
+
+p r.smembers('foo-tags')
+
+puts
+p 'bar-tags'
+
+p r.smembers('bar-tags')
+
+puts
+p 'intersection of foo-tags and bar-tags'
+
+p r.sinter('foo-tags', 'bar-tags')
diff --git a/lib/vendor/redis/examples/unicorn/config.ru b/lib/vendor/redis/examples/unicorn/config.ru
new file mode 100644
index 0000000..ede5573
--- /dev/null
+++ b/lib/vendor/redis/examples/unicorn/config.ru
@@ -0,0 +1,3 @@
+run lambda { |env|
+ [200, {"Content-Type" => "text/plain"}, [Redis.current.randomkey]]
+}
diff --git a/lib/vendor/redis/examples/unicorn/unicorn.rb b/lib/vendor/redis/examples/unicorn/unicorn.rb
new file mode 100644
index 0000000..957d384
--- /dev/null
+++ b/lib/vendor/redis/examples/unicorn/unicorn.rb
@@ -0,0 +1,20 @@
+require "redis"
+
+worker_processes 3
+
+# If you set the connection to Redis *before* forking,
+# you will cause forks to share a file descriptor.
+#
+# This causes a concurrency problem by which one fork
+# can read or write to the socket while others are
+# performing other operations.
+#
+# Most likely you'll be getting ProtocolError exceptions
+# mentioning a wrong initial byte in the reply.
+#
+# Thus we need to connect to Redis after forking the
+# worker processes.
+
+after_fork do |server, worker|
+ Redis.current.disconnect!
+end