File: servers.rb

package info (click to toggle)
ruby-redis-client 0.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,116 kB
  • sloc: ansic: 7,517; ruby: 5,801; makefile: 246; sh: 123
file content (263 lines) | stat: -rw-r--r-- 5,525 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# frozen_string_literal: true

require_relative "redis_builder"
require_relative "server_manager"

module Servers
  ROOT = Pathname.new(File.expand_path("../../", __dir__))
  platform = `echo $(uname -m)-$(uname -s | tr '[:upper:]' '[:lower:]')`.strip
  CACHE_DIR = ROOT.join("tmp/cache", platform)

  HOST = "127.0.0.1"
  CERTS_PATH = ServerManager::ROOT.join("test/fixtures/certs")

  SENTINEL_CONF_PATH = ServerManager::ROOT.join("tmp/sentinel.conf")
  SENTINEL_NAME = "cache"

  DEFAULT_REDIS_VERSION = "7.0"

  class << self
    def build_redis
      redis_builder.install
    end

    def redis_server_bin
      redis_builder.bin_path
    end

    def redis_builder
      @redis_builder ||= RedisBuilder.new(ENV.fetch("REDIS", DEFAULT_REDIS_VERSION), ROOT.join("tmp").to_s)
    end

    attr_accessor :all

    def reset
      all.reset
    end
  end

  class RedisManager < ServerManager
    def spawn
      begin
        dir.join("dump.rdb").to_s
      rescue Errno::ENOENT
      end
      super
    end

    def socket_file
      dir.join("redis.sock").to_s
    end

    def command
      ROOT.join("tmp/redis-#{worker_index.to_i}").mkpath
      [
        Servers.redis_server_bin,
        "--unixsocket", socket_file,
        "--unixsocketperm", "700",
        "--port", real_port.to_s,
        "--tls-port", real_tls_port.to_s,
        "--tls-cert-file", CERTS_PATH.join("redis.crt").to_s,
        "--tls-key-file", CERTS_PATH.join("redis.key").to_s,
        "--tls-ca-cert-file", CERTS_PATH.join("ca.crt").to_s,
        "--save", "",
        "--appendonly", "no",
        "--dir", dir,
      ]
    end

    def dir
      ROOT.join("tmp/redis-#{worker_index.to_i}")
    end

    def tls_port
      port + 10_000
    end

    def real_tls_port
      real_port + 10_000
    end
  end

  REDIS = RedisManager.new(
    "redis",
    port: 16379,
    real_port: 16380,
  )

  class RedisReplicaManager < ServerManager
    def command
      [
        Servers.redis_server_bin,
        "--port", real_port.to_s,
        "--save", "",
        "--appendonly", "no",
        "--dir", "tmp/",
        "--replicaof", REDIS.host, REDIS.real_port.to_s,
      ]
    end
  end

  REDIS_REPLICA = RedisReplicaManager.new(
    "redis_replica",
    port: 16381,
    real_port: 16382,
  )

  REDIS_REPLICA_2 = RedisReplicaManager.new(
    "redis_replica_2",
    port: 16383,
    real_port: 16384,
  )

  class SentinelManager < ServerManager
    def generate_conf
      conf_file.write(<<~EOS)
        sentinel monitor #{SENTINEL_NAME} #{REDIS.host} #{REDIS.port} 2
        sentinel down-after-milliseconds #{SENTINEL_NAME} 10
        sentinel failover-timeout #{SENTINEL_NAME} 2000
        sentinel parallel-syncs #{SENTINEL_NAME} 1
        user alice on allcommands allkeys >superpassword
      EOS
    end

    def conf_file
      ROOT.join("tmp/#{name}-#{worker_index.to_i}.conf")
    end

    def command
      [
        Servers.redis_server_bin,
        conf_file.to_s,
        "--port", real_port.to_s,
        "--sentinel",
      ]
    end

    def spawn
      generate_conf
      super
    end
  end

  SENTINELS = [
    SentinelManager.new(
      "redis_sentinel_1",
      port: 26_300,
      real_port: 26_301,
      command: [],
    ),
    SentinelManager.new(
      "redis_sentinel_2",
      port: 26_302,
      real_port: 26_303,
      command: [],
    ),
    SentinelManager.new(
      "redis_sentinel_3",
      port: 26_304,
      real_port: 26_305,
      command: [],
    ),
  ].freeze

  class ToxiproxyManager < ServerManager
    BIN = CACHE_DIR.join("toxiproxy-server")

    def spawn
      system(ROOT.join("bin/install-toxiproxy").to_s) unless BIN.exist?
      super
    end

    def command
      [
        ToxiproxyManager::BIN.to_s,
        "-port",
        port.to_s,
      ]
    end

    def on_ready
      Toxiproxy.host = "http://#{host}:#{port}"

      retries = 3

      begin
        Toxiproxy.populate(proxies)
      rescue SystemCallError, Net::HTTPError, Net::ProtocolError
        retries -= 1
        if retries > 0
          sleep 0.5
          retry
        else
          raise
        end
      end
    end

    def proxies
      [
        {
          name: "redis",
          upstream: "localhost:#{REDIS.real_port}",
          listen: ":#{REDIS.port}",
        },
        {
          name: "redis_tls",
          upstream: "localhost:#{REDIS.real_tls_port}",
          listen: ":#{REDIS.tls_port}",
        },
        {
          name: "redis_replica",
          upstream: "localhost:#{REDIS_REPLICA.real_port}",
          listen: ":#{REDIS_REPLICA.port}",
        },
        {
          name: "redis_replica_2",
          upstream: "localhost:#{REDIS_REPLICA_2.real_port}",
          listen: ":#{REDIS_REPLICA_2.port}",
        },
      ]
    end
  end

  class ToxiproxySentinelManager < ToxiproxyManager
    def proxies
      sentinels = SENTINELS.map do |sentinel|
        {
          name: sentinel.name,
          upstream: "localhost:#{sentinel.real_port}",
          listen: ":#{sentinel.port}",
        }
      end
      super + sentinels
    end
  end

  TOXIPROXY = ToxiproxyManager.new(
    "toxiproxy",
    port: 8475,
    command: [],
  )

  TOXIPROXY_SENTINELS = ToxiproxySentinelManager.new(
    "toxiproxy",
    port: 8475,
    command: [],
  )

  TESTS = ServerList.new(
    TOXIPROXY,
    REDIS,
  )

  SENTINEL_TESTS = ServerList.new(
    TOXIPROXY_SENTINELS,
    REDIS,
    REDIS_REPLICA,
    REDIS_REPLICA_2,
    *SENTINELS,
  )

  BENCHMARK = ServerList.new(REDIS)
end