Skip to content

Commit

Permalink
grpc-js-xds: Implement ignore_resource_deletion option
Browse files Browse the repository at this point in the history
  • Loading branch information
murgatroid99 committed Aug 17, 2022
1 parent 1b1930f commit 8973e32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/grpc-js-xds/src/xds-client.ts
Expand Up @@ -360,6 +360,10 @@ export class XdsClient {
} else {
this.apiVersion = XdsApiVersion.V2;
}
if (bootstrapInfo.xdsServers[0].serverFeatures.indexOf('ignore_resource_deletion') >= 0) {
this.adsState.lds.enableIgnoreResourceDeletion();
this.adsState.cds.enableIgnoreResourceDeletion();
}
const nodeV2: NodeV2 = {
...bootstrapInfo.node,
build_version: `gRPC Node Pure JS ${clientVersion}`,
Expand Down
36 changes: 29 additions & 7 deletions packages/grpc-js-xds/src/xds-stream-state/xds-stream-state.ts
Expand Up @@ -75,6 +75,7 @@ interface SubscriptionEntry<ResponseType> {
watchers: Watcher<ResponseType>[];
cachedResponse: ResponseType | null;
resourceTimer: NodeJS.Timer;
deletionIgnored: boolean;
}

const RESOURCE_TIMEOUT_MS = 15_000;
Expand All @@ -86,11 +87,12 @@ export abstract class BaseXdsStreamState<ResponseType> implements XdsStreamState
private subscriptions: Map<string, SubscriptionEntry<ResponseType>> = new Map<string, SubscriptionEntry<ResponseType>>();
private latestIsV2 = false;
private isAdsStreamRunning = false;
private ignoreResourceDeletion = false;

constructor(private updateResourceNames: () => void) {}

protected trace(text: string) {
experimental.trace(logVerbosity.DEBUG, TRACER_NAME, this.getProtocolName() + ' | ' + text);
protected trace(text: string, verbosity = logVerbosity.DEBUG) {
experimental.trace(verbosity, TRACER_NAME, this.getProtocolName() + ' | ' + text);
}

private startResourceTimer(subscriptionEntry: SubscriptionEntry<ResponseType>) {
Expand All @@ -111,7 +113,8 @@ export abstract class BaseXdsStreamState<ResponseType> implements XdsStreamState
subscriptionEntry = {
watchers: [],
cachedResponse: null,
resourceTimer: setTimeout(() => {}, 0)
resourceTimer: setTimeout(() => {}, 0),
deletionIgnored: false
};
if (this.isAdsStreamRunning) {
this.startResourceTimer(subscriptionEntry);
Expand Down Expand Up @@ -142,6 +145,9 @@ export abstract class BaseXdsStreamState<ResponseType> implements XdsStreamState
}
if (subscriptionEntry.watchers.length === 0) {
clearTimeout(subscriptionEntry.resourceTimer);
if (subscriptionEntry.deletionIgnored) {
this.trace('Unsubscribing from resource with previously ignored deletion: ' + resourceName, LogVerbosity.INFO);
}
this.subscriptions.delete(resourceName);
this.updateResourceNames();
}
Expand Down Expand Up @@ -187,6 +193,10 @@ export abstract class BaseXdsStreamState<ResponseType> implements XdsStreamState
}
clearTimeout(subscriptionEntry.resourceTimer);
subscriptionEntry.cachedResponse = resource;
if (subscriptionEntry.deletionIgnored) {
this.trace('Received resource with previously ignored deletion: ' + resourceName, LogVerbosity.INFO);
subscriptionEntry.deletionIgnored = false;
}
}
}
result.missing = this.handleMissingNames(allResourceNames);
Expand Down Expand Up @@ -218,10 +228,18 @@ export abstract class BaseXdsStreamState<ResponseType> implements XdsStreamState
const missingNames: string[] = [];
for (const [resourceName, subscriptionEntry] of this.subscriptions.entries()) {
if (!allResponseNames.has(resourceName) && subscriptionEntry.cachedResponse !== null) {
this.trace('Reporting resource does not exist named ' + resourceName);
missingNames.push(resourceName);
for (const watcher of subscriptionEntry.watchers) {
watcher.onResourceDoesNotExist();
if (this.ignoreResourceDeletion) {
if (!subscriptionEntry.deletionIgnored) {
this.trace('Ignoring nonexistent resource ' + resourceName, LogVerbosity.ERROR);
subscriptionEntry.deletionIgnored = true;
}
} else {
this.trace('Reporting resource does not exist named ' + resourceName);
missingNames.push(resourceName);
for (const watcher of subscriptionEntry.watchers) {
watcher.onResourceDoesNotExist();
}
subscriptionEntry.cachedResponse = null;
}
}
}
Expand All @@ -231,6 +249,10 @@ export abstract class BaseXdsStreamState<ResponseType> implements XdsStreamState
}
}

enableIgnoreResourceDeletion() {
this.ignoreResourceDeletion = true;
}

/**
* Apply the validation rules for this resource type to this resource
* instance.
Expand Down

0 comments on commit 8973e32

Please sign in to comment.