test_consolidate.cpp 16.4 KB
Newer Older
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
// Copyright 2017-2020, Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "test_all.h"
#include "../exception.h"
#include "../impl/file.h"
#include "../impl/file_consolidate.h"

#include <iostream>
#include <memory>


using namespace OpenZGY;
using namespace OpenZGY::Errors;
using namespace InternalZGY;

namespace {
#if 0
}
#endif

static int _nextseq = 0;

static std::string nicenumber(std::int64_t n)
{
  std::string neg(n<0?"-":"");
  n = std::abs(n);
  if (n >= (1024*1024*1024) && (n % (1024*1024*1024)) == 0)
    return neg + std::to_string(n / (1024*1024*1024)) + " GB";
  if (n >= (1024*1024) && (n % (1024*1024)) == 0)
    return neg + std::to_string(n / (1024*1024)) + " MB";
  if (n >= (1024) && (n % (1024)) == 0)
    return neg + std::to_string(n / (1024)) + " KB";
  return neg + std::to_string(n) + " bytes";
}

class MyReceiver
{
public:
  std::shared_ptr<int> firstval;
  std::shared_ptr<int> gotsize;
  std::shared_ptr<int> callcount;
  int seq;
public:
  MyReceiver() : firstval(new int), gotsize(new int), callcount(new int)
  {
    *firstval = 0;
    *gotsize = 0;
    *callcount = 0;
    seq = ++_nextseq;
    //std::cout << "MyReceiver #" << seq << " was constructed" << std::endl;
  }

  MyReceiver(const MyReceiver& other) : firstval(other.firstval), gotsize(other.gotsize), callcount(other.callcount)
  {
    seq = ++_nextseq;
    //std::cout << "MyReceiver #" << seq << " was cloned" << std::endl;
  }

  void operator()(const void* data, std::int64_t size)
  {
    *callcount += 1;
    *gotsize = size;
    if (size >= 3*static_cast<std::int64_t>(sizeof(int))) {
      const int* const idata = reinterpret_cast<const int*>(data);
      *firstval = idata[0];
      if (false)
        std::cout << "Receiver # " << seq
                  << ": Deliver " << nicenumber(size) << ": "
                  << idata[0] << ", " << idata[1] << ", " << idata[2] << ", ..."
                  << "\n";
    }
    else {
      //std::cout << std::hex << "Deliver " << size << " bytes.\n" << std::dec;
    }
  }

  bool check(int expect_firstval, int expect_gotsize) const
  {
    if (*callcount == 1 && *gotsize == expect_gotsize && *firstval == expect_firstval)
      return true;
    std::cout << "ERROR: Did not get expected delivery."
              << "\n  Expect "  << 1 << " delivery"
              << " with first " << expect_firstval
              << " and size "   << expect_gotsize
              << "\n  Actual "  << *callcount << " delivery"
              << " with first " << *firstval
              << " and size "   << *gotsize
              << std::endl;
    return false;
  }
};

/**
 * Trivial case: Empty list in, empty list out.
 */
void
test_zero()
{
  ReadList list{};
  ReadList result = ConsolidateRequests::consolidate(list);
  TEST_CHECK(result.size() == 0);
}

/**
 * Trivial case: Single-block request in, equivalent request out.
 * The code might do a short cut and just return the input list,
 * or it can insert a (in this case) unneeded method to forward
 * the delivery.
 */
void
test_one()
{
  MyReceiver receiver;
  ReadList list{ReadRequest(0x1000, 0x3000, receiver)};
  ReadList result = ConsolidateRequests::consolidate(list);
  TEST_CHECK(result.size() == 1);
  TEST_CHECK(result[0].offset == list[0].offset);
  TEST_CHECK(result[0].size == list[0].size);
  TEST_CHECK((bool)result[0].delivery);
  std::vector<int> data(list[0].size, 42);
  data[0] = list[0].offset;
  data[1] = list[0].size;
  result[0].delivery(data.data(), data.size() * sizeof(int));
}

