Skip to content

Commit

Permalink
throw exception if store size for a channel is exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
hg-ms committed Jan 17, 2025
1 parent bec2a9e commit 5831445
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

import java.nio.ByteBuffer;

import org.eclipse.store.storage.exceptions.StorageExceptionConsistency;
import org.eclipse.serializer.memory.XMemory;
import org.eclipse.serializer.persistence.binary.types.Binary;
import org.eclipse.serializer.persistence.binary.types.BinaryEntityRawDataIterator;
import org.eclipse.serializer.persistence.exceptions.PersistenceExceptionCommitSizeExceeded;
import org.eclipse.store.storage.exceptions.StorageExceptionConsistency;


public interface StorageDataChunkValidator
{
public void validateDataChunk(Binary data);




public static StorageDataChunkValidator New(
final BinaryEntityRawDataIterator entityDataIterator ,
final StorageEntityDataValidator entityDataValidator
Expand Down Expand Up @@ -321,5 +321,45 @@ public final Provider provideDataChunkValidatorProvider(
}

}

public class MaxFileSize implements StorageDataChunkValidator, Provider, Provider2
{
@Override
public Provider provideDataChunkValidatorProvider(
StorageFoundation<?> foundation
)
{
return this;
}

@Override
public StorageDataChunkValidator provideDataChunkValidator(
StorageTypeDictionary typeDictionary
)
{
return this;
}

@Override
public final void validateDataChunk(
final Binary data
)
{
for(int channelIndex = 0; channelIndex < data.channelCount(); channelIndex++)
{
long commitSize = 0;
for(ByteBuffer bb:data.channelChunk(channelIndex).buffers())
{
commitSize += bb.limit();
}

if(commitSize >= Integer.MAX_VALUE)
{
throw new PersistenceExceptionCommitSizeExceeded(channelIndex, commitSize);
}

}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.serializer.collections.types.XGettingSequence;
import org.eclipse.serializer.exceptions.MultiCauseException;
import org.eclipse.serializer.memory.XMemory;
import org.eclipse.serializer.persistence.exceptions.PersistenceExceptionCommitSizeExceeded;
import org.eclipse.serializer.typing.Disposable;
import org.eclipse.serializer.typing.XTypes;
import org.eclipse.serializer.util.BufferSizeProvider;
Expand Down Expand Up @@ -594,6 +595,32 @@ private long ensureHeadFileTotalLength()

return physicalLength;
}

private void ensureHeadFileLoadableSize(final ByteBuffer[] dataBuffers)
{
//Must ensure that a filesize never exceeds 2^31 bytes size.
//If that size is exceeded the storage is corrupted and can't be loaded any more because
//the current implementation use only one byte buffer to load a file.
//This buffer size is limited to an Integer.MAX_VALUE value.

long commitSize = 0;
for(int i = 0; i < dataBuffers.length; i++)
{
commitSize += dataBuffers[i].limit();
if(commitSize > Integer.MAX_VALUE)
{
//Should never be reached, this case should be alreay handled by
//org.eclipse.store.storage.types.StorageDataChunkValidator.MaxFileSize.
//But keep exception to have secound guard as the validator might be replaced.
throw new PersistenceExceptionCommitSizeExceeded(this.channelIndex(), commitSize);
}
if(commitSize + this.headFile.totalLength() > Integer.MAX_VALUE)
{
logger.debug("creating new storage file because appending would exceed the file size limit of 2^31 bytes");
this.createNextStorageFile();
}
}
}

@Override
public final long[] storeChunks(final long timestamp, final ByteBuffer[] dataBuffers)
Expand All @@ -605,6 +632,8 @@ public final long[] storeChunks(final long timestamp, final ByteBuffer[] dataBuf
}

this.checkForNewFile();
this.ensureHeadFileLoadableSize(dataBuffers);

final long oldTotalLength = this.ensureHeadFileTotalLength();
final long[] storagePositions = allChunksStoragePositions(dataBuffers, oldTotalLength);
final long writeCount = this.writer.writeStore(this.headFile, X.ArrayView(dataBuffers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ protected StorageDataChunkValidator.Provider ensureDataChunkValidatorProvider()

protected StorageDataChunkValidator.Provider2 ensureDataChunkValidatorProvider2()
{
return new StorageDataChunkValidator.NoOp();
return new StorageDataChunkValidator.MaxFileSize();
}

protected StorageChannelsCreator ensureChannelCreator()
Expand Down

0 comments on commit 5831445

Please sign in to comment.