aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/tensor/hnsw_index/stress_hnsw_mt.cpp
blob: 1feb968fbb48b5454e3a4239c4f0bc59219a0ba3 (plain) (blame)
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
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <fcntl.h>
#include <cstdio>
#include <unistd.h>
#include <chrono>
#include <cstdlib>
#include <future>
#include <vector>

#include <vespa/eval/eval/typed_cells.h>
#include <vespa/eval/eval/value_type.h>
#include <vespa/searchlib/common/bitvector.h>
#include <vespa/searchlib/tensor/distance_functions.h>
#include <vespa/searchlib/tensor/doc_vector_access.h>
#include <vespa/searchlib/tensor/hnsw_index.h>
#include <vespa/searchlib/tensor/inv_log_level_generator.h>
#include <vespa/searchlib/tensor/random_level_generator.h>
#include <vespa/searchlib/tensor/vector_bundle.h>
#include <vespa/vespalib/data/input.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/blockingthreadstackexecutor.h>
#include <vespa/vespalib/util/generationhandler.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/data/simple_buffer.h>

#include <vespa/log/log.h>
LOG_SETUP("stress_hnsw_mt");

using namespace search::tensor;
using namespace vespalib::slime;
using search::BitVector;
using vespalib::eval::CellType;
using vespalib::eval::ValueType;
using vespalib::GenerationHandler;
using vespalib::MemoryUsage;
using vespalib::Slime;

#define NUM_DIMS 128
#define NUM_POSSIBLE_V 1000000
#define NUM_POSSIBLE_DOCS 30000
#define NUM_OPS 1000000

namespace {

SubspaceType subspace_type(ValueType::make_type(CellType::FLOAT, {{"dims", NUM_DIMS }}));

}

class RndGen {
private:
    std::mt19937_64 urng;
    std::uniform_real_distribution<double> uf;
public:
    RndGen() : urng(0x1234deadbeef5678uLL), uf(0.0, 1.0) {}

    double nextUniform() {
        return uf(urng);
    }
};

using ConstVectorRef = vespalib::ConstArrayRef<float>;

struct MallocPointVector {
    float v[NUM_DIMS];
    operator ConstVectorRef() const { return ConstVectorRef(v, NUM_DIMS); }
};
static MallocPointVector *aligned_alloc_pv(size_t num) {
    size_t num_bytes = num * sizeof(MallocPointVector);
    double mega_bytes = num_bytes / double(1_Mi);
    fprintf(stderr, "allocate %.2f MB of vectors\n", mega_bytes);
    char *mem = (char *)malloc(num_bytes + 512);
    mem += 512;
    size_t val = (size_t)mem;
    size_t unalign = val % 512;
    mem -= unalign;
    return reinterpret_cast<MallocPointVector *>(mem);
}

void read_vector_file(MallocPointVector *p) {
    std::string data_set = "sift";
    std::string data_dir = ".";
    char *home = getenv("HOME");
    if (home) {
        data_dir = home;
        data_dir += "/" + data_set;
    }
    std::string fn = data_dir + "/" + data_set + "_base.fvecs";
    int fd = open(fn.c_str(), O_RDONLY);
    if (fd < 0) {
        perror(fn.c_str());
        std::_Exit(1);
    }
    int d;
    size_t rv;
    fprintf(stderr, "reading %u vectors from %s\n", NUM_POSSIBLE_V, fn.c_str());
    for (uint32_t i = 0; i < NUM_POSSIBLE_V; ++i) {
        rv = read(fd, &d, 4);
        ASSERT_EQ(rv, 4u);
        ASSERT_EQ(d, NUM_DIMS);
        rv = read(fd, &p[i].v, NUM_DIMS*sizeof(float));
        ASSERT_EQ(rv, sizeof(MallocPointVector));
    }
    close(fd);
    fprintf(stderr, "reading %u vectors OK\n", NUM_POSSIBLE_V);
}

class MyDocVectorStore : public DocVectorAccess {
private:
    MallocPointVector *_vectors;
public:
    MyDocVectorStore() {
        _vectors = aligned_alloc_pv(NUM_POSSIBLE_DOCS);
    }
    MyDocVectorStore& set(uint32_t docid, ConstVectorRef vec) {
        assert(docid < NUM_POSSIBLE_DOCS);
        memcpy(&_vectors[docid], vec.cbegin(), sizeof(MallocPointVector));
        return *this;
    }
    vespalib::eval::TypedCells get_vector(uint32_t docid, uint32_t subspace) const override {
        assert(docid < NUM_POSSIBLE_DOCS);
        (void) subspace;
        ConstVectorRef ref(_vectors[docid]);
        return vespalib::eval::TypedCells(ref);
    }
    VectorBundle get_vectors(uint32_t docid) const override {
        assert(docid < NUM_POSSIBLE_DOCS);
        ConstVectorRef ref(_vectors[docid]);
        assert(subspace_type.size() == ref.size());
        return VectorBundle(ref.data(), 1, subspace_type);
    }
};

