28
28
ConnectionReinitializer ,
29
29
)
30
30
from google .cloud .pubsublite .internal .wire .connection import Connection
31
- from google .cloud .pubsublite .internal .wire .serial_batcher import SerialBatcher
32
31
from google .cloud .pubsublite_v1 import Cursor
33
32
from google .cloud .pubsublite_v1 .types import (
34
33
StreamingCommitCursorRequest ,
35
34
StreamingCommitCursorResponse ,
36
35
InitialCommitCursorRequest ,
37
36
)
38
- from google .cloud .pubsublite .internal .wire .work_item import WorkItem
39
37
40
38
41
39
_LOGGER = logging .getLogger (__name__ )
@@ -53,9 +51,8 @@ class CommitterImpl(
53
51
StreamingCommitCursorRequest , StreamingCommitCursorResponse
54
52
]
55
53
56
- _batcher : SerialBatcher [Cursor , None ]
57
-
58
- _outstanding_commits : List [List [WorkItem [Cursor , None ]]]
54
+ _next_to_commit : Optional [Cursor ]
55
+ _outstanding_commits : List [Cursor ]
59
56
60
57
_receiver : Optional [asyncio .Future ]
61
58
_flusher : Optional [asyncio .Future ]
@@ -72,7 +69,7 @@ def __init__(
72
69
self ._initial = initial
73
70
self ._flush_seconds = flush_seconds
74
71
self ._connection = RetryingConnection (factory , self )
75
- self ._batcher = SerialBatcher ()
72
+ self ._next_to_commit = None
76
73
self ._outstanding_commits = []
77
74
self ._receiver = None
78
75
self ._flusher = None
@@ -113,9 +110,7 @@ def _handle_response(self, response: StreamingCommitCursorResponse):
113
110
)
114
111
)
115
112
for _ in range (response .commit .acknowledged_commits ):
116
- batch = self ._outstanding_commits .pop (0 )
117
- for item in batch :
118
- item .response_future .set_result (None )
113
+ self ._outstanding_commits .pop (0 )
119
114
if len (self ._outstanding_commits ) == 0 :
120
115
self ._empty .set ()
121
116
@@ -131,39 +126,31 @@ async def _flush_loop(self):
131
126
132
127
async def __aexit__ (self , exc_type , exc_val , exc_tb ):
133
128
await self ._stop_loopers ()
134
- if self ._connection .error ():
135
- self ._fail_if_retrying_failed ()
136
- else :
129
+ if not self ._connection .error ():
137
130
await self ._flush ()
138
131
await self ._connection .__aexit__ (exc_type , exc_val , exc_tb )
139
132
140
- def _fail_if_retrying_failed (self ):
141
- if self ._connection .error ():
142
- for batch in self ._outstanding_commits :
143
- for item in batch :
144
- item .response_future .set_exception (self ._connection .error ())
145
-
146
133
async def _flush (self ):
147
- batch = self ._batcher .flush ()
148
- if not batch :
134
+ if self ._next_to_commit is None :
149
135
return
150
- self ._outstanding_commits .append (batch )
151
- self ._empty .clear ()
152
136
req = StreamingCommitCursorRequest ()
153
- req .commit .cursor = batch [- 1 ].request
137
+ req .commit .cursor = self ._next_to_commit
138
+ self ._outstanding_commits .append (self ._next_to_commit )
139
+ self ._next_to_commit = None
140
+ self ._empty .clear ()
154
141
try :
155
142
await self ._connection .write (req )
156
143
except GoogleAPICallError as e :
157
144
_LOGGER .debug (f"Failed commit on stream: { e } " )
158
- self ._fail_if_retrying_failed ()
159
145
160
146
async def wait_until_empty (self ):
161
147
await self ._flush ()
162
148
await self ._connection .await_unless_failed (self ._empty .wait ())
163
149
164
- async def commit (self , cursor : Cursor ) -> None :
165
- future = self ._batcher .add (cursor )
166
- await future
150
+ def commit (self , cursor : Cursor ) -> None :
151
+ if self ._connection .error ():
152
+ raise self ._connection .error ()
153
+ self ._next_to_commit = cursor
167
154
168
155
async def reinitialize (
169
156
self ,
@@ -181,14 +168,8 @@ async def reinitialize(
181
168
"Received an invalid initial response on the publish stream."
182
169
)
183
170
)
184
- if self ._outstanding_commits :
185
- # Roll up outstanding commits
186
- rollup : List [WorkItem [Cursor , None ]] = []
187
- for batch in self ._outstanding_commits :
188
- for item in batch :
189
- rollup .append (item )
190
- self ._outstanding_commits = [rollup ]
191
- req = StreamingCommitCursorRequest ()
192
- req .commit .cursor = rollup [- 1 ].request
193
- await connection .write (req )
171
+ if self ._next_to_commit is None :
172
+ if self ._outstanding_commits :
173
+ self ._next_to_commit = self ._outstanding_commits [- 1 ]
174
+ self ._outstanding_commits = []
194
175
self ._start_loopers ()
0 commit comments