-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
StandardRecordReader.java
292 lines (243 loc) · 12.4 KB
/
StandardRecordReader.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
/*
* 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.nifi.provenance;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
import org.apache.nifi.provenance.serialization.CompressableRecordReader;
import org.apache.nifi.provenance.toc.TocReader;
import org.apache.nifi.stream.io.StreamUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StandardRecordReader extends CompressableRecordReader {
public static final int SERIALIZATION_VERISON = 9;
public static final String SERIALIZATION_NAME = "org.apache.nifi.provenance.PersistentProvenanceRepository";
private static final Logger logger = LoggerFactory.getLogger(StandardRecordReader.class);
private static final Pattern UUID_PATTERN = Pattern.compile("[a-fA-F0-9]{8}\\-([a-fA-F0-9]{4}\\-){3}[a-fA-F0-9]{12}");
public StandardRecordReader(final InputStream in, final String filename, final int maxAttributeChars) throws IOException {
this(in, filename, null, maxAttributeChars);
}
public StandardRecordReader(final InputStream in, final String filename, final TocReader tocReader, final int maxAttributeChars) throws IOException {
super(in, filename, tocReader, maxAttributeChars);
logger.trace("Creating RecordReader for {}", filename);
}
private StandardProvenanceEventRecord readPreVersion6Record(final DataInputStream dis, final int serializationVersion) throws IOException {
final long startOffset = getBytesConsumed();
final StandardProvenanceEventRecord.Builder builder = new StandardProvenanceEventRecord.Builder();
final long eventId = dis.readLong();
if (serializationVersion == 4) {
// notion of a UUID for the event was added in Version 4 so that Events can be referred to uniquely
// across a cluster. This was then removed in version 5 because it was decided that a unique id
// could better be generated based on the event id and the cluster node identifier.
// Therefore, we read in the Event Identifier and throw it away.
dis.readUTF();
}
final String eventTypeName = dis.readUTF();
final ProvenanceEventType eventType = ProvenanceEventType.valueOf(eventTypeName);
builder.setEventType(eventType);
builder.setEventTime(dis.readLong());
if (serializationVersion > 3) {
// event duration introduced in version 4.
builder.setEventDuration(dis.readLong());
}
dis.readLong(); // Used to persist FlowFileId
final long fileSize = dis.readLong();
builder.setComponentId(readNullableString(dis));
builder.setComponentType(readNullableString(dis));
builder.setFlowFileUUID(readNullableString(dis));
final int numParents = dis.readInt();
for (int i = 0; i < numParents; i++) {
builder.addParentUuid(dis.readUTF());
}
if (serializationVersion > 2) {
// notion of child UUID's was introduced in version 3.
final int numChildren = dis.readInt();
for (int i = 0; i < numChildren; i++) {
builder.addChildUuid(dis.readUTF());
}
}
final String sourceSystemUri = readNullableString(dis);
if (serializationVersion > 3) {
// notion of a source system flowfile identifier was introduced in version 4.
builder.setSourceSystemFlowFileIdentifier(readNullableString(dis));
}
final String destinationSystemUri = readNullableString(dis);
if (sourceSystemUri != null) {
builder.setTransitUri(sourceSystemUri);
} else if (destinationSystemUri != null) {
builder.setTransitUri(destinationSystemUri);
}
readNullableString(dis); // Content-Type No longer used
builder.setAlternateIdentifierUri(readNullableString(dis));
final Map<String, String> attrs = readAttributes(dis, false);
builder.setFlowFileEntryDate(System.currentTimeMillis());
builder.setLineageStartDate(-1L);
builder.setAttributes(Collections.<String, String>emptyMap(), attrs);
builder.setCurrentContentClaim(null, null, null, null, fileSize);
builder.setStorageLocation(getFilename(), startOffset);
final StandardProvenanceEventRecord record = builder.build();
record.setEventId(eventId);
return record;
}
@Override
public StandardProvenanceEventRecord nextRecord(final DataInputStream dis, final int serializationVersion) throws IOException {
if (serializationVersion > SERIALIZATION_VERISON) {
throw new IllegalArgumentException("Unable to deserialize record because the version is "
+ serializationVersion + " and supported versions are 1-" + SERIALIZATION_VERISON);
}
// Schema changed drastically in version 6 so we created a new method to handle old records
if (serializationVersion < 6) {
return readPreVersion6Record(dis, serializationVersion);
}
final long startOffset = getBytesConsumed();
final StandardProvenanceEventRecord.Builder builder = new StandardProvenanceEventRecord.Builder();
final long eventId = dis.readLong();
final String eventTypeName = dis.readUTF();
final ProvenanceEventType eventType = ProvenanceEventType.valueOf(eventTypeName);
builder.setEventType(eventType);
builder.setEventTime(dis.readLong());
final Long flowFileEntryDate = dis.readLong();
builder.setEventDuration(dis.readLong());
if (serializationVersion < 9){
final int numLineageIdentifiers = dis.readInt();
for (int i = 0; i < numLineageIdentifiers; i++) {
readUUID(dis, serializationVersion); //skip identifiers
}
}
final long lineageStartDate = dis.readLong();
final long fileSize;
if (serializationVersion < 7) {
fileSize = dis.readLong(); // file size moved in version 7 to be with content claims
builder.setCurrentContentClaim(null, null, null, null, fileSize);
}
builder.setComponentId(readNullableString(dis));
builder.setComponentType(readNullableString(dis));
final String uuid = readUUID(dis, serializationVersion);
builder.setFlowFileUUID(uuid);
builder.setDetails(readNullableString(dis));
// Read in the FlowFile Attributes
if (serializationVersion >= 7) {
final Map<String, String> previousAttrs = readAttributes(dis, false);
final Map<String, String> attrUpdates = readAttributes(dis, true);
builder.setAttributes(previousAttrs, attrUpdates);
final boolean hasContentClaim = dis.readBoolean();
if (hasContentClaim) {
builder.setCurrentContentClaim(dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readLong(), dis.readLong());
} else {
builder.setCurrentContentClaim(null, null, null, null, 0L);
}
final boolean hasPreviousClaim = dis.readBoolean();
if (hasPreviousClaim) {
builder.setPreviousContentClaim(dis.readUTF(), dis.readUTF(), dis.readUTF(), dis.readLong(), dis.readLong());
}
builder.setSourceQueueIdentifier(readNullableString(dis));
} else {
final Map<String, String> attrs = readAttributes(dis, false);
builder.setAttributes(Collections.<String, String>emptyMap(), attrs);
}
// Read Event-Type specific fields.
if (eventType == ProvenanceEventType.FORK || eventType == ProvenanceEventType.JOIN || eventType == ProvenanceEventType.CLONE || eventType == ProvenanceEventType.REPLAY) {
final int numParents = dis.readInt();
for (int i = 0; i < numParents; i++) {
builder.addParentUuid(readUUID(dis, serializationVersion));
}
final int numChildren = dis.readInt();
for (int i = 0; i < numChildren; i++) {
builder.addChildUuid(readUUID(dis, serializationVersion));
}
} else if (eventType == ProvenanceEventType.RECEIVE) {
builder.setTransitUri(readNullableString(dis));
builder.setSourceSystemFlowFileIdentifier(readNullableString(dis));
} else if (eventType == ProvenanceEventType.FETCH) {
builder.setTransitUri(readNullableString(dis));
} else if (eventType == ProvenanceEventType.SEND) {
builder.setTransitUri(readNullableString(dis));
} else if (eventType == ProvenanceEventType.ADDINFO) {
builder.setAlternateIdentifierUri(readNullableString(dis));
} else if (eventType == ProvenanceEventType.ROUTE) {
builder.setRelationship(readNullableString(dis));
}
builder.setFlowFileEntryDate(flowFileEntryDate);
builder.setLineageStartDate(lineageStartDate);
builder.setStorageLocation(getFilename(), startOffset);
final StandardProvenanceEventRecord record = builder.build();
record.setEventId(eventId);
return record;
}
private Map<String, String> readAttributes(final DataInputStream dis, final boolean valueNullable) throws IOException {
final int numAttributes = dis.readInt();
final Map<String, String> attrs = new HashMap<>();
for (int i = 0; i < numAttributes; i++) {
final String key = readLongString(dis);
final String value = valueNullable ? readLongNullableString(dis) : readLongString(dis);
final String truncatedValue;
if (value == null) {
truncatedValue = null;
} else if (value.length() > getMaxAttributeLength()) {
truncatedValue = value.substring(0, getMaxAttributeLength());
} else {
truncatedValue = value;
}
attrs.put(key, truncatedValue);
}
return attrs;
}
private String readUUID(final DataInputStream in, final int serializationVersion) throws IOException {
if (serializationVersion < 8) {
final long msb = in.readLong();
final long lsb = in.readLong();
return new UUID(msb, lsb).toString();
} else {
// before version 8, we serialized UUID's as two longs in order to
// write less data. However, in version 8 we changed to just writing
// out the string because it's extremely expensive to call UUID.fromString.
// In the end, since we generally compress, the savings in minimal anyway.
final String uuid = in.readUTF();
if (!UUID_PATTERN.matcher(uuid).matches()) {
throw new IOException("Failed to parse Provenance Event Record: expected a UUID but got: " + uuid);
}
return uuid;
}
}
private String readNullableString(final DataInputStream in) throws IOException {
final boolean valueExists = in.readBoolean();
if (valueExists) {
return in.readUTF();
} else {
return null;
}
}
private String readLongNullableString(final DataInputStream in) throws IOException {
final boolean valueExists = in.readBoolean();
if (valueExists) {
return readLongString(in);
} else {
return null;
}
}
private String readLongString(final DataInputStream in) throws IOException {
final int length = in.readInt();
final byte[] strBytes = new byte[length];
StreamUtils.fillBuffer(in, strBytes);
return new String(strBytes, "UTF-8");
}
}