Resources.h 17.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// 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.

#pragma once

#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
18
#include <set>
19 20
#include <string>
#include <thread>
J
Jin Hai 已提交
21
#include <utility>
22 23 24 25 26
#include <vector>
#include "db/Utils.h"
#include "db/snapshot/BaseResource.h"
#include "db/snapshot/ResourceTypes.h"
#include "db/snapshot/ScopedResource.h"
27
#include "utils/Json.h"
28 29 30

using milvus::engine::utils::GetMicroSecTimeStamp;

J
Jin Hai 已提交
31
namespace milvus::engine::snapshot {
32

33
static const json JEmpty = {};
34

35 36
class MappingsField {
 public:
J
Jin Hai 已提交
37
    explicit MappingsField(MappingT mappings = {}) : mappings_(std::move(mappings)) {
38
    }
J
Jin Hai 已提交
39

40 41 42 43 44 45 46 47 48 49 50 51 52
    const MappingT&
    GetMappings() const {
        return mappings_;
    }
    MappingT&
    GetMappings() {
        return mappings_;
    }

 protected:
    MappingT mappings_;
};

C
Cai Yudong 已提交
53
class StateField {
54
 public:
C
Cai Yudong 已提交
55
    explicit StateField(State state = PENDING) : state_(state) {
56
    }
J
Jin Hai 已提交
57

58
    State
C
Cai Yudong 已提交
59 60
    GetState() const {
        return state_;
61 62
    }

C
Cai Yudong 已提交
63
    bool
64
    IsActive() const {
C
Cai Yudong 已提交
65
        return state_ == ACTIVE;
66
    }
J
Jin Hai 已提交
67

68 69
    bool
    IsDeactive() const {
C
Cai Yudong 已提交
70
        return state_ == DEACTIVE;
71 72 73 74 75 76
    }

    bool
    Activate() {
        if (IsDeactive())
            return false;
C
Cai Yudong 已提交
77
        state_ = ACTIVE;
78 79 80 81 82
        return true;
    }

    void
    Deactivate() {
C
Cai Yudong 已提交
83
        state_ = DEACTIVE;
84 85 86 87
    }

    void
    ResetStatus() {
C
Cai Yudong 已提交
88
        state_ = PENDING;
89 90 91
    }

 protected:
C
Cai Yudong 已提交
92
    State state_;
93 94
};

X
XuPeng-SH 已提交
95 96 97 98
class LsnField {
 public:
    explicit LsnField(LSN_TYPE lsn = 0) : lsn_(lsn) {
    }
J
Jin Hai 已提交
99

X
XuPeng-SH 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    const LSN_TYPE&
    GetLsn() const {
        return lsn_;
    }

    void
    SetLsn(const LSN_TYPE& lsn) {
        lsn_ = lsn;
    }

 protected:
    LSN_TYPE lsn_;
};

114 115 116 117
class CreatedOnField {
 public:
    explicit CreatedOnField(TS_TYPE created_on = GetMicroSecTimeStamp()) : created_on_(created_on) {
    }
J
Jin Hai 已提交
118

119 120 121 122 123 124 125 126 127
    TS_TYPE
    GetCreatedTime() const {
        return created_on_;
    }

 protected:
    TS_TYPE created_on_;
};

X
XuPeng-SH 已提交
128 129 130 131
class UpdatedOnField {
 public:
    explicit UpdatedOnField(TS_TYPE updated_on = GetMicroSecTimeStamp()) : updated_on_(updated_on) {
    }
J
Jin Hai 已提交
132

X
XuPeng-SH 已提交
133 134 135 136 137 138 139 140 141
    TS_TYPE
    GetUpdatedTime() const {
        return updated_on_;
    }

 protected:
    TS_TYPE updated_on_;
};

142 143 144 145
class IdField {
 public:
    explicit IdField(ID_TYPE id) : id_(id) {
    }
J
Jin Hai 已提交
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    ID_TYPE
    GetID() const {
        return id_;
    }
    void
    SetID(ID_TYPE id) {
        id_ = id;
    }
    bool
    HasAssigned() {
        return id_ > 0;
    }

 protected:
    ID_TYPE id_;
};

class CollectionIdField {
 public:
    explicit CollectionIdField(ID_TYPE id) : collection_id_(id) {
    }
J
Jin Hai 已提交
168

169 170 171 172 173 174 175 176 177 178 179 180 181
    ID_TYPE
    GetCollectionId() const {
        return collection_id_;
    }

