/
RaptorPageSink.java
186 lines (160 loc) · 6.73 KB
/
RaptorPageSink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* 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.raptor;
import com.facebook.presto.raptor.metadata.ShardInfo;
import com.facebook.presto.raptor.storage.StorageManager;
import com.facebook.presto.raptor.storage.StoragePageSink;
import com.facebook.presto.raptor.util.PageBuffer;
import com.facebook.presto.spi.ConnectorPageSink;
import com.facebook.presto.spi.Page;
import com.facebook.presto.spi.PageSorter;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.SortOrder;
import com.facebook.presto.spi.type.Type;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import io.airlift.json.JsonCodec;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import io.airlift.units.DataSize;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
public class RaptorPageSink
implements ConnectorPageSink
{
private final StoragePageSink storagePageSink;
private final JsonCodec<ShardInfo> shardInfoCodec;
private final int sampleWeightField;
private final PageSorter pageSorter;
private final List<Type> columnTypes;
private final List<Integer> sortFields;
private final List<SortOrder> sortOrders;
private final PageBuffer pageBuffer;
public RaptorPageSink(
PageSorter pageSorter,
StorageManager storageManager,
JsonCodec<ShardInfo> shardInfoCodec,
long transactionId,
List<Long> columnIds,
List<Type> columnTypes,
Optional<Long> sampleWeightColumnId,
List<Long> sortColumnIds,
List<SortOrder> sortOrders,
DataSize maxBufferSize)
{
this.pageSorter = requireNonNull(pageSorter, "pageSorter is null");
this.columnTypes = ImmutableList.copyOf(requireNonNull(columnTypes, "columnTypes is null"));
requireNonNull(storageManager, "storageManager is null");
this.storagePageSink = storageManager.createStoragePageSink(transactionId, columnIds, columnTypes);
this.shardInfoCodec = requireNonNull(shardInfoCodec, "shardInfoCodec is null");
requireNonNull(sampleWeightColumnId, "sampleWeightColumnId is null");
this.sampleWeightField = columnIds.indexOf(sampleWeightColumnId.orElse(-1L));
this.sortFields = ImmutableList.copyOf(sortColumnIds.stream().map(columnIds::indexOf).collect(toList()));
this.sortOrders = ImmutableList.copyOf(requireNonNull(sortOrders, "sortOrders is null"));
// allow only Integer.MAX_VALUE rows to be buffered as that is the max rows we can sort
this.pageBuffer = new PageBuffer(maxBufferSize.toBytes(), Integer.MAX_VALUE);
}
@Override
public void appendPage(Page page, Block sampleWeightBlock)
{
if (page.getPositionCount() == 0) {
return;
}
flushPageBufferIfNecessary(page.getPositionCount());
if (sampleWeightField >= 0) {
page = createPageWithSampleWeightBlock(page, sampleWeightBlock);
}
pageBuffer.add(page);
}
@Override
public Collection<Slice> finish()
{
flushPages(pageBuffer.getPages());
List<ShardInfo> shards = storagePageSink.commit();
ImmutableList.Builder<Slice> fragments = ImmutableList.builder();
for (ShardInfo shard : shards) {
fragments.add(Slices.wrappedBuffer(shardInfoCodec.toJsonBytes(shard)));
}
return fragments.build();
}
@Override
public void abort()
{
storagePageSink.rollback();
}
/**
* @return page with the sampleWeightBlock at the sampleWeightField index
*/
private Page createPageWithSampleWeightBlock(Page page, Block sampleWeightBlock)
{
checkArgument(page.getPositionCount() == sampleWeightBlock.getPositionCount(), "position count of page and sampleWeightBlock must match");
int outputChannelCount = page.getChannelCount() + 1;
Block[] blocks = new Block[outputChannelCount];
blocks[sampleWeightField] = sampleWeightBlock;
int pageChannel = 0;
for (int channel = 0; channel < outputChannelCount; channel++) {
if (channel == sampleWeightField) {
continue;
}
blocks[channel] = page.getBlock(pageChannel);
pageChannel++;
}
return new Page(blocks);
}
private void flushPageBufferIfNecessary(int rowsToAdd)
{
if (shouldFlush(rowsToAdd)) {
flushPages(pageBuffer.getPages());
pageBuffer.reset();
storagePageSink.flush();
}
}
/**
* Flushes pages in the PageBuffer to StoragePageSink if ANY of the following is true:
* <ul>
* <li>rows written to the StoragePageSink >= maxRowsCount</li>
* <li>pageBuffer has maximum allowable bytes</li>
* <li>pageBuffer has more than Integer.MAX_VALUE rows (PagesSorter.sort can sort Integer.MAX_VALUE rows at a time)</li>
* </ul>
*/
private boolean shouldFlush(int rowsToAdd)
{
return storagePageSink.isFull() || !pageBuffer.canAddRows(rowsToAdd);
}
private void flushPages(List<Page> pages)
{
if (pages.isEmpty()) {
return;
}
if (sortFields.isEmpty()) {
storagePageSink.appendPages(pages);
}
else {
checkState(pageBuffer.getRowCount() <= Integer.MAX_VALUE);
long[] orderedAddresses = pageSorter.sort(columnTypes, pages, sortFields, sortOrders, Ints.checkedCast(pageBuffer.getRowCount()));
int[] orderedPageIndex = new int[orderedAddresses.length];
int[] orderedPositionIndex = new int[orderedAddresses.length];
for (int i = 0; i < orderedAddresses.length; i++) {
orderedPageIndex[i] = pageSorter.decodePageIndex(orderedAddresses[i]);
orderedPositionIndex[i] = pageSorter.decodePositionIndex(orderedAddresses[i]);
}
storagePageSink.appendPages(pages, orderedPageIndex, orderedPositionIndex);
}
}
}