-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(pollux): Add retries field for ATL-3205 #380
Changes from 5 commits
74db310
ce738ae
f0bd386
dd0c7a5
e59d1f7
e615c3b
ef66528
f4afcde
739d037
da8f800
d5b6ba1
548a1df
33bfdef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
version = 3.5.9 | ||
runner.dialect = scala3 | ||
|
||
maxColumn = 120 | ||
maxColumn = 120 | ||
trailingCommas = preserve |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
ALTER TABLE public.issue_credential_records | ||
ADD COLUMN "issuing_did" TEXT; | ||
-- ALTER TABLE public.issue_credential_records | ||
FabioPinheiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- ADD COLUMN "issuing_did" TEXT; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- ALTER TABLE public.issue_credential_records | ||
-- ADD meta_retries BIGINT NOT NULL DEFAULT '3', | ||
-- ADD meta_next_retry TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
-- ADD meta_last_failure TEXT; | ||
FabioPinheiro marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,10 @@ import io.circe.syntax._ | |
import io.iohk.atala.mercury.protocol.issuecredential.IssueCredential | ||
import io.iohk.atala.mercury.protocol.issuecredential.OfferCredential | ||
import io.iohk.atala.mercury.protocol.issuecredential.RequestCredential | ||
import io.iohk.atala.castor.core.model.did._ | ||
import io.iohk.atala.pollux.core.model._ | ||
import io.iohk.atala.pollux.core.model.IssueCredentialRecord.ProtocolState | ||
import io.iohk.atala.pollux.core.model.* | ||
import io.iohk.atala.pollux.core.model.error.CredentialRepositoryError | ||
import io.iohk.atala.pollux.core.model.error.CredentialRepositoryError._ | ||
import io.iohk.atala.pollux.core.repository.CredentialRepository | ||
import io.iohk.atala.pollux.sql.model.JWTCredentialRow | ||
|
@@ -23,8 +25,9 @@ import zio.interop.catz.* | |
|
||
import java.time.Instant | ||
import java.util.UUID | ||
import io.iohk.atala.castor.core.model.did.CanonicalPrismDID | ||
import io.iohk.atala.castor.core.model.did.PrismDID | ||
|
||
import org.postgresql.util.PSQLState | ||
import java.sql.SQLException | ||
|
||
// TODO: replace with actual implementation | ||
class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepository[Task] { | ||
|
@@ -47,8 +50,9 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
given uuidGet: Get[UUID] = Get[String].map(UUID.fromString) | ||
given uuidPut: Put[UUID] = Put[String].contramap(_.toString()) | ||
|
||
given instantGet: Get[Instant] = Get[Long].map(Instant.ofEpochSecond) | ||
FabioPinheiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
given instantPut: Put[Instant] = Put[Long].contramap(_.getEpochSecond()) | ||
// given instantGet: Get[Instant] = Get[Long].map(Instant.ofEpochSecond) | ||
// given instantPut: Put[Instant] = Put[Long].contramap(_.getEpochSecond()) | ||
import doobie.postgres.implicits.JavaTimeInstantMeta | ||
|
||
given protocolStateGet: Get[ProtocolState] = Get[String].map(ProtocolState.valueOf) | ||
given protocolStatePut: Put[ProtocolState] = Put[String].contramap(_.toString) | ||
|
@@ -93,7 +97,10 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
| request_credential_data, | ||
| issue_credential_data, | ||
| issued_credential_raw, | ||
| issuing_did | ||
| issuing_did, | ||
| meta_retries, | ||
| meta_next_retry, | ||
| meta_last_failure | ||
| ) values ( | ||
| ${record.id}, | ||
| ${record.createdAt}, | ||
|
@@ -111,14 +118,17 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
| ${record.requestCredentialData}, | ||
| ${record.issueCredentialData}, | ||
| ${record.issuedCredentialRaw}, | ||
| ${record.issuingDID} | ||
| ${record.issuingDID}, | ||
| ${record.metaRetries}, | ||
| ${record.metaNextRetry}, | ||
| ${record.metaLastFailure} | ||
| ) | ||
""".stripMargin.update | ||
|
||
cxnIO.run | ||
.transact(xa) | ||
.mapError { | ||
case e: PSQLException => UniqueConstraintViolation(e.getMessage()) | ||
case e: PSQLException => CredentialRepositoryError.fromPSQLException(e.getSQLState, e.getMessage) | ||
case e => e | ||
} | ||
} | ||
|
@@ -142,7 +152,10 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
| request_credential_data, | ||
| issue_credential_data, | ||
| issued_credential_raw, | ||
| issuing_did | ||
| issuing_did, | ||
| meta_retries, | ||
| meta_next_retry, | ||
| meta_last_failure | ||
| FROM public.issue_credential_records | ||
""".stripMargin | ||
.query[IssueCredentialRecord] | ||
|
@@ -179,7 +192,10 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
| request_credential_data, | ||
| issue_credential_data, | ||
| issued_credential_raw, | ||
| issuing_did | ||
| issuing_did, | ||
| meta_retries, | ||
| meta_next_retry, | ||
| meta_last_failure | ||
| FROM public.issue_credential_records | ||
| WHERE $inClauseFragment | ||
""".stripMargin | ||
|
@@ -209,7 +225,10 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
| request_credential_data, | ||
| issue_credential_data, | ||
| issued_credential_raw, | ||
| issuing_did | ||
| issuing_did, | ||
| meta_retries, | ||
| meta_next_retry, | ||
| meta_last_failure | ||
| FROM public.issue_credential_records | ||
| WHERE id = $recordId | ||
""".stripMargin | ||
|
@@ -239,7 +258,10 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
| request_credential_data, | ||
| issue_credential_data, | ||
| issued_credential_raw, | ||
| issuing_did | ||
| issuing_did, | ||
| meta_retries, | ||
| meta_next_retry, | ||
| meta_last_failure | ||
| FROM public.issue_credential_records | ||
| WHERE thid = $thid | ||
""".stripMargin | ||
|
@@ -409,6 +431,23 @@ class JdbcCredentialRepository(xa: Transactor[Task]) extends CredentialRepositor | |
cxnIO.run | ||
.transact(xa) | ||
} | ||
|
||
def updateAfterFail( | ||
recordId: UUID, | ||
failReason: Option[String] | ||
): Task[Int] = { | ||
val cxnIO = sql""" | ||
| UPDATE public.issue_credential_records | ||
| SET | ||
| meta_retries = meta_retries - 1, | ||
| meta_next_retry = ${Instant.now().plusSeconds(60)}, | ||
| meta_last_failure = ${failReason} | ||
| WHERE | ||
| id = $recordId | ||
""".stripMargin.update | ||
// FIXME meta_next_retry = ${if (record.metaRetries - 1 == 0) None else Some(Instant.now().plusSeconds(60))} // TODO exponention time | ||
cxnIO.run.transact(xa) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why have not you implemented There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mainly because I am not good with SQL, and I forgot There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you necessarily need to write one update query that does all the logic via SQL, I mean that would be ideal but, you can just make one SELECT statement, get values, calculate via regular scala code, and then run UPDATE with that value. Yes it is not ideal and we are making 2 queries instead of one but it is better than incorrect logic, and after all, if this becomes a performance issue we can figure it out later. p.s
I don't know, I'd have to google myself :p There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer those operations to be atomic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to implement this logic via DB query that is fine with me (it is good actually). If you don't want to do it right now in this PR, then I'd ask to align this logic with InMemory implementation logic, because otherwise, inMemory has exponential increase and regular DB does not, I think that would be quite confusing for other devs who will work on this codebase. Not having an exponential increase is fina for the first iteration, in my opinion, just want to make sure 2 implementations are aligned. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @bvoiturier There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I create a ticket https://input-output.atlassian.net/browse/ATL-3686 |
||
} | ||
|
||
object JdbcCredentialRepository { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an error string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FabioPinheiro Maybe we can replace the error string with a wrapper error like this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good point, let me try.