-
Notifications
You must be signed in to change notification settings - Fork 14
/
ProtonSessionOutgoingWindow.java
365 lines (304 loc) · 13.9 KB
/
ProtonSessionOutgoingWindow.java
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.qpid.protonj2.engine.impl;
import java.util.Set;
import org.apache.qpid.protonj2.buffer.ProtonBuffer;
import org.apache.qpid.protonj2.engine.OutgoingAMQPEnvelope;
import org.apache.qpid.protonj2.engine.util.UnsettledMap;
import org.apache.qpid.protonj2.types.DeliveryTag;
import org.apache.qpid.protonj2.types.transport.Begin;
import org.apache.qpid.protonj2.types.transport.Disposition;
import org.apache.qpid.protonj2.types.transport.Flow;
import org.apache.qpid.protonj2.types.transport.Performative;
import org.apache.qpid.protonj2.types.transport.Role;
import org.apache.qpid.protonj2.types.transport.Transfer;
/**
* Holds Session level credit window information for outgoing transfers from this
* Session. The window is constrained by the remote incoming capacity restrictions
* or if present outgoing restrictions on pending transfers.
*/
public class ProtonSessionOutgoingWindow {
private final ProtonSession session;
private final ProtonEngine engine;
private final int localChannel;
// This is used for the delivery-id actually stamped in each transfer frame of a given message delivery.
private int outgoingDeliveryId = 0;
// Conceptual outgoing Transfer ID value.
private int nextOutgoingId = 0;
// Track outgoing windowing state information in order to stop outgoing writes if the high
// water mark is hit and restart later once the low water mark is hit. When outgoing capacity
// is at the default -1 value then no real limit is applied. If set to zero no writes are allowed.
private int outgoingCapacity = -1;
private int outgoingWindowHighWaterMark = Integer.MAX_VALUE;
private int outgoingWindowLowWaterMark = Integer.MAX_VALUE / 2;
private int pendingOutgoingWrites;
private boolean writeable;
private long remoteIncomingWindow;
private int remoteNextIncomingId = nextOutgoingId;
private Runnable outgoingFrameWriteComplete = this::handleOutgoingFrameWriteComplete;
private final UnsettledMap<ProtonOutgoingDelivery> unsettled =
new UnsettledMap<ProtonOutgoingDelivery>(ProtonOutgoingDelivery::getDeliveryIdInt);
public ProtonSessionOutgoingWindow(ProtonSession session) {
this.session = session;
this.engine = session.getConnection().getEngine();
this.localChannel = session.getLocalChannel();
}
/**
* Initialize the session level window values on the outbound Begin
*
* @param begin
* The {@link Begin} performative that is about to be sent.
*
* @return the configured performative
*/
Begin configureOutbound(Begin begin) {
begin.setNextOutgoingId(getNextOutgoingId());
begin.setOutgoingWindow(getOutgoingWindow());
updateOutgoingWindowState();
return begin;
}
int getAndIncrementNextDeliveryId() {
return outgoingDeliveryId++;
}
void setOutgoingCapacity(int outgoingCapacity) {
this.outgoingCapacity = outgoingCapacity;
updateOutgoingWindowState();
}
int getOutgoingCapacity() {
return outgoingCapacity;
}
int getRemainingOutgoingCapacity() {
// If set to lower value after some writes are pending this calculation could go negative which we don't want
// so ensure it never drops below zero. Then limit the max value to max positive value and hold there
// as it being more than that is a fairly pointless value to try and convey.
final int allowedWrites = Math.max(0, outgoingWindowHighWaterMark - pendingOutgoingWrites);
final int remaining = (int) (allowedWrites * session.getEngine().configuration().getOutboundMaxFrameSize());
if (outgoingCapacity < 0 || remaining < 0) {
return Integer.MAX_VALUE;
} else {
return remaining;
}
}
boolean isSendable() {
return writeable;
}
private void updateOutgoingWindowState() {
final boolean oldWritable = writeable;
final int maxFrameSize = (int) session.getEngine().configuration().getOutboundMaxFrameSize();
if (outgoingCapacity == 0) {
// At a setting of zero outgoing writes is manually disabled until elevated again to > 0
outgoingWindowHighWaterMark = outgoingWindowLowWaterMark = 0;
writeable = false;
} else if (outgoingCapacity > 0) {
// The local end is writable here if the current pending writes count is below the low water
// mark and also if there is remote incoming window to allow more write.
outgoingWindowHighWaterMark = Math.max(1, outgoingCapacity / maxFrameSize);
outgoingWindowLowWaterMark = outgoingWindowHighWaterMark / 2;
writeable = pendingOutgoingWrites <= outgoingWindowLowWaterMark && remoteIncomingWindow > 0;
} else {
// User disabled outgoing windowing so reset state to reflect that we are not
// enforcing any limit from now on, at least not any sane limit.
outgoingWindowHighWaterMark = Integer.MAX_VALUE;
outgoingWindowLowWaterMark = Integer.MAX_VALUE / 2;
writeable = remoteIncomingWindow > 0;
}
if (!oldWritable && writeable) {
Set<ProtonSender> senders = session.senders();
for (ProtonSender sender : senders) {
sender.handleSessionCreditStateUpdate(this);
if (!writeable) {
break;
}
}
}
}
private void handleOutgoingFrameWriteComplete() {
pendingOutgoingWrites = Math.max(0, --pendingOutgoingWrites);
if (!writeable && (writeable = pendingOutgoingWrites <= outgoingWindowLowWaterMark && remoteIncomingWindow > 0)) {
Set<ProtonSender> senders = session.senders();
for (ProtonSender sender : senders) {
sender.handleSessionCreditStateUpdate(this);
if (!writeable) {
break;
}
}
}
}
//----- Handle incoming performatives relevant to the session.
/**
* Update the session level window values based on remote information.
*
* @param begin
* The {@link Begin} performative received from the remote.
*
* @return the given performative for chaining
*/
Begin handleBegin(Begin begin) {
remoteIncomingWindow = begin.getIncomingWindow();
return begin;
}
/**
* Update the session window state based on an incoming {@link Flow} performative
*
* @param flow
* the incoming {@link Flow} performative to process.
*/
Flow handleFlow(Flow flow) {
if (flow.hasNextIncomingId()) {
remoteNextIncomingId = (int) flow.getNextIncomingId();
remoteIncomingWindow = (remoteNextIncomingId + flow.getIncomingWindow()) - nextOutgoingId;
} else {
remoteIncomingWindow = flow.getIncomingWindow();
}
writeable = remoteIncomingWindow > 0 && pendingOutgoingWrites <= outgoingWindowLowWaterMark;
return flow;
}
/**
* Update the session window state based on an incoming {@link Transfer} performative
*
* @param transfer
* the incoming {@link Transfer} performative to process.
*/
Transfer handleTransfer(Transfer transfer, ProtonBuffer payload) {
return transfer;
}
/**
* Update the state of any sent Transfers that are indicated in the disposition
* with the state information conveyed therein.
*
* @param disposition
* The {@link Disposition} performative to process
*
* @return the {@link Disposition}
*/
Disposition handleDisposition(Disposition disposition) {
final int first = (int) disposition.getFirst();
if (disposition.hasLast() && disposition.getLast() != first) {
handleRangedDisposition(unsettled, disposition);
} else {
final ProtonOutgoingDelivery delivery = disposition.getSettled() ?
unsettled.remove(first) : unsettled.get(first);
if (delivery != null) {
delivery.getLink().remoteDisposition(disposition, delivery);
}
}
return disposition;
}
private static void handleRangedDisposition(UnsettledMap<ProtonOutgoingDelivery> unsettled, Disposition disposition) {
// Dispositions cover a contiguous range in the map and since the tracker always moves forward
// when appending new deliveries the range can wrap without needing a second iteration.
if (disposition.getSettled()) {
unsettled.removeEach((int) disposition.getFirst(), (int) disposition.getLast(), (delivery) -> {
delivery.getLink().remoteDisposition(disposition, delivery);
});
} else {
unsettled.forEach((int) disposition.getFirst(), (int) disposition.getLast(), (delivery) -> {
delivery.getLink().remoteDisposition(disposition, delivery);
});
}
}
//----- Handle sender link actions in the session window context
private final Disposition cachedDisposition = new Disposition();
private final Transfer cachedTransfer = new Transfer();
private static void handlePayloadToLargeRequiresSplitFrames(Performative performative) {
((Transfer) performative).setMore(true);
}
boolean processSend(ProtonSender sender, ProtonOutgoingDelivery delivery, ProtonBuffer payload, boolean complete) {
// For a transfer that hasn't completed but has no bytes in the final transfer write we want
// to allow a transfer to go out with the more flag as false.
if (!delivery.isSettled()) {
unsettled.put((int) delivery.getDeliveryId(), delivery);
}
try {
cachedTransfer.setDeliveryId(delivery.getDeliveryId());
if (delivery.getMessageFormat() != 0) {
cachedTransfer.setMessageFormat(delivery.getMessageFormat());
} else {
cachedTransfer.clearMessageFormat();
}
cachedTransfer.setHandle(sender.getHandle());
cachedTransfer.setSettled(delivery.isSettled());
cachedTransfer.setState(delivery.getState());
do {
// Update session window tracking for each transfer that ends up being sent.
++nextOutgoingId;
++pendingOutgoingWrites;
--remoteIncomingWindow;
writeable = pendingOutgoingWrites < outgoingWindowHighWaterMark && remoteIncomingWindow > 0;
// Only the first transfer requires the delivery tag, afterwards we can omit it for efficiency.
if (delivery.getTransferCount() == 0) {
cachedTransfer.setDeliveryTag(delivery.getTag());
} else {
cachedTransfer.setDeliveryTag((DeliveryTag) null);
}
cachedTransfer.setMore(!complete);
OutgoingAMQPEnvelope frame = engine.wrap(cachedTransfer, localChannel, payload);
frame.setPayloadToLargeHandler(ProtonSessionOutgoingWindow::handlePayloadToLargeRequiresSplitFrames);
frame.setFrameWriteCompletionHandler(outgoingFrameWriteComplete);
engine.fireWrite(frame);
delivery.afterTransferWritten();
} while (payload != null && payload.isReadable() && isSendable());
} finally {
cachedTransfer.reset();
}
return isSendable();
}
void processDisposition(ProtonSender sender, ProtonOutgoingDelivery delivery) {
// Would only be tracked if not already remotely settled.
if (delivery.isSettled() && !delivery.isRemotelySettled()) {
unsettled.remove((int) delivery.getDeliveryId());
}
if (!delivery.isRemotelySettled()) {
cachedDisposition.setFirst(delivery.getDeliveryId());
cachedDisposition.setRole(Role.SENDER);
cachedDisposition.setSettled(delivery.isSettled());
cachedDisposition.setState(delivery.getState());
try {
engine.fireWrite(cachedDisposition, session.getLocalChannel());
} finally {
cachedDisposition.reset();
}
}
}
void processAbort(ProtonSender sender, ProtonOutgoingDelivery delivery) {
cachedTransfer.setDeliveryId(delivery.getDeliveryId());
cachedTransfer.setDeliveryTag(delivery.getTag());
cachedTransfer.setSettled(true);
cachedTransfer.setAborted(true);
cachedTransfer.setHandle(sender.getHandle());
// Ensure we don't track the aborted delivery any longer.
unsettled.remove((int) delivery.getDeliveryId());
try {
engine.fireWrite(cachedTransfer, session.getLocalChannel());
} finally {
cachedTransfer.reset();
}
}
//----- Access to internal state useful for tests
int getNextOutgoingId() {
return nextOutgoingId;
}
long getOutgoingWindow() {
return Integer.MAX_VALUE;
}
int getRemoteNextIncomingId() {
return remoteNextIncomingId;
}
long getRemoteIncomingWindow() {
return remoteIncomingWindow;
}
}