void
run_test(const ReadList& list_in,
         const ReadList& expect,
         std::int64_t max_hole,
         std::int64_t max_size,
         std::int64_t align,
         bool do_overlaps,
         std::int64_t filesize)
{
  // Caller did not provide delivery callbacka, neither for the list to
  // process nor for the expected result. This probably only makes sense
  // in a unit test. Here I add my own callbacks for the input list.
  // The expect list is just used for offset/size; arguably this shouldn't
  // have been a ReadList at all.
  ReadList list(list_in);
  std::vector<MyReceiver> receivers(list.size());
  for (std::size_t ii = 0; ii < list.size(); ++ii)
    list[ii].delivery = receivers[ii];

  // Run the algorithm under test.
  ReadList result = ConsolidateRequests::consolidate
    (list, max_hole, max_size, align, do_overlaps, filesize);

  //ConsolidateRequests::_print_requests(ReadDoubleList{expect}, "Expect:");

  // Compare expected and actual consolidated list.
  // This does not check whether data is delivered correctly.
  TEST_CHECK(result.size() == expect.size());
  for (std::size_t ii = 0; ii < result.size() && ii < expect.size(); ++ii)
    TEST_CHECK(result[ii].offset == expect[ii].offset &&
               result[ii].size   == expect[ii].size);

  // Pretend we read this data file a file. The raw data contains the
  // offset from the start of the buffer so we can check what was sent.
  std::vector<int> data(filesize/sizeof(int), 0);
  for (std::size_t ii=0; ii<data.size(); ++ii)
    data[ii] = sizeof(int)*static_cast<int>(ii);

  // Deliver my data according to the consolidated list.
  for (const ReadRequest& rr : result)
178
    FileADT::_deliver(rr.delivery, data.data(), rr.offset, rr.size, false);
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

  // Check that all the data that was originally requested
  // was delivered to the correct place in a timely manner.
  // One delivery to a consolidated request might end up as
  // deliveries to more than one of the original requests.
  for (std::size_t ii = 0; ii < list.size(); ++ii)
    TEST_CHECK(receivers[ii].check(list[ii].offset, list[ii].size));
}

void
test_simple()
{
  // Test data: 67 kb, may or may not be algned to 4 kb boundaries.
  // Note 67 is not divisible by 4, so eof is not aligned.
  // The test itself submits a list of 7 regions to access.
  //
  // Visualize the contents of the request as follows:
  //   - First line: Ruler to show 4 KB aligned blocks.
  //   - Second line: A digit if this data is being requested.
  //     The actual digit is the position of this request
  //     in the input; they don't need to be sorted.
  //   - Third line: Ruler for file size, one character per KB.
  //
  // [--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--
  // --------2222------------1111333---4444-----5555----------7777-6666-
  // 0000000000111111111122222222223333333333444444444455555555556666666
  // .123456789.123456789.123456789.123456789.123456789.123456789.123456
  //
  // The first 3 chunks have start aligned to 4 KB, the rest are not.
  // The third chunk has an odd size.
  // The 6th chunk triggers a corner case: If start and end are adjusted
  // to align with 4 KB then the end position will be past eof.
  //
  // This test: No reading across holes. Request part 1 and 3 get combined,
  // everything else is unchanged. When checking the result, note that the
  // consolidated list will always be sorted on offset.

  const std::int64_t KB = 1024;

  ReadList list{ReadRequest(24*KB, 4*KB, nullptr),
                ReadRequest( 8*KB, 4*KB, nullptr),
                ReadRequest(28*KB, 3*KB, nullptr),
                ReadRequest(34*KB, 4*KB, nullptr),
                ReadRequest(43*KB, 4*KB, nullptr),
                ReadRequest(62*KB, 4*KB, nullptr),
                ReadRequest(57*KB, 4*KB, nullptr)
  };

  ReadList expect{ReadRequest( 8*KB, 4*KB, nullptr),
                  ReadRequest(24*KB, 7*KB, nullptr),
                  ReadRequest(34*KB, 4*KB, nullptr),
                  ReadRequest(43*KB, 4*KB, nullptr),
                  ReadRequest(57*KB, 4*KB, nullptr),
                  ReadRequest(62*KB, 4*KB, nullptr)
  };

  run_test(list, expect,
           /*max_hole*/0, /*max_size*/0, /*align*/0,
           /*do_overlap*/false, /*eof*/67*KB);
}

