Promise based sleeps (#28722)

* Promise based sleep

* Embrace absl::Status

* Automated change: Fix sanity tests

* Add another test, fix bug

* fix

* fix

* review feedback

Co-authored-by: ctiller <ctiller@users.noreply.github.com>
pull/28862/head
Craig Tiller 3 years ago committed by GitHub
parent 6565584c7b
commit df943da2c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      BUILD
  2. 37
      CMakeLists.txt
  3. 13
      build_autogenerated.yaml
  4. 74
      src/core/lib/promise/sleep.cc
  5. 66
      src/core/lib/promise/sleep.h
  6. 18
      test/core/promise/BUILD
  7. 86
      test/core/promise/sleep_test.cc
  8. 24
      tools/run_tests/generated/tests.json

16
BUILD

@ -1076,6 +1076,22 @@ grpc_cc_library(
],
)
grpc_cc_library(
name = "sleep",
srcs = [
"src/core/lib/promise/sleep.cc",
],
hdrs = [
"src/core/lib/promise/sleep.h",
],
deps = [
"activity",
"gpr_platform",
"grpc_base",
"poll",
],
)
grpc_cc_library(
name = "promise",
external_deps = [

37
CMakeLists.txt generated

@ -981,6 +981,7 @@ if(gRPC_BUILD_TESTS)
add_dependencies(buildtests_cxx settings_timeout_test)
add_dependencies(buildtests_cxx shutdown_test)
add_dependencies(buildtests_cxx simple_request_bad_client_test)
add_dependencies(buildtests_cxx sleep_test)
add_dependencies(buildtests_cxx smoke_test)
add_dependencies(buildtests_cxx sockaddr_utils_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
@ -15229,6 +15230,42 @@ target_link_libraries(simple_request_bad_client_test
)
endif()
if(gRPC_BUILD_TESTS)
add_executable(sleep_test
src/core/lib/promise/sleep.cc
test/core/promise/sleep_test.cc
third_party/googletest/googletest/src/gtest-all.cc
third_party/googletest/googlemock/src/gmock-all.cc
)
target_include_directories(sleep_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}
${_gRPC_RE2_INCLUDE_DIR}
${_gRPC_SSL_INCLUDE_DIR}
${_gRPC_UPB_GENERATED_DIR}
${_gRPC_UPB_GRPC_GENERATED_DIR}
${_gRPC_UPB_INCLUDE_DIR}
${_gRPC_XXHASH_INCLUDE_DIR}
${_gRPC_ZLIB_INCLUDE_DIR}
third_party/googletest/googletest/include
third_party/googletest/googletest
third_party/googletest/googlemock/include
third_party/googletest/googlemock
${_gRPC_PROTO_GENS_DIR}
)
target_link_libraries(sleep_test
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_ALLTARGETS_LIBRARIES}
grpc
)
endif()
if(gRPC_BUILD_TESTS)

@ -7745,6 +7745,19 @@ targets:
- test/core/end2end/cq_verifier.cc
deps:
- grpc_test_util
- name: sleep_test
gtest: true
build: test
language: c++
headers:
- src/core/lib/promise/sleep.h
- test/core/promise/test_wakeup_schedulers.h
src:
- src/core/lib/promise/sleep.cc
- test/core/promise/sleep_test.cc
deps:
- grpc
uses_polling: false
- name: smoke_test
gtest: true
build: test

@ -0,0 +1,74 @@
// Copyright 2022 gRPC authors.
//
// 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.
#include <grpc/support/port_platform.h>
#include "src/core/lib/promise/sleep.h"
namespace grpc_core {
Sleep::Sleep(grpc_millis deadline) : state_(new State(deadline)) {
GRPC_CLOSURE_INIT(&state_->on_timer, &OnTimer, state_, nullptr);
}
Sleep::~Sleep() {
if (state_ == nullptr) return;
{
MutexLock lock(&state_->mu);
switch (state_->stage) {
case Stage::kInitial:
state_->Unref();
break;
case Stage::kStarted:
grpc_timer_cancel(&state_->timer);
break;
case Stage::kDone:
break;
}
}
state_->Unref();
}
void Sleep::OnTimer(void* arg, grpc_error_handle) {
auto* state = static_cast<State*>(arg);
Waker waker;
{
MutexLock lock(&state->mu);
state->stage = Stage::kDone;
waker = std::move(state->waker);
}
waker.Wakeup();
state->Unref();
}
Poll<absl::Status> Sleep::operator()() {
MutexLock lock(&state_->mu);
switch (state_->stage) {
case Stage::kInitial:
if (state_->deadline <= ExecCtx::Get()->Now()) {
return absl::OkStatus();
}
state_->stage = Stage::kStarted;
grpc_timer_init(&state_->timer, state_->deadline, &state_->on_timer);
break;
case Stage::kStarted:
break;
case Stage::kDone:
return absl::OkStatus();
}
state_->waker = Activity::current()->MakeNonOwningWaker();
return Pending{};
}
} // namespace grpc_core

@ -0,0 +1,66 @@
// Copyright 2022 gRPC authors.
//
// 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.
#ifndef GRPC_CORE_LIB_PROMISE_SLEEP_H
#define GRPC_CORE_LIB_PROMISE_SLEEP_H
#include <grpc/support/port_platform.h>
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/promise/activity.h"
#include "src/core/lib/promise/poll.h"
namespace grpc_core {
// Promise that sleeps until a deadline and then finishes.
class Sleep {
public:
explicit Sleep(grpc_millis deadline);
~Sleep();
Sleep(const Sleep&) = delete;
Sleep& operator=(const Sleep&) = delete;
Sleep(Sleep&& other) noexcept : state_(other.state_) {
other.state_ = nullptr;
}
Sleep& operator=(Sleep&& other) noexcept {
std::swap(state_, other.state_);
return *this;
}
Poll<absl::Status> operator()();
private:
static void OnTimer(void* arg, grpc_error_handle error);
enum class Stage { kInitial, kStarted, kDone };
struct State {
explicit State(grpc_millis deadline) : deadline(deadline) {}
RefCount refs{2};
const grpc_millis deadline;
grpc_timer timer;
grpc_closure on_timer;
Mutex mu;
Stage stage ABSL_GUARDED_BY(mu) = Stage::kInitial;
Waker waker ABSL_GUARDED_BY(mu);
void Unref() {
if (refs.Unref()) delete this;
}
};
State* state_;
};
} // namespace grpc_core
#endif // GRPC_CORE_LIB_PROMISE_SLEEP_H

@ -328,3 +328,21 @@ grpc_cc_test(
"//test/core/util:grpc_suppressions",
],
)
grpc_cc_test(
name = "sleep_test",
srcs = ["sleep_test.cc"],
external_deps = [
"gtest",
"absl/synchronization",
],
language = "c++",
uses_polling = False,
deps = [
"test_wakeup_schedulers",
"//:activity",
"//:grpc",
"//:sleep",
"//test/core/util:grpc_suppressions",
],
)

