-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
977 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
x-pack/plugins/security/server/elasticsearch/elasticsearch_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { BehaviorSubject, combineLatest, Observable } from 'rxjs'; | ||
import { distinctUntilChanged, filter, map, shareReplay, tap } from 'rxjs/operators'; | ||
import { | ||
IClusterClient, | ||
ICustomClusterClient, | ||
Logger, | ||
ServiceStatusLevels, | ||
StatusServiceSetup, | ||
ElasticsearchServiceSetup as CoreElasticsearchServiceSetup, | ||
} from '../../../../../src/core/server'; | ||
import { SecurityLicense } from '../../common/licensing'; | ||
import { elasticsearchClientPlugin } from './elasticsearch_client_plugin'; | ||
|
||
export interface ElasticsearchServiceSetupParams { | ||
readonly elasticsearch: CoreElasticsearchServiceSetup; | ||
readonly status: StatusServiceSetup; | ||
readonly license: SecurityLicense; | ||
} | ||
|
||
export interface ElasticsearchServiceSetup { | ||
readonly clusterClient: IClusterClient; | ||
} | ||
|
||
export interface ElasticsearchServiceStart { | ||
readonly clusterClient: IClusterClient; | ||
readonly watchOnlineStatus$: () => Observable<OnlineStatusRetryScheduler>; | ||
} | ||
|
||
export interface OnlineStatusRetryScheduler { | ||
scheduleRetry: () => void; | ||
} | ||
|
||
export class ElasticsearchService { | ||
private clusterClient?: ICustomClusterClient; | ||
private coreStatus$!: Observable<boolean>; | ||
|
||
constructor(private readonly logger: Logger) {} | ||
|
||
setup({ | ||
elasticsearch, | ||
status, | ||
license, | ||
}: ElasticsearchServiceSetupParams): ElasticsearchServiceSetup { | ||
this.clusterClient = elasticsearch.legacy.createClient('security', { | ||
plugins: [elasticsearchClientPlugin], | ||
}); | ||
|
||
this.coreStatus$ = combineLatest([status.core$, license.features$]).pipe( | ||
map( | ||
([coreStatus]) => | ||
license.isEnabled() && coreStatus.elasticsearch.level === ServiceStatusLevels.available | ||
), | ||
shareReplay(1) | ||
); | ||
|
||
return { clusterClient: this.clusterClient }; | ||
} | ||
|
||
start(): ElasticsearchServiceStart { | ||
return { | ||
clusterClient: this.clusterClient!, | ||
watchOnlineStatus$: () => { | ||
const RETRY_SCALE_DURATION = 100; | ||
const RETRY_TIMEOUT_MAX = 10000; | ||
const retries$ = new BehaviorSubject(0); | ||
|
||
const retryScheduler = { | ||
scheduleRetry: () => { | ||
const retriesElapsed = retries$.getValue() + 1; | ||
const nextRetryTimeout = Math.min( | ||
retriesElapsed * RETRY_SCALE_DURATION, | ||
RETRY_TIMEOUT_MAX | ||
); | ||
|
||
this.logger.debug(`Scheduling re-try in ${nextRetryTimeout} ms.`); | ||
|
||
retryTimeout = setTimeout(() => retries$.next(retriesElapsed), nextRetryTimeout); | ||
}, | ||
}; | ||
|
||
let retryTimeout: NodeJS.Timeout; | ||
return combineLatest([ | ||
this.coreStatus$.pipe( | ||
tap(() => { | ||
// If status or license change occurred before retry timeout we should cancel | ||
// it and reset retry counter. | ||
if (retryTimeout) { | ||
clearTimeout(retryTimeout); | ||
} | ||
|
||
if (retries$.value > 0) { | ||
retries$.next(0); | ||
} | ||
}) | ||
), | ||
retries$.asObservable().pipe( | ||
// We shouldn't emit new value if retry counter is reset. This comparator isn't called for | ||
// the initial value. | ||
distinctUntilChanged((prev, curr) => prev === curr || curr === 0) | ||
), | ||
]).pipe( | ||
filter(([isAvailable]) => isAvailable), | ||
map(() => retryScheduler) | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
stop() { | ||
if (this.clusterClient) { | ||
this.clusterClient.close(); | ||
this.clusterClient = undefined; | ||
} | ||
} | ||
} |
Oops, something went wrong.