Skip to content

Added retry function #63

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

Merged
merged 23 commits into from
Jun 27, 2023
Merged

Added retry function #63

merged 23 commits into from
Jun 27, 2023

Conversation

ilyakharev
Copy link
Contributor

@ilyakharev ilyakharev commented Jun 4, 2023

I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

Issue numbers: #61, #62

@ilyakharev ilyakharev self-assigned this Jun 4, 2023

namespace YdbPlatform\Ydb\Exceptions;

class FastBackoffRetryableException extends RetryableException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need Fast/Slow backoff in exceptions hierarchy.
Because it will difficult to change backoff type in future.

It may be property of exception, or (better) some function, which detect retry delay

* @return mixed|void|null
* @throws \Exception
*/
public function makeRequest(Closure $closure, bool $retryable){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the function need?


namespace YdbPlatform\Ydb\Traits;

trait RetryTrait
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is empty trait need?

src/Session.php Outdated
$result = $closure($this);
$this->commitTransaction();
return $result;
return $this->makeRequest(function ($session) use ($closure) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need the changes, retry only in retry function

src/Table.php Outdated
@@ -421,4 +424,54 @@ protected function streamRequest($method, array $data = [])
return $this->doStreamRequest('Table', $method, $data);
}

/**
* @param Closure $closure пользовательская функция, которую должен выполнить sdk.
* Функция принимает новую сессию
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need example:

retrySession(function($session){...})

src/Table.php Outdated
* Функция принимает новую сессию
* @return mixed результат выполнения функции
*/
public function retrySession(Closure $closure, int $waitTime = 100, bool $independent = false){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What unit of waitTime?
independent -> idempotent

src/Table.php Outdated
$waitBefore = microtime(true)+$waitTime/1000;
$requestCount = 0;
$session = $this->session();
while ($waitBefore>microtime(true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will good to reuse $Ydb->retry in the session instead of copy paste code.

src/Ydb.php Outdated
return $this->scripting;
}

public function retry(Closure $closure, int $waitTime = 100, bool $independent = false){
$waitBefore = microtime(true)+$waitTime*1e-3;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$waitTime*1e-3 - difficult to read and parse by human:

  1. science form is unusual in programs
  2. is that "($waitTime1e)-3" or "$waitTime(1e-3)"?

use YdbPlatform\Ydb\Auth\Auth;
use YdbPlatform\Ydb\Auth\TokenInfo;

class FakeCredentials extends Auth {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the file need in the PR?

$output->writeln('Table ' . $table_name . ' has been created.');

$session->transaction(function($session) use ($table_name, $columns) {
$session->query('upsert into `' . $table_name . '` (`' . implode('`, `', array_keys($columns)) . '`) values (' . implode('), (', $this->getData()) . ');');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert data in text of query id bad example.

I don't see is it your changes or original code.

src/Table.php Outdated
Comment on lines 470 to 471
$sessionId = $session->id();
$session->beginTransaction();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about use transaction function?

->primaryKey(['series_id', 'season_id'])
);

$this->print('Table `seasons` has been created.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here and in other places:

print should be out of retry


$output->writeln('Table ' . $table_name . ' has been created.');
$output->writeln('Table ' . $table_name . ' has been created.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$output->writeln mat be called multiply times if transaction will retry.

need separate create tables from requests to the tables.

{
$output->writeln('Empty directory');
}
$ydb->retry(function (Ydb $ydb) use ($output, $dirname) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retry should include only logic for work with ydb, for prevent bad examples.

prepare
retry (func (){work with database})
show/return result, render, etc.


class AbortedException extends RetryableException
{
public function isFastBackoff(): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the function need?

}

/**
* @throws NonRetryableException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be simpler - ydb/Exception?

@@ -158,9 +151,13 @@ protected function doStreamRequest($service, $method, $data = [])
*/
protected function checkStatus($service, $method, $status)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better rename to checkGrpcStatus, for prevent mistakes with check server status code.

);

$this->print('Table `episodes` has been created.');
$this->ydb->table()->retrySession(function (Session $session) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need set idempotend = true for this and other idempotent operations

@ilyakharev ilyakharev merged commit a9ea4c4 into ydb-platform:main Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants