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: add extended math utils and duration support #923

Merged
merged 1 commit into from
Dec 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"scripts": {
"test": "cross-env MOCHA_SUITE_NAME=\"Unit Tests\" c8 --report-dir='coverage/unit' mocha 'test/unit/**/*.ts' --reporter-options configFile=mocha-multi-reporter.json,cmrOutput=mocha-junit-reporter+mochaFile+junit.xml",
"test-mathex": "cross-env MOCHA_SUITE_NAME=\"MathEx Unit Tests\" c8 --report-dir='coverage/unit-mathex' mocha 'test/unit/**/math_ex*.ts' --reporter-options configFile=mocha-multi-reporter.json,cmrOutput=mocha-junit-reporter+mochaFile+junit.xml",
"test-e2e-all": "cross-env MOCHA_SUITE_NAME=\"Mocha E2E All Tests\" c8 --report-dir='coverage/e2e-all' mocha 'test/e2e/**/*.ts' --reporter-options configFile=mocha-multi-reporter.json,cmrOutput=mocha-junit-reporter+mochaFile+junit-e2e-all.xml",
"test-e2e-integration": "cross-env MOCHA_SUITE_NAME=\"Mocha E2E Integration Tests\" c8 --report-dir='coverage/e2e-integration' mocha 'test/e2e/integration/**/*.ts' --reporter-options configFile=mocha-multi-reporter.json,cmrOutput=mocha-junit-reporter+mochaFile+junit-e2e-integration.xml",
"test-e2e-leases": "cross-env MOCHA_SUITE_NAME=\"Mocha E2E Lease Tests\" c8 --report-dir='coverage/e2e-leases' mocha 'test/e2e/integration/core/lease*.test.ts' --reporter-options configFile=mocha-multi-reporter.json,cmrOutput=mocha-junit-reporter+mochaFile+junit-e2e-integration.xml",
Expand Down
10 changes: 5 additions & 5 deletions src/commands/node/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
HEDERA_NODE_DEFAULT_STAKE_AMOUNT,
IGNORED_NODE_ACCOUNT_ID,
LOCAL_HOST,
SECONDS,
TREASURY_ACCOUNT_ID,
} from '../../core/constants.js';
import {
Expand Down Expand Up @@ -82,6 +81,7 @@ import type {
} from './configs.js';
import {type Lease} from '../../core/lease/types.js';
import {ListrLease} from '../../core/lease/listr_lease.js';
import {Duration} from '../../core/time/duration.js';
import {type BaseCommand} from '../base.js';

export class NodeCommandTasks {
Expand Down Expand Up @@ -403,7 +403,7 @@ export class NodeCommandTasks {

attempt++;
clearTimeout(timeoutId);
await sleep(delay);
await sleep(Duration.ofMillis(delay));
}

await this.k8.stopPortForward(srv);
Expand All @@ -415,7 +415,7 @@ export class NodeCommandTasks {
);
}

await sleep(1.5 * SECONDS); // delaying prevents - gRPC service error
await sleep(Duration.ofSeconds(2)); // delaying prevents - gRPC service error

return podName;
}
Expand Down Expand Up @@ -990,7 +990,7 @@ export class NodeCommandTasks {
self.logger.info(
'sleep 60 seconds for the handler to be able to trigger the network node stake weight recalculate',
);
await sleep(60 * SECONDS);
await sleep(Duration.ofSeconds(60));
const accountMap = getNodeAccountMap(config.allNodeAliases);

switch (transactionType) {
Expand Down Expand Up @@ -1490,7 +1490,7 @@ export class NodeCommandTasks {

sleep(title: string, milliseconds: number) {
return new Task(title, async (ctx: any, task: ListrTaskWrapper<any, any, any>) => {
await sleep(milliseconds);
await sleep(Duration.ofMillis(milliseconds));
});
}

Expand Down
3 changes: 0 additions & 3 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ export const UPGRADE_FILE_CHUNK_SIZE = 1024 * 5; // 5Kb

export const JVM_DEBUG_PORT = 5005;

export const SECONDS = 1000;
export const MINUTES = 60 * SECONDS;

export const PODS_RUNNING_MAX_ATTEMPTS = +process.env.PODS_RUNNING_MAX_ATTEMPTS || 60 * 15;
export const PODS_RUNNING_DELAY = +process.env.PODS_RUNNING_DELAY || 1000;
export const NETWORK_NODE_ACTIVE_MAX_ATTEMPTS = +process.env.NETWORK_NODE_ACTIVE_MAX_ATTEMPTS || 120;
Expand Down
5 changes: 3 additions & 2 deletions src/core/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import {PrivateKey, ServiceEndpoint} from '@hashgraph/sdk';
import {type NodeAlias, type NodeAliases} from '../types/aliases.js';
import {type CommandFlag} from '../types/flag_types.js';
import {type SoloLogger} from './logging.js';
import {type Duration} from './time/duration.js';

export function sleep(ms: number) {
export function sleep(duration: Duration) {
return new Promise<void>(resolve => {
setTimeout(resolve, ms);
setTimeout(resolve, duration.toMillis());
});
}

Expand Down
23 changes: 12 additions & 11 deletions src/core/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import {type SoloLogger} from './logging.js';
import {type PodName, type TarCreateFilter} from '../types/aliases.js';
import type {ExtendedNetServer, LocalContextObject} from '../types/index.js';
import {HEDERA_HAPI_PATH, MINUTES, ROOT_CONTAINER, SOLO_LOGS_DIR} from './constants.js';
import {HEDERA_HAPI_PATH, ROOT_CONTAINER, SOLO_LOGS_DIR} from './constants.js';
import {Duration} from './time/duration.js';

interface TDirectoryData {
directory: boolean;
Expand Down Expand Up @@ -213,7 +214,7 @@
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

return this.filterItem(resp.body.items, {name});
Expand All @@ -237,7 +238,7 @@
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

return result.body.items;
Expand Down Expand Up @@ -296,7 +297,7 @@
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

return this.filterItem(resp.body.items, {name});
Expand Down Expand Up @@ -999,7 +1000,7 @@
} catch (e: Error | any) {
return;
}
await sleep(timeout);
await sleep(Duration.ofMillis(timeout));

Check warning on line 1003 in src/core/k8.ts

View check run for this annotation

Codecov / codecov/patch

src/core/k8.ts#L1003

Added line #L1003 was not covered by tests
}
if (attempts >= maxAttempts) {
throw new SoloError(`failed to stop port-forwarder [${server.info}]`);
Expand Down Expand Up @@ -1036,7 +1037,7 @@
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

this.logger.debug(
Expand Down Expand Up @@ -1149,7 +1150,7 @@
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

for (const item of resp.body.items) {
Expand Down Expand Up @@ -1179,7 +1180,7 @@
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

for (const item of resp.body.items) {
Expand Down Expand Up @@ -1234,7 +1235,7 @@
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

if (result.response.statusCode === 200 && result.body.items && result.body.items.length > 0) {
Expand Down Expand Up @@ -1520,7 +1521,7 @@
if (!pod?.metadata?.deletionTimestamp) {
podExists = false;
} else {
await sleep(1000);
await sleep(Duration.ofSeconds(1));
}
}
} catch (e) {
Expand Down Expand Up @@ -1562,7 +1563,7 @@
const scriptName = 'support-zip.sh';
const sourcePath = path.join(constants.RESOURCES_DIR, scriptName); // script source path
await this.copyTo(podName, ROOT_CONTAINER, sourcePath, `${HEDERA_HAPI_PATH}`);
await sleep(1000); // wait for the script to sync to the file system
await sleep(Duration.ofSeconds(1)); // wait for the script to sync to the file system
await this.execContainer(podName, ROOT_CONTAINER, [
'bash',
'-c',
Expand Down
11 changes: 6 additions & 5 deletions src/core/lease/lease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import {MissingArgumentError, SoloError} from '../errors.js';
import {type V1Lease} from '@kubernetes/client-node';
import {type K8} from '../k8.js';
import {SECONDS} from '../constants.js';
import {LeaseHolder} from './lease_holder.js';
import {LeaseAcquisitionError, LeaseRelinquishmentError} from './lease_errors.js';
import {sleep} from '../helpers.js';
import {type Lease, type LeaseRenewalService} from './types.js';
import {Duration} from '../time/duration.js';
import type {Lease, LeaseRenewalService} from './types.js';

/**
* Concrete implementation of a Kubernetes based time-based mutually exclusive lock via the Coordination API.
Expand Down Expand Up @@ -382,10 +382,11 @@ export class IntervalLease implements Lease {
* @returns true if the lease has expired; otherwise, false.
*/
private static checkExpiration(lease: V1Lease): boolean {
const now = Date.now();
const now = Duration.ofMillis(Date.now());
const durationSec = lease.spec.leaseDurationSeconds || IntervalLease.DEFAULT_LEASE_DURATION;
const lastRenewal = lease.spec?.renewTime || lease.spec?.acquireTime;
const deltaSec = (now - new Date(lastRenewal).valueOf()) / SECONDS;
const lastRenewalTime = lease.spec?.renewTime || lease.spec?.acquireTime;
const lastRenewal = Duration.ofMillis(new Date(lastRenewalTime).valueOf());
const deltaSec = now.minus(lastRenewal).seconds;
return deltaSec > durationSec;
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/lease/lease_renewal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*
*/
import {type Lease} from './types.js';
import {SECONDS} from '../constants.js';
import {type LeaseRenewalService} from './types.js';
import {Duration} from '../time/duration.js';

/**
* Implements a lease renewal service which utilizes a setInterval() based approach to renew leases at regular intervals.
Expand Down Expand Up @@ -53,7 +53,7 @@ export class IntervalLeaseRenewalService implements LeaseRenewalService {
*/
public async schedule(lease: Lease): Promise<number> {
const renewalDelay = this.calculateRenewalDelay(lease);
const timeout = setInterval(() => lease.tryRenew(), renewalDelay);
const timeout = setInterval(() => lease.tryRenew(), renewalDelay.toMillis());
const scheduleId = Number(timeout);

this._scheduledLeases.set(scheduleId, lease);
Expand Down Expand Up @@ -102,7 +102,7 @@ export class IntervalLeaseRenewalService implements LeaseRenewalService {
* @param lease - the lease to be renewed.
* @returns the delay in milliseconds.
*/
public calculateRenewalDelay(lease: Lease): number {
return Math.round(lease.durationSeconds * 0.5) * SECONDS;
public calculateRenewalDelay(lease: Lease): Duration {
return Duration.ofSeconds(lease.durationSeconds * 0.5);
}
}
3 changes: 2 additions & 1 deletion src/core/lease/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import type {K8} from '../k8.js';
import {type LeaseHolder} from './lease_holder.js';
import {type Duration} from '../time/duration.js';

/**
* Copyright (C) 2024 Hedera Hashgraph, LLC
Expand Down Expand Up @@ -66,7 +67,7 @@ export interface LeaseRenewalService {
* @param lease - the lease to be renewed.
* @returns the delay in milliseconds.
*/
calculateRenewalDelay(lease: Lease): number;
calculateRenewalDelay(lease: Lease): Duration;
}
export interface Lease {
readonly client: K8;
Expand Down
Loading
Loading