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 6 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
4 changes: 4 additions & 0 deletions api-report/fluid-static.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { AttachState } from '@fluidframework/container-definitions';
import { BaseContainerRuntimeFactory } from '@fluidframework/aqueduct';
import { ConnectionState } from '@fluidframework/container-definitions';
import { DataObject } from '@fluidframework/aqueduct';
import { IAudience } from '@fluidframework/container-definitions';
import { IChannelFactory } from '@fluidframework/datastore-definitions';
Expand Down Expand Up @@ -42,6 +43,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 +61,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,6 +134,7 @@ export interface IContainer extends IEventProvider<IContainerEvents>, IFluidRout
close(error?: ICriticalContainerError): void;
closeAndGetPendingLocalState(): string;
readonly closed: boolean;
// @deprecated
readonly connected: boolean;
readonly connectionState: ConnectionState;
deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>;
Expand All @@ -148,10 +149,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
11 changes: 9 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,27 @@ 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;

/**
* 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
17 changes: 16 additions & 1 deletion packages/framework/fluid-static/src/fluidContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { TypedEventEmitter } from "@fluidframework/common-utils";
import { IFluidLoadable } from "@fluidframework/core-interfaces";
import { IEvent, IEventProvider } from "@fluidframework/common-definitions";
import { AttachState, IContainer } from "@fluidframework/container-definitions";
import { AttachState, IContainer, ConnectionState } from "@fluidframework/container-definitions";
import { LoadableObjectClass, LoadableObjectRecord } from "./types";
import { RootDataObject } from "./rootDataObject";

Expand Down Expand Up @@ -74,9 +74,17 @@ export interface IFluidContainerEvents extends IEvent {
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 +192,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
73 changes: 73 additions & 0 deletions packages/loader/container-loader/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,79 @@ 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.

assert(this._attachState === AttachState.Attached, "Attempting to connect() a container that is not attached");
scottn12 marked this conversation as resolved.
Show resolved Hide resolved

// 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.connectionState !== ConnectionState.Disconnected) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that if we are in the process of attempting to connect, then this.connectionState would be disconnected, and calling this API would be noop. But the outcome - connection will succeed eventually, while user clearly does not want that. This is a new flow that did not exist before, thus I think we do not have good mechanisms to control it (i.e. there is no way to cancel pending connection, the only thing we do is to throw away connection on success if container is closed).

I think this needs to be redesigned in some way and implemented correctly (i.e. doing what users expect from this API). It's probably fine to do it as separate PR, but we should do it right after that, as it is not that uncommon situation (especially when user is offline).

Copy link
Contributor

Choose a reason for hiding this comment

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

If you allow to go past that check and assess this._deltaManager.connectionManager.reconnectMode in disconnectInternal(), then I think it will do 95% of what is required (assuming that DeltaManager can throw this connection away once it connects and it realizes that reconnect mode is Disabled, though I think we need to use Never, not Disabled, and review code around it, as I think some asserts will not like it)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you be able to explain why Never is preferred over Disabled? At the moment we seem to only use Never to indicate that a container should never be able to reconnect in the future. This doesn't seem to align with what we want to accomplish with disconnect(), since we want to allow the container to reconnect at a later point when calling connect().

Copy link
Contributor

Choose a reason for hiding this comment

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

I think Disabled is used (for example) for agents (including summarizer) and it allows one connection. So decision not to reconnect is made on loss of connection, not when connection is successfully established. Here, when user wants to stop connectivity flow, it's too late to check (pending connection is already in flight), and once pending connection is established it can't make decision to abandon such connection for Disalbed state (as that will kill agent connection flow). So we need some other state to differentiate "first connection" vs. "connection should not be allows".
I think Never might also be wrong (as some other things are tied to it), so more exploration is required to properly handle that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like it's probably best to look into this (and the other concerns you mentioned above with disconnect() during a pending connection) in a separate PR like you suggested.

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