fix(xo-server/servers): always remove pool→connection association (#3782)

* fix(xo-server/servers): delete correctly the connection from the cashe

* useless findKey

* CHANGELOG

* fix error

* code improvement

* code improvement

* code improvement

* Update CHANGELOG.md

* fix errors

* Update xen-servers.js

* missing entry in CHANGELOG
This commit is contained in:
badrAZ 2018-12-13 12:21:02 +01:00 committed by Julien Fontanet
parent ece881c02c
commit d3b6d1a97f
3 changed files with 17 additions and 17 deletions

View File

@ -18,9 +18,11 @@
### Bug fixes
- [Home/SRs] Fixed SR status for non admin users [#2204](https://github.com/vatesfr/xen-orchestra/issues/2204) (PR [#3742](https://github.com/vatesfr/xen-orchestra/pull/3742))
- [Servers] Fix occasional "server's pool already connected" errors when pool is not connected (PR [#3782](https://github.com/vatesfr/xen-orchestra/pull/3782))
### Released packages
- xen-api v0.23.0
- xo-acl-resolver v0.4.1
- xo-server v5.32.0
- xo-web v5.32.0

View File

@ -407,8 +407,9 @@ export class Xapi extends EventEmitter {
auth.user,
auth.password,
]).then(
sessionId => {
async sessionId => {
this._sessionId = sessionId
this._pool = (await this.getAllRecords('pool'))[0]
debug('%s: connected', this._humanId)

View File

@ -1,9 +1,8 @@
import createLogger from '@xen-orchestra/log'
import { BaseError } from 'make-error'
import { pDelay, ignoreErrors } from 'promise-toolbox'
import { fibonacci } from 'iterable-backoff'
import { findKey } from 'lodash'
import { noSuchObject } from 'xo-common/api-errors'
import { pDelay, ignoreErrors } from 'promise-toolbox'
import Xapi from '../xapi'
import xapiObjectToXo from '../xapi-object-to-xo'
@ -36,10 +35,10 @@ export default class {
prefix: 'xo:server',
indexes: ['host'],
}))
this._serverIdsByPool = { __proto__: null }
this._stats = new XapiStats()
this._xapiOptions = xapiOptions
this._xapis = { __proto__: null }
this._xapisByPool = { __proto__: null }
this._xo = xo
xo.on('clean', () => serversDb.rebuildIndexes())
@ -265,13 +264,14 @@ export default class {
try {
await xapi.connect()
const xapisByPool = this._xapisByPool
const [{ $id: poolId }] = await xapi.getAllRecords('pool')
if (xapisByPool[poolId] !== undefined) {
const serverIdsByPool = this._serverIdsByPool
const poolId = xapi.pool.$id
if (serverIdsByPool[poolId] !== undefined) {
throw new PoolAlreadyConnected()
}
this._xapis[server.id] = xapisByPool[poolId] = xapi
serverIdsByPool[poolId] = server.id
this._xapis[server.id] = xapi
xapi.xo = (() => {
const conId = server.id
@ -383,11 +383,7 @@ export default class {
}
delete this._xapis[id]
const { pool } = xapi
if (pool != null) {
delete this._xapisByPool[pool.$id]
}
delete this._serverIdsByPool[xapi.pool.$id]
xapi.xo.uninstall()
return xapi.disconnect()
@ -408,8 +404,8 @@ export default class {
throw new Error(`object ${object.id} does not belong to a pool`)
}
const xapi = this._xapisByPool[poolId]
if (!xapi) {
const xapi = this._xapis[this._serverIdsByPool[poolId]]
if (xapi === undefined) {
throw new Error(`no connection found for object ${object.id}`)
}
@ -469,17 +465,18 @@ export default class {
}
this.unregisterXenServer(
findKey(this._xapis, candidate => candidate === sourceXapi)
this._serverIdsByPool[sourcePoolId]
)::ignoreErrors()
}
async detachHostFromPool(hostId) {
const xapi = this.getXapi(hostId)
const poolId = xapi.pool.$id
const { address } = xapi.getObject(hostId)
await xapi.ejectHostFromPool(hostId)
this._getXenServer(findKey(this._xapis, candidate => candidate === xapi))
this._getXenServer(this._serverIdsByPool[poolId])
.then(async ({ properties }) => {
const { id } = await this.registerXenServer({
...properties,