EventExecutor.h 2.6 KB
Newer Older
C
Cai Yudong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 <memory>
#include <mutex>
#include <thread>
C
Cai Yudong 已提交
17 18

#include "db/snapshot/Event.h"
C
Cai Yudong 已提交
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
#include "utils/BlockingQueue.h"

namespace milvus {
namespace engine {
namespace snapshot {

using EventPtr = std::shared_ptr<Event>;
using ThreadPtr = std::shared_ptr<std::thread>;
using EventQueue = BlockingQueue<EventPtr>;

class EventExecutor {
 public:
    EventExecutor() = default;
    EventExecutor(const EventExecutor&) = delete;

    ~EventExecutor() {
        Stop();
    }

    static EventExecutor&
    GetInstance() {
        static EventExecutor inst;
        return inst;
    }

    Status
C
Cai Yudong 已提交
45
    Submit(const EventPtr& evt) {
C
Cai Yudong 已提交
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
        if (evt == nullptr) {
            return Status(SS_INVALID_ARGUMENT_ERROR, "Invalid Resource");
        }
        Enqueue(evt);
        return Status::OK();
    }

    void
    Start() {
        if (thread_ptr_ == nullptr) {
            thread_ptr_ = std::make_shared<std::thread>(&EventExecutor::ThreadMain, this);
        }
    }

    void
    Stop() {
        if (thread_ptr_ != nullptr) {
            Enqueue(nullptr);
            thread_ptr_->join();
            thread_ptr_ = nullptr;
            std::cout << "EventExecutor Stopped" << std::endl;
        }
    }

 private:
    void
    ThreadMain() {
73
        Status status;
C
Cai Yudong 已提交
74 75 76 77 78
        while (true) {
            EventPtr evt = queue_.Take();
            if (evt == nullptr) {
                break;
            }
79
            /* std::cout << std::this_thread::get_id() << " Dequeue Event " << std::endl; */
80 81 82 83
            status = evt->Process();
            if (!status.ok()) {
                std::cout << "EventExecutor Handle Event Error: " << status.ToString() << std::endl;
            }
C
Cai Yudong 已提交
84 85 86 87
        }
    }

    void
C
Cai Yudong 已提交
88
    Enqueue(const EventPtr& evt) {
C
Cai Yudong 已提交
89 90
        queue_.Put(evt);
        if (evt != nullptr) {
91
            /* std::cout << std::this_thread::get_id() << " Enqueue Event " << std::endl; */
C
Cai Yudong 已提交
92 93 94 95 96 97 98 99 100 101 102
        }
    }

 private:
    ThreadPtr thread_ptr_ = nullptr;
    EventQueue queue_;
};

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