/
committer_impl.py
180 lines (157 loc) · 5.97 KB
/
committer_impl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright 2020 Google LLC
#
# 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.
import asyncio
from typing import Optional, List
import logging
from overrides import overrides
from google.cloud.pubsublite.internal.wait_ignore_cancelled import wait_ignore_errors
from google.cloud.pubsublite.internal.wire.committer import Committer
from google.cloud.pubsublite.internal.wire.retrying_connection import (
RetryingConnection,
ConnectionFactory,
)
from google.api_core.exceptions import FailedPrecondition, GoogleAPICallError
from google.cloud.pubsublite.internal.wire.connection_reinitializer import (
ConnectionReinitializer,
)
from google.cloud.pubsublite.internal.wire.connection import Connection
from google.cloud.pubsublite_v1 import Cursor
from google.cloud.pubsublite_v1.types import (
StreamingCommitCursorRequest,
StreamingCommitCursorResponse,
InitialCommitCursorRequest,
)
_LOGGER = logging.getLogger(__name__)
class CommitterImpl(
Committer,
ConnectionReinitializer[
StreamingCommitCursorRequest, StreamingCommitCursorResponse
],
):
_initial: InitialCommitCursorRequest
_flush_seconds: float
_connection: RetryingConnection[
StreamingCommitCursorRequest, StreamingCommitCursorResponse
]
_next_to_commit: Optional[Cursor]
_outstanding_commits: List[Cursor]
_receiver: Optional[asyncio.Future]
_flusher: Optional[asyncio.Future]
_empty: asyncio.Event
def __init__(
self,
initial: InitialCommitCursorRequest,
flush_seconds: float,
factory: ConnectionFactory[
StreamingCommitCursorRequest, StreamingCommitCursorResponse
],
):
self._initial = initial
self._flush_seconds = flush_seconds
self._connection = RetryingConnection(factory, self)
self._next_to_commit = None
self._outstanding_commits = []
self._receiver = None
self._flusher = None
self._empty = asyncio.Event()
self._empty.set()
async def __aenter__(self):
await self._connection.__aenter__()
return self
def _start_loopers(self):
assert self._receiver is None
assert self._flusher is None
self._receiver = asyncio.ensure_future(self._receive_loop())
self._flusher = asyncio.ensure_future(self._flush_loop())
async def _stop_loopers(self):
if self._receiver:
self._receiver.cancel()
await wait_ignore_errors(self._receiver)
self._receiver = None
if self._flusher:
self._flusher.cancel()
await wait_ignore_errors(self._flusher)
self._flusher = None
def _handle_response(self, response: StreamingCommitCursorResponse):
if "commit" not in response:
self._connection.fail(
FailedPrecondition(
"Received an invalid subsequent response on the commit stream."
)
)
if response.commit.acknowledged_commits > len(self._outstanding_commits):
self._connection.fail(
FailedPrecondition(
"Received a commit response on the stream with no outstanding commits."
)
)
for _ in range(response.commit.acknowledged_commits):
self._outstanding_commits.pop(0)
if len(self._outstanding_commits) == 0:
self._empty.set()
async def _receive_loop(self):
while True:
response = await self._connection.read()
self._handle_response(response)
async def _flush_loop(self):
while True:
await asyncio.sleep(self._flush_seconds)
await self._flush()
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._stop_loopers()
if not self._connection.error():
await self._flush()
await self._connection.__aexit__(exc_type, exc_val, exc_tb)
async def _flush(self):
if self._next_to_commit is None:
return
req = StreamingCommitCursorRequest()
req.commit.cursor = self._next_to_commit
self._outstanding_commits.append(self._next_to_commit)
self._next_to_commit = None
self._empty.clear()
try:
await self._connection.write(req)
except GoogleAPICallError as e:
_LOGGER.debug(f"Failed commit on stream: {e}")
async def wait_until_empty(self):
await self._flush()
await self._connection.await_unless_failed(self._empty.wait())
def commit(self, cursor: Cursor) -> None:
if self._connection.error():
raise self._connection.error()
self._next_to_commit = cursor
@overrides
async def stop_processing(self, error: GoogleAPICallError):
await self._stop_loopers()
@overrides
async def reinitialize(
self,
connection: Connection[
StreamingCommitCursorRequest, StreamingCommitCursorResponse
],
):
await connection.write(StreamingCommitCursorRequest(initial=self._initial))
response = await connection.read()
if "initial" not in response:
self._connection.fail(
FailedPrecondition(
"Received an invalid initial response on the publish stream."
)
)
if self._next_to_commit is None:
if self._outstanding_commits:
self._next_to_commit = self._outstanding_commits[-1]
self._outstanding_commits = []
self._start_loopers()