@ -0,0 +1,86 @@
// Copyright 2022 gRPC authors.
//
// 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.
#include "src/core/lib/promise/sleep.h"
#include <atomic>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/synchronization/notification.h"
#include <grpc/grpc.h>
#include "src/core/lib/promise/race.h"
#include "src/core/lib/promise/seq.h"
#include "test/core/promise/test_wakeup_schedulers.h"
namespace grpc_core {
namespace {
TEST(Sleep, Zzzz) {
ExecCtx exec_ctx;
absl::Notification done;
grpc_millis done_time = ExecCtx::Get()->Now() + 1000;
// Sleep for one second then set done to true.
auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(),
[&done](absl::Status r) {
EXPECT_EQ(r, absl::OkStatus());
done.Notify();
});
done.WaitForNotification();
exec_ctx.InvalidateNow();
EXPECT_GE(ExecCtx::Get()->Now(), done_time);
}
TEST(Sleep, AlreadyDone) {
ExecCtx exec_ctx;
absl::Notification done;
grpc_millis done_time = ExecCtx::Get()->Now() - 1000;
// Sleep for no time at all then set done to true.
auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(),
[&done](absl::Status r) {
EXPECT_EQ(r, absl::OkStatus());
done.Notify();
});
done.WaitForNotification();
}
TEST(Sleep, Cancel) {
ExecCtx exec_ctx;
absl::Notification done;
grpc_millis done_time = ExecCtx::Get()->Now() + 1000;
// Sleep for one second but race it to complete immediately
auto activity = MakeActivity(
Race(Sleep(done_time), [] { return absl::CancelledError(); }),
InlineWakeupScheduler(), [&done](absl::Status r) {
EXPECT_EQ(r, absl::CancelledError());
done.Notify();
});
done.WaitForNotification();
exec_ctx.InvalidateNow();
EXPECT_LT(ExecCtx::Get()->Now(), done_time);
}
} // namespace
} // namespace grpc_core
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
grpc_init();
int r = RUN_ALL_TESTS();
grpc_shutdown();
return r;
}

@ -6523,6 +6523,30 @@
],
"uses_polling": true
},
{
"args": [],
"benchmark": false,
"ci_platforms": [
"linux",
"mac",
"posix",
"windows"
],
"cpu_cost": 1.0,
"exclude_configs": [],
"exclude_iomgrs": [],
"flaky": false,
"gtest": true,
"language": "c++",
"name": "sleep_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": false
},
{
"args": [],
"benchmark": false,

Loading…
Cancel
Save