template <typename IndexType>
class Stressor : public ::testing::Test {
private:
    struct LoadedVectors {
        MallocPointVector *pv_storage;
        void load() {
            pv_storage = aligned_alloc_pv(size());
            read_vector_file(pv_storage);
        }
        size_t size() const { return NUM_POSSIBLE_V; }
        vespalib::ConstArrayRef<float> operator[] (size_t i) {
            return pv_storage[i];
        }
    } loaded_vectors;
public:
    BitVector::UP in_progress;
    std::mutex in_progress_lock;
    BitVector::UP existing_ids;
    RndGen rng;
    MyDocVectorStore vectors;
    GenerationHandler gen_handler;
    std::unique_ptr<IndexType> index;
    vespalib::BlockingThreadStackExecutor multi_prepare_workers;
    vespalib::BlockingThreadStackExecutor write_thread;

    using PrepUP = std::unique_ptr<PrepareResult>;
    using ReadGuard = GenerationHandler::Guard;
    using PrepareFuture = std::future<PrepUP>;

    // union of data required by tasks
    struct TaskBase : vespalib::Executor::Task {
        Stressor &parent;
        uint32_t docid;
        ConstVectorRef vec;
        PrepareFuture prepare_future;
        ReadGuard read_guard;

        TaskBase(Stressor &p, uint32_t d, ConstVectorRef v, PrepareFuture f, ReadGuard g)
            : parent(p), docid(d), vec(v), prepare_future(std::move(f)), read_guard(g)
        {}
        TaskBase(Stressor &p, uint32_t d, ConstVectorRef v, ReadGuard g) // prepare add
            : TaskBase(p, d, v, PrepareFuture(), g) {}
        TaskBase(Stressor &p, uint32_t d, ConstVectorRef v, PrepareFuture r) // complete add+update
            : TaskBase(p, d, v, std::move(r), ReadGuard()) {}
        TaskBase(Stressor &p, uint32_t d) // complete remove
            : TaskBase(p, d, ConstVectorRef(), PrepareFuture(), ReadGuard()) {}

        ~TaskBase() {}
    };

    struct PrepareAddTask  : TaskBase {
        using TaskBase::TaskBase;
        using TaskBase::docid;
        using TaskBase::parent;
        using TaskBase::read_guard;
        using TaskBase::vec;
        std::promise<PrepUP> result_promise;
        auto get_result_future() {
            return result_promise.get_future();
        }
        void run() override {
            assert(subspace_type.size() == vec.size());
            VectorBundle v(vec.data(), 1, subspace_type);
            auto up = parent.index->prepare_add_document(docid, v, read_guard);
            result_promise.set_value(std::move(up));
        }
    };

    struct CompleteAddTask : TaskBase {
        using TaskBase::TaskBase;
        using TaskBase::docid;
        using TaskBase::parent;
        using TaskBase::prepare_future;
        using TaskBase::vec;
        void run() override {
            auto prepare_result = prepare_future.get();
            parent.vectors.set(docid, vec);
            parent.index->complete_add_document(docid, std::move(prepare_result));
            parent.existing_ids->setBit(docid);
            parent.commit(docid);
        }
    };

    struct CompleteRemoveTask : TaskBase {
        using TaskBase::TaskBase;
        using TaskBase::docid;
        using TaskBase::parent;
        void run() override {
            parent.index->remove_document(docid);
            parent.existing_ids->clearBit(docid);
            parent.commit(docid);
        }
    };

    struct CompleteUpdateTask : TaskBase {
        using TaskBase::TaskBase;
        using TaskBase::docid;
        using TaskBase::parent;
        using TaskBase::prepare_future;
        using TaskBase::vec;
        void run() override {
            auto prepare_result = prepare_future.get();
            parent.index->remove_document(docid);
            parent.vectors.set(docid, vec);
            parent.index->complete_add_document(docid, std::move(prepare_result));
            EXPECT_EQ(parent.existing_ids->testBit(docid), true);
            parent.commit(docid);
        }
    };

    Stressor()
        : loaded_vectors(),
          in_progress(BitVector::create(NUM_POSSIBLE_DOCS)),
          existing_ids(BitVector::create(NUM_POSSIBLE_DOCS)),
          rng(),
          vectors(),
          gen_handler(),
          index(),
          multi_prepare_workers(10, 50),
          write_thread(1, 500)
    {
        loaded_vectors.load();
    }

    ~Stressor() {}

    auto dff() {
        return search::tensor::make_distance_function_factory(
                search::attribute::DistanceMetric::Euclidean,
                vespalib::eval::CellType::FLOAT);
    }

