Skip to content

Commit

Permalink
adjusting expiry down for replica drift
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 30, 2023
1 parent e21169b commit 70cf27d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 36 deletions.
34 changes: 2 additions & 32 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,28 +613,7 @@ describe('retry failures', () => {
});
});
jest.useFakeTimers({ legacyFakeTimers: true });
test('should change nothing if time is within 30 seconds of replica', async () => {
const systemTime = new Date('August 19, 1975 23:15:30');
// jest.setSystemTime(systemTime);
const mockFetch = jest.fn();

const agent = new HttpAgent({ host: HTTP_AGENT_HOST, fetch: mockFetch });

await agent.syncTime();

agent
.call(Principal.managementCanister(), {
methodName: 'test',
arg: new Uint8Array().buffer,
})
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
.catch(function (_) {});

const requestBody = cbor.decode(mockFetch.mock.calls[0][1].body);
expect((requestBody as unknown as any).content.ingress_expiry).toMatchInlineSnapshot(
`1240000000000`,
);
});
test('should adjust the Expiry if the clock is more than 30 seconds behind', async () => {
const mockFetch = jest.fn();

Expand Down Expand Up @@ -669,11 +648,8 @@ test('should adjust the Expiry if the clock is more than 30 seconds behind', asy
// Expiry should be: ingress expiry + replica time
const expiryInMs = requestBody.content.ingress_expiry / NANOSECONDS_PER_MILLISECONDS;

const delay = expiryInMs + REPLICA_PERMITTED_DRIFT_MILLISECONDS - Number(replicaTime);
expect(requestBody.content.ingress_expiry).toMatchInlineSnapshot(`1260000000000`);

expect(requestBody.content.ingress_expiry).toMatchInlineSnapshot(`1271000000000`);

expect(delay).toBe(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);
jest.resetModules();
});

Expand Down Expand Up @@ -709,14 +685,8 @@ test('should adjust the Expiry if the clock is more than 30 seconds ahead', asyn

const requestBody: any = cbor.decode(mockFetch.mock.calls[0][1].body);

// Expiry should be: replica time - ingress expiry
const expiryInMs = requestBody.content.ingress_expiry / NANOSECONDS_PER_MILLISECONDS;

const delay = Number(replicaTime) - (expiryInMs + REPLICA_PERMITTED_DRIFT_MILLISECONDS);

expect(requestBody.content.ingress_expiry).toMatchInlineSnapshot(`1209000000000`);
expect(requestBody.content.ingress_expiry).toMatchInlineSnapshot(`1200000000000`);

expect(delay).toBe(-1 * DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);
jest.resetModules();
});

Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/agent/http/transforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ test('it should round down to the nearest minute', () => {
jest.setSystemTime(new Date(1619459231314));

const expiry = new Expiry(5 * 60 * 1000);
expect(expiry['_value']).toEqual(BigInt(1619459520000000000n));
expect(expiry['_value']).toEqual(BigInt(1619459460000000000));

const expiry_as_date_string = new Date(
Number(expiry['_value'] / BigInt(1_000_000)),
).toISOString();
expect(expiry_as_date_string).toBe('2021-04-26T17:52:00.000Z');
expect(expiry_as_date_string).toBe('2021-04-26T17:51:00.000Z');
});
6 changes: 4 additions & 2 deletions packages/agent/src/agent/http/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import {

const NANOSECONDS_PER_MILLISECONDS = BigInt(1_000_000);

const REPLICA_PERMITTED_DRIFT_MILLISECONDS = BigInt(60 * 1000);
const REPLICA_PERMITTED_DRIFT_MILLISECONDS = 60 * 1000;

export class Expiry {
private readonly _value: bigint;

constructor(deltaInMSec: number) {
// Use bigint because it can overflow the maximum number allowed in a double float.
const raw_value = (BigInt(Date.now()) + BigInt(deltaInMSec)) * NANOSECONDS_PER_MILLISECONDS;
const raw_value =
BigInt(Math.floor(Date.now() + deltaInMSec - REPLICA_PERMITTED_DRIFT_MILLISECONDS)) *
NANOSECONDS_PER_MILLISECONDS;

// round down to the nearest second
const ingress_as_seconds = raw_value / BigInt(1_000_000_000);
Expand Down

0 comments on commit 70cf27d

Please sign in to comment.