99
1010package  org .elasticsearch .common .blobstore .support ;
1111
12+ import  org .apache .logging .log4j .Level ;
13+ import  org .apache .logging .log4j .LogManager ;
14+ import  org .apache .logging .log4j .Logger ;
15+ import  org .elasticsearch .common .ReferenceDocs ;
1216import  org .elasticsearch .common .Strings ;
1317import  org .elasticsearch .common .blobstore .BlobContainer ;
1418import  org .elasticsearch .common .bytes .BytesArray ;
1519import  org .elasticsearch .common .bytes .BytesReference ;
20+ import  org .elasticsearch .common .logging .ChunkedLoggingStream ;
21+ import  org .elasticsearch .common .unit .ByteSizeUnit ;
1622
1723import  java .io .IOException ;
1824import  java .io .InputStream ;
@@ -32,6 +38,8 @@ public static void ensureValidRegisterContent(BytesReference bytesReference) {
3238        }
3339    }
3440
41+     private  static  final  Logger  logger  = LogManager .getLogger (BlobContainerUtils .class );
42+ 
3543    /** 
3644     * Many blob stores have consistent (linearizable/atomic) read semantics and in these casees it is safe to implement {@link 
3745     * BlobContainer#getRegister} by simply reading the blob using this utility. 
@@ -51,7 +59,28 @@ public static BytesReference getRegisterUsingConsistentRead(InputStream inputStr
5159            len  -= read ;
5260            pos  += read ;
5361        }
54-         if  (inputStream .read () != -1 ) {
62+         final  int  nextByte  = inputStream .read ();
63+         if  (nextByte  != -1 ) {
64+             try  (
65+                 var  cls  = ChunkedLoggingStream .create (
66+                     logger ,
67+                     Level .ERROR ,
68+                     "getRegisterUsingConsistentRead including trailing data" ,
69+                     ReferenceDocs .LOGGING 
70+                 )
71+             ) {
72+                 cls .write (bytes );
73+                 cls .write (nextByte );
74+                 final  var  buffer  = new  byte [ByteSizeUnit .KB .toIntBytes (1 )];
75+                 while  (true ) {
76+                     final  var  readSize  = inputStream .read (buffer );
77+                     if  (readSize  == -1 ) {
78+                         break ;
79+                     }
80+                     cls .write (buffer , 0 , readSize );
81+                 }
82+             }
83+ 
5584            throw  new  IllegalStateException (
5685                Strings .format ("[%s] failed reading register [%s] due to unexpected trailing data" , container , key )
5786            );
0 commit comments