-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BoundedNetworkChannel.java
240 lines (217 loc) · 6.26 KB
/
BoundedNetworkChannel.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.causalclustering.messaging;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import java.util.Objects;
import java.util.Queue;
import org.neo4j.storageengine.api.WritableChannel;
import static java.lang.Integer.min;
/**
* Uses provided allocator to create {@link ByteBuf}. The buffers will be split if maximum size is reached. The full buffer is then added
* to the provided output and a new buffer is allocated. If the output queue is bounded then writing to this channel may block!
*/
public class BoundedNetworkChannel implements WritableChannel, AutoCloseable
{
private static final int DEFAULT_INIT_CHUNK_SIZE = 512;
private final ByteBufAllocator allocator;
private final int maxChunkSize;
private final int initSize;
private final Queue<ByteBuf> byteBufs;
private ByteBuf current;
private boolean isClosed;
/**
* @param allocator used to allocated {@link ByteBuf}
* @param maxChunkSize when reached the current buffer will be moved to the @param outputQueue and a new {@link ByteBuf} is allocated
* @param outputQueue full or flushed buffers are added here. If this queue is bounded then writing to this channel may block!
*/
public BoundedNetworkChannel( ByteBufAllocator allocator, int maxChunkSize, Queue<ByteBuf> outputQueue )
{
Objects.requireNonNull( allocator, "allocator cannot be null" );
Objects.requireNonNull( outputQueue, "outputQueue cannot be null" );
this.allocator = allocator;
this.maxChunkSize = maxChunkSize;
this.initSize = min( DEFAULT_INIT_CHUNK_SIZE, maxChunkSize );
if ( maxChunkSize < Double.BYTES )
{
throw new IllegalArgumentException( "Chunk size must be at least 8. Got " + maxChunkSize );
}
this.byteBufs = outputQueue;
}
@Override
public WritableChannel put( byte value )
{
checkState();
prepareWrite( 1 );
current.writeByte( value );
return this;
}
@Override
public WritableChannel putShort( short value )
{
checkState();
prepareWrite( Short.BYTES );
current.writeShort( value );
return this;
}
@Override
public WritableChannel putInt( int value )
{
checkState();
prepareWrite( Integer.BYTES );
current.writeInt( value );
return this;
}
@Override
public WritableChannel putLong( long value )
{
checkState();
prepareWrite( Long.BYTES );
current.writeLong( value );
return this;
}
@Override
public WritableChannel putFloat( float value )
{
checkState();
prepareWrite( Float.BYTES );
current.writeFloat( value );
return this;
}
@Override
public WritableChannel putDouble( double value )
{
checkState();
prepareWrite( Double.BYTES );
current.writeDouble( value );
return this;
}
@Override
public WritableChannel put( byte[] value, int length )
{
checkState();
int writeIndex = 0;
int remaining = length;
while ( remaining != 0 )
{
int toWrite = prepareGently( remaining );
ByteBuf current = getOrCreateCurrent();
current.writeBytes( value, writeIndex, toWrite );
writeIndex += toWrite;
remaining = length - writeIndex;
}
return this;
}
/**
* Move the current buffer to the output.
*/
public WritableChannel flush()
{
storeCurrent();
return this;
}
private int prepareGently( int size )
{
if ( getOrCreateCurrent().writerIndex() == maxChunkSize )
{
prepareWrite( size );
}
return min( maxChunkSize - current.writerIndex(), size );
}
private ByteBuf getOrCreateCurrent()
{
if ( current == null )
{
current = allocateNewBuffer();
}
return current;
}
private void prepareWrite( int size )
{
if ( (getOrCreateCurrent().writerIndex() + size) > maxChunkSize )
{
storeCurrent();
}
getOrCreateCurrent();
}
private void storeCurrent()
{
if ( current == null )
{
return;
}
try
{
while ( !byteBufs.offer( current ) )
{
Thread.sleep( 10 );
}
current = null;
}
catch ( InterruptedException e )
{
Thread.currentThread().interrupt();
throw new IllegalStateException( "Unable to flush. Thread interrupted" );
}
}
private void releaseCurrent()
{
if ( this.current != null )
{
current.release();
}
}
private ByteBuf allocateNewBuffer()
{
return allocator.buffer( initSize, maxChunkSize );
}
private void checkState()
{
if ( isClosed )
{
throw new IllegalStateException( "Channel has been closed already" );
}
}
/**
* Flushes and closes the channel
*
* @see #flush()
*/
@Override
public void close()
{
try
{
flush();
}
finally
{
isClosed = true;
releaseCurrent();
}
}
public boolean closed()
{
return isClosed;
}
}