Skip to content

Commit

Permalink
Merge pull request #217 from weaviate/grpc/add-retry-middleware-for-u…
Browse files Browse the repository at this point in the history
…navailable

Use `nice-grpc-client-middleware-retry` for auto-retry of UNAVAILABLE grpc error
  • Loading branch information
tsmith023 authored Oct 25, 2024
2 parents 39b876f + 90e1a9f commit b36d1fb
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 151 deletions.
360 changes: 218 additions & 142 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"graphql-request": "^6.1.0",
"long": "^5.2.3",
"nice-grpc": "^2.1.9",
"nice-grpc-client-middleware-deadline": "^2.0.12",
"nice-grpc-client-middleware-retry": "^3.1.9",
"uuid": "^9.0.1"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/connection/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { InternalConnectionParams } from './http.js';
import { ConsistencyLevel } from '../data/index.js';

import { ChannelCredentials, ChannelOptions, createChannel, createClientFactory, Metadata } from 'nice-grpc';
import { deadlineMiddleware } from 'nice-grpc-client-middleware-deadline';
import { retryMiddleware } from 'nice-grpc-client-middleware-retry';

import { HealthCheckResponse_ServingStatus, HealthDefinition } from '../proto/google/health/v1/health.js';
import { WeaviateDefinition } from '../proto/v1/weaviate.js';
Expand All @@ -23,7 +23,7 @@ export interface GrpcConnectionParams extends InternalConnectionParams {
grpcSecure: boolean;
}

const clientFactory = createClientFactory().use(deadlineMiddleware);
const clientFactory = createClientFactory().use(retryMiddleware);

const MAX_GRPC_MESSAGE_LENGTH = 104858000; // 10mb, needs to be synchronized with GRPC server

Expand Down
5 changes: 3 additions & 2 deletions src/grpc/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { isAbortError } from 'abort-controller-x';
import { ConsistencyLevel } from '../data/index.js';

import { Metadata } from 'nice-grpc';
import { RetryOptions } from 'nice-grpc-client-middleware-retry';
import { WeaviateRequestTimeoutError } from '../errors.js';
import { ConsistencyLevel as ConsistencyLevelGRPC } from '../proto/v1/base.js';
import { WeaviateClient } from '../proto/v1/weaviate.js';

export default class Base {
protected connection: WeaviateClient;
protected connection: WeaviateClient<RetryOptions>;
protected collection: string;
protected timeout: number;
protected consistencyLevel?: ConsistencyLevelGRPC;
protected tenant?: string;
protected metadata?: Metadata;

protected constructor(
connection: WeaviateClient,
connection: WeaviateClient<RetryOptions>,
collection: string,
metadata: Metadata,
timeout: number,
Expand Down
6 changes: 5 additions & 1 deletion src/grpc/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { ConsistencyLevel } from '../data/index.js';
import { BatchObject, BatchObjectsReply, BatchObjectsRequest } from '../proto/v1/batch.js';
import { WeaviateClient } from '../proto/v1/weaviate.js';

import { RetryOptions } from 'nice-grpc-client-middleware-retry';
import { WeaviateBatchError, WeaviateDeleteManyError } from '../errors.js';
import { Filters } from '../proto/v1/base.js';
import { BatchDeleteReply, BatchDeleteRequest } from '../proto/v1/batch_delete.js';
import Base from './base.js';

import { retryOptions } from './retry.js';

export interface Batch {
withDelete: (args: BatchDeleteArgs) => Promise<BatchDeleteReply>;
withObjects: (args: BatchObjectsArgs) => Promise<BatchObjectsReply>;
Expand All @@ -27,7 +30,7 @@ export interface BatchDeleteArgs {

export default class Batcher extends Base implements Batch {
public static use(
connection: WeaviateClient,
connection: WeaviateClient<RetryOptions>,
collection: string,
metadata: Metadata,
timeout: number,
Expand Down Expand Up @@ -70,6 +73,7 @@ export default class Batcher extends Base implements Batch {
{
metadata: this.metadata,
signal,
...retryOptions,
}
)
.catch((err) => {
Expand Down
11 changes: 11 additions & 0 deletions src/grpc/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ClientError, Status } from 'nice-grpc';
import { RetryOptions } from 'nice-grpc-client-middleware-retry';

export const retryOptions: RetryOptions = {
retry: true,
retryMaxAttempts: 5,
retryableStatuses: [Status.UNAVAILABLE],
onRetryableError(error: ClientError, attempt: number, delayMs: number) {
console.warn(error, `Attempt ${attempt} failed. Retrying in ${delayMs}ms.`);
},
};
5 changes: 4 additions & 1 deletion src/grpc/searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import {
} from '../proto/v1/search_get.js';
import { WeaviateClient } from '../proto/v1/weaviate.js';

import { RetryOptions } from 'nice-grpc-client-middleware-retry';
import { WeaviateQueryError } from '../errors.js';
import { GenerativeSearch } from '../proto/v1/generative.js';
import Base from './base.js';
import { retryOptions } from './retry.js';

export type SearchFetchArgs = {
limit?: number;
Expand Down Expand Up @@ -113,7 +115,7 @@ export interface Search {

export default class Searcher extends Base implements Search {
public static use(
connection: WeaviateClient,
connection: WeaviateClient<RetryOptions>,
collection: string,
metadata: Metadata,
timeout: number,
Expand Down Expand Up @@ -151,6 +153,7 @@ export default class Searcher extends Base implements Search {
{
metadata: this.metadata,
signal,
...retryOptions,
}
)
.catch((err) => {
Expand Down
7 changes: 5 additions & 2 deletions src/grpc/tenantsManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Metadata } from 'nice-grpc';
import { RetryOptions } from 'nice-grpc-client-middleware-retry';
import { TenantsGetReply, TenantsGetRequest } from '../proto/v1/tenants.js';
import { WeaviateClient } from '../proto/v1/weaviate.js';
import Base from './base.js';
import { retryOptions } from './retry.js';

export type TenantsGetArgs = {
names?: string[];
Expand All @@ -11,9 +13,9 @@ export interface Tenants {
withGet: (args: TenantsGetArgs) => Promise<TenantsGetReply>;
}

export default class TenantsManager extends Base implements TenantsManager {
export default class TenantsManager extends Base implements Tenants {
public static use(
connection: WeaviateClient,
connection: WeaviateClient<RetryOptions>,
collection: string,
metadata: Metadata,
timeout: number
Expand All @@ -34,6 +36,7 @@ export default class TenantsManager extends Base implements TenantsManager {
{
metadata: this.metadata,
signal,
...retryOptions,
}
)
);
Expand Down

0 comments on commit b36d1fb

Please sign in to comment.