-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for kinesis enhanced fanout consumers
- Loading branch information
1 parent
177c134
commit 4bc0e51
Showing
48 changed files
with
5,885 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
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
37 changes: 37 additions & 0 deletions
37
...n-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/AWSClientsProvider.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,37 @@ | ||
/* | ||
* 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.sdk.io.aws2.kinesis; | ||
|
||
import java.io.Serializable; | ||
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; | ||
import software.amazon.awssdk.services.kinesis.KinesisAsyncClient; | ||
import software.amazon.awssdk.services.kinesis.KinesisClient; | ||
|
||
/** | ||
* Provides instances of AWS clients. | ||
* | ||
* <p>Please note, that any instance of {@link AWSClientsProvider} must be {@link Serializable} to | ||
* ensure it can be sent to worker machines. | ||
*/ | ||
public interface AWSClientsProvider extends Serializable { | ||
KinesisClient getKinesisClient(); | ||
|
||
KinesisAsyncClient getKinesisAsyncClient(); | ||
|
||
CloudWatchClient getCloudWatchClient(); | ||
} |
92 changes: 92 additions & 0 deletions
92
...web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/BasicKinesisProvider.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,92 @@ | ||
/* | ||
* 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.sdk.io.aws2.kinesis; | ||
|
||
import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument; | ||
|
||
import java.net.URI; | ||
import javax.annotation.Nullable; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; | ||
import software.amazon.awssdk.services.cloudwatch.CloudWatchClientBuilder; | ||
import software.amazon.awssdk.services.kinesis.KinesisAsyncClient; | ||
import software.amazon.awssdk.services.kinesis.KinesisAsyncClientBuilder; | ||
import software.amazon.awssdk.services.kinesis.KinesisClient; | ||
import software.amazon.awssdk.services.kinesis.KinesisClientBuilder; | ||
|
||
/** Basic implementation of {@link AWSClientsProvider} used by default in {@link KinesisIO}. */ | ||
class BasicKinesisProvider implements AWSClientsProvider { | ||
private final String accessKey; | ||
private final String secretKey; | ||
private final String region; | ||
@Nullable private final String serviceEndpoint; | ||
|
||
BasicKinesisProvider( | ||
String accessKey, String secretKey, Region region, @Nullable String serviceEndpoint) { | ||
checkArgument(accessKey != null, "accessKey can not be null"); | ||
checkArgument(secretKey != null, "secretKey can not be null"); | ||
checkArgument(region != null, "region can not be null"); | ||
this.accessKey = accessKey; | ||
this.secretKey = secretKey; | ||
this.region = region.toString(); | ||
this.serviceEndpoint = serviceEndpoint; | ||
} | ||
|
||
private AwsCredentialsProvider getCredentialsProvider() { | ||
return StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)); | ||
} | ||
|
||
@Override | ||
public KinesisClient getKinesisClient() { | ||
KinesisClientBuilder clientBuilder = | ||
KinesisClient.builder() | ||
.credentialsProvider(getCredentialsProvider()) | ||
.region(Region.of(region)); | ||
if (serviceEndpoint != null) { | ||
clientBuilder.endpointOverride(URI.create(serviceEndpoint)); | ||
} | ||
return clientBuilder.build(); | ||
} | ||
|
||
@Override | ||
public KinesisAsyncClient getKinesisAsyncClient() { | ||
KinesisAsyncClientBuilder clientBuilder = | ||
KinesisAsyncClient.builder() | ||
.credentialsProvider(getCredentialsProvider()) | ||
.region(Region.of(region)); | ||
if (serviceEndpoint != null) { | ||
clientBuilder.endpointOverride(URI.create(serviceEndpoint)); | ||
} | ||
return clientBuilder.build(); | ||
} | ||
|
||
@Override | ||
public CloudWatchClient getCloudWatchClient() { | ||
CloudWatchClientBuilder clientBuilder = | ||
CloudWatchClient.builder() | ||
.credentialsProvider(getCredentialsProvider()) | ||
.region(Region.of(region)); | ||
if (serviceEndpoint != null) { | ||
clientBuilder.endpointOverride(URI.create(serviceEndpoint)); | ||
} | ||
return clientBuilder.build(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/CheckpointGenerator.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,29 @@ | ||
/* | ||
* 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.sdk.io.aws2.kinesis; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Used to generate checkpoint object on demand. How exactly the checkpoint is generated is up to | ||
* implementing class. | ||
*/ | ||
interface CheckpointGenerator extends Serializable { | ||
|
||
KinesisReaderCheckpoint generate(SimplifiedKinesisClient client) throws TransientKinesisException; | ||
} |
101 changes: 101 additions & 0 deletions
101
...mazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/CustomOptional.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,101 @@ | ||
/* | ||
* 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.sdk.io.aws2.kinesis; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Similar to Guava {@code Optional}, but throws {@link NoSuchElementException} for missing element. | ||
*/ | ||
abstract class CustomOptional<T> { | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> CustomOptional<T> absent() { | ||
return (Absent<T>) Absent.INSTANCE; | ||
} | ||
|
||
public static <T> CustomOptional<T> of(T v) { | ||
return new Present<>(v); | ||
} | ||
|
||
public abstract boolean isPresent(); | ||
|
||
public abstract T get(); | ||
|
||
private static class Present<T> extends CustomOptional<T> { | ||
|
||
private final T value; | ||
|
||
private Present(T value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean isPresent() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (!(o instanceof Present)) { | ||
return false; | ||
} | ||
|
||
Present<?> present = (Present<?>) o; | ||
return Objects.equals(value, present.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} | ||
|
||
private static class Absent<T> extends CustomOptional<T> { | ||
|
||
private static final Absent<Object> INSTANCE = new Absent<>(); | ||
|
||
private Absent() {} | ||
|
||
@Override | ||
public boolean isPresent() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return o instanceof Absent; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 0; | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...rvices2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/DynamicCheckpointGenerator.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,81 @@ | ||
/* | ||
* 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.sdk.io.aws2.kinesis; | ||
|
||
import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.awssdk.services.kinesis.model.Shard; | ||
|
||
/** | ||
* Creates {@link KinesisReaderCheckpoint}, which spans over all shards in given stream. List of | ||
* shards is obtained dynamically on call to {@link #generate(SimplifiedKinesisClient)}. | ||
*/ | ||
class DynamicCheckpointGenerator implements CheckpointGenerator { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DynamicCheckpointGenerator.class); | ||
private final String streamName; | ||
private final String consumerArn; | ||
private final StartingPoint startingPoint; | ||
private final StartingPointShardsFinder startingPointShardsFinder; | ||
|
||
public DynamicCheckpointGenerator( | ||
String streamName, String consumerArn, StartingPoint startingPoint) { | ||
this.streamName = streamName; | ||
this.consumerArn = consumerArn; | ||
this.startingPoint = startingPoint; | ||
this.startingPointShardsFinder = new StartingPointShardsFinder(); | ||
} | ||
|
||
public DynamicCheckpointGenerator( | ||
String streamName, | ||
String consumerArn, | ||
StartingPoint startingPoint, | ||
StartingPointShardsFinder startingPointShardsFinder) { | ||
this.streamName = checkNotNull(streamName, "streamName"); | ||
this.consumerArn = consumerArn; | ||
this.startingPoint = checkNotNull(startingPoint, "startingPoint"); | ||
this.startingPointShardsFinder = | ||
checkNotNull(startingPointShardsFinder, "startingPointShardsFinder"); | ||
} | ||
|
||
@Override | ||
public KinesisReaderCheckpoint generate(SimplifiedKinesisClient kinesis) | ||
throws TransientKinesisException { | ||
Set<Shard> shardsAtStartingPoint = | ||
startingPointShardsFinder.findShardsAtStartingPoint(kinesis, streamName, startingPoint); | ||
LOG.info( | ||
"Creating a checkpoint with following shards {} at {}", | ||
shardsAtStartingPoint, | ||
startingPoint.getTimestamp()); | ||
return new KinesisReaderCheckpoint( | ||
shardsAtStartingPoint.stream() | ||
.map( | ||
shard -> | ||
new ShardCheckpoint(streamName, shard.shardId(), consumerArn, startingPoint)) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Checkpoint generator for %s: %s", streamName, startingPoint); | ||
} | ||
} |
Oops, something went wrong.