forked from prestodb/presto
-
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.
BinaryFileSpiller spills data to binary files without any compression.
- Loading branch information
Showing
7 changed files
with
331 additions
and
3 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
133 changes: 133 additions & 0 deletions
133
presto-main/src/main/java/com/facebook/presto/spiller/BinaryFileSpiller.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,133 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.spiller; | ||
|
||
import com.facebook.presto.block.PagesSerde; | ||
import com.facebook.presto.spi.Page; | ||
import com.facebook.presto.spi.PrestoException; | ||
import com.facebook.presto.spi.block.BlockEncodingSerde; | ||
import com.google.common.io.Closer; | ||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import io.airlift.concurrent.MoreFutures; | ||
import io.airlift.slice.InputStreamSliceInput; | ||
import io.airlift.slice.OutputStreamSliceOutput; | ||
import io.airlift.slice.RuntimeIOException; | ||
import io.airlift.slice.SliceOutput; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
import static com.facebook.presto.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR; | ||
import static com.facebook.presto.spiller.BinarySpillerFactory.SPILL_PATH; | ||
import static com.facebook.presto.util.ImmutableCollectors.toImmutableList; | ||
import static java.util.Objects.requireNonNull; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
public class BinaryFileSpiller | ||
implements Spiller | ||
{ | ||
private final Path targetDirectory; | ||
private final Closer closer = Closer.create(); | ||
private final BlockEncodingSerde blockEncodingSerde; | ||
|
||
private int spillsCount; | ||
private final ListeningExecutorService executor; | ||
|
||
public BinaryFileSpiller(BlockEncodingSerde blockEncodingSerde, ListeningExecutorService executor) | ||
{ | ||
this.blockEncodingSerde = requireNonNull(blockEncodingSerde, "blockEncodingSerde is null"); | ||
this.executor = requireNonNull(executor, "executor is null"); | ||
try { | ||
this.targetDirectory = Files.createTempDirectory(SPILL_PATH, "presto-spill"); | ||
} | ||
catch (IOException e) { | ||
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Failed to create spill directory", e); | ||
} | ||
} | ||
|
||
@Override | ||
public CompletableFuture<?> spill(Iterator<Page> pageIterator) | ||
{ | ||
Path spillPath = getPath(spillsCount++); | ||
|
||
return MoreFutures.toCompletableFuture(executor.submit( | ||
() -> writePages(pageIterator, spillPath) | ||
)); | ||
} | ||
|
||
private void writePages(Iterator<Page> pageIterator, Path spillPath) | ||
{ | ||
try (SliceOutput output = new OutputStreamSliceOutput(new BufferedOutputStream(new FileOutputStream(spillPath.toFile())))) { | ||
PagesSerde.writePages(blockEncodingSerde, output, pageIterator); | ||
} | ||
catch (RuntimeIOException | IOException e) { | ||
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Failed to spill pages", e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Iterator<Page>> getSpills() | ||
{ | ||
return IntStream.range(0, spillsCount) | ||
.mapToObj(i -> readPages(getPath(i))) | ||
.collect(toImmutableList()); | ||
} | ||
|
||
private Iterator<Page> readPages(Path spillPath) | ||
{ | ||
try { | ||
InputStream input = new BufferedInputStream(new FileInputStream(spillPath.toFile())); | ||
closer.register(input); | ||
return PagesSerde.readPages(blockEncodingSerde, new InputStreamSliceInput(input)); | ||
} | ||
catch (IOException e) { | ||
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Failed to read spilled pages", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() | ||
{ | ||
try (Stream<Path> list = Files.list(targetDirectory)) { | ||
closer.close(); | ||
for (Path path : list.collect(toList())) { | ||
Files.delete(path); | ||
} | ||
Files.delete(targetDirectory); | ||
} | ||
catch (IOException e) { | ||
throw new PrestoException( | ||
GENERIC_INTERNAL_ERROR, | ||
String.format("Failed to delete directory [%s]", targetDirectory), | ||
e); | ||
} | ||
} | ||
|
||
private Path getPath(int spillNumber) | ||
{ | ||
return Paths.get(targetDirectory.toAbsolutePath().toString(), String.format("%d.bin", spillNumber)); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
presto-main/src/main/java/com/facebook/presto/spiller/BinarySpillerFactory.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,58 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.spiller; | ||
|
||
import com.facebook.presto.spi.block.BlockEncodingSerde; | ||
import com.facebook.presto.spi.type.Type; | ||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.inject.Inject; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
import static io.airlift.concurrent.Threads.daemonThreadsNamed; | ||
import static java.util.Objects.requireNonNull; | ||
import static java.util.concurrent.Executors.newFixedThreadPool; | ||
|
||
public class BinarySpillerFactory | ||
implements SpillerFactory | ||
{ | ||
public static final Path SPILL_PATH = Paths.get("/tmp/spills"); | ||
|
||
private final ListeningExecutorService executor; | ||
private final BlockEncodingSerde blockEncodingSerde; | ||
|
||
@Inject | ||
public BinarySpillerFactory(BlockEncodingSerde blockEncodingSerde) | ||
{ | ||
this(blockEncodingSerde, MoreExecutors.listeningDecorator(newFixedThreadPool(4, daemonThreadsNamed("binary-spiller-%s")))); | ||
} | ||
|
||
public BinarySpillerFactory(BlockEncodingSerde blockEncodingSerde, ListeningExecutorService executor) | ||
{ | ||
this.blockEncodingSerde = requireNonNull(blockEncodingSerde, "blockEncodingSerde is null"); | ||
this.executor = requireNonNull(executor, "executor is null"); | ||
|
||
SPILL_PATH.toFile().mkdirs(); | ||
} | ||
|
||
@Override | ||
public Spiller create(List<Type> types) | ||
{ | ||
return new BinaryFileSpiller(blockEncodingSerde, executor); | ||
} | ||
} |
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
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
Oops, something went wrong.