Skip to content

Commit

Permalink
feat(global-writes): setup feature support COMPASS-8273 (#6271)
Browse files Browse the repository at this point in the history
* setup global writes support

* fix tests
  • Loading branch information
mabaasit committed Sep 23, 2024
1 parent 2b8df72 commit 53dbd10
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 65 deletions.
185 changes: 123 additions & 62 deletions packages/compass-connections/src/hooks/use-connection-supports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const mockConnections: ConnectionInfo[] = [
metricsId: 'metricsId',
metricsType: 'host',
instanceSize: 'M10',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
Expand All @@ -38,6 +40,8 @@ const mockConnections: ConnectionInfo[] = [
metricsId: 'metricsId',
metricsType: 'replicaSet',
instanceSize: 'M0',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
Expand All @@ -53,6 +57,8 @@ const mockConnections: ConnectionInfo[] = [
metricsId: 'metricsId',
metricsType: 'serverless',
instanceSize: 'SERVERLESS_V2',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
Expand All @@ -68,6 +74,8 @@ const mockConnections: ConnectionInfo[] = [
metricsId: 'metricsId',
metricsType: 'replicaSet',
instanceSize: 'M10',
clusterType: 'REPLICASET',
clusterUniqueId: 'clusterUniqueId',
},
},
{
Expand All @@ -83,79 +91,132 @@ const mockConnections: ConnectionInfo[] = [
metricsId: 'metricsId',
metricsType: 'cluster',
instanceSize: 'M10',
clusterType: 'SHARDED',
clusterUniqueId: 'clusterUniqueId',
},
},
{
id: 'dedicated-geo-sharded',
connectionOptions: {
connectionString: 'mongodb://foo',
},
atlasMetadata: {
orgId: 'orgId',
projectId: 'projectId',
clusterName: 'clusterName',
regionalBaseUrl: 'https://example.com',
metricsId: 'metricsId',
metricsType: 'cluster',
instanceSize: 'M30',
clusterType: 'GEOSHARDED',
clusterUniqueId: 'clusterUniqueId',
},
},
];

describe('useConnectionSupports', function () {
it('should return false if the connection does not exist', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('does-not-exist', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});
context('rollingIndexCreation', function () {
it('should return false if the connection does not exist', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('does-not-exist', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false if the connection has no atlasMetadata', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('no-atlasMetadata', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});
it('should return false if the connection has no atlasMetadata', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('no-atlasMetadata', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false for host cluster type', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('host-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});
it('should return false for host cluster type', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('host-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false for serverless cluster type', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('serverless-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});
it('should return false for serverless cluster type', function () {
const { result } = renderHookWithConnections(
() =>
useConnectionSupports('serverless-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return false for free/shared tier clusters', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('free-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});
it('should return false for free/shared tier clusters', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('free-cluster', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return true for dedicated replicaSet clusters', function () {
const { result } = renderHookWithConnections(
() =>
useConnectionSupports('dedicated-replicaSet', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(true);
});

it('should return true for dedicated replicaSet clusters', function () {
const { result } = renderHookWithConnections(
() =>
useConnectionSupports('dedicated-replicaSet', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(true);
it('should return true for dedicated sharded clusters', function () {
const { result } = renderHookWithConnections(
() =>
useConnectionSupports('dedicated-sharded', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(true);
});
});
context('globalWrites', function () {
it('should return false if the connection does not exist', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('does-not-exist', 'globalWrites'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});
it('should return false if the connection has no atlasMetadata', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('no-atlasMetadata', 'globalWrites'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(false);
});

it('should return true for dedicated sharded clusters', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('dedicated-sharded', 'rollingIndexCreation'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(true);
it('should return true if the cluster type is geosharded', function () {
const { result } = renderHookWithConnections(
() => useConnectionSupports('dedicated-geo-sharded', 'globalWrites'),
{
connections: mockConnections,
}
);
expect(result.current).to.equal(true);
});
});
});
18 changes: 16 additions & 2 deletions packages/compass-connections/src/hooks/use-connection-supports.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useSelector } from '../stores/store-context';
import type { ConnectionState } from '../stores/connections-store-redux';

// only one for now
type ConnectionFeature = 'rollingIndexCreation';
type ConnectionFeature = 'rollingIndexCreation' | 'globalWrites';

function isFreeOrSharedTierCluster(instanceSize: string | undefined): boolean {
if (!instanceSize) {
Expand All @@ -25,6 +24,17 @@ function supportsRollingIndexCreation(connection: ConnectionState) {
(metricsType === 'cluster' || metricsType === 'replicaSet')
);
}

function supportsGlobalWrites(connection: ConnectionState) {
const atlasMetadata = connection.info?.atlasMetadata;

if (!atlasMetadata) {
return false;
}

return atlasMetadata.clusterType === 'GEOSHARDED';
}

export function useConnectionSupports(
connectionId: string,
connectionFeature: ConnectionFeature
Expand All @@ -40,6 +50,10 @@ export function useConnectionSupports(
return supportsRollingIndexCreation(connection);
}

if (connectionFeature === 'globalWrites') {
return supportsGlobalWrites(connection);
}

return false;
});
}
10 changes: 10 additions & 0 deletions packages/compass-preferences-model/src/preferences-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type InternalUserPreferences = {
telemetryAnonymousId?: string;
telemetryAtlasUserId?: string;
userCreatedAt: number;
enableGlobalWrites: boolean;
};

// UserPreferences contains all preferences stored to disk.
Expand Down Expand Up @@ -852,6 +853,15 @@ export const storedUserPreferencesProps: Required<{
type: 'boolean',
},

enableGlobalWrites: {
ui: false,
cli: false,
global: false,
description: null,
validator: z.boolean().default(false),
type: 'boolean',
},

...allFeatureFlagsProps,
};

Expand Down
1 change: 1 addition & 0 deletions packages/compass-web/sandbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const App = () => {
maximumNumberOfActiveConnections: isAtlas ? 10 : undefined,
atlasServiceBackendPreset: atlasServiceSandboxBackendVariant,
enableCreatingNewConnections: !isAtlas,
enableGlobalWrites: isAtlas,
}}
onTrack={sandboxTelemetry.track}
onDebug={sandboxLogger.debug}
Expand Down
1 change: 1 addition & 0 deletions packages/compass-web/src/connection-storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe('buildConnectionInfoFromClusterDescription', function () {
metricsType: type === 'sharded' ? 'cluster' : type,
instanceSize: expectedInstanceSize,
regionalBaseUrl: 'https://example.com',
clusterType: clusterDescription.clusterType,
});
});
}
Expand Down
5 changes: 4 additions & 1 deletion packages/compass-web/src/connection-storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ type ReplicationSpec = {
regionConfigs: RegionConfig[];
};

type ClusterType = 'REPLICASET' | 'SHARDED' | 'GEOSHARDED';

type ClusterDescription = {
'@provider': string;
uniqueId: string;
groupId: string;
name: string;
clusterType: string;
clusterType: ClusterType;
srvAddress: string;
state: string;
deploymentItemName: string;
Expand Down Expand Up @@ -196,6 +198,7 @@ export function buildConnectionInfoFromClusterDescription(
regionalBaseUrl: description.dataProcessingRegion.regionalUrl,
...getMetricsIdAndType(description, deploymentItem),
instanceSize: getInstanceSize(description),
clusterType: description.clusterType,
},
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/compass-web/src/entrypoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ const CompassWeb = ({
trackUsageStatistics: true,
enableShell: false,
enableCreatingNewConnections: false,
enableGlobalWrites: false,
...initialPreferences,
})
);
Expand Down
7 changes: 7 additions & 0 deletions packages/connection-info/src/connection-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export interface AtlasClusterMetadata {
* https://github.com/10gen/mms/blob/9e6bf2d81d4d85b5ac68a15bf471dcddc5922323/client/packages/types/nds/provider.ts#L60-L107
*/
instanceSize?: string;

/**
* Possible types of Atlas clusters.
* Copied from:
* https://github.com/10gen/mms/blob/9e6bf2d81d4d85b5ac68a15bf471dcddc5922323/client/packages/types/nds/clusterDescription.ts#L12-L16
*/
clusterType: 'REPLICASET' | 'SHARDED' | 'GEOSHARDED';
}

export interface ConnectionInfo {
Expand Down

0 comments on commit 53dbd10

Please sign in to comment.