cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob f2e01d6a6a38a843700e2bdd3d397047e12c9d8f 2850 bytes (raw)
$ git show malloc:test/mgmt_auto_adjust.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net>
# License: GPLv3 or later (see COPYING for details)
require 'test/test_helper'

class TestMgmtAutoAdjust < Test::Unit::TestCase
  def setup
    @tmpdir = Dir.mktmpdir('cmogstored-mgmt-test')
    @to_close = []
    @host = TEST_HOST
    srv = TCPServer.new(@host, 0)
    @port = srv.addr[1]
    srv.close

    http = TCPServer.new(@host, 0)
    @httpport = http.addr[1]
    http.close

    @err = Tempfile.new("stderr")
    Dir.mkdir("#@tmpdir/dev666")
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--mgmtlisten=#@host:#@port",
            "--httplisten=#@host:#@httpport",
            "--maxconns=500" ]
    vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
    @pid = fork {
      ENV["MOG_DISK_USAGE_INTERVAL"] = "1"
      $stderr.reopen(@err)
      @err.close
      exec(*cmd)
    }
    @client = get_client
  end

  def teardown
    Process.kill(:QUIT, @pid) rescue nil
    _, status = Process.waitpid2(@pid)
    @to_close.each { |io| io.close unless io.closed? }
    FileUtils.rm_rf(@tmpdir)
    @err.rewind
    # $stderr.write(@err.read)
    assert status.success?, status.inspect
  end

  def wait_for_seen_dev
    @client.write "watch\r\n"
    seen_dev = false
    while line = @client.gets
      line.strip!
      case line
      when /^666\t/
        seen_dev = true
      when "."
        break if seen_dev
      end
    end
  end

  def test_aio_threads_auto_adjust
    wait_for_seen_dev
    t_yield # wait for threads to spawn
    taskdir = "/proc/#@pid/task"
    glob = "#{taskdir}/*"
    prev_threads = Dir[glob].size if File.directory?(taskdir)
    if RUBY_PLATFORM =~ /linux/
      assert File.directory?(taskdir), "/proc not mounted on Linux?"
    end

    File.directory?(taskdir) or return
    Dir.mkdir("#@tmpdir/dev333")
    stop = Time.now + 10
    while prev_threads == Dir[glob].size && Time.now < stop
      sleep(0.1)
    end
    cur_threads = Dir[glob].size
    assert_operator cur_threads, :>, prev_threads,
           "prev_threads=#{cur_threads} > prev_threads=#{prev_threads}"
    assert_match(%r{updating server aio_threads=20}, File.read(@err.path))
  end

  def test_aio_threads_auto_adjust_warn
    @client.write "server aio_threads = 1\r\n"
    assert_equal "\r\n", @client.gets
    stop = Time.now + 60
    expect = "server aio_threads=1"
    while (! File.read(@err.path).include?(expect) && Time.now < stop)
      sleep 0.1
    end
    assert(File.read(@err.path).include?(expect), File.read(@err.path))

    Dir.mkdir("#@tmpdir/dev333")
    warning = "fewer aio_threads(1) than devices(2)"
    stop = Time.now + 10
    while (! File.read(@err.path).include?(warning)) && Time.now < stop
      sleep 0.1
    end
    assert(File.read(@err.path).include?(warning), File.read(@err.path))
  end
end

git clone https://yhbt.net/cmogstored.git