@fluid-experimental/attributor 2.0.0-dev-rc.1.0.0.224419
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +20 -0
- package/.mocharc.js +12 -0
- package/CHANGELOG.md +122 -0
- package/LICENSE +21 -0
- package/README.md +144 -0
- package/api-extractor-lint.json +4 -0
- package/api-extractor.json +4 -0
- package/api-report/attributor.api.md +71 -0
- package/dist/attributor-alpha.d.ts +27 -0
- package/dist/attributor-beta.d.ts +31 -0
- package/dist/attributor-public.d.ts +31 -0
- package/dist/attributor-untrimmed.d.ts +124 -0
- package/dist/attributor.cjs +69 -0
- package/dist/attributor.cjs.map +1 -0
- package/dist/attributor.d.ts +56 -0
- package/dist/attributor.d.ts.map +1 -0
- package/dist/encoders.cjs +80 -0
- package/dist/encoders.cjs.map +1 -0
- package/dist/encoders.d.ts +28 -0
- package/dist/encoders.d.ts.map +1 -0
- package/dist/index.cjs +16 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lz4Encoder.cjs +29 -0
- package/dist/lz4Encoder.cjs.map +1 -0
- package/dist/lz4Encoder.d.ts +7 -0
- package/dist/lz4Encoder.d.ts.map +1 -0
- package/dist/mixinAttributor.cjs +170 -0
- package/dist/mixinAttributor.cjs.map +1 -0
- package/dist/mixinAttributor.d.ts +57 -0
- package/dist/mixinAttributor.d.ts.map +1 -0
- package/dist/stringInterner.cjs +65 -0
- package/dist/stringInterner.cjs.map +1 -0
- package/dist/stringInterner.d.ts +55 -0
- package/dist/stringInterner.d.ts.map +1 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/lib/attributor-alpha.d.mts +27 -0
- package/lib/attributor-beta.d.mts +31 -0
- package/lib/attributor-public.d.mts +31 -0
- package/lib/attributor-untrimmed.d.mts +124 -0
- package/lib/attributor.d.mts +56 -0
- package/lib/attributor.d.mts.map +1 -0
- package/lib/attributor.mjs +60 -0
- package/lib/attributor.mjs.map +1 -0
- package/lib/encoders.d.mts +28 -0
- package/lib/encoders.d.mts.map +1 -0
- package/lib/encoders.mjs +71 -0
- package/lib/encoders.mjs.map +1 -0
- package/lib/index.d.mts +3 -0
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +3 -0
- package/lib/index.mjs.map +1 -0
- package/lib/lz4Encoder.d.mts +7 -0
- package/lib/lz4Encoder.d.mts.map +1 -0
- package/lib/lz4Encoder.mjs +21 -0
- package/lib/lz4Encoder.mjs.map +1 -0
- package/lib/mixinAttributor.d.mts +57 -0
- package/lib/mixinAttributor.d.mts.map +1 -0
- package/lib/mixinAttributor.mjs +165 -0
- package/lib/mixinAttributor.mjs.map +1 -0
- package/lib/stringInterner.d.mts +55 -0
- package/lib/stringInterner.d.mts.map +1 -0
- package/lib/stringInterner.mjs +61 -0
- package/lib/stringInterner.mjs.map +1 -0
- package/package.json +138 -0
- package/prettier.config.cjs +8 -0
- package/src/attributor.ts +107 -0
- package/src/encoders.ts +111 -0
- package/src/index.ts +12 -0
- package/src/lz4Encoder.ts +27 -0
- package/src/mixinAttributor.ts +317 -0
- package/src/stringInterner.ts +86 -0
- package/tsc-multi.test.json +4 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { assert } from "@fluidframework/core-utils";
|
|
6
|
+
import { IDocumentMessage, ISequencedDocumentMessage } from "@fluidframework/protocol-definitions";
|
|
7
|
+
import { AttributionInfo } from "@fluidframework/runtime-definitions";
|
|
8
|
+
import { UsageError } from "@fluidframework/telemetry-utils";
|
|
9
|
+
import { IAudience, IDeltaManager } from "@fluidframework/container-definitions";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Provides lookup between attribution keys and their associated attribution information.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export interface IAttributor {
|
|
16
|
+
/**
|
|
17
|
+
* Retrieves attribution information associated with a particular key.
|
|
18
|
+
* @param key - Attribution key to look up.
|
|
19
|
+
* @throws If no attribution information is recorded for that key.
|
|
20
|
+
*/
|
|
21
|
+
getAttributionInfo(key: number): AttributionInfo;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param key - Attribution key to look up.
|
|
25
|
+
* @returns the attribution information associated with the provided key, or undefined if no information exists.
|
|
26
|
+
*/
|
|
27
|
+
tryGetAttributionInfo(key: number): AttributionInfo | undefined;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @returns an iterable of (attribution key, attribution info) pairs for each stored key.
|
|
31
|
+
*/
|
|
32
|
+
entries(): IterableIterator<[number, AttributionInfo]>;
|
|
33
|
+
|
|
34
|
+
// TODO:
|
|
35
|
+
// - GC
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* {@inheritdoc IAttributor}
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export class Attributor implements IAttributor {
|
|
43
|
+
protected readonly keyToInfo: Map<number, AttributionInfo>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param initialEntries - Any entries which should be populated on instantiation.
|
|
47
|
+
*/
|
|
48
|
+
constructor(initialEntries?: Iterable<[number, AttributionInfo]>) {
|
|
49
|
+
this.keyToInfo = new Map(initialEntries ?? []);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* {@inheritdoc IAttributor.getAttributionInfo}
|
|
54
|
+
*/
|
|
55
|
+
public getAttributionInfo(key: number): AttributionInfo {
|
|
56
|
+
const result = this.tryGetAttributionInfo(key);
|
|
57
|
+
if (!result) {
|
|
58
|
+
throw new UsageError(`Requested attribution information for unstored key: ${key}.`);
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* {@inheritdoc IAttributor.tryGetAttributionInfo}
|
|
65
|
+
*/
|
|
66
|
+
public tryGetAttributionInfo(key: number): AttributionInfo | undefined {
|
|
67
|
+
return this.keyToInfo.get(key);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* {@inheritdoc IAttributor.entries}
|
|
72
|
+
*/
|
|
73
|
+
public entries(): IterableIterator<[number, AttributionInfo]> {
|
|
74
|
+
return this.keyToInfo.entries();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Attributor which listens to an op stream and records entries for each op.
|
|
80
|
+
* Sequence numbers are used as attribution keys.
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
export class OpStreamAttributor extends Attributor implements IAttributor {
|
|
84
|
+
constructor(
|
|
85
|
+
deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
|
|
86
|
+
audience: IAudience,
|
|
87
|
+
initialEntries?: Iterable<[number, AttributionInfo]>,
|
|
88
|
+
) {
|
|
89
|
+
super(initialEntries);
|
|
90
|
+
deltaManager.on("op", (message: ISequencedDocumentMessage) => {
|
|
91
|
+
// TODO: Verify whether this should be able to handle server-generated ops (with null clientId)
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
93
|
+
const client = audience.getMember(message.clientId as string);
|
|
94
|
+
if (message.type === "op") {
|
|
95
|
+
// TODO: This case may be legitimate, and if so we need to figure out how to handle it.
|
|
96
|
+
assert(
|
|
97
|
+
client !== undefined,
|
|
98
|
+
0x4af /* Received message from user not in the audience */,
|
|
99
|
+
);
|
|
100
|
+
this.keyToInfo.set(message.sequenceNumber, {
|
|
101
|
+
user: client.user,
|
|
102
|
+
timestamp: message.timestamp,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/encoders.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { assert } from "@fluidframework/core-utils";
|
|
6
|
+
import { IUser } from "@fluidframework/protocol-definitions";
|
|
7
|
+
import { AttributionInfo } from "@fluidframework/runtime-definitions";
|
|
8
|
+
import { IAttributor } from "./attributor";
|
|
9
|
+
import { InternedStringId, MutableStringInterner } from "./stringInterner";
|
|
10
|
+
|
|
11
|
+
export interface Encoder<TDecoded, TEncoded> {
|
|
12
|
+
encode(decoded: TDecoded): TEncoded;
|
|
13
|
+
|
|
14
|
+
decode(encoded: TEncoded): TDecoded;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Note: the encoded format doesn't matter as long as it's serializable;
|
|
18
|
+
// these types could be weakened.
|
|
19
|
+
export type TimestampEncoder = Encoder<number[], number[]>;
|
|
20
|
+
|
|
21
|
+
export const deltaEncoder: TimestampEncoder = {
|
|
22
|
+
encode: (timestamps: number[]) => {
|
|
23
|
+
const deltaTimestamps: number[] = new Array(timestamps.length);
|
|
24
|
+
let prev = 0;
|
|
25
|
+
for (let i = 0; i < timestamps.length; i++) {
|
|
26
|
+
deltaTimestamps[i] = timestamps[i] - prev;
|
|
27
|
+
prev = timestamps[i];
|
|
28
|
+
}
|
|
29
|
+
return deltaTimestamps;
|
|
30
|
+
},
|
|
31
|
+
decode: (encoded: unknown) => {
|
|
32
|
+
assert(
|
|
33
|
+
Array.isArray(encoded),
|
|
34
|
+
0x4b0 /* Encoded timestamps should be an array of numbers */,
|
|
35
|
+
);
|
|
36
|
+
const timestamps: number[] = new Array(encoded.length);
|
|
37
|
+
let cumulativeSum = 0;
|
|
38
|
+
for (let i = 0; i < encoded.length; i++) {
|
|
39
|
+
cumulativeSum += encoded[i];
|
|
40
|
+
timestamps[i] = cumulativeSum;
|
|
41
|
+
}
|
|
42
|
+
return timestamps;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type IAttributorSerializer = Encoder<IAttributor, SerializedAttributor>;
|
|
47
|
+
|
|
48
|
+
export interface SerializedAttributor {
|
|
49
|
+
interner: readonly string[] /* result of calling getSerializable() on a StringInterner */;
|
|
50
|
+
seqs: number[];
|
|
51
|
+
timestamps: number[];
|
|
52
|
+
attributionRefs: InternedStringId[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export class AttributorSerializer implements IAttributorSerializer {
|
|
56
|
+
constructor(
|
|
57
|
+
private readonly makeAttributor: (
|
|
58
|
+
entries: Iterable<[number, AttributionInfo]>,
|
|
59
|
+
) => IAttributor,
|
|
60
|
+
private readonly timestampEncoder: TimestampEncoder,
|
|
61
|
+
) {}
|
|
62
|
+
|
|
63
|
+
public encode(attributor: IAttributor): SerializedAttributor {
|
|
64
|
+
const interner = new MutableStringInterner();
|
|
65
|
+
const seqs: number[] = [];
|
|
66
|
+
const timestamps: number[] = [];
|
|
67
|
+
const attributionRefs: InternedStringId[] = [];
|
|
68
|
+
for (const [seq, { user, timestamp }] of attributor.entries()) {
|
|
69
|
+
seqs.push(seq);
|
|
70
|
+
timestamps.push(timestamp);
|
|
71
|
+
const ref = interner.getOrCreateInternedId(JSON.stringify(user));
|
|
72
|
+
attributionRefs.push(ref);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const serialized: SerializedAttributor = {
|
|
76
|
+
interner: interner.getSerializable(),
|
|
77
|
+
seqs,
|
|
78
|
+
timestamps: this.timestampEncoder.encode(timestamps),
|
|
79
|
+
attributionRefs,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return serialized;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public decode(encoded: SerializedAttributor): IAttributor {
|
|
86
|
+
const interner = new MutableStringInterner(encoded.interner);
|
|
87
|
+
const { seqs, timestamps: encodedTimestamps, attributionRefs } = encoded;
|
|
88
|
+
const timestamps = this.timestampEncoder.decode(encodedTimestamps);
|
|
89
|
+
assert(
|
|
90
|
+
seqs.length === timestamps.length && timestamps.length === attributionRefs.length,
|
|
91
|
+
0x4b1 /* serialized attribution columns should have the same length */,
|
|
92
|
+
);
|
|
93
|
+
const entries = new Array(seqs.length);
|
|
94
|
+
for (let i = 0; i < seqs.length; i++) {
|
|
95
|
+
const key = seqs[i];
|
|
96
|
+
const timestamp = timestamps[i];
|
|
97
|
+
const ref = attributionRefs[i];
|
|
98
|
+
const user: IUser = JSON.parse(interner.getString(ref));
|
|
99
|
+
entries[i] = [key, { user, timestamp }];
|
|
100
|
+
}
|
|
101
|
+
return this.makeAttributor(entries);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @returns an encoder which composes `a` and `b`.
|
|
107
|
+
*/
|
|
108
|
+
export const chain = <T1, T2, T3>(a: Encoder<T1, T2>, b: Encoder<T2, T3>): Encoder<T1, T3> => ({
|
|
109
|
+
encode: (content) => b.encode(a.encode(content)),
|
|
110
|
+
decode: (content) => a.decode(b.decode(content)),
|
|
111
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
export { Attributor, OpStreamAttributor, IAttributor } from "./attributor";
|
|
6
|
+
export {
|
|
7
|
+
createRuntimeAttributor,
|
|
8
|
+
enableOnNewFileKey,
|
|
9
|
+
IProvideRuntimeAttributor,
|
|
10
|
+
IRuntimeAttributor,
|
|
11
|
+
mixinAttributor,
|
|
12
|
+
} from "./mixinAttributor";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { compress, decompress } from "lz4js";
|
|
6
|
+
import { bufferToString, stringToBuffer } from "@fluid-internal/client-utils";
|
|
7
|
+
import { Jsonable } from "@fluidframework/datastore-definitions";
|
|
8
|
+
import { Encoder } from "./encoders";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @alpha
|
|
12
|
+
*/
|
|
13
|
+
export function makeLZ4Encoder<T>(): Encoder<Jsonable<T>, string> {
|
|
14
|
+
return {
|
|
15
|
+
encode: (decoded: Jsonable<T>) => {
|
|
16
|
+
const uncompressed = new TextEncoder().encode(JSON.stringify(decoded));
|
|
17
|
+
const compressed = compress(uncompressed);
|
|
18
|
+
return bufferToString(compressed, "base64");
|
|
19
|
+
},
|
|
20
|
+
decode: (serializedSummary: string): Jsonable<T> => {
|
|
21
|
+
const compressed = new Uint8Array(stringToBuffer(serializedSummary, "base64"));
|
|
22
|
+
const uncompressed = decompress(compressed);
|
|
23
|
+
const decoded: Jsonable<T> = JSON.parse(new TextDecoder().decode(uncompressed));
|
|
24
|
+
return decoded;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
IDocumentMessage,
|
|
7
|
+
ISequencedDocumentMessage,
|
|
8
|
+
ISnapshotTree,
|
|
9
|
+
} from "@fluidframework/protocol-definitions";
|
|
10
|
+
import { IAudience, IContainerContext, IDeltaManager } from "@fluidframework/container-definitions";
|
|
11
|
+
import { ContainerRuntime } from "@fluidframework/container-runtime";
|
|
12
|
+
import type { IContainerRuntimeOptions } from "@fluidframework/container-runtime";
|
|
13
|
+
import {
|
|
14
|
+
AttributionInfo,
|
|
15
|
+
AttributionKey,
|
|
16
|
+
ISummaryTreeWithStats,
|
|
17
|
+
ITelemetryContext,
|
|
18
|
+
NamedFluidDataStoreRegistryEntries,
|
|
19
|
+
} from "@fluidframework/runtime-definitions";
|
|
20
|
+
import { addSummarizeResultToSummary, SummaryTreeBuilder } from "@fluidframework/runtime-utils";
|
|
21
|
+
import { IContainerRuntime } from "@fluidframework/container-runtime-definitions";
|
|
22
|
+
import { IRequest, IResponse, FluidObject } from "@fluidframework/core-interfaces";
|
|
23
|
+
import { bufferToString } from "@fluid-internal/client-utils";
|
|
24
|
+
import { assert, unreachableCase } from "@fluidframework/core-utils";
|
|
25
|
+
import {
|
|
26
|
+
createChildLogger,
|
|
27
|
+
loggerToMonitoringContext,
|
|
28
|
+
PerformanceEvent,
|
|
29
|
+
UsageError,
|
|
30
|
+
} from "@fluidframework/telemetry-utils";
|
|
31
|
+
import { Attributor, IAttributor, OpStreamAttributor } from "./attributor";
|
|
32
|
+
import { AttributorSerializer, chain, deltaEncoder, Encoder } from "./encoders";
|
|
33
|
+
import { makeLZ4Encoder } from "./lz4Encoder";
|
|
34
|
+
|
|
35
|
+
// Summary tree keys
|
|
36
|
+
const attributorTreeName = ".attributor";
|
|
37
|
+
const opBlobName = "op";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export const enableOnNewFileKey = "Fluid.Attribution.EnableOnNewFile";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
export const IRuntimeAttributor: keyof IProvideRuntimeAttributor = "IRuntimeAttributor";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
export interface IProvideRuntimeAttributor {
|
|
53
|
+
readonly IRuntimeAttributor: IRuntimeAttributor;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Provides access to attribution information stored on the container runtime.
|
|
58
|
+
*
|
|
59
|
+
* Attributors are only populated after the container runtime they are injected into has initialized.
|
|
60
|
+
* @sealed
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
export interface IRuntimeAttributor extends IProvideRuntimeAttributor {
|
|
64
|
+
/**
|
|
65
|
+
* @throws - If no AttributionInfo exists for this key.
|
|
66
|
+
*/
|
|
67
|
+
get(key: AttributionKey): AttributionInfo;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @returns Whether any AttributionInfo exists for the provided key.
|
|
71
|
+
*/
|
|
72
|
+
has(key: AttributionKey): boolean;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @returns Whether the runtime is currently tracking attribution information for the loaded container.
|
|
76
|
+
* See {@link mixinAttributor} for more details on when this happens.
|
|
77
|
+
*/
|
|
78
|
+
readonly isEnabled: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @returns an IRuntimeAttributor for usage with `mixinAttributor`. The attributor will only be populated with data
|
|
83
|
+
* once it's passed via scope to a container runtime load flow. See {@link mixinAttributor}.
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
export function createRuntimeAttributor(): IRuntimeAttributor {
|
|
87
|
+
return new RuntimeAttributor();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Mixes in logic to load and store runtime-based attribution functionality.
|
|
92
|
+
*
|
|
93
|
+
* The `scope` passed to `load` should implement `IProvideRuntimeAttributor`.
|
|
94
|
+
*
|
|
95
|
+
* Existing documents without stored attributors will not start storing attribution information: if an
|
|
96
|
+
* IRuntimeAttributor is passed via scope to load a document that never previously had attribution information,
|
|
97
|
+
* that attributor's `has` method will always return `false`.
|
|
98
|
+
* @param Base - base class, inherits from FluidAttributorRuntime
|
|
99
|
+
* @internal
|
|
100
|
+
*/
|
|
101
|
+
export const mixinAttributor = (Base: typeof ContainerRuntime = ContainerRuntime) =>
|
|
102
|
+
class ContainerRuntimeWithAttributor extends Base {
|
|
103
|
+
public static async loadRuntime(params: {
|
|
104
|
+
context: IContainerContext;
|
|
105
|
+
registryEntries: NamedFluidDataStoreRegistryEntries;
|
|
106
|
+
existing: boolean;
|
|
107
|
+
runtimeOptions?: IContainerRuntimeOptions;
|
|
108
|
+
containerScope?: FluidObject;
|
|
109
|
+
containerRuntimeCtor?: typeof ContainerRuntime;
|
|
110
|
+
/** @deprecated Will be removed once Loader LTS version is "2.0.0-internal.7.0.0". Migrate all usage of IFluidRouter to the "entryPoint" pattern. Refer to Removing-IFluidRouter.md */
|
|
111
|
+
requestHandler?: (request: IRequest, runtime: IContainerRuntime) => Promise<IResponse>;
|
|
112
|
+
provideEntryPoint: (containerRuntime: IContainerRuntime) => Promise<FluidObject>;
|
|
113
|
+
}): Promise<ContainerRuntime> {
|
|
114
|
+
const {
|
|
115
|
+
context,
|
|
116
|
+
registryEntries,
|
|
117
|
+
existing,
|
|
118
|
+
requestHandler,
|
|
119
|
+
provideEntryPoint,
|
|
120
|
+
runtimeOptions,
|
|
121
|
+
containerScope,
|
|
122
|
+
containerRuntimeCtor = ContainerRuntimeWithAttributor as unknown as typeof ContainerRuntime,
|
|
123
|
+
} = params;
|
|
124
|
+
|
|
125
|
+
const runtimeAttributor = (
|
|
126
|
+
containerScope as FluidObject<IProvideRuntimeAttributor> | undefined
|
|
127
|
+
)?.IRuntimeAttributor;
|
|
128
|
+
if (!runtimeAttributor) {
|
|
129
|
+
throw new UsageError(
|
|
130
|
+
"ContainerRuntimeWithAttributor must be passed a scope implementing IProvideRuntimeAttributor",
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const pendingRuntimeState = context.pendingLocalState as {
|
|
135
|
+
baseSnapshot?: ISnapshotTree;
|
|
136
|
+
};
|
|
137
|
+
const baseSnapshot: ISnapshotTree | undefined =
|
|
138
|
+
pendingRuntimeState?.baseSnapshot ?? context.baseSnapshot;
|
|
139
|
+
|
|
140
|
+
const { audience, deltaManager } = context;
|
|
141
|
+
assert(
|
|
142
|
+
audience !== undefined,
|
|
143
|
+
0x508 /* Audience must exist when instantiating attribution-providing runtime */,
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const mc = loggerToMonitoringContext(context.taggedLogger);
|
|
147
|
+
|
|
148
|
+
const shouldTrackAttribution = mc.config.getBoolean(enableOnNewFileKey) ?? false;
|
|
149
|
+
if (shouldTrackAttribution) {
|
|
150
|
+
(context.options.attribution ??= {}).track = true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const runtime = (await Base.loadRuntime({
|
|
154
|
+
context,
|
|
155
|
+
registryEntries,
|
|
156
|
+
requestHandler,
|
|
157
|
+
provideEntryPoint,
|
|
158
|
+
// ! This prop is needed for back-compat. Can be removed in 2.0.0-internal.8.0.0
|
|
159
|
+
initializeEntryPoint: provideEntryPoint,
|
|
160
|
+
runtimeOptions,
|
|
161
|
+
containerScope,
|
|
162
|
+
existing,
|
|
163
|
+
containerRuntimeCtor,
|
|
164
|
+
} as any)) as ContainerRuntimeWithAttributor;
|
|
165
|
+
runtime.runtimeAttributor = runtimeAttributor as RuntimeAttributor;
|
|
166
|
+
|
|
167
|
+
const logger = createChildLogger({ logger: runtime.logger, namespace: "Attributor" });
|
|
168
|
+
|
|
169
|
+
// Note: this fetches attribution blobs relatively eagerly in the load flow; we may want to optimize
|
|
170
|
+
// this to avoid blocking on such information until application actually requests some op-based attribution
|
|
171
|
+
// info or we need to summarize. All that really needs to happen immediately is to start recording
|
|
172
|
+
// op seq# -> attributionInfo for new ops.
|
|
173
|
+
await PerformanceEvent.timedExecAsync(
|
|
174
|
+
logger,
|
|
175
|
+
{
|
|
176
|
+
eventName: "initialize",
|
|
177
|
+
},
|
|
178
|
+
async (event) => {
|
|
179
|
+
await runtime.runtimeAttributor?.initialize(
|
|
180
|
+
deltaManager,
|
|
181
|
+
audience,
|
|
182
|
+
baseSnapshot,
|
|
183
|
+
async (id) => runtime.storage.readBlob(id),
|
|
184
|
+
shouldTrackAttribution,
|
|
185
|
+
);
|
|
186
|
+
event.end({
|
|
187
|
+
attributionEnabledInConfig: shouldTrackAttribution,
|
|
188
|
+
attributionEnabledInDoc: runtime.runtimeAttributor
|
|
189
|
+
? runtime.runtimeAttributor.isEnabled
|
|
190
|
+
: false,
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return runtime;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private runtimeAttributor: RuntimeAttributor | undefined;
|
|
199
|
+
|
|
200
|
+
protected addContainerStateToSummary(
|
|
201
|
+
summaryTree: ISummaryTreeWithStats,
|
|
202
|
+
fullTree: boolean,
|
|
203
|
+
trackState: boolean,
|
|
204
|
+
telemetryContext?: ITelemetryContext,
|
|
205
|
+
) {
|
|
206
|
+
super.addContainerStateToSummary(summaryTree, fullTree, trackState, telemetryContext);
|
|
207
|
+
const attributorSummary = this.runtimeAttributor?.summarize();
|
|
208
|
+
if (attributorSummary) {
|
|
209
|
+
addSummarizeResultToSummary(summaryTree, attributorTreeName, attributorSummary);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
} as unknown as typeof ContainerRuntime;
|
|
213
|
+
|
|
214
|
+
class RuntimeAttributor implements IRuntimeAttributor {
|
|
215
|
+
public get IRuntimeAttributor(): IRuntimeAttributor {
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
public get(key: AttributionKey): AttributionInfo {
|
|
220
|
+
assert(
|
|
221
|
+
this.opAttributor !== undefined,
|
|
222
|
+
0x509 /* RuntimeAttributor must be initialized before getAttributionInfo can be called */,
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
if (key.type === "detached") {
|
|
226
|
+
throw new Error("Attribution of detached keys is not yet supported.");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (key.type === "local") {
|
|
230
|
+
// Note: we can *almost* orchestrate this correctly with internal-only changes by looking up the current
|
|
231
|
+
// client id in the audience. However, for read->write client transition, the container might have not yet
|
|
232
|
+
// received a client id. This is left as a TODO as it might be more easily solved once the detached case
|
|
233
|
+
// is settled (e.g. if it's reasonable for the host to know the current user information at container
|
|
234
|
+
// creation time, we could just use that here as well).
|
|
235
|
+
throw new Error("Attribution of local keys is not yet supported.");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return this.opAttributor.getAttributionInfo(key.seq);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public has(key: AttributionKey): boolean {
|
|
242
|
+
if (key.type === "detached") {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (key.type === "local") {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return this.opAttributor?.tryGetAttributionInfo(key.seq) !== undefined;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private encoder: Encoder<IAttributor, string> = {
|
|
254
|
+
encode: unreachableCase,
|
|
255
|
+
decode: unreachableCase,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
private opAttributor: IAttributor | undefined;
|
|
259
|
+
public isEnabled = false;
|
|
260
|
+
|
|
261
|
+
public async initialize(
|
|
262
|
+
deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
|
|
263
|
+
audience: IAudience,
|
|
264
|
+
baseSnapshot: ISnapshotTree | undefined,
|
|
265
|
+
readBlob: (id: string) => Promise<ArrayBufferLike>,
|
|
266
|
+
shouldAddAttributorOnNewFile: boolean,
|
|
267
|
+
): Promise<void> {
|
|
268
|
+
const attributorTree = baseSnapshot?.trees[attributorTreeName];
|
|
269
|
+
// Existing documents that don't already have a snapshot containing runtime attribution info shouldn't
|
|
270
|
+
// inject any for now--this causes some back-compat integration problems that aren't fully worked out.
|
|
271
|
+
const shouldExcludeAttributor =
|
|
272
|
+
(baseSnapshot !== undefined && attributorTree === undefined) ||
|
|
273
|
+
(baseSnapshot === undefined && !shouldAddAttributorOnNewFile);
|
|
274
|
+
if (shouldExcludeAttributor) {
|
|
275
|
+
// This gives a consistent error for calls to `get` on keys that don't exist.
|
|
276
|
+
this.opAttributor = new Attributor();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
this.isEnabled = true;
|
|
281
|
+
this.encoder = chain(
|
|
282
|
+
new AttributorSerializer(
|
|
283
|
+
(entries) => new OpStreamAttributor(deltaManager, audience, entries),
|
|
284
|
+
deltaEncoder,
|
|
285
|
+
),
|
|
286
|
+
makeLZ4Encoder(),
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
if (attributorTree !== undefined) {
|
|
290
|
+
const id = attributorTree.blobs[opBlobName];
|
|
291
|
+
assert(
|
|
292
|
+
id !== undefined,
|
|
293
|
+
0x50a /* Attributor tree should have op attributor summary blob. */,
|
|
294
|
+
);
|
|
295
|
+
const blobContents = await readBlob(id);
|
|
296
|
+
const attributorSnapshot = bufferToString(blobContents, "utf8");
|
|
297
|
+
this.opAttributor = this.encoder.decode(attributorSnapshot);
|
|
298
|
+
} else {
|
|
299
|
+
this.opAttributor = new OpStreamAttributor(deltaManager, audience);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
public summarize(): ISummaryTreeWithStats | undefined {
|
|
304
|
+
if (!this.isEnabled) {
|
|
305
|
+
// Loaded existing document without attributor data: avoid injecting any data.
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
assert(
|
|
310
|
+
this.opAttributor !== undefined,
|
|
311
|
+
0x50b /* RuntimeAttributor should be initialized before summarization */,
|
|
312
|
+
);
|
|
313
|
+
const builder = new SummaryTreeBuilder();
|
|
314
|
+
builder.addBlob(opBlobName, this.encoder.encode(this.opAttributor));
|
|
315
|
+
return builder.getSummaryTree();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { UsageError } from "@fluidframework/telemetry-utils";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The ID of the string that has been interned, which can be used by a {@link StringInterner} to retrieve the
|
|
10
|
+
* original string.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export type InternedStringId = number & {
|
|
14
|
+
readonly InternedStringId: "e221abc9-9d17-4493-8db0-70c871a1c27c";
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Interns strings as integers.
|
|
19
|
+
*/
|
|
20
|
+
export interface StringInterner {
|
|
21
|
+
getInternedId(input: string): InternedStringId | undefined;
|
|
22
|
+
getString(internedId: number): string;
|
|
23
|
+
getSerializable(): readonly string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Interns strings as integers.
|
|
28
|
+
* Given a string, this class will produce a unique integer associated with that string that can then be used to
|
|
29
|
+
* retrieve the string.
|
|
30
|
+
*/
|
|
31
|
+
export class MutableStringInterner implements StringInterner {
|
|
32
|
+
private readonly stringToInternedIdMap = new Map<string, InternedStringId>();
|
|
33
|
+
private readonly internedStrings: string[] = [];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param inputStrings - A list of strings to intern in the order given. Can be used to rehydrate from a previous
|
|
37
|
+
* `StringInterner`'s {@link StringInterner.getSerializable} return value.
|
|
38
|
+
*/
|
|
39
|
+
constructor(inputStrings: readonly string[] = []) {
|
|
40
|
+
for (const value of inputStrings) {
|
|
41
|
+
this.getOrCreateInternedId(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param input - The string to get the associated intern ID for
|
|
47
|
+
* @returns an intern ID that is uniquely associated with the input string
|
|
48
|
+
*/
|
|
49
|
+
public getOrCreateInternedId(input: string): InternedStringId {
|
|
50
|
+
return this.getInternedId(input) ?? this.createNewId(input);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public getInternedId(input: string): InternedStringId | undefined {
|
|
54
|
+
return this.stringToInternedIdMap.get(input);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
*
|
|
59
|
+
* @param internId - The intern ID to get the associated string for. Can only retrieve strings that have been
|
|
60
|
+
* used as inputs to calls of `getInternId`.
|
|
61
|
+
* @returns a string that is uniquely associated with the given intern ID
|
|
62
|
+
*/
|
|
63
|
+
public getString(internId: number): string {
|
|
64
|
+
const result = this.internedStrings[internId];
|
|
65
|
+
if (!result) {
|
|
66
|
+
throw new UsageError(`No string associated with ${internId}.`);
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @returns The list of strings interned where the indices map to the associated {@link InternedStringId} of
|
|
73
|
+
* each string.
|
|
74
|
+
*/
|
|
75
|
+
public getSerializable(): readonly string[] {
|
|
76
|
+
return this.internedStrings;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Create a new interned id. Assumes without validation that the input doesn't already have an interned id. */
|
|
80
|
+
private createNewId(input: string): InternedStringId {
|
|
81
|
+
const internedId = this.stringToInternedIdMap.size as InternedStringId;
|
|
82
|
+
this.stringToInternedIdMap.set(input, internedId);
|
|
83
|
+
this.internedStrings.push(input);
|
|
84
|
+
return internedId;
|
|
85
|
+
}
|
|
86
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"../../../common/build/build-common/tsconfig.base.json",
|
|
4
|
+
"../../../common/build/build-common/tsconfig.cjs.json",
|
|
5
|
+
],
|
|
6
|
+
"include": ["src/**/*"],
|
|
7
|
+
"exclude": ["src/test/**/*"],
|
|
8
|
+
"compilerOptions": {
|
|
9
|
+
"rootDir": "./src",
|
|
10
|
+
"outDir": "./dist",
|
|
11
|
+
},
|
|
12
|
+
}
|