Skip to content

Instantly share code, notes, and snippets.

@scalabl3
Last active December 15, 2015 07:19
Show Gist options
  • Save scalabl3/5222694 to your computer and use it in GitHub Desktop.
Save scalabl3/5222694 to your computer and use it in GitHub Desktop.
cbgb simple passivation test - ruby

UPDATED Results

So in order to make this work for Ruby, I have to add something in my wrapper around Couchbase Ruby gem (I do have a wrapper I made/use, need to modify it), to handle the Couchbase::Error::Network on pretty much every operation to be able to reconnect, since currently that is not automatic.

#<Couchbase::Bucket:0x007fcb1411d780 "http://localhost:8091/pools/default/buckets/test/" default_format=:document, default_flags=0x0, quiet=false, connected=true, timeout=2500000>
2013-03-22 09:54:59.109 INCR curr = 1, counter = 1; connected = true, sleeping for 1s
2013-03-22 09:55:00.109 wake
2013-03-22 09:55:00.109 INCR curr = 2, counter = 2; connected = true, sleeping for 2s
2013-03-22 09:55:02.110 wake
2013-03-22 09:55:02.111 INCR curr = 3, counter = 3; connected = true, sleeping for 4s
2013-03-22 09:55:06.111 wake
2013-03-22 09:55:06.111 INCR curr = 4, counter = 4; connected = true, sleeping for 8s
2013-03-22 09:55:14.112 wake
2013-03-22 09:55:14.112 INCR curr = 5, counter = 5; connected = true, sleeping for 16s
2013-03-22 09:55:30.113 wake
2013-03-22 09:55:30.114 INCR curr = 6, counter = 6; connected = true, sleeping for 32s
2013-03-22 09:56:02.114 wake
2013-03-22 09:56:02.114 INCR curr = 7, counter = 7; connected = true, sleeping for 64s
2013-03-22 09:57:06.114 wake
2013-03-22 09:57:06.115 INCR curr = 8, counter = 8; connected = true, sleeping for 128s
2013-03-22 09:59:14.115 wake
2013-03-22 09:59:14.115 INCR curr = 9, counter = 9; connected = true, sleeping for 256s
2013-03-22 10:03:30.116 wake
2013-03-22 10:03:30.117 INCR curr = 10, counter = 10; connected = true, sleeping for 512s
2013-03-22 10:12:02.118 wake
FAIL: Couchbase::Error::Network - failed to perform arithmetic operation (key="counter", error=0x10)
rbtest.rb:14:in `incr'
rbtest.rb:14:in `run_test'
rbtest.rb:26:in `block in <main>'
rbtest.rb:24:in `loop'
rbtest.rb:24:in `<main>'
2013-03-22 10:12:02.119 CBGB Disconnected due to Passivation, Reconnect and Resume
#<Couchbase::Bucket:0x007fcb1411d780 "http://localhost:8091/pools/default/buckets/test/" default_format=:document, default_flags=0x0, quiet=false, connected=true, timeout=2500000>
2013-03-22 10:12:02.123 INCR curr = 1, counter = 1; connected = true, sleeping for 1s
2013-03-22 10:12:03.124 wake
2013-03-22 10:12:03.124 INCR curr = 2, counter = 2; connected = true, sleeping for 2s
2013-03-22 10:12:05.124 wake
2013-03-22 10:12:05.125 INCR curr = 3, counter = 3; connected = true, sleeping for 4s
2013-03-22 10:12:09.126 wake
2013-03-22 10:12:09.126 INCR curr = 4, counter = 4; connected = true, sleeping for 8s
2013-03-22 10:12:17.127 wake
2013-03-22 10:12:17.127 INCR curr = 5, counter = 5; connected = true, sleeping for 16s

After bucket passivates, it comes back 3 seconds later (not because of an op, likely because there is still a live connection, and perhaps ruby/libcouchbase pings the cb server for something at a specified interval, i am unsure)

After passivation and re-activation, ruby client has network error, so I can handle that error automatically and reconnect (this I added after running the test [output below]).

Theoretically this should be able run forever, testing that now.

Simple Ruby Connect, Op, Sleep Cycle

require 'rubygems'
require 'couchbase'

CBGB = Couchbase.connect(bucket: 'test', quiet: false)
puts CBGB.inspect

def run_test
  counter = 0
  curr = 0
  timer = 1
  CBGB.set("counter", counter)

  while curr == counter
    curr = CBGB.incr("counter")
    counter += 1
    puts "#{Time.now.strftime "%Y-%m-%d %H:%M:%S.%L"} INCR curr = #{curr}, counter = #{counter}; connected = #{CBGB.connected?}, sleeping for #{timer}s"
    sleep timer
    puts "#{Time.now.strftime "%Y-%m-%d %H:%M:%S.%L"} wake"
    timer *= 2 
    timer = 1 if timer > 1024 # reset this at about 14 mins wait and start again
  end
end

loop do 
  begin 
    run_test
  rescue Exception => e
    puts "FAIL: #{e.class.to_s} - #{e.message}"
    puts e.backtrace
  
    if e.is_a? Couchbase::Error::Network
      CBGB.reconnect
    end
  end
end

ruby output

#<Couchbase::Bucket:0x007fc0a3232b28 "http://localhost:8091/pools/default/buckets/test/" default_format=:document, default_flags=0x0, quiet=false, connected=true, timeout=2500000>
2013-03-22 08:59:53.836 sleeping for 1s, curr = 1, counter = 1; connected = true
2013-03-22 08:59:54.837 wake
2013-03-22 08:59:54.837 sleeping for 2s, curr = 2, counter = 2; connected = true
2013-03-22 08:59:56.838 wake
2013-03-22 08:59:56.838 sleeping for 4s, curr = 3, counter = 3; connected = true
2013-03-22 09:00:00.839 wake
2013-03-22 09:00:00.840 sleeping for 8s, curr = 4, counter = 4; connected = true
2013-03-22 09:00:08.841 wake
2013-03-22 09:00:08.841 sleeping for 16s, curr = 5, counter = 5; connected = true
2013-03-22 09:00:24.841 wake
2013-03-22 09:00:24.841 sleeping for 32s, curr = 6, counter = 6; connected = true
2013-03-22 09:00:56.842 wake
2013-03-22 09:00:56.842 sleeping for 64s, curr = 7, counter = 7; connected = true
2013-03-22 09:02:00.842 wake
2013-03-22 09:02:00.843 sleeping for 128s, curr = 8, counter = 8; connected = true
2013-03-22 09:04:08.843 wake
2013-03-22 09:04:08.843 sleeping for 256s, curr = 9, counter = 9; connected = true
2013-03-22 09:08:24.844 wake
2013-03-22 09:08:24.845 sleeping for 512s, curr = 10, counter = 10; connected = true
2013-03-22 09:16:56.846 wake
FAIL: Couchbase::Error::Network - failed to perform arithmetic operation (key="counter", error=0x10)
rbtest.rb:14:in `incr'
rbtest.rb:14:in `<main>'
jasdeep @SpaceStation : ~/cbgb $ 

cbgb output

2013/03/22 08:59:50 cbgb - 0.2.0-6-g4f2787a
2013/03/22 08:59:50   addr=:11211
2013/03/22 08:59:50   data=./tmp
2013/03/22 08:59:50   default-bucket-name=default
2013/03/22 08:59:50   default-persistence=2
2013/03/22 08:59:50   default-quota=100MB
2013/03/22 08:59:50   num-partitions=1
2013/03/22 08:59:50   rest-couch=:8092
2013/03/22 08:59:50   rest-ns=:8091
2013/03/22 08:59:50   static-path=http://cbfs-ext.hq.couchbase.com/cbgb/static.zip
2013/03/22 08:59:50   v=true
2013/03/22 08:59:50   []
2013/03/22 08:59:50 loading buckets from: ./tmp
2013/03/22 08:59:50 loading bucket: default
2013/03/22 08:59:50 loading bucket: test
2013/03/22 08:59:50 listening data on: :11211
2013/03/22 08:59:50 listening rest-couch on: :8092
2013/03/22 08:59:50 Loading static content from http://cbfs-ext.hq.couchbase.com/cbgb/static.zip
2013/03/22 08:59:50 listening rest-ns on: :8091
2013/03/22 09:04:50 Passivating bucket default
2013/03/22 09:04:50 Scan complete, cleaned up 0 items with <nil>, 9 remaining
2013/03/22 09:09:50 Scan complete, cleaned up 0 items with <nil>, 10 remaining
2013/03/22 09:14:50 Passivating bucket test
2013/03/22 09:14:50 Scan complete, cleaned up 0 items with <nil>, 10 remaining
2013/03/22 09:14:53 loading bucket: test
2013/03/22 09:18:23 http: multiple response.WriteHeader calls
2013/03/22 09:18:23 Error sending streaming result: write tcp 127.0.0.1:58973: broken pipe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment