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

Add connect(), disconnect(), deprecate setAutoReconnect(), resume(), and connected #9439

Merged
merged 18 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions api-report/container-loader.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ export class Container extends EventEmitterWithErrorHandling<IContainerEvents> i
// (undocumented)
get closed(): boolean;
// (undocumented)
connect(): void;
markfields marked this conversation as resolved.
Show resolved Hide resolved
// (undocumented)
get connected(): boolean;
// (undocumented)
get connectionState(): ConnectionState;
static createDetached(loader: Loader, codeDetails: IFluidCodeDetails): Promise<Container>;
// (undocumented)
get deltaManager(): IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>;
// (undocumented)
disconnect(): void;
forceReadonly(readonly: boolean): void;
// (undocumented)
getAbsoluteUrl(relativeUrl: string): Promise<string | undefined>;
Expand Down
13 changes: 13 additions & 0 deletions api-report/fluid-static.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ import { IFluidDataStoreFactory } from '@fluidframework/runtime-definitions';
import { IFluidLoadable } from '@fluidframework/core-interfaces';
import { TypedEventEmitter } from '@fluidframework/common-utils';

// @public
export namespace ConnectionState {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really love to see us change names here:

  • "Connecting" state should be actually call "Connected".
  • "Connected" should be renamed to something else that clearly articulates actual state - "up-to-date & syncing changes".

Also, enums are expensive in JS, and I've heard advice to use union types only (as on line 29).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also suggest to re-export - it's cheaper that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied these values from container-definitions in order to make IContianer and IFluidContainer consistent, however we can actually just import ConnectionState from container-definitions directly. As per changing the names, I believe that would be a breaking change, so we should probably do that in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to have discussion on naming here. If we agree to change names, then I'd rather see you use new names here (doing remapping), not to create more back compat debt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • "Connected" should be renamed to something else that clearly articulates actual state - "up-to-date & syncing changes".

@vladsud, I think Synced could be an adequate name. My only concern is that it does not emphasize that the container is actively syncing changes, however, I think most developers should be able to infer this.

Copy link
Contributor Author

@scottn12 scottn12 Mar 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vladsud, on a related note, do you know why we created ConnectionState in container.ts instead of just importing the one defined in container-definitions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know, I'd guess someone added it there, likely as result of adding method to IContainer. Likely before that work there was no need to have it in definitions package.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markfields for naming discussion. I'm fine with "Synced", but let's run it by wider audience.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For more naming discussion: #9677

export type Connected = 2;
export type Connecting = 1;
export type Disconnected = 0;
}

// @public
export type ConnectionState = ConnectionState.Disconnected | ConnectionState.Connecting | ConnectionState.Connected;

