-
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 (#33566)
- Loading branch information
1 parent
5e59465
commit 7c9fb33
Showing
12 changed files
with
381 additions
and
8 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
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
64 changes: 64 additions & 0 deletions
64
...main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/cache/AsyncWatermarkCache.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,64 @@ | ||
/* | ||
* 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.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executors; | ||
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; | ||
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import org.joda.time.Duration; | ||
|
||
/** | ||
* Asynchronously compute the earliest partition watermark and stores it in memory. The value will | ||
* be recomputed periodically, as configured by the refresh rate. | ||
* | ||
* <p>On every period, we will call {@link PartitionMetadataDao#getUnfinishedMinWatermark()} to | ||
* refresh the value. | ||
*/ | ||
public class AsyncWatermarkCache implements WatermarkCache { | ||
|
||
private static final String THREAD_NAME_FORMAT = "watermark_loading_thread_%d"; | ||
private static final Object MIN_WATERMARK_KEY = new Object(); | ||
private final LoadingCache<Object, Optional<Timestamp>> cache; | ||
|
||
public AsyncWatermarkCache(PartitionMetadataDao dao, Duration refreshRate) { | ||
this.cache = | ||
CacheBuilder.newBuilder() | ||
.refreshAfterWrite(java.time.Duration.ofMillis(refreshRate.getMillis())) | ||
.build( | ||
CacheLoader.asyncReloading( | ||
CacheLoader.from(key -> Optional.ofNullable(dao.getUnfinishedMinWatermark())), | ||
Executors.newSingleThreadExecutor( | ||
new ThreadFactoryBuilder().setNameFormat(THREAD_NAME_FORMAT).build()))); | ||
} | ||
|
||
@Override | ||
public @Nullable Timestamp getUnfinishedMinWatermark() { | ||
try { | ||
return cache.get(MIN_WATERMARK_KEY).orElse(null); | ||
} catch (ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...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,58 @@ | ||
/* | ||
* 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 java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.DaoFactory; | ||
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; | ||
import org.joda.time.Duration; | ||
|
||
public class CacheFactory implements Serializable { | ||
|
||
private static final long serialVersionUID = -8722905670370252723L; | ||
private static final Map<Long, WatermarkCache> WATERMARK_CACHE = new ConcurrentHashMap<>(); | ||
private static final AtomicLong CACHE_ID = new AtomicLong(); | ||
|
||
// The unique id for the cache of this CacheFactory. This guarantees that if the CacheFactory is | ||
// serialized / deserialized it will get the same instance of the factory. | ||
private final long cacheId = CACHE_ID.getAndIncrement(); | ||
private final DaoFactory daoFactory; | ||
private final Duration refreshRate; | ||
|
||
public CacheFactory(DaoFactory daoFactory, Duration watermarkRefreshRate) { | ||
this.daoFactory = daoFactory; | ||
this.refreshRate = watermarkRefreshRate; | ||
} | ||
|
||
public WatermarkCache getWatermarkCache() { | ||
return WATERMARK_CACHE.computeIfAbsent( | ||
cacheId, | ||
key -> | ||
refreshRate.getMillis() == 0 | ||
? new NoOpWatermarkCache(daoFactory.getPartitionMetadataDao()) | ||
: new AsyncWatermarkCache(daoFactory.getPartitionMetadataDao(), refreshRate)); | ||
} | ||
|
||
@VisibleForTesting | ||
long getCacheId() { | ||
return cacheId; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../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,40 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Synchronously compute the earliest partition watermark, by delegating the call to {@link | ||
* PartitionMetadataDao#getUnfinishedMinWatermark()}. | ||
*/ | ||
public class NoOpWatermarkCache implements WatermarkCache { | ||
|
||
private final PartitionMetadataDao dao; | ||
|
||
public NoOpWatermarkCache(PartitionMetadataDao dao) { | ||
this.dao = dao; | ||
} | ||
|
||
@Override | ||
public @Nullable Timestamp getUnfinishedMinWatermark() { | ||
return dao.getUnfinishedMinWatermark(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../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,35 @@ | ||
/* | ||
* 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.model.PartitionMetadata.State; | ||
|
||
@FunctionalInterface | ||
public interface WatermarkCache { | ||
|
||
/** | ||
* Fetches the earliest partition watermark from the partition metadata table that is not in a | ||
* {@link State#FINISHED} state. | ||
* | ||
* @return the earliest partition watermark which is not in a {@link State#FINISHED} state. | ||
*/ | ||
@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; |
Oops, something went wrong.