Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 100 additions & 4 deletions core/src/main/java/feast/core/job/dataflow/DataflowJobManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
import feast.proto.core.StoreProto;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.beam.runners.dataflow.DataflowPipelineJob;
Expand All @@ -61,6 +66,15 @@ public class DataflowJobManager implements JobManager {
private final MetricsProperties metrics;
private final IngestionJobProto.SpecsStreamingUpdateConfig specsStreamingUpdateConfig;

// For Dataflow jobs with multiple FeatureSets with same label key, the label values from
// different FeatureSets will be joined by JOB_LABEL_VALUE_JOIN_CHAR character
static final String JOB_LABEL_VALUE_JOIN_CHAR = "_";
// Label key and value must start with a lowercase letter and contains only letters, numbers,
// dashes or underscores. The total length of label key or value is at most 63 characters.
// Adapted from:
// https://cloud.google.com/resource-manager/docs/creating-managing-labels#requirements
static final Pattern JOB_LABEL_VALID_PATTERN = Pattern.compile("^[a-z][a-z0-9-_]{0,62}$");

public DataflowJobManager(
DataflowRunnerConfigOptions runnerConfigOptions,
MetricsProperties metricsProperties,
Expand Down Expand Up @@ -114,14 +128,16 @@ public Runner getRunnerType() {
@Override
public Job startJob(Job job) {
try {
Map<String, String> labels = getJobLabels(job.getFeatureSets());
String extId =
submitDataflowJob(
job.getId(),
job.getSource().toProto(),
job.getStores().stream()
.map(wrapException(Store::toProto))
.collect(Collectors.toSet()),
false);
false,
labels);
job.setExtId(extId);
job.setStatus(JobStatus.RUNNING);
return job;
Expand Down Expand Up @@ -232,9 +248,13 @@ public JobStatus getJobStatus(Job job) {
}

private String submitDataflowJob(
String jobName, SourceProto.Source source, Set<StoreProto.Store> sinks, boolean update) {
String jobName,
SourceProto.Source source,
Set<StoreProto.Store> sinks,
boolean update,
Map<String, String> labels) {
try {
ImportOptions pipelineOptions = getPipelineOptions(jobName, source, sinks, update);
ImportOptions pipelineOptions = getPipelineOptions(jobName, source, sinks, update, labels);
DataflowPipelineJob pipelineResult = runPipeline(pipelineOptions);
String jobId = waitForJobToRun(pipelineResult);
return jobId;
Expand All @@ -245,7 +265,11 @@ private String submitDataflowJob(
}

private ImportOptions getPipelineOptions(
String jobName, SourceProto.Source source, Set<StoreProto.Store> sinks, boolean update)
String jobName,
SourceProto.Source source,
Set<StoreProto.Store> sinks,
boolean update,
Map<String, String> labels)
throws IOException, IllegalAccessException {
ImportOptions pipelineOptions =
PipelineOptionsFactory.fromArgs(defaultOptions.toArgs()).as(ImportOptions.class);
Expand All @@ -262,6 +286,7 @@ private ImportOptions getPipelineOptions(
pipelineOptions.setUpdate(update);
pipelineOptions.setRunner(DataflowRunner.class);
pipelineOptions.setJobName(jobName);
pipelineOptions.setLabels(labels);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Cmiiw, but this overwrites all labels that could be set in application properties. Shouldn't we extend them instead?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes I think we should extend them. Thanks for checking

pipelineOptions.setFilesToStage(
detectClassPathResourcesToStage(DataflowRunner.class.getClassLoader()));
if (metrics.isEnabled()) {
Expand Down Expand Up @@ -298,4 +323,75 @@ private String waitForJobToRun(DataflowPipelineJob pipelineResult)
Thread.sleep(2000);
}
}

/**
* GetJobLabels returns a key, value map of labels that should be applied to the DataflowJob.
*
* <p>A Dataflow job can ingest data from multiple FeatureSets. Each FeatureSet can have its own
* key, value labels. When multiple FeatureSets have the same label key, the label value for the
* DataflowJob will be the concatenation of all the FeatureSets label values with the same key.
*
* <p>The label key and value for the Dataflow job will follow the requirements defined here:
* https://cloud.google.com/resource-manager/docs/creating-managing-labels#requirements. If the
* label does not fulfill the requirements, the label will be ignored rather than errors being
* thrown. This is because invalid labels should not result in failed Dataflow jobs and disruption
* to the ingestion of feature data.
*
* <p>Warning logs from Feast Core can be inspected to debug cases where expected labels for
* Dataflow jobs are not applied.
*
* @param featureSets FeatureSets that the DataflowJob expects to ingest from
* @return Labels that should be applied to the Dataflow job
*/
private Map<String, String> getJobLabels(Collection<FeatureSet> featureSets) {
Comment thread
davidheryanto marked this conversation as resolved.
Resolved
HashMap<String, Set<String>> jobLabels = new HashMap<>();

for (FeatureSet fs : featureSets) {
for (Map.Entry<String, String> label : fs.getLabelsMap().entrySet()) {
String key = label.getKey().toLowerCase();
String value = label.getValue().toLowerCase();

if (!JOB_LABEL_VALID_PATTERN.matcher(key).matches()) {
log.warn(
String.format(
"FeatureSet '%s' with label key '%s' does not match the accepted pattern '%s'. Label will be ignored.",
fs.getName(), key, JOB_LABEL_VALID_PATTERN.toString()));
continue;
}

if (!JOB_LABEL_VALID_PATTERN.matcher(value).matches()) {
log.warn(
String.format(
"FeatureSet '%s' with label value '%s' does not match the accepted pattern '%s'. Label will be ignored.",
fs.getName(), value, JOB_LABEL_VALID_PATTERN.toString()));
continue;
}

// Ensure the set object is initialized for new jobLabels key
if (!jobLabels.containsKey(key)) {
jobLabels.put(key, new TreeSet<>());
}
jobLabels.get(key).add(value);
}
}

// jobLabelsFlattened contains the combined label values from multiple FeatureSets
// with the same label key.
HashMap<String, String> jobLabelsFlattened = new HashMap<>();
for (Map.Entry<String, Set<String>> label : jobLabels.entrySet()) {
String flattenedVal = String.join(JOB_LABEL_VALUE_JOIN_CHAR, label.getValue());

if (!JOB_LABEL_VALID_PATTERN.matcher(flattenedVal).matches()) {
log.warn(
String.format(
"DataflowJob with with label value '%s' does not match the accepted pattern '%s'. Label with key '%s' will be ignored.",
flattenedVal, JOB_LABEL_VALID_PATTERN.toString(), label.getKey()));
continue;
}

jobLabelsFlattened.put(label.getKey(), flattenedVal);
}

return jobLabelsFlattened;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package feast.core.job.dataflow;

import static feast.common.models.Store.convertStringToSubscription;
import static feast.core.job.dataflow.DataflowJobManager.JOB_LABEL_VALUE_JOIN_CHAR;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -53,6 +54,8 @@
import feast.proto.core.StoreProto.Store.StoreType;
import feast.proto.core.StoreProto.Store.Subscription;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.beam.runners.dataflow.DataflowPipelineJob;
import org.apache.beam.runners.dataflow.DataflowRunner;
import org.apache.beam.sdk.PipelineResult.State;
Expand Down Expand Up @@ -85,7 +88,6 @@ public void setUp() {
optionsBuilder.setTempLocation("tempLocation");
optionsBuilder.setNetwork("network");
optionsBuilder.setSubnetwork("subnetwork");
optionsBuilder.putLabels("orchestrator", "feast");
defaults = optionsBuilder.build();
MetricsProperties metricsProperties = new MetricsProperties();
metricsProperties.setEnabled(false);
Expand Down Expand Up @@ -130,28 +132,58 @@ public void shouldStartJobWithCorrectPipelineOptions() throws IOException {
.build())
.build();

FeatureSetProto.FeatureSet featureSet =
FeatureSetProto.FeatureSet featureSet1 =
FeatureSetProto.FeatureSet.newBuilder()
.setMeta(FeatureSetMeta.newBuilder())
.setSpec(
FeatureSetSpec.newBuilder()
.setSource(source)
.setName("featureSet")
.setMaxAge(Duration.newBuilder().build()))
.setName("featureSet1")
.setMaxAge(Duration.newBuilder().build())
.putLabels("Label1", "Value1")
.putLabels("label2", "value2")
.putLabels("label3", "value3")
.putLabels(
"labelKeyTooLong64char-------------------------------------------",
"ignored")
.putLabels("label4", "value4"))
.build();

FeatureSetProto.FeatureSet featureSet2 =
FeatureSetProto.FeatureSet.newBuilder()
.setMeta(FeatureSetMeta.newBuilder())
.setSpec(
FeatureSetSpec.newBuilder()
.setSource(source)
.setName("featureSet2")
.setMaxAge(Duration.newBuilder().build())
.putLabels("label1", "value1-B-invalid-char-*")
.putLabels("label2", "value2-B")
.putLabels(
"label3", "value3-B-too-long-when-flattened-xxxxxxxxxxxxxxxxxxxxxxxxx")
.putLabels(
"ignored",
"labelValTooLong64char-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.putLabels("label4", "value4"))
.build();

Printer printer = JsonFormat.printer();
String expectedExtJobId = "feast-job-0";
String jobName = "job";

Map<String, String> expectedLabels = new HashMap<>();
expectedLabels.put("label1", "value1");
expectedLabels.put("label2", "value2" + JOB_LABEL_VALUE_JOIN_CHAR + "value2-b");
expectedLabels.put("label4", "value4");

ImportOptions expectedPipelineOptions =
PipelineOptionsFactory.fromArgs("").as(ImportOptions.class);
expectedPipelineOptions.setRunner(DataflowRunner.class);
expectedPipelineOptions.setProject("project");
expectedPipelineOptions.setRegion("region");
expectedPipelineOptions.setUpdate(false);
expectedPipelineOptions.setAppName("DataflowJobManager");
expectedPipelineOptions.setLabels(defaults.getLabelsMap());
expectedPipelineOptions.setLabels(expectedLabels);
expectedPipelineOptions.setJobName(jobName);
expectedPipelineOptions.setStoresJson(Lists.newArrayList(printer.print(store)));
expectedPipelineOptions.setSourceJson(printer.print(source));
Expand All @@ -164,16 +196,19 @@ public void shouldStartJobWithCorrectPipelineOptions() throws IOException {

doReturn(mockPipelineResult).when(dfJobManager).runPipeline(any());

FeatureSetJobStatus featureSetJobStatus = new FeatureSetJobStatus();
featureSetJobStatus.setFeatureSet(FeatureSet.fromProto(featureSet));
FeatureSetJobStatus featureSetJobStatus1 = new FeatureSetJobStatus();
featureSetJobStatus1.setFeatureSet(FeatureSet.fromProto(featureSet1));

FeatureSetJobStatus featureSetJobStatus2 = new FeatureSetJobStatus();
featureSetJobStatus2.setFeatureSet(FeatureSet.fromProto(featureSet2));

Job job =
Job.builder()
.setId(jobName)
.setExtId("")
.setRunner(Runner.DATAFLOW)
.setSource(Source.fromProto(source))
.setFeatureSetJobStatuses(Sets.newHashSet(featureSetJobStatus))
.setFeatureSetJobStatuses(Sets.newHashSet(featureSetJobStatus1, featureSetJobStatus2))
.setStatus(JobStatus.PENDING)
.build();
job.setStores(ImmutableSet.of(Store.fromProto(store)));
Expand Down Expand Up @@ -214,6 +249,7 @@ public void shouldStartJobWithCorrectPipelineOptions() throws IOException {
equalTo(printer.print(specsStreamingUpdateConfig)));
assertThat(actual.getExtId(), equalTo(expectedExtJobId));
assertThat(actual.getStatus(), equalTo(JobStatus.RUNNING));
assertThat(actualPipelineOptions.getLabels(), equalTo(expectedPipelineOptions.getLabels()));
}

@Test
Expand Down