summaryrefslogtreecommitdiff
path: root/test/elixir/test/partition_size_test.exs
blob: 68759ad91eaaa320db5c253b520783bae2926674 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
defmodule PartitionSizeTest do
  use CouchTestCase

  @moduledoc """
  Test Partition size functionality
  """

  setup do
    db_name = random_db_name()
    {:ok, _} = create_db(db_name, query: %{partitioned: true, q: 1})
    on_exit(fn -> delete_db(db_name) end)

    {:ok, [db_name: db_name]}
  end

  def get_db_info(dbname) do
    resp = Couch.get("/#{dbname}")
    assert resp.status_code == 200
    %{:body => body} = resp
    body
  end

  def get_partition_info(dbname, partition) do
    resp = Couch.get("/#{dbname}/_partition/#{partition}")
    assert resp.status_code == 200
    %{:body => body} = resp
    body
  end

  def mk_partition(i) do
    i |> rem(10) |> Integer.to_string() |> String.pad_leading(3, "0")
  end

  def mk_docid(i) do
    id = i |> Integer.to_string() |> String.pad_leading(4, "0")
    "#{mk_partition(i)}:#{id}"
  end

  def mk_docs(db_name) do
    docs =
      for i <- 1..1000 do
        group = Integer.to_string(rem(i, 3))

        %{
          :_id => mk_docid(i),
          :value => i,
          :some => "field",
          :group => group
        }
      end

    body = %{:w => 3, :docs => docs}
    resp = Couch.post("/#{db_name}/_bulk_docs", body: body)
    assert resp.status_code == 201
  end

  def save_doc(db_name, doc) do
    resp = Couch.post("/#{db_name}", query: [w: 3], body: doc)
    assert resp.status_code == 201
    %{:body => body} = resp
    body["rev"]
  end

  test "get empty partition", context do
    db_name = context[:db_name]
    partition = "non_existent_partition"

    info = get_partition_info(db_name, partition)

    assert info["doc_count"] == 0
    assert info["doc_del_count"] == 0
    assert info["partition"] == partition
    assert info["sizes"]["external"] == 0
    assert info["sizes"]["active"] == 0
  end

  test "unknown partition return's zero", context do
    db_name = context[:db_name]
    mk_docs(db_name)

    info = get_partition_info(db_name, "unknown")
    assert info["doc_count"] == 0
    assert info["doc_del_count"] == 0
    assert info["sizes"]["external"] == 0
    assert info["sizes"]["active"] == 0
  end

  test "simple partition size", context do
    db_name = context[:db_name]
    save_doc(db_name, %{_id: "foo:bar", val: 42})

    info = get_partition_info(db_name, "foo")
    assert info["doc_count"] == 1
    assert info["doc_del_count"] == 0
    assert info["sizes"]["external"] > 0
    assert info["sizes"]["active"] > 0
  end

  test "adding docs increases partition sizes", context do
    db_name = context[:db_name]
    save_doc(db_name, %{_id: "foo:bar", val: 42})
    pre_info = get_partition_info(db_name, "foo")

    save_doc(db_name, %{_id: "foo:baz", val: 24})
    post_info = get_partition_info(db_name, "foo")

    assert post_info["doc_count"] == 2
    assert post_info["doc_del_count"] == 0
    assert post_info["sizes"]["external"] > pre_info["sizes"]["external"]
    assert post_info["sizes"]["active"] > pre_info["sizes"]["active"]
  end

  test "updating docs affects partition sizes", context do
    db_name = context[:db_name]
    rev1 = save_doc(db_name, %{_id: "foo:bar", val: ""})
    info1 = get_partition_info(db_name, "foo")

    rev2 =
      save_doc(db_name, %{
        _id: "foo:bar",
        _rev: rev1,
        val: "this is a very long string that is so super long its beyond long"
      })

    info2 = get_partition_info(db_name, "foo")

    save_doc(db_name, %{
      _id: "foo:bar",
      _rev: rev2,
      val: "this string is shorter"
    })

    info3 = get_partition_info(db_name, "foo")

    assert info3["doc_count"] == 1
    assert info3["doc_del_count"] == 0

    assert info3["sizes"]["external"] > info1["sizes"]["external"]
    assert info2["sizes"]["external"] > info3["sizes"]["external"]
  end

  test "deleting a doc affects partition sizes", context do
    db_name = context[:db_name]
    rev1 = save_doc(db_name, %{_id: "foo:bar", val: "some stuff here"})
    info1 = get_partition_info(db_name, "foo")

    save_doc(db_name, %{_id: "foo:bar", _rev: rev1, _deleted: true})
    info2 = get_partition_info(db_name, "foo")

    assert info1["doc_count"] == 1
    assert info1["doc_del_count"] == 0

    assert info2["doc_count"] == 0
    assert info2["doc_del_count"] == 1

    assert info2["sizes"]["external"] < info1["sizes"]["external"]
  end

  test "design docs do not affect partition sizes", context do
    db_name = context[:db_name]
    mk_docs(db_name)

    pre_infos =
      0..9
      |> Enum.map(fn i ->
        get_partition_info(db_name, mk_partition(i))
      end)

    0..5
    |> Enum.map(fn i ->
      base = i |> Integer.to_string() |> String.pad_leading(5, "0")
      docid = "_design/#{base}"
      save_doc(db_name, %{_id: docid, value: "some stuff here"})
    end)

    post_infos =
      0..9
      |> Enum.map(fn i ->
        get_partition_info(db_name, mk_partition(i))
      end)

    assert post_infos == pre_infos
  end

  @tag :skip_on_jenkins
  test "get all partition sizes", context do
    db_name = context[:db_name]
    mk_docs(db_name)

    {esum, asum} =
      0..9
      |> Enum.reduce({0, 0}, fn i, {esize, asize} ->
        partition = mk_partition(i)
        info = get_partition_info(db_name, partition)
        assert info["doc_count"] == 100
        assert info["doc_del_count"] == 0
        assert info["sizes"]["external"] > 0
        assert info["sizes"]["active"] > 0
        {esize + info["sizes"]["external"], asize + info["sizes"]["active"]}
      end)

    db_info = get_db_info(db_name)
    assert db_info["sizes"]["external"] >= esum
    assert db_info["sizes"]["active"] >= asum
  end

  test "get partition size with attachment", context do
    db_name = context[:db_name]

    doc = %{
      _id: "foo:doc-with-attachment",
      _attachments: %{
        "foo.txt": %{
          content_type: "text/plain",
          data: Base.encode64("This is a text document to save")
        }
      }
    }

    save_doc(db_name, doc)

    db_info = get_db_info(db_name)
    foo_info = get_partition_info(db_name, "foo")

    assert foo_info["doc_count"] == 1
    assert foo_info["doc_del_count"] == 0
    assert foo_info["sizes"]["active"] > 0
    assert foo_info["sizes"]["external"] > 0

    assert foo_info["sizes"]["active"] <= db_info["sizes"]["active"]
    assert foo_info["sizes"]["external"] <= db_info["sizes"]["external"]
  end

  test "attachments don't affect other partitions", context do
    db_name = context[:db_name]
    mk_docs(db_name)

    pre_infos =
      0..9
      |> Enum.map(fn i ->
        get_partition_info(db_name, mk_partition(i))
      end)

    doc = %{
      _id: "foo:doc-with-attachment",
      _attachments: %{
        "foo.txt": %{
          content_type: "text/plain",
          data: Base.encode64("This is a text document to save")
        }
      }
    }

    save_doc(db_name, doc)

    att_info = get_partition_info(db_name, "foo")
    assert att_info["doc_count"] == 1
    assert att_info["sizes"]["external"] > 0

    post_infos =
      0..9
      |> Enum.map(fn i ->
        get_partition_info(db_name, mk_partition(i))
      end)

    assert post_infos == pre_infos

    esize =
      ([att_info] ++ post_infos)
      |> Enum.reduce(0, fn info, acc ->
        info["sizes"]["external"] + acc
      end)

    db_info = get_db_info(db_name)
    assert esize == db_info["sizes"]["external"]
  end

  test "partition activity not affect other partition sizes", context do
    db_name = context[:db_name]
    mk_docs(db_name)

    partition1 = "000"
    partition2 = "001"

    info2 = get_partition_info(db_name, partition2)

    doc_id = "#{partition1}:doc-with-attachment"

    doc = %{
      _id: doc_id,
      _attachments: %{
        "foo.txt": %{
          content_type: "text/plain",
          data: Base.encode64("This is a text document to save")
        }
      }
    }

    doc_rev = save_doc(db_name, doc)

    info2_attach = get_partition_info(db_name, partition2)
    assert info2_attach == info2

    doc =
      Enum.into(
        %{
          another: "add another field",
          _rev: doc_rev
        },
        doc
      )

    doc_rev = save_doc(db_name, doc)

    info2_update = get_partition_info(db_name, partition2)
    assert info2_update == info2

    resp = Couch.delete("/#{db_name}/#{doc_id}", query: %{rev: doc_rev})
    assert resp.status_code == 200

    info2_delete = get_partition_info(db_name, partition2)
    assert info2_delete == info2
  end

  test "purging docs decreases partition size", context do
    db_name = context[:db_name]
    mk_docs(db_name)

    partition = "000"

    query = [
      start_key: "\"#{partition}:0000\"",
      end_key: "\"#{partition}:9999\"",
      limit: 50
    ]

    resp = Couch.get("/#{db_name}/_all_docs", query: query)
    assert resp.status_code == 200
    %{body: body} = resp

    pre_info = get_partition_info(db_name, partition)

    pbody =
      body["rows"]
      |> Enum.reduce(%{}, fn row, acc ->
        Map.put(acc, row["id"], [row["value"]["rev"]])
      end)

    resp = Couch.post("/#{db_name}/_purge", query: [w: 3], body: pbody)
    assert resp.status_code == 201

    post_info = get_partition_info(db_name, partition)
    assert post_info["doc_count"] == pre_info["doc_count"] - 50
    assert post_info["doc_del_count"] == 0
    assert post_info["sizes"]["active"] < pre_info["sizes"]["active"]
    assert post_info["sizes"]["external"] < pre_info["sizes"]["external"]
  end
end