Skip to content

Commit

Permalink
Add gateway flag to server command
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Oct 16, 2024
1 parent 31cc753 commit 31fc46d
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 60 deletions.
6 changes: 6 additions & 0 deletions cmd/yorkie/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ func init() {
server.DefaultHostname,
"Yorkie Server Hostname",
)
cmd.Flags().StringVar(
&conf.Backend.GatewayAddr,
"backend-gateway-addr",
server.DefaultGatewayAddr,
"Gateway address",
)

rootCmd.AddCommand(cmd)
}
3 changes: 3 additions & 0 deletions server/backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type Config struct {

// Hostname is yorkie server hostname. hostname is used by metrics.
Hostname string `yaml:"Hostname"`

// GatewayAddr is the address of the gateway server.
GatewayAddr string `yaml:"GatewayAddr"`
}

// Validate validates this config.
Expand Down
50 changes: 0 additions & 50 deletions server/backend/database/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,56 +676,6 @@ func RunActivateClientDeactivateClientTest(t *testing.T, db database.Database, p
assert.Equal(t, t.Name(), clientInfo.Key)
assert.Equal(t, database.ClientDeactivated, clientInfo.Status)
})

t.Run("ensure document detached when deactivate client test", func(t *testing.T) {
ctx := context.Background()

// 01. Create a client
clientInfo, err := db.ActivateClient(ctx, projectID, t.Name())
assert.NoError(t, err)
assert.Equal(t, t.Name(), clientInfo.Key)
assert.Equal(t, database.ClientActivated, clientInfo.Status)

// 02. Create documents and attach them to the client
for i := 0; i < 3; i++ {
docInfo, _ := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), helper.TestDocKey(t, i), true)
assert.NoError(t, clientInfo.AttachDocument(docInfo.ID, false))
clientInfo.Documents[docInfo.ID].ServerSeq = 1
clientInfo.Documents[docInfo.ID].ClientSeq = 1
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo, docInfo))

result, err := db.FindClientInfoByRefKey(ctx, clientInfo.RefKey())
assert.Equal(t, result.Documents[docInfo.ID].Status, database.DocumentAttached)
assert.Equal(t, result.Documents[docInfo.ID].ServerSeq, int64(1))
assert.Equal(t, result.Documents[docInfo.ID].ClientSeq, uint32(1))
assert.NoError(t, err)
}

// 03. Remove one document
docInfo, err := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), helper.TestDocKey(t, 2), true)
assert.NoError(t, err)
assert.NoError(t, clientInfo.RemoveDocument(docInfo.ID))
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo, docInfo))

// 04. Deactivate the client
result, err := db.DeactivateClient(ctx, types.ClientRefKey{
ProjectID: projectID,
ClientID: clientInfo.ID,
})
assert.NoError(t, err)

// 05. Check whether doc.Status is reflected properly.
// If it was `DocumentAttached`, it should be changed to `DocumentDetached`.
for i := 0; i < 2; i++ {
docInfo, err := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), helper.TestDocKey(t, i), true)
assert.NoError(t, err)
assert.Equal(t, result.Documents[docInfo.ID].Status, database.DocumentDetached)
}
// If it was `DocumentRemoved`, it should be remained `DocumentRemoved`.
docInfo, err = db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), helper.TestDocKey(t, 2), true)
assert.NoError(t, err)
assert.Equal(t, result.Documents[docInfo.ID].Status, database.DocumentRemoved)
})
}

// RunUpdateProjectInfoTest runs the UpdateProjectInfo tests for the given db.
Expand Down
13 changes: 7 additions & 6 deletions server/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"

"github.com/yorkie-team/yorkie/api/types"
"github.com/yorkie-team/yorkie/server/backend"
"github.com/yorkie-team/yorkie/server/backend/database"
"github.com/yorkie-team/yorkie/system"
)
Expand All @@ -47,28 +48,28 @@ func Activate(
// Deactivate deactivates the given client.
func Deactivate(
ctx context.Context,
db database.Database,
be *backend.Backend,
project *types.Project,
refKey types.ClientRefKey,
) (*database.ClientInfo, error) {
info, err := FindActiveClientInfo(ctx, db, refKey)
info, err := FindActiveClientInfo(ctx, be.DB, refKey)
if err != nil {
return nil, err
}

// TODO(hackerwins): Inject gatewayAddr
systemClient, err := system.Dial("localhost:8080")
systemClient, err := system.Dial(be.Config.GatewayAddr, system.WithInsecure(true))
if err != nil {
return nil, err
}
defer systemClient.Close()

for docID, clientDocInfo := range info.Documents {
// TODO(hackerwins): Solve N+1
if clientDocInfo.Status == database.DocumentDetached {
continue
}

docInfo, err := db.FindDocInfoByRefKey(ctx, types.DocRefKey{
docInfo, err := be.DB.FindDocInfoByRefKey(ctx, types.DocRefKey{
ProjectID: project.ID,
DocID: docID,
})
Expand All @@ -86,7 +87,7 @@ func Deactivate(
}
}

info, err = db.DeactivateClient(ctx, refKey)
info, err = be.DB.DeactivateClient(ctx, refKey)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion server/clients/housekeeping.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func DeactivateInactives(

deactivatedCount := 0
for _, pair := range candidates {
if _, err := Deactivate(ctx, be.DB, pair.project.ToProject(), pair.client.RefKey()); err != nil {
if _, err := Deactivate(ctx, be, pair.project.ToProject(), pair.client.RefKey()); err != nil {
return database.DefaultProjectID, err
}

Expand Down
3 changes: 2 additions & 1 deletion server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const (
DefaultProjectInfoCacheSize = 256
DefaultProjectInfoCacheTTL = 10 * time.Minute

DefaultHostname = ""
DefaultHostname = ""
DefaultGatewayAddr = "localhost:8080"
)

// Config is the configuration for creating a Yorkie instance.
Expand Down
3 changes: 3 additions & 0 deletions server/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ Backend:
# determined automatically by the OS (Optional, default: os.Hostname()).
Hostname: ""

# GatewayAddr is the address of the gateway server.
GatewayAddr: ""

# Mongo is the MongoDB configuration (Optional).
Mongo:
# ConnectionTimeout is the timeout for connecting to MongoDB.
Expand Down
2 changes: 1 addition & 1 deletion server/rpc/yorkie_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *yorkieServer) DeactivateClient(
}

project := projects.From(ctx)
_, err = clients.Deactivate(ctx, s.backend.DB, project, types.ClientRefKey{
_, err = clients.Deactivate(ctx, s.backend, project, types.ClientRefKey{
ProjectID: project.ID,
ClientID: types.IDFromActorID(actorID),
})
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r *Yorkie) DeactivateClient(ctx context.Context, c1 *client.Client) error
return err
}

_, err = clients.Deactivate(ctx, r.backend.DB, project, types.ClientRefKey{
_, err = clients.Deactivate(ctx, r.backend, project, types.ClientRefKey{
ProjectID: project.ID,
ClientID: types.IDFromActorID(c1.ID()),
})
Expand Down
1 change: 1 addition & 0 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func TestConfig() *server.Config {
AuthWebhookCacheUnauthTTL: AuthWebhookCacheUnauthTTL.String(),
ProjectInfoCacheSize: ProjectInfoCacheSize,
ProjectInfoCacheTTL: ProjectInfoCacheTTL.String(),
GatewayAddr: fmt.Sprintf("localhost:%d", RPCPort+portOffset),
},
Mongo: &mongo.Config{
ConnectionURI: MongoConnectionURI,
Expand Down

0 comments on commit 31fc46d

Please sign in to comment.