Skip to content
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(xen-api): automatically retry ro calls on ECONNRESET #5674

Merged
merged 2 commits into from Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -13,6 +13,7 @@

- [Pool] Fix `an error has occurred` when using the "Disconnect" button from the pool page [#5669](https://github.com/vatesfr/xen-orchestra/issues/5669) (PR [#5671](https://github.com/vatesfr/xen-orchestra/pull/5671))
- [Configuration] Automatically connect enabled servers after import [#5660](https://github.com/vatesfr/xen-orchestra/issues/5660) (PR [#5672](https://github.com/vatesfr/xen-orchestra/pull/5672))
- Work-around some `ECONNRESET` errors when connecting to XEN-API (PR [#5674](https://github.com/vatesfr/xen-orchestra/pull/5674))

### Packages to release

Expand All @@ -31,5 +32,6 @@
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.

- xen-api minor
- xo-server patch
- xo-web patch
28 changes: 21 additions & 7 deletions packages/xen-api/src/index.js
Expand Up @@ -86,6 +86,13 @@ export class Xapi extends EventEmitter {
this._readOnly = Boolean(opts.readOnly)
this._RecordsByType = { __proto__: null }

this._roCallRetryOptions = {
delay: 1e3,
tries: 10,
...opts.roCallRetryOptions,
when: { code: 'ECONNRESET' },
}

this._auth = opts.auth
const url = parseUrl(opts.url)
if (this._auth === undefined) {
Expand Down Expand Up @@ -231,7 +238,9 @@ export class Xapi extends EventEmitter {

// this should be used for instantaneous calls, otherwise use `callAsync`
call(method, ...args) {
return this._readOnly && !isReadOnlyCall(method, args)
return isReadOnlyCall(method, args)
? this._roCall(method, args)
: this._readOnly
? Promise.reject(new Error(`cannot call ${method}() in read only mode`))
: this._sessionCall(method, args)
}
Expand Down Expand Up @@ -261,23 +270,23 @@ export class Xapi extends EventEmitter {
// ===========================================================================

async getAllRecords(type) {
return map(await this._sessionCall(`${type}.get_all_records`), (record, ref) => this._wrapRecord(type, ref, record))
return map(await this._roCall(`${type}.get_all_records`), (record, ref) => this._wrapRecord(type, ref, record))
}

async getRecord(type, ref) {
return this._wrapRecord(type, ref, await this._sessionCall(`${type}.get_record`, [ref]))
return this._wrapRecord(type, ref, await this._roCall(`${type}.get_record`, [ref]))
}

async getRecordByUuid(type, uuid) {
return this.getRecord(type, await this._sessionCall(`${type}.get_by_uuid`, [uuid]))
return this.getRecord(type, await this._roCall(`${type}.get_by_uuid`, [uuid]))
}

getRecords(type, refs) {
return Promise.all(refs.map(ref => this.getRecord(type, ref)))
}

getField(type, ref, field) {
return this._sessionCall(`${type}.get_${field}`, [ref])
return this._roCall(`${type}.get_${field}`, [ref])
}

setField(type, ref, field, value) {
Expand Down Expand Up @@ -849,7 +858,7 @@ export class Xapi extends EventEmitter {
types.map(async type => {
try {
const toRemove = toRemoveByType[type]
const records = await this._sessionCall(`${type}.get_all_records`)
const records = await this._roCall(`${type}.get_all_records`)
const refs = getKeys(records)
refs.forEach(ref => {
toRemove.delete(ref)
Expand Down Expand Up @@ -900,6 +909,11 @@ export class Xapi extends EventEmitter {
}
}

// read-only call, automatically retry in case of connection issues
_roCall(method, args) {
return pRetry(() => this._sessionCall(method, args), this._roCallRetryOptions)
}

_watchEvents = coalesceCalls(this._watchEvents)
async _watchEvents() {
// eslint-disable-next-line no-labels
Expand Down Expand Up @@ -1015,7 +1029,7 @@ export class Xapi extends EventEmitter {

try {
await this._connected
this._processEvents(await this._sessionCall('event.next', undefined, EVENT_TIMEOUT * 1e3))
this._processEvents(await this._roCall('event.next', undefined, EVENT_TIMEOUT * 1e3))
} catch (error) {
if (error?.code === 'EVENTS_LOST') {
await ignoreErrors.call(this._sessionCall('event.unregister', [types]))
Expand Down