@@ -63,7 +63,7 @@ Path getChunkPath(long baseLSN) {
6363 return dir .resolve (name );
6464 }
6565
66- public long getNextLSN () {
66+ public synchronized long getNextLSN () {
6767 return nextLSN ;
6868 }
6969
@@ -111,10 +111,12 @@ public synchronized long post(InputStream data) throws IOException {
111111 throw new IllegalArgumentException (nwritten + " != 8" );
112112 }
113113 out .force (true );
114- nextLSN += total + 8 ;
115- if (getBaseLSN (nextLSN ) != baseLSN ) {
116- // we exceeded this chunk, round this up to the next chunk
117- nextLSN = getBaseLSN (nextLSN ) + CHUNK_SIZE ;
114+ synchronized (this ) {
115+ nextLSN += total + 8 ;
116+ if (getBaseLSN (nextLSN ) != baseLSN ) {
117+ // we exceeded this chunk, round this up to the next chunk
118+ nextLSN = getBaseLSN (nextLSN ) + CHUNK_SIZE ;
119+ }
118120 }
119121 } catch (IOException e ) {
120122 // truncate the file back to the original size
@@ -191,12 +193,12 @@ public Message get(long lsn) throws IOException {
191193 String .format ("%s/%s is %s bytes but chunk is %s bytes" , relativeLSN , lsn , messageSize , chunkSize ));
192194 }
193195
194- long nextLSN = lsn + 8 + messageSize ;
195- if (getBaseLSN (nextLSN ) != baseLSN ) {
196+ long lsnAfter = lsn + 8 + messageSize ;
197+ if (getBaseLSN (lsnAfter ) != baseLSN ) {
196198 // we exceeded this chunk, round this up to the next chunk
197- nextLSN = getBaseLSN (nextLSN ) + CHUNK_SIZE ;
199+ lsnAfter = getBaseLSN (lsnAfter ) + CHUNK_SIZE ;
198200 }
199- return new Message (ByteStreams .limit (Channels .newInputStream (in ), messageSize ), nextLSN );
201+ return new Message (ByteStreams .limit (Channels .newInputStream (in ), messageSize ), lsnAfter );
200202 } catch (BufferUnderflowException e ) {
201203 throw new IOException (e );
202204 } catch (IOException e ) {
0 commit comments