@@ -217,47 +217,58 @@ private void loadNextSegmentV1() throws IOException {
217217 }
218218
219219 private void loadNextSegmentV2 () throws IOException {
220+ long position = channel .position ();
220221 SegmentInfo segmentInfo = getNextSegmentInfo ();
221- if (segmentInfo .compressionType != CompressionType .UNCOMPRESSED ) {
222- // A compressed segment
223- if (Objects .isNull (dataBuffer )
224- || dataBuffer .capacity () < segmentInfo .uncompressedSize
225- || dataBuffer .capacity () > segmentInfo .uncompressedSize * 2 ) {
226- MmapUtil .clean (dataBuffer );
227- dataBuffer = ByteBuffer .allocateDirect (segmentInfo .uncompressedSize );
228- }
229- dataBuffer .clear ();
222+ try {
223+ if (segmentInfo .compressionType != CompressionType .UNCOMPRESSED ) {
224+ // A compressed segment
225+ if (Objects .isNull (dataBuffer )
226+ || dataBuffer .capacity () < segmentInfo .uncompressedSize
227+ || dataBuffer .capacity () > segmentInfo .uncompressedSize * 2 ) {
228+ MmapUtil .clean (dataBuffer );
229+ dataBuffer = ByteBuffer .allocateDirect (segmentInfo .uncompressedSize );
230+ }
231+ dataBuffer .clear ();
230232
231- if (Objects .isNull (compressedBuffer )
232- || compressedBuffer .capacity () < segmentInfo .dataInDiskSize
233- || compressedBuffer .capacity () > segmentInfo .dataInDiskSize * 2 ) {
234- MmapUtil .clean (compressedBuffer );
235- compressedBuffer = ByteBuffer .allocateDirect (segmentInfo .dataInDiskSize );
236- }
237- compressedBuffer .clear ();
238- // limit the buffer to prevent it from reading too much byte than expected
239- compressedBuffer .limit (segmentInfo .dataInDiskSize );
240- if (readWALBufferFromChannel (compressedBuffer ) != segmentInfo .dataInDiskSize ) {
241- throw new IOException ("Unexpected end of file" );
242- }
243- compressedBuffer .flip ();
244- IUnCompressor unCompressor = IUnCompressor .getUnCompressor (segmentInfo .compressionType );
245- uncompressWALBuffer (compressedBuffer , dataBuffer , unCompressor );
246- } else {
247- // An uncompressed segment
248- if (Objects .isNull (dataBuffer )
249- || dataBuffer .capacity () < segmentInfo .dataInDiskSize
250- || dataBuffer .capacity () > segmentInfo .dataInDiskSize * 2 ) {
251- MmapUtil .clean (dataBuffer );
252- dataBuffer = ByteBuffer .allocateDirect (segmentInfo .dataInDiskSize );
253- }
254- dataBuffer .clear ();
255- // limit the buffer to prevent it from reading too much byte than expected
256- dataBuffer .limit (segmentInfo .dataInDiskSize );
233+ if (Objects .isNull (compressedBuffer )
234+ || compressedBuffer .capacity () < segmentInfo .dataInDiskSize
235+ || compressedBuffer .capacity () > segmentInfo .dataInDiskSize * 2 ) {
236+ MmapUtil .clean (compressedBuffer );
237+ compressedBuffer = ByteBuffer .allocateDirect (segmentInfo .dataInDiskSize );
238+ }
239+ compressedBuffer .clear ();
240+ // limit the buffer to prevent it from reading too much byte than expected
241+ compressedBuffer .limit (segmentInfo .dataInDiskSize );
242+ if (readWALBufferFromChannel (compressedBuffer ) != segmentInfo .dataInDiskSize ) {
243+ throw new IOException ("Unexpected end of file" );
244+ }
245+ compressedBuffer .flip ();
246+ IUnCompressor unCompressor = IUnCompressor .getUnCompressor (segmentInfo .compressionType );
247+ uncompressWALBuffer (compressedBuffer , dataBuffer , unCompressor );
248+ } else {
249+ // An uncompressed segment
250+ if (Objects .isNull (dataBuffer )
251+ || dataBuffer .capacity () < segmentInfo .dataInDiskSize
252+ || dataBuffer .capacity () > segmentInfo .dataInDiskSize * 2 ) {
253+ MmapUtil .clean (dataBuffer );
254+ dataBuffer = ByteBuffer .allocateDirect (segmentInfo .dataInDiskSize );
255+ }
256+ dataBuffer .clear ();
257+ // limit the buffer to prevent it from reading too much byte than expected
258+ dataBuffer .limit (segmentInfo .dataInDiskSize );
257259
258- if (readWALBufferFromChannel (dataBuffer ) != segmentInfo .dataInDiskSize ) {
259- throw new IOException ("Unexpected end of file" );
260+ if (readWALBufferFromChannel (dataBuffer ) != segmentInfo .dataInDiskSize ) {
261+ throw new IOException ("Unexpected end of file" );
262+ }
260263 }
264+ } catch (Exception e ) {
265+ logger .error (
266+ "Unexpected error when loading a wal segment {} in {}@{}" ,
267+ segmentInfo ,
268+ logFile ,
269+ position ,
270+ e );
271+ throw new IOException (e );
261272 }
262273 dataBuffer .flip ();
263274 }
@@ -391,5 +402,17 @@ int headerSize() {
391402 ? Byte .BYTES + Integer .BYTES
392403 : Byte .BYTES + Integer .BYTES * 2 ;
393404 }
405+
406+ @ Override
407+ public String toString () {
408+ return "SegmentInfo{"
409+ + "compressionType="
410+ + compressionType
411+ + ", dataInDiskSize="
412+ + dataInDiskSize
413+ + ", uncompressedSize="
414+ + uncompressedSize
415+ + '}' ;
416+ }
394417 }
395418}
0 commit comments