forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(entity-service): fallback logic for aspect version (datahub-proj…
- Loading branch information
1 parent
852a23b
commit fc92d23
Showing
9 changed files
with
325 additions
and
99 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
69 changes: 69 additions & 0 deletions
69
metadata-io/src/main/java/com/linkedin/metadata/entity/TransactionContext.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,69 @@ | ||
package com.linkedin.metadata.entity; | ||
|
||
import io.ebean.DuplicateKeyException; | ||
import io.ebean.Transaction; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NonNull; | ||
import lombok.experimental.Accessors; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** Wrap the transaction with additional information about the exceptions during retry. */ | ||
@Data | ||
@AllArgsConstructor | ||
@Accessors(fluent = true) | ||
public class TransactionContext { | ||
public static final int DEFAULT_MAX_TRANSACTION_RETRY = 3; | ||
|
||
public static TransactionContext empty() { | ||
return empty(DEFAULT_MAX_TRANSACTION_RETRY); | ||
} | ||
|
||
public static TransactionContext empty(@Nullable Integer maxRetries) { | ||
return empty(null, maxRetries == null ? DEFAULT_MAX_TRANSACTION_RETRY : maxRetries); | ||
} | ||
|
||
public static TransactionContext empty(Transaction tx, int maxRetries) { | ||
return new TransactionContext(tx, maxRetries, new ArrayList<>()); | ||
} | ||
|
||
@Nullable private Transaction tx; | ||
private int maxRetries; | ||
@NonNull private List<RuntimeException> exceptions; | ||
|
||
public TransactionContext success() { | ||
exceptions.clear(); | ||
return this; | ||
} | ||
|
||
public TransactionContext addException(RuntimeException e) { | ||
exceptions.add(e); | ||
return this; | ||
} | ||
|
||
public int getFailedAttempts() { | ||
return exceptions.size(); | ||
} | ||
|
||
@Nullable | ||
public RuntimeException lastException() { | ||
return exceptions.isEmpty() ? null : exceptions.get(exceptions.size() - 1); | ||
} | ||
|
||
public boolean lastExceptionIsDuplicateKey() { | ||
return lastException() instanceof DuplicateKeyException; | ||
} | ||
|
||
public boolean shouldAttemptRetry() { | ||
return exceptions.size() <= maxRetries; | ||
} | ||
|
||
public void commitAndContinue() { | ||
if (tx != null) { | ||
tx.commitAndContinue(); | ||
} | ||
success(); | ||
} | ||
} |
Oops, something went wrong.