    void init() {
        uint32_t m = 16;
        index = std::make_unique<IndexType>(vectors, dff(),
                                            std::make_unique<InvLogLevelGenerator>(m),
                                            HnswIndexConfig(2*m, m, 200, 10, true));
    }
    size_t get_rnd(size_t size) {
        return rng.nextUniform() * size;
    }
    void add_document(uint32_t docid) {
        size_t vec_num = get_rnd(loaded_vectors.size());
        ConstVectorRef vec = loaded_vectors[vec_num];
        auto guard = take_read_guard();
        auto prepare_task = std::make_unique<PrepareAddTask>(*this, docid, vec, guard);
        auto complete_task = std::make_unique<CompleteAddTask>(*this, docid, vec, prepare_task->get_result_future());
        auto r = multi_prepare_workers.execute(std::move(prepare_task));
        ASSERT_EQ(r.get(), nullptr);
        r = write_thread.execute(std::move(complete_task));
        ASSERT_EQ(r.get(), nullptr);
    }
    void remove_document(uint32_t docid) {
        auto task = std::make_unique<CompleteRemoveTask>(*this, docid);
        auto r = write_thread.execute(std::move(task));
        ASSERT_EQ(r.get(), nullptr);
    }
    void update_document(uint32_t docid) {
        size_t vec_num = get_rnd(loaded_vectors.size());
        ConstVectorRef vec = loaded_vectors[vec_num];
        auto guard = take_read_guard();
        auto prepare_task = std::make_unique<PrepareAddTask>(*this, docid, vec, guard);
        auto complete_task = std::make_unique<CompleteUpdateTask>(*this, docid, vec, prepare_task->get_result_future());
        auto r = multi_prepare_workers.execute(std::move(prepare_task));
        ASSERT_EQ(r.get(), nullptr);
        r = write_thread.execute(std::move(complete_task));
        ASSERT_EQ(r.get(), nullptr);
    }
    void commit(uint32_t docid) {
        index->assign_generation(gen_handler.getCurrentGeneration());
        gen_handler.incGeneration();
        index->reclaim_memory(gen_handler.get_oldest_used_generation());
        std::lock_guard<std::mutex> guard(in_progress_lock);
        in_progress->clearBit(docid);
        // printf("commit: %u\n", docid);
    }
    void gen_operation() {
        uint32_t docid = get_rnd(NUM_POSSIBLE_DOCS);
        {
            std::lock_guard<std::mutex> guard(in_progress_lock);
            while (in_progress->testBit(docid)) {
                docid = get_rnd(NUM_POSSIBLE_DOCS);
            }
            in_progress->setBit(docid);
        }
        if (existing_ids->testBit(docid)) {
            if (get_rnd(100) < 70) {
                // printf("start remove op: %u\n", docid);
                remove_document(docid);
            } else {
                // printf("start update op: %u\n", docid);
                update_document(docid);
            }
        } else {
            // printf("start add op: %u\n", docid);
            add_document(docid);
        }
    }
    GenerationHandler::Guard take_read_guard() {
        return gen_handler.takeGuard();
    }
    MemoryUsage memory_usage() const {
        return index->memory_usage();
    }
    uint32_t count_in_progress() {
        std::lock_guard<std::mutex> guard(in_progress_lock);
        in_progress->invalidateCachedCount();
        return in_progress->countTrueBits();
    }
    std::string json_state() {
        Slime actualSlime;
        SlimeInserter inserter(actualSlime);
        index->get_state(inserter);
        vespalib::SimpleBuffer buf;
        vespalib::slime::JsonFormat::encode(actualSlime, buf, false);
        return buf.get().make_string();
    }
};

using StressorTypes = ::testing::Types<HnswIndex<HnswIndexType::SINGLE>>;

TYPED_TEST_SUITE(Stressor, StressorTypes);

TYPED_TEST(Stressor, stress)
{
    this->init();
    for (int i = 0; i < NUM_OPS; ++i) {
        this->gen_operation();
        if (i % 1000 == 0) {
            uint32_t cnt = this->count_in_progress();
            fprintf(stderr, "generating operations %d / %d; in progress: %u ops\n",
                    i, NUM_OPS, cnt);
            auto r = this->write_thread.execute(vespalib::makeLambdaTask([&]() {
                        EXPECT_TRUE(this->index->check_link_symmetry());
            }));
            EXPECT_EQ(r.get(), nullptr);
        }
    }
    fprintf(stderr, "waiting for queued operations...\n");
    this->multi_prepare_workers.sync();
    this->write_thread.sync();
    EXPECT_EQ(this->count_in_progress(), 0);
    EXPECT_TRUE(this->index->check_link_symmetry());
    fprintf(stderr, "HNSW index state after test:\n%s\n", this->json_state().c_str());
    this->existing_ids->invalidateCachedCount();
    fprintf(stderr, "Expected valid nodes: %u\n", this->existing_ids->countTrueBits());
    fprintf(stderr, "all done.\n");
}

GTEST_MAIN_RUN_ALL_TESTS()