SegmentWriter.cpp 9.8 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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 "segment/SegmentWriter.h"

#include <algorithm>
#include <memory>

#include "SegmentReader.h"
#include "Vectors.h"
#include "codecs/default/DefaultCodec.h"
Y
yudong.cai 已提交
26 27 28
#include "storage/disk/DiskIOReader.h"
#include "storage/disk/DiskIOWriter.h"
#include "storage/disk/DiskOperation.h"
29 30 31 32 33 34
#include "utils/Log.h"

namespace milvus {
namespace segment {

SegmentWriter::SegmentWriter(const std::string& directory) {
Y
yudong.cai 已提交
35 36 37 38
    storage::IOReaderPtr reader_ptr = std::make_shared<storage::DiskIOReader>();
    storage::IOWriterPtr writer_ptr = std::make_shared<storage::DiskIOWriter>();
    storage::OperationPtr operation_ptr = std::make_shared<storage::DiskOperation>(directory);
    fs_ptr_ = std::make_shared<storage::FSHandler>(reader_ptr, writer_ptr, operation_ptr);
39 40 41 42 43 44 45 46 47 48 49 50 51
    segment_ptr_ = std::make_shared<Segment>();
}

Status
SegmentWriter::AddVectors(const std::string& name, const std::vector<uint8_t>& data,
                          const std::vector<doc_id_t>& uids) {
    segment_ptr_->vectors_ptr_->AddData(data);
    segment_ptr_->vectors_ptr_->AddUids(uids);
    segment_ptr_->vectors_ptr_->SetName(name);

    return Status::OK();
}

C
Cai Yudong 已提交
52 53 54 55 56 57
Status
SegmentWriter::SetVectorIndex(const milvus::knowhere::VecIndexPtr& index) {
    segment_ptr_->vector_index_ptr_->SetVectorIndex(index);
    return Status::OK();
}

58 59
Status
SegmentWriter::Serialize() {
Z
Zhiru Zhu 已提交
60 61
    auto start = std::chrono::high_resolution_clock::now();

62 63
    auto status = WriteBloomFilter();
    if (!status.ok()) {
64
        ENGINE_LOG_ERROR << status.message();
65 66 67
        return status;
    }

Z
Zhiru Zhu 已提交
68 69 70 71 72 73
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> diff = end - start;
    ENGINE_LOG_DEBUG << "Writing bloom filter took " << diff.count() << " s in total";

    start = std::chrono::high_resolution_clock::now();

74
    ENGINE_LOG_DEBUG << "Write vectors";
75 76
    status = WriteVectors();
    if (!status.ok()) {
77
        ENGINE_LOG_ERROR << "Write vectors fail: " << status.message();
78 79 80
        return status;
    }

Z
Zhiru Zhu 已提交
81 82 83 84 85 86
    end = std::chrono::high_resolution_clock::now();
    diff = end - start;
    ENGINE_LOG_DEBUG << "Writing vectors and uids took " << diff.count() << " s in total";

    start = std::chrono::high_resolution_clock::now();

87 88
    // Write an empty deleted doc
    status = WriteDeletedDocs();
Z
Zhiru Zhu 已提交
89 90 91 92 93

    end = std::chrono::high_resolution_clock::now();
    diff = end - start;
    ENGINE_LOG_DEBUG << "Writing deleted docs took " << diff.count() << " s";

94 95 96 97 98 99 100
    return status;
}

Status
SegmentWriter::WriteVectors() {
    codec::DefaultCodec default_codec;
    try {
Y
yudong.cai 已提交
101 102
        fs_ptr_->operation_ptr_->CreateDirectory();
        default_codec.GetVectorsFormat()->write(fs_ptr_, segment_ptr_->vectors_ptr_);
Z
Zhiru Zhu 已提交
103 104
    } catch (std::exception& e) {
        std::string err_msg = "Failed to write vectors: " + std::string(e.what());
105
        ENGINE_LOG_ERROR << err_msg;
Z
Zhiru Zhu 已提交
106
        return Status(SERVER_WRITE_ERROR, err_msg);
107 108 109 110
    }
    return Status::OK();
}

C
Cai Yudong 已提交
111
Status
112
SegmentWriter::WriteVectorIndex(const std::string& location) {
C
Cai Yudong 已提交
113 114 115
    codec::DefaultCodec default_codec;
    try {
        fs_ptr_->operation_ptr_->CreateDirectory();
116
        default_codec.GetVectorIndexFormat()->write(fs_ptr_, location, segment_ptr_->vector_index_ptr_);
C
Cai Yudong 已提交
117 118 119 120 121 122 123 124
    } catch (std::exception& e) {
        std::string err_msg = "Failed to write vector index: " + std::string(e.what());
        ENGINE_LOG_ERROR << err_msg;
        return Status(SERVER_WRITE_ERROR, err_msg);
    }
    return Status::OK();
}

125 126 127 128
Status
SegmentWriter::WriteBloomFilter() {
    codec::DefaultCodec default_codec;
    try {
Y
yudong.cai 已提交
129
        fs_ptr_->operation_ptr_->CreateDirectory();
Z
Zhiru Zhu 已提交
130 131 132

        auto start = std::chrono::high_resolution_clock::now();

Y
yudong.cai 已提交
133
        default_codec.GetIdBloomFilterFormat()->create(fs_ptr_, segment_ptr_->id_bloom_filter_ptr_);
Z
Zhiru Zhu 已提交
134 135 136 137 138 139 140

        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> diff = end - start;
        ENGINE_LOG_DEBUG << "Initializing bloom filter took " << diff.count() << " s";

        start = std::chrono::high_resolution_clock::now();

141 142
        auto& uids = segment_ptr_->vectors_ptr_->GetUids();
        for (auto& uid : uids) {
Z
Zhiru Zhu 已提交
143
            segment_ptr_->id_bloom_filter_ptr_->Add(uid);
144
        }
Z
Zhiru Zhu 已提交
145 146 147 148 149 150 151

        end = std::chrono::high_resolution_clock::now();
        diff = end - start;
        ENGINE_LOG_DEBUG << "Adding " << uids.size() << " ids to bloom filter took " << diff.count() << " s";

        start = std::chrono::high_resolution_clock::now();

Y
yudong.cai 已提交
152
        default_codec.GetIdBloomFilterFormat()->write(fs_ptr_, segment_ptr_->id_bloom_filter_ptr_);
Z
Zhiru Zhu 已提交
153 154 155 156

        end = std::chrono::high_resolution_clock::now();
        diff = end - start;
        ENGINE_LOG_DEBUG << "Writing bloom filter took " << diff.count() << " s";
Z
Zhiru Zhu 已提交
157 158
    } catch (std::exception& e) {
        std::string err_msg = "Failed to write vectors: " + std::string(e.what());
159
        ENGINE_LOG_ERROR << err_msg;
Z
Zhiru Zhu 已提交
160
        return Status(SERVER_WRITE_ERROR, err_msg);
161 162 163 164 165 166 167 168
    }
    return Status::OK();
}

Status
SegmentWriter::WriteDeletedDocs() {
    codec::DefaultCodec default_codec;
    try {
Y
yudong.cai 已提交
169
        fs_ptr_->operation_ptr_->CreateDirectory();
170
        DeletedDocsPtr deleted_docs_ptr = std::make_shared<DeletedDocs>();
Y
yudong.cai 已提交
171
        default_codec.GetDeletedDocsFormat()->write(fs_ptr_, deleted_docs_ptr);
Z
Zhiru Zhu 已提交
172 173
    } catch (std::exception& e) {
        std::string err_msg = "Failed to write deleted docs: " + std::string(e.what());
174
        ENGINE_LOG_ERROR << err_msg;
Z
Zhiru Zhu 已提交
175
        return Status(SERVER_WRITE_ERROR, err_msg);
176 177 178 179 180 181 182 183
    }
    return Status::OK();
}

Status
SegmentWriter::WriteDeletedDocs(const DeletedDocsPtr& deleted_docs) {
    codec::DefaultCodec default_codec;
    try {
Y
yudong.cai 已提交
184 185
        fs_ptr_->operation_ptr_->CreateDirectory();
        default_codec.GetDeletedDocsFormat()->write(fs_ptr_, deleted_docs);
Z
Zhiru Zhu 已提交
186 187
    } catch (std::exception& e) {
        std::string err_msg = "Failed to write deleted docs: " + std::string(e.what());
188
        ENGINE_LOG_ERROR << err_msg;
Z
Zhiru Zhu 已提交
189
        return Status(SERVER_WRITE_ERROR, err_msg);
190 191 192 193 194 195 196 197
    }
    return Status::OK();
}

Status
SegmentWriter::WriteBloomFilter(const IdBloomFilterPtr& id_bloom_filter_ptr) {
    codec::DefaultCodec default_codec;
    try {
Y
yudong.cai 已提交
198 199
        fs_ptr_->operation_ptr_->CreateDirectory();
        default_codec.GetIdBloomFilterFormat()->write(fs_ptr_, id_bloom_filter_ptr);
Z
Zhiru Zhu 已提交
200 201
    } catch (std::exception& e) {
        std::string err_msg = "Failed to write bloom filter: " + std::string(e.what());
202
        ENGINE_LOG_ERROR << err_msg;
Z
Zhiru Zhu 已提交
203
        return Status(SERVER_WRITE_ERROR, err_msg);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    }
    return Status::OK();
}

Status
SegmentWriter::Cache() {
    // TODO(zhiru)
    return Status::OK();
}

Status
SegmentWriter::GetSegment(SegmentPtr& segment_ptr) {
    segment_ptr = segment_ptr_;
    return Status::OK();
}

Status
SegmentWriter::Merge(const std::string& dir_to_merge, const std::string& name) {
Y
yudong.cai 已提交
222
    if (dir_to_merge == fs_ptr_->operation_ptr_->GetDirectory()) {
223 224 225
        return Status(DB_ERROR, "Cannot Merge Self");
    }

Y
yudong.cai 已提交
226
    ENGINE_LOG_DEBUG << "Merging from " << dir_to_merge << " to " << fs_ptr_->operation_ptr_->GetDirectory();
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

    auto start = std::chrono::high_resolution_clock::now();

    SegmentReader segment_reader_to_merge(dir_to_merge);
    bool in_cache;
    auto status = segment_reader_to_merge.LoadCache(in_cache);
    if (!in_cache) {
        status = segment_reader_to_merge.Load();
        if (!status.ok()) {
            std::string msg = "Failed to load segment from " + dir_to_merge;
            ENGINE_LOG_ERROR << msg;
            return Status(DB_ERROR, msg);
        }
    }
    SegmentPtr segment_to_merge;
    segment_reader_to_merge.GetSegment(segment_to_merge);
    auto& uids = segment_to_merge->vectors_ptr_->GetUids();

    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> diff = end - start;
    ENGINE_LOG_DEBUG << "Loading segment took " << diff.count() << " s";

    if (segment_to_merge->deleted_docs_ptr_ != nullptr) {
        auto offsets_to_delete = segment_to_merge->deleted_docs_ptr_->GetDeletedDocs();

        // Erase from raw data
        segment_to_merge->vectors_ptr_->Erase(offsets_to_delete);
    }

    start = std::chrono::high_resolution_clock::now();

    AddVectors(name, segment_to_merge->vectors_ptr_->GetData(), segment_to_merge->vectors_ptr_->GetUids());

    end = std::chrono::high_resolution_clock::now();
    diff = end - start;
    ENGINE_LOG_DEBUG << "Adding " << segment_to_merge->vectors_ptr_->GetCount() << " vectors and uids took "
                     << diff.count() << " s";

Y
yudong.cai 已提交
265
    ENGINE_LOG_DEBUG << "Merging completed from " << dir_to_merge << " to " << fs_ptr_->operation_ptr_->GetDirectory();
266 267 268 269 270 271 272

    return Status::OK();
}

size_t
SegmentWriter::Size() {
    // TODO(zhiru): switch to actual directory size
273 274
    size_t vectors_size = segment_ptr_->vectors_ptr_->VectorsSize();
    size_t uids_size = segment_ptr_->vectors_ptr_->UidsSize();
Z
Zhiru Zhu 已提交
275
    /*
276 277 278
    if (segment_ptr_->id_bloom_filter_ptr_) {
        ret += segment_ptr_->id_bloom_filter_ptr_->Size();
    }
Z
Zhiru Zhu 已提交
279
     */
280
    return (vectors_size * sizeof(uint8_t) + uids_size * sizeof(doc_id_t));
281 282 283 284 285 286 287 288 289
}

size_t
SegmentWriter::VectorCount() {
    return segment_ptr_->vectors_ptr_->GetCount();
}

}  // namespace segment
}  // namespace milvus