mongo

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

README

MongoDB (6.0.1) Sharded Cluster with Docker Compose

Mongo Components

  • Config Server (3 member replica set): configsvr01,configsvr02,configsvr03
  • 3 Shards (each a 3 member PSS replica set):
    • shard01-a,shard01-b, shard01-c
    • shard02-a,shard02-b, shard02-c
    • shard03-a,shard03-b, shard03-c
  • 2 Routers (mongos): router01, router02

Steps

Step 1: Start all of the containers

Run this command:

docker-compose up -d
Step 2: Initialize the replica sets (config servers and shards)

Run these command one by one:

docker-compose exec configsvr01 sh -c "mongosh < /scripts/init-configserver.js"

docker-compose exec shard01-a sh -c "mongosh < /scripts/init-shard01.js"
docker-compose exec shard02-a sh -c "mongosh < /scripts/init-shard02.js"
docker-compose exec shard03-a sh -c "mongosh < /scripts/init-shard03.js"
Step 3: Initializing the router

Run this command:

docker-compose exec router01 sh -c "mongosh < /scripts/init-router.js"
Step 4: Enable sharding and setup sharding-key

Run this command:

docker-compose exec router01 mongosh --port 27017

// Enable sharding for database `yorkie-meta`
sh.enableSharding("yorkie-meta")

// Setup shardingKey for yorkie-meta collections
db.adminCommand( { shardCollection: "yorkie-meta.projects", key: { _id: "hashed" } } )
db.adminCommand( { shardCollection: "yorkie-meta.users", key: { username: "hashed" } } )
db.adminCommand( { shardCollection: "yorkie-meta.clients", key: { project_id: "hashed" } } )
db.adminCommand( { shardCollection: "yorkie-meta.documents", key: { project_id: "hashed" } } )
db.adminCommand( { shardCollection: "yorkie-meta.changes", key: { doc_id: "hashed" } } )
db.adminCommand( { shardCollection: "yorkie-meta.snapshots", key: { doc_id: "hashed" } } )
db.adminCommand( { shardCollection: "yorkie-meta.syncedseqs", key: { doc_id: "hashed" } } )
Step 5: Check mongodb connection & status

mongodb connection string to connect mongodb cluster:

mongodb://127.0.0.1:27117,127.0.0.1:27118

check mongodb shard status:

docker exec -it mongo-config-01 bash -c "echo 'rs.status()' | mongosh --port 27017"


docker exec -it shard-01-node-a bash -c "echo 'rs.help()' | mongosh --port 27017"
docker exec -it shard-01-node-a bash -c "echo 'rs.status()' | mongosh --port 27017" 
docker exec -it shard-01-node-a bash -c "echo 'rs.printReplicationInfo()' | mongosh --port 27017" 
docker exec -it shard-01-node-a bash -c "echo 'rs.printSlaveReplicationInfo()' | mongosh --port 27017"
Step 6: Cleanup

Run these commands:

docker-compose rm
docker-compose down -v --rmi all --remove-orphans

Documentation

Overview

Package mongo implements database interfaces using MongoDB.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a client that connects to Mongo DB and reads or saves Yorkie data.

func Dial

func Dial(conf *Config) (*Client, error)

Dial creates an instance of Client and dials the given MongoDB.

func (*Client) ActivateClient

func (c *Client) ActivateClient(ctx context.Context, projectID types.ID, key string) (*database.ClientInfo, error)

ActivateClient activates the client of the given key.

func (*Client) Close

func (c *Client) Close() error

Close all resources of this client.

func (*Client) CreateChangeInfos

func (c *Client) CreateChangeInfos(
	ctx context.Context,
	projectID types.ID,
	docInfo *database.DocInfo,
	initialServerSeq int64,
	changes []*change.Change,
) error

CreateChangeInfos stores the given changes and doc info.

func (*Client) CreateProjectInfo

func (c *Client) CreateProjectInfo(
	ctx context.Context,
	name string,
	owner types.ID,
) (*database.ProjectInfo, error)

CreateProjectInfo creates a new project.

func (*Client) CreateSnapshotInfo

func (c *Client) CreateSnapshotInfo(
	ctx context.Context,
	docID types.ID,
	doc *document.InternalDocument,
) error

CreateSnapshotInfo stores the snapshot of the given document.

func (*Client) CreateUserInfo

func (c *Client) CreateUserInfo(
	ctx context.Context,
	username string,
	hashedPassword string,
) (*database.UserInfo, error)

CreateUserInfo creates a new user.

func (*Client) DeactivateClient

func (c *Client) DeactivateClient(ctx context.Context, projectID, clientID types.ID) (*database.ClientInfo, error)

DeactivateClient deactivates the client of the given ID.

func (*Client) EnsureDefaultUserAndProject

func (c *Client) EnsureDefaultUserAndProject(
	ctx context.Context,
	username,
	password string,
) (*database.UserInfo, *database.ProjectInfo, error)

EnsureDefaultUserAndProject creates the default user and project if they do not exist.

func (*Client) FindChangeInfosBetweenServerSeqs

func (c *Client) FindChangeInfosBetweenServerSeqs(
	ctx context.Context,
	docID types.ID,
	from int64,
	to int64,
) ([]*database.ChangeInfo, error)

FindChangeInfosBetweenServerSeqs returns the changeInfos between two server sequences.

func (*Client) FindChangesBetweenServerSeqs

