forked from voldemort/voldemort
/
HadoopStoreWriter.java
321 lines (256 loc) · 12.3 KB
/
HadoopStoreWriter.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package voldemort.store.readonly.disk;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.Reporter;
import org.apache.log4j.Logger;
import voldemort.VoldemortException;
import voldemort.cluster.Cluster;
import voldemort.store.StoreDefinition;
import voldemort.store.readonly.ReadOnlyUtils;
import voldemort.store.readonly.checksum.CheckSum;
import voldemort.store.readonly.checksum.CheckSum.CheckSumType;
import voldemort.utils.ByteUtils;
import voldemort.xml.ClusterMapper;
import voldemort.xml.StoreDefinitionsMapper;
public class HadoopStoreWriter implements KeyValueWriter<BytesWritable, BytesWritable> {
private static final Logger logger = Logger.getLogger(HadoopStoreWriter.class);
private DataOutputStream indexFileStream = null;
private DataOutputStream valueFileStream = null;
private int position;
private String taskId = null;
private int nodeId = -1;
private int partitionId = -1;
private int chunkId = -1;
private int replicaType = -1;
private Path taskIndexFileName;
private Path taskValueFileName;
private JobConf conf;
private CheckSumType checkSumType;
private CheckSum checkSumDigestIndex;
private CheckSum checkSumDigestValue;
private String outputDir;
private FileSystem fs;
private int numChunks;
private Cluster cluster;
private StoreDefinition storeDef;
private boolean saveKeys;
private boolean reducerPerBucket;
public Cluster getCluster() {
checkNotNull(cluster);
return cluster;
}
public boolean getSaveKeys() {
return this.saveKeys;
}
public boolean getReducerPerBucket() {
return this.reducerPerBucket;
}
public StoreDefinition getStoreDef() {
checkNotNull(storeDef);
return storeDef;
}
public String getStoreName() {
checkNotNull(storeDef);
return storeDef.getName();
}
private final void checkNotNull(Object o) {
if(o == null)
throw new VoldemortException("Not configured yet!");
}
public int getNumChunks() {
return this.numChunks;
}
@Override
public void conf(JobConf job) {
conf = job;
try {
this.cluster = new ClusterMapper().readCluster(new StringReader(conf.get("cluster.xml")));
List<StoreDefinition> storeDefs = new StoreDefinitionsMapper().readStoreList(new StringReader(conf.get("stores.xml")));
if(storeDefs.size() != 1)
throw new IllegalStateException("Expected to find only a single store, but found multiple!");
this.storeDef = storeDefs.get(0);
this.numChunks = conf.getInt("num.chunks", -1);
if(this.numChunks < 1)
throw new VoldemortException("num.chunks not specified in the job conf.");
this.saveKeys = conf.getBoolean("save.keys", false);
this.reducerPerBucket = conf.getBoolean("reducer.per.bucket", false);
this.conf = job;
this.position = 0;
this.outputDir = job.get("final.output.dir");
this.taskId = job.get("mapred.task.id");
this.checkSumType = CheckSum.fromString(job.get("checksum.type"));
this.checkSumDigestIndex = CheckSum.getInstance(checkSumType);
this.checkSumDigestValue = CheckSum.getInstance(checkSumType);
this.taskIndexFileName = new Path(FileOutputFormat.getOutputPath(job), getStoreName()
+ "."
+ this.taskId
+ ".index");
this.taskValueFileName = new Path(FileOutputFormat.getOutputPath(job), getStoreName()
+ "."
+ this.taskId
+ ".data");
if(this.fs == null)
this.fs = this.taskIndexFileName.getFileSystem(job);
this.indexFileStream = fs.create(this.taskIndexFileName);
this.valueFileStream = fs.create(this.taskValueFileName);
logger.info("Opening " + this.taskIndexFileName + " and " + this.taskValueFileName
+ " for writing.");
} catch(IOException e) {
throw new RuntimeException("Failed to open Input/OutputStream", e);
}
}
@Override
public void write(BytesWritable key, Iterator<BytesWritable> iterator, Reporter reporter)
throws IOException {
// Write key and position
this.indexFileStream.write(key.get(), 0, key.getSize());
this.indexFileStream.writeInt(this.position);
// Run key through checksum digest
if(this.checkSumDigestIndex != null) {
this.checkSumDigestIndex.update(key.get(), 0, key.getSize());
this.checkSumDigestIndex.update(this.position);
}
short numTuples = 0;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream valueStream = new DataOutputStream(stream);
while(iterator.hasNext()) {
BytesWritable writable = iterator.next();
byte[] valueBytes = writable.get();
int offsetTillNow = 0;
// Read node Id
if(this.nodeId == -1)
this.nodeId = ByteUtils.readInt(valueBytes, offsetTillNow);
offsetTillNow += ByteUtils.SIZE_OF_INT;
// Read partition id
if(this.partitionId == -1)
this.partitionId = ByteUtils.readInt(valueBytes, offsetTillNow);
offsetTillNow += ByteUtils.SIZE_OF_INT;
// Read chunk id
if(this.chunkId == -1)
this.chunkId = ReadOnlyUtils.chunk(key.get(), getNumChunks());
// Read replica type
if(getSaveKeys()) {
if(this.replicaType == -1)
this.replicaType = (int) ByteUtils.readBytes(valueBytes,
offsetTillNow,
ByteUtils.SIZE_OF_BYTE);
offsetTillNow += ByteUtils.SIZE_OF_BYTE;
}
int valueLength = writable.getSize() - offsetTillNow;
if(getSaveKeys()) {
// Write ( key_length, value_length, key,
// value )
valueStream.write(valueBytes, offsetTillNow, valueLength);
} else {
// Write (value_length + value)
valueStream.writeInt(valueLength);
valueStream.write(valueBytes, offsetTillNow, valueLength);
}
numTuples++;
// If we have multiple values for this md5 that is a collision,
// throw an exception--either the data itself has duplicates, there
// are trillions of keys, or someone is attempting something
// malicious ( We obviously expect collisions when we save keys )
if(!getSaveKeys() && numTuples > 1)
throw new VoldemortException("Duplicate keys detected for md5 sum "
+ ByteUtils.toHexString(ByteUtils.copy(key.get(),
0,
key.getSize())));
}
if(numTuples < 0) {
// Overflow
throw new VoldemortException("Found too many collisions: chunk " + chunkId
+ " has exceeded " + Short.MAX_VALUE + " collisions.");
} else if(numTuples > 1) {
// Update number of collisions + max keys per collision
reporter.incrCounter(CollisionCounter.NUM_COLLISIONS, 1);
long numCollisions = reporter.getCounter(CollisionCounter.MAX_COLLISIONS).getCounter();
if(numTuples > numCollisions) {
reporter.incrCounter(CollisionCounter.MAX_COLLISIONS, numTuples - numCollisions);
}
}
// Flush the value
valueStream.flush();
byte[] value = stream.toByteArray();
// Start writing to file now
// First, if save keys flag set the number of keys
if(getSaveKeys()) {
this.valueFileStream.writeShort(numTuples);
this.position += ByteUtils.SIZE_OF_SHORT;
if(this.checkSumDigestValue != null) {
this.checkSumDigestValue.update(numTuples);
}
}
this.valueFileStream.write(value);
this.position += value.length;
if(this.checkSumDigestValue != null) {
this.checkSumDigestValue.update(value);
}
if(this.position < 0)
throw new VoldemortException("Chunk overflow exception: chunk " + chunkId
+ " has exceeded " + Integer.MAX_VALUE + " bytes.");
}
@Override
public void close() throws IOException {
this.indexFileStream.close();
this.valueFileStream.close();
if(this.nodeId == -1 || this.chunkId == -1 || this.partitionId == -1) {
// Issue 258 - No data was read in the reduce phase, do not create
// any output
return;
}
// If the replica type read was not valid, shout out
if(getSaveKeys() && this.replicaType == -1) {
throw new RuntimeException("Could not read the replica type correctly for node "
+ nodeId + " ( partition - " + this.partitionId + " )");
}
String fileNamePrefix = null;
if(getSaveKeys()) {
fileNamePrefix = new String(Integer.toString(this.partitionId) + "_"
+ Integer.toString(this.replicaType) + "_"
+ Integer.toString(this.chunkId));
} else {
fileNamePrefix = new String(Integer.toString(this.partitionId) + "_"
+ Integer.toString(this.chunkId));
}
// Initialize the node directory
Path nodeDir = new Path(this.outputDir, "node-" + this.nodeId);
// Create output directory, if it doesn't exist
FileSystem outputFs = nodeDir.getFileSystem(this.conf);
outputFs.mkdirs(nodeDir);
// Write the checksum and output files
if(this.checkSumType != CheckSumType.NONE) {
if(this.checkSumDigestIndex != null && this.checkSumDigestValue != null) {
Path checkSumIndexFile = new Path(nodeDir, fileNamePrefix + ".index.checksum");
Path checkSumValueFile = new Path(nodeDir, fileNamePrefix + ".data.checksum");
FSDataOutputStream output = outputFs.create(checkSumIndexFile);
output.write(this.checkSumDigestIndex.getCheckSum());
output.close();
output = outputFs.create(checkSumValueFile);
output.write(this.checkSumDigestValue.getCheckSum());
output.close();
} else {
throw new RuntimeException("Failed to open checksum digest for node " + nodeId
+ " ( partition - " + this.partitionId + ", chunk - "
+ chunkId + " )");
}
}
// Generate the final chunk files
Path indexFile = new Path(nodeDir, fileNamePrefix + ".index");
Path valueFile = new Path(nodeDir, fileNamePrefix + ".data");
logger.info("Moving " + this.taskIndexFileName + " to " + indexFile);
outputFs.rename(taskIndexFileName, indexFile);
logger.info("Moving " + this.taskValueFileName + " to " + valueFile);
outputFs.rename(this.taskValueFileName, valueFile);
}
}