be/src/load/routine_load/data_consumer_pool.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <stdint.h> |
21 | | |
22 | | #include <list> |
23 | | #include <memory> |
24 | | #include <mutex> |
25 | | |
26 | | #include "util/countdown_latch.h" |
27 | | #include "util/thread.h" |
28 | | |
29 | | namespace doris { |
30 | | |
31 | | class DataConsumer; |
32 | | class DataConsumerGroup; |
33 | | class Status; |
34 | | class StreamLoadContext; |
35 | | |
36 | | // DataConsumerPool saves all available data consumer |
37 | | // to be reused |
38 | | class DataConsumerPool { |
39 | | public: |
40 | 1 | DataConsumerPool() : _stop_background_threads_latch(1) {} |
41 | | |
42 | 1 | ~DataConsumerPool() = default; |
43 | | |
44 | 1 | void stop() { |
45 | 1 | _stop_background_threads_latch.count_down(); |
46 | 1 | if (_clean_idle_consumer_thread) { |
47 | 1 | _clean_idle_consumer_thread->join(); |
48 | 1 | } |
49 | 1 | } |
50 | | |
51 | | // get a already initialized consumer from cache, |
52 | | // if not found in cache, create a new one. |
53 | | Status get_consumer(std::shared_ptr<StreamLoadContext> ctx, std::shared_ptr<DataConsumer>* ret); |
54 | | |
55 | | // get several consumers and put them into group |
56 | | Status get_consumer_grp(std::shared_ptr<StreamLoadContext> ctx, |
57 | | std::shared_ptr<DataConsumerGroup>* ret); |
58 | | |
59 | | // return the consumer to the pool |
60 | | void return_consumer(std::shared_ptr<DataConsumer> consumer); |
61 | | // return the consumers in consumer group to the pool |
62 | | void return_consumers(DataConsumerGroup* grp); |
63 | | |
64 | | Status start_bg_worker(); |
65 | | |
66 | | private: |
67 | | void _clean_idle_consumer_bg(); |
68 | | |
69 | | private: |
70 | | std::mutex _lock; |
71 | | std::list<std::shared_ptr<DataConsumer>> _pool; |
72 | | |
73 | | CountDownLatch _stop_background_threads_latch; |
74 | | std::shared_ptr<Thread> _clean_idle_consumer_thread; |
75 | | }; |
76 | | |
77 | | } // end namespace doris |