-
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.
feat: caches watermark for Spanner change streams
- Loading branch information
1 parent
0b2e57c
commit d8b47c9
Showing
11 changed files
with
229 additions
and
9 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
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
49 changes: 49 additions & 0 deletions
49
...rm/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/cache/CacheFactory.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,49 @@ | ||
/* | ||
* 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.gcp.spanner.changestreams.cache; | ||
|
||
import java.io.Serializable; | ||
import javax.annotation.Nullable; | ||
import org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.DaoFactory; | ||
import org.joda.time.Duration; | ||
|
||
@SuppressWarnings({"initialization.fields.uninitialized", "nullness"}) | ||
public class CacheFactory implements Serializable { | ||
|
||
private static final long serialVersionUID = -8722905670370252723L; | ||
private final DaoFactory daoFactory; | ||
private final @Nullable Duration refreshRate; | ||
private transient WatermarkCache watermarkCacheInstance; | ||
|
||
public CacheFactory(DaoFactory daoFactory, @Nullable Duration watermarkRefreshRate) { | ||
this.daoFactory = daoFactory; | ||
this.refreshRate = watermarkRefreshRate; | ||
} | ||
|
||
public synchronized WatermarkCache getWatermarkCache() { | ||
if (watermarkCacheInstance == null) { | ||
if (refreshRate == null) { | ||
watermarkCacheInstance = new NoOpWatermarkCache(daoFactory.getPartitionMetadataDao()); | ||
} else { | ||
watermarkCacheInstance = | ||
new LoadingWatermarkCache(daoFactory.getPartitionMetadataDao(), refreshRate); | ||
} | ||
} | ||
return watermarkCacheInstance; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...in/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/cache/LoadingWatermarkCache.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,56 @@ | ||
/* | ||
* 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.gcp.spanner.changestreams.cache; | ||
|
||
import com.google.cloud.Timestamp; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import javax.annotation.Nullable; | ||
import org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataDao; | ||
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.CacheBuilder; | ||
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.CacheLoader; | ||
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.LoadingCache; | ||
|
||
public class LoadingWatermarkCache implements WatermarkCache { | ||
private static final Object MIN_WATERMARK_KEY = new Object(); | ||
|
||
private final LoadingCache<Object, Optional<Timestamp>> cache; | ||
|
||
public LoadingWatermarkCache(PartitionMetadataDao dao, org.joda.time.Duration refreshRate) { | ||
this.cache = | ||
CacheBuilder.newBuilder() | ||
.refreshAfterWrite(Duration.ofMillis(refreshRate.getMillis())) | ||
.build( | ||
new CacheLoader<Object, Optional<Timestamp>>() { | ||
@Override | ||
public Optional<Timestamp> load(Object key) { | ||
return Optional.ofNullable(dao.getUnfinishedMinWatermark()); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public @Nullable Timestamp getUnfinishedMinWatermark() { | ||
try { | ||
return cache.get(MIN_WATERMARK_KEY).orElse(null); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/cache/NoOpWatermarkCache.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,36 @@ | ||
/* | ||
* 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.gcp.spanner.changestreams.cache; | ||
|
||
import com.google.cloud.Timestamp; | ||
import javax.annotation.Nullable; | ||
import org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataDao; | ||
|
||
public class NoOpWatermarkCache implements WatermarkCache { | ||
|
||
private final PartitionMetadataDao dao; | ||
|
||
public NoOpWatermarkCache(PartitionMetadataDao dao) { | ||
this.dao = dao; | ||
} | ||
|
||
@Override | ||
public @Nullable Timestamp getUnfinishedMinWatermark() { | ||
return dao.getUnfinishedMinWatermark(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/cache/WatermarkCache.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,26 @@ | ||
/* | ||
* 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.gcp.spanner.changestreams.cache; | ||
|
||
import com.google.cloud.Timestamp; | ||
import javax.annotation.Nullable; | ||
|
||
public interface WatermarkCache { | ||
@Nullable | ||
Timestamp getUnfinishedMinWatermark(); | ||
} |
20 changes: 20 additions & 0 deletions
20
...rm/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/cache/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. | ||
*/ | ||
|
||
/** Caching strategy for watermark. */ | ||
package org.apache.beam.sdk.io.gcp.spanner.changestreams.cache; |
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
Oops, something went wrong.