 protected:
    ID_TYPE collection_id_;
};

class SchemaIdField {
 public:
    explicit SchemaIdField(ID_TYPE id) : schema_id_(id) {
    }
J
Jin Hai 已提交
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    ID_TYPE
    GetSchemaId() const {
        return schema_id_;
    }
    void
    SetSchemaId(ID_TYPE schema_id) {
        schema_id_ = schema_id;
    }

 protected:
    ID_TYPE schema_id_;
};

class NumField {
 public:
    explicit NumField(NUM_TYPE num) : num_(num) {
    }
J
Jin Hai 已提交
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    NUM_TYPE
    GetNum() const {
        return num_;
    }
    void
    SetNum(NUM_TYPE num) {
        num_ = num;
    }

 protected:
    NUM_TYPE num_;
};

class FtypeField {
 public:
    explicit FtypeField(FTYPE_TYPE type) : ftype_(type) {
    }
J
Jin Hai 已提交
218

219 220 221 222 223 224 225 226 227 228 229 230 231
    FTYPE_TYPE
    GetFtype() const {
        return ftype_;
    }

 protected:
    FTYPE_TYPE ftype_;
};

class FieldIdField {
 public:
    explicit FieldIdField(ID_TYPE id) : field_id_(id) {
    }
J
Jin Hai 已提交
232

233 234 235 236 237 238 239 240 241 242 243 244 245
    ID_TYPE
    GetFieldId() const {
        return field_id_;
    }

 protected:
    ID_TYPE field_id_;
};

class FieldElementIdField {
 public:
    explicit FieldElementIdField(ID_TYPE id) : field_element_id_(id) {
    }
J
Jin Hai 已提交
246

247 248 249 250 251 252 253 254 255 256 257 258 259
    ID_TYPE
    GetFieldElementId() const {
        return field_element_id_;
    }

 protected:
    ID_TYPE field_element_id_;
};

class PartitionIdField {
 public:
    explicit PartitionIdField(ID_TYPE id) : partition_id_(id) {
    }
J
Jin Hai 已提交
260

261 262 263 264 265 266 267 268 269 270 271 272 273
    ID_TYPE
    GetPartitionId() const {
        return partition_id_;
    }

 protected:
    ID_TYPE partition_id_;
};

class SegmentIdField {
 public:
    explicit SegmentIdField(ID_TYPE id) : segment_id_(id) {
    }
J
Jin Hai 已提交
274

275 276 277 278 279 280 281 282 283 284 285
    ID_TYPE
    GetSegmentId() const {
        return segment_id_;
    }

 protected:
    ID_TYPE segment_id_;
};

class NameField {
 public:
J
Jin Hai 已提交
286
    explicit NameField(std::string name) : name_(std::move(name)) {
287
    }
J
Jin Hai 已提交
288

289 290 291 292 293 294 295 296 297
    const std::string&
    GetName() const {
        return name_;
    }

 protected:
    std::string name_;
};

298 299
class ParamsField {
 public:
300
    explicit ParamsField(const json& params) : params_(params) {
301 302
    }

303
    const json&
304 305 306 307 308
    GetParams() const {
        return params_;
    }

 protected:
309
    json params_;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
};

class SizeField {
 public:
    explicit SizeField(SIZE_TYPE size) : size_(size) {
    }

    SIZE_TYPE
    GetSize() const {
        return size_;
    }

 protected:
    SIZE_TYPE size_;
};

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
class RowCountField {
 public:
    explicit RowCountField(SIZE_TYPE size) : size_(size) {
    }

    SIZE_TYPE
    GetRowCount() const {
        return size_;
    }

    void
    SetRowCount(SIZE_TYPE row_cnt) {
        size_ = row_cnt;
    }

 protected:
    SIZE_TYPE size_;
};

C
Cai Yudong 已提交
345 346
///////////////////////////////////////////////////////////////////////////////

C
Cai Yudong 已提交
347
class Collection : public BaseResource,
348
                   public NameField,
349
                   public ParamsField,
350
                   public IdField,
X
XuPeng-SH 已提交
351
                   public LsnField,
C
Cai Yudong 已提交
352
                   public StateField,
X
XuPeng-SH 已提交
353 354
                   public CreatedOnField,
                   public UpdatedOnField {
355 356 357
 public:
    using Ptr = std::shared_ptr<Collection>;
    using MapT = std::map<ID_TYPE, Ptr>;
358
    using SetT = std::set<Ptr>;
X
XuPeng-SH 已提交
359
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<Collection>>;
360 361 362
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "Collection";

363
    explicit Collection(const std::string& name, const json& params = JEmpty, ID_TYPE id = 0, LSN_TYPE lsn = 0,
364 365
                        State status = PENDING, TS_TYPE created_on = GetMicroSecTimeStamp(),
                        TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
366 367 368 369
};

using CollectionPtr = Collection::Ptr;

C
Cai Yudong 已提交
370
class CollectionCommit : public BaseResource,
371 372 373
                         public CollectionIdField,
                         public SchemaIdField,
                         public MappingsField,
374
                         public RowCountField,
375
                         public SizeField,
376
                         public IdField,
X
XuPeng-SH 已提交
377
                         public LsnField,
C
Cai Yudong 已提交
378
                         public StateField,
X
XuPeng-SH 已提交
379 380
                         public CreatedOnField,
                         public UpdatedOnField {
381 382 383 384
 public:
    static constexpr const char* Name = "CollectionCommit";
    using Ptr = std::shared_ptr<CollectionCommit>;
    using MapT = std::map<ID_TYPE, Ptr>;
385
    using SetT = std::set<Ptr>;
X
XuPeng-SH 已提交
386
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<CollectionCommit>>;
387
    using VecT = std::vector<Ptr>;
388 389
    CollectionCommit(ID_TYPE collection_id, ID_TYPE schema_id, const MappingT& mappings = {}, SIZE_TYPE row_cnt = 0,
                     SIZE_TYPE size = 0, ID_TYPE id = 0, LSN_TYPE lsn = 0, State status = PENDING,
390
                     TS_TYPE created_on = GetMicroSecTimeStamp(), TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
391 392 393 394
};

using CollectionCommitPtr = CollectionCommit::Ptr;

C
Cai Yudong 已提交
395 396
///////////////////////////////////////////////////////////////////////////////

C
Cai Yudong 已提交
397
class Partition : public BaseResource,
398 399 400
                  public NameField,
                  public CollectionIdField,
                  public IdField,
X
XuPeng-SH 已提交
401
                  public LsnField,
C
Cai Yudong 已提交
402
                  public StateField,
X
XuPeng-SH 已提交
403 404
                  public CreatedOnField,
                  public UpdatedOnField {
405 406 407
 public:
    using Ptr = std::shared_ptr<Partition>;
    using MapT = std::map<ID_TYPE, Ptr>;
408
    using SetT = std::set<Ptr>;
X
XuPeng-SH 已提交
409
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<Partition>>;
410 411 412
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "Partition";

X
XuPeng-SH 已提交
413
    Partition(const std::string& name, ID_TYPE collection_id, ID_TYPE id = 0, LSN_TYPE lsn = 0, State status = PENDING,
X
XuPeng-SH 已提交
414
              TS_TYPE created_on = GetMicroSecTimeStamp(), TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
415 416 417 418
};

using PartitionPtr = Partition::Ptr;

C
Cai Yudong 已提交
419
class PartitionCommit : public BaseResource,
420 421 422
                        public CollectionIdField,
                        public PartitionIdField,
                        public MappingsField,
423
                        public RowCountField,
424
                        public SizeField,
425
                        public IdField,
X
XuPeng-SH 已提交
426
                        public LsnField,
C
Cai Yudong 已提交
427
                        public StateField,
X
XuPeng-SH 已提交
428 429
                        public CreatedOnField,
                        public UpdatedOnField {
430 431 432
 public:
    using Ptr = std::shared_ptr<PartitionCommit>;
    using MapT = std::map<ID_TYPE, Ptr>;
433
    using SetT = std::set<Ptr>;
X
XuPeng-SH 已提交
434
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<PartitionCommit>>;
435 436 437
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "PartitionCommit";

438 439
    PartitionCommit(ID_TYPE collection_id, ID_TYPE partition_id, const MappingT& mappings = {}, SIZE_TYPE row_cnt = 0,
                    SIZE_TYPE size = 0, ID_TYPE id = 0, LSN_TYPE lsn = 0, State status = PENDING,
440
                    TS_TYPE created_on = GetMicroSecTimeStamp(), TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
441

C
Cai Yudong 已提交
442
    std::string
443 444 445 446 447
    ToString() const override;
};

using PartitionCommitPtr = PartitionCommit::Ptr;

C
Cai Yudong 已提交
448 449
///////////////////////////////////////////////////////////////////////////////

C
Cai Yudong 已提交
450
class Segment : public BaseResource,
C
Cai Yudong 已提交
451
                public CollectionIdField,
452 453 454
                public PartitionIdField,
                public NumField,
                public IdField,
X
XuPeng-SH 已提交
455
                public LsnField,
C
Cai Yudong 已提交
456
                public StateField,
X
XuPeng-SH 已提交
457 458
                public CreatedOnField,
                public UpdatedOnField {
459 460 461
 public:
    using Ptr = std::shared_ptr<Segment>;
    using MapT = std::map<ID_TYPE, Ptr>;
462
    using SetT = std::set<Ptr>;
X
XuPeng-SH 已提交
463
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<Segment>>;
464 465 466
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "Segment";

C
Cai Yudong 已提交
467 468 469
    explicit Segment(ID_TYPE collection_id, ID_TYPE partition_id, ID_TYPE num = 0, ID_TYPE id = 0, LSN_TYPE lsn = 0,
                     State status = PENDING, TS_TYPE created_on = GetMicroSecTimeStamp(),
                     TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
470

C
Cai Yudong 已提交
471
    std::string
472 473 474 475 476
    ToString() const override;
};

using SegmentPtr = Segment::Ptr;

C
Cai Yudong 已提交
477
class SegmentCommit : public BaseResource,
478 479 480 481
                      public SchemaIdField,
                      public PartitionIdField,
                      public SegmentIdField,
                      public MappingsField,
482
                      public RowCountField,
483
                      public SizeField,
484
                      public IdField,
X
XuPeng-SH 已提交
485
                      public LsnField,
C
Cai Yudong 已提交
486
                      public StateField,
X
XuPeng-SH 已提交
487 488
                      public CreatedOnField,
                      public UpdatedOnField {
489 490 491
 public:
    using Ptr = std::shared_ptr<SegmentCommit>;
    using MapT = std::map<ID_TYPE, Ptr>;
492
    using SetT = std::set<Ptr>;
X
XuPeng-SH 已提交
493
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<SegmentCommit>>;
494 495 496 497
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "SegmentCommit";

    SegmentCommit(ID_TYPE schema_id, ID_TYPE partition_id, ID_TYPE segment_id, const MappingT& mappings = {},
498
                  SIZE_TYPE row_cnt = 0, SIZE_TYPE size = 0, ID_TYPE id = 0, LSN_TYPE lsn = 0, State status = PENDING,
499
                  TS_TYPE created_on = GetMicroSecTimeStamp(), TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
500

C
Cai Yudong 已提交
501
    std::string
502 503 504 505 506
    ToString() const override;
};

using SegmentCommitPtr = SegmentCommit::Ptr;

C
Cai Yudong 已提交
507 508
///////////////////////////////////////////////////////////////////////////////

C
Cai Yudong 已提交
509
class SegmentFile : public BaseResource,
C
Cai Yudong 已提交
510
                    public CollectionIdField,
511 512 513
                    public PartitionIdField,
                    public SegmentIdField,
                    public FieldElementIdField,
514
                    public RowCountField,
515
                    public SizeField,
516
                    public IdField,
X
XuPeng-SH 已提交
517
                    public LsnField,
C
Cai Yudong 已提交
518
                    public StateField,
X
XuPeng-SH 已提交
519 520
                    public CreatedOnField,
                    public UpdatedOnField {
521 522 523
 public:
    using Ptr = std::shared_ptr<SegmentFile>;
    using MapT = std::map<ID_TYPE, Ptr>;
524
    using SetT = std::set<Ptr>;
X
XuPeng-SH 已提交
525
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<SegmentFile>>;
526 527 528
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "SegmentFile";

C
Cai Yudong 已提交
529
    SegmentFile(ID_TYPE collection_id, ID_TYPE partition_id, ID_TYPE segment_id, ID_TYPE field_element_id,
530
                SIZE_TYPE row_cnt = 0, SIZE_TYPE size = 0, ID_TYPE id = 0, LSN_TYPE lsn = 0, State status = PENDING,
531
                TS_TYPE created_on = GetMicroSecTimeStamp(), TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
532 533 534 535
};

using SegmentFilePtr = SegmentFile::Ptr;

C
Cai Yudong 已提交
536 537 538 539 540 541 542
///////////////////////////////////////////////////////////////////////////////

class SchemaCommit : public BaseResource,
                     public CollectionIdField,
                     public MappingsField,
                     public IdField,
                     public LsnField,
C
Cai Yudong 已提交
543
                     public StateField,
C
Cai Yudong 已提交
544 545 546 547 548
                     public CreatedOnField,
                     public UpdatedOnField {
 public:
    using Ptr = std::shared_ptr<SchemaCommit>;
    using MapT = std::map<ID_TYPE, Ptr>;
549
    using SetT = std::set<Ptr>;
C
Cai Yudong 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<SchemaCommit>>;
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "SchemaCommit";

    explicit SchemaCommit(ID_TYPE collection_id, const MappingT& mappings = {}, ID_TYPE id = 0, LSN_TYPE lsn = 0,
                          State status = PENDING, TS_TYPE created_on = GetMicroSecTimeStamp(),
                          TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
};

using SchemaCommitPtr = SchemaCommit::Ptr;

///////////////////////////////////////////////////////////////////////////////

class Field : public BaseResource,
              public NameField,
              public NumField,
566 567
              public FtypeField,
              public ParamsField,
C
Cai Yudong 已提交
568 569
              public IdField,
              public LsnField,
C
Cai Yudong 已提交
570
              public StateField,
C
Cai Yudong 已提交
571 572 573 574 575
              public CreatedOnField,
              public UpdatedOnField {
 public:
    using Ptr = std::shared_ptr<Field>;
    using MapT = std::map<ID_TYPE, Ptr>;
576
    using SetT = std::set<Ptr>;
C
Cai Yudong 已提交
577 578 579 580
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<Field>>;
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "Field";

581
    Field(const std::string& name, NUM_TYPE num, FTYPE_TYPE ftype, const json& params = JEmpty, ID_TYPE id = 0,
582 583
          LSN_TYPE lsn = 0, State status = PENDING, TS_TYPE created_on = GetMicroSecTimeStamp(),
          TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
C
Cai Yudong 已提交
584 585 586 587 588 589 590 591 592 593
};

using FieldPtr = Field::Ptr;

class FieldCommit : public BaseResource,
                    public CollectionIdField,
                    public FieldIdField,
                    public MappingsField,
                    public IdField,
                    public LsnField,
C
Cai Yudong 已提交
594
                    public StateField,
C
Cai Yudong 已提交
595 596 597 598 599
                    public CreatedOnField,
                    public UpdatedOnField {
 public:
    using Ptr = std::shared_ptr<FieldCommit>;
    using MapT = std::map<ID_TYPE, Ptr>;
600
    using SetT = std::set<Ptr>;
C
Cai Yudong 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<FieldCommit>>;
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "FieldCommit";

    FieldCommit(ID_TYPE collection_id, ID_TYPE field_id, const MappingT& mappings = {}, ID_TYPE id = 0,
                LSN_TYPE lsn = 0, State status = PENDING, TS_TYPE created_on = GetMicroSecTimeStamp(),
                TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
};

using FieldCommitPtr = FieldCommit::Ptr;

///////////////////////////////////////////////////////////////////////////////

class FieldElement : public BaseResource,
                     public CollectionIdField,
                     public FieldIdField,
                     public NameField,
                     public FtypeField,
619
                     public ParamsField,
C
Cai Yudong 已提交
620 621
                     public IdField,
                     public LsnField,
C
Cai Yudong 已提交
622
                     public StateField,
C
Cai Yudong 已提交
623 624 625 626 627
                     public CreatedOnField,
                     public UpdatedOnField {
 public:
    using Ptr = std::shared_ptr<FieldElement>;
    using MapT = std::map<ID_TYPE, Ptr>;
628
    using SetT = std::set<Ptr>;
C
Cai Yudong 已提交
629 630 631
    using ScopedMapT = std::map<ID_TYPE, ScopedResource<FieldElement>>;
    using VecT = std::vector<Ptr>;
    static constexpr const char* Name = "FieldElement";
632
    FieldElement(ID_TYPE collection_id, ID_TYPE field_id, const std::string& name, FTYPE_TYPE ftype,
633
                 const json& params = JEmpty, ID_TYPE id = 0, LSN_TYPE lsn = 0, State status = PENDING,
634
                 TS_TYPE created_on = GetMicroSecTimeStamp(), TS_TYPE UpdatedOnField = GetMicroSecTimeStamp());
C
Cai Yudong 已提交
635 636 637 638
};

using FieldElementPtr = FieldElement::Ptr;

J
Jin Hai 已提交
639
}  // namespace milvus::engine::snapshot