如何使用 Guice 将我的 api 注入到数据流作业中而不需要序列化?
How can I inject with Guice my api into dataflow jobs without needed to be serializable?
这个问题是在如此出色的答案之后的后续问题
这让我意识到'ok, what I want is injection with no serialization so that I can mock and test'。
我们当前的方法要求我们的 apis/mocks 是可序列化的,但是然后,我必须将静态字段放入 mock 中,因为它会被序列化和反序列化,从而创建数据流使用的新实例。
我的同事指出,也许这需要是一个水槽,并且处理方式不同? <- 我们可能会稍后尝试并更新,但我们现在不确定。
我的愿望是在测试期间用模拟替换 apis。有人有这方面的例子吗?
这是我们的 bootstrap 代码,不知道它是在生产中还是在功能测试中。我们测试端到端结果,在我们的测试中没有 apache beam 导入,这意味着如果我们想要调整并保留所有测试,我们可以切换到任何技术。不仅如此,我们发现了更多的集成错误,并且可以在不重写测试的情况下进行重构,因为我们测试的合同是我们无法轻易更改的客户合同。
public class App {
private Pipeline pipeline;
private RosterFileTransform transform;
@Inject
public App(Pipeline pipeline, RosterFileTransform transform) {
this.pipeline = pipeline;
this.transform = transform;
}
public void start() {
pipeline.apply(transform);
pipeline.run();
}
}
请注意,我们所做的一切都是基于 Guice 注入的,因此流水线可能是直接运行器,也可能不是。我可能需要修改此 class 以通过 :( 但现在能用的任何东西都会很棒。
我试图在没有序列化的情况下获得 api(并模拟和实现)的函数是
private class ValidRecordPublisher extends DoFn<Validated<PractitionerDataRecord>, String> {
@ProcessElement
public void processElement(@Element Validated<PractitionerDataRecord>element) {
microServiceApi.writeRecord(element.getValue);
}
}
我不确定如何以避免序列化的方式传入 microServiceApi。在使用 guice Provider 提供程序反序列化后,我也可以延迟创建;如果那里也有解决方案,请使用 provider.get()。
我想这可能不是您想要的,但您的用例让我想到了使用工厂对象。它们可能取决于您传递的管道选项(即您的 PipelineOptions
对象)或其他一些配置对象。
也许是这样的:
class MicroserviceApiClientFactory implements Serializable {
MicroserviceApiClientFactory(PipelineOptions options) {
this.options = options;
}
public static MicroserviceApiClient getClient() {
MyPipelineOptions specialOpts = options.as(MySpecialOptions.class);
if (specialOpts.getMockMicroserviceApi()) {
return new MockedMicroserviceApiClient(...); // Or whatever
} else {
return new MicroserviceApiClient(specialOpts.getMicroserviceEndpoint()); // Or whatever parameters it needs
}
}
}
对于您的 DoFns 和任何其他需要它的执行时对象,您将传递工厂:
private class ValidRecordPublisher extends DoFn<Validated<PractitionerDataRecord>, String> {
ValidRecordPublisher(MicroserviceApiClientFactory msFactory) {
this.msFactory = msFactory;
}
@ProcessElement
public void processElement(@Element Validated<PractitionerDataRecord>element) {
if (microServiceapi == null) microServiceApi = msFactory.getClient();
microServiceApi.writeRecord(element.getValue);
}
}
这应该允许您将模拟功能封装到一个 class 中,在管道执行时懒惰地创建您的模拟或客户端。
让我知道这是否符合您的要求,或者我们是否应该尝试进一步迭代。
我没有使用 Guice 的经验,所以我不知道 Guice 配置是否可以轻松地跨越管道构建和管道执行(序列化/提交 JAR 等)之间的边界。
这应该是水槽吗?也许,如果您有一个外部服务,并且您正在写入它,您可以编写一个 PTransform 来处理它——但是如何注入各种依赖项的问题仍然存在。
以这样一种方式解决,即模拟不再需要静态或序列化,因为玻璃桥接了数据流世界(在生产和测试中),就像这样
注意:我们公司还有其他 magic-ness 从一个服务到另一个服务并通过数据流通过 headers,其中有一些您可以忽略(即。 RouterRequest 请求 = Current.request();)。所以对于其他人,他们每次都必须将 projectId 传递给 getInstance。
public abstract class DataflowClientFactory implements Serializable {
private static final Logger log = LoggerFactory.getLogger(DataflowClientFactory.class);
public static final String PROJECT_KEY = "projectKey";
private transient static Injector injector;
private transient static Module overrides;
private static int counter = 0;
public DataflowClientFactory() {
counter++;
log.info("creating again(usually due to deserialization). counter="+counter);
}
public static void injectOverrides(Module dfOverrides) {
overrides = dfOverrides;
}
private synchronized void initialize(String project) {
if(injector != null)
return;
/********************************************
* The hardest part is this piece since this is specific to each Dataflow
* so each project subclasses DataflowClientFactory
* This solution is the best ONLY in the fact of time crunch and it works
* decently for end to end testing without developers needing fancy
* wrappers around mocks anymore.
***/
Module module = loadProjectModule();
Module modules = Modules.combine(module, new OrderlyDataflowModule(project));
if(overrides != null) {
modules = Modules.override(modules).with(overrides);
}
injector = Guice.createInjector(modules);
}
protected abstract Module loadProjectModule();
public <T> T getInstance(Class<T> clazz) {
if(!Current.isContextSet()) {
throw new IllegalStateException("Someone on the stack is extending DoFn instead of OrderlyDoFn so you need to fix that first");
}
RouterRequest request = Current.request();
String project = (String)request.requestState.get(PROJECT_KEY);
initialize(project);
return injector.getInstance(clazz);
}
}
这个问题是在如此出色的答案之后的后续问题
这让我意识到'ok, what I want is injection with no serialization so that I can mock and test'。
我们当前的方法要求我们的 apis/mocks 是可序列化的,但是然后,我必须将静态字段放入 mock 中,因为它会被序列化和反序列化,从而创建数据流使用的新实例。
我的同事指出,也许这需要是一个水槽,并且处理方式不同? <- 我们可能会稍后尝试并更新,但我们现在不确定。
我的愿望是在测试期间用模拟替换 apis。有人有这方面的例子吗?
这是我们的 bootstrap 代码,不知道它是在生产中还是在功能测试中。我们测试端到端结果,在我们的测试中没有 apache beam 导入,这意味着如果我们想要调整并保留所有测试,我们可以切换到任何技术。不仅如此,我们发现了更多的集成错误,并且可以在不重写测试的情况下进行重构,因为我们测试的合同是我们无法轻易更改的客户合同。
public class App {
private Pipeline pipeline;
private RosterFileTransform transform;
@Inject
public App(Pipeline pipeline, RosterFileTransform transform) {
this.pipeline = pipeline;
this.transform = transform;
}
public void start() {
pipeline.apply(transform);
pipeline.run();
}
}
请注意,我们所做的一切都是基于 Guice 注入的,因此流水线可能是直接运行器,也可能不是。我可能需要修改此 class 以通过 :( 但现在能用的任何东西都会很棒。
我试图在没有序列化的情况下获得 api(并模拟和实现)的函数是
private class ValidRecordPublisher extends DoFn<Validated<PractitionerDataRecord>, String> {
@ProcessElement
public void processElement(@Element Validated<PractitionerDataRecord>element) {
microServiceApi.writeRecord(element.getValue);
}
}
我不确定如何以避免序列化的方式传入 microServiceApi。在使用 guice Provider 提供程序反序列化后,我也可以延迟创建;如果那里也有解决方案,请使用 provider.get()。
我想这可能不是您想要的,但您的用例让我想到了使用工厂对象。它们可能取决于您传递的管道选项(即您的 PipelineOptions
对象)或其他一些配置对象。
也许是这样的:
class MicroserviceApiClientFactory implements Serializable {
MicroserviceApiClientFactory(PipelineOptions options) {
this.options = options;
}
public static MicroserviceApiClient getClient() {
MyPipelineOptions specialOpts = options.as(MySpecialOptions.class);
if (specialOpts.getMockMicroserviceApi()) {
return new MockedMicroserviceApiClient(...); // Or whatever
} else {
return new MicroserviceApiClient(specialOpts.getMicroserviceEndpoint()); // Or whatever parameters it needs
}
}
}
对于您的 DoFns 和任何其他需要它的执行时对象,您将传递工厂:
private class ValidRecordPublisher extends DoFn<Validated<PractitionerDataRecord>, String> {
ValidRecordPublisher(MicroserviceApiClientFactory msFactory) {
this.msFactory = msFactory;
}
@ProcessElement
public void processElement(@Element Validated<PractitionerDataRecord>element) {
if (microServiceapi == null) microServiceApi = msFactory.getClient();
microServiceApi.writeRecord(element.getValue);
}
}
这应该允许您将模拟功能封装到一个 class 中,在管道执行时懒惰地创建您的模拟或客户端。
让我知道这是否符合您的要求,或者我们是否应该尝试进一步迭代。
我没有使用 Guice 的经验,所以我不知道 Guice 配置是否可以轻松地跨越管道构建和管道执行(序列化/提交 JAR 等)之间的边界。
这应该是水槽吗?也许,如果您有一个外部服务,并且您正在写入它,您可以编写一个 PTransform 来处理它——但是如何注入各种依赖项的问题仍然存在。
以这样一种方式解决,即模拟不再需要静态或序列化,因为玻璃桥接了数据流世界(在生产和测试中),就像这样
注意:我们公司还有其他 magic-ness 从一个服务到另一个服务并通过数据流通过 headers,其中有一些您可以忽略(即。 RouterRequest 请求 = Current.request();)。所以对于其他人,他们每次都必须将 projectId 传递给 getInstance。
public abstract class DataflowClientFactory implements Serializable {
private static final Logger log = LoggerFactory.getLogger(DataflowClientFactory.class);
public static final String PROJECT_KEY = "projectKey";
private transient static Injector injector;
private transient static Module overrides;
private static int counter = 0;
public DataflowClientFactory() {
counter++;
log.info("creating again(usually due to deserialization). counter="+counter);
}
public static void injectOverrides(Module dfOverrides) {
overrides = dfOverrides;
}
private synchronized void initialize(String project) {
if(injector != null)
return;
/********************************************
* The hardest part is this piece since this is specific to each Dataflow
* so each project subclasses DataflowClientFactory
* This solution is the best ONLY in the fact of time crunch and it works
* decently for end to end testing without developers needing fancy
* wrappers around mocks anymore.
***/
Module module = loadProjectModule();
Module modules = Modules.combine(module, new OrderlyDataflowModule(project));
if(overrides != null) {
modules = Modules.override(modules).with(overrides);
}
injector = Guice.createInjector(modules);
}
protected abstract Module loadProjectModule();
public <T> T getInstance(Class<T> clazz) {
if(!Current.isContextSet()) {
throw new IllegalStateException("Someone on the stack is extending DoFn instead of OrderlyDoFn so you need to fix that first");
}
RouterRequest request = Current.request();
String project = (String)request.requestState.get(PROJECT_KEY);
initialize(project);
return injector.getInstance(clazz);
}
}