-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CDAP-21131] Delay in spanner table creation should not crash statele…
…ss pods
- Loading branch information
1 parent
c3bc2f2
commit 1939795
Showing
3 changed files
with
103 additions
and
2 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
84 changes: 84 additions & 0 deletions
84
...-spanner/src/main/java/io/cdap/cdap/storage/spanner/RetryingSpannerTransactionRunner.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,84 @@ | ||
/* | ||
* Copyright © 2025 Cask Data, Inc. | ||
* | ||
* Licensed 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 io.cdap.cdap.storage.spanner; | ||
|
||
import com.google.api.client.util.ExponentialBackOff; | ||
import com.google.common.base.Throwables; | ||
import io.cdap.cdap.spi.data.TableNotFoundException; | ||
import io.cdap.cdap.spi.data.transaction.TransactionException; | ||
import io.cdap.cdap.spi.data.transaction.TransactionRunner; | ||
import io.cdap.cdap.spi.data.transaction.TxRunnable; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Retries Cloud Spanner operations in case they fail due to retryable errors. | ||
*/ | ||
public class RetryingSpannerTransactionRunner implements TransactionRunner { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RetryingSpannerTransactionRunner.class); | ||
static final String MAX_RETRIES = "tx.runner.max.retries"; | ||
static final String INITIAL_DELAY_MILLIS = "tx.runner.initial.delay.ms"; | ||
private final int maxRetries; | ||
private final int initialDelayMs; | ||
private final SpannerTransactionRunner transactionRunner; | ||
|
||
RetryingSpannerTransactionRunner(Map<String, String> conf, SpannerStructuredTableAdmin admin) { | ||
this.maxRetries = Integer.parseInt(conf.get(MAX_RETRIES)); | ||
this.initialDelayMs = Integer.parseInt(conf.get(INITIAL_DELAY_MILLIS)); | ||
this.transactionRunner = new SpannerTransactionRunner(admin); | ||
} | ||
|
||
@Override | ||
public void run(TxRunnable runnable) throws TransactionException { | ||
ExponentialBackOff backOff = new ExponentialBackOff.Builder().setInitialIntervalMillis( | ||
initialDelayMs).build(); | ||
int counter = 0; | ||
while (counter < maxRetries) { | ||
counter++; | ||
try { | ||
transactionRunner.run(runnable); | ||
break; | ||
} catch (TransactionException e) { | ||
if (isRetryable(e)) { | ||
LOG.debug("Transaction failed with retryable exception", e); | ||
try { | ||
Thread.sleep(backOff.nextBackOffMillis()); | ||
} catch (InterruptedException | IOException ex) { | ||
// Reinstate the interrupt | ||
Thread.currentThread().interrupt(); | ||
// Fail with the original exception | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean isRetryable(TransactionException e) { | ||
List<Throwable> causes = Throwables.getCausalChain(e); | ||
for (Throwable cause : causes) { | ||
if (cause instanceof TableNotFoundException) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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