Skip to content

Commit

Permalink
Dogfood Connect functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Aug 9, 2023
1 parent 8da4160 commit c766394
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
27 changes: 14 additions & 13 deletions samples/Infrastructure/Store.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,29 @@ module Cosmos =
// - In normal usage, you typically connect to a single container only.
// - In hot-warm scenarios, the Archive Container will frequently be within the same account and hence can share a CosmosClient
// For these typical purposes, CosmosStoreClient.Connect should be used to establish the Client and Connection, not custom wiring as we have here
let createClient (a : Arguments) connectionString =
let connector = CosmosStoreConnector(Discovery.ConnectionString connectionString, a.Timeout, a.Retries, a.MaxRetryWaitTime, ?mode=a.Mode)
connector.CreateUninitialized()
let createConnector (a: Arguments) connectionString =
CosmosStoreConnector(Discovery.ConnectionString connectionString, a.Timeout, a.Retries, a.MaxRetryWaitTime, ?mode=a.Mode)
let connect (log: ILogger) (a: Arguments) =
let primaryClient, primaryDatabase, primaryContainer as primary = createClient a a.Connection, a.Database, a.Container
logContainer log "Primary" (a.Mode, primaryClient.Endpoint, primaryDatabase, primaryContainer)
let primaryConnector, primaryDatabase, primaryContainer as primary = createConnector a a.Connection, a.Database, a.Container
logContainer log "Primary" (a.Mode, primaryConnector.Endpoint, primaryDatabase, primaryContainer)
let archive =
match a.Archive with
| Some (Some c2, db, container) -> Some (createClient a c2, db, container)
| Some (None, db, container) -> Some (primaryClient, db, container)
| Some (Some c2, db, container) -> Some (createConnector a c2, db, container)
| Some (None, db, container) -> Some (primaryConnector, db, container)
| None -> None
archive |> Option.iter (fun (client, db, container) -> logContainer log "Archive" (a.Mode, client.Endpoint, db, container))
primary, archive
let config (log : ILogger) (cache, unfolds) (a : Arguments) =
let context =
match connect log a with
| (client, databaseId, containerId), None ->
let c = CosmosStoreClient client
CosmosStoreContext(c, databaseId, containerId, a.TipMaxEvents, queryMaxItems = a.QueryMaxItems, tipMaxJsonLength = a.TipMaxJsonLength)
| (client, databaseId, containerId), Some (aClient, aDatabaseId, aContainerId) ->
let c = CosmosStoreClient(client, aClient)
CosmosStoreContext(c, databaseId, containerId, a.TipMaxEvents, queryMaxItems = a.QueryMaxItems, tipMaxJsonLength = a.TipMaxJsonLength,
| (connector, databaseId, containerId), None ->
CosmosStoreContext.Connect(connector, databaseId, containerId, a.TipMaxEvents, queryMaxItems = a.QueryMaxItems, tipMaxJsonLength = a.TipMaxJsonLength)
|> Async.RunSynchronously
| (connector, databaseId, containerId), Some (aConnector, aDatabaseId, aContainerId) ->
let cosmosClient = connector.Connect(databaseId, [| containerId |]) |> Async.RunSynchronously
let archiveCosmosClient = aConnector.Connect(aDatabaseId, [| aContainerId |]) |> Async.RunSynchronously
let client = CosmosStoreClient(cosmosClient, archiveCosmosClient)
CosmosStoreContext(client, databaseId, containerId, a.TipMaxEvents, queryMaxItems = a.QueryMaxItems, tipMaxJsonLength = a.TipMaxJsonLength,
archiveDatabaseId = aDatabaseId, archiveContainerId = aContainerId)
log.Information("CosmosStore Max Events in Tip: {maxTipEvents}e {maxTipJsonLength}b Items in Query: {queryMaxItems}",
a.TipMaxEvents, a.TipMaxJsonLength, a.QueryMaxItems)
Expand Down
37 changes: 15 additions & 22 deletions tests/Equinox.CosmosStore.Integration/CosmosFixtures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,61 +87,54 @@ let discoverConnection () =
| None -> "localDocDbSim", Discovery.AccountUriAndKey(Uri "https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==")
| Some connectionString -> "EQUINOX_COSMOS_CONNECTION", Discovery.ConnectionString connectionString

let createClient (log : Serilog.ILogger) name (discovery : Discovery) =
let createConnector (log: Serilog.ILogger) =
let name, discovery = discoverConnection ()
let connector = CosmosStoreConnector(discovery, requestTimeout = TimeSpan.FromSeconds 3.,
maxRetryAttemptsOnRateLimitedRequests = 2, maxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromMinutes 1.)
log.Information("CosmosStore {name} {endpoint}", name, discovery.Endpoint)
connector.CreateUninitialized()

let connect log =
let name, discovery = discoverConnection ()
let client = createClient log name discovery
CosmosStoreClient(client)
connector

[<Xunit.CollectionDefinition "DocStore">]
type DocStoreCollection() =
do ()

let createPrimaryContextIgnoreMissing client containerId queryMaxItems tipMaxEvents ignoreMissing =
CosmosStoreContext(client, databaseId, containerId, tipMaxEvents = tipMaxEvents, queryMaxItems = queryMaxItems, ignoreMissingEvents = ignoreMissing)
let createPrimaryContextIgnoreMissing connector containerId queryMaxItems tipMaxEvents ignoreMissing =
CosmosStoreContext.Connect(connector, databaseId, containerId, tipMaxEvents = tipMaxEvents, queryMaxItems = queryMaxItems, ignoreMissingEvents = ignoreMissing)

let defaultTipMaxEvents = 10
let createArchiveContext log queryMaxItems =
let client = connect log
CosmosStoreContext(client, databaseId, containerId, defaultTipMaxEvents, queryMaxItems = queryMaxItems)
CosmosStoreContext.Connect(createConnector log, databaseId, containerId, defaultTipMaxEvents, queryMaxItems = queryMaxItems)
let createFallbackContext log queryMaxItems =
let client = connect log
CosmosStoreContext(client, databaseId, containerId, defaultTipMaxEvents, queryMaxItems = queryMaxItems, archiveContainerId = archiveContainerId)
CosmosStoreContext.Connect(createConnector log, databaseId, containerId, defaultTipMaxEvents, queryMaxItems = queryMaxItems, archiveContainerId = archiveContainerId)

type StoreContext = CosmosStoreContext
type StoreCategory<'E, 'S> = CosmosStoreCategory<'E, 'S, unit>
let primaryTarget = containerId
#endif

let createPrimaryContextEx log queryMaxItems tipMaxEvents =
let client = connect log
createPrimaryContextIgnoreMissing client primaryTarget queryMaxItems tipMaxEvents false
createPrimaryContextIgnoreMissing (createConnector log) primaryTarget queryMaxItems tipMaxEvents false

let createPrimaryContext log queryMaxItems =
createPrimaryContextEx log queryMaxItems defaultTipMaxEvents

let defaultQueryMaxItems = 10

let createPrimaryEventsContext log queryMaxItems tipMaxItems =
let context = createPrimaryContextEx log queryMaxItems tipMaxItems
let context = createPrimaryContextEx log queryMaxItems tipMaxItems |> Async.RunSynchronously
Core.EventsContext(context, log)

let createPrimaryEventsContextWithUnsafe log queryMaxItems tipMaxItems =
let client = connect log
let connector = createConnector log
let create ignoreMissing =
let context = createPrimaryContextIgnoreMissing client primaryTarget queryMaxItems tipMaxItems ignoreMissing
let context = createPrimaryContextIgnoreMissing connector primaryTarget queryMaxItems tipMaxItems ignoreMissing |> Async.RunSynchronously
Core.EventsContext(context, log)
create false, create true

let createArchiveEventsContext log queryMaxItems =
let context = createArchiveContext log queryMaxItems
let context = createArchiveContext log queryMaxItems |> Async.RunSynchronously
Core.EventsContext(context, log)

let createFallbackEventsContext log queryMaxItems =
let context = createFallbackContext log queryMaxItems
let context = createFallbackContext log queryMaxItems |> Async.RunSynchronously
Core.EventsContext(context, log)

let defaultQueryMaxItems = 10

0 comments on commit c766394

Please sign in to comment.