]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/demos/sharded_parameter_demo.cc
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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright 2020 ScyllaDB
23 // Demonstration of seastar::sharded_parameter
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>
31 // This is some service that we wish to run on all shards.
35 // Pretend that this int is some important resource.
36 int get_capacity() const { return _capacity
; }
39 // Another service that we run on all shards, that depends on service_one.
41 int _resource_allocation
;
43 service_two(service_one
& s1
, int resource_allocation
) : _resource_allocation(resource_allocation
) {}
44 int get_resource_allocation() const { return _resource_allocation
; }
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([&] {
54 seastar::sharded
<service_one
> s1
;
56 auto stop_s1
= seastar::defer([&] { s1
.stop().get(); });
58 auto calculate_half_capacity
= [] (service_one
& s1
) {
59 return s1
.get_capacity() / 2;
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
66 // Each service_two instance will get a reference to a service_one instance on the same shard
68 // This calculation will be performed on each shard
69 seastar::sharded_parameter(calculate_half_capacity
, std::ref(s1
))
71 seastar::defer([&] { s2
.stop().get(); });
73 s2
.invoke_on_all([] (service_two
& s2
) {
74 assert(s2
.get_resource_allocation() == 3);