Skip to content

Commit

Permalink
SDC-3457. Add Whole File Format.
Browse files Browse the repository at this point in the history
Introduces Whole File Data Format along with Data parser and Data Generator.

Whole File Data Parser:
  The Whole file Data Parser creates a record for each file with a FileRef
Field and a metadata field for each file, so each record parsed is one for
single file

Whole file Data Generator:
   The Whole File Data Generator uses the record created by the Whole File Data
Parser which contains information about the file and the File Stream Ref for
creating a stream for reading, the generator treats one record as a whole file
and transfer it in one shot (internally buffering and writing).

Any errors during this operation will have to restarted from scratch, this
indicates file transfer will not have a feature of resumability.

Error handling using different generator format:
	Most of the existing data generator formats (TEXT, BINARY, DELIMITED)
use getValueAs function which will by default fail during fileRef conversion.
For Avro, Protobuf there is an augumented which will make sure the conversion
fails.

Metrics:
 	The target will produce two metrics to keep track of the file transfer
progress.
	1) File Tranfer rate (bytes/sec)
	2) Remaining bytes to be transferred.

This patch also edits the existing sources to add this as a dummy group
as the data format value is not in the list for supported data format
chooser values.

Upgrade:
   Given that all are new configurations introduced with default values,
there is not an upgrade impact.

Checksum Verification:
        The target when it reads the file also calculates the checksum and
compare it with the checksum (if provided) in the file metadata of the record
If the source system does not support checksum verification the record will be
an error record.
If the source system automatically verifies the checksum on file open the
verifyChecksum configuration is ignored.
If the source system provides the checksum in the file metadata and does
not automatically verify the checksum, the target when it reads the file
compares thc checksum during the stream close call.

This patch also edits the existing sources to add this as a dummy group
as the data format value is not in the list for supported data format
chooser values.

Change-Id: I2b667f76f93fd80c4b01893183297134a58f9e41
Reviewed-on: https://review.streamsets.net/3722
Tested-by: StreamSets CI <streamsets-ci-spam@streamsets.com>
Reviewed-by: Alejandro Abdelnur <tucu@streamsets.com>
  • Loading branch information
santhoshkumarml authored and Adam Kunicki committed Aug 4, 2016
1 parent 1881b93 commit d6fac73
Show file tree
Hide file tree
Showing 56 changed files with 2,176 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public enum FirehoseGroups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")

DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public enum Groups implements Label {
BINARY("Binary"),
PROTOBUF("Protobuf"),
ADVANCED("Advanced"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")

DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public enum Groups implements Label {
AVRO("Avro"),
BINARY("Binary"),
PROTOBUF("Protobuf"),
DATAGRAM("Datagram")
DATAGRAM("Datagram"),
WHOLE_FILE("Whole File"),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public enum DataFormat implements Label {
BINARY("Binary", DataParserFormat.BINARY, DataGeneratorFormat.BINARY),
PROTOBUF("Protobuf", DataParserFormat.PROTOBUF, DataGeneratorFormat.PROTOBUF),
DATAGRAM("Datagram", DataParserFormat.DATAGRAM, null),
WHOLE_FILE("Whole File", DataParserFormat.WHOLE_FILE, DataGeneratorFormat.WHOLE_FILE),
;

private final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.streamsets.pipeline.lib.generator.protobuf.ProtobufDataGeneratorFactory;
import com.streamsets.pipeline.lib.generator.sdcrecord.SdcRecordDataGeneratorFactory;
import com.streamsets.pipeline.lib.generator.text.TextDataGeneratorFactory;
import com.streamsets.pipeline.lib.generator.wholefile.WholeFileDataGeneratorFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
Expand All @@ -45,6 +46,7 @@ public enum DataGeneratorFormat implements DataFormat<DataGeneratorFactory> {
AVRO(AvroDataGeneratorFactory.class, AvroDataGeneratorFactory.MODES, AvroDataGeneratorFactory.CONFIGS),
BINARY(BinaryDataGeneratorFactory.class, BinaryDataGeneratorFactory.MODES, BinaryDataGeneratorFactory.CONFIGS),
PROTOBUF(ProtobufDataGeneratorFactory.class, ProtobufDataGeneratorFactory.MODES, ProtobufDataGeneratorFactory.CONFIGS),
WHOLE_FILE(WholeFileDataGeneratorFactory.class, WholeFileDataGeneratorFactory.MODES, WholeFileDataGeneratorFactory.CONFIGS),
;

private final Class<? extends DataGeneratorFactory> klass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
@GenerateResourceBundle
public enum Errors implements ErrorCode {
JSON_GENERATOR_00("Record '{}' root field should be List instead of '{}'"),
JSON_GENERATOR_01("Record Field '{}' of type Field Ref is unsupported for json serialization"),

;

private final String msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public static Object fieldToJsonObject(Record record, Field field) throws DataGe
Object obj;
if (field == null || field.getValue() == null) {
obj = null;
} else if(field.getType() == Field.Type.FILE_REF) {
throw new DataGeneratorException(Errors.JSON_GENERATOR_01, field);
} else if (field.getType() == Field.Type.LIST) {
List<Field> list = field.getValueAsList();
List<Object> toReturn = new ArrayList<>(list.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright 2016 StreamSets Inc.
*
* Licensed under 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 com.streamsets.pipeline.lib.generator.wholefile;

import com.streamsets.pipeline.api.ErrorCode;

public enum Errors implements ErrorCode {
WHOLE_FILE_GENERATOR_ERROR_0("Record does not contain the mandatory field {} for Whole File Format."),
;

private final String msg;

Errors(String msg) {
this.msg = msg;
}

@Override
public String getCode() {
return name();
}

@Override
public String getMessage() {
return msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright 2016 StreamSets Inc.
*
* Licensed under 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 com.streamsets.pipeline.lib.generator.wholefile;

import com.google.common.collect.ImmutableList;
import com.streamsets.pipeline.api.FileRef;
import com.streamsets.pipeline.api.Record;
import com.streamsets.pipeline.api.Stage;
import com.streamsets.pipeline.lib.generator.DataGenerator;
import com.streamsets.pipeline.lib.generator.DataGeneratorException;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.List;

public class WholeFileDataGenerator implements DataGenerator {
private static final String FILE_REF = "/fileRef";
private static final String FILE_INFO = "/fileInfo";
private static final List<String> MANDATORY_FIELD_PATHS = ImmutableList.of(FILE_REF, FILE_INFO);
private final Stage.Context context;
private final OutputStream outputStream;

WholeFileDataGenerator(
Stage.Context context,
OutputStream os
) throws IOException {
this.context = context;
this.outputStream = os;
}

private void validateRecord(Record record) throws DataGeneratorException {
for (String fieldPath : MANDATORY_FIELD_PATHS) {
if (!record.has(fieldPath)) {
throw new DataGeneratorException(Errors.WHOLE_FILE_GENERATOR_ERROR_0, fieldPath);
}
}
}

@Override
public void write(Record record) throws IOException, DataGeneratorException {
validateRecord(record);
FileRef fileRef = record.get(FILE_REF).getValueAsFileRef();
int bufferSize = fileRef.getBufferSize();
boolean canUseDirectByteBuffer = fileRef.getSupportedStreamClasses().contains(ReadableByteChannel.class);
if (canUseDirectByteBuffer) {
WritableByteChannel writableByteChannel = Channels.newChannel(outputStream);
try (ReadableByteChannel readableByteChannel = fileRef.createInputStream(context, ReadableByteChannel.class)){
ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
while ((readableByteChannel.read(buffer)) > 0) {
//Flip to use the buffer from 0 to position.
buffer.flip();
while (buffer.hasRemaining()) {
writableByteChannel.write(buffer);
}
//Compact the buffer for reuse.
buffer.clear();
}
}
} else {
byte[] b = new byte[bufferSize];
try (InputStream stream = fileRef.createInputStream(context, InputStream.class)) {
IOUtils.copyLarge(stream, outputStream, b);
}
}
}

@Override
public void flush() throws IOException {
outputStream.flush();
}

@Override
public void close() throws IOException {
outputStream.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2016 StreamSets Inc.
*
* Licensed under 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 com.streamsets.pipeline.lib.generator.wholefile;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.streamsets.pipeline.lib.generator.DataGenerator;
import com.streamsets.pipeline.lib.generator.DataGeneratorFactory;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;

public class WholeFileDataGeneratorFactory extends DataGeneratorFactory {
public static final Map<String, Object> CONFIGS = ImmutableMap.of();
public static final Set<Class<? extends Enum>> MODES = ImmutableSet.of();

public WholeFileDataGeneratorFactory(Settings settings) {
super(settings);
}

@Override
public DataGenerator getGenerator(OutputStream os) throws IOException {
return new WholeFileDataGenerator(getSettings().getContext(), os);
}
}
Loading

0 comments on commit d6fac73

Please sign in to comment.