-
Notifications
You must be signed in to change notification settings - Fork 9
chore: refactor efm plugins #662
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
Open
karenc-bq
wants to merge
2
commits into
chore/remove-aliases
Choose a base branch
from
refactor/monitor-service
base: chore/remove-aliases
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,147 @@ | ||
| /* | ||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"). | ||
| You may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| import { ClientWrapper } from "../../../client_wrapper"; | ||
| import { TelemetryCounter } from "../../../utils/telemetry/telemetry_counter"; | ||
| import { logger } from "../../../../logutils"; | ||
| import { Messages } from "../../../utils/messages"; | ||
| import { getCurrentTimeNano } from "../../../utils/utils"; | ||
|
|
||
| /** | ||
| * Monitoring context for each connection. This contains each connection's criteria for whether a | ||
| * host should be considered unhealthy. The context is shared between the main task and the | ||
| * monitor task. | ||
| */ | ||
| export interface ConnectionContext { | ||
| readonly failureDetectionIntervalMillis: number; | ||
| readonly failureDetectionCount: number; | ||
| readonly expectedActiveMonitoringStartTimeNano: number; | ||
|
|
||
| isActiveContext(): boolean; | ||
| isHostUnhealthy(): boolean; | ||
| setInactive(): void; | ||
| abortConnection(): Promise<void>; | ||
| updateConnectionStatus(hostName: string, statusCheckStartTimeNano: number, statusCheckEndTimeNano: number, isValid: boolean): Promise<void>; | ||
| } | ||
|
|
||
| export class ConnectionContextImpl implements ConnectionContext { | ||
| readonly failureDetectionIntervalMillis: number; | ||
| readonly failureDetectionCount: number; | ||
| readonly expectedActiveMonitoringStartTimeNano: number; | ||
|
|
||
| private readonly failureDetectionTimeMillis: number; | ||
| private readonly connectionToAbortRef: WeakRef<ClientWrapper>; | ||
| private readonly abortedConnectionsCounter: TelemetryCounter; | ||
| private readonly startMonitorTimeNano: number; | ||
|
|
||
| private _activeContext: boolean = true; | ||
| private _hostUnhealthy: boolean = false; | ||
| private invalidHostStartTimeNano: number = 0; | ||
| private failureCount: number = 0; | ||
|
|
||
| constructor( | ||
| connectionToAbort: ClientWrapper, | ||
| failureDetectionTimeMillis: number, | ||
| failureDetectionIntervalMillis: number, | ||
| failureDetectionCount: number, | ||
| abortedConnectionsCounter: TelemetryCounter | ||
| ) { | ||
| this.connectionToAbortRef = new WeakRef(connectionToAbort); | ||
| this.failureDetectionTimeMillis = failureDetectionTimeMillis; | ||
| this.failureDetectionIntervalMillis = failureDetectionIntervalMillis; | ||
| this.failureDetectionCount = failureDetectionCount; | ||
| this.abortedConnectionsCounter = abortedConnectionsCounter; | ||
| this.startMonitorTimeNano = getCurrentTimeNano(); | ||
| this.expectedActiveMonitoringStartTimeNano = this.startMonitorTimeNano + this.failureDetectionTimeMillis * 1_000_000; | ||
| } | ||
|
|
||
| isActiveContext(): boolean { | ||
| return this._activeContext; | ||
| } | ||
|
|
||
| isHostUnhealthy(): boolean { | ||
| return this._hostUnhealthy; | ||
| } | ||
|
|
||
| setInactive(): void { | ||
| this._activeContext = false; | ||
| } | ||
|
|
||
| async abortConnection(): Promise<void> { | ||
| const connectionToAbort = this.connectionToAbortRef.deref(); | ||
| if (connectionToAbort == null || !this._activeContext) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await connectionToAbort.abort(); | ||
| this.abortedConnectionsCounter.inc(); | ||
| } catch (error: any) { | ||
| // ignore | ||
| logger.debug(Messages.get("MonitorConnectionContext.errorAbortingConnection", error.message)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update whether the connection is still valid if the total elapsed time has passed the | ||
| * grace period. | ||
| */ | ||
| async updateConnectionStatus(hostName: string, statusCheckStartTimeNano: number, statusCheckEndTimeNano: number, isValid: boolean): Promise<void> { | ||
| if (!this._activeContext) { | ||
| return; | ||
| } | ||
|
|
||
| const totalElapsedTimeNano = statusCheckEndTimeNano - this.startMonitorTimeNano; | ||
|
|
||
| if (totalElapsedTimeNano > this.failureDetectionTimeMillis * 1_000_000) { | ||
| await this.setConnectionValid(hostName, isValid, statusCheckStartTimeNano, statusCheckEndTimeNano); | ||
| } | ||
| } | ||
|
|
||
| private async setConnectionValid( | ||
| hostName: string, | ||
| connectionValid: boolean, | ||
| statusCheckStartNano: number, | ||
| statusCheckEndNano: number | ||
| ): Promise<void> { | ||
| if (!connectionValid) { | ||
| this.failureCount++; | ||
|
|
||
| if (this.invalidHostStartTimeNano === 0) { | ||
| this.invalidHostStartTimeNano = statusCheckStartNano; | ||
| } | ||
|
|
||
| const invalidHostDurationNano = statusCheckEndNano - this.invalidHostStartTimeNano; | ||
| const maxInvalidHostDurationNano = this.failureDetectionIntervalMillis * Math.max(0, this.failureDetectionCount) * 1_000_000; | ||
|
|
||
| if (invalidHostDurationNano >= maxInvalidHostDurationNano) { | ||
| logger.debug(Messages.get("MonitorConnectionContext.hostDead", hostName)); | ||
| this._hostUnhealthy = true; | ||
| await this.abortConnection(); | ||
| return; | ||
| } | ||
|
|
||
| logger.debug(Messages.get("MonitorConnectionContext.hostNotResponding", hostName)); | ||
| return; | ||
| } | ||
|
|
||
| this.failureCount = 0; | ||
| this.invalidHostStartTimeNano = 0; | ||
| this._hostUnhealthy = false; | ||
|
|
||
| logger.debug(Messages.get("MonitorConnectionContext.hostAlive", hostName)); | ||
| } | ||
| } | ||
This file contains hidden or 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,230 @@ | ||
| /* | ||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"). | ||
| You may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| import { ConnectionContext } from "./connection_context"; | ||
| import { HostInfo } from "../../../host_info"; | ||
| import { PluginService } from "../../../plugin_service"; | ||
| import { ClientWrapper } from "../../../client_wrapper"; | ||
| import { WrapperProperties } from "../../../wrapper_property"; | ||
| import { logger } from "../../../../logutils"; | ||
| import { Messages } from "../../../utils/messages"; | ||
| import { getCurrentTimeNano } from "../../../utils/utils"; | ||
| import { getTimeInNanos } from "../../../utils/utils"; | ||
| import { TelemetryCounter } from "../../../utils/telemetry/telemetry_counter"; | ||
| import { TelemetryFactory } from "../../../utils/telemetry/telemetry_factory"; | ||
| import { TelemetryTraceLevel } from "../../../utils/telemetry/telemetry_trace_level"; | ||
| import { AbstractMonitor, MonitorState } from "../../../utils/monitoring/monitor"; | ||
|
|
||
| export interface HostMonitor { | ||
| startMonitoring(context: ConnectionContext): void; | ||
|
|
||
| stopMonitoring(context: ConnectionContext): void; | ||
|
|
||
| clearContexts(): void; | ||
|
|
||
| isStopped(): boolean; | ||
|
|
||
| run(): Promise<void>; | ||
|
|
||
| releaseResources(): Promise<void>; | ||
| } | ||
|
|
||
| type ConnectionStatus = [isValid: boolean, elapsedTimeNano: number]; | ||
|
|
||
| export class HostMonitorImpl extends AbstractMonitor implements HostMonitor { | ||
| private static readonly SLEEP_WHEN_INACTIVE_MILLIS = 100; | ||
| private static readonly MIN_CONNECTION_CHECK_TIMEOUT_MILLIS = 3000; | ||
| private static readonly MONITOR_TERMINATION_TIMEOUT_SEC = 30; | ||
|
|
||
| private readonly pluginService: PluginService; | ||
| private readonly telemetryFactory: TelemetryFactory; | ||
| private readonly hostInvalidCounter: TelemetryCounter; | ||
| private readonly properties: Map<string, any>; | ||
| private readonly hostInfo: HostInfo; | ||
| private readonly monitorDisposalTimeMillis: number; | ||
| private contexts: ConnectionContext[] = []; | ||
|
|
||
| private contextLastUsedTimestampNano: number; | ||
| private monitoringClient: ClientWrapper | null = null; | ||
| private delayTimeoutId: ReturnType<typeof setTimeout> | undefined; | ||
| private sleepTimeoutId: ReturnType<typeof setTimeout> | undefined; | ||
|
|
||
| constructor(pluginService: PluginService, hostInfo: HostInfo, properties: Map<string, any>, monitorDisposalTimeMillis: number) { | ||
| super(HostMonitorImpl.MONITOR_TERMINATION_TIMEOUT_SEC); | ||
| this.pluginService = pluginService; | ||
| this.telemetryFactory = this.pluginService.getTelemetryFactory(); | ||
| this.hostInfo = hostInfo; | ||
| this.properties = properties; | ||
| this.monitorDisposalTimeMillis = monitorDisposalTimeMillis; | ||
| this.hostInvalidCounter = this.telemetryFactory.createCounter(`efm.nodeUnhealthy.count.${hostInfo.hostId || hostInfo.host}`); | ||
| this.contextLastUsedTimestampNano = getCurrentTimeNano(); | ||
| } | ||
|
|
||
| startMonitoring(context: ConnectionContext): void { | ||
| if (this._stop) { | ||
| logger.warn(Messages.get("MonitorImpl.monitorIsStopped", this.hostInfo.host)); | ||
| } | ||
|
|
||
| this.contextLastUsedTimestampNano = getCurrentTimeNano(); | ||
| this.lastActivityTimestampNanos = getTimeInNanos(); | ||
| this.contexts.push(context); | ||
| } | ||
|
|
||
| stopMonitoring(context: ConnectionContext): void { | ||
| if (!context) { | ||
| logger.warn(Messages.get("MonitorImpl.contextNullWarning")); | ||
| return; | ||
| } | ||
|
|
||
| context.setInactive(); | ||
| this.contextLastUsedTimestampNano = getCurrentTimeNano(); | ||
| this.lastActivityTimestampNanos = getTimeInNanos(); | ||
| } | ||
|
|
||
| clearContexts(): void { | ||
| this.contexts.length = 0; | ||
| } | ||
|
|
||
| isStopped(): boolean { | ||
| return this._stop || this.state === MonitorState.STOPPED; | ||
| } | ||
|
|
||
| canDispose(): boolean { | ||
| return this.contexts.length === 0; | ||
| } | ||
|
|
||
| async monitor(): Promise<void> { | ||
| logger.debug(Messages.get("MonitorImpl.startMonitoring", this.hostInfo.host)); | ||
|
|
||
| try { | ||
| while (!this._stop) { | ||
| try { | ||
| this.lastActivityTimestampNanos = getTimeInNanos(); | ||
|
|
||
| const activeContexts = this.contexts.filter((ctx) => ctx.isActiveContext()); | ||
|
|
||
| if (activeContexts.length > 0) { | ||
| this.contextLastUsedTimestampNano = getCurrentTimeNano(); | ||
|
|
||
| const statusCheckStartTimeNano = getCurrentTimeNano(); | ||
| const [isValid, elapsedTimeNano] = await this.checkConnectionStatus(); | ||
|
|
||
| let delayMillis = -1; | ||
|
|
||
| for (const context of activeContexts) { | ||
| if (!context.isActiveContext()) { | ||
| continue; | ||
| } | ||
|
|
||
| await context.updateConnectionStatus(this.hostInfo.url, statusCheckStartTimeNano, statusCheckStartTimeNano + elapsedTimeNano, isValid); | ||
|
|
||
| if ( | ||
| context.isActiveContext() && | ||
| !context.isHostUnhealthy() && | ||
| statusCheckStartTimeNano >= context.expectedActiveMonitoringStartTimeNano | ||
| ) { | ||
| if (delayMillis === -1 || delayMillis > context.failureDetectionIntervalMillis) { | ||
| delayMillis = context.failureDetectionIntervalMillis; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| this.contexts = this.contexts.filter((ctx) => ctx.isActiveContext() && !ctx.isHostUnhealthy()); | ||
|
|
||
| if (delayMillis === -1) { | ||
| delayMillis = HostMonitorImpl.SLEEP_WHEN_INACTIVE_MILLIS; | ||
| } else { | ||
| delayMillis -= Math.round(elapsedTimeNano / 1_000_000); | ||
| if (delayMillis <= 0) { | ||
| delayMillis = HostMonitorImpl.MIN_CONNECTION_CHECK_TIMEOUT_MILLIS; | ||
| } | ||
| } | ||
|
|
||
| await new Promise<void>((resolve) => { | ||
| this.delayTimeoutId = setTimeout(resolve, delayMillis); | ||
| }); | ||
| } else { | ||
| if (getCurrentTimeNano() - this.contextLastUsedTimestampNano >= this.monitorDisposalTimeMillis * 1_000_000) { | ||
| break; | ||
| } | ||
| await new Promise<void>((resolve) => { | ||
| this.sleepTimeoutId = setTimeout(resolve, HostMonitorImpl.SLEEP_WHEN_INACTIVE_MILLIS); | ||
| }); | ||
| } | ||
| } catch (error: any) { | ||
| logger.debug(Messages.get("MonitorImpl.errorDuringMonitoringContinue", error.message)); | ||
| } | ||
| } | ||
| } catch (error: any) { | ||
| logger.debug(Messages.get("MonitorImpl.errorDuringMonitoringStop", error.message)); | ||
| } | ||
|
|
||
| logger.debug(Messages.get("MonitorImpl.stopMonitoring", this.hostInfo.host)); | ||
| } | ||
|
|
||
| async close(): Promise<void> { | ||
| this.contexts.length = 0; | ||
| await this.closeMonitoringClient(); | ||
| } | ||
|
|
||
| async releaseResources(): Promise<void> { | ||
| clearTimeout(this.delayTimeoutId); | ||
| clearTimeout(this.sleepTimeoutId); | ||
| await this.stop(); | ||
| } | ||
|
|
||
| protected async checkConnectionStatus(): Promise<ConnectionStatus> { | ||
| const connectContext = this.telemetryFactory.openTelemetryContext("Connection status check", TelemetryTraceLevel.FORCE_TOP_LEVEL); | ||
| connectContext.setAttribute("url", this.hostInfo.host); | ||
| return await connectContext.start(async () => { | ||
| const startNanos = getCurrentTimeNano(); | ||
| try { | ||
| if (this.monitoringClient != null && (await this.pluginService.isClientValid(this.monitoringClient))) { | ||
| return [true, getCurrentTimeNano() - startNanos]; | ||
| } | ||
|
|
||
| await this.closeMonitoringClient(); | ||
|
|
||
| const monitoringConnProperties = new Map(this.properties); | ||
| for (const key of monitoringConnProperties.keys()) { | ||
| if (!key.startsWith(WrapperProperties.MONITORING_PROPERTY_PREFIX)) { | ||
| continue; | ||
| } | ||
| monitoringConnProperties.set(key.substring(WrapperProperties.MONITORING_PROPERTY_PREFIX.length), this.properties.get(key)); | ||
| monitoringConnProperties.delete(key); | ||
| } | ||
|
|
||
| this.monitoringClient = await this.pluginService.forceConnect(this.hostInfo, monitoringConnProperties); | ||
| return [true, getCurrentTimeNano() - startNanos]; | ||
| } catch (error: any) { | ||
| this.hostInvalidCounter.inc(); | ||
| await this.closeMonitoringClient(); | ||
| return [false, getCurrentTimeNano() - startNanos]; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private async closeMonitoringClient(): Promise<void> { | ||
| if (this.monitoringClient) { | ||
| try { | ||
| await this.pluginService.abortTargetClient(this.monitoringClient); | ||
| } catch { | ||
| // ignore | ||
| } | ||
| this.monitoringClient = null; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we put nano times as bigint in the other parts of the wrapper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The util method
getCurrentTimeNanoreturns the current nanos as a Number.I will create a separate PR going over all the nano variables in the codebase and make it consistent.