void
test_holes()
{
  // Same input data as test_simple but allow 8 KB holes.
  // The diagram below shows an asterisk for the data that will be
  // read and discarded.
  //
  // [--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--
  // --------2222------------1111333***4444*****5555----------7777*6666-
  // 0000000000111111111122222222223333333333444444444455555555556666666
  // .123456789.123456789.123456789.123456789.123456789.123456789.123456

  const std::int64_t KB = 1024;

  ReadList list{ReadRequest(24*KB, 4*KB, nullptr),
                ReadRequest( 8*KB, 4*KB, nullptr),
                ReadRequest(28*KB, 3*KB, nullptr),
                ReadRequest(34*KB, 4*KB, nullptr),
                ReadRequest(43*KB, 4*KB, nullptr),
                ReadRequest(62*KB, 4*KB, nullptr),
                ReadRequest(57*KB, 4*KB, nullptr)
  };

  ReadList expect{ReadRequest( 8*KB,  4*KB, nullptr),
                  ReadRequest(24*KB, 23*KB, nullptr),
                  ReadRequest(57*KB,  9*KB, nullptr)
  };

  run_test(list, expect,
           /*max_hole*/8*KB, /*max_size*/0, /*align*/0,
           /*do_overlap*/false, /*eof*/67*KB);
}

void
test_align()
{
  // Same input data as test_simple but align to 4 KB boundaries.
  // test1: No holes allowed, test2: 8 KB holes allowed.
  //
  // Note: Even if padding caused two chunks to become adjacent, they
  // might not get consolidated. See _split_requests. This is an
  // academic issue because max_hole will
  //
  // Note: The code should not attempt to read past eof. This is
  // a corner case. The test data covers that case (request #6)
  //
  // Note: Even if the input has no overlaps, padding might make some.
  // The test data covers that case (request 6 and 7).
  //
  // The diagram below shows an asterisk for the data
  // that will be read and discarded
  //
  // [--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--
  // --------2222------------1111333***4444*****5555*--------*7777*6666*
  // 0000000000111111111122222222223333333333444444444455555555556666666
  // .123456789.123456789.123456789.123456789.123456789.123456789.123456

  const std::int64_t KB = 1024;

  ReadList list{ReadRequest(24*KB, 4*KB, nullptr),
                ReadRequest( 8*KB, 4*KB, nullptr),
                ReadRequest(28*KB, 3*KB, nullptr),
                ReadRequest(34*KB, 4*KB, nullptr),
                ReadRequest(43*KB, 4*KB, nullptr),
                ReadRequest(62*KB, 4*KB, nullptr),
                ReadRequest(57*KB, 4*KB, nullptr)
  };

  // Expected result given that blocks don't consolidate
  // if they became contiguous just due to padding.
  ReadList expect1{ReadRequest( 8*KB,  4*KB, nullptr),
                   ReadRequest(24*KB,  8*KB, nullptr),
                   ReadRequest(32*KB,  8*KB, nullptr),
                   ReadRequest(40*KB,  8*KB, nullptr),
                   ReadRequest(56*KB,  8*KB, nullptr),
                   ReadRequest(60*KB,  7*KB, nullptr),
  };
  run_test(list, expect1,
           /*max_hole*/0, /*max_size*/0, /*align*/4*KB,
           /*do_overlap*/false, /*eof*/67*KB);

  // Expected result given that blocks will consolidate
  // anyway, since both hole removal and align is specified.
  ReadList expect2{ReadRequest( 8*KB,  4*KB, nullptr),
                   ReadRequest(24*KB, 24*KB, nullptr),
                   ReadRequest(56*KB, 11*KB, nullptr)
  };
  run_test(list, expect2,
           /*max_hole*/8*KB, /*max_size*/0, /*align*/4*KB,
           /*do_overlap*/false, /*eof*/67*KB);
}


