Skip to content

Commit

Permalink
feat: add extended math utils and duration support
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Klick <[email protected]>
  • Loading branch information
nathanklick committed Dec 6, 2024
1 parent d5aaa91 commit c3d940d
Show file tree
Hide file tree
Showing 45 changed files with 1,194 additions and 188 deletions.
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 @@ -34,7 +34,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 @@ -87,6 +86,7 @@ import type {
} from './configs.js';
import {type Lease} from '../../core/lease/lease.js';
import {ListrLease} from '../../core/lease/listr_lease.js';
import {Duration} from '../../core/time/duration.js';

export class NodeCommandTasks {
private readonly accountManager: AccountManager;
Expand Down Expand Up @@ -407,7 +407,7 @@ export class NodeCommandTasks {

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

await this.k8.stopPortForward(srv);
Expand All @@ -419,7 +419,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 @@ -994,7 +994,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 @@ -1494,7 +1494,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 @@ -174,9 +174,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
8 changes: 4 additions & 4 deletions src/core/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import {type NodeDeleteConfigClass, type NodeUpdateConfigClass} from '../command
import {type CommandFlag, type CommandHandlers} from '../types/index.js';
import {type V1Pod} from '@kubernetes/client-node';
import {type SoloLogger} from './logging.js';
import {type NodeCommandHandlers} from '../commands/node/handlers.js';
import {type Lease} from './lease/lease.js';
import {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 Expand Up @@ -212,7 +212,7 @@ async function getNodeLog(pod: V1Pod, namespace: string, timeString: string, k8:
const scriptName = 'support-zip.sh';
const sourcePath = path.join(constants.RESOURCES_DIR, scriptName); // script source path
await k8.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 k8.execContainer(podName, ROOT_CONTAINER, [
'bash',
'-c',
Expand Down
20 changes: 10 additions & 10 deletions src/core/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type * as WebSocket from 'ws';
import {type PodName, type TarCreateFilter} from '../types/aliases.js';
import type {ExtendedNetServer, LocalContextObject} from '../types/index.js';
import type * as http from 'node:http';
import {MINUTES} from './constants.js';
import {Duration} from './time/duration.js';

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

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

return result.body.items;
Expand Down Expand Up @@ -295,7 +295,7 @@ export class K8 {
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

return this.filterItem(resp.body.items, {name});
Expand Down Expand Up @@ -998,7 +998,7 @@ export class K8 {
} catch (e: Error | any) {
return;
}
await sleep(timeout);
await sleep(Duration.ofMillis(timeout));
}
if (attempts >= maxAttempts) {
throw new SoloError(`failed to stop port-forwarder [${server.info}]`);
Expand Down Expand Up @@ -1035,7 +1035,7 @@ export class K8 {
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

this.logger.debug(
Expand Down Expand Up @@ -1148,7 +1148,7 @@ export class K8 {
undefined,
undefined,
undefined,
5 * MINUTES,
Duration.ofMinutes(5).toMillis(),
);

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

for (const item of resp.body.items) {
Expand Down Expand Up @@ -1233,7 +1233,7 @@ export class K8 {
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 @@ -1519,7 +1519,7 @@ export class K8 {
if (!pod?.metadata?.deletionTimestamp) {
podExists = false;
} else {
await sleep(1000);
await sleep(Duration.ofSeconds(1));
}
}
} catch (e) {
Expand Down
9 changes: 5 additions & 4 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 {type LeaseRenewalService} from './lease_renewal.js';
import {sleep} from '../helpers.js';
import {Duration} from '../time/duration.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 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 || Lease.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
10 changes: 5 additions & 5 deletions src/core/lease/lease_renewal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/
import {type Lease} from './lease.js';
import {SECONDS} from '../constants.js';
import {Duration} from '../time/duration.js';

/**
* A service for managing cancellable lease renewals.
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface LeaseRenewalService {
* @param lease - the lease to be renewed.
* @returns the delay in milliseconds.
*/
calculateRenewalDelay(lease: Lease): number;
calculateRenewalDelay(lease: Lease): Duration;
}

/**
Expand Down Expand Up @@ -91,7 +91,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 @@ -140,7 +140,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);
}
}
Loading

0 comments on commit c3d940d

Please sign in to comment.