// @public
export interface ContainerSchema {
dynamicObjectTypes?: LoadableObjectClass<any>[];
Expand All @@ -42,6 +52,7 @@ export class FluidContainer extends TypedEventEmitter<IFluidContainerEvents> imp
attach(): Promise<string>;
get attachState(): AttachState;
get connected(): boolean;
get connectionState(): ConnectionState;
create<T extends IFluidLoadable>(objectClass: LoadableObjectClass<T>): Promise<T>;
dispose(): void;
get disposed(): boolean;
Expand All @@ -59,7 +70,9 @@ export interface IConnection {
export interface IFluidContainer extends IEventProvider<IFluidContainerEvents> {
attach(): Promise<string>;
readonly attachState: AttachState;
// @deprecated
readonly connected: boolean;
readonly connectionState: ConnectionState;
create<T extends IFluidLoadable>(objectClass: LoadableObjectClass<T>): Promise<T>;
dispose(): void;
readonly disposed: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,12 @@ export interface IContainer extends IEventProvider<IContainerEvents>, IFluidRout
close(error?: ICriticalContainerError): void;
closeAndGetPendingLocalState(): string;
readonly closed: boolean;
connect?(): void;
// @deprecated
readonly connected: boolean;
readonly connectionState: ConnectionState;
deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>;
disconnect?(): void;
// @alpha
forceReadonly?(readonly: boolean): any;
getAbsoluteUrl(relativeUrl: string): Promise<string | undefined>;
Expand All @@ -148,10 +151,10 @@ export interface IContainer extends IEventProvider<IContainerEvents>, IFluidRout
readonly readOnlyInfo: ReadOnlyInfo;
request(request: IRequest): Promise<IResponse>;
resolvedUrl: IResolvedUrl | undefined;
// @alpha
// @deprecated
resume?(): void;
serialize(): string;
// @alpha
// @deprecated
setAutoReconnect?(reconnect: boolean): void;
}

Expand Down
21 changes: 19 additions & 2 deletions common/lib/container-definitions/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,37 @@ export interface IContainer extends IEventProvider<IContainerEvents>, IFluidRout

/**
* Boolean indicating whether the container is currently connected or not
* @deprecated - 0.58, This API will be removed in 0.60
* Use `connectionState` instead
* See https://github.com/microsoft/FluidFramework/issues/9167 for context
scottn12 marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly connected: boolean;

/**
* Try to connect the container to the delta stream
*/
connect?(): void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add new APIs on class, propagate and wait some time, and then add them here, but not make them optional?
Alternatively, we can make them non-optional from start - I do not see big problem here. As long as we are adding implementation in parallel, package bump would be noop in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense. I need to create a follow up PR to add connect() and disconnect() to IFluidContainer anyway, so we could just add in the functions to both IContainer and IFluidContainer in one PR.


/**
* Disconnect the container from the delta stream
*/
disconnect?(): void;

/**
* Dictates whether or not the current container will automatically attempt to reconnect to the delta stream
* after receiving a disconnect event
* @param reconnect - Boolean indicating if reconnect should automatically occur
* @alpha
* @deprecated - 0.58.1, This API will be removed in 0.59.0
* Use `connect()` and `disconnect()` instead
* See https://github.com/microsoft/FluidFramework/issues/9167 for context
*/
setAutoReconnect?(reconnect: boolean): void;

/**
* Have the container attempt to resume processing ops
* @alpha
* @deprecated - 0.58.1, This API will be removed in 0.59.0
* Use `connect()` instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add connect() and disconnect() as optional right away, especially since you're mentioning them here, and to help consumers transition? I don't think adding an optional thing is ever breaking (but could be wrong).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually had them added as optional earlier but changed it after discussion here. To summarize, I will have to add the functions to IFluidContainer in a separate PR anyway, so I am planning to add them to IContainer and IFluidContainer in one PR. This way we won't have to make another PR to change it from optional to mandatory functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Feel free to ignore what I'm about to say then :)

In general, I believe having a property absent is completely equivalent to having it present but optional, in terms of compatibility. It's just a hint saying "you may find something here...". So I would think you can and should add the optional functions to both interfaces in this PR.

This way we won't have to make another PR to change it from optional to mandatory

To say the above another way, changing a member from optional to mandatory is equivalent to adding it when it's missing. At least I think so - correct me if I'm wrong, this stuff really bends my brain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markfields The main problem with going from optional to mandatory I think is that it can still be a breaking change to anyone with an external implementation of the interface, where an implementation/object matching the interface suddenly no longer does so. It sounds like this shouldn't be an issue in this case though, but if there's a breaking change no matter how you slice it there's some appeal to doing just one step instead of two

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying something different, I agree with that for sure.

Going from nothing to optional is never any trouble. Going from optional to required is trouble - it's as much trouble as adding a required thing outright. So might as well add optional earlier than later to give people an easier time onboarding/preparing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the appeal of doing one step instead of two?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @heliocliu and I'm not sure why this was removed from this PR? The goal of this PR is to introduce connect and disconnect at the Container level. Since we are enforcing using IContainer we are telling devs to use APIs as part of depreciation that aren't actually exposed.

Seems like the right step is to add them as optional as part of this PR and include the version in the Breaking.md that we will be making the breaking change. Then make them mandatory in next (or whatever breaking change we decide)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the appeal of doing one step instead of two?

Fewer steps 🙃

* See https://github.com/microsoft/FluidFramework/issues/9167 for context
*/
resume?(): void;

Expand Down
40 changes: 40 additions & 0 deletions packages/framework/fluid-static/src/fluidContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,49 @@ export interface IFluidContainerEvents extends IEvent {
(event: "connected" | "dispose" | "disconnected" | "saved" | "dirty", listener: () => void): void;
}

/**
* Namespace for the different connection states a container can be in
*/
export namespace ConnectionState {
/**
* The document is no longer connected to the delta server
*/
export type Disconnected = 0;

/**
* The document has an inbound connection but is still pending for outbound deltas
*/
export type Connecting = 1;

/**
* The document is fully connected
*/
export type Connected = 2;
}

/**
* Type defining the different states of connectivity a container can be in
*/
export type ConnectionState = ConnectionState.Disconnected | ConnectionState.Connecting | ConnectionState.Connected;

/**
* The IFluidContainer provides an entrypoint into the client side of collaborative Fluid data. It provides access
* to the data as well as status on the collaboration session.
*/
export interface IFluidContainer extends IEventProvider<IFluidContainerEvents> {
/**
* Whether the container is connected to the collaboration session.
* @deprecated - 0.58.1, This API will be removed in 0.59.0
* Use `connectionState` instead
* See https://github.com/microsoft/FluidFramework/issues/9167 for context
*/
readonly connected: boolean;

/**
* Provides the current connected state of the container
*/
readonly connectionState: ConnectionState;

/**
* A container is considered **dirty** if it has local changes that have not yet been acknowledged by the service.
* You should always check the `isDirty` flag before closing the container or navigating away from the page.
Expand Down Expand Up @@ -184,6 +217,13 @@ export class FluidContainer extends TypedEventEmitter<IFluidContainerEvents> imp
return this.container.connected;
}

/**
* {@inheritDoc IFluidContainer.connectionState}
*/
public get connectionState(): ConnectionState {
scottn12 marked this conversation as resolved.
Show resolved Hide resolved
return this.container.connectionState;
}

/**
* {@inheritDoc IFluidContainer.initialObjects}
*/
Expand Down
72 changes: 72 additions & 0 deletions packages/loader/container-loader/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,78 @@ export class Container extends EventEmitterWithErrorHandling<IContainerEvents> i
}
}

public connect() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should actually just inherit the docs from the interface.

if (!this.closed && !this.connected) {
// Note: no need to fetch ops as we do it preemptively as part of DeltaManager.attachOpHandler().
// If there is gap, we will learn about it once connected, but the gap should be small (if any),
// assuming that connect() is called quickly after initial container boot.
this.connectInternal({ reason: "DocumentOpenResume", fetchOpsFromStorage: false });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's think about what the reason should be. It used to be different for resume v. setAutoReconnect, now it will be the same.

Copy link
Contributor Author

@scottn12 scottn12 Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markfields, thoughts on DocumentConnect? This would match the DocumentOpen reason used when the container is initially created and tries to connect.

}
}

