]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/demos/sharded_parameter_demo.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / demos / sharded_parameter_demo.cc
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * 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 /*
19 * Copyright 2020 ScyllaDB
20 */
21
22
23 // Demonstration of seastar::sharded_parameter
24
25 #include <seastar/core/sharded.hh>
26 #include <seastar/core/app-template.hh>
27 #include <seastar/core/thread.hh>
28 #include <seastar/util/defer.hh>
29 #include <cassert>
30
31 // This is some service that we wish to run on all shards.
32 class service_one {
33 int _capacity = 7;
34 public:
35 // Pretend that this int is some important resource.
36 int get_capacity() const { return _capacity; }
37 };
38
39 // Another service that we run on all shards, that depends on service_one.
40 class service_two {
41 int _resource_allocation;
42 public:
43 service_two(service_one& s1, int resource_allocation) : _resource_allocation(resource_allocation) {}
44 int get_resource_allocation() const { return _resource_allocation; }
45 };
46
47 int main(int ac, char** av) {
48 seastar::app_template app;
49 return app.run(ac, av, [&] {
50 // sharded<> setup code is typically run in a seastar::thread
51 return seastar::async([&] {
52
53 // Launch service_one
54 seastar::sharded<service_one> s1;
55 s1.start().get();
56 auto stop_s1 = seastar::defer([&] { s1.stop().get(); });
57
58 auto calculate_half_capacity = [] (service_one& s1) {
59 return s1.get_capacity() / 2;
60 };
61
62 // Launch service_two, passing it per-shard dependencies from s1
63 seastar::sharded<service_two> s2;
64 // Start s2, passing two parameters to service_two's constructor
65 s2.start(
66 // Each service_two instance will get a reference to a service_one instance on the same shard
67 std::ref(s1),
68 // This calculation will be performed on each shard
69 seastar::sharded_parameter(calculate_half_capacity, std::ref(s1))
70 ).get();
71 seastar::defer([&] { s2.stop().get(); });
72
73 s2.invoke_on_all([] (service_two& s2) {
74 assert(s2.get_resource_allocation() == 3);
75 }).get();
76 });
77 });
78 }