/
PartitionedDatasetWriter.java
204 lines (162 loc) · 6.34 KB
/
PartitionedDatasetWriter.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Copyright 2013 Cloudera Inc.
*
* 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.cloudera.data.filesystem;
import com.cloudera.data.Dataset;
import com.cloudera.data.DatasetWriter;
import com.cloudera.data.DatasetWriterException;
import com.cloudera.data.PartitionKey;
import com.cloudera.data.PartitionStrategy;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;
class PartitionedDatasetWriter<E> implements DatasetWriter<E>, Closeable {
private static final Logger logger = LoggerFactory
.getLogger(PartitionedDatasetWriter.class);
private Dataset dataset;
private int maxWriters;
private final PartitionStrategy partitionStrategy;
private LoadingCache<PartitionKey, DatasetWriter<E>> cachedWriters;
private PartitionKey key;
private ReaderWriterState state;
public PartitionedDatasetWriter(Dataset dataset) {
Preconditions.checkArgument(dataset.getDescriptor().isPartitioned(),
"Dataset " + dataset + " is not partitioned");
this.dataset = dataset;
this.partitionStrategy = dataset.getDescriptor().getPartitionStrategy();
this.maxWriters = Math.min(10, partitionStrategy.getCardinality());
this.state = ReaderWriterState.NEW;
}
@Deprecated
public PartitionedDatasetWriter(Dataset dataset,
PartitionStrategy partitionStrategy) {
Preconditions.checkArgument(dataset.getDescriptor().isPartitioned(),
"Dataset " + dataset + " is not partitioned");
Preconditions
.checkArgument(
dataset.getDescriptor().getPartitionStrategy()
.equals(partitionStrategy),
"Dataset descriptor's partitions strategy doesn't match the provided partition strategy");
this.dataset = dataset;
this.partitionStrategy = dataset.getDescriptor().getPartitionStrategy();
this.maxWriters = Math.min(10, this.partitionStrategy.getCardinality());
this.state = ReaderWriterState.NEW;
}
@Override
public void open() {
Preconditions.checkState(state.equals(ReaderWriterState.NEW),
"Unable to open a writer from state:%s", state);
logger.debug("Opening partitioned dataset writer w/strategy:{}",
partitionStrategy);
cachedWriters = CacheBuilder.newBuilder().maximumSize(maxWriters)
.removalListener(new DatasetWriterRemovalStrategy<E>())
.build(new DatasetWriterCacheLoader<E>(dataset));
state = ReaderWriterState.OPEN;
}
@Override
public void write(E entity) {
Preconditions.checkState(state.equals(ReaderWriterState.OPEN),
"Attempt to write to a writer in state:%s", state);
key = partitionStrategy.partitionKeyForEntity(entity, key);
DatasetWriter<E> writer;
try {
writer = cachedWriters.get(key);
} catch (ExecutionException e) {
throw new DatasetWriterException("Unable to get a writer for entity:" + entity
+ " partition key:" + Arrays.asList(key), e);
}
writer.write(entity);
}
@Override
public void flush() {
Preconditions.checkState(state.equals(ReaderWriterState.OPEN),
"Attempt to write to a writer in state:%s", state);
logger.debug("Flushing all cached writers for partition strategy:{}",
partitionStrategy);
/*
* There's a potential for flushing entries that are created by other
* threads while looping through the writers. While normally just wasteful,
* on HDFS, this is particularly bad. We should probably do something about
* this, but it will be difficult as Cache (ideally) uses multiple
* partitions to prevent cached writer contention.
*/
for (Map.Entry<PartitionKey, DatasetWriter<E>> entry : cachedWriters
.asMap().entrySet()) {
logger.debug("Flushing partition writer:{}.{}", entry.getKey(),
entry.getValue());
entry.getValue().flush();
}
}
@Override
public void close() {
if (state.equals(ReaderWriterState.OPEN)) {
logger.debug("Closing all cached writers for partition strategy:{}",
partitionStrategy);
for (Map.Entry<PartitionKey, DatasetWriter<E>> entry : cachedWriters
.asMap().entrySet()) {
logger.debug("Closing partition writer:{}.{}", entry.getKey(),
entry.getValue());
entry.getValue().close();
}
state = ReaderWriterState.CLOSED;
}
}
@Override
public boolean isOpen() {
return state.equals(ReaderWriterState.OPEN);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("partitionStrategy", partitionStrategy)
.add("maxWriters", maxWriters).add("dataset", dataset)
.add("cachedWriters", cachedWriters).toString();
}
private static class DatasetWriterCacheLoader<E> extends
CacheLoader<PartitionKey, DatasetWriter<E>> {
private Dataset dataset;
public DatasetWriterCacheLoader(Dataset dataset) {
this.dataset = dataset;
}
@Override
public DatasetWriter<E> load(PartitionKey key) throws Exception {
Dataset partition = dataset.getPartition(key, true);
DatasetWriter<E> writer = partition.getWriter();
writer.open();
return writer;
}
}
private static class DatasetWriterRemovalStrategy<E> implements
RemovalListener<PartitionKey, DatasetWriter<E>> {
@Override
public void onRemoval(
RemovalNotification<PartitionKey, DatasetWriter<E>> notification) {
DatasetWriter<E> writer = notification.getValue();
logger.debug("Removing writer:{} for partition:{}", writer,
notification.getKey());
writer.close();
}
}
}