Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GOBBLIN-236] Add a ControlMessage injector as a RecordStreamProcessor #2090

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public class ConfigurationKeys {
public static final String TASK_DATA_ROOT_DIR_KEY = "task.data.root.dir";
public static final String SOURCE_CLASS_KEY = "source.class";
public static final String CONVERTER_CLASSES_KEY = "converter.classes";
public static final String RECORD_STREAM_PROCESSOR_CLASSES_KEY = "recordStreamProcessor.classes";
public static final String FORK_OPERATOR_CLASS_KEY = "fork.operator.class";
public static final String DEFAULT_FORK_OPERATOR_CLASS = "org.apache.gobblin.fork.IdentityForkOperator";
public static final String JOB_COMMIT_POLICY_KEY = "job.commit.policy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
import org.apache.gobblin.configuration.WorkUnitState;
import org.apache.gobblin.converter.initializer.ConverterInitializer;
import org.apache.gobblin.converter.initializer.NoopConverterInitializer;
import org.apache.gobblin.metadata.GlobalMetadata;
import org.apache.gobblin.stream.ControlMessage;
import org.apache.gobblin.records.ControlMessageHandler;
import org.apache.gobblin.records.RecordStreamProcessor;
import org.apache.gobblin.records.RecordStreamWithMetadata;
import org.apache.gobblin.stream.MetadataUpdateControlMessage;
import org.apache.gobblin.stream.RecordEnvelope;
import org.apache.gobblin.source.workunit.WorkUnitStream;
import org.apache.gobblin.stream.StreamEntity;
import org.apache.gobblin.util.FinalState;

import com.google.common.base.Optional;

import io.reactivex.Flowable;


Expand All @@ -55,6 +59,9 @@
* @param <DO> output data type
*/
public abstract class Converter<SI, SO, DI, DO> implements Closeable, FinalState, RecordStreamProcessor<SI, SO, DI, DO> {
// Metadata containing the output schema. This may be changed when a MetadataUpdateControlMessage is received.
private GlobalMetadata<SO> outputGlobalMetadata;

/**
* Initialize this {@link Converter}.
*
Expand Down Expand Up @@ -120,16 +127,29 @@ public State getFinalState() {
public RecordStreamWithMetadata<DO, SO> processStream(RecordStreamWithMetadata<DI, SI> inputStream,
WorkUnitState workUnitState) throws SchemaConversionException {
init(workUnitState);
SO outputSchema = convertSchema(inputStream.getSchema(), workUnitState);
this.outputGlobalMetadata = GlobalMetadata.<SI, SO>builderWithInput(inputStream.getGlobalMetadata(),
Optional.of(convertSchema(inputStream.getGlobalMetadata().getSchema(), workUnitState))).build();
Flowable<StreamEntity<DO>> outputStream =
inputStream.getRecordStream()
.flatMap(in -> {
if (in instanceof ControlMessage) {
ControlMessage out = (ControlMessage) in;

getMessageHandler().handleMessage((ControlMessage) in);
return Flowable.just(((ControlMessage<DO>) in));

// update the output schema with the new input schema from the MetadataUpdateControlMessage
if (in instanceof MetadataUpdateControlMessage) {
this.outputGlobalMetadata = GlobalMetadata.<SI, SO>builderWithInput(inputStream.getGlobalMetadata(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be GlobalMetadata.builderWithInput(in.getMetadata, Optional.of...)? (instead of inputStream)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently there are no other fields, so it either works. The original idea was to start with the inputStream GlobalMetadata and then overlay from in. But I think the suggestion of using in.getGlobalMetadata() is good since the copy will be handled in builderWithInput.

Optional.of(convertSchema((SI)((MetadataUpdateControlMessage) in).getGlobalMetadata()
.getSchema(), workUnitState))).build();
out = new MetadataUpdateControlMessage<SO, DO>(this.outputGlobalMetadata);
}

return Flowable.just(((ControlMessage<DO>) out));
} else if (in instanceof RecordEnvelope) {
RecordEnvelope<DI> recordEnvelope = (RecordEnvelope<DI>) in;
Iterator<DO> convertedIterable = convertRecord(outputSchema, recordEnvelope.getRecord(), workUnitState).iterator();
Iterator<DO> convertedIterable = convertRecord(this.outputGlobalMetadata.getSchema(),
recordEnvelope.getRecord(), workUnitState).iterator();

if (!convertedIterable.hasNext()) {
// if the iterable is empty, ack the record, return an empty flowable
Expand All @@ -153,7 +173,7 @@ public RecordStreamWithMetadata<DO, SO> processStream(RecordStreamWithMetadata<D
}
}, 1);
outputStream = outputStream.doOnComplete(this::close);
return inputStream.withRecordStream(outputStream, outputSchema);
return inputStream.withRecordStream(outputStream, this.outputGlobalMetadata);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions gobblin-api/src/main/java/org/apache/gobblin/fork/Forker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.apache.gobblin.configuration.ConfigurationKeys;
import org.apache.gobblin.configuration.WorkUnitState;
import org.apache.gobblin.metadata.GlobalMetadata;
import org.apache.gobblin.records.RecordStreamWithMetadata;
import org.apache.gobblin.stream.ControlMessage;
import org.apache.gobblin.stream.RecordEnvelope;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class Forker {
workUnitState.setProp(ConfigurationKeys.FORK_BRANCHES_KEY, branches);

forkOperator.init(workUnitState);
List<Boolean> forkedSchemas = forkOperator.forkSchema(workUnitState, inputStream.getSchema());
List<Boolean> forkedSchemas = forkOperator.forkSchema(workUnitState, inputStream.getGlobalMetadata().getSchema());
int activeForks = (int) forkedSchemas.stream().filter(b -> b).count();

Preconditions.checkState(forkedSchemas.size() == branches, String
Expand Down Expand Up @@ -90,7 +91,8 @@ public class Forker {
Flowable<StreamEntity<D>> thisStream =
forkedStream.filter(new ForkFilter<>(idx)).map(RecordWithForkMap::getRecordCopyIfNecessary);
forkStreams.add(inputStream.withRecordStream(thisStream,
mustCopy ? (S) CopyHelper.copy(inputStream.getSchema()) : inputStream.getSchema()));
mustCopy ? (GlobalMetadata<S>) CopyHelper.copy(inputStream.getGlobalMetadata()) :
inputStream.getGlobalMetadata()));
} else {
forkStreams.add(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.gobblin.metadata;

import org.apache.gobblin.fork.CopyHelper;
import org.apache.gobblin.fork.CopyNotSupportedException;
import org.apache.gobblin.fork.Copyable;

import com.google.common.base.Optional;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;


/**
* Global metadata
* @param <S> schema type
*/
@AllArgsConstructor(access=AccessLevel.PRIVATE)
@EqualsAndHashCode
@Builder
public class GlobalMetadata<S> implements Copyable<GlobalMetadata<S>> {
@Getter
private S schema;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to provide more functional methods:

/** Generate a copy of this object with a new schema. */
public GlobalMetadata<S> withSchema(S newSchema);

That way, when we add new attributes to GlobalMetadata, converters will require no change and still copy the correct metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, added a builder.

@Override
public GlobalMetadata<S> copy() throws CopyNotSupportedException {
if (CopyHelper.isCopyable(schema)) {
return new GlobalMetadata((S)CopyHelper.copy(schema));
}

throw new CopyNotSupportedException("Type is not copyable: " + schema.getClass().getName());
}

/**
* Builder that takes in an input {@GlobalMetadata} to use as a base.
* @param inputMetadata input metadata
* @param outputSchema output schema to set in the builder
* @param <SI> input schema type
* @param <SO> output schema type
* @return builder
*/
public static <SI, SO> GlobalMetadataBuilder<SO> builderWithInput(GlobalMetadata<SI> inputMetadata, Optional<SO> outputSchema) {
GlobalMetadataBuilder<SO> builder = (GlobalMetadataBuilder<SO>) builder();

if (outputSchema.isPresent()) {
builder.schema(outputSchema.get());
}

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.function.Function;

import org.apache.gobblin.metadata.GlobalMetadata;
import org.apache.gobblin.stream.RecordEnvelope;
import org.apache.gobblin.stream.StreamEntity;

Expand All @@ -34,20 +35,29 @@
@Data
public class RecordStreamWithMetadata<D, S> {
private final Flowable<StreamEntity<D>> recordStream;
private final S schema;
private final GlobalMetadata<S> globalMetadata;

/**
* @return a new {@link RecordStreamWithMetadata} with a different {@link #recordStream} but same schema.
*/
public <DO> RecordStreamWithMetadata<DO, S> withRecordStream(Flowable<StreamEntity<DO>> newRecordStream) {
return withRecordStream(newRecordStream, this.schema);
return withRecordStream(newRecordStream, this.globalMetadata);
}

/**
* @return a new {@link RecordStreamWithMetadata} with a different {@link #recordStream} and {@link #schema}.
* @return a new {@link RecordStreamWithMetadata} with a different {@link #recordStream} and {@link #globalMetadata}.
*/
@Deprecated
public <DO, SO> RecordStreamWithMetadata<DO, SO> withRecordStream(Flowable<StreamEntity<DO>> newRecordStream, SO newSchema) {
return new RecordStreamWithMetadata<>(newRecordStream, newSchema);
return new RecordStreamWithMetadata<>(newRecordStream, GlobalMetadata.<SO>builder().schema(newSchema).build());
}

/**
* @return a new {@link RecordStreamWithMetadata} with a different {@link #recordStream} and {@link #globalMetadata}.
*/
public <DO, SO> RecordStreamWithMetadata<DO, SO> withRecordStream(Flowable<StreamEntity<DO>> newRecordStream,
GlobalMetadata<SO> newGlobalMetadata) {
return new RecordStreamWithMetadata<>(newRecordStream, newGlobalMetadata);
}

/**
Expand All @@ -56,7 +66,7 @@ public <DO, SO> RecordStreamWithMetadata<DO, SO> withRecordStream(Flowable<Strea
*/
public <DO> RecordStreamWithMetadata<DO, S>
mapStream(Function<? super Flowable<StreamEntity<D>>, ? extends Flowable<StreamEntity<DO>>> transform) {
return new RecordStreamWithMetadata<>(transform.apply(this.recordStream), this.schema);
return new RecordStreamWithMetadata<>(transform.apply(this.recordStream), this.globalMetadata);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.gobblin.metadata.GlobalMetadata;
import org.apache.gobblin.records.RecordStreamWithMetadata;
import org.apache.gobblin.stream.RecordEnvelope;
import org.apache.gobblin.stream.StreamEntity;
Expand Down Expand Up @@ -128,7 +129,7 @@ default RecordStreamWithMetadata<D, S> recordStream(AtomicBoolean shutdownReques
}
});
recordStream = recordStream.doFinally(this::close);
return new RecordStreamWithMetadata<>(recordStream, schema);
return new RecordStreamWithMetadata<>(recordStream, GlobalMetadata.<S>builder().schema(schema).build());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.gobblin.stream;

import java.io.Closeable;
import java.io.IOException;

import org.apache.gobblin.configuration.WorkUnitState;
import org.apache.gobblin.metadata.GlobalMetadata;
import org.apache.gobblin.records.ControlMessageHandler;
import org.apache.gobblin.records.RecordStreamProcessor;
import org.apache.gobblin.records.RecordStreamWithMetadata;

import io.reactivex.Flowable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

/**
* A {@link RecordStreamProcessor} that inspects an input record and outputs control messages before, after, or around
* the input record
* @param <SI>
* @param <DI>
*/
public abstract class ControlMessageInjector<SI, DI> implements Closeable,
RecordStreamProcessor<SI, SI, DI, DI> {

@Setter(AccessLevel.PROTECTED)
@Getter(AccessLevel.PROTECTED)
private GlobalMetadata<SI> inputGlobalMetadata;

/**
* Initialize this {@link ControlMessageInjector}.
*
* @param workUnitState a {@link WorkUnitState} object carrying configuration properties
* @return an initialized {@link ControlMessageInjector} instance
*/
protected ControlMessageInjector<SI, DI> init(WorkUnitState workUnitState) {
return this;
}

@Override
public void close() throws IOException {
}

/**
* Set the global metadata of the input messages
* @param inputGlobalMetadata the global metadata for input messages
* @param workUnitState
*/
protected void setInputGlobalMetadata(GlobalMetadata<SI> inputGlobalMetadata, WorkUnitState workUnitState) {
this.inputGlobalMetadata = inputGlobalMetadata;
}

/**
* Inject {@link ControlMessage}s before the record
* @param inputRecordEnvelope
* @param workUnitState
* @return The {@link ControlMessage}s to inject before the record
*/
protected abstract Iterable<ControlMessage<DI>> injectControlMessagesBefore(RecordEnvelope<DI> inputRecordEnvelope,
WorkUnitState workUnitState);

/**
* Inject {@link ControlMessage}s after the record
* @param inputRecordEnvelope
* @param workUnitState
* @return The {@link ControlMessage}s to inject after the record
*/
protected abstract Iterable<ControlMessage<DI>> injectControlMessagesAfter(RecordEnvelope<DI> inputRecordEnvelope,
WorkUnitState workUnitState);

/**
* Apply injections to the input {@link RecordStreamWithMetadata}.
* {@link ControlMessage}s may be injected before, after, or around the input record.
* A {@link MetadataUpdateControlMessage} will update the current input {@link GlobalMetadata} and pass the
* updated input {@link GlobalMetadata} to the next processor to propagate the metadata update down the pipeline.
*/
@Override
public RecordStreamWithMetadata<DI, SI> processStream(RecordStreamWithMetadata<DI, SI> inputStream,
WorkUnitState workUnitState) throws StreamProcessingException {
init(workUnitState);

setInputGlobalMetadata(inputStream.getGlobalMetadata(), workUnitState);

Flowable<StreamEntity<DI>> outputStream =
inputStream.getRecordStream()
.flatMap(in -> {
if (in instanceof ControlMessage) {
ControlMessage out = (ControlMessage) in;
if (in instanceof MetadataUpdateControlMessage) {
setInputGlobalMetadata(((MetadataUpdateControlMessage) in).getGlobalMetadata(),
workUnitState);
out = new MetadataUpdateControlMessage<SI, DI>(this.inputGlobalMetadata);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need a new output message? It seems to be identical to the input message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setInputGlobalMetadata() can set this.inputGlobalMetadata to something other than the input metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed inputGlobalMetadata from this class so that there is no confusion over the propagation of metadata. A ControlMessageInjector can store a modified copy of the metadata, but it won't be able to pass that change along to the next construct. Only converters should change schemas.

}

getMessageHandler().handleMessage((ControlMessage) in);
return Flowable.just(((ControlMessage<DI>) out));

} else if (in instanceof RecordEnvelope) {
RecordEnvelope<DI> recordEnvelope = (RecordEnvelope<DI>) in;
Iterable<ControlMessage<DI>> injectedBeforeIterable =
injectControlMessagesBefore(recordEnvelope, workUnitState);
Iterable<ControlMessage<DI>> injectedAfterIterable =
injectControlMessagesAfter(recordEnvelope, workUnitState);

if (injectedBeforeIterable == null && injectedAfterIterable == null) {
// nothing injected so return the record envelope
return Flowable.just(recordEnvelope);
} else {
Flowable<StreamEntity<DI>> flowable;

if (injectedBeforeIterable != null) {
flowable = Flowable.<StreamEntity<DI>>fromIterable(injectedBeforeIterable)
.concatWith(Flowable.just(recordEnvelope));
} else {
flowable = Flowable.just(recordEnvelope);
}

if (injectedAfterIterable != null) {
flowable.concatWith(Flowable.fromIterable(injectedAfterIterable));
}
return flowable;
}
} else {
throw new UnsupportedOperationException();
}
}, 1);
outputStream = outputStream.doOnComplete(this::close);
return inputStream.withRecordStream(outputStream, this.inputGlobalMetadata);
}

/**
* @return {@link ControlMessageHandler} to call for each {@link ControlMessage} received.
*/
protected ControlMessageHandler getMessageHandler() {
return ControlMessageHandler.NOOP;
}
}