void
test_overlap()
{
  // Test data:
  //   Request 1 and 3 fully overlap.
  //   Request 2 and 4 partly overlap.
  //   Request 5 and 6 are contiguous and should be merged.
  //   Request 7 is far away and should not be merged.
  //   Request 8 and 9 start at the same offset but differs in size.
  //
  // How to handle overlapping requests is still not decided.
  //
  //   - Pass do_overlap=false to allow overlaps but don't consolidate,
  //     which means the overlapping data actually gets read twice.
  //     This is simpler and also avoids issues with mutable buffers.
  //
  //   - Pass do_overlap=true to try to consolidate everything.
  //
  //   - Implement a check in the code that forbids overlapping requests.
  //     This is problematic because specifying an alignment might cause
  //     a valid list of requests to become overlapped.
  //
  // [--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--
  // --------2222--------------------66665555--------7777---------------
  // ----------44444444------------------------------888888-------------
  // ----------------------1111-----------------------------------------
  // ----------------------3333-----------------------------------------
  // 0000000000111111111122222222223333333333444444444455555555556666666
  // .123456789.123456789.123456789.123456789.123456789.123456789.123456

  const std::int64_t KB = 1024;

  ReadList list{ReadRequest(22*KB, 4*KB, nullptr),
                ReadRequest( 8*KB, 4*KB, nullptr),
                ReadRequest(22*KB, 4*KB, nullptr),
                ReadRequest(10*KB, 8*KB, nullptr),
                ReadRequest(36*KB, 4*KB, nullptr),
                ReadRequest(32*KB, 4*KB, nullptr),
                ReadRequest(48*KB, 4*KB, nullptr),
                ReadRequest(48*KB, 6*KB, nullptr)
  };

  // 5 and 6 should consolidate, the rest not because of overlap.
  // sorted order: 2,4,1,3,6+5,7,8
  ReadList expect1{ReadRequest( 8*KB,  4*KB, nullptr),
                   ReadRequest(10*KB,  8*KB, nullptr),
                   ReadRequest(22*KB,  4*KB, nullptr),
                   ReadRequest(22*KB,  4*KB, nullptr),
                   ReadRequest(32*KB,  8*KB, nullptr),
                   ReadRequest(48*KB,  4*KB, nullptr),
                   ReadRequest(48*KB,  6*KB, nullptr)
  };

  run_test(list, expect1,
           /*max_hole*/0*KB, /*max_size*/0, /*align*/0,
           /*do_overlap*/false, /*eof*/67*KB);

  // Now allow consolidating overlapping requests.
  ReadList expect2{ReadRequest( 8*KB, 10*KB, nullptr),
                   ReadRequest(22*KB,  4*KB, nullptr),
                   ReadRequest(32*KB,  8*KB, nullptr),
                   ReadRequest(48*KB,  6*KB, nullptr)
  };
  run_test(list, expect2,
           /*max_hole*/0*KB, /*max_size*/0, /*align*/0,
           /*do_overlap*/true, /*eof*/67*KB);
}

void
test_large()
{
  // Some requests that might have been consolidated but won't be
  // because the total size got too large.
  //
  // There are still opportunities for optimizing: When a region is split
  // in two it could be split in to roughly equal parts instead of using
  // a greedy algorithm. But frankly I doubt this will make much difference.
  // With this particular test data the greedy algorithm gives the best
  // result in any case.
  //
  // [--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--][--
  // ---111222222222233333--------455555555555555555555----6------------
  // 0000000000111111111122222222223333333333444444444455555555556666666
  // .123456789.123456789.123456789.123456789.123456789.123456789.123456

  const std::int64_t KB = 1024;

  ReadList list{ReadRequest( 3*KB,  3*KB, nullptr),
                ReadRequest( 6*KB, 10*KB, nullptr),
                ReadRequest(16*KB,  5*KB, nullptr),
                ReadRequest(29*KB,  1*KB, nullptr),
                ReadRequest(30*KB, 20*KB, nullptr),
                ReadRequest(54*KB,  1*KB, nullptr)
  };

  ReadList expect1{ReadRequest( 3*KB, 13*KB, nullptr),
                   ReadRequest(16*KB,  5*KB, nullptr),
                   ReadRequest(29*KB,  1*KB, nullptr),
                   ReadRequest(30*KB, 20*KB, nullptr),
                   ReadRequest(54*KB,  1*KB, nullptr),
  };
  run_test(list, expect1,
           /*max_hole*/0, /*max_size*/16*KB, /*align*/0*KB,
           /*do_overlap*/false, /*eof*/67*KB);

}

} // namespace for tests

namespace {
  class Register
  {
  public:
    Register()
    {
      register_test("consolidate.zero",        test_zero);
      register_test("consolidate.one",         test_one);
      register_test("consolidate.simple",      test_simple);
      register_test("consolidate.holes",       test_holes);
      register_test("consolidate.align",       test_align);
      register_test("consolidate.overlap",     test_overlap);
      register_test("consolidate.large",       test_large);
    }
  } dummy;
} // namespace for registration