func (c *Client) FindChangesBetweenServerSeqs(
	ctx context.Context,
	docID types.ID,
	from int64,
	to int64,
) ([]*change.Change, error)

FindChangesBetweenServerSeqs returns the changes between two server sequences.

func (*Client) FindClientInfoByID

func (c *Client) FindClientInfoByID(ctx context.Context, projectID, clientID types.ID) (*database.ClientInfo, error)

FindClientInfoByID finds the client of the given ID.

func (*Client) FindClosestSnapshotInfo

func (c *Client) FindClosestSnapshotInfo(
	ctx context.Context,
	docID types.ID,
	serverSeq int64,
) (*database.SnapshotInfo, error)

FindClosestSnapshotInfo finds the last snapshot of the given document.

func (*Client) FindDeactivateCandidates

func (c *Client) FindDeactivateCandidates(
	ctx context.Context,
	deactivateThreshold gotime.Duration,
	candidatesLimit int,
) ([]*database.ClientInfo, error)

FindDeactivateCandidates finds the clients that need housekeeping.

func (*Client) FindDocInfoByID

func (c *Client) FindDocInfoByID(ctx context.Context, id types.ID) (*database.DocInfo, error)

FindDocInfoByID finds a docInfo of the given ID.

func (*Client) FindDocInfoByKey

func (c *Client) FindDocInfoByKey(
	ctx context.Context,
	projectID types.ID,
	docKey key.Key,
) (*database.DocInfo, error)

FindDocInfoByKey finds the document of the given key.

func (*Client) FindDocInfoByKeyAndOwner

func (c *Client) FindDocInfoByKeyAndOwner(
	ctx context.Context,
	projectID types.ID,
	clientID types.ID,
	docKey key.Key,
	createDocIfNotExist bool,
) (*database.DocInfo, error)

FindDocInfoByKeyAndOwner finds the document of the given key. If the createDocIfNotExist condition is true, create the document if it does not exist.

func (*Client) FindDocInfosByPaging

func (c *Client) FindDocInfosByPaging(
	ctx context.Context,
	projectID types.ID,
	paging types.Paging[types.ID],
) ([]*database.DocInfo, error)

FindDocInfosByPaging returns the docInfos of the given paging.

func (*Client) FindDocInfosByQuery

func (c *Client) FindDocInfosByQuery(
	ctx context.Context,
	projectID types.ID,
	query string,
	pageSize int,
) (*types.SearchResult[*database.DocInfo], error)

FindDocInfosByQuery returns the docInfos which match the given query.

func (*Client) FindMinSyncedSeqInfo

func (c *Client) FindMinSyncedSeqInfo(
	ctx context.Context,
	docID types.ID,
) (*database.SyncedSeqInfo, error)

FindMinSyncedSeqInfo finds the minimum synced sequence info.

func (*Client) FindProjectInfoByID

func (c *Client) FindProjectInfoByID(ctx context.Context, id types.ID) (*database.ProjectInfo, error)

FindProjectInfoByID returns a project by the given id.

func (*Client) FindProjectInfoByName

func (c *Client) FindProjectInfoByName(
	ctx context.Context,
	owner types.ID,
	name string,
) (*database.ProjectInfo, error)

FindProjectInfoByName returns a project by name.

func (*Client) FindProjectInfoByPublicKey

func (c *Client) FindProjectInfoByPublicKey(ctx context.Context, publicKey string) (*database.ProjectInfo, error)

FindProjectInfoByPublicKey returns a project by public key.

func (*Client) FindUserInfo

func (c *Client) FindUserInfo(ctx context.Context, username string) (*database.UserInfo, error)

FindUserInfo returns a user by username.

func (*Client) ListProjectInfos

func (c *Client) ListProjectInfos(
	ctx context.Context,
	owner types.ID,
) ([]*database.ProjectInfo, error)

ListProjectInfos returns all project infos.

func (*Client) ListUserInfos

func (c *Client) ListUserInfos(
	ctx context.Context,
) ([]*database.UserInfo, error)

ListUserInfos returns all users.

func (*Client) PurgeStaleChanges

func (c *Client) PurgeStaleChanges(
	ctx context.Context,
	docID types.ID,
) error

PurgeStaleChanges delete changes before the smallest in `syncedseqs` to save storage.

func (*Client) UpdateAndFindMinSyncedTicket

func (c *Client) UpdateAndFindMinSyncedTicket(
	ctx context.Context,
	clientInfo *database.ClientInfo,
	docID types.ID,
	serverSeq int64,
) (*time.Ticket, error)

UpdateAndFindMinSyncedTicket updates the given serverSeq of the given client and returns the min synced ticket.

func (*Client) UpdateClientInfoAfterPushPull

func (c *Client) UpdateClientInfoAfterPushPull(
	ctx context.Context,
	clientInfo *database.ClientInfo,
	docInfo *database.DocInfo,
) error

UpdateClientInfoAfterPushPull updates the client from the given clientInfo after handling PushPull.

func (*Client) UpdateProjectInfo

func (c *Client) UpdateProjectInfo(
	ctx context.Context,
	owner types.ID,
	id types.ID,
	fields *types.UpdatableProjectFields,
) (*database.ProjectInfo, error)

UpdateProjectInfo updates the project info.

func (*Client) UpdateSyncedSeq

func (c *Client) UpdateSyncedSeq(
	ctx context.Context,
	clientInfo *database.ClientInfo,
	docID types.ID,
	serverSeq int64,
) error

UpdateSyncedSeq updates the syncedSeq of the given client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL