forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RRIO] Create Caller and SetupTeardown interfaces (apache#28905)
* Create test Caller and SetupTeardown interfaces * Update Javadoc * Defer Call transform to future PR * Rename package to requestresponseio * Add username to TODO
- Loading branch information
1 parent
6bab4b5
commit 104c10b
Showing
9 changed files
with
462 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/Caller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.beam.io.requestresponseio; | ||
|
||
import java.io.Serializable; | ||
|
||
/** {@link Caller} interfaces user custom code intended for API calls. */ | ||
public interface Caller<RequestT, ResponseT> extends Serializable { | ||
|
||
/** Calls a Web API with the {@link RequestT} and returns a {@link ResponseT}. */ | ||
ResponseT call(RequestT request) throws UserCodeExecutionException; | ||
} |
34 changes: 34 additions & 0 deletions
34
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/SetupTeardown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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.beam.io.requestresponseio; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Provided by user and called within {@link org.apache.beam.sdk.transforms.DoFn.Setup} and @{link | ||
* org.apache.beam.sdk.transforms.DoFn.Teardown} lifecycle methods of {@link Call}'s {@link | ||
* org.apache.beam.sdk.transforms.DoFn}. | ||
*/ | ||
public interface SetupTeardown extends Serializable { | ||
|
||
/** Called during the {@link org.apache.beam.sdk.transforms.DoFn}'s setup lifecycle method. */ | ||
void setup() throws UserCodeExecutionException; | ||
|
||
/** Called during the {@link org.apache.beam.sdk.transforms.DoFn}'s teardown lifecycle method. */ | ||
void teardown() throws UserCodeExecutionException; | ||
} |
38 changes: 38 additions & 0 deletions
38
...o/rrio/src/main/java/org/apache/beam/io/requestresponseio/UserCodeExecutionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.beam.io.requestresponseio; | ||
|
||
/** Base {@link Exception} for signaling errors in user custom code. */ | ||
public class UserCodeExecutionException extends Exception { | ||
public UserCodeExecutionException(String message) { | ||
super(message); | ||
} | ||
|
||
public UserCodeExecutionException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public UserCodeExecutionException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public UserCodeExecutionException( | ||
String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...va/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/UserCodeQuotaException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.beam.io.requestresponseio; | ||
|
||
/** | ||
* Extends {@link UserCodeQuotaException} to allow the user custom code to specifically signal a | ||
* Quota or API overuse related error. | ||
*/ | ||
public class UserCodeQuotaException extends UserCodeExecutionException { | ||
|
||
public UserCodeQuotaException(String message) { | ||
super(message); | ||
} | ||
|
||
public UserCodeQuotaException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public UserCodeQuotaException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public UserCodeQuotaException( | ||
String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../io/rrio/src/main/java/org/apache/beam/io/requestresponseio/UserCodeTimeoutException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.beam.io.requestresponseio; | ||
|
||
/** An extension of {@link UserCodeQuotaException} to specifically signal a user code timeout. */ | ||
public class UserCodeTimeoutException extends UserCodeExecutionException { | ||
|
||
public UserCodeTimeoutException(String message) { | ||
super(message); | ||
} | ||
|
||
public UserCodeTimeoutException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public UserCodeTimeoutException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public UserCodeTimeoutException( | ||
String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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 provides Beam I/O transform support for safely reading from and writing to Web APIs. */ | ||
package org.apache.beam.io.requestresponseio; |
126 changes: 126 additions & 0 deletions
126
sdks/java/io/rrio/src/test/java/org/apache/beam/io/rrio/CallerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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.beam.io.rrio; | ||
|
||
import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import org.apache.beam.io.requestresponseio.Caller; | ||
import org.apache.beam.io.requestresponseio.UserCodeExecutionException; | ||
import org.apache.beam.io.requestresponseio.UserCodeQuotaException; | ||
import org.apache.beam.io.requestresponseio.UserCodeTimeoutException; | ||
import org.apache.beam.sdk.Pipeline.PipelineExecutionException; | ||
import org.apache.beam.sdk.coders.StringUtf8Coder; | ||
import org.apache.beam.sdk.coders.VarIntCoder; | ||
import org.apache.beam.sdk.testing.TestPipeline; | ||
import org.apache.beam.sdk.transforms.Create; | ||
import org.apache.beam.sdk.transforms.DoFn; | ||
import org.apache.beam.sdk.transforms.ParDo; | ||
import org.apache.beam.sdk.util.SerializableUtils; | ||
import org.joda.time.Instant; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for {@link Caller}. */ | ||
@RunWith(JUnit4.class) | ||
public class CallerTest { | ||
|
||
@Rule public TestPipeline pipeline = TestPipeline.create(); | ||
|
||
@Test | ||
public void canSerializeImplementingClasses() { | ||
SerializableUtils.serializeToByteArray(new CallerImpl()); | ||
} | ||
|
||
@Test | ||
public void canSerializeWhenUsedInDoFn() { | ||
pipeline | ||
.apply(Create.of(Instant.now())) | ||
.apply(ParDo.of(new CallerUsingDoFn<>(new CallerImpl()))) | ||
.setCoder(StringUtf8Coder.of()); | ||
|
||
pipeline.run(); | ||
} | ||
|
||
@Test | ||
public void canSignalQuotaException() { | ||
pipeline | ||
.apply(Create.of(1)) | ||
.apply(ParDo.of(new CallerUsingDoFn<>(new CallerThrowsQuotaException()))) | ||
.setCoder(VarIntCoder.of()); | ||
|
||
PipelineExecutionException executionException = | ||
assertThrows(PipelineExecutionException.class, pipeline::run); | ||
assertEquals(UserCodeQuotaException.class, executionException.getCause().getClass()); | ||
} | ||
|
||
@Test | ||
public void canSignalTimeoutException() { | ||
pipeline | ||
.apply(Create.of(1)) | ||
.apply(ParDo.of(new CallerUsingDoFn<>(new CallerThrowsTimeoutException()))) | ||
.setCoder(VarIntCoder.of()); | ||
|
||
PipelineExecutionException executionException = | ||
assertThrows(PipelineExecutionException.class, pipeline::run); | ||
assertEquals(UserCodeTimeoutException.class, executionException.getCause().getClass()); | ||
} | ||
|
||
private static class CallerUsingDoFn<RequestT, ResponseT> extends DoFn<RequestT, ResponseT> { | ||
private final Caller<RequestT, ResponseT> caller; | ||
|
||
private CallerUsingDoFn(Caller<RequestT, ResponseT> caller) { | ||
this.caller = caller; | ||
} | ||
|
||
@ProcessElement | ||
public void process(@Element RequestT request, OutputReceiver<ResponseT> receiver) | ||
throws UserCodeExecutionException { | ||
RequestT safeRequest = checkStateNotNull(request); | ||
ResponseT response = caller.call(safeRequest); | ||
receiver.output(response); | ||
} | ||
} | ||
|
||
private static class CallerImpl implements Caller<Instant, String> { | ||
|
||
@Override | ||
public String call(Instant request) throws UserCodeExecutionException { | ||
return request.toString(); | ||
} | ||
} | ||
|
||
private static class CallerThrowsQuotaException implements Caller<Integer, Integer> { | ||
|
||
@Override | ||
public Integer call(Integer request) throws UserCodeExecutionException { | ||
throw new UserCodeQuotaException("quota"); | ||
} | ||
} | ||
|
||
private static class CallerThrowsTimeoutException implements Caller<Integer, Integer> { | ||
|
||
@Override | ||
public Integer call(Integer request) throws UserCodeExecutionException { | ||
throw new UserCodeTimeoutException("timeout"); | ||
} | ||
} | ||
} |
Oops, something went wrong.