private connectInternal(args: IConnectionArgs) {
assert(!this.closed, "Attempting to connect() a closed DeltaManager");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see code reuse with existing resume/setAutoReconnect flow to easier see where this logic is different from existing logic. Ideally there should be no differences and one implementation should call into another, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So basically have connect() call resumeInteranl() and setAutoReconnect(true)? If so, we should probably make setAutoReconnect() a private function (since we would no longer be removing it).

Copy link
Contributor

@vladsud vladsud Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not matter which way you go (connect calling setAutoReconnect(true) or other way around). We can clean that later when we remove deprecated APIs (and have just one function), but for now it would be really great to see if new functionality differs from the previous one through code reuse. If you can't reuse code, that means it's not 1-to-1 reman, and it should be clearly spelled out for consumers such that they are not caught by surprise.

Copy link
Contributor Author

@scottn12 scottn12 Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I wrote a new connectInernal() function is because I felt that what we wanted is an overlap between the current resume() and setAutoReconnect(true) functions. However, I didn't want to call those functions straight up because there is a bit of overlap between the two. For example, they both call end up calling this.connectToDeltaStream(). Although I don't think this would cause issues it did seem a bit messy so I ended up essentially combining the functions while trying to eliminate the overlap (which resulted in connectInternal()). So I do believe we could reuse code, but it would be a bit inefficient. Do you think this is a bad approach and we should just reuse the existing logic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I assume the guidance is for people to move from setAutoReconnect(true) to connect() calls.
The two obvious things I see that are different in flows:

  1. There is a check for _attachState === AttachState.Attached && this.resumedOpProcessingAfterLoad in setAutoReconnect() that is missing here
  2. There is manualReconnectInProgress tracking.

Second one is likely not interesting and can probably be removed (or added).
First part - I do not know, it feels like it should apply here (I'd look at history on when/why it was added)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable after latest changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could call resumeInternal from the top of connectInternal now, would be nice to reduce code duplication (and resumeInternal looks like it will stick around since it's called from attach)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And totally optional, but consider pulling the if (currentMode !== mode) { ... } block into a helper, since it's now here in triplicate. And it's pretty crucial the three places stay in sync since they're messing with the same state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to putting manualReconnectInProgress back the way it was... Or if not, then just delete that variable because this was the only place it was set to true.

Probably should take a pass over the values used for connectionInitiationReason in logConnectionStateChangeTelemetry and see if they still make sense given the merging of resume and setAutoReconnect(true) from an API standpoint. I think I'm confused why "AutoReconnect" and "ManualReconnect" were distinguished by this case anyway...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your first two comments and will update the PR shortly to make these changes. Re: your last comment, since we merged resume() and setAutoReconnect(), I think it does make sense to just delete manualReconnectInProgress.


// Resume processing ops
if (!this.resumedOpProcessingAfterLoad) {
this.resumedOpProcessingAfterLoad = true;
this._deltaManager.inbound.resume();
this._deltaManager.inboundSignal.resume();
}

// Ensure connection to web socket
this.connectToDeltaStream(args);

const mode = ReconnectMode.Enabled;
const currentMode = this._deltaManager.connectionManager.reconnectMode;

// Set Auto Reconnect Mode
if (currentMode !== mode) {
const now = performance.now();
const duration = now - this.setAutoReconnectTime;
this.setAutoReconnectTime = now;

this.mc.logger.sendTelemetryEvent({
eventName: "AutoReconnectEnabled",
connectionMode: this.connectionMode,
connectionState: ConnectionState[this.connectionState],
duration,
});

this._deltaManager.connectionManager.setAutoReconnect(mode);
}
}

public disconnect() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation.

if (!this.closed && this.connected) {
scottn12 marked this conversation as resolved.
Show resolved Hide resolved
this.disconnectInternal();
}
}

private disconnectInternal() {
assert(!this.closed, "Attempting to disconnect() a closed DeltaManager");
scottn12 marked this conversation as resolved.
Show resolved Hide resolved

const mode = ReconnectMode.Disabled;
const currentMode = this._deltaManager.connectionManager.reconnectMode;

// Set Auto Reconnect Mode
if (currentMode !== mode) {
const now = performance.now();
const duration = now - this.setAutoReconnectTime;
this.setAutoReconnectTime = now;

this.mc.logger.sendTelemetryEvent({
eventName: "AutoReconnectDisabled",
connectionMode: this.connectionMode,
connectionState: ConnectionState[this.connectionState],
duration,
});

// ConnectionManager will handle disconnecting the container
this._deltaManager.connectionManager.setAutoReconnect(mode);
}
}

public resume() {
if (!this.closed) {
// Note: no need to fetch ops as we do it preemptively as part of DeltaManager.attachOpHandler().
Expand Down