[
https://issues.apache.org/jira/browse/CAMEL-9735?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15206256#comment-15206256
]
Sverker Abrahamsson commented on CAMEL-9735:
--------------------------------------------
Ok, I'm starting to get the grip on this.
The issue with the fix in CAMEL-9375 is that a call to hasNext() will cause the
inputstream to move to the next tar entry, hence the StreamCache was used to
copy the data from InputStream at that point before the next hasNext() call.
However, that doesn't work with entries in the tarfile that are bigger than can
be kept in memory, hence that I get the OutOfMemoryError. Even if spooling of
the StreamCache to temp file would work this is not an acceptable solution as I
don't want to write and read unnecessary gigabytes of data to/from disk.
When I restore hasNext() to the previous logic (more or less) my use case work
as well as all the unit tests. The reason is that hasNext() will not move on to
the next entry in tarfile until all data from the current entry is consumed.
The problem is that this is not correct behaviour of hasNext() because it
creates a race condition. When next() method has returned the last entry,
hasNext() should return false but it will return true until the data of that
entry has been consumed from the inputstream even though next call to next()
will return null (although it should throw NoSuchElementException according to
Iterator interface).
TarFileMultipleFilesSplitterTest doesn't catch that since the testfile is so
small that the race condition doesn't appear.
> camel-tarfile throws OutOfMemoryError when splitting large files
> ----------------------------------------------------------------
>
> Key: CAMEL-9735
> URL: https://issues.apache.org/jira/browse/CAMEL-9735
> Project: Camel
> Issue Type: Improvement
> Components: camel-tarfile
> Affects Versions: 2.16.2, 2.17.0
> Reporter: Sverker Abrahamsson
> Assignee: Claus Ibsen
> Priority: Minor
> Fix For: 2.16.3, 2.18.0, 2.17.1
>
>
> After the change in https://issues.apache.org/jira/browse/CAMEL-9375 I
> started to get failures with the following stacktrace:
> {noformat}
> Caused by: java.lang.OutOfMemoryError: Java heap space
> at java.util.Arrays.copyOf(Arrays.java:3236)
> at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
> at
> java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
> at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
> at
> org.apache.camel.converter.stream.CachedOutputStream.write(CachedOutputStream.java:104)
> at org.apache.camel.util.IOHelper.copy(IOHelper.java:199)
> at org.apache.camel.util.IOHelper.copy(IOHelper.java:169)
> at org.apache.camel.util.IOHelper.copyAndCloseInput(IOHelper.java:218)
> at org.apache.camel.util.IOHelper.copyAndCloseInput(IOHelper.java:214)
> at
> org.apache.camel.converter.stream.StreamCacheConverter.convertToStreamCache(StreamCacheConverter.java:82)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:497)
> at
> org.apache.camel.util.ObjectHelper.invokeMethod(ObjectHelper.java:1243)
> at
> org.apache.camel.impl.converter.StaticMethodTypeConverter.convertTo(StaticMethodTypeConverter.java:59)
> at
> org.apache.camel.impl.converter.BaseTypeConverterRegistry.doConvertTo(BaseTypeConverterRegistry.java:293)
> at
> org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:168)
> at
> org.apache.camel.dataformat.tarfile.TarIterator.createNextMessage(TarIterator.java:121)
> at
> org.apache.camel.dataformat.tarfile.TarIterator.tryAdvanceToNext(TarIterator.java:100)
> at
> org.apache.camel.dataformat.tarfile.TarIterator.hasNext(TarIterator.java:78)
> at
> org.apache.camel.processor.Splitter$SplitterIterable$1.hasNext(Splitter.java:163)
> at
> org.apache.camel.processor.Splitter.updateNewExchange(Splitter.java:256)
> {noformat}
> I.e. using StreamCache makes the whole entry go into memory and since they in
> my usecase are several gigabytes it doesn't work very well. I realize that
> this might be caused since I don't set any StreamCachingStrategy on my
> context but reading the corresponding doc part I get the impression that the
> default settings should be that it will spool to disk after reading 128 kbyte.
> However, I really don't want it to do that neither as that would 1) be a
> significant overhead to write gigabytes of data to a temp file and 2) I might
> not even have that much space where my temp directory is located. Instead the
> functionality that I want is that the tarfile entries are streamed and
> written directly to the destination files without any unnecessary caching in
> between.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)