GrpcRequestScheduler.h 2.1 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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.

K
kun yu 已提交
18 19
#pragma once

S
starlord 已提交
20 21
#include "grpc/gen-status/status.grpc.pb.h"
#include "grpc/gen-status/status.pb.h"
G
groot 已提交
22
#include "server/grpc_impl/request/GrpcBaseRequest.h"
S
starlord 已提交
23 24
#include "utils/BlockingQueue.h"
#include "utils/Status.h"
K
kun yu 已提交
25 26

#include <map>
S
starlord 已提交
27 28
#include <memory>
#include <string>
S
starlord 已提交
29 30
#include <thread>
#include <vector>
K
kun yu 已提交
31 32 33

namespace milvus {
namespace server {
Y
Yu Kun 已提交
34
namespace grpc {
K
kun yu 已提交
35

G
groot 已提交
36 37
using RequestQueue = BlockingQueue<BaseRequestPtr>;
using RequestQueuePtr = std::shared_ptr<RequestQueue>;
K
kun yu 已提交
38 39
using ThreadPtr = std::shared_ptr<std::thread>;

Y
Yu Kun 已提交
40
class GrpcRequestScheduler {
S
starlord 已提交
41
 public:
S
starlord 已提交
42 43
    static GrpcRequestScheduler&
    GetInstance() {
Y
Yu Kun 已提交
44
        static GrpcRequestScheduler scheduler;
K
kun yu 已提交
45 46 47
        return scheduler;
    }

S
starlord 已提交
48 49
    void
    Start();
Y
Yu Kun 已提交
50

S
starlord 已提交
51 52
    void
    Stop();
K
kun yu 已提交
53

S
starlord 已提交
54
    Status
G
groot 已提交
55
    ExecuteRequest(const BaseRequestPtr& request_ptr);
K
kun yu 已提交
56

S
starlord 已提交
57
    static void
G
groot 已提交
58
    ExecRequest(BaseRequestPtr& request_ptr, ::milvus::grpc::Status* grpc_status);
K
kun yu 已提交
59

S
starlord 已提交
60
 protected:
Y
Yu Kun 已提交
61
    GrpcRequestScheduler();
Y
Yu Kun 已提交
62

Y
Yu Kun 已提交
63
    virtual ~GrpcRequestScheduler();
K
kun yu 已提交
64

S
starlord 已提交
65
    void
G
groot 已提交
66
    TakeToExecute(RequestQueuePtr request_queue);
S
starlord 已提交
67

S
starlord 已提交
68
    Status
G
groot 已提交
69
    PutToQueue(const BaseRequestPtr& request_ptr);
K
kun yu 已提交
70

S
starlord 已提交
71
 private:
K
kun yu 已提交
72 73
    mutable std::mutex queue_mtx_;

G
groot 已提交
74
    std::map<std::string, RequestQueuePtr> request_groups_;
K
kun yu 已提交
75 76 77 78 79 80

    std::vector<ThreadPtr> execute_threads_;

    bool stopped_;
};

S
starlord 已提交
81 82 83
}  // namespace grpc
}  // namespace server
}  // namespace milvus