forked from yorkie-team/yorkie
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Coordinator with PubSub and LockerMap (yorkie-team#198)
- Loading branch information
1 parent
ffa2ded
commit a67af08
Showing
16 changed files
with
263 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2020 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package sync | ||
|
||
import ( | ||
"errors" | ||
gotime "time" | ||
|
||
"github.com/yorkie-team/yorkie/pkg/types" | ||
) | ||
|
||
var ( | ||
// ErrEmptyTopics is returned when the given topic is empty. | ||
ErrEmptyTopics = errors.New("empty topics") | ||
) | ||
|
||
// DocEvent represents events that occur related to the document. | ||
type DocEvent struct { | ||
Type types.EventType | ||
DocKey string | ||
Publisher types.Client | ||
} | ||
|
||
// AgentInfo represents the information of the Agent. | ||
type AgentInfo struct { | ||
ID string `json:"id"` | ||
Hostname string `json:"hostname"` | ||
RPCAddr string `json:"rpc_addr"` | ||
UpdatedAt gotime.Time `json:"updated_at"` | ||
} | ||
|
||
// Coordinator provides synchronization functions such as locks and event Pub/Sub. | ||
type Coordinator interface { | ||
LockerMap | ||
PubSub | ||
|
||
// Members returns the members of this cluster. | ||
Members() map[string]*AgentInfo | ||
|
||
// Close closes all resources of this Coordinator. | ||
Close() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2021 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package memory | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/moby/locker" | ||
|
||
"github.com/yorkie-team/yorkie/pkg/document/time" | ||
"github.com/yorkie-team/yorkie/pkg/types" | ||
"github.com/yorkie-team/yorkie/yorkie/backend/sync" | ||
) | ||
|
||
// Coordinator is a memory-based implementation of sync.Coordinator. | ||
type Coordinator struct { | ||
agentInfo *sync.AgentInfo | ||
|
||
locks *locker.Locker | ||
pubSub *PubSub | ||
} | ||
|
||
// NewCoordinator creates an instance of Coordinator. | ||
func NewCoordinator(agentInfo *sync.AgentInfo) *Coordinator { | ||
return &Coordinator{ | ||
agentInfo: agentInfo, | ||
locks: locker.New(), | ||
pubSub: NewPubSub(), | ||
} | ||
} | ||
|
||
// NewLocker creates locker of the given key. | ||
func (m *Coordinator) NewLocker( | ||
ctx context.Context, | ||
key sync.Key, | ||
) (sync.Locker, error) { | ||
return &internalLocker{ | ||
key.String(), | ||
m.locks, | ||
}, nil | ||
} | ||
|
||
// Subscribe subscribes to the given topics. | ||
func (m *Coordinator) Subscribe( | ||
subscriber types.Client, | ||
topics []string, | ||
) (*sync.Subscription, map[string][]types.Client, error) { | ||
return m.pubSub.Subscribe(subscriber, topics) | ||
} | ||
|
||
// Unsubscribe unsubscribes the given topics. | ||
func (m *Coordinator) Unsubscribe(topics []string, sub *sync.Subscription) { | ||
m.pubSub.Unsubscribe(topics, sub) | ||
} | ||
|
||
// Publish publishes the given event to the given Topic. | ||
func (m *Coordinator) Publish( | ||
publisherID *time.ActorID, | ||
topic string, | ||
event sync.DocEvent, | ||
) { | ||
m.pubSub.Publish(publisherID, topic, event) | ||
} | ||
|
||
// Members returns the members of this cluster. | ||
func (m *Coordinator) Members() map[string]*sync.AgentInfo { | ||
members := make(map[string]*sync.AgentInfo) | ||
members[m.agentInfo.ID] = m.agentInfo | ||
return members | ||
} | ||
|
||
// Close closes all resources of this Coordinator. | ||
func (m *Coordinator) Close() error { | ||
return nil | ||
} |
Oops, something went wrong.