forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-17547][task] Implement getUnconsumedSegment for spilled buffers
- Loading branch information
1 parent
5415574
commit 2aacb62
Showing
7 changed files
with
448 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
flink-core/src/test/java/org/apache/flink/core/memory/MemorySegmentFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.core.memory; | ||
|
||
import org.junit.Test; | ||
|
||
import static java.lang.System.arraycopy; | ||
import static org.junit.Assert.assertArrayEquals; | ||
|
||
/** | ||
* {@link MemorySegmentFactory} test. | ||
*/ | ||
public class MemorySegmentFactoryTest { | ||
|
||
@Test | ||
public void testWrapCopyChangingData() { | ||
byte[] data = {1, 2, 3, 4, 5}; | ||
byte[] changingData = new byte[data.length]; | ||
arraycopy(data, 0, changingData, 0, data.length); | ||
MemorySegment segment = MemorySegmentFactory.wrapCopy(changingData, 0, changingData.length); | ||
changingData[0]++; | ||
assertArrayEquals(data, segment.heapMemory); | ||
} | ||
|
||
@Test | ||
public void testWrapPartialCopy() { | ||
byte[] data = {1, 2, 3, 5, 6}; | ||
MemorySegment segment = MemorySegmentFactory.wrapCopy(data, 0, data.length / 2); | ||
byte[] exp = new byte[segment.size()]; | ||
arraycopy(data, 0, exp, 0, exp.length); | ||
assertArrayEquals(exp, segment.heapMemory); | ||
} | ||
|
||
@Test | ||
public void testWrapCopyEmpty() { | ||
MemorySegmentFactory.wrapCopy(new byte[0], 0, 0); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testWrapCopyWrongStart() { | ||
MemorySegmentFactory.wrapCopy(new byte[]{1, 2, 3}, 10, 3); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testWrapCopyWrongEnd() { | ||
MemorySegmentFactory.wrapCopy(new byte[]{1, 2, 3}, 0, 10); | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
flink-core/src/test/java/org/apache/flink/util/CloseableIteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
/** | ||
* {@link CloseableIterator} test. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public class CloseableIteratorTest { | ||
|
||
private static final String[] ELEMENTS = new String[]{"flink", "blink"}; | ||
|
||
@Test | ||
public void testFlattenEmpty() throws Exception { | ||
List<CloseableIterator<?>> iterators = asList( | ||
CloseableIterator.flatten(), | ||
CloseableIterator.flatten(CloseableIterator.empty()), | ||
CloseableIterator.flatten(CloseableIterator.flatten())); | ||
for (CloseableIterator<?> i : iterators) { | ||
assertFalse(i.hasNext()); | ||
i.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testFlattenIteration() { | ||
CloseableIterator<String> iterator = CloseableIterator.flatten( | ||
CloseableIterator.ofElement(ELEMENTS[0], unused -> { | ||
}), | ||
CloseableIterator.ofElement(ELEMENTS[1], unused -> { | ||
}) | ||
); | ||
|
||
List<String> iterated = new ArrayList<>(); | ||
iterator.forEachRemaining(iterated::add); | ||
assertArrayEquals(ELEMENTS, iterated.toArray()); | ||
} | ||
|
||
@Test(expected = TestException.class) | ||
public void testFlattenErrorHandling() throws Exception { | ||
List<String> closed = new ArrayList<>(); | ||
CloseableIterator<String> iterator = CloseableIterator.flatten( | ||
CloseableIterator.ofElement(ELEMENTS[0], e -> { | ||
closed.add(e); | ||
throw new TestException(); | ||
}), | ||
CloseableIterator.ofElement(ELEMENTS[1], closed::add) | ||
); | ||
try { | ||
iterator.close(); | ||
} finally { | ||
assertArrayEquals(ELEMENTS, closed.toArray()); | ||
} | ||
} | ||
|
||
private static class TestException extends RuntimeException { | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
flink-runtime/src/main/java/org/apache/flink/runtime/io/disk/FileBasedBufferIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.runtime.io.disk; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.core.fs.RefCountedFile; | ||
import org.apache.flink.runtime.io.network.buffer.Buffer; | ||
import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler; | ||
import org.apache.flink.runtime.io.network.buffer.NetworkBuffer; | ||
import org.apache.flink.util.CloseableIterator; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
import static org.apache.flink.core.memory.MemorySegmentFactory.wrap; | ||
import static org.apache.flink.runtime.io.network.buffer.Buffer.DataType.DATA_BUFFER; | ||
import static org.apache.flink.util.IOUtils.closeAll; | ||
import static org.apache.flink.util.Preconditions.checkArgument; | ||
import static org.apache.flink.util.Preconditions.checkNotNull; | ||
import static org.apache.flink.util.Preconditions.checkState; | ||
|
||
/** | ||
* {@link CloseableIterator} of {@link Buffer buffers} over file content. | ||
*/ | ||
@Internal | ||
public class FileBasedBufferIterator implements CloseableIterator<Buffer> { | ||
|
||
private final RefCountedFile file; | ||
private final FileInputStream stream; | ||
private final int bufferSize; | ||
|
||
private int offset; | ||
private int bytesToRead; | ||
|
||
public FileBasedBufferIterator(RefCountedFile file, int bytesToRead, int bufferSize) throws FileNotFoundException { | ||
checkNotNull(file); | ||
checkArgument(bytesToRead >= 0); | ||
checkArgument(bufferSize > 0); | ||
this.stream = new FileInputStream(file.getFile()); | ||
this.file = file; | ||
this.bufferSize = bufferSize; | ||
this.bytesToRead = bytesToRead; | ||
file.retain(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return bytesToRead > 0; | ||
} | ||
|
||
@Override | ||
public Buffer next() { | ||
byte[] buffer = new byte[bufferSize]; | ||
int bytesRead = read(buffer); | ||
checkState(bytesRead >= 0, "unexpected end of file, file = " + file.getFile() + ", offset=" + offset); | ||
offset += bytesRead; | ||
bytesToRead -= bytesRead; | ||
return new NetworkBuffer(wrap(buffer), FreeingBufferRecycler.INSTANCE, DATA_BUFFER, bytesRead); | ||
} | ||
|
||
private int read(byte[] buffer) { | ||
int limit = Math.min(buffer.length, bytesToRead); | ||
try { | ||
return stream.read(buffer, offset, limit); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
closeAll(stream, file::release); | ||
} | ||
} |
Oops, something went wrong.