Skip to content

Commit 60eb689

Browse files
author
zhilingc
committed
Change error store to be part of configuration instead
1 parent 14cb64c commit 60eb689

15 files changed

Lines changed: 241 additions & 106 deletions

File tree

‎core/src/main/java/feast/core/config/AppConfig.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public ImportJobDefaults getImportJobDefaults(
3232
@Value("${feast.jobs.runner}") String runner,
3333
@Value("${feast.jobs.options}") String options,
3434
@Value("${feast.jobs.executable}") String executable,
35-
@Value("${feast.jobs.errorsStoreId}") String errorsStoreId) {
36-
return new ImportJobDefaults(coreApiUri, runner, options, executable, errorsStoreId);
35+
@Value("${feast.jobs.errorsStoreType}") String errorsStoreType,
36+
@Value("${feast.jobs.errorsStoreOptions}") String errorsStoreOptions) {
37+
return new ImportJobDefaults(
38+
coreApiUri, runner, options, executable, errorsStoreType, errorsStoreOptions);
3739
}
3840
}

‎core/src/main/java/feast/core/config/ImportJobDefaults.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class ImportJobDefaults {
3232
private String runner;
3333
private String importJobOptions;
3434
private String executable;
35-
private String errorsStoreId;
35+
private String errorsStoreType;
36+
private String errorsStoreOptions;
3637
}
3738

‎core/src/main/java/feast/core/service/JobExecutionService.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ public ProcessBuilder getProcessBuilder(ImportSpec importSpec, String jobId) {
153153
commands.add(
154154
option("importSpecBase64", Base64.getEncoder().encodeToString(importSpec.toByteArray())));
155155
commands.add(option("coreApiUri", defaults.getCoreApiUri()));
156-
commands.add(option("errorsStoreId", defaults.getErrorsStoreId()));
156+
commands.add(option("errorsStoreType", defaults.getErrorsStoreType()));
157+
commands.add(option("errorsStoreOptions", defaults.getErrorsStoreOptions()));
157158
options.forEach((k, v) -> commands.add(option(k, v)));
158159
return new ProcessBuilder(commands);
159160
}

‎core/src/main/resources/application.properties‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ feast.jobs.coreUri=${CORE_API_URI:localhost:8433}
2020
feast.jobs.runner=${JOB_RUNNER:DirectRunner}
2121
feast.jobs.options=${JOB_OPTIONS:{}}
2222
feast.jobs.executable=${JOB_EXECUTABLE:feast-ingestion.jar}
23-
feast.jobs.errorsStoreId=${JOB_ERRORS_STORE_ID:STDOUT}
23+
feast.jobs.errorsStoreType=${JOB_ERRORS_STORE_TYPE:stdout}
24+
feast.jobs.errorsStoreOptions=${JOB_ERRORS_STORE_OPTIONS:{}}
2425

2526
feast.jobs.dataflow.projectId = ${DATAFLOW_PROJECT_ID:}
2627
feast.jobs.dataflow.location = ${DATAFLOW_LOCATION:}

‎core/src/test/java/feast/core/service/JobExecutionServiceTest.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public void setUp() {
6060
"DirectRunner",
6161
"{\"key\":\"value\"}",
6262
"ingestion.jar",
63-
"STDOUT");
63+
"STDOUT",
64+
"{}");
6465
}
6566

6667
@Test
@@ -77,7 +78,8 @@ public void shouldBuildProcessBuilderWithCorrectOptions() {
7778
"--runner=DirectRunner",
7879
"--importSpecBase64=CgRmaWxl",
7980
"--coreApiUri=localhost:8080",
80-
"--errorsStoreId=STDOUT",
81+
"--errorsStoreType=STDOUT",
82+
"--errorsStoreOptions={}",
8183
"--key=value");
8284
assertThat(pb.command(), equalTo(expected));
8385
}

‎ingestion/src/main/java/feast/ingestion/boot/ImportJobModule.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
import feast.storage.ErrorsStore;
3232
import feast.storage.ServingStore;
3333
import feast.storage.WarehouseStore;
34+
import feast.storage.file.json.JsonFileStores;
3435
import feast.storage.service.ErrorsStoreService;
3536
import feast.storage.service.ServingStoreService;
3637
import feast.storage.service.WarehouseStoreService;
3738
import java.util.List;
3839
import org.apache.beam.sdk.options.PipelineOptions;
3940

41+
import static feast.ingestion.transform.ErrorsStoreTransform.ERRORS_STORE_JSON;
42+
4043
/** An ImportJobModule is a Guice module for creating dependency injection bindings. */
4144
public class ImportJobModule extends AbstractModule {
4245

@@ -48,11 +51,18 @@ public ImportJobModule(ImportJobOptions options, ImportSpec importSpec) {
4851
this.importSpec = importSpec;
4952
}
5053

54+
private void configureErrorsStore() {
55+
if (options.getErrorsStoreType().equals(ERRORS_STORE_JSON)) {
56+
ErrorsStoreService.register(new JsonFileStores.JsonFileErrorsStore());
57+
}
58+
}
59+
5160
@Override
5261
protected void configure() {
5362
bind(ImportJobOptions.class).toInstance(options);
5463
bind(PipelineOptions.class).toInstance(options);
5564
bind(ImportSpec.class).toInstance(importSpec);
65+
configureErrorsStore();
5666
}
5767

5868
@Provides
@@ -88,7 +98,7 @@ List<ServingStore> provideServingStores() {
8898

8999
@Provides
90100
@Singleton
91-
List<ErrorsStore> provideErrorsStores() {
92-
return ErrorsStoreService.getAll();
101+
ErrorsStore provideErrorsStores() {
102+
return ErrorsStoreService.get();
93103
}
94104
}

‎ingestion/src/main/java/feast/ingestion/options/ImportJobOptions.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,19 @@ public interface ImportJobOptions extends PipelineOptions {
7272
void setLimit(Long value);
7373

7474
@Description(
75-
"Set a store id to store errors in, if your data input is **very** small, you can use STDOUT"
76-
+ " or STDERR as the store id, otherwise it must match an associated storage spec")
77-
String getErrorsStoreId();
75+
"Set an errors store type. One of: [STDERR, STDOUT, JSON]. Note that you should not use "
76+
+ "STDERR/STDOUT in production unless your data volume is extremely small.")
77+
String getErrorsStoreType();
7878

79-
void setErrorsStoreId(String value);
79+
void setErrorsStoreType(String value);
80+
81+
@Description(
82+
"Provide errors store options as a json string containing key-values. Options required"
83+
+ "depend on the type of store set.")
84+
@Default.String("{}")
85+
String getErrorsStoreOptions();
86+
87+
void setErrorsStoreOptions(String value);
8088

8189
@AutoService(PipelineOptionsRegistrar.class)
8290
class ImportJobOptionsRegistrar implements PipelineOptionsRegistrar {

‎ingestion/src/main/java/feast/ingestion/transform/ErrorsStoreTransform.java‎

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@
1717

1818
package feast.ingestion.transform;
1919

20-
import static com.google.common.base.Preconditions.checkNotNull;
21-
2220
import com.google.inject.Inject;
23-
import java.util.List;
24-
import lombok.extern.slf4j.Slf4j;
25-
import org.apache.beam.sdk.transforms.ParDo;
26-
import org.apache.beam.sdk.values.PCollection;
27-
import org.apache.beam.sdk.values.PDone;
2821
import feast.ingestion.model.Specs;
2922
import feast.ingestion.options.ImportJobOptions;
3023
import feast.ingestion.transform.FeatureIO.Write;
@@ -33,54 +26,59 @@
3326
import feast.storage.ErrorsStore;
3427
import feast.storage.noop.NoOpIO;
3528
import feast.types.FeatureRowExtendedProto.FeatureRowExtended;
29+
import lombok.extern.slf4j.Slf4j;
30+
import org.apache.beam.sdk.transforms.ParDo;
31+
import org.apache.beam.sdk.values.PCollection;
32+
import org.apache.beam.sdk.values.PDone;
3633
import org.slf4j.event.Level;
3734

35+
import javax.annotation.Nullable;
36+
37+
import static feast.ingestion.util.JsonUtil.convertJsonStringToMap;
38+
3839
@Slf4j
3940
public class ErrorsStoreTransform extends FeatureIO.Write {
40-
public static final String STDERR_STORE_ID = "STDERR";
41-
public static final String STDOUT_STORE_ID = "STDOUT";
41+
public static final String ERRORS_STORE_STDERR = "stderr";
42+
public static final String ERRORS_STORE_STDOUT = "stdout";
43+
public static final String ERRORS_STORE_JSON = "file.json";
4244

43-
private String errorsStoreId;
44-
private List<ErrorsStore> stores;
45+
private String errorsStoreType;
46+
private StorageSpec errorsStoreSpec;
47+
private ErrorsStore errorsStore;
4548
private Specs specs;
4649

4750
@Inject
48-
public ErrorsStoreTransform(ImportJobOptions options, List<ErrorsStore> stores, Specs specs) {
49-
this.errorsStoreId = options.getErrorsStoreId();
50-
this.stores = stores;
51+
public ErrorsStoreTransform(
52+
ImportJobOptions options, Specs specs, @Nullable ErrorsStore errorsStore) {
5153
this.specs = specs;
54+
this.errorsStoreType = options.getErrorsStoreType();
55+
this.errorsStore = errorsStore;
56+
StorageSpec.Builder errorsStoreBuilder = StorageSpec.newBuilder().setType(errorsStoreType);
57+
58+
switch (errorsStoreType) {
59+
case ERRORS_STORE_JSON:
60+
errorsStoreBuilder.putAllOptions(convertJsonStringToMap(options.getErrorsStoreOptions()));
61+
default:
62+
this.errorsStoreSpec = errorsStoreBuilder.build();
63+
}
5264
}
5365

5466
@Override
5567
public PDone expand(PCollection<FeatureRowExtended> input) {
56-
if (errorsStoreId == null) {
57-
log.warn("No errorsStoreId specified, errors will be discarded");
58-
return input.apply(new NoOpIO.Write());
59-
}
60-
61-
if (errorsStoreId.equals(STDOUT_STORE_ID)) {
62-
input.apply("Log errors to STDOUT", ParDo.of(new LoggerDoFn(Level.INFO)));
63-
} else if (errorsStoreId.equals(STDERR_STORE_ID)) {
64-
input.apply("Log errors to STDERR", ParDo.of(new LoggerDoFn(Level.ERROR)));
65-
} else {
66-
StorageSpec storageSpec = specs.getStorageSpec(errorsStoreId);
67-
storageSpec =
68-
checkNotNull(
69-
storageSpec,
70-
String.format("errorsStoreId=%s not found in storage specs", errorsStoreId));
71-
Write write = null;
72-
for (ErrorsStore errorsStore : stores) {
73-
if (errorsStore.getType().equals(storageSpec.getType())) {
74-
write = errorsStore.create(storageSpec, specs);
68+
switch (errorsStoreType) {
69+
case ERRORS_STORE_STDOUT:
70+
input.apply("Log errors to STDOUT", ParDo.of(new LoggerDoFn(Level.INFO)));
71+
break;
72+
case ERRORS_STORE_STDERR:
73+
input.apply("Log errors to STDERR", ParDo.of(new LoggerDoFn(Level.ERROR)));
74+
break;
75+
default:
76+
if (errorsStore == null) {
77+
log.warn("No valid errors store specified, errors will be discarded");
78+
return input.apply(new NoOpIO.Write());
7579
}
76-
}
77-
write =
78-
checkNotNull(
79-
write,
80-
"No errors storage factory found for errorsStoreId=%s with type=%s",
81-
errorsStoreId,
82-
storageSpec.getType());
83-
return input.apply(write);
80+
Write write = errorsStore.create(this.errorsStoreSpec, specs);
81+
return input.apply(write);
8482
}
8583
return PDone.in(input.getPipeline());
8684
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package feast.ingestion.util;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
6+
import java.lang.reflect.Type;
7+
import java.util.Collections;
8+
import java.util.Map;
9+
10+
public class JsonUtil {
11+
private static Gson gson = new Gson();
12+
/**
13+
* Unmarshals a given json string to map
14+
*
15+
* @param jsonString valid json formatted string
16+
* @return map of keys to values in json
17+
*/
18+
public static Map<String, String> convertJsonStringToMap(String jsonString) {
19+
if (jsonString == null || jsonString.equals("") || jsonString.equals("{}")) {
20+
return Collections.emptyMap();
21+
}
22+
Type stringMapType = new TypeToken<Map<String, String>>() {}.getType();
23+
return gson.fromJson(jsonString, stringMapType);
24+
}
25+
}

‎ingestion/src/main/java/feast/storage/service/ErrorsStoreService.java‎

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,18 @@
1717

1818
package feast.storage.service;
1919

20-
import com.google.common.collect.Iterators;
21-
import com.google.common.collect.Lists;
22-
import java.util.ArrayList;
23-
import java.util.List;
24-
import java.util.ServiceLoader;
25-
import lombok.extern.slf4j.Slf4j;
2620
import feast.storage.ErrorsStore;
21+
import lombok.extern.slf4j.Slf4j;
2722

2823
@Slf4j
2924
public class ErrorsStoreService {
30-
private static ServiceLoader<ErrorsStore> serviceLoader = ServiceLoader.load(ErrorsStore.class);
31-
private static List<ErrorsStore> manuallyRegistered = new ArrayList<>();
32-
33-
static {
34-
for (ErrorsStore store : getAll()) {
35-
log.info("ErrorsStore type found: " + store.getType());
36-
}
37-
}
38-
39-
public static List<ErrorsStore> getAll() {
40-
return Lists.newArrayList(
41-
Iterators.concat(manuallyRegistered.iterator(), serviceLoader.iterator()));
42-
}
25+
private static ErrorsStore errorsStore;
4326

44-
/** Get store of the given subclass. */
45-
public static <T extends ErrorsStore> T get(Class<T> clazz) {
46-
for (ErrorsStore store : getAll()) {
47-
if (clazz.isInstance(store)) {
48-
//noinspection unchecked
49-
return (T) store;
50-
}
51-
}
52-
return null;
27+
public static ErrorsStore get() {
28+
return errorsStore;
5329
}
5430

5531
public static void register(ErrorsStore store) {
56-
manuallyRegistered.add(store);
32+
errorsStore = store;
5733
}
5834
}

0 commit comments

Comments
 (0)