ResourceHolder.h 4.8 KB
Newer Older
C
Cai Yudong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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>
#include <string>
#include <thread>
C
Cai Yudong 已提交
20 21
#include "db/snapshot/Event.h"
#include "db/snapshot/EventExecutor.h"
C
Cai Yudong 已提交
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
#include "db/snapshot/Operations.h"
#include "db/snapshot/ResourceTypes.h"
#include "db/snapshot/ScopedResource.h"
#include "db/snapshot/Store.h"

namespace milvus {
namespace engine {
namespace snapshot {

template <typename ResourceT, typename Derived>
class ResourceHolder {
    using ResourcePtr = std::shared_ptr<ResourceT>;
    using ScopedT = ScopedResource<ResourceT>;
    using ScopedPtr = std::shared_ptr<ScopedT>;
    using IdMapT = std::map<ID_TYPE, ResourcePtr>;
    using Ptr = std::shared_ptr<Derived>;

 protected:
    ResourceHolder() = default;
    virtual ~ResourceHolder() = default;

 public:
    static Derived&
    GetInstance() {
        static Derived holder;
        return holder;
    }

    ScopedT
    GetResource(ID_TYPE id, bool scoped = true) {
        // TODO: Temp to use Load here. Will be removed when resource is loaded just post Compound
        // Operations.
        return Load(Store::GetInstance(), id, scoped);

        {
            std::unique_lock<std::mutex> lock(mutex_);
            auto cit = id_map_.find(id);
            if (cit != id_map_.end()) {
                return ScopedT(cit->second, scoped);
            }
        }
        return ScopedT();
    }

    virtual bool
    Add(ResourcePtr resource) {
        std::unique_lock<std::mutex> lock(mutex_);
        return AddNoLock(resource);
    }

    virtual bool
    Release(ID_TYPE id) {
        std::unique_lock<std::mutex> lock(mutex_);
        return ReleaseNoLock(id);
    }

    // TODO: Resource should be loaded into holder in OperationExecutor thread
    ScopedT
    Load(Store& store, ID_TYPE id, bool scoped = true) {
        {
            std::unique_lock<std::mutex> lock(mutex_);
            auto cit = id_map_.find(id);
            if (cit != id_map_.end()) {
                return ScopedT(cit->second, scoped);
            }
        }
        auto ret = DoLoad(store, id);
        if (!ret) {
            return ScopedT();
        }
        return ScopedT(ret, scoped);
    }

    virtual bool
    HardDelete(ID_TYPE id) {
        auto op = std::make_shared<HardDeleteOperation<ResourceT>>(id);
        // TODO:
        (*op)(Store::GetInstance());
        return true;
    }

    virtual void
    Reset() {
        id_map_.clear();
    }

    virtual void
    Dump(const std::string& tag = "") {
        std::unique_lock<std::mutex> lock(mutex_);
        std::cout << typeid(*this).name() << " Dump Start [" << tag << "]:" << id_map_.size() << std::endl;
        for (auto& kv : id_map_) {
            /* std::cout << "\t" << kv.second->ToString() << std::endl; */
J
Jin Hai 已提交
114
            std::cout << "\t" << kv.first << " RefCnt " << kv.second->ref_count() << std::endl;
C
Cai Yudong 已提交
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
        }
        std::cout << typeid(*this).name() << " Dump   End [" << tag << "]" << std::endl;
    }

 private:
    bool
    AddNoLock(ResourcePtr resource) {
        if (!resource) {
            return false;
        }
        if (id_map_.find(resource->GetID()) != id_map_.end()) {
            return false;
        }
        id_map_[resource->GetID()] = resource;
        resource->RegisterOnNoRefCB(std::bind(&Derived::OnNoRefCallBack, this, resource));
        return true;
    }

    bool
    ReleaseNoLock(ID_TYPE id) {
        auto it = id_map_.find(id);
        if (it == id_map_.end()) {
            return false;
        }
        id_map_.erase(it);
        return true;
    }

    virtual void
    OnNoRefCallBack(ResourcePtr resource) {
C
Cai Yudong 已提交
145 146
        auto evt_ptr = std::make_shared<ResourceGCEvent<ResourceT>>(resource);
        EventExecutor::GetInstance().Submit(evt_ptr);
C
Cai Yudong 已提交
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
        Release(resource->GetID());
    }

    virtual ResourcePtr
    DoLoad(Store& store, ID_TYPE id) {
        LoadOperationContext context;
        context.id = id;
        auto op = std::make_shared<LoadOperation<ResourceT>>(context);
        (*op)(store);
        typename ResourceT::Ptr c;
        auto status = op->GetResource(c);
        if (status.ok()) {
            Add(c);
            return c;
        }
        return nullptr;
    }

 private:
    std::mutex mutex_;
    IdMapT id_map_;
};

}  // namespace snapshot
}  // namespace engine